GIDSignInButtonTest.m 7.9 KB

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