OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/chrome/app/application_delegate/user_activity_handler.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 9 #import <CoreSpotlight/CoreSpotlight.h> |
| 10 |
| 11 #include "base/ios/ios_util.h" |
| 12 #include "base/mac/scoped_block.h" |
| 13 #include "base/mac/scoped_nsobject.h" |
| 14 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/test/scoped_command_line.h" |
| 16 #include "components/handoff/handoff_utility.h" |
| 17 #include "ios/chrome/app/application_delegate/fake_startup_information.h" |
| 18 #include "ios/chrome/app/application_delegate/mock_tab_opener.h" |
| 19 #include "ios/chrome/app/application_delegate/startup_information.h" |
| 20 #include "ios/chrome/app/application_delegate/tab_opening.h" |
| 21 #include "ios/chrome/app/application_mode.h" |
| 22 #include "ios/chrome/app/main_controller.h" |
| 23 #include "ios/chrome/app/spotlight/actions_spotlight_manager.h" |
| 24 #import "ios/chrome/app/spotlight/spotlight_util.h" |
| 25 #include "ios/chrome/browser/app_startup_parameters.h" |
| 26 #include "ios/chrome/browser/chrome_switches.h" |
| 27 #include "ios/chrome/browser/chrome_url_constants.h" |
| 28 #import "ios/chrome/browser/tabs/tab.h" |
| 29 #import "ios/chrome/browser/tabs/tab_model.h" |
| 30 #import "ios/chrome/browser/tabs/tab_model_observer.h" |
| 31 #import "ios/chrome/browser/u2f/u2f_controller.h" |
| 32 #import "ios/chrome/test/base/scoped_block_swizzler.h" |
| 33 #import "net/base/mac/url_conversions.h" |
| 34 #include "net/test/gtest_util.h" |
| 35 #include "testing/platform_test.h" |
| 36 #import "third_party/ocmock/OCMock/OCMock.h" |
| 37 #include "third_party/ocmock/gtest_support.h" |
| 38 #include "ui/base/page_transition_types.h" |
| 39 #include "url/gurl.h" |
| 40 |
| 41 #pragma mark - Tab Mock |
| 42 |
| 43 // Tab mock for using in UserActivity tests. |
| 44 @interface UserActivityHandlerTabMock : NSObject |
| 45 |
| 46 @property(nonatomic, readonly) GURL url; |
| 47 @property(nonatomic, readonly) NSString* tabId; |
| 48 |
| 49 @end |
| 50 |
| 51 @implementation UserActivityHandlerTabMock |
| 52 @synthesize url = _url; |
| 53 @synthesize tabId = _tabId; |
| 54 |
| 55 - (void)evaluateU2FResultFromURL:(const GURL&)url { |
| 56 _url = url; |
| 57 } |
| 58 |
| 59 @end |
| 60 |
| 61 #pragma mark - TabModel Mock |
| 62 |
| 63 // TabModel mock for using in UserActivity tests. |
| 64 @interface UserActivityHandlerTabModelMock : NSObject<NSFastEnumeration> { |
| 65 @private |
| 66 base::scoped_nsobject<NSMutableArray> _tabs; |
| 67 } |
| 68 |
| 69 - (void)addTab:(Tab*)tab; |
| 70 - (void)addObserver:(id<TabModelObserver>)observer; |
| 71 - (void)removeObserver:(id<TabModelObserver>)observer; |
| 72 |
| 73 @end |
| 74 |
| 75 @implementation UserActivityHandlerTabModelMock |
| 76 |
| 77 - (instancetype)init { |
| 78 if ((self = [super init])) { |
| 79 _tabs.reset([[NSMutableArray alloc] init]); |
| 80 } |
| 81 return self; |
| 82 } |
| 83 |
| 84 - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState*)state |
| 85 objects:(id*)stackbuf |
| 86 count:(NSUInteger)len { |
| 87 return [_tabs countByEnumeratingWithState:state objects:stackbuf count:len]; |
| 88 } |
| 89 |
| 90 - (void)addTab:(Tab*)tab { |
| 91 [_tabs addObject:tab]; |
| 92 } |
| 93 |
| 94 - (void)addObserver:(id<TabModelObserver>)observer { |
| 95 // Empty. |
| 96 } |
| 97 |
| 98 - (void)removeObserver:(id<TabModelObserver>)observer { |
| 99 // Empty. |
| 100 } |
| 101 |
| 102 @end |
| 103 |
| 104 #pragma mark - Test class. |
| 105 |
| 106 // A block that takes as arguments the caller and the arguments from |
| 107 // UserActivityHandler +handleStartupParameters and returns nothing. |
| 108 typedef void (^startupParameterBlock)(id, |
| 109 id<TabOpening>, |
| 110 id<StartupInformation>, |
| 111 id<BrowserViewInformation>); |
| 112 |
| 113 // A block that takes a BOOL argument and returns nothing. |
| 114 typedef void (^conditionBlock)(BOOL); |
| 115 |
| 116 class UserActivityHandlerTest : public PlatformTest { |
| 117 protected: |
| 118 void swizzleHandleStartupParameters() { |
| 119 handle_startup_parameters_has_been_called_ = NO; |
| 120 swizzle_block_.reset([^(id self) { |
| 121 handle_startup_parameters_has_been_called_ = YES; |
| 122 } copy]); |
| 123 user_activity_handler_swizzler_.reset(new ScopedBlockSwizzler( |
| 124 [UserActivityHandler class], |
| 125 @selector(handleStartupParametersWithTabOpener: |
| 126 startupInformation: |
| 127 browserViewInformation:), |
| 128 swizzle_block_)); |
| 129 } |
| 130 |
| 131 BOOL getHandleStartupParametersHasBeenCalled() { |
| 132 return handle_startup_parameters_has_been_called_; |
| 133 } |
| 134 |
| 135 void resetHandleStartupParametersHasBeenCalled() { |
| 136 handle_startup_parameters_has_been_called_ = NO; |
| 137 } |
| 138 |
| 139 conditionBlock getCompletionHandler() { |
| 140 if (!completion_block_) { |
| 141 block_executed_ = NO; |
| 142 completion_block_.reset([^(BOOL arg) { |
| 143 block_executed_ = YES; |
| 144 block_argument_ = arg; |
| 145 } copy]); |
| 146 } |
| 147 return completion_block_; |
| 148 } |
| 149 |
| 150 BOOL completionHandlerExecuted() { return block_executed_; } |
| 151 |
| 152 BOOL completionHandlerArgument() { return block_argument_; } |
| 153 |
| 154 private: |
| 155 __block BOOL block_executed_; |
| 156 __block BOOL block_argument_; |
| 157 std::unique_ptr<ScopedBlockSwizzler> user_activity_handler_swizzler_; |
| 158 base::mac::ScopedBlock<startupParameterBlock> swizzle_block_; |
| 159 base::mac::ScopedBlock<conditionBlock> completion_block_; |
| 160 __block BOOL handle_startup_parameters_has_been_called_; |
| 161 }; |
| 162 |
| 163 #pragma mark - Tests. |
| 164 |
| 165 // Tests that Chrome notifies the user if we are passing a correct |
| 166 // userActivityType. |
| 167 TEST(UserActivityHandlerNoFixtureTest, |
| 168 willContinueUserActivityCorrectActivity) { |
| 169 EXPECT_TRUE([UserActivityHandler |
| 170 willContinueUserActivityWithType:handoff::kChromeHandoffActivityType]); |
| 171 |
| 172 if (spotlight::IsSpotlightAvailable()) { |
| 173 EXPECT_TRUE([UserActivityHandler |
| 174 willContinueUserActivityWithType:CSSearchableItemActionType]); |
| 175 } |
| 176 } |
| 177 |
| 178 // Tests that Chrome does not notifies the user if we are passing an incorrect |
| 179 // userActivityType. |
| 180 TEST(UserActivityHandlerNoFixtureTest, |
| 181 willContinueUserActivityIncorrectActivity) { |
| 182 EXPECT_FALSE([UserActivityHandler |
| 183 willContinueUserActivityWithType:[handoff::kChromeHandoffActivityType |
| 184 stringByAppendingString:@"test"]]); |
| 185 |
| 186 EXPECT_FALSE([UserActivityHandler |
| 187 willContinueUserActivityWithType:@"it.does.not.work"]); |
| 188 |
| 189 EXPECT_FALSE([UserActivityHandler willContinueUserActivityWithType:@""]); |
| 190 |
| 191 EXPECT_FALSE([UserActivityHandler willContinueUserActivityWithType:nil]); |
| 192 } |
| 193 |
| 194 // Tests that Chrome does not continue the activity is the activity type is |
| 195 // random. |
| 196 TEST(UserActivityHandlerNoFixtureTest, continueUserActivityFromGarbage) { |
| 197 // Setup. |
| 198 NSString* handoffWithSuffix = |
| 199 [handoff::kChromeHandoffActivityType stringByAppendingString:@"test"]; |
| 200 NSString* handoffWithPrefix = |
| 201 [@"test" stringByAppendingString:handoff::kChromeHandoffActivityType]; |
| 202 NSArray* userActivityTypes = @[ |
| 203 @"thisIsGarbage", @"it.does.not.work", handoffWithSuffix, handoffWithPrefix |
| 204 ]; |
| 205 for (NSString* userActivityType in userActivityTypes) { |
| 206 base::scoped_nsobject<NSUserActivity> userActivity( |
| 207 [[NSUserActivity alloc] initWithActivityType:userActivityType]); |
| 208 [userActivity setWebpageURL:[NSURL URLWithString:@"http://www.google.com"]]; |
| 209 |
| 210 // The test will fail is a method of those objects is called. |
| 211 id tabOpenerMock = [OCMockObject mockForProtocol:@protocol(TabOpening)]; |
| 212 id startupInformationMock = |
| 213 [OCMockObject mockForProtocol:@protocol(StartupInformation)]; |
| 214 |
| 215 // Action. |
| 216 BOOL result = |
| 217 [UserActivityHandler continueUserActivity:userActivity |
| 218 applicationIsActive:NO |
| 219 tabOpener:tabOpenerMock |
| 220 startupInformation:startupInformationMock]; |
| 221 |
| 222 // Tests. |
| 223 EXPECT_FALSE(result); |
| 224 } |
| 225 } |
| 226 |
| 227 // Tests that Chrome does not continue the activity if the webpage url is not |
| 228 // set. |
| 229 TEST(UserActivityHandlerNoFixtureTest, continueUserActivityNoWebpage) { |
| 230 // Setup. |
| 231 base::scoped_nsobject<NSUserActivity> userActivity([[NSUserActivity alloc] |
| 232 initWithActivityType:handoff::kChromeHandoffActivityType]); |
| 233 |
| 234 // The test will fail is a method of those objects is called. |
| 235 id tabOpenerMock = [OCMockObject mockForProtocol:@protocol(TabOpening)]; |
| 236 id startupInformationMock = |
| 237 [OCMockObject mockForProtocol:@protocol(StartupInformation)]; |
| 238 |
| 239 // Action. |
| 240 BOOL result = |
| 241 [UserActivityHandler continueUserActivity:userActivity |
| 242 applicationIsActive:NO |
| 243 tabOpener:tabOpenerMock |
| 244 startupInformation:startupInformationMock]; |
| 245 |
| 246 // Tests. |
| 247 EXPECT_FALSE(result); |
| 248 } |
| 249 |
| 250 // Tests that Chrome does not continue the activity if the activity is a |
| 251 // Spotlight action of an unknown type. |
| 252 TEST(UserActivityHandlerNoFixtureTest, |
| 253 continueUserActivitySpotlightActionFromGarbage) { |
| 254 // Only test Spotlight if it is enabled and available on the device. |
| 255 if (!spotlight::IsSpotlightAvailable()) { |
| 256 return; |
| 257 } |
| 258 // Setup. |
| 259 base::scoped_nsobject<NSUserActivity> userActivity( |
| 260 [[NSUserActivity alloc] initWithActivityType:CSSearchableItemActionType]); |
| 261 NSString* invalidAction = |
| 262 [NSString stringWithFormat:@"%@.invalidAction", |
| 263 spotlight::StringFromSpotlightDomain( |
| 264 spotlight::DOMAIN_ACTIONS)]; |
| 265 NSDictionary* userInfo = |
| 266 @{CSSearchableItemActivityIdentifier : invalidAction}; |
| 267 [userActivity addUserInfoEntriesFromDictionary:userInfo]; |
| 268 |
| 269 // Enable the SpotlightActions experiment. |
| 270 base::test::ScopedCommandLine scoped_command_line; |
| 271 scoped_command_line.GetProcessCommandLine()->AppendSwitch( |
| 272 switches::kEnableSpotlightActions); |
| 273 |
| 274 id tabOpenerMock = [OCMockObject mockForProtocol:@protocol(TabOpening)]; |
| 275 id startupInformationMock = |
| 276 [OCMockObject mockForProtocol:@protocol(StartupInformation)]; |
| 277 |
| 278 // Action. |
| 279 BOOL result = |
| 280 [UserActivityHandler continueUserActivity:userActivity |
| 281 applicationIsActive:NO |
| 282 tabOpener:tabOpenerMock |
| 283 startupInformation:startupInformationMock]; |
| 284 |
| 285 // Tests. |
| 286 EXPECT_FALSE(result); |
| 287 } |
| 288 |
| 289 // Tests that Chrome continues the activity if the application is in background |
| 290 // by saving the url to startupParameters. |
| 291 TEST(UserActivityHandlerNoFixtureTest, continueUserActivityBackground) { |
| 292 // Setup. |
| 293 base::scoped_nsobject<NSUserActivity> userActivity([[NSUserActivity alloc] |
| 294 initWithActivityType:handoff::kChromeHandoffActivityType]); |
| 295 NSURL* nsurl = [NSURL URLWithString:@"http://www.google.com"]; |
| 296 [userActivity setWebpageURL:nsurl]; |
| 297 |
| 298 id startupInformationMock = |
| 299 [OCMockObject niceMockForProtocol:@protocol(StartupInformation)]; |
| 300 [[startupInformationMock expect] |
| 301 setStartupParameters:[OCMArg checkWithBlock:^BOOL(id value) { |
| 302 EXPECT_TRUE([value isKindOfClass:[AppStartupParameters class]]); |
| 303 |
| 304 AppStartupParameters* startupParameters = (AppStartupParameters*)value; |
| 305 const GURL calledURL = startupParameters.externalURL; |
| 306 return calledURL == net::GURLWithNSURL(nsurl); |
| 307 }]]; |
| 308 |
| 309 // The test will fail is a method of this object is called. |
| 310 id tabOpenerMock = [OCMockObject mockForProtocol:@protocol(TabOpening)]; |
| 311 |
| 312 // Action. |
| 313 BOOL result = |
| 314 [UserActivityHandler continueUserActivity:userActivity |
| 315 applicationIsActive:NO |
| 316 tabOpener:tabOpenerMock |
| 317 startupInformation:startupInformationMock]; |
| 318 |
| 319 // Test. |
| 320 EXPECT_OCMOCK_VERIFY(startupInformationMock); |
| 321 EXPECT_TRUE(result); |
| 322 } |
| 323 |
| 324 // Tests that Chrome continues the activity if the application is in foreground |
| 325 // by opening a new tab. |
| 326 TEST(UserActivityHandlerNoFixtureTest, continueUserActivityForeground) { |
| 327 // Setup. |
| 328 base::scoped_nsobject<NSUserActivity> userActivity([[NSUserActivity alloc] |
| 329 initWithActivityType:handoff::kChromeHandoffActivityType]); |
| 330 NSURL* nsurl = [NSURL URLWithString:@"http://www.google.com"]; |
| 331 [userActivity setWebpageURL:nsurl]; |
| 332 |
| 333 base::scoped_nsobject<MockTabOpener> tabOpener([[MockTabOpener alloc] init]); |
| 334 |
| 335 id startupInformationMock = |
| 336 [OCMockObject mockForProtocol:@protocol(StartupInformation)]; |
| 337 [[[startupInformationMock stub] andReturnValue:@NO] isPresentingFirstRunUI]; |
| 338 |
| 339 base::scoped_nsobject<AppStartupParameters> startupParams( |
| 340 [[AppStartupParameters alloc] |
| 341 initWithExternalURL:(GURL("http://www.google.com"))]); |
| 342 [[[startupInformationMock stub] andReturn:startupParams] startupParameters]; |
| 343 |
| 344 // Action. |
| 345 BOOL result = |
| 346 [UserActivityHandler continueUserActivity:userActivity |
| 347 applicationIsActive:YES |
| 348 tabOpener:tabOpener |
| 349 startupInformation:startupInformationMock]; |
| 350 |
| 351 // Test. |
| 352 EXPECT_EQ(net::GURLWithNSURL(nsurl), [tabOpener url]); |
| 353 EXPECT_TRUE(result); |
| 354 } |
| 355 |
| 356 // Tests that a new tab is created when application is started via Universal |
| 357 // Link. |
| 358 TEST_F(UserActivityHandlerTest, continueUserActivityBrowsingWeb) { |
| 359 base::scoped_nsobject<NSUserActivity> userActivity([[NSUserActivity alloc] |
| 360 initWithActivityType:NSUserActivityTypeBrowsingWeb]); |
| 361 // This URL is passed to application by iOS but is not used in this part |
| 362 // of application logic. |
| 363 NSURL* nsurl = [NSURL URLWithString:@"http://goo.gl/foo/bar"]; |
| 364 [userActivity setWebpageURL:nsurl]; |
| 365 |
| 366 base::scoped_nsobject<MockTabOpener> tabOpener([[MockTabOpener alloc] init]); |
| 367 |
| 368 // Use an object to capture the startup paramters set by UserActivityHandler. |
| 369 base::scoped_nsobject<FakeStartupInformation> fakeStartupInformation( |
| 370 [[FakeStartupInformation alloc] init]); |
| 371 [fakeStartupInformation setIsPresentingFirstRunUI:NO]; |
| 372 |
| 373 BOOL result = |
| 374 [UserActivityHandler continueUserActivity:userActivity |
| 375 applicationIsActive:YES |
| 376 tabOpener:tabOpener |
| 377 startupInformation:fakeStartupInformation]; |
| 378 |
| 379 GURL newTabURL(kChromeUINewTabURL); |
| 380 EXPECT_EQ(newTabURL, [tabOpener url]); |
| 381 // AppStartupParameters default to opening pages in non-Incognito mode. |
| 382 EXPECT_EQ(ApplicationMode::NORMAL, [tabOpener applicationMode]); |
| 383 EXPECT_TRUE(result); |
| 384 // Verifies that a new tab is being requested. |
| 385 EXPECT_EQ(newTabURL, |
| 386 [[fakeStartupInformation startupParameters] externalURL]); |
| 387 } |
| 388 |
| 389 // Tests that continueUserActivity sets startupParameters accordingly to the |
| 390 // Spotlight action used. |
| 391 TEST_F(UserActivityHandlerTest, continueUserActivityShortcutActions) { |
| 392 // Only test Spotlight if it is enabled and available on the device. |
| 393 if (!spotlight::IsSpotlightAvailable()) { |
| 394 return; |
| 395 } |
| 396 // Setup. |
| 397 GURL gurlNewTab(kChromeUINewTabURL); |
| 398 base::scoped_nsobject<FakeStartupInformation> fakeStartupInformation( |
| 399 [[FakeStartupInformation alloc] init]); |
| 400 |
| 401 NSArray* parametersToTest = @[ |
| 402 @[ |
| 403 base::SysUTF8ToNSString(spotlight::kSpotlightActionNewTab), @NO, @NO, @NO |
| 404 ], |
| 405 @[ |
| 406 base::SysUTF8ToNSString(spotlight::kSpotlightActionNewIncognitoTab), @YES, |
| 407 @NO, @NO |
| 408 ], |
| 409 @[ |
| 410 base::SysUTF8ToNSString(spotlight::kSpotlightActionVoiceSearch), @NO, |
| 411 @YES, @NO |
| 412 ], |
| 413 @[ |
| 414 base::SysUTF8ToNSString(spotlight::kSpotlightActionQRScanner), @NO, @NO, |
| 415 @YES |
| 416 ] |
| 417 ]; |
| 418 |
| 419 // Enable the QR Scanner and Spotlight Actions experiments. |
| 420 base::test::ScopedCommandLine scoped_command_line; |
| 421 scoped_command_line.GetProcessCommandLine()->AppendSwitch( |
| 422 switches::kEnableQRScanner); |
| 423 scoped_command_line.GetProcessCommandLine()->AppendSwitch( |
| 424 switches::kEnableSpotlightActions); |
| 425 |
| 426 for (id parameters in parametersToTest) { |
| 427 base::scoped_nsobject<NSUserActivity> userActivity([[NSUserActivity alloc] |
| 428 initWithActivityType:CSSearchableItemActionType]); |
| 429 NSString* action = [NSString |
| 430 stringWithFormat:@"%@.%@", spotlight::StringFromSpotlightDomain( |
| 431 spotlight::DOMAIN_ACTIONS), |
| 432 parameters[0]]; |
| 433 NSDictionary* userInfo = @{CSSearchableItemActivityIdentifier : action}; |
| 434 [userActivity addUserInfoEntriesFromDictionary:userInfo]; |
| 435 |
| 436 id tabOpenerMock = [OCMockObject mockForProtocol:@protocol(TabOpening)]; |
| 437 |
| 438 // Action. |
| 439 BOOL result = |
| 440 [UserActivityHandler continueUserActivity:userActivity |
| 441 applicationIsActive:NO |
| 442 tabOpener:tabOpenerMock |
| 443 startupInformation:fakeStartupInformation]; |
| 444 |
| 445 // Tests. |
| 446 EXPECT_TRUE(result); |
| 447 EXPECT_EQ(gurlNewTab, |
| 448 [fakeStartupInformation startupParameters].externalURL); |
| 449 EXPECT_EQ([parameters[1] boolValue], |
| 450 [fakeStartupInformation startupParameters].launchInIncognito); |
| 451 EXPECT_EQ([parameters[2] boolValue], |
| 452 [fakeStartupInformation startupParameters].launchVoiceSearch); |
| 453 EXPECT_EQ([parameters[3] boolValue], |
| 454 [fakeStartupInformation startupParameters].launchQRScanner); |
| 455 } |
| 456 } |
| 457 |
| 458 // Tests that handleStartupParameters with a non-U2F url opens a new tab. |
| 459 TEST(UserActivityHandlerNoFixtureTest, handleStartupParamsNonU2F) { |
| 460 // Setup. |
| 461 GURL gurl("http://www.google.com"); |
| 462 |
| 463 base::scoped_nsobject<AppStartupParameters> startupParams( |
| 464 [[AppStartupParameters alloc] initWithExternalURL:gurl]); |
| 465 [startupParams setLaunchInIncognito:YES]; |
| 466 |
| 467 id startupInformationMock = |
| 468 [OCMockObject mockForProtocol:@protocol(StartupInformation)]; |
| 469 [[[startupInformationMock stub] andReturnValue:@NO] isPresentingFirstRunUI]; |
| 470 [[[startupInformationMock stub] andReturn:startupParams] startupParameters]; |
| 471 [[startupInformationMock expect] setStartupParameters:nil]; |
| 472 |
| 473 base::scoped_nsobject<MockTabOpener> tabOpener([[MockTabOpener alloc] init]); |
| 474 |
| 475 // The test will fail is a method of this object is called. |
| 476 id browserViewMock = |
| 477 [OCMockObject mockForProtocol:@protocol(BrowserViewInformation)]; |
| 478 |
| 479 // Action. |
| 480 [UserActivityHandler |
| 481 handleStartupParametersWithTabOpener:tabOpener |
| 482 startupInformation:startupInformationMock |
| 483 browserViewInformation:browserViewMock]; |
| 484 [tabOpener completionBlock](); |
| 485 |
| 486 // Tests. |
| 487 EXPECT_OCMOCK_VERIFY(startupInformationMock); |
| 488 EXPECT_EQ(gurl, [tabOpener url]); |
| 489 EXPECT_EQ(ApplicationMode::INCOGNITO, [tabOpener applicationMode]); |
| 490 } |
| 491 |
| 492 // Tests that handleStartupParameters with a U2F url opens in the correct tab. |
| 493 TEST(UserActivityHandlerNoFixtureTest, handleStartupParamsU2F) { |
| 494 // Setup. |
| 495 GURL gurl("chromium://u2f-callback?isU2F=1&tabID=B05B1860"); |
| 496 NSString* tabID = [U2FController tabIDFromResponseURL:gurl]; |
| 497 |
| 498 base::scoped_nsobject<AppStartupParameters> startupParams( |
| 499 [[AppStartupParameters alloc] initWithExternalURL:gurl]); |
| 500 [startupParams setLaunchInIncognito:YES]; |
| 501 |
| 502 base::scoped_nsobject<UserActivityHandlerTabMock> tabMock( |
| 503 [[UserActivityHandlerTabMock alloc] init]); |
| 504 id tabOCMock = [OCMockObject partialMockForObject:tabMock]; |
| 505 [[[tabOCMock stub] andReturn:tabID] tabId]; |
| 506 |
| 507 base::scoped_nsobject<UserActivityHandlerTabModelMock> tabModel( |
| 508 [[UserActivityHandlerTabModelMock alloc] init]); |
| 509 [tabModel addTab:(Tab*)tabMock.get()]; |
| 510 |
| 511 id startupInformationMock = |
| 512 [OCMockObject mockForProtocol:@protocol(StartupInformation)]; |
| 513 [[[startupInformationMock stub] andReturnValue:@NO] isPresentingFirstRunUI]; |
| 514 [[[startupInformationMock stub] andReturn:startupParams] startupParameters]; |
| 515 [[startupInformationMock expect] setStartupParameters:nil]; |
| 516 |
| 517 id browserViewInformationMock = |
| 518 [OCMockObject mockForProtocol:@protocol(BrowserViewInformation)]; |
| 519 [[[browserViewInformationMock stub] andReturn:(TabModel*)tabModel.get()] |
| 520 mainTabModel]; |
| 521 [[[browserViewInformationMock stub] andReturn:(TabModel*)tabModel.get()] |
| 522 otrTabModel]; |
| 523 |
| 524 base::scoped_nsobject<MockTabOpener> tabOpener([[MockTabOpener alloc] init]); |
| 525 |
| 526 // Action. |
| 527 [UserActivityHandler |
| 528 handleStartupParametersWithTabOpener:tabOpener |
| 529 startupInformation:startupInformationMock |
| 530 browserViewInformation:browserViewInformationMock]; |
| 531 |
| 532 // Tests. |
| 533 EXPECT_OCMOCK_VERIFY(startupInformationMock); |
| 534 EXPECT_EQ(gurl, [tabMock url]); |
| 535 } |
| 536 |
| 537 // Tests that performActionForShortcutItem set startupParameters accordingly to |
| 538 // the shortcut used |
| 539 TEST_F(UserActivityHandlerTest, performActionForShortcutItemWithRealShortcut) { |
| 540 // Setup. |
| 541 GURL gurlNewTab("chrome://newtab/"); |
| 542 |
| 543 base::scoped_nsobject<FakeStartupInformation> fakeStartupInformation( |
| 544 [[FakeStartupInformation alloc] init]); |
| 545 [fakeStartupInformation setIsPresentingFirstRunUI:NO]; |
| 546 |
| 547 NSArray* parametersToTest = @[ |
| 548 @[ @"OpenNewTab", @NO, @NO, @NO ], @[ @"OpenIncognitoTab", @YES, @NO, @NO ], |
| 549 @[ @"OpenVoiceSearch", @NO, @YES, @NO ], |
| 550 @[ @"OpenQRScanner", @NO, @NO, @YES ] |
| 551 ]; |
| 552 |
| 553 // Enable the QR Scanner experiment. |
| 554 base::test::ScopedCommandLine scoped_command_line; |
| 555 scoped_command_line.GetProcessCommandLine()->AppendSwitch( |
| 556 switches::kEnableQRScanner); |
| 557 |
| 558 swizzleHandleStartupParameters(); |
| 559 |
| 560 for (id parameters in parametersToTest) { |
| 561 base::scoped_nsobject<UIApplicationShortcutItem> shortcut( |
| 562 [[UIApplicationShortcutItem alloc] initWithType:parameters[0] |
| 563 localizedTitle:parameters[0]]); |
| 564 |
| 565 resetHandleStartupParametersHasBeenCalled(); |
| 566 |
| 567 // The test will fail is a method of those objects is called. |
| 568 id tabOpenerMock = [OCMockObject mockForProtocol:@protocol(TabOpening)]; |
| 569 id browserViewInformationMock = |
| 570 [OCMockObject mockForProtocol:@protocol(BrowserViewInformation)]; |
| 571 |
| 572 // Action. |
| 573 [UserActivityHandler |
| 574 performActionForShortcutItem:shortcut |
| 575 completionHandler:getCompletionHandler() |
| 576 tabOpener:tabOpenerMock |
| 577 startupInformation:fakeStartupInformation |
| 578 browserViewInformation:browserViewInformationMock]; |
| 579 |
| 580 // Tests. |
| 581 EXPECT_EQ(gurlNewTab, |
| 582 [fakeStartupInformation startupParameters].externalURL); |
| 583 EXPECT_EQ([[parameters objectAtIndex:1] boolValue], |
| 584 [fakeStartupInformation startupParameters].launchInIncognito); |
| 585 EXPECT_EQ([[parameters objectAtIndex:2] boolValue], |
| 586 [fakeStartupInformation startupParameters].launchVoiceSearch); |
| 587 EXPECT_EQ([[parameters objectAtIndex:3] boolValue], |
| 588 [fakeStartupInformation startupParameters].launchQRScanner); |
| 589 EXPECT_TRUE(completionHandlerExecuted()); |
| 590 EXPECT_TRUE(completionHandlerArgument()); |
| 591 EXPECT_TRUE(getHandleStartupParametersHasBeenCalled()); |
| 592 } |
| 593 } |
| 594 |
| 595 // Tests that performActionForShortcutItem just executes the completionHandler |
| 596 // with NO if the firstRunUI is present. |
| 597 TEST_F(UserActivityHandlerTest, performActionForShortcutItemWithFirstRunUI) { |
| 598 // Setup. |
| 599 id startupInformationMock = |
| 600 [OCMockObject mockForProtocol:@protocol(StartupInformation)]; |
| 601 [[[startupInformationMock stub] andReturnValue:@YES] isPresentingFirstRunUI]; |
| 602 |
| 603 base::scoped_nsobject<UIApplicationShortcutItem> shortcut( |
| 604 [[UIApplicationShortcutItem alloc] initWithType:@"OpenNewTab" |
| 605 localizedTitle:@""]); |
| 606 |
| 607 swizzleHandleStartupParameters(); |
| 608 |
| 609 // The test will fail is a method of those objects is called. |
| 610 id tabOpenerMock = [OCMockObject mockForProtocol:@protocol(TabOpening)]; |
| 611 id browserViewInformationMock = |
| 612 [OCMockObject mockForProtocol:@protocol(BrowserViewInformation)]; |
| 613 |
| 614 // Action. |
| 615 [UserActivityHandler performActionForShortcutItem:shortcut |
| 616 completionHandler:getCompletionHandler() |
| 617 tabOpener:tabOpenerMock |
| 618 startupInformation:startupInformationMock |
| 619 browserViewInformation:browserViewInformationMock]; |
| 620 |
| 621 // Tests. |
| 622 EXPECT_TRUE(completionHandlerExecuted()); |
| 623 EXPECT_FALSE(completionHandlerArgument()); |
| 624 EXPECT_FALSE(getHandleStartupParametersHasBeenCalled()); |
| 625 } |
OLD | NEW |