FIRIAMActionUrlFollowerTests.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. if (@available(iOS 10.0, *)) {
  101. OCMStub([self.mockApplication openURL:[OCMArg any]
  102. options:[OCMArg any]
  103. completionHandler:([OCMArg invokeBlockWithArgs:yesOrNo, nil])]);
  104. }
  105. } else {
  106. OCMStub([self.mockApplication openURL:[OCMArg any]]).andReturn(returnValue);
  107. }
  108. }
  109. - (void)testUniversalLinkHandlingReturnNo {
  110. self.mockAppDelegate = OCMClassMock([Delegate1 class]);
  111. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  112. // In this test case, app delegate's application:continueUserActivity:restorationHandler
  113. // tries to handle the url but returns NO. We should fallback to the do iOS OpenURL for
  114. // this case
  115. NSURL *url = [NSURL URLWithString:@"http://test.com"];
  116. OCMExpect([self.mockAppDelegate application:[OCMArg isKindOfClass:[UIApplication class]]
  117. continueUserActivity:[OCMArg any]
  118. restorationHandler:[OCMArg any]])
  119. .andReturn(NO);
  120. [self setupOpenURLViaIOSForUIApplicationWithReturnValue:YES];
  121. FIRIAMActionURLFollower *follower =
  122. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[]
  123. withApplication:self.mockApplication];
  124. XCTestExpectation *expectation =
  125. [self expectationWithDescription:@"Completion Callback Triggered"];
  126. [follower followActionURL:url
  127. withCompletionBlock:^(BOOL success) {
  128. [expectation fulfill];
  129. }];
  130. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  131. OCMVerifyAll((id)self.mockAppDelegate);
  132. }
  133. - (void)testCustomSchemeHandlingReturnYES {
  134. self.mockAppDelegate = OCMClassMock([Delegate2 class]);
  135. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  136. // we support custom url scheme 'scheme1' and 'scheme2' in this setup
  137. FIRIAMActionURLFollower *follower =
  138. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[ @"scheme1", @"scheme2" ]
  139. withApplication:self.mockApplication];
  140. NSURL *customURL = [NSURL URLWithString:@"scheme1://test.com"];
  141. OCMExpect([self.mockAppDelegate application:[OCMArg isKindOfClass:[UIApplication class]]
  142. openURL:[OCMArg checkWithBlock:^BOOL(id urlId) {
  143. // verifying url received by the app delegate is expected
  144. NSURL *url = (NSURL *)urlId;
  145. return [url isEqual:customURL];
  146. }]
  147. options:[OCMArg any]])
  148. .andReturn(YES);
  149. XCTestExpectation *expectation =
  150. [self expectationWithDescription:@"Completion Callback Triggered"];
  151. [follower followActionURL:customURL
  152. withCompletionBlock:^(BOOL success) {
  153. XCTAssertTrue(success);
  154. [expectation fulfill];
  155. }];
  156. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  157. OCMVerifyAll((id)self.mockAppDelegate);
  158. }
  159. - (void)testCustomSchemeHandlingReturnNO {
  160. self.mockAppDelegate = OCMClassMock([Delegate2 class]);
  161. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  162. // we support custom url scheme 'scheme1' and 'scheme2' in this setup
  163. FIRIAMActionURLFollower *follower =
  164. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[ @"scheme1", @"scheme2" ]
  165. withApplication:self.mockApplication];
  166. NSURL *customURL = [NSURL URLWithString:@"scheme1://test.com"];
  167. OCMExpect([self.mockAppDelegate application:[OCMArg isKindOfClass:[UIApplication class]]
  168. openURL:[OCMArg checkWithBlock:^BOOL(id urlId) {
  169. // verifying url received by the app delegate is expected
  170. NSURL *url = (NSURL *)urlId;
  171. return [url isEqual:customURL];
  172. }]
  173. options:[OCMArg any]])
  174. .andReturn(NO);
  175. // it would fallback to Open URL with iOS System
  176. [self setupOpenURLViaIOSForUIApplicationWithReturnValue:NO];
  177. XCTestExpectation *expectation =
  178. [self expectationWithDescription:@"Completion Callback Triggered"];
  179. [follower followActionURL:customURL
  180. withCompletionBlock:^(BOOL success) {
  181. // since both custom scheme url open and fallback iOS url open returns NO, we expect
  182. // to get a NO here
  183. XCTAssertFalse(success);
  184. [expectation fulfill];
  185. }];
  186. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  187. OCMVerifyAll((id)self.mockAppDelegate);
  188. }
  189. - (void)testCustomSchemeNotMatching {
  190. self.mockAppDelegate = OCMClassMock([Delegate2 class]);
  191. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  192. // we support custom url scheme 'scheme1' and 'scheme2' in this setup
  193. FIRIAMActionURLFollower *follower =
  194. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[ @"scheme1", @"scheme2" ]
  195. withApplication:self.mockApplication];
  196. NSURL *customURL = [NSURL URLWithString:@"unknown-scheme://test.com"];
  197. // since custom scheme does not match, we should not expect app delegate's open URL method
  198. // being triggered
  199. OCMReject([self.mockAppDelegate application:[OCMArg any]
  200. openURL:[OCMArg any]
  201. options:[OCMArg any]]);
  202. XCTestExpectation *expectation =
  203. [self expectationWithDescription:@"Completion Callback Triggered"];
  204. [self setupOpenURLViaIOSForUIApplicationWithReturnValue:YES];
  205. [follower followActionURL:customURL
  206. withCompletionBlock:^(BOOL success) {
  207. XCTAssertTrue(success);
  208. [expectation fulfill];
  209. }];
  210. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  211. OCMVerifyAll((id)self.mockAppDelegate);
  212. }
  213. - (void)testUniversalLinkWithoutContinueUserActivityDefined {
  214. // Delegate2 does not define application:continueUserActivity:restorationHandler
  215. self.mockAppDelegate = OCMClassMock([Delegate2 class]);
  216. OCMStub([self.mockApplication delegate]).andReturn(self.mockAppDelegate);
  217. FIRIAMActionURLFollower *follower =
  218. [[FIRIAMActionURLFollower alloc] initWithCustomURLSchemeArray:@[]
  219. withApplication:self.mockApplication];
  220. // so for this url, even if it's a http or https link, we should fall back to openURL with
  221. // iOS system
  222. NSURL *url = [NSURL URLWithString:@"http://test.com"];
  223. XCTestExpectation *expectation =
  224. [self expectationWithDescription:@"Completion Callback Triggered"];
  225. [self setupOpenURLViaIOSForUIApplicationWithReturnValue:YES];
  226. [follower followActionURL:url
  227. withCompletionBlock:^(BOOL success) {
  228. XCTAssertTrue(success);
  229. [expectation fulfill];
  230. }];
  231. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  232. }
  233. @end