FIRIAMActionUrlFollowerTests.m 11 KB

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