GIDSignInButtonTest.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2021 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import <UIKit/UIKit.h>
  15. #import <XCTest/XCTest.h>
  16. #import "GoogleSignIn/Sources/Public/GoogleSignIn/GIDSignInButton.h"
  17. #ifdef SWIFT_PACKAGE
  18. @import OCMock;
  19. #else
  20. #import <OCMock/OCMock.h>
  21. #endif
  22. static NSString *const kWidthConstraintIdentifier =
  23. @"buttonWidth - auto generated by GIDSignInButton";
  24. static NSString *const kHeightConstraintIdentifier =
  25. @"buttonHeight - auto generated by GIDSignInButton";
  26. static NSString * const kAppBundleId = @"FakeBundleID";
  27. @interface GIDSignInButton (Test)
  28. - (void)updateUI;
  29. @end
  30. @interface GIDSignInButtonTest : XCTestCase
  31. @end
  32. @implementation GIDSignInButtonTest
  33. #pragma mark - Tests
  34. // Verify the default style and color scheme for the button.
  35. - (void)testDefaultButtonSettings {
  36. GIDSignInButton *button = [[GIDSignInButton alloc] init];
  37. XCTAssertTrue(button.style == kGIDSignInButtonStyleStandard,
  38. @"Default button style is incorrect");
  39. XCTAssertTrue(button.colorScheme == kGIDSignInButtonColorSchemeLight,
  40. @"Default button color scheme is incorrect");
  41. }
  42. // Verify that setting the button's style/color scheme will refresh the image.
  43. - (void)testRefreshImage {
  44. GIDSignInButton *button = [[GIDSignInButton alloc] init];
  45. id buttonMock = OCMPartialMock(button);
  46. [[buttonMock expect] updateUI];
  47. [(GIDSignInButton *)buttonMock setStyle:kGIDSignInButtonStyleWide];
  48. [buttonMock verify];
  49. [[buttonMock expect] updateUI];
  50. [buttonMock setColorScheme:kGIDSignInButtonColorSchemeDark];
  51. [buttonMock verify];
  52. }
  53. - (void)testNSCoding {
  54. GIDSignInButton *button = [[GIDSignInButton alloc] init];
  55. button.style = kGIDSignInButtonStyleIconOnly;
  56. button.colorScheme = kGIDSignInButtonColorSchemeLight;
  57. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:button];
  58. GIDSignInButton *newButton = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  59. XCTAssertEqual(button.style, newButton.style);
  60. XCTAssertEqual(button.colorScheme, newButton.colorScheme);
  61. }
  62. - (void)testSetStyle {
  63. GIDSignInButton *button = [[GIDSignInButton alloc] init];
  64. id buttonMock = OCMPartialMock(button);
  65. [[buttonMock expect] setNeedsDisplay];
  66. button.style = kGIDSignInButtonStyleWide;
  67. [buttonMock verify];
  68. XCTAssertEqual(button.style, kGIDSignInButtonStyleWide);
  69. [[buttonMock expect] setNeedsDisplay];
  70. button.style = kGIDSignInButtonStyleIconOnly;
  71. [buttonMock verify];
  72. XCTAssertEqual(button.style, kGIDSignInButtonStyleIconOnly);
  73. [[buttonMock expect] setNeedsDisplay];
  74. button.style = kGIDSignInButtonStyleStandard;
  75. [buttonMock verify];
  76. XCTAssertEqual(button.style, kGIDSignInButtonStyleStandard);
  77. }
  78. - (void)testSetEnabled {
  79. GIDSignInButton *button = [[GIDSignInButton alloc] init];
  80. id buttonMock = OCMPartialMock(button);
  81. // Checks default value for |button.enabled|
  82. XCTAssertTrue(button.enabled, @"Button should be default enabled");
  83. // Checks that button redraw when enabled set YES.
  84. [[buttonMock expect] setNeedsDisplay];
  85. button.enabled = NO;
  86. [buttonMock verify];
  87. // Checks nothing happen if setting same value.
  88. button.enabled = NO;
  89. // Checks that button redraw when enabled set YES.
  90. [[buttonMock expect] setNeedsDisplay];
  91. button.enabled = YES;
  92. [buttonMock verify];
  93. // Checks nothing happen if setting same value.
  94. button.enabled = YES;
  95. }
  96. - (void)testWidthAndHeightConstraintAddition {
  97. GIDSignInButton *button = [[GIDSignInButton alloc] init];
  98. XCTAssertEqual([button.constraints count], 0u);
  99. [button updateConstraints];
  100. XCTAssertEqual([button.constraints count], 2u);
  101. // Ensure we don't duplicate constraints
  102. [button updateConstraints];
  103. XCTAssertEqual([button.constraints count], 2u);
  104. }
  105. - (void)testHeightConstraintReplacement {
  106. GIDSignInButton *button = [[GIDSignInButton alloc] init];
  107. [button addConstraint:[NSLayoutConstraint constraintWithItem:button
  108. attribute:NSLayoutAttributeHeight
  109. relatedBy:NSLayoutRelationEqual
  110. toItem:nil
  111. attribute:NSLayoutAttributeNotAnAttribute
  112. multiplier:1.0
  113. constant:30]];
  114. XCTAssertEqual([button.constraints count], 1u);
  115. [button updateConstraints];
  116. XCTAssertEqual([button.constraints count], 2u);
  117. for (NSLayoutConstraint *constraint in button.constraints) {
  118. if ([constraint.identifier isEqualToString:kHeightConstraintIdentifier]) {
  119. XCTAssertEqual(constraint.firstAttribute, NSLayoutAttributeHeight);
  120. XCTAssertEqual(constraint.constant, 48);
  121. return;
  122. }
  123. }
  124. XCTFail(@"New constraint not found.");
  125. }
  126. - (void)testWidthConstraintBelowMinimumRemoval {
  127. GIDSignInButton *button = [[GIDSignInButton alloc] init];
  128. [button addConstraint:[NSLayoutConstraint constraintWithItem:button
  129. attribute:NSLayoutAttributeWidth
  130. relatedBy:NSLayoutRelationEqual
  131. toItem:nil
  132. attribute:NSLayoutAttributeNotAnAttribute
  133. multiplier:1.0
  134. constant:50]];
  135. XCTAssertEqual([button.constraints count], 1u);
  136. [button updateConstraints];
  137. XCTAssertEqual([button.constraints count], 2u);
  138. for (NSLayoutConstraint *constraint in button.constraints) {
  139. XCTAssertTrue([constraint.identifier isEqualToString:kWidthConstraintIdentifier] ||
  140. [constraint.identifier isEqualToString:kHeightConstraintIdentifier]);
  141. }
  142. }
  143. - (void)testDontRemoveValidWidthConstraint {
  144. GIDSignInButton *button = [[GIDSignInButton alloc] init];
  145. NSLayoutConstraint *widthConstraint =
  146. [NSLayoutConstraint constraintWithItem:button
  147. attribute:NSLayoutAttributeWidth
  148. relatedBy:NSLayoutRelationEqual
  149. toItem:nil
  150. attribute:NSLayoutAttributeNotAnAttribute
  151. multiplier:1.0
  152. constant:250];
  153. [button addConstraint:widthConstraint];
  154. XCTAssertEqual([button.constraints count], 1u);
  155. [button updateConstraints];
  156. XCTAssertEqual([button.constraints count], 3u);
  157. XCTAssertTrue([button.constraints containsObject:widthConstraint]);
  158. }
  159. - (void)testDontRemoveValidHeightConstraint {
  160. GIDSignInButton *button = [[GIDSignInButton alloc] init];
  161. NSLayoutConstraint *heightConstraint =
  162. [NSLayoutConstraint constraintWithItem:button
  163. attribute:NSLayoutAttributeHeight
  164. relatedBy:NSLayoutRelationEqual
  165. toItem:nil
  166. attribute:NSLayoutAttributeNotAnAttribute
  167. multiplier:1.0
  168. constant:48];
  169. [button addConstraint:heightConstraint];
  170. XCTAssertEqual([button.constraints count], 1u);
  171. [button updateConstraints];
  172. XCTAssertEqual([button.constraints count], 2u);
  173. XCTAssertTrue([button.constraints containsObject:heightConstraint]);
  174. }
  175. @end