tab_opener_unittest.mm 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // Copyright 2012 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. #import <Foundation/Foundation.h>
  5. #include "base/threading/thread.h"
  6. #include "components/sync_preferences/pref_service_mock_factory.h"
  7. #include "components/sync_preferences/pref_service_syncable.h"
  8. #import "ios/chrome/app/application_delegate/app_state.h"
  9. #include "ios/chrome/app/application_delegate/startup_information.h"
  10. #import "ios/chrome/app/application_delegate/tab_opening.h"
  11. #import "ios/chrome/app/application_delegate/url_opener.h"
  12. #import "ios/chrome/app/application_delegate/url_opener_params.h"
  13. #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
  14. #import "ios/chrome/browser/prefs/browser_prefs.h"
  15. #import "ios/chrome/browser/ui/main/scene_controller.h"
  16. #import "ios/chrome/browser/ui/main/scene_state.h"
  17. #import "ios/chrome/browser/ui/main/test/stub_browser_interface.h"
  18. #import "ios/testing/scoped_block_swizzler.h"
  19. #include "ios/web/public/test/web_task_environment.h"
  20. #include "testing/platform_test.h"
  21. #import "third_party/ocmock/OCMock/OCMock.h"
  22. #if !defined(__has_feature) || !__has_feature(objc_arc)
  23. #error "This file requires ARC support."
  24. #endif
  25. @interface SceneController (Testing)
  26. - (id<BrowserInterface>)currentInterface;
  27. @end
  28. namespace {
  29. // A block that takes the arguments of
  30. // +handleLaunchOptions:applicationActive:tabOpener:startupInformation: and
  31. // returns nothing.
  32. typedef void (^HandleLaunchOptions)(id self,
  33. NSDictionary* options,
  34. id<TabOpening> tabOpener,
  35. id<StartupInformation> startupInformation,
  36. AppState* appState);
  37. class TabOpenerTest : public PlatformTest {
  38. protected:
  39. void TearDown() override {
  40. PlatformTest::TearDown();
  41. }
  42. BOOL swizzleHasBeenCalled() { return swizzle_block_executed_; }
  43. void swizzleHandleLaunchOptions(
  44. URLOpenerParams* expectedParams,
  45. id<ConnectionInformation> expectedConnectionInformation,
  46. id<StartupInformation> expectedStartupInformation,
  47. AppState* expectedAppState) {
  48. swizzle_block_executed_ = NO;
  49. swizzle_block_ =
  50. [^(id self, URLOpenerParams* params, id<TabOpening> tabOpener,
  51. id<ConnectionInformation> connectionInformation,
  52. id<StartupInformation> startupInformation, AppState* appState) {
  53. swizzle_block_executed_ = YES;
  54. EXPECT_EQ(expectedParams, params);
  55. EXPECT_EQ(expectedConnectionInformation, connectionInformation);
  56. EXPECT_EQ(expectedStartupInformation, startupInformation);
  57. EXPECT_EQ(scene_controller_, tabOpener);
  58. EXPECT_EQ(expectedAppState, appState);
  59. } copy];
  60. URL_opening_handle_launch_swizzler_.reset(new ScopedBlockSwizzler(
  61. [URLOpener class],
  62. @selector(handleLaunchOptions:
  63. tabOpener:connectionInformation:startupInformation
  64. :appState:prefService:),
  65. swizzle_block_));
  66. }
  67. SceneController* GetSceneController() {
  68. if (!scene_controller_) {
  69. StubBrowserInterface* browser_interface =
  70. [[StubBrowserInterface alloc] init];
  71. sync_preferences::PrefServiceMockFactory factory;
  72. scoped_refptr<user_prefs::PrefRegistrySyncable> registry(
  73. new user_prefs::PrefRegistrySyncable);
  74. RegisterBrowserStatePrefs(registry.get());
  75. TestChromeBrowserState::Builder builder;
  76. builder.SetPrefService(factory.CreateSyncable(registry.get()));
  77. browser_state_ = builder.Build();
  78. browser_interface.browserState =
  79. (ChromeBrowserState*)browser_state_.get();
  80. SceneController* controller =
  81. [[SceneController alloc] initWithSceneState:scene_state_];
  82. mockController_ = OCMPartialMock(controller);
  83. OCMStub([mockController_ currentInterface]).andReturn(browser_interface);
  84. scene_controller_ = controller;
  85. }
  86. return scene_controller_;
  87. }
  88. private:
  89. web::WebTaskEnvironment task_environment_;
  90. // Keep the partial mock object alive to avoid automatic deallocation when out
  91. // of scope.
  92. id mockController_;
  93. std::unique_ptr<TestChromeBrowserState> browser_state_;
  94. SceneState* scene_state_;
  95. SceneController* scene_controller_;
  96. __block BOOL swizzle_block_executed_;
  97. HandleLaunchOptions swizzle_block_;
  98. std::unique_ptr<ScopedBlockSwizzler> URL_opening_handle_launch_swizzler_;
  99. };
  100. #pragma mark - Tests.
  101. // Tests that -newTabFromLaunchOptions calls +handleLaunchOption and reset
  102. // options.
  103. TEST_F(TabOpenerTest, openTabFromLaunchWithParamsWithOptions) {
  104. // Setup.
  105. NSString* sourceApplication = @"com.apple.mobilesafari";
  106. URLOpenerParams* params =
  107. [[URLOpenerParams alloc] initWithURL:nil
  108. sourceApplication:sourceApplication];
  109. id startupInformationMock =
  110. [OCMockObject mockForProtocol:@protocol(StartupInformation)];
  111. id appStateMock = [OCMockObject mockForClass:[AppState class]];
  112. id<TabOpening> tabOpener = GetSceneController();
  113. id<ConnectionInformation> connectionInformation = GetSceneController();
  114. swizzleHandleLaunchOptions(params, connectionInformation,
  115. startupInformationMock, appStateMock);
  116. // Action.
  117. [tabOpener openTabFromLaunchWithParams:params
  118. startupInformation:startupInformationMock
  119. appState:appStateMock];
  120. // Test.
  121. EXPECT_TRUE(swizzleHasBeenCalled());
  122. }
  123. // Tests that -newTabFromLaunchOptions do nothing if launchOptions is nil.
  124. TEST_F(TabOpenerTest, openTabFromLaunchWithParamsWithNil) {
  125. // Setup.
  126. id startupInformationMock =
  127. [OCMockObject mockForProtocol:@protocol(StartupInformation)];
  128. id appStateMock = [OCMockObject mockForClass:[AppState class]];
  129. id<TabOpening> tabOpener = GetSceneController();
  130. id<ConnectionInformation> connectionInformation = GetSceneController();
  131. swizzleHandleLaunchOptions(nil, connectionInformation, startupInformationMock,
  132. appStateMock);
  133. // Action.
  134. [tabOpener openTabFromLaunchWithParams:nil
  135. startupInformation:startupInformationMock
  136. appState:appStateMock];
  137. // Test.
  138. EXPECT_FALSE(swizzleHasBeenCalled());
  139. }
  140. } // namespace