FIRIAMActionUrlFollowerTests.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * Copyright 2018 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <OCMock/OCMock.h>
  17. #import <XCTest/XCTest.h>
  18. #import "FIRIAMActionURLFollower.h"
  19. // since OCMock does support mocking respondsToSelector on mock object, we have to define
  20. // different delegate classes with different coverages of certain delegate methods:
  21. // FIRIAMActionURLFollower behavior depend on these method implementation coverages on the
  22. // delegate
  23. // this delegate only implements application:continueUserActivity:restorationHandler
  24. @interface Delegate1 : NSObject <UIApplicationDelegate>
  25. - (BOOL)application:(UIApplication *)application
  26. continueUserActivity:(NSUserActivity *)userActivity
  27. restorationHandler:(void (^)(NSArray *))restorationHandler;
  28. @end
  29. @implementation Delegate1
  30. - (BOOL)application:(UIApplication *)application
  31. continueUserActivity:(NSUserActivity *)userActivity
  32. restorationHandler:(void (^)(NSArray *))restorationHandler {
  33. return YES;
  34. }
  35. @end
  36. // this delegate only implements application:openURL:options which is suitable for custom url scheme
  37. // link handling
  38. @interface Delegate2 : NSObject <UIApplicationDelegate>
  39. - (BOOL)application:(UIApplication *)app
  40. openURL:(NSURL *)url
  41. options:(NSDictionary<NSString *, id> *)options;
  42. @end
  43. @implementation Delegate2
  44. - (BOOL)application:(UIApplication *)app
  45. openURL:(NSURL *)url
  46. options:(NSDictionary<NSString *, id> *)options {
  47. return YES;
  48. }
  49. @end
  50. @interface FIRIAMActionURLFollowerTests : XCTestCase
  51. @property FIRIAMActionURLFollower *actionFollower;
  52. @property UIApplication *mockApplication;
  53. @property id<UIApplicationDelegate> mockAppDelegate;
  54. @end
  55. @implementation FIRIAMActionURLFollowerTests
  56. - (void)setUp {
  57. [super setUp];
  58. self.mockApplication = OCMClassMock([UIApplication class]);
  59. }
  60. - (void)tearDown {
  61. // Put teardown code here. This method is called after the invocation of each test method in the
  62. // class.
  63. [super tearDown];
  64. }
  65. - (void)testUniversalLinkHandlingReturnYES {
  66. self.mockAppDelegate = OCMClassMock([Delegate1 class]);
  67. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  68. // In this test case, app delegate's application:continueUserActivity:restorationHandler
  69. // handles the url and returns YES
  70. NSURL *url = [NSURL URLWithString:@"http://test.com"];
  71. OCMExpect([self.mockAppDelegate application:[OCMArg isKindOfClass:[UIApplication class]]
  72. continueUserActivity:[OCMArg checkWithBlock:^BOOL(id userActivity) {
  73. // verifying the type and url field for the userActivity object
  74. NSUserActivity *activity = (NSUserActivity *)userActivity;
  75. return [activity.activityType
  76. isEqualToString:NSUserActivityTypeBrowsingWeb] &&
  77. [activity.webpageURL isEqual:url];
  78. }]
  79. restorationHandler:[OCMArg any]])
  80. .andReturn(YES);
  81. FIRIAMActionURLFollower *follower =
  82. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[]
  83. withApplication:self.mockApplication];
  84. XCTestExpectation *expectation =
  85. [self expectationWithDescription:@"Completion Callback Triggered"];
  86. [follower followActionURL:url
  87. withCompletionBlock:^(BOOL success) {
  88. XCTAssertTrue(success);
  89. [expectation fulfill];
  90. }];
  91. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  92. OCMVerifyAll((id)self.mockAppDelegate);
  93. }
  94. - (void)setupOpenURLViaIOSForUIApplicationWithReturnValue:(BOOL)returnValue {
  95. // it would fallback to either openURL:options:completionHandler:
  96. // or openURL: on the UIApplication object to follow the url
  97. if ([self.mockApplication respondsToSelector:@selector(openURL:options:completionHandler:)]) {
  98. // id types is needed for calling invokeBlockWithArgs
  99. id yesOrNo = returnValue ? @YES : @NO;
  100. OCMStub([self.mockApplication openURL:[OCMArg any]
  101. options:[OCMArg any]
  102. completionHandler:([OCMArg invokeBlockWithArgs:yesOrNo, nil])]);
  103. } else {
  104. OCMStub([self.mockApplication openURL:[OCMArg any]]).andReturn(returnValue);
  105. }
  106. }
  107. - (void)testUniversalLinkHandlingReturnNo {
  108. self.mockAppDelegate = OCMClassMock([Delegate1 class]);
  109. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  110. // In this test case, app delegate's application:continueUserActivity:restorationHandler
  111. // tries to handle the url but returns NO. We should fallback to the do iOS OpenURL for
  112. // this case
  113. NSURL *url = [NSURL URLWithString:@"http://test.com"];
  114. OCMExpect([self.mockAppDelegate application:[OCMArg isKindOfClass:[UIApplication class]]
  115. continueUserActivity:[OCMArg any]
  116. restorationHandler:[OCMArg any]])
  117. .andReturn(NO);
  118. [self setupOpenURLViaIOSForUIApplicationWithReturnValue:YES];
  119. FIRIAMActionURLFollower *follower =
  120. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[]
  121. withApplication:self.mockApplication];
  122. XCTestExpectation *expectation =
  123. [self expectationWithDescription:@"Completion Callback Triggered"];
  124. [follower followActionURL:url
  125. withCompletionBlock:^(BOOL success) {
  126. [expectation fulfill];
  127. }];
  128. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  129. OCMVerifyAll((id)self.mockAppDelegate);
  130. }
  131. - (void)testCustomSchemeHandlingReturnYES {
  132. self.mockAppDelegate = OCMClassMock([Delegate2 class]);
  133. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  134. // we support custom url scheme 'scheme1' and 'scheme2' in this setup
  135. FIRIAMActionURLFollower *follower =
  136. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[ @"scheme1", @"scheme2" ]
  137. withApplication:self.mockApplication];
  138. NSURL *customURL = [NSURL URLWithString:@"scheme1://test.com"];
  139. OCMExpect([self.mockAppDelegate application:[OCMArg isKindOfClass:[UIApplication class]]
  140. openURL:[OCMArg checkWithBlock:^BOOL(id urlId) {
  141. // verifying url received by the app delegate is expected
  142. NSURL *url = (NSURL *)urlId;
  143. return [url isEqual:customURL];
  144. }]
  145. options:[OCMArg any]])
  146. .andReturn(YES);
  147. XCTestExpectation *expectation =
  148. [self expectationWithDescription:@"Completion Callback Triggered"];
  149. [follower followActionURL:customURL
  150. withCompletionBlock:^(BOOL success) {
  151. XCTAssertTrue(success);
  152. [expectation fulfill];
  153. }];
  154. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  155. OCMVerifyAll((id)self.mockAppDelegate);
  156. }
  157. - (void)testCustomSchemeHandlingReturnNO {
  158. self.mockAppDelegate = OCMClassMock([Delegate2 class]);
  159. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  160. // we support custom url scheme 'scheme1' and 'scheme2' in this setup
  161. FIRIAMActionURLFollower *follower =
  162. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[ @"scheme1", @"scheme2" ]
  163. withApplication:self.mockApplication];
  164. NSURL *customURL = [NSURL URLWithString:@"scheme1://test.com"];
  165. OCMExpect([self.mockAppDelegate application:[OCMArg isKindOfClass:[UIApplication class]]
  166. openURL:[OCMArg checkWithBlock:^BOOL(id urlId) {
  167. // verifying url received by the app delegate is expected
  168. NSURL *url = (NSURL *)urlId;
  169. return [url isEqual:customURL];
  170. }]
  171. options:[OCMArg any]])
  172. .andReturn(NO);
  173. // it would fallback to Open URL with iOS System
  174. [self setupOpenURLViaIOSForUIApplicationWithReturnValue:NO];
  175. XCTestExpectation *expectation =
  176. [self expectationWithDescription:@"Completion Callback Triggered"];
  177. [follower followActionURL:customURL
  178. withCompletionBlock:^(BOOL success) {
  179. // since both custom scheme url open and fallback iOS url open returns NO, we expect
  180. // to get a NO here
  181. XCTAssertFalse(success);
  182. [expectation fulfill];
  183. }];
  184. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  185. OCMVerifyAll((id)self.mockAppDelegate);
  186. }
  187. - (void)testCustomSchemeNotMatching {
  188. self.mockAppDelegate = OCMClassMock([Delegate2 class]);
  189. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  190. // we support custom url scheme 'scheme1' and 'scheme2' in this setup
  191. FIRIAMActionURLFollower *follower =
  192. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[ @"scheme1", @"scheme2" ]
  193. withApplication:self.mockApplication];
  194. NSURL *customURL = [NSURL URLWithString:@"unknown-scheme://test.com"];
  195. // since custom scheme does not match, we should not expect app delegate's open URL method
  196. // being triggered
  197. OCMReject([self.mockAppDelegate application:[OCMArg any]
  198. openURL:[OCMArg any]
  199. options:[OCMArg any]]);
  200. XCTestExpectation *expectation =
  201. [self expectationWithDescription:@"Completion Callback Triggered"];
  202. [self setupOpenURLViaIOSForUIApplicationWithReturnValue:YES];
  203. [follower followActionURL:customURL
  204. withCompletionBlock:^(BOOL success) {
  205. XCTAssertTrue(success);
  206. [expectation fulfill];
  207. }];
  208. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  209. OCMVerifyAll((id)self.mockAppDelegate);
  210. }
  211. - (void)testUniversalLinkWithoutContinueUserActivityDefined {
  212. // Delegate2 does not define application:continueUserActivity:restorationHandler
  213. self.mockAppDelegate = OCMClassMock([Delegate2 class]);
  214. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  215. FIRIAMActionURLFollower *follower =
  216. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[]
  217. withApplication:self.mockApplication];
  218. // so for this url, even if it's a http or https link, we should fall back to openURL with
  219. // iOS system
  220. NSURL *url = [NSURL URLWithString:@"http://test.com"];
  221. XCTestExpectation *expectation =
  222. [self expectationWithDescription:@"Completion Callback Triggered"];
  223. [self setupOpenURLViaIOSForUIApplicationWithReturnValue:YES];
  224. [follower followActionURL:url
  225. withCompletionBlock:^(BOOL success) {
  226. XCTAssertTrue(success);
  227. [expectation fulfill];
  228. }];
  229. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  230. }
  231. @end