-
Notifications
You must be signed in to change notification settings - Fork 946
/
ButtonsTests.m
1109 lines (895 loc) · 36.3 KB
/
ButtonsTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016-present the Material Components for iOS authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#import <XCTest/XCTest.h>
#import "MaterialButtons.h"
#import "MaterialShadowElevations.h"
#import "MaterialShapes.h"
#import "MaterialShadowLayer.h"
#import "MaterialTypography.h"
static const CGFloat kEpsilonAccuracy = (CGFloat)0.001;
// A value greater than the largest value created by combining normal values of UIControlState.
// This is a complete hack, but UIControlState doesn't expose anything useful here.
// This assumes that UIControlState is actually a set of bitfields and ignores application-specific
// values.
static const UIControlState kNumUIControlStates = 2 * UIControlStateSelected - 1;
static const UIControlState kUIControlStateDisabledHighlighted =
UIControlStateHighlighted | UIControlStateDisabled;
static CGFloat randomNumber() {
return arc4random_uniform(100) / (CGFloat)10;
}
static CGFloat randomNumberNotEqualTo(const CGFloat targetNumber) {
while (1) {
CGFloat number = randomNumber();
if (number != targetNumber) {
return number;
}
}
}
static UIColor *randomColor() {
switch (arc4random_uniform(5)) {
case 0:
return [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
break;
case 1:
return [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
break;
case 2:
return [UIColor redColor];
break;
case 3:
return [UIColor orangeColor];
break;
case 4:
return [UIColor greenColor];
break;
default:
return [UIColor blueColor];
break;
}
}
static NSString *controlStateDescription(UIControlState controlState) {
if (controlState == UIControlStateNormal) {
return @"Normal";
}
NSMutableString *string = [NSMutableString string];
if ((UIControlStateHighlighted & controlState) == UIControlStateHighlighted) {
[string appendString:@"Highlighted "];
}
if ((UIControlStateDisabled & controlState) == UIControlStateDisabled) {
[string appendString:@"Disabled "];
}
if ((UIControlStateSelected & controlState) == UIControlStateSelected) {
[string appendString:@"Selected "];
}
return [string copy];
}
@interface FakeShadowLayer : MDCShapedShadowLayer
@property(nonatomic, assign) NSInteger elevationAssignmentCount;
@end
@implementation FakeShadowLayer
- (void)setElevation:(MDCShadowElevation)elevation {
++self.elevationAssignmentCount;
[super setElevation:elevation];
}
@end
@interface TestButton : MDCButton
@property(nonatomic, strong) FakeShadowLayer *shadowLayer;
@end
@implementation TestButton
+ (Class)layerClass {
return [FakeShadowLayer class];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
_shadowLayer = (FakeShadowLayer *)self.layer;
}
return self;
}
@end
@interface ButtonsTests : XCTestCase
@end
@implementation ButtonsTests
- (void)testUppercaseTitleYes {
// Given
MDCButton *button = [[MDCButton alloc] init];
NSString *originalTitle = @"some Text";
// When
button.uppercaseTitle = YES;
[button setTitle:originalTitle forState:UIControlStateNormal];
// Then
XCTAssertEqualObjects(button.currentTitle,
[originalTitle uppercaseStringWithLocale:[NSLocale currentLocale]]);
}
- (void)testUppercaseTitleNo {
// Given
MDCButton *button = [[MDCButton alloc] init];
NSString *originalTitle = @"some Text";
// When
button.uppercaseTitle = NO;
[button setTitle:originalTitle forState:UIControlStateNormal];
// Then
XCTAssertEqualObjects(button.currentTitle, originalTitle);
}
- (void)testUppercaseTitleNoChangedToYes {
// Given
MDCButton *button = [[MDCButton alloc] init];
NSString *originalTitle = @"some Text";
// When
button.uppercaseTitle = NO;
[button setTitle:originalTitle forState:UIControlStateNormal];
[button setTitle:originalTitle forState:UIControlStateHighlighted];
[button setTitle:originalTitle forState:UIControlStateDisabled];
button.uppercaseTitle = YES;
// Then
XCTAssertEqualObjects(button.currentTitle,
[originalTitle uppercaseStringWithLocale:[NSLocale currentLocale]]);
}
- (void)testUppercaseTitleYesChangedToNo {
// Given
MDCButton *button = [[MDCButton alloc] init];
NSString *originalTitle = @"some Text";
// When
button.uppercaseTitle = YES;
[button setTitle:originalTitle forState:UIControlStateNormal];
[button setTitle:originalTitle forState:UIControlStateHighlighted];
[button setTitle:originalTitle forState:UIControlStateDisabled];
button.uppercaseTitle = NO;
// Then
XCTAssertEqualObjects(button.currentTitle, originalTitle);
}
- (void)testSetEnabledAnimated {
// Given
MDCButton *button = [[MDCButton alloc] init];
NSArray *boolValues = @[ @YES, @NO ];
for (id enabled in boolValues) {
for (id animated in boolValues) {
// When
[button setEnabled:[enabled boolValue] animated:[animated boolValue]];
// Then
XCTAssertEqual(button.enabled, [enabled boolValue]);
}
}
}
- (void)testElevationForState {
// Given
MDCButton *button = [[MDCButton alloc] init];
for (NSUInteger controlState = 0; controlState < kNumUIControlStates; ++controlState) {
// And given
CGFloat elevation = randomNumber();
// When
[button setElevation:elevation forState:controlState];
// Then
XCTAssertEqual([button elevationForState:controlState], elevation);
}
}
- (void)testSetElevationOnlyUpdatesCurrentState {
// Given
TestButton *selectedButton = [[TestButton alloc] init];
TestButton *highlightedButton = [[TestButton alloc] init];
XCTAssertEqualWithAccuracy([selectedButton elevationForState:UIControlStateNormal],
[selectedButton elevationForState:UIControlStateHighlighted],
0.0001,
@"This test assumes that .normal and .highlighted start with the same "
"elevation values.");
[selectedButton setSelected:YES];
[highlightedButton setHighlighted:YES];
NSInteger selectedButtonElevationCount = selectedButton.shadowLayer.elevationAssignmentCount;
NSInteger highlightedButtonElevationCount =
highlightedButton.shadowLayer.elevationAssignmentCount;
// When
[selectedButton setElevation:77 forState:UIControlStateHighlighted];
[highlightedButton setElevation:75 forState:UIControlStateNormal];
// Then
XCTAssertEqual(selectedButton.shadowLayer.elevationAssignmentCount,
selectedButtonElevationCount,
@"Updating an unrelated elevation should not update the layer elevation.");
XCTAssertEqual(highlightedButtonElevationCount + 1,
highlightedButton.shadowLayer.elevationAssignmentCount,
@"Updating the current elevation should cause one elevation update.");
}
- (void)testElevationNormal {
// Given
MDCButton *button = [[MDCButton alloc] init];
CGFloat normalElevation = randomNumberNotEqualTo(0);
// When
[button setElevation:normalElevation forState:UIControlStateNormal];
// Then
XCTAssertEqual([button elevationForState:UIControlStateNormal], normalElevation);
XCTAssertEqual([button elevationForState:UIControlStateHighlighted], normalElevation);
XCTAssertEqual([button elevationForState:UIControlStateDisabled], normalElevation);
XCTAssertEqual([button elevationForState:UIControlStateSelected], normalElevation);
}
- (void)testElevationNormalZeroElevation {
// Given
MDCButton *button = [[MDCButton alloc] init];
// When
[button setElevation:0 forState:UIControlStateNormal];
// Then
XCTAssertEqual([button elevationForState:UIControlStateNormal], 0);
}
- (void)testBackgroundColorForState {
// Given
MDCButton *button = [[MDCButton alloc] init];
for (NSUInteger controlState = 0; controlState < kNumUIControlStates; ++controlState) {
// And given
UIColor *color = randomColor();
// When
[button setBackgroundColor:color forState:controlState];
// Then
XCTAssertEqualObjects([button backgroundColorForState:controlState], color);
}
}
// TODO(https://github.com/material-components/material-components-ios/issues/3411 ): Enable this
// test when the behavior in MDCButton is fixed.
- (void)_disabled_testBackgroundColorForStateFallbackBehavior {
// Given
MDCButton *button = [[MDCButton alloc] init];
// When
[button setBackgroundColor:UIColor.purpleColor forState:UIControlStateNormal];
// Then
for (NSUInteger controlState = 0; controlState < kNumUIControlStates; ++controlState) {
XCTAssertEqualObjects([button backgroundColorForState:controlState], UIColor.purpleColor);
}
}
- (void)testBackgroundColorForStateUpdatesBackgroundColor {
// Given
MDCButton *button = [[MDCButton alloc] init];
for (NSUInteger controlState = 0; controlState <= kNumUIControlStates; ++controlState) {
// Disabling the button removes any highlighted state
UIControlState testState = controlState;
if ((testState & UIControlStateDisabled) == UIControlStateDisabled) {
testState &= ~UIControlStateHighlighted;
}
BOOL isDisabled = (testState & UIControlStateDisabled) == UIControlStateDisabled;
BOOL isSelected = (testState & UIControlStateSelected) == UIControlStateSelected;
BOOL isHighlighted = (testState & UIControlStateHighlighted) == UIControlStateHighlighted;
// Also given
UIColor *color = randomColor();
[button setBackgroundColor:color forState:testState];
// When
button.enabled = !isDisabled;
button.selected = isSelected;
button.highlighted = isHighlighted;
XCTAssertEqualObjects(button.backgroundColor, color, @"for state (%lu).",
(unsigned long)testState);
}
}
- (void)testBackgroundColorForStateUpdatesBackgroundColorWithFallback {
// Given
MDCButton *button = [[MDCButton alloc] init];
[button setBackgroundColor:UIColor.magentaColor forState:UIControlStateNormal];
for (NSUInteger controlState = 0; controlState <= kNumUIControlStates; ++controlState) {
BOOL isDisabled = (controlState & UIControlStateDisabled) == UIControlStateDisabled;
BOOL isSelected = (controlState & UIControlStateSelected) == UIControlStateSelected;
BOOL isHighlighted = (controlState & UIControlStateHighlighted) == UIControlStateHighlighted;
// When
button.enabled = !isDisabled;
button.selected = isSelected;
button.highlighted = isHighlighted;
XCTAssertEqualObjects(button.backgroundColor,
[button backgroundColorForState:UIControlStateNormal],
@"for state (%lu).", (unsigned long)controlState);
}
}
// Behavioral test to verify that MDCButton's `backgroundColor:forState:` matches the behavior of
// UIButton's `titleColor:forState:`. Specifically, to ensure that the special handling of
// (UIControlStateDisabled | UIControlStateHighlighted) is identical.
//
// This test is valuable because clients who are familiar with the fallback behavior of
// `titleColor:forState:` may be surprised if the MDCButton APIs don't match. For example, setting
// the titleColor for (UIControlStateDisabled | UIControlStateHighlighted) will actually update the
// value assigned for UIControlStateHighlighted, but ONLY if it has already been assigned. Otherwise
// no update will take place.
- (void)testBackgroundColorForStateBehaviorMatchesTitleColorForStateWithoutFallbackForward {
// Given
MDCButton *testButton = [[MDCButton alloc] init];
UIButton *uiButton = [[UIButton alloc] init];
// When
UIControlState maxState = UIControlStateNormal | UIControlStateHighlighted |
UIControlStateDisabled | UIControlStateSelected;
for (UIControlState state = 0; state <= maxState; ++state) {
UIColor *color = [UIColor colorWithWhite:0 alpha:(CGFloat)(state / (CGFloat)maxState)];
[testButton setBackgroundColor:color forState:state];
[uiButton setTitleColor:color forState:state];
}
// Then
for (UIControlState state = 0; state <= maxState; ++state) {
XCTAssertEqualObjects([testButton backgroundColorForState:state],
[uiButton titleColorForState:state], @" for state (%lu)",
(unsigned long)state);
}
}
- (void)testBackgroundColorForStateBehaviorMatchesTitleColorForStateWithoutFallbackBackward {
// Given
MDCButton *testButton = [[MDCButton alloc] init];
UIButton *uiButton = [[UIButton alloc] init];
// When
UIControlState maxState = UIControlStateNormal | UIControlStateHighlighted |
UIControlStateDisabled | UIControlStateSelected;
for (NSInteger state = maxState; state >= 0; --state) {
UIColor *color = [UIColor colorWithWhite:0 alpha:(CGFloat)(state / (CGFloat)maxState)];
[testButton setBackgroundColor:color forState:(UIControlState)state];
[uiButton setTitleColor:color forState:(UIControlState)state];
}
// Then
for (UIControlState state = 0; state <= maxState; ++state) {
XCTAssertEqualObjects([testButton backgroundColorForState:state],
[uiButton titleColorForState:state], @" for state (%lu)",
(unsigned long)state);
}
}
#pragma mark - shadowColor:forState:
- (void)testRemovedShadowColorForState {
// Given
MDCButton *button = [[MDCButton alloc] init];
// When
[button setShadowColor:nil forState:UIControlStateNormal];
// Then
XCTAssertNil([button shadowColorForState:UIControlStateNormal]);
XCTAssertNil([button shadowColorForState:UIControlStateHighlighted]);
}
- (void)testDefaultShadowColorForState {
// Given
MDCButton *button = [[MDCButton alloc] init];
// Then
XCTAssertNotNil([button shadowColorForState:UIControlStateSelected]);
}
- (void)testShadowColorForUnspecifiedStateEqualsNormalState {
// Given
MDCButton *button = [[MDCButton alloc] init];
UIColor *color = randomColor();
// When
[button setShadowColor:color forState:UIControlStateNormal];
XCTAssertEqual([button shadowColorForState:UIControlStateHighlighted], color);
}
- (void)testShadowColorForState {
// Given
MDCButton *button = [[MDCButton alloc] init];
for (NSUInteger controlState = 0; controlState < kNumUIControlStates; ++controlState) {
// And given
UIColor *color = randomColor();
// When
[button setShadowColor:color forState:controlState];
// Then
XCTAssertEqualObjects([button shadowColorForState:controlState], color);
}
}
- (void)testLayerShadowColorForState {
// Given
MDCButton *button = [[MDCButton alloc] init];
for (NSUInteger controlState = 0; controlState <= kNumUIControlStates; ++controlState) {
NSUInteger effectiveControlState = controlState;
// And given
UIColor *color = randomColor();
// When
[button setShadowColor:color forState:controlState];
if ((effectiveControlState & UIControlStateHighlighted) == UIControlStateHighlighted) {
button.highlighted = YES;
} else {
button.highlighted = NO;
}
if ((effectiveControlState & UIControlStateDisabled) == UIControlStateDisabled) {
button.enabled = NO;
// Disabling a button turns off "highlighted"
effectiveControlState = (effectiveControlState & ~UIControlStateHighlighted);
} else {
button.enabled = YES;
}
if ((effectiveControlState & UIControlStateSelected) == UIControlStateSelected) {
button.selected = YES;
} else {
button.selected = NO;
}
// Then
UIColor *layerShadowColor = [UIColor colorWithCGColor:button.layer.shadowColor];
XCTAssertEqualObjects([button shadowColorForState:effectiveControlState], layerShadowColor);
}
}
#pragma mark - imageTintColor:forState:
- (void)testRemovedImageTintColorForState {
// Given
MDCButton *button = [[MDCButton alloc] init];
// When
[button setImageTintColor:nil forState:UIControlStateNormal];
// Then
XCTAssertNil([button imageTintColorForState:UIControlStateNormal]);
XCTAssertNil([button imageTintColorForState:UIControlStateHighlighted]);
}
- (void)testDefaultImageTintColorForState {
// Given
MDCButton *button = [[MDCButton alloc] init];
// Then
XCTAssertNil([button imageTintColorForState:UIControlStateSelected]);
}
- (void)testImageTintForUnspecifiedStateEqualsNormalState {
// Given
MDCButton *button = [[MDCButton alloc] init];
UIColor *color = randomColor();
// When
[button setImageTintColor:color forState:UIControlStateNormal];
XCTAssertEqual([button imageTintColorForState:UIControlStateHighlighted], color);
}
- (void)testImageTintColorForState {
// Given
MDCButton *button = [[MDCButton alloc] init];
for (NSUInteger controlState = 0; controlState < kNumUIControlStates; ++controlState) {
// And given
UIColor *color = randomColor();
// When
[button setImageTintColor:color forState:controlState];
// Then
XCTAssertEqualObjects([button imageTintColorForState:controlState], color);
}
}
- (void)testImageTintColorForStateFallsBackToDefault {
// Given
MDCButton *button = [[MDCButton alloc] init];
UIColor *normalTint = [UIColor yellowColor];
UIColor *selectedTint = [UIColor redColor];
// When
[button setImageTintColor:normalTint forState:UIControlStateNormal];
[button setImageTintColor:selectedTint forState:UIControlStateSelected];
// Then
XCTAssertEqualObjects([button imageTintColorForState:UIControlStateNormal], normalTint);
XCTAssertEqualObjects([button imageTintColorForState:UIControlStateSelected], selectedTint);
XCTAssertEqualObjects([button imageTintColorForState:UIControlStateHighlighted], normalTint);
}
- (void)testImageTintColorForStateSetsImageViewTintColor {
// Given
MDCButton *button = [[MDCButton alloc] init];
UIColor *normalTint = [UIColor yellowColor];
UIColor *selectedTint = [UIColor redColor];
// When
[button setImageTintColor:normalTint forState:UIControlStateNormal];
[button setImageTintColor:selectedTint forState:UIControlStateSelected];
// Then
XCTAssertEqualObjects(button.imageView.tintColor, normalTint);
// When
button.selected = YES;
// Then
XCTAssertEqualObjects(button.imageView.tintColor, selectedTint);
}
#pragma mark - backgroundColor:forState:
- (void)testCurrentBackgroundColorNormal {
// Given
MDCButton *button = [[MDCButton alloc] init];
UIColor *normalColor = [UIColor redColor];
[button setBackgroundColor:normalColor forState:UIControlStateNormal];
// Then
XCTAssertEqualObjects([button backgroundColor], normalColor);
}
- (void)testCurrentBackgroundColorHighlighted {
// Given
MDCButton *button = [[MDCButton alloc] init];
UIColor *normalColor = [UIColor redColor];
UIColor *color = [UIColor orangeColor];
[button setBackgroundColor:normalColor forState:UIControlStateNormal];
[button setBackgroundColor:color forState:UIControlStateHighlighted];
// When
button.highlighted = YES;
// Then
XCTAssertEqualObjects([button backgroundColor], color);
}
- (void)testCurrentBackgroundColorDisabled {
// Given
MDCButton *button = [[MDCButton alloc] init];
UIColor *normalColor = [UIColor redColor];
UIColor *color = [UIColor orangeColor];
[button setBackgroundColor:normalColor forState:UIControlStateNormal];
[button setBackgroundColor:color forState:UIControlStateDisabled];
// When
button.enabled = NO;
// Then
XCTAssertEqualObjects([button backgroundColor], color);
}
- (void)testCurrentBackgroundColorSelected {
// Given
MDCButton *button = [[MDCButton alloc] init];
UIColor *normalColor = [UIColor redColor];
UIColor *color = [UIColor orangeColor];
[button setBackgroundColor:normalColor forState:UIControlStateNormal];
[button setBackgroundColor:color forState:UIControlStateSelected];
// When
button.selected = YES;
// Then
XCTAssertEqualObjects([button backgroundColor], color);
}
#pragma mark - elevation:forState:
- (void)testCurrentElevationNormal {
// Given
MDCButton *button = [[MDCButton alloc] init];
CGFloat normalElevation = 10;
[button setElevation:normalElevation forState:UIControlStateNormal];
// Then
XCTAssertEqualWithAccuracy([button elevationForState:button.state],
normalElevation,
kEpsilonAccuracy);
}
- (void)testCurrentElevationHighlighted {
// Given
MDCButton *button = [[MDCButton alloc] init];
CGFloat normalElevation = 10;
CGFloat elevation = 40;
[button setElevation:normalElevation forState:UIControlStateNormal];
[button setElevation:elevation forState:UIControlStateHighlighted];
// When
button.highlighted = YES;
// Then
XCTAssertEqualWithAccuracy([button elevationForState:button.state], elevation, kEpsilonAccuracy);
}
- (void)testCurrentElevationDisabled {
// Given
MDCButton *button = [[MDCButton alloc] init];
CGFloat normalElevation = 10;
CGFloat elevation = 40;
[button setElevation:normalElevation forState:UIControlStateNormal];
[button setElevation:elevation forState:UIControlStateDisabled];
// When
button.enabled = NO;
// Then
XCTAssertEqualWithAccuracy([button elevationForState:button.state], elevation, kEpsilonAccuracy);
}
- (void)testCurrentElevationSelected {
// Given
MDCButton *button = [[MDCButton alloc] init];
CGFloat normalElevation = 10;
CGFloat elevation = 40;
[button setElevation:normalElevation forState:UIControlStateNormal];
[button setElevation:elevation forState:UIControlStateSelected];
// When
button.selected = YES;
// Then
XCTAssertEqualWithAccuracy([button elevationForState:button.state], elevation, kEpsilonAccuracy);
}
#pragma mark - Ink Color
- (void)testInkColors {
// Given
MDCButton *button = [[MDCButton alloc] init];
UIColor *color = randomColor();
// When
button.inkColor = color;
// Then
XCTAssertEqualObjects(button.inkColor, color);
}
/*
TODO: things to unit test
(should these even be a thing?)
- hitAreaInset
- underlyingColor (text color)
*/
- (void)testAlphaRestoredWhenReenabled {
// Given
MDCButton *button = [[MDCButton alloc] initWithFrame:CGRectMake(0, 0, 80, 48)];
CGFloat alpha = (CGFloat)0.5;
// When
button.alpha = alpha;
button.enabled = NO;
button.enabled = YES;
// Then
XCTAssertEqualWithAccuracy(alpha, button.alpha, 0.0001);
}
- (void)testEnabledAlphaNotSetWhileDisabled {
// Given
MDCButton *button = [[MDCButton alloc] initWithFrame:CGRectMake(0, 0, 80, 48)];
CGFloat alpha = (CGFloat)0.2;
// When
button.alpha = alpha;
button.enabled = NO;
button.alpha = 1 - alpha;
button.enabled = YES;
// Then
XCTAssertEqualWithAccuracy(alpha, button.alpha, (CGFloat)0.0001);
}
- (void)testDisabledAlpha {
// Given
MDCButton *button = [[MDCButton alloc] initWithFrame:CGRectMake(0, 0, 80, 48)];
CGFloat alpha = 0.5;
// When
[button setDisabledAlpha:alpha];
button.enabled = NO;
// Then
XCTAssertEqualWithAccuracy(alpha, button.alpha, (CGFloat)0.0001);
}
- (void)testPointInsideWithoutHitAreaInsets {
// Given
MDCButton *button = [[MDCButton alloc] initWithFrame:CGRectMake(0, 0, 80, 50)];
CGPoint touchPointInsideBoundsTopLeft = CGPointMake(0, 0);
CGPoint touchPointInsideBoundsTopRight = CGPointMake((CGFloat)79.9, 0);
CGPoint touchPointInsideBoundsBottomRight = CGPointMake((CGFloat)79.9, (CGFloat)49.9);
CGPoint touchPointInsideBoundsBottomLeft = CGPointMake(0, (CGFloat)49.9);
CGPoint touchPointOutsideBoundsTopLeft = CGPointMake(0, (CGFloat)-0.1);
CGPoint touchPointOutsideBoundsTopRight = CGPointMake(80, 0);
CGPoint touchPointOutsideBoundsBottomRight = CGPointMake(80, 50);
CGPoint touchPointOutsideBoundsBottomLeft = CGPointMake(0, 50);
// Then
XCTAssertTrue([button pointInside:touchPointInsideBoundsTopLeft withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideBoundsTopRight withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideBoundsBottomRight withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideBoundsBottomLeft withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideBoundsTopLeft withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideBoundsTopRight withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideBoundsBottomRight withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideBoundsBottomLeft withEvent:nil]);
}
- (void)testPointInsideWithoutHitAreaInsetsTooSmall {
// Given
MDCButton *button = [[MDCButton alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
CGPoint touchPointInsideBoundsTopLeft = CGPointMake(0, 0);
CGPoint touchPointInsideBoundsTopRight = CGPointMake((CGFloat)9.9, 0);
CGPoint touchPointInsideBoundsBottomRight = CGPointMake((CGFloat)9.9, (CGFloat)9.9);
CGPoint touchPointInsideBoundsBottomLeft = CGPointMake(0, (CGFloat)9.9);
CGPoint touchPointOutsideBoundsTopLeft = CGPointMake(0, (CGFloat)-0.1);
CGPoint touchPointOutsideBoundsTopRight = CGPointMake(10, 0);
CGPoint touchPointOutsideBoundsBottomRight = CGPointMake(10, 10);
CGPoint touchPointOutsideBoundsBottomLeft = CGPointMake(0, 10);
// Then
XCTAssertTrue([button pointInside:touchPointInsideBoundsTopLeft withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideBoundsTopRight withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideBoundsBottomRight withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideBoundsBottomLeft withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideBoundsTopLeft withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideBoundsTopRight withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideBoundsBottomRight withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideBoundsBottomLeft withEvent:nil]);
}
- (void)testPointInsideWithCustomHitAreaInsets {
// Given
MDCButton *button = [[MDCButton alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
CGPoint touchPointInsideHitAreaTopLeft = CGPointMake(-5, -5);
CGPoint touchPointInsideHitAreaTopRight = CGPointMake(-5, (CGFloat)14.9);
CGPoint touchPointInsideHitAreaBottomRight = CGPointMake((CGFloat)14.9, (CGFloat)14.9);
CGPoint touchPointInsideHitAreaBottomLeft = CGPointMake((CGFloat)14.9, -5);
CGPoint touchPointOutsideHitAreaTopLeft = CGPointMake((CGFloat)-5.1, -5);
CGPoint touchPointOutsideHitAreaTopRight = CGPointMake(-5, 15);
CGPoint touchPointOutsideHitAreaBottomRight = CGPointMake(15, 15);
CGPoint touchPointOutsideHitAreaBottomLeft = CGPointMake(15, -5);
// When
button.hitAreaInsets = UIEdgeInsetsMake(-5, -5, -5, -5);
// Then
XCTAssertTrue([button pointInside:touchPointInsideHitAreaTopLeft withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideHitAreaTopRight withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideHitAreaBottomRight withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideHitAreaBottomLeft withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideHitAreaTopLeft withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideHitAreaTopRight withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideHitAreaBottomRight withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideHitAreaBottomLeft withEvent:nil]);
}
- (void)testPointInsideWithNonStandardizedBounds {
// Given
MDCButton *button = [[MDCButton alloc] initWithFrame:CGRectZero];
// This is (-10, -10, 20, 20) in standardized form
CGRect bounds = CGRectMake(10, 10, -20, -20);
// Once applied, these insets should increase the hitArea to (-15, -20, 30, 40)
UIEdgeInsets insets = UIEdgeInsetsMake(-10, -5, -10, -5);
CGPoint touchPointInsideHitAreaTopLeft = CGPointMake(-15, -20);
CGPoint touchPointInsideHitAreaTopRight = CGPointMake((CGFloat)14.9, -20);
CGPoint touchPointInsideHitAreaBottomRight = CGPointMake((CGFloat)14.9, (CGFloat)19.9);
CGPoint touchPointInsideHitAreaBottomLeft = CGPointMake(-15, (CGFloat)19.9);
CGPoint touchPointOutsideHitAreaTopLeft = CGPointMake((CGFloat)-15.1, -20);
CGPoint touchPointOutsideHitAreaTopRight = CGPointMake(20, -20);
CGPoint touchPointOutsideHitAreaBottomRight = CGPointMake(15, 20);
CGPoint touchPointOutsideHitAreaBottomLeft = CGPointMake(-15, 20);
// When
button.bounds = bounds;
button.hitAreaInsets = insets;
// Then
XCTAssertTrue([button pointInside:touchPointInsideHitAreaTopLeft withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideHitAreaTopRight withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideHitAreaBottomRight withEvent:nil]);
XCTAssertTrue([button pointInside:touchPointInsideHitAreaBottomLeft withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideHitAreaTopLeft withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideHitAreaTopRight withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideHitAreaBottomRight withEvent:nil]);
XCTAssertFalse([button pointInside:touchPointOutsideHitAreaBottomLeft withEvent:nil]);
}
#pragma mark - UIButton strangeness
- (void)testTitleColorForState {
for (NSUInteger controlState = 0; controlState < kNumUIControlStates; ++controlState) {
if (controlState & kUIControlStateDisabledHighlighted) {
// We skip the Disabled Highlighted state because UIButton titleColorForState ignores it.
continue;
}
// Given
MDCButton *button = [[MDCButton alloc] init];
UIColor *color = [UIColor blueColor];
// When
[button setTitleColor:color forState:controlState];
// Then
XCTAssertEqualObjects([button titleColorForState:controlState],
color,
@"for control state:%@ ",
controlStateDescription(controlState));
}
}
- (void)testTitleColorForStateDisabledHighlight {
// This is strange that setting the color for a state does not return the value of that state.
// It turns out that it returns the value set to the normal state.
// Given
UIControlState controlState = kUIControlStateDisabledHighlighted;
MDCButton *button = [[MDCButton alloc] init];
UIColor *color = [UIColor blueColor];
UIColor *normalColor = [UIColor greenColor];
[button setTitleColor:normalColor forState:UIControlStateNormal];
// When
[button setTitleColor:color forState:controlState];
// Then
XCTAssertEqualObjects([button titleColorForState:controlState],
normalColor,
@"for control state:%@ ",
controlStateDescription(controlState));
XCTAssertNotEqualObjects([button titleColorForState:controlState],
color,
@"for control state:%@ ",
controlStateDescription(controlState));
}
#pragma mark - UIButton state changes
- (void)testEnabled {
// Given
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.highlighted = (BOOL)arc4random_uniform(2);
button.selected = (BOOL)arc4random_uniform(2);
button.enabled = (BOOL)arc4random_uniform(2);
// When
button.enabled = YES;
// Then
XCTAssertTrue(button.enabled);
XCTAssertFalse(button.state & UIControlStateDisabled);
}
- (void)testDisabled {
// Given
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.highlighted = (BOOL)arc4random_uniform(2);
button.selected = (BOOL)arc4random_uniform(2);
button.enabled = (BOOL)arc4random_uniform(2);
// When
button.enabled = NO;
// Then
XCTAssertFalse(button.enabled);
XCTAssertTrue(button.state & UIControlStateDisabled);
}
- (void)testHighlighted {
// Given
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.highlighted = NO;
button.selected = (BOOL)arc4random_uniform(2);
// For some reason we can only set the highlighted state to YES if its enabled is also YES.
button.enabled = YES;
UIControlState oldState = button.state;
XCTAssertFalse(button.highlighted);
// When
button.highlighted = YES;
// Then
XCTAssertTrue(button.highlighted);
XCTAssertTrue(button.state & UIControlStateHighlighted);
XCTAssertEqual(button.state, (oldState | UIControlStateHighlighted));
}
- (void)testUnhighlighted {
// Given
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.highlighted = YES;
button.selected = (BOOL)arc4random_uniform(2);
button.enabled = (BOOL)arc4random_uniform(2);
UIControlState oldState = button.state;
XCTAssertTrue(button.highlighted);
// When
button.highlighted = NO;
// Then
XCTAssertFalse(button.highlighted);
XCTAssertFalse(button.state & UIControlStateHighlighted);
XCTAssertEqual(button.state, (oldState & ~UIControlStateHighlighted));
}
- (void)testSelected {
// Given
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.highlighted = (BOOL)arc4random_uniform(2);
button.selected = NO;
button.enabled = (BOOL)arc4random_uniform(2);
UIControlState oldState = button.state;
// When
button.selected = YES;
// Then
XCTAssertTrue(button.selected);
XCTAssertTrue(button.state & UIControlStateSelected);
XCTAssertEqual(button.state, (oldState | UIControlStateSelected));
}