Skip to content

Commit

Permalink
Fix alignment issue with default values for image and activity indicator
Browse files Browse the repository at this point in the history
also add test around preferred padding being respected
  • Loading branch information
dmiedema committed Aug 2, 2015
1 parent e32092f commit 68d2a58
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CRToast/CRToastView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
50 changes: 45 additions & 5 deletions Example/Tests/Unit Tests/CRToastViewTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {

Expand Down

0 comments on commit 68d2a58

Please sign in to comment.