18
18
#import " MaterialButtons.h"
19
19
#import " MDCShadowElevations.h"
20
20
21
- static const NSUInteger kNumberOfRepeats = 20 ;
22
- static const CGFloat kEpsilonAccuracy = 0 .0001f ;
21
+ // A value greater than the largest value created by combining normal values of UIControlState.
22
+ // This is a complete hack, but UIControlState doesn't expose anything useful here.
23
+ // This assumes that UIControlState is actually a set of bitfields and ignores application-specific
24
+ // values.
25
+ static const UIControlState kNumUIControlStates = 2 * UIControlStateSelected - 1 ;
23
26
24
27
static inline UIColor *MDCColorFromRGB (NSInteger rgbValue) {
25
28
return [UIColor colorWithRed: ((float )((rgbValue & 0xFF0000 ) >> 16 )) / 255.0
28
31
alpha: 1.0 ];
29
32
}
30
33
31
- @interface ButtonsTests : XCTestCase
34
+ static CGFloat randomNumber () {
35
+ return arc4random_uniform (100 ) / (CGFloat)10 ;
36
+ }
32
37
38
+ static CGFloat randomNumberNotEqualTo (const CGFloat targetNumber) {
39
+ while (1 ) {
40
+ CGFloat number = randomNumber ();
41
+ if (number != targetNumber) {
42
+ return number;
43
+ }
44
+ }
45
+ }
46
+
47
+ static UIColor *randomColor () {
48
+ switch (arc4random_uniform (5 )) {
49
+ case 0 :
50
+ return [UIColor whiteColor ];
51
+ break ;
52
+ case 1 :
53
+ return [UIColor blackColor ];
54
+ break ;
55
+ case 2 :
56
+ return [UIColor redColor ];
57
+ break ;
58
+ case 3 :
59
+ return [UIColor orangeColor ];
60
+ break ;
61
+ case 4 :
62
+ return [UIColor greenColor ];
63
+ break ;
64
+ default :
65
+ return [UIColor blueColor ];
66
+ break ;
67
+ }
68
+ }
69
+
70
+ @interface ButtonsTests : XCTestCase
33
71
@end
34
72
35
73
@implementation ButtonsTests
@@ -74,118 +112,91 @@ - (void)testUppercaseTitleNoChangedToYes {
74
112
XCTAssertEqualObjects (button.currentTitle , [originalTitle uppercaseStringWithLocale: [NSLocale currentLocale ]]);
75
113
}
76
114
77
- - (void )testSetEnabledAnimatedNo {
115
+ - (void )testSetEnabledAnimated {
78
116
// Given
79
117
MDCButton *button = [[MDCButton alloc ] init ];
80
118
81
- // When
82
- [button setEnabled: NO animated: arc4random_uniform (2 )];
119
+ NSArray *boolValues = @[ @YES , @NO ];
120
+ for (id enabled in boolValues) {
121
+ for (id animated in boolValues) {
122
+ // When
123
+ [button setEnabled: [enabled boolValue ] animated: [animated boolValue ]];
83
124
84
- // Then
85
- XCTAssertFalse (button.enabled );
125
+ // Then
126
+ XCTAssertEqual (button.enabled , [enabled boolValue ]);
127
+ }
128
+ }
86
129
}
87
130
88
- - (void )testSetEnabledAnimatedYES {
131
+ - (void )testElevationForState {
89
132
// Given
90
133
MDCButton *button = [[MDCButton alloc ] init ];
91
- button.enabled = NO ;
92
134
93
- // When
94
- [button setEnabled: YES animated: arc4random_uniform (2 )];
135
+ for (NSUInteger controlState = 0 ; controlState < kNumUIControlStates ; ++controlState) {
136
+ // And given
137
+ CGFloat elevation = randomNumber ();
95
138
96
- // Then
97
- XCTAssertTrue (button.enabled );
139
+ // When
140
+ [button setElevation: elevation forState: controlState];
141
+
142
+ // Then
143
+ XCTAssertEqual ([button elevationForState: controlState], elevation);
144
+ }
98
145
}
99
146
100
- - (void )testElevationForState {
147
+ - (void )testResetElevationForState {
101
148
// Given
102
149
MDCButton *button = [[MDCButton alloc ] init ];
103
- UIControlState controlState = [self randomControlState ];
104
- CGFloat elevation = [self randomNumber ];
105
150
106
- // When
107
- [button setElevation: elevation forState: controlState];
108
-
109
- // Then
110
- XCTAssertEqualWithAccuracy ([button elevationForState: controlState], elevation, kEpsilonAccuracy );
111
- }
112
-
113
- - (void )testResetElevationForState {
114
- for (int ii = 0 ; ii < kNumberOfRepeats ; ++ii) {
115
- // Given
116
- MDCButton *button = [[MDCButton alloc ] init ];
117
- UIControlState controlState = [self randomControlState ];
118
- CGFloat elevation = [self randomNumber ];
119
- [button setElevation: elevation forState: controlState];
151
+ for (NSUInteger controlState = 0 ; controlState < kNumUIControlStates ; ++controlState) {
152
+ // And given
153
+ CGFloat defaultValue = [button elevationForState: controlState];
120
154
121
155
// When
156
+ [button setElevation: randomNumberNotEqualTo (defaultValue) forState: controlState];
122
157
[button resetElevationForState: controlState];
123
158
124
159
// Then
125
- if (controlState & UIControlStateSelected) {
126
- XCTAssertNotEqualWithAccuracy ([button elevationForState: controlState], MDCShadowElevationNone,
127
- kEpsilonAccuracy );
128
- } else {
129
- XCTAssertEqualWithAccuracy ([button elevationForState: controlState], MDCShadowElevationNone,
130
- kEpsilonAccuracy );
131
- }
160
+ XCTAssertEqual ([button elevationForState: controlState], defaultValue);
132
161
}
133
162
}
134
163
135
164
- (void )testBackgroundColorForState {
136
165
// Given
137
166
MDCButton *button = [[MDCButton alloc ] init ];
138
- UIControlState controlState = [self randomControlState ];
139
- UIColor *color = [self randomColor ];
140
167
141
- // When
142
- [button setBackgroundColor: color forState: controlState];
143
-
144
- // Then
145
- XCTAssertEqualObjects ([button backgroundColorForState: controlState], color);
146
- }
147
-
148
- - (void )testCurrentBackgroundColor {
149
- for (int ii = 0 ; ii < kNumberOfRepeats ; ++ii) {
150
- // Given
151
- MDCButton *button = [[MDCButton alloc ] init ];
152
- UIControlState controlState = [self randomControlState ];
153
- UIColor *color = [self randomColor ];
154
- [button setBackgroundColor: color forState: controlState];
168
+ for (NSUInteger controlState = 0 ; controlState < kNumUIControlStates ; ++controlState) {
169
+ // And given
170
+ UIColor *color = randomColor ();
155
171
156
172
// When
157
- button.highlighted = (controlState & UIControlStateHighlighted) == UIControlStateHighlighted;
158
- button.selected = (controlState & UIControlStateSelected) == UIControlStateSelected;
159
- button.enabled = (controlState & UIControlStateDisabled) != UIControlStateDisabled;
160
- NSLog (@" controlstate:%lu " , (unsigned long )button.state );
173
+ [button setBackgroundColor: color forState: controlState];
161
174
162
175
// Then
163
- XCTAssertEqual (button.state , controlState);
164
- XCTAssertEqualObjects ([button backgroundColorForState: button.state], color);
176
+ XCTAssertEqualObjects ([button backgroundColorForState: controlState], color);
165
177
}
166
178
}
167
179
168
- - (void )testCurrentBackgroundColorFallbackToNormal {
180
+ - (void )testCurrentBackgroundColor {
169
181
// Given
170
182
MDCButton *button = [[MDCButton alloc ] init ];
171
- UIControlState controlState = [self randomControlState ];
172
- UIColor *color = [self randomColor ];
173
- [button setBackgroundColor: color forState: UIControlStateNormal];
174
183
175
- // When
176
- button.highlighted = (controlState & UIControlStateHighlighted) == UIControlStateHighlighted;
177
- button.selected = (controlState & UIControlStateSelected) == UIControlStateSelected;
178
- button.enabled = (controlState & UIControlStateDisabled) != UIControlStateDisabled;
184
+ for (NSUInteger controlState = 0 ; controlState < kNumUIControlStates ; ++controlState) {
185
+ // And given
186
+ UIColor *color = randomColor ();
179
187
180
- // Then
181
- XCTAssertEqual (button.state , controlState);
182
- XCTAssertEqualObjects ([button backgroundColorForState: button.state], color);
188
+ // When
189
+ [button setBackgroundColor: color forState: controlState];
190
+
191
+ // Then
192
+ XCTAssertEqualObjects ([button backgroundColorForState: controlState], color);
193
+ }
183
194
}
184
195
185
196
- (void )testInkColors {
186
197
// Given
187
198
MDCButton *button = [[MDCButton alloc ] init ];
188
- UIColor *color = [ self randomColor ] ;
199
+ UIColor *color = randomColor () ;
189
200
190
201
// When
191
202
button.inkColor = color;
@@ -202,53 +213,7 @@ - (void)testInkColors {
202
213
- underlyingColor (text color)
203
214
*/
204
215
205
- #pragma mark private test helpers
206
-
207
- - (UIControlState)randomControlState {
208
- if (arc4random_uniform (2 )) {
209
- return UIControlStateNormal; // Test normal the most.
210
- }
211
- if (arc4random_uniform (2 )) {
212
- return UIControlStateSelected; // Test selected the second most.
213
- }
214
- // Everything else including overlapping states.
215
- return arc4random_uniform (UIControlStateDisabled | UIControlStateHighlighted |
216
- UIControlStateSelected + 1 );
217
- // UIButton *button = [[UIButton alloc] init];
218
- // button.enabled = arc4random_uniform(2);
219
- // button.highlighted = arc4random_uniform(2);
220
- // button.selected = arc4random_uniform(2);
221
- // return button.state;
222
- }
223
-
224
- - (CGFloat)randomNumber {
225
- return arc4random_uniform (1000 ) / (CGFloat)(arc4random_uniform (9 ) + 1 );
226
- }
227
-
228
- - (UIColor *)randomColor {
229
- switch (arc4random_uniform (5 )) {
230
- case 0 :
231
- return [UIColor whiteColor ];
232
- break ;
233
- case 1 :
234
- return [UIColor blackColor ];
235
- break ;
236
- case 2 :
237
- return [UIColor redColor ];
238
- break ;
239
- case 3 :
240
- return [UIColor orangeColor ];
241
- break ;
242
- case 4 :
243
- return [UIColor greenColor ];
244
- break ;
245
- default :
246
- return [UIColor blueColor ];
247
- break ;
248
- }
249
- }
250
-
251
- #pragma mark - tests exploring UIButton state changes
216
+ #pragma mark - UIButton state changes
252
217
253
218
- (void )testEnabled {
254
219
// Given
@@ -348,21 +313,4 @@ - (void)testUnselected {
348
313
XCTAssertFalse (button.state & UIControlStateSelected);
349
314
}
350
315
351
- - (void )testButtonState {
352
- for (int ii = 0 ; ii < kNumberOfRepeats ; ++ii) {
353
- // Given
354
- UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
355
- UIControlState controlState = [self randomControlState ];
356
- // When
357
- button.highlighted = (controlState & UIControlStateHighlighted) == UIControlStateHighlighted;
358
- button.selected = (controlState & UIControlStateSelected) == UIControlStateSelected;
359
- button.enabled = (controlState & UIControlStateDisabled) != UIControlStateDisabled;
360
-
361
- // Then
362
- NSLog (@" controlstate:%lu ==%lu highlight:%i enabled:%i selected:%i " , (unsigned long )controlState,
363
- (unsigned long )button.state , button.highlighted , button.enabled , button.selected );
364
- XCTAssertEqual (button.state , controlState);
365
- }
366
- }
367
-
368
316
@end
0 commit comments