OLD | NEW |
(Empty) | |
| 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 |
| 5 #import <Foundation/Foundation.h> |
| 6 |
| 7 #import "base/mac/bind_objc_block.h" |
| 8 #include "base/threading/thread.h" |
| 9 #import "ios/chrome/app/application_delegate/app_state.h" |
| 10 #import "ios/chrome/app/application_delegate/url_opener.h" |
| 11 #include "ios/chrome/app/main_controller_private.h" |
| 12 #import "ios/chrome/browser/tabs/tab_model.h" |
| 13 #import "ios/chrome/test/base/scoped_block_swizzler.h" |
| 14 #include "testing/platform_test.h" |
| 15 #import "third_party/ocmock/OCMock/OCMock.h" |
| 16 |
| 17 #pragma mark - MainController Testing Additions |
| 18 |
| 19 @interface MainController (TestingAdditions) |
| 20 - (id)initForTesting; |
| 21 @end |
| 22 |
| 23 @implementation MainController (TestingAdditions) |
| 24 - (id)initForTesting { |
| 25 self = [self init]; |
| 26 if (self) { |
| 27 [self setUpAsForegrounded]; |
| 28 id mainTabModel = [OCMockObject mockForClass:[TabModel class]]; |
| 29 [[mainTabModel stub] resetSessionMetrics]; |
| 30 [[mainTabModel stub] browserStateDestroyed]; |
| 31 [[mainTabModel stub] addObserver:[OCMArg any]]; |
| 32 [[mainTabModel stub] removeObserver:[OCMArg any]]; |
| 33 [[self browserViewInformation] setMainTabModel:mainTabModel]; |
| 34 } |
| 35 return self; |
| 36 } |
| 37 |
| 38 @end |
| 39 |
| 40 #pragma mark - MainController Test |
| 41 |
| 42 namespace { |
| 43 |
| 44 // A block that takes the arguments of |
| 45 // +handleLaunchOptions:applicationActive:tabOpener:startupInformation: and |
| 46 // returns nothing. |
| 47 typedef void (^HandleLaunchOptions)(id self, |
| 48 NSDictionary* options, |
| 49 BOOL applicationActive, |
| 50 id<TabOpening> tabOpener, |
| 51 id<StartupInformation> startupInformation, |
| 52 AppState* appState); |
| 53 |
| 54 class TabOpenerTest : public PlatformTest { |
| 55 protected: |
| 56 BOOL swizzleHasBeenCalled() { return swizzle_block_executed_; } |
| 57 |
| 58 void swizzleHandleLaunchOptions( |
| 59 NSDictionary* expectedLaunchOptions, |
| 60 id<StartupInformation> expectedStartupInformation, |
| 61 AppState* expectedAppState) { |
| 62 swizzle_block_executed_ = NO; |
| 63 swizzle_block_.reset( |
| 64 [^(id self, NSDictionary* options, BOOL applicationActive, |
| 65 id<TabOpening> tabOpener, id<StartupInformation> startupInformation, |
| 66 AppState* appState) { |
| 67 swizzle_block_executed_ = YES; |
| 68 EXPECT_EQ(expectedLaunchOptions, options); |
| 69 EXPECT_EQ(expectedStartupInformation, startupInformation); |
| 70 EXPECT_EQ(main_controller_.get(), tabOpener); |
| 71 EXPECT_EQ(expectedAppState, appState); |
| 72 } copy]); |
| 73 URL_opening_handle_launch_swizzler_.reset(new ScopedBlockSwizzler( |
| 74 [URLOpener class], @selector(handleLaunchOptions: |
| 75 applicationActive: |
| 76 tabOpener: |
| 77 startupInformation: |
| 78 appState:), |
| 79 swizzle_block_)); |
| 80 } |
| 81 |
| 82 MainController* GetMainController() { |
| 83 if (!main_controller_.get()) { |
| 84 main_controller_.reset([[MainController alloc] initForTesting]); |
| 85 } |
| 86 return main_controller_.get(); |
| 87 } |
| 88 |
| 89 private: |
| 90 base::scoped_nsobject<MainController> main_controller_; |
| 91 __block BOOL swizzle_block_executed_; |
| 92 base::mac::ScopedBlock<HandleLaunchOptions> swizzle_block_; |
| 93 std::unique_ptr<ScopedBlockSwizzler> URL_opening_handle_launch_swizzler_; |
| 94 }; |
| 95 |
| 96 #pragma mark - Tests. |
| 97 |
| 98 // Tests that -newTabFromLaunchOptions calls +handleLaunchOption and reset |
| 99 // options. |
| 100 TEST_F(TabOpenerTest, openTabFromLaunchOptionsWithOptions) { |
| 101 // Setup. |
| 102 NSString* sourceApplication = @"com.apple.mobilesafari"; |
| 103 NSDictionary* launchOptions = |
| 104 @{UIApplicationLaunchOptionsSourceApplicationKey : sourceApplication}; |
| 105 |
| 106 id startupInformationMock = |
| 107 [OCMockObject mockForProtocol:@protocol(StartupInformation)]; |
| 108 id appStateMock = [OCMockObject mockForClass:[AppState class]]; |
| 109 |
| 110 swizzleHandleLaunchOptions(launchOptions, startupInformationMock, |
| 111 appStateMock); |
| 112 |
| 113 id<TabOpening> tabOpener = GetMainController(); |
| 114 |
| 115 // Action. |
| 116 [tabOpener openTabFromLaunchOptions:launchOptions |
| 117 startupInformation:startupInformationMock |
| 118 appState:appStateMock]; |
| 119 |
| 120 // Test. |
| 121 EXPECT_TRUE(swizzleHasBeenCalled()); |
| 122 } |
| 123 |
| 124 // Tests that -newTabFromLaunchOptions do nothing if launchOptions is nil. |
| 125 TEST_F(TabOpenerTest, openTabFromLaunchOptionsWithNil) { |
| 126 // Setup. |
| 127 id startupInformationMock = |
| 128 [OCMockObject mockForProtocol:@protocol(StartupInformation)]; |
| 129 id appStateMock = [OCMockObject mockForClass:[AppState class]]; |
| 130 |
| 131 swizzleHandleLaunchOptions(nil, startupInformationMock, appStateMock); |
| 132 |
| 133 id<TabOpening> tabOpener = GetMainController(); |
| 134 |
| 135 // Action. |
| 136 [tabOpener openTabFromLaunchOptions:nil |
| 137 startupInformation:startupInformationMock |
| 138 appState:appStateMock]; |
| 139 |
| 140 // Test. |
| 141 EXPECT_FALSE(swizzleHasBeenCalled()); |
| 142 } |
| 143 } // namespace |
OLD | NEW |