Skip to content

Commit efe7488

Browse files
committed
Add comprehensive unit tests for SCLAlertView components and features
1 parent 555c48b commit efe7488

10 files changed

Lines changed: 444 additions & 0 deletions

SCLAlertView.xcodeproj/project.pbxproj

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
DDAA00032F00000100A11E57 /* SCLAlertViewComponentTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDAA00092F00000100A11E57 /* SCLAlertViewComponentTests.m */; };
3434
DDAA00042F00000100A11E57 /* SCLAlertViewPresentationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDAA000A2F00000100A11E57 /* SCLAlertViewPresentationTests.m */; };
3535
DDAA00052F00000100A11E57 /* SCLAlertViewBuilderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDAA000B2F00000100A11E57 /* SCLAlertViewBuilderTests.m */; };
36+
DDAA000C2F00000100A11E57 /* SCLAlertViewImageEffectsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDAA00102F00000100A11E57 /* SCLAlertViewImageEffectsTests.m */; };
37+
DDAA000D2F00000100A11E57 /* SCLAlertViewStyleKitTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDAA00112F00000100A11E57 /* SCLAlertViewStyleKitTests.m */; };
38+
DDAA000E2F00000100A11E57 /* SCLTimerDisplayTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDAA00122F00000100A11E57 /* SCLTimerDisplayTests.m */; };
39+
DDAA000F2F00000100A11E57 /* SCLAlertViewLifecycleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDAA00132F00000100A11E57 /* SCLAlertViewLifecycleTests.m */; };
40+
DDAA00142F00000100A11E57 /* SCLAlertViewPresentationWrapperTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDAA00152F00000100A11E57 /* SCLAlertViewPresentationWrapperTests.m */; };
41+
DDAA00162F00000100A11E57 /* SCLAlertViewLayoutAndKeyboardTests.m in Sources */ = {isa = PBXBuildFile; fileRef = DDAA00172F00000100A11E57 /* SCLAlertViewLayoutAndKeyboardTests.m */; };
3642
DDB15FD019D5B8BF00173158 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DDB15FCF19D5B8BF00173158 /* Images.xcassets */; };
3743
DDB15FD419D5B8DB00173158 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = DDB15FD319D5B8DB00173158 /* main.m */; };
3844
DDB15FD719D5B96500173158 /* SCLAlertView.m in Sources */ = {isa = PBXBuildFile; fileRef = DDB15FD619D5B96500173158 /* SCLAlertView.m */; };
@@ -98,6 +104,12 @@
98104
DDAA00092F00000100A11E57 /* SCLAlertViewComponentTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCLAlertViewComponentTests.m; sourceTree = "<group>"; };
99105
DDAA000A2F00000100A11E57 /* SCLAlertViewPresentationTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCLAlertViewPresentationTests.m; sourceTree = "<group>"; };
100106
DDAA000B2F00000100A11E57 /* SCLAlertViewBuilderTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCLAlertViewBuilderTests.m; sourceTree = "<group>"; };
107+
DDAA00102F00000100A11E57 /* SCLAlertViewImageEffectsTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCLAlertViewImageEffectsTests.m; sourceTree = "<group>"; };
108+
DDAA00112F00000100A11E57 /* SCLAlertViewStyleKitTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCLAlertViewStyleKitTests.m; sourceTree = "<group>"; };
109+
DDAA00122F00000100A11E57 /* SCLTimerDisplayTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCLTimerDisplayTests.m; sourceTree = "<group>"; };
110+
DDAA00132F00000100A11E57 /* SCLAlertViewLifecycleTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCLAlertViewLifecycleTests.m; sourceTree = "<group>"; };
111+
DDAA00152F00000100A11E57 /* SCLAlertViewPresentationWrapperTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCLAlertViewPresentationWrapperTests.m; sourceTree = "<group>"; };
112+
DDAA00172F00000100A11E57 /* SCLAlertViewLayoutAndKeyboardTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SCLAlertViewLayoutAndKeyboardTests.m; sourceTree = "<group>"; };
101113
DDB15FCF19D5B8BF00173158 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; name = Images.xcassets; path = SCLAlertViewExample/Images.xcassets; sourceTree = SOURCE_ROOT; };
102114
DDB15FD319D5B8DB00173158 /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = SCLAlertViewExample/main.m; sourceTree = SOURCE_ROOT; };
103115
DDB15FD619D5B96500173158 /* SCLAlertView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SCLAlertView.m; sourceTree = "<group>"; };
@@ -251,6 +263,12 @@
251263
DDAA00092F00000100A11E57 /* SCLAlertViewComponentTests.m */,
252264
DDAA000A2F00000100A11E57 /* SCLAlertViewPresentationTests.m */,
253265
DDAA000B2F00000100A11E57 /* SCLAlertViewBuilderTests.m */,
266+
DDAA00102F00000100A11E57 /* SCLAlertViewImageEffectsTests.m */,
267+
DDAA00112F00000100A11E57 /* SCLAlertViewStyleKitTests.m */,
268+
DDAA00122F00000100A11E57 /* SCLTimerDisplayTests.m */,
269+
DDAA00132F00000100A11E57 /* SCLAlertViewLifecycleTests.m */,
270+
DDAA00152F00000100A11E57 /* SCLAlertViewPresentationWrapperTests.m */,
271+
DDAA00172F00000100A11E57 /* SCLAlertViewLayoutAndKeyboardTests.m */,
254272
DDB15FBA19D5B7C600173158 /* Supporting Files */,
255273
);
256274
path = SCLAlertViewTests;
@@ -480,6 +498,12 @@
480498
DDAA00032F00000100A11E57 /* SCLAlertViewComponentTests.m in Sources */,
481499
DDAA00042F00000100A11E57 /* SCLAlertViewPresentationTests.m in Sources */,
482500
DDAA00052F00000100A11E57 /* SCLAlertViewBuilderTests.m in Sources */,
501+
DDAA000C2F00000100A11E57 /* SCLAlertViewImageEffectsTests.m in Sources */,
502+
DDAA000D2F00000100A11E57 /* SCLAlertViewStyleKitTests.m in Sources */,
503+
DDAA000E2F00000100A11E57 /* SCLTimerDisplayTests.m in Sources */,
504+
DDAA000F2F00000100A11E57 /* SCLAlertViewLifecycleTests.m in Sources */,
505+
DDAA00142F00000100A11E57 /* SCLAlertViewPresentationWrapperTests.m in Sources */,
506+
DDAA00162F00000100A11E57 /* SCLAlertViewLayoutAndKeyboardTests.m in Sources */,
483507
);
484508
runOnlyForDeploymentPostprocessing = 0;
485509
};

SCLAlertViewTests/SCLAlertViewButtonTests.m

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,16 @@ @interface SCLAlertViewButtonTests : SCLAlertViewTestCase
1313

1414
@implementation SCLAlertViewButtonTests
1515

16+
- (void)testDefaultInitializerConfiguresButton
17+
{
18+
SCLButton *button = [[SCLButton alloc] init];
19+
20+
XCTAssertEqualWithAccuracy(button.frame.size.width, 216.0f, 0.001f);
21+
XCTAssertEqualWithAccuracy(button.frame.size.height, 35.0f, 0.001f);
22+
XCTAssertEqual(button.titleLabel.lineBreakMode, NSLineBreakByWordWrapping);
23+
XCTAssertEqualWithAccuracy(button.layer.cornerRadius, 3.0f, 0.001f);
24+
}
25+
1626
- (void)testButtonActionBlockRunsWhenTapped
1727
{
1828
SCLAlertView *alertView = [[SCLAlertView alloc] init];
@@ -92,6 +102,15 @@ - (void)testButtonParseConfigAppliesSupportedKeys
92102
XCTAssertEqualObjects(button.titleLabel.font, font);
93103
}
94104

105+
- (void)testButtonParseConfigAppliesBorderWidthWithoutBorderColor
106+
{
107+
SCLButton *button = [[SCLButton alloc] initWithWindowWidth:240.0f];
108+
109+
[button parseConfig:@{@"borderWidth": @3.0f}];
110+
111+
XCTAssertEqualWithAccuracy(button.layer.borderWidth, 3.0f, 0.001f);
112+
}
113+
95114
- (void)testButtonHighlightUsesDarkerDefaultBackgroundColor
96115
{
97116
SCLButton *button = [[SCLButton alloc] initWithWindowWidth:240.0f];

SCLAlertViewTests/SCLAlertViewComponentTests.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ @interface SCLAlertViewComponentTests : SCLAlertViewTestCase
1515

1616
@implementation SCLAlertViewComponentTests
1717

18+
- (void)testSwitchDefaultInitializerAndLayout
19+
{
20+
SCLSwitchView *switchView = [[SCLSwitchView alloc] init];
21+
switchView.frame = CGRectMake(0.0f, 0.0f, 180.0f, 40.0f);
22+
switchView.labelText = @"Enabled";
23+
24+
[switchView layoutSubviews];
25+
26+
XCTAssertEqualObjects(switchView.backgroundColor, UIColor.clearColor);
27+
XCTAssertGreaterThan([switchView sizeThatFits:CGSizeMake(180.0f, 0.0f)].width, 0.0f);
28+
}
29+
1830
- (void)testAddTextFieldConfiguresTextAndReturnKeys
1931
{
2032
SCLAlertView *alertView = [[SCLAlertView alloc] init];
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
//
2+
// SCLAlertViewImageEffectsTests.m
3+
// SCLAlertViewTests
4+
//
5+
// Created by Diogo Autilio on 9/26/14.
6+
// Copyright (c) 2014-2016 AnyKey Entertainment. All rights reserved.
7+
//
8+
9+
#import "SCLAlertViewTestHelpers.h"
10+
#import "UIImage+ImageEffects.h"
11+
12+
@interface SCLAlertViewImageEffectsTests : SCLAlertViewTestCase
13+
@end
14+
15+
@implementation SCLAlertViewImageEffectsTests
16+
17+
- (UIImage *)testImage
18+
{
19+
CGSize size = CGSizeMake(8.0f, 8.0f);
20+
UIGraphicsBeginImageContextWithOptions(size, YES, 1.0f);
21+
[UIColor.redColor setFill];
22+
UIRectFill(CGRectMake(0.0f, 0.0f, 4.0f, 8.0f));
23+
[UIColor.blueColor setFill];
24+
UIRectFill(CGRectMake(4.0f, 0.0f, 4.0f, 8.0f));
25+
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
26+
UIGraphicsEndImageContext();
27+
return image;
28+
}
29+
30+
- (void)assertImage:(UIImage *)image matchesSize:(CGSize)size
31+
{
32+
XCTAssertNotNil(image);
33+
XCTAssertEqualWithAccuracy(image.size.width, size.width, 0.001f);
34+
XCTAssertEqualWithAccuracy(image.size.height, size.height, 0.001f);
35+
}
36+
37+
- (void)testImageWithColorCreatesOnePointImage
38+
{
39+
UIImage *image = [UIImage imageWithColor:UIColor.greenColor];
40+
41+
[self assertImage:image matchesSize:CGSizeMake(1.0f, 1.0f)];
42+
}
43+
44+
- (void)testConvertViewToImageHandlesNilAndInvalidViews
45+
{
46+
XCTAssertNil([UIImage convertViewToImage:nil]);
47+
48+
UIView *zeroSizeView = [[UIView alloc] initWithFrame:CGRectZero];
49+
XCTAssertNil([UIImage convertViewToImage:zeroSizeView]);
50+
}
51+
52+
- (void)testConvertViewToImageCapturesValidView
53+
{
54+
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 20.0f, 10.0f)];
55+
view.backgroundColor = UIColor.orangeColor;
56+
57+
UIImage *image = [UIImage convertViewToImage:view];
58+
59+
[self assertImage:image matchesSize:view.bounds.size];
60+
}
61+
62+
- (void)testPresetEffectsReturnImages
63+
{
64+
UIImage *image = [self testImage];
65+
66+
[self assertImage:[image applyLightEffect] matchesSize:image.size];
67+
[self assertImage:[image applyExtraLightEffect] matchesSize:image.size];
68+
[self assertImage:[image applyDarkEffect] matchesSize:image.size];
69+
}
70+
71+
- (void)testTintEffectSupportsRGBAndWhiteColors
72+
{
73+
UIImage *image = [self testImage];
74+
75+
[self assertImage:[image applyTintEffectWithColor:UIColor.purpleColor] matchesSize:image.size];
76+
[self assertImage:[image applyTintEffectWithColor:UIColor.whiteColor] matchesSize:image.size];
77+
}
78+
79+
- (void)testBlurSupportsNoBlurTintOnlyAndMask
80+
{
81+
UIImage *image = [self testImage];
82+
UIImage *mask = [UIImage imageWithColor:UIColor.blackColor];
83+
84+
[self assertImage:[image applyBlurWithRadius:0.0f
85+
tintColor:UIColor.yellowColor
86+
saturationDeltaFactor:1.0f
87+
maskImage:nil] matchesSize:image.size];
88+
[self assertImage:[image applyBlurWithRadius:2.0f
89+
tintColor:nil
90+
saturationDeltaFactor:1.2f
91+
maskImage:mask] matchesSize:image.size];
92+
}
93+
94+
@end
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// SCLAlertViewLayoutAndKeyboardTests.m
3+
// SCLAlertViewTests
4+
//
5+
// Created by Diogo Autilio on 9/26/14.
6+
// Copyright (c) 2014-2016 AnyKey Entertainment. All rights reserved.
7+
//
8+
9+
#import "SCLAlertViewTestHelpers.h"
10+
11+
@interface SCLAlertViewLayoutAndKeyboardTests : SCLAlertViewTestCase
12+
@end
13+
14+
@implementation SCLAlertViewLayoutAndKeyboardTests
15+
16+
- (void)testLargerIconLayoutWithHorizontalButtons
17+
{
18+
SCLAlertView *alertView = [[SCLAlertView alloc] initWithWidth:280.0f];
19+
alertView.useLargerIcon = YES;
20+
alertView.horizontalButtons = YES;
21+
[alertView addButton:@"One" actionBlock:^{
22+
}];
23+
[alertView addButton:@"Two" actionBlock:^{
24+
}];
25+
26+
[alertView showSuccess:[self presentingViewController] title:@"Title" subTitle:@"Body" closeButtonTitle:nil duration:0.0];
27+
[alertView viewWillLayoutSubviews];
28+
29+
NSArray<SCLButton *> *buttons = [self buttonsInView:alertView.view];
30+
XCTAssertEqual(buttons.count, 2U);
31+
XCTAssertLessThan(buttons.firstObject.frame.origin.x, buttons.lastObject.frame.origin.x);
32+
33+
[alertView hideView];
34+
}
35+
36+
- (void)testKeyboardNotificationsMoveAndRestoreAlertContent
37+
{
38+
UIViewController *viewController = [self presentingViewController];
39+
SCLAlertView *alertView = [[SCLAlertView alloc] init];
40+
[alertView addTextField:@"Name" setDefaultText:nil];
41+
[alertView showSuccess:viewController title:@"Title" subTitle:@"Body" closeButtonTitle:nil duration:0.0];
42+
[alertView viewWillLayoutSubviews];
43+
44+
NSDictionary *showUserInfo = @{
45+
UIKeyboardAnimationDurationUserInfoKey: @0.0,
46+
UIKeyboardAnimationCurveUserInfoKey: @(UIViewAnimationCurveLinear),
47+
UIKeyboardFrameEndUserInfoKey: [NSValue valueWithCGRect:CGRectMake(0.0f, 120.0f, 320.0f, 360.0f)]
48+
};
49+
NSNotification *showNotification = [NSNotification notificationWithName:UIKeyboardWillShowNotification
50+
object:nil
51+
userInfo:showUserInfo];
52+
NSNotification *hideNotification = [NSNotification notificationWithName:UIKeyboardWillHideNotification
53+
object:nil
54+
userInfo:showUserInfo];
55+
56+
XCTAssertNoThrow([alertView keyboardWillShow:showNotification]);
57+
XCTAssertNoThrow([alertView keyboardWillHide:hideNotification]);
58+
59+
[alertView hideView];
60+
}
61+
62+
@end
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// SCLAlertViewLifecycleTests.m
3+
// SCLAlertViewTests
4+
//
5+
// Created by Diogo Autilio on 9/26/14.
6+
// Copyright (c) 2014-2016 AnyKey Entertainment. All rights reserved.
7+
//
8+
9+
#import "SCLAlertViewTestHelpers.h"
10+
#import "SCLAlertView+WindowResolver.h"
11+
12+
@interface SCLAlertViewLifecycleTests : SCLAlertViewTestCase
13+
@end
14+
15+
@implementation SCLAlertViewLifecycleTests
16+
17+
- (void)testUnsupportedCoderInitializerThrows
18+
{
19+
NSCoder *coder = (NSCoder *)[NSNull null];
20+
XCTAssertThrows([[SCLAlertView alloc] initWithCoder:coder]);
21+
}
22+
23+
- (void)testInitializersAndStatusBarAccessors
24+
{
25+
SCLAlertView *widthAlert = [[SCLAlertView alloc] initWithWindowWidth:280.0f];
26+
SCLAlertView *newWindowAlert = [[SCLAlertView alloc] initWithNewWindowWidth:260.0f];
27+
28+
widthAlert.statusBarHidden = YES;
29+
widthAlert.statusBarStyle = UIStatusBarStyleLightContent;
30+
31+
XCTAssertNotNil(widthAlert.view);
32+
XCTAssertNotNil(newWindowAlert.view);
33+
XCTAssertTrue(widthAlert.prefersStatusBarHidden);
34+
XCTAssertEqual(widthAlert.preferredStatusBarStyle, UIStatusBarStyleLightContent);
35+
XCTAssertNotNil([SCLAlertView alertWithNewWindow]);
36+
}
37+
38+
- (void)testWindowResolverReturnsResolvedViewOrFallback
39+
{
40+
UIView *fallback = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 10.0f, 10.0f)];
41+
UIView *resolved = [SCLAlertView scl_resolveAppViewWithFallback:fallback];
42+
43+
XCTAssertNotNil(resolved);
44+
}
45+
46+
@end
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//
2+
// SCLAlertViewPresentationWrapperTests.m
3+
// SCLAlertViewTests
4+
//
5+
// Created by Diogo Autilio on 9/26/14.
6+
// Copyright (c) 2014-2016 AnyKey Entertainment. All rights reserved.
7+
//
8+
9+
#import "SCLAlertViewTestHelpers.h"
10+
#import "SCLAlertViewStyleKit.h"
11+
12+
@interface SCLAlertViewPresentationWrapperTests : SCLAlertViewTestCase
13+
@end
14+
15+
@implementation SCLAlertViewPresentationWrapperTests
16+
17+
- (void)testEditCustomAndWaitingShowWrappers
18+
{
19+
UIViewController *viewController = [self presentingViewController];
20+
21+
SCLAlertView *editAlert = [[SCLAlertView alloc] init];
22+
[editAlert showEdit:viewController title:@"Edit" subTitle:@"Body" closeButtonTitle:nil duration:0.0];
23+
XCTAssertEqualObjects(editAlert.labelTitle.text, @"Edit");
24+
[editAlert hideView];
25+
26+
SCLAlertView *customAlert = [[SCLAlertView alloc] init];
27+
[customAlert showCustom:viewController
28+
image:SCLAlertViewStyleKit.imageOfEdit
29+
color:UIColor.magentaColor
30+
title:@"Custom"
31+
subTitle:@"Body"
32+
closeButtonTitle:nil
33+
duration:0.0];
34+
XCTAssertEqualObjects(customAlert.labelTitle.text, @"Custom");
35+
[customAlert hideView];
36+
37+
SCLAlertView *waitingAlert = [[SCLAlertView alloc] init];
38+
[waitingAlert showWaiting:viewController title:@"Wait" subTitle:@"Body" closeButtonTitle:nil duration:0.0];
39+
XCTAssertNotNil(waitingAlert.activityIndicatorView);
40+
[waitingAlert hideView];
41+
}
42+
43+
- (void)testTransparentBackgroundAndTapOutsideDismissalConfiguration
44+
{
45+
SCLAlertView *alertView = [[SCLAlertView alloc] init];
46+
alertView.backgroundViewColor = UIColor.cyanColor;
47+
alertView.backgroundType = SCLAlertViewBackgroundTransparent;
48+
alertView.shouldDismissOnTapOutside = YES;
49+
50+
[alertView showSuccess:[self presentingViewController] title:@"Title" subTitle:@"Body" closeButtonTitle:nil duration:0.0];
51+
52+
XCTAssertTrue(alertView.shouldDismissOnTapOutside);
53+
XCTAssertNoThrow([alertView handleTap:[[UITapGestureRecognizer alloc] init]]);
54+
[alertView hideView];
55+
}
56+
57+
@end

0 commit comments

Comments
 (0)