From 68d2a58275095e33196bc3ca92c1c2773f241866 Mon Sep 17 00:00:00 2001 From: dmiedema Date: Sat, 1 Aug 2015 20:54:52 -0700 Subject: [PATCH] Fix alignment issue with default values for image and activity indicator also add test around preferred padding being respected --- CRToast/CRToastView.m | 6 +++ Example/Tests/Unit Tests/CRToastViewTests.m | 50 ++++++++++++++++++--- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/CRToast/CRToastView.m b/CRToast/CRToastView.m index c889ca0c..b4d03d1c 100644 --- a/CRToast/CRToastView.m +++ b/CRToast/CRToastView.m @@ -238,6 +238,12 @@ - (void)layoutSubviews { // Get the smallest X value so our image/activity indicator doesn't cover any thing CGFloat smallestXView = MIN(CGRectGetMinX(self.label.frame), CGRectGetMinX(self.subtitleLabel.frame)); + // If both our labels have 0 width (empty text) don't change the centers of our + // image or activity indicator and just move along + if (CGRectGetWidth(self.label.frame) == 0.0 + && CGRectGetWidth(self.subtitleLabel.frame) == 0.0) { + return; + } // Move our image if that is what we're showing if (showingImage && self.toast.imageAlignment == CRToastAccessoryViewAlignmentCenter) { self.imageView.frame = (CGRect) { diff --git a/Example/Tests/Unit Tests/CRToastViewTests.m b/Example/Tests/Unit Tests/CRToastViewTests.m index dc206593..83d978f3 100644 --- a/Example/Tests/Unit Tests/CRToastViewTests.m +++ b/Example/Tests/Unit Tests/CRToastViewTests.m @@ -48,13 +48,11 @@ - (void)testImageFrameCenterAlignment { toast.options = options; self.view.toast = toast; - CGRect assumedRect = CGRectMake(100, 0, 100, 100); - [self.view layoutSubviews]; - BOOL rectsEqual = CGRectEqualToRect(assumedRect, self.view.imageView.frame); + BOOL centersEqual = CGPointEqualToPoint(self.view.center, self.view.imageView.center); - XCTAssertTrue(rectsEqual, @"center aligned rect should be equal to assumed rect. Intead was %@", NSStringFromCGRect(self.view.imageView.frame)); + XCTAssertTrue(centersEqual, @"center of image view should be center of self.view (%@). Intead was %@", NSStringFromCGPoint(self.view.center), NSStringFromCGPoint(self.view.imageView.center)); } - (void)testImageFrameRightAlignment { @@ -113,7 +111,7 @@ - (void)testActivityFrameCenterAlignment { BOOL centersEqual = CGPointEqualToPoint(assumedCenter, self.view.activityIndicator.center); - XCTAssertTrue(centersEqual, @"center aligned activity indicator center shold equal assumed center. Instead was %@", NSStringFromCGPoint(self.view.activityIndicator.center)); + XCTAssertTrue(centersEqual, @"center aligned activity indicator center shold equal assumed center (%@). Instead was %@", NSStringFromCGPoint(self.view.center), NSStringFromCGPoint(self.view.activityIndicator.center)); } - (void)testActivityFrameRightAlignment { @@ -136,6 +134,48 @@ - (void)testActivityFrameRightAlignment { XCTAssertTrue(centersEqual, @"right aligned activity indicator center shold equal assumed center. Instead was %@", NSStringFromCGPoint(self.view.activityIndicator.center)); } +#pragma mark Padding + +- (void)testPreferredPaddingSetToZero { + CRToast *toast = __TestToast(); + NSMutableDictionary *options = [NSMutableDictionary dictionary]; + + options[kCRToastImageKey] = [UIImage imageNamed:@"alert_icon"]; + options[kCRToastImageAlignmentKey] = @(CRToastAccessoryViewAlignmentLeft); + options[kCRToastTextKey] = @"Test"; + options[kCRToastTextAlignmentKey] = @(NSTextAlignmentLeft); + options[kCRToastNotificationPreferredPaddingKey] = @0; + + toast.options = options; + self.view.toast = toast; + + [self.view layoutSubviews]; + + CGFloat imageViewX = CGRectGetMinX(self.view.imageView.frame); + + XCTAssertTrue(imageViewX == 0.0, @"With no padding minX should be 0.0. Instead was %f", imageViewX); +} + +- (void)testPrefferedPaddingIsRespectedWhenHigher { + CRToast *toast = __TestToast(); + NSMutableDictionary *options = [NSMutableDictionary dictionary]; + + options[kCRToastShowActivityIndicatorKey] = @YES; + options[kCRToastActivityIndicatorAlignmentKey] = @(CRToastAccessoryViewAlignmentLeft); + options[kCRToastTextKey] = @"Test"; + options[kCRToastTextAlignmentKey] = @(NSTextAlignmentLeft); + options[kCRToastNotificationPreferredPaddingKey] = @20; + + toast.options = options; + self.view.toast = toast; + + [self.view layoutSubviews]; + + CGFloat imageViewX = CGRectGetMinX(self.view.imageView.frame); + + XCTAssertTrue(imageViewX == 20.0, @"With no padding minX should be 20.0. Instead was %f", imageViewX); +} + #pragma mark Width Calculations - (void)testWidthWithOnlyLeftItem {