GIDEMMErrorHandlerTest.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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/GIDEMMErrorHandler.h"
  17. #import "GoogleSignIn/Sources/GIDSignInStrings.h"
  18. #ifdef SWIFT_PACKAGE
  19. @import GoogleUtilities_MethodSwizzler;
  20. @import GoogleUtilities_SwizzlerTestHelpers;
  21. @import OCMock;
  22. #else
  23. #import <GoogleUtilities/GULSwizzler.h>
  24. #import <GoogleUtilities/GULSwizzler+Unswizzle.h>
  25. #import <OCMock/OCMock.h>
  26. #endif
  27. NS_ASSUME_NONNULL_BEGIN
  28. // Addtional methods added to UIAlertAction for testing.
  29. @interface UIAlertAction (Testing)
  30. // Returns the handler block for this alert action.
  31. - (void (^)(UIAlertAction *))actionHandler;
  32. @end
  33. @implementation UIAlertAction (Testing)
  34. - (void (^)(UIAlertAction *))actionHandler {
  35. return [self valueForKey:@"handler"];
  36. }
  37. @end
  38. // Unit test for GIDEMMErrorHandler.
  39. @interface GIDEMMErrorHandlerTest : XCTestCase
  40. @end
  41. @implementation GIDEMMErrorHandlerTest {
  42. // Whether or not the current device runs on iOS 10.
  43. BOOL _isIOS10;
  44. // Whether key window has been set.
  45. BOOL _keyWindowSet;
  46. // The view controller that has been presented, if any.
  47. UIViewController *_presentedViewController;
  48. }
  49. - (void)setUp {
  50. [super setUp];
  51. _isIOS10 = [UIDevice currentDevice].systemVersion.integerValue == 10;
  52. _keyWindowSet = NO;
  53. _presentedViewController = nil;
  54. [GULSwizzler swizzleClass:[UIWindow class]
  55. selector:@selector(makeKeyAndVisible)
  56. isClassSelector:NO
  57. withBlock:^() { _keyWindowSet = YES; }];
  58. [GULSwizzler swizzleClass:[UIViewController class]
  59. selector:@selector(presentViewController:animated:completion:)
  60. isClassSelector:NO
  61. withBlock:^(id obj, id arg1) { _presentedViewController = arg1; }];
  62. [GULSwizzler swizzleClass:[GIDSignInStrings class]
  63. selector:@selector(localizedStringForKey:text:)
  64. isClassSelector:YES
  65. withBlock:^(id obj, NSString *key, NSString *text) { return text; }];
  66. }
  67. - (void)tearDown {
  68. [GULSwizzler unswizzleClass:[UIWindow class]
  69. selector:@selector(makeKeyAndVisible)
  70. isClassSelector:NO];
  71. [GULSwizzler unswizzleClass:[UIViewController class]
  72. selector:@selector(presentViewController:animated:completion:)
  73. isClassSelector:NO];
  74. [GULSwizzler unswizzleClass:[GIDSignInStrings class]
  75. selector:@selector(localizedStringForKey:text:)
  76. isClassSelector:YES];
  77. _presentedViewController = nil;
  78. [super tearDown];
  79. }
  80. // Expects opening a particular URL string in performing an action.
  81. - (void)expectOpenURLString:(NSString *)urlString inAction:(void (^)())action {
  82. // Swizzle and mock [UIApplication sharedApplication] since it is unavailable in unit tests.
  83. id mockApplication = OCMStrictClassMock([UIApplication class]);
  84. [GULSwizzler swizzleClass:[UIApplication class]
  85. selector:@selector(sharedApplication)
  86. isClassSelector:YES
  87. withBlock:^() { return mockApplication; }];
  88. [[mockApplication expect] openURL:[NSURL URLWithString:urlString]];
  89. action();
  90. [mockApplication verify];
  91. [GULSwizzler unswizzleClass:[UIApplication class]
  92. selector:@selector(sharedApplication)
  93. isClassSelector:YES];
  94. }
  95. // Verifies that the handler doesn't handle non-exist error.
  96. - (void)testNoError {
  97. __block BOOL completionCalled = NO;
  98. BOOL result = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:@{ @"abc" : @123 }
  99. completion:^() {
  100. completionCalled = YES;
  101. }];
  102. XCTAssertFalse(result);
  103. XCTAssertTrue(completionCalled);
  104. XCTAssertFalse(_keyWindowSet);
  105. XCTAssertNil(_presentedViewController);
  106. }
  107. // Verifies that the handler doesn't handle non-EMM error.
  108. - (void)testNoEMMError {
  109. __block BOOL completionCalled = NO;
  110. NSDictionary<NSString *, NSString *> *response = @{ @"error" : @"invalid_token" };
  111. BOOL result = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:response
  112. completion:^() {
  113. completionCalled = YES;
  114. }];
  115. XCTAssertFalse(result);
  116. XCTAssertTrue(completionCalled);
  117. XCTAssertFalse(_keyWindowSet);
  118. XCTAssertNil(_presentedViewController);
  119. }
  120. // Verifies that the handler handles general EMM error with user tapping 'OK'.
  121. - (void)testGeneralEMMErrorOK {
  122. __block BOOL completionCalled = NO;
  123. NSDictionary<NSString *, NSString *> *response = @{ @"error" : @"emm_something_wrong" };
  124. BOOL result = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:response
  125. completion:^() {
  126. completionCalled = YES;
  127. }];
  128. if (![UIAlertController class]) {
  129. XCTAssertFalse(result);
  130. XCTAssertTrue(completionCalled);
  131. XCTAssertFalse(_keyWindowSet);
  132. XCTAssertNil(_presentedViewController);
  133. return;
  134. }
  135. XCTAssertTrue(result);
  136. XCTAssertFalse(completionCalled);
  137. XCTAssertFalse(_keyWindowSet);
  138. XCTAssertNil(_presentedViewController);
  139. // Should handle no more error while the previous one is being handled.
  140. __block BOOL secondCompletionCalled = NO;
  141. BOOL secondResult = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:response
  142. completion:^() {
  143. secondCompletionCalled = YES;
  144. }];
  145. XCTAssertFalse(secondResult);
  146. XCTAssertTrue(secondCompletionCalled);
  147. XCTAssertFalse(_keyWindowSet);
  148. XCTAssertNil(_presentedViewController);
  149. // Wait for the code under test to be executed on the main thread.
  150. XCTestExpectation *expectation = [self expectationWithDescription:@"wait for main thread"];
  151. dispatch_async(dispatch_get_main_queue(), ^() {
  152. [expectation fulfill];
  153. });
  154. [self waitForExpectationsWithTimeout:1 handler:nil];
  155. XCTAssertFalse(completionCalled);
  156. XCTAssertTrue(_keyWindowSet);
  157. XCTAssertTrue([_presentedViewController isKindOfClass:[UIAlertController class]]);
  158. UIAlertController *alert = (UIAlertController *)_presentedViewController;
  159. XCTAssertNotNil(alert.title);
  160. XCTAssertNotNil(alert.message);
  161. XCTAssertEqual(alert.actions.count, 1);
  162. // Pretend to touch the "OK" button.
  163. UIAlertAction *action = alert.actions[0];
  164. XCTAssertEqualObjects(action.title, @"OK");
  165. action.actionHandler(action);
  166. XCTAssertTrue(completionCalled);
  167. }
  168. // Verifies that the handler handles EMM screenlock required error with user tapping 'Cancel'.
  169. - (void)testScreenlockRequiredCancel {
  170. if (_isIOS10) {
  171. // The dialog is different on iOS 10.
  172. return;
  173. }
  174. __block BOOL completionCalled = NO;
  175. NSDictionary<NSString *, NSString *> *response = @{ @"error" : @"emm_passcode_required" };
  176. BOOL result = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:response
  177. completion:^() {
  178. completionCalled = YES;
  179. }];
  180. if (![UIAlertController class]) {
  181. XCTAssertFalse(result);
  182. XCTAssertTrue(completionCalled);
  183. XCTAssertFalse(_keyWindowSet);
  184. XCTAssertNil(_presentedViewController);
  185. return;
  186. }
  187. XCTAssertTrue(result);
  188. XCTAssertFalse(completionCalled);
  189. XCTAssertFalse(_keyWindowSet);
  190. XCTAssertNil(_presentedViewController);
  191. // Wait for the code under test to be executed on the main thread.
  192. XCTestExpectation *expectation = [self expectationWithDescription:@"wait for main thread"];
  193. dispatch_async(dispatch_get_main_queue(), ^() {
  194. [expectation fulfill];
  195. });
  196. [self waitForExpectationsWithTimeout:1 handler:nil];
  197. XCTAssertFalse(completionCalled);
  198. XCTAssertTrue(_keyWindowSet);
  199. XCTAssertTrue([_presentedViewController isKindOfClass:[UIAlertController class]]);
  200. UIAlertController *alert = (UIAlertController *)_presentedViewController;
  201. XCTAssertNotNil(alert.title);
  202. XCTAssertNotNil(alert.message);
  203. XCTAssertEqual(alert.actions.count, 2);
  204. // Pretend to touch the "Cancel" button.
  205. UIAlertAction *action = alert.actions[0];
  206. XCTAssertEqualObjects(action.title, @"Cancel");
  207. action.actionHandler(action);
  208. XCTAssertTrue(completionCalled);
  209. }
  210. // Verifies that the handler handles EMM screenlock required error with user tapping 'Settings'.
  211. - (void)testScreenlockRequiredSettings {
  212. if (_isIOS10) {
  213. // The dialog is different on iOS 10.
  214. return;
  215. }
  216. __block BOOL completionCalled = NO;
  217. NSDictionary<NSString *, NSString *> *response = @{ @"error" : @"emm_passcode_required" };
  218. BOOL result = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:response
  219. completion:^() {
  220. completionCalled = YES;
  221. }];
  222. if (![UIAlertController class]) {
  223. XCTAssertFalse(result);
  224. XCTAssertTrue(completionCalled);
  225. XCTAssertFalse(_keyWindowSet);
  226. XCTAssertNil(_presentedViewController);
  227. return;
  228. }
  229. XCTAssertTrue(result);
  230. XCTAssertFalse(completionCalled);
  231. XCTAssertFalse(_keyWindowSet);
  232. XCTAssertNil(_presentedViewController);
  233. // Wait for the code under test to be executed on the main thread.
  234. XCTestExpectation *expectation = [self expectationWithDescription:@"wait for main thread"];
  235. dispatch_async(dispatch_get_main_queue(), ^() {
  236. [expectation fulfill];
  237. });
  238. [self waitForExpectationsWithTimeout:1 handler:nil];
  239. XCTAssertFalse(completionCalled);
  240. XCTAssertTrue(_keyWindowSet);
  241. XCTAssertTrue([_presentedViewController isKindOfClass:[UIAlertController class]]);
  242. UIAlertController *alert = (UIAlertController *)_presentedViewController;
  243. XCTAssertNotNil(alert.title);
  244. XCTAssertNotNil(alert.message);
  245. XCTAssertEqual(alert.actions.count, 2);
  246. // Pretend to touch the "Settings" button.
  247. UIAlertAction *action = alert.actions[1];
  248. XCTAssertEqualObjects(action.title, @"Settings");
  249. [self expectOpenURLString:UIApplicationOpenSettingsURLString inAction:^() {
  250. action.actionHandler(action);
  251. }];
  252. XCTAssertTrue(completionCalled);
  253. }
  254. - (void)testScreenlockRequiredOkOnIOS10 {
  255. if (!_isIOS10) {
  256. // A more useful dialog is used for other iOS versions.
  257. return;
  258. }
  259. __block BOOL completionCalled = NO;
  260. NSDictionary<NSString *, NSString *> *response = @{ @"error" : @"emm_passcode_required" };
  261. BOOL result = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:response
  262. completion:^() {
  263. completionCalled = YES;
  264. }];
  265. if (![UIAlertController class]) {
  266. XCTAssertFalse(result);
  267. XCTAssertTrue(completionCalled);
  268. XCTAssertFalse(_keyWindowSet);
  269. XCTAssertNil(_presentedViewController);
  270. return;
  271. }
  272. XCTAssertTrue(result);
  273. XCTAssertFalse(completionCalled);
  274. XCTAssertFalse(_keyWindowSet);
  275. XCTAssertNil(_presentedViewController);
  276. // Wait for the code under test to be executed on the main thread.
  277. XCTestExpectation *expectation = [self expectationWithDescription:@"wait for main thread"];
  278. dispatch_async(dispatch_get_main_queue(), ^() {
  279. [expectation fulfill];
  280. });
  281. [self waitForExpectationsWithTimeout:1 handler:nil];
  282. XCTAssertFalse(completionCalled);
  283. XCTAssertTrue(_keyWindowSet);
  284. XCTAssertTrue([_presentedViewController isKindOfClass:[UIAlertController class]]);
  285. UIAlertController *alert = (UIAlertController *)_presentedViewController;
  286. XCTAssertNotNil(alert.title);
  287. XCTAssertNotNil(alert.message);
  288. XCTAssertEqual(alert.actions.count, 1);
  289. // Pretend to touch the "OK" button.
  290. UIAlertAction *action = alert.actions[0];
  291. XCTAssertEqualObjects(action.title, @"OK");
  292. action.actionHandler(action);
  293. XCTAssertTrue(completionCalled);
  294. }
  295. // Verifies that the handler handles EMM app verification required error without a URL.
  296. - (void)testAppVerificationNoURL {
  297. __block BOOL completionCalled = NO;
  298. NSDictionary<NSString *, NSString *> *response = @{ @"error" : @"emm_app_verification_required" };
  299. BOOL result = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:response
  300. completion:^() {
  301. completionCalled = YES;
  302. }];
  303. if (![UIAlertController class]) {
  304. XCTAssertFalse(result);
  305. XCTAssertTrue(completionCalled);
  306. XCTAssertFalse(_keyWindowSet);
  307. XCTAssertNil(_presentedViewController);
  308. return;
  309. }
  310. XCTAssertTrue(result);
  311. XCTAssertFalse(completionCalled);
  312. XCTAssertFalse(_keyWindowSet);
  313. XCTAssertNil(_presentedViewController);
  314. // Wait for the code under test to be executed on the main thread.
  315. XCTestExpectation *expectation = [self expectationWithDescription:@"wait for main thread"];
  316. dispatch_async(dispatch_get_main_queue(), ^() {
  317. [expectation fulfill];
  318. });
  319. [self waitForExpectationsWithTimeout:1 handler:nil];
  320. XCTAssertFalse(completionCalled);
  321. XCTAssertTrue(_keyWindowSet);
  322. XCTAssertTrue([_presentedViewController isKindOfClass:[UIAlertController class]]);
  323. UIAlertController *alert = (UIAlertController *)_presentedViewController;
  324. XCTAssertNotNil(alert.title);
  325. XCTAssertNotNil(alert.message);
  326. XCTAssertEqual(alert.actions.count, 1);
  327. // Pretend to touch the "OK" button.
  328. UIAlertAction *action = alert.actions[0];
  329. XCTAssertEqualObjects(action.title, @"OK");
  330. action.actionHandler(action);
  331. XCTAssertTrue(completionCalled);
  332. }
  333. // Verifies that the handler handles EMM app verification required error user tapping 'Cancel'.
  334. - (void)testAppVerificationCancel {
  335. __block BOOL completionCalled = NO;
  336. NSDictionary<NSString *, NSString *> *response =
  337. @{ @"error" : @"emm_app_verification_required: https://host.domain/path" };
  338. BOOL result = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:response
  339. completion:^() {
  340. completionCalled = YES;
  341. }];
  342. if (![UIAlertController class]) {
  343. XCTAssertFalse(result);
  344. XCTAssertTrue(completionCalled);
  345. XCTAssertFalse(_keyWindowSet);
  346. XCTAssertNil(_presentedViewController);
  347. return;
  348. }
  349. XCTAssertTrue(result);
  350. XCTAssertFalse(completionCalled);
  351. XCTAssertFalse(_keyWindowSet);
  352. XCTAssertNil(_presentedViewController);
  353. // Wait for the code under test to be executed on the main thread.
  354. XCTestExpectation *expectation = [self expectationWithDescription:@"wait for main thread"];
  355. dispatch_async(dispatch_get_main_queue(), ^() {
  356. [expectation fulfill];
  357. });
  358. [self waitForExpectationsWithTimeout:1 handler:nil];
  359. XCTAssertFalse(completionCalled);
  360. XCTAssertTrue(_keyWindowSet);
  361. XCTAssertTrue([_presentedViewController isKindOfClass:[UIAlertController class]]);
  362. UIAlertController *alert = (UIAlertController *)_presentedViewController;
  363. XCTAssertNotNil(alert.title);
  364. XCTAssertNotNil(alert.message);
  365. XCTAssertEqual(alert.actions.count, 2);
  366. // Pretend to touch the "Cancel" button.
  367. UIAlertAction *action = alert.actions[0];
  368. XCTAssertEqualObjects(action.title, @"Cancel");
  369. action.actionHandler(action);
  370. XCTAssertTrue(completionCalled);
  371. }
  372. // Verifies that the handler handles EMM app verification required error user tapping 'Connect'.
  373. - (void)testAppVerificationConnect {
  374. __block BOOL completionCalled = NO;
  375. NSDictionary<NSString *, NSString *> *response =
  376. @{ @"error" : @"emm_app_verification_required: https://host.domain/path" };
  377. BOOL result = [[GIDEMMErrorHandler sharedInstance] handleErrorFromResponse:response
  378. completion:^() {
  379. completionCalled = YES;
  380. }];
  381. if (![UIAlertController class]) {
  382. XCTAssertFalse(result);
  383. XCTAssertTrue(completionCalled);
  384. XCTAssertFalse(_keyWindowSet);
  385. XCTAssertNil(_presentedViewController);
  386. return;
  387. }
  388. XCTAssertTrue(result);
  389. XCTAssertFalse(completionCalled);
  390. XCTAssertFalse(_keyWindowSet);
  391. XCTAssertNil(_presentedViewController);
  392. // Wait for the code under test to be executed on the main thread.
  393. XCTestExpectation *expectation = [self expectationWithDescription:@"wait for main thread"];
  394. dispatch_async(dispatch_get_main_queue(), ^() {
  395. [expectation fulfill];
  396. });
  397. [self waitForExpectationsWithTimeout:1 handler:nil];
  398. XCTAssertFalse(completionCalled);
  399. XCTAssertTrue(_keyWindowSet);
  400. XCTAssertTrue([_presentedViewController isKindOfClass:[UIAlertController class]]);
  401. UIAlertController *alert = (UIAlertController *)_presentedViewController;
  402. XCTAssertNotNil(alert.title);
  403. XCTAssertNotNil(alert.message);
  404. XCTAssertEqual(alert.actions.count, 2);
  405. // Pretend to touch the "Connect" button.
  406. UIAlertAction *action = alert.actions[1];
  407. XCTAssertEqualObjects(action.title, @"Connect");
  408. [self expectOpenURLString:@"https://host.domain/path" inAction:^() {
  409. action.actionHandler(action);
  410. }];
  411. XCTAssertTrue(completionCalled);
  412. }
  413. // Verifies that the handler can handle sequential errors independently.
  414. - (void)testSequentialErrors {
  415. [self testGeneralEMMErrorOK];
  416. _keyWindowSet = NO;
  417. _presentedViewController = nil;
  418. [self testScreenlockRequiredCancel];
  419. }
  420. @end
  421. NS_ASSUME_NONNULL_END