OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ios/chrome/app/application_delegate/url_opener.h" | 5 #include "ios/chrome/app/application_delegate/url_opener.h" |
6 | 6 |
7 #import <Foundation/Foundation.h> | 7 #import <Foundation/Foundation.h> |
8 | 8 |
9 #include "base/mac/scoped_nsobject.h" | |
10 #include "ios/chrome/app/application_delegate/app_state.h" | 9 #include "ios/chrome/app/application_delegate/app_state.h" |
11 #include "ios/chrome/app/application_delegate/app_state_testing.h" | 10 #include "ios/chrome/app/application_delegate/app_state_testing.h" |
12 #include "ios/chrome/app/application_delegate/mock_tab_opener.h" | 11 #include "ios/chrome/app/application_delegate/mock_tab_opener.h" |
13 #include "ios/chrome/app/chrome_app_startup_parameters.h" | 12 #include "ios/chrome/app/chrome_app_startup_parameters.h" |
14 #include "ios/chrome/app/main_application_delegate.h" | 13 #include "ios/chrome/app/main_application_delegate.h" |
15 #include "ios/chrome/app/main_controller.h" | 14 #include "ios/chrome/app/main_controller.h" |
16 #include "ios/chrome/app/main_controller_private.h" | 15 #include "ios/chrome/app/main_controller_private.h" |
17 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" | 16 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" |
18 #import "ios/chrome/browser/tabs/tab.h" | 17 #import "ios/chrome/browser/tabs/tab.h" |
19 #import "ios/chrome/browser/ui/browser_view_controller.h" | 18 #import "ios/chrome/browser/ui/browser_view_controller.h" |
20 #import "ios/chrome/test/base/scoped_block_swizzler.h" | 19 #import "ios/chrome/test/base/scoped_block_swizzler.h" |
21 #import "ios/testing/ocmock_complex_type_helper.h" | 20 #import "ios/testing/ocmock_complex_type_helper.h" |
22 #import "net/base/mac/url_conversions.h" | 21 #import "net/base/mac/url_conversions.h" |
23 #include "testing/platform_test.h" | 22 #include "testing/platform_test.h" |
24 #import "third_party/ocmock/OCMock/OCMock.h" | 23 #import "third_party/ocmock/OCMock/OCMock.h" |
25 #include "third_party/ocmock/gtest_support.h" | 24 #include "third_party/ocmock/gtest_support.h" |
26 | 25 |
| 26 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 27 #error "This file requires ARC support." |
| 28 #endif |
| 29 |
27 #pragma mark - Tab Switcher Mock | 30 #pragma mark - Tab Switcher Mock |
28 | 31 |
29 // This mocks either a iPad tab switcher controller or a iPhone stack view | 32 // This mocks either a iPad tab switcher controller or a iPhone stack view |
30 // controller. | 33 // controller. |
31 @interface URLOpenerOCMockComplexTypeHandler : OCMockComplexTypeHelper | 34 @interface URLOpenerOCMockComplexTypeHandler : OCMockComplexTypeHelper |
32 @end | 35 @end |
33 | 36 |
34 @implementation URLOpenerOCMockComplexTypeHandler | 37 @implementation URLOpenerOCMockComplexTypeHandler |
35 | 38 |
36 typedef Tab* (^mock_gurl_nsuinteger_pagetransition)(const GURL&, | 39 typedef Tab* (^mock_gurl_nsuinteger_pagetransition)(const GURL&, |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 | 108 |
106 - (TabModel*)tabModel { | 109 - (TabModel*)tabModel { |
107 return nil; | 110 return nil; |
108 } | 111 } |
109 | 112 |
110 @end | 113 @end |
111 | 114 |
112 class URLOpenerTest : public PlatformTest { | 115 class URLOpenerTest : public PlatformTest { |
113 protected: | 116 protected: |
114 MainController* GetMainController() { | 117 MainController* GetMainController() { |
115 if (!main_controller_.get()) { | 118 if (!main_controller_) { |
116 main_controller_.reset([[MainController alloc] init]); | 119 main_controller_ = [[MainController alloc] init]; |
117 [main_controller_ setUpAsForegrounded]; | 120 [main_controller_ setUpAsForegrounded]; |
118 id mainTabModel = [OCMockObject mockForClass:[TabModel class]]; | 121 id mainTabModel = [OCMockObject mockForClass:[TabModel class]]; |
119 [[mainTabModel stub] resetSessionMetrics]; | 122 [[mainTabModel stub] resetSessionMetrics]; |
120 [[mainTabModel stub] browserStateDestroyed]; | 123 [[mainTabModel stub] browserStateDestroyed]; |
121 [[mainTabModel stub] addObserver:[OCMArg any]]; | 124 [[mainTabModel stub] addObserver:[OCMArg any]]; |
122 [[mainTabModel stub] removeObserver:[OCMArg any]]; | 125 [[mainTabModel stub] removeObserver:[OCMArg any]]; |
123 [[main_controller_ browserViewInformation] setMainTabModel:mainTabModel]; | 126 [[main_controller_ browserViewInformation] setMainTabModel:mainTabModel]; |
124 } | 127 } |
125 return main_controller_.get(); | 128 return main_controller_; |
126 } | 129 } |
127 | 130 |
128 private: | 131 private: |
129 base::scoped_nsobject<MainController> main_controller_; | 132 MainController* main_controller_; |
130 }; | 133 }; |
131 | 134 |
132 TEST_F(URLOpenerTest, HandleOpenURLWithNoOpenTab) { | 135 TEST_F(URLOpenerTest, HandleOpenURLWithNoOpenTab) { |
133 // The tab switcher controller should be dismissed with a new tab containing | 136 // The tab switcher controller should be dismissed with a new tab containing |
134 // the external URL. | 137 // the external URL. |
135 NSURL* url = [NSURL URLWithString:@"chromium://www.google.com"]; | 138 NSURL* url = [NSURL URLWithString:@"chromium://www.google.com"]; |
136 base::scoped_nsobject<ChromeAppStartupParameters> params( | 139 ChromeAppStartupParameters* params = |
137 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url | 140 [ChromeAppStartupParameters newChromeAppStartupParametersWithURL:url |
138 fromSourceApplication:nil]); | 141 fromSourceApplication:nil]; |
139 | 142 |
140 base::scoped_nsobject<id> bvcMock([[URLOpenerMockBVC alloc] init]); | 143 id bvcMock = [[URLOpenerMockBVC alloc] init]; |
141 | 144 |
142 base::scoped_nsobject<id> tabSwitcherController; | 145 id tabSwitcherController; |
143 tabSwitcherController.reset([[URLOpenerOCMockComplexTypeHandler alloc] | 146 tabSwitcherController = [[URLOpenerOCMockComplexTypeHandler alloc] |
144 initWithRepresentedObject:[OCMockObject | 147 initWithRepresentedObject:[OCMockObject |
145 mockForProtocol:@protocol(UrlLoader)]]); | 148 mockForProtocol:@protocol(UrlLoader)]]; |
146 | 149 |
147 base::scoped_nsobject<id> block([(id) ^ (const GURL& url, NSUInteger position, | 150 id block = [(id) ^ (const GURL& url, NSUInteger position, |
148 ui::PageTransition transition) { | 151 ui::PageTransition transition) { |
149 EXPECT_EQ(url, [params externalURL]); | 152 EXPECT_EQ(url, [params externalURL]); |
150 EXPECT_EQ(NSNotFound, static_cast<NSInteger>(position)); | 153 EXPECT_EQ(NSNotFound, static_cast<NSInteger>(position)); |
151 EXPECT_TRUE(PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK)); | 154 EXPECT_TRUE(PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_LINK)); |
152 } copy]); | 155 } copy]; |
153 SEL dismissSelector = | 156 SEL dismissSelector = |
154 @selector(dismissWithNewTabAnimationToModel:withURL:atIndex:transition:); | 157 @selector(dismissWithNewTabAnimationToModel:withURL:atIndex:transition:); |
155 [tabSwitcherController onSelector:dismissSelector callBlockExpectation:block]; | 158 [tabSwitcherController onSelector:dismissSelector callBlockExpectation:block]; |
156 | 159 |
157 // Setup main controller. | 160 // Setup main controller. |
158 MainController* controller = GetMainController(); | 161 MainController* controller = GetMainController(); |
159 controller.browserViewInformation.mainBVC = bvcMock; | 162 controller.browserViewInformation.mainBVC = bvcMock; |
160 controller.tabSwitcherController = tabSwitcherController; | 163 controller.tabSwitcherController = tabSwitcherController; |
161 controller.tabSwitcherActive = YES; | 164 controller.tabSwitcherActive = YES; |
162 | 165 |
163 id mainApplicationDelegate = | 166 id mainApplicationDelegate = |
164 [OCMockObject mockForClass:[MainApplicationDelegate class]]; | 167 [OCMockObject mockForClass:[MainApplicationDelegate class]]; |
165 | 168 |
166 AppState* appState = | 169 AppState* appState = |
167 [[[AppState alloc] initWithBrowserLauncher:controller | 170 [[AppState alloc] initWithBrowserLauncher:controller |
168 startupInformation:controller | 171 startupInformation:controller |
169 applicationDelegate:mainApplicationDelegate | 172 applicationDelegate:mainApplicationDelegate |
170 window:controller.window | 173 window:controller.window |
171 shouldOpenNTP:YES] autorelease]; | 174 shouldOpenNTP:YES]; |
172 controller.appState = appState; | 175 controller.appState = appState; |
173 | 176 |
174 NSDictionary<NSString*, id>* options = nil; | 177 NSDictionary<NSString*, id>* options = nil; |
175 [URLOpener openURL:url | 178 [URLOpener openURL:url |
176 applicationActive:YES | 179 applicationActive:YES |
177 options:options | 180 options:options |
178 tabOpener:controller | 181 tabOpener:controller |
179 startupInformation:controller]; | 182 startupInformation:controller]; |
180 | 183 |
181 EXPECT_OCMOCK_VERIFY(tabSwitcherController); | 184 EXPECT_OCMOCK_VERIFY(tabSwitcherController); |
182 } | 185 } |
183 | 186 |
184 TEST_F(URLOpenerTest, HandleOpenURLWithOpenTabs) { | 187 TEST_F(URLOpenerTest, HandleOpenURLWithOpenTabs) { |
185 NSURL* url = [NSURL URLWithString:@"chromium://www.google.com"]; | 188 NSURL* url = [NSURL URLWithString:@"chromium://www.google.com"]; |
186 base::scoped_nsobject<URLOpenerMockBVC> bvc_mock( | 189 URLOpenerMockBVC* bvc_mock = [[URLOpenerMockBVC alloc] init]; |
187 [[URLOpenerMockBVC alloc] init]); | 190 URLOpenerMockBVC* otr_bvc_mock = [[URLOpenerMockBVC alloc] init]; |
188 base::scoped_nsobject<URLOpenerMockBVC> otr_bvc_mock( | |
189 [[URLOpenerMockBVC alloc] init]); | |
190 TestChromeBrowserState::Builder main_browser_state_builder; | 191 TestChromeBrowserState::Builder main_browser_state_builder; |
191 std::unique_ptr<TestChromeBrowserState> chrome_browser_state = | 192 std::unique_ptr<TestChromeBrowserState> chrome_browser_state = |
192 main_browser_state_builder.Build(); | 193 main_browser_state_builder.Build(); |
193 bvc_mock.get().browserState = chrome_browser_state.get(); | 194 bvc_mock.browserState = chrome_browser_state.get(); |
194 | 195 |
195 // Setup main controller. | 196 // Setup main controller. |
196 MainController* controller = GetMainController(); | 197 MainController* controller = GetMainController(); |
197 controller.browserViewInformation.mainBVC = | 198 controller.browserViewInformation.mainBVC = |
198 static_cast<BrowserViewController*>(bvc_mock.get()); | 199 static_cast<BrowserViewController*>(bvc_mock); |
199 controller.browserViewInformation.otrBVC = | 200 controller.browserViewInformation.otrBVC = |
200 static_cast<BrowserViewController*>(otr_bvc_mock.get()); | 201 static_cast<BrowserViewController*>(otr_bvc_mock); |
201 | 202 |
202 NSDictionary<NSString*, id>* options = nil; | 203 NSDictionary<NSString*, id>* options = nil; |
203 [URLOpener openURL:url | 204 [URLOpener openURL:url |
204 applicationActive:YES | 205 applicationActive:YES |
205 options:options | 206 options:options |
206 tabOpener:controller | 207 tabOpener:controller |
207 startupInformation:controller]; | 208 startupInformation:controller]; |
208 | 209 |
209 EXPECT_EQ(GURL("http://www.google.com/"), [bvc_mock tabURL]); | 210 EXPECT_EQ(GURL("http://www.google.com/"), [bvc_mock tabURL]); |
210 EXPECT_EQ(NSNotFound, static_cast<NSInteger>([bvc_mock position])); | 211 EXPECT_EQ(NSNotFound, static_cast<NSInteger>([bvc_mock position])); |
211 EXPECT_TRUE(PageTransitionCoreTypeIs([bvc_mock transition], | 212 EXPECT_TRUE(PageTransitionCoreTypeIs([bvc_mock transition], |
212 ui::PAGE_TRANSITION_LINK)); | 213 ui::PAGE_TRANSITION_LINK)); |
213 | 214 |
214 EXPECT_FALSE([otr_bvc_mock tabURL].is_valid()); | 215 EXPECT_FALSE([otr_bvc_mock tabURL].is_valid()); |
215 } | 216 } |
216 | 217 |
217 TEST_F(URLOpenerTest, HandleOpenURL) { | 218 TEST_F(URLOpenerTest, HandleOpenURL) { |
218 // A set of tests for robustness of | 219 // A set of tests for robustness of |
219 // application:openURL:options:tabOpener:startupInformation: | 220 // application:openURL:options:tabOpener:startupInformation: |
220 // It verifies that the function handles correctly differents URL parsed by | 221 // It verifies that the function handles correctly differents URL parsed by |
221 // ChromeAppStartupParameters. | 222 // ChromeAppStartupParameters. |
222 MainController* controller = GetMainController(); | 223 MainController* controller = GetMainController(); |
223 | 224 |
224 // The array with the different states to tests (active, not active). | 225 // The array with the different states to tests (active, not active). |
225 NSArray* applicationStatesToTest = @[ @YES, @NO ]; | 226 NSArray* applicationStatesToTest = @[ @YES, @NO ]; |
226 | 227 |
227 // Mock of TabOpening, preventing the creation of a new tab. | 228 // Mock of TabOpening, preventing the creation of a new tab. |
228 base::scoped_nsobject<MockTabOpener> tabOpener([[MockTabOpener alloc] init]); | 229 MockTabOpener* tabOpener = [[MockTabOpener alloc] init]; |
229 | 230 |
230 // The keys for this dictionary is the URL to call openURL:. The value | 231 // The keys for this dictionary is the URL to call openURL:. The value |
231 // from the key is either YES or NO to indicate if this is a valid URL | 232 // from the key is either YES or NO to indicate if this is a valid URL |
232 // or not. | 233 // or not. |
233 NSNumber* kIsValid = [NSNumber numberWithBool:YES]; | 234 NSNumber* kIsValid = [NSNumber numberWithBool:YES]; |
234 NSNumber* kNotValid = [NSNumber numberWithBool:NO]; | 235 NSNumber* kNotValid = [NSNumber numberWithBool:NO]; |
235 NSDictionary* urlsToTest = [NSDictionary | 236 NSDictionary* urlsToTest = [NSDictionary |
236 dictionaryWithObjectsAndKeys: | 237 dictionaryWithObjectsAndKeys: |
237 kNotValid, [NSNull null], | 238 kNotValid, [NSNull null], |
238 // Tests for http, googlechrome, and chromium scheme URLs. | 239 // Tests for http, googlechrome, and chromium scheme URLs. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 for (id annotation in annotationsToTest) { | 275 for (id annotation in annotationsToTest) { |
275 for (NSNumber* applicationActive in applicationStatesToTest) { | 276 for (NSNumber* applicationActive in applicationStatesToTest) { |
276 BOOL applicationIsActive = [applicationActive boolValue]; | 277 BOOL applicationIsActive = [applicationActive boolValue]; |
277 | 278 |
278 controller.startupParameters = nil; | 279 controller.startupParameters = nil; |
279 [tabOpener resetURL]; | 280 [tabOpener resetURL]; |
280 NSURL* testUrl = urlString == [NSNull null] | 281 NSURL* testUrl = urlString == [NSNull null] |
281 ? nil | 282 ? nil |
282 : [NSURL URLWithString:urlString]; | 283 : [NSURL URLWithString:urlString]; |
283 BOOL isValid = [[urlsToTest objectForKey:urlString] boolValue]; | 284 BOOL isValid = [[urlsToTest objectForKey:urlString] boolValue]; |
284 base::scoped_nsobject<NSMutableDictionary> options( | 285 NSMutableDictionary* options = [[NSMutableDictionary alloc] init]; |
285 [[NSMutableDictionary alloc] init]); | |
286 if (source != [NSNull null]) { | 286 if (source != [NSNull null]) { |
287 [options setObject:source | 287 [options setObject:source |
288 forKey:UIApplicationOpenURLOptionsSourceApplicationKey]; | 288 forKey:UIApplicationOpenURLOptionsSourceApplicationKey]; |
289 } | 289 } |
290 if (annotation != [NSNull null]) { | 290 if (annotation != [NSNull null]) { |
291 [options setObject:annotation | 291 [options setObject:annotation |
292 forKey:UIApplicationOpenURLOptionsAnnotationKey]; | 292 forKey:UIApplicationOpenURLOptionsAnnotationKey]; |
293 } | 293 } |
294 base::scoped_nsobject<ChromeAppStartupParameters> params( | 294 ChromeAppStartupParameters* params = [ChromeAppStartupParameters |
295 [ChromeAppStartupParameters | 295 newChromeAppStartupParametersWithURL:testUrl |
296 newChromeAppStartupParametersWithURL:testUrl | 296 fromSourceApplication:nil]; |
297 fromSourceApplication:nil]); | |
298 | 297 |
299 // Action. | 298 // Action. |
300 BOOL result = [URLOpener openURL:testUrl | 299 BOOL result = [URLOpener openURL:testUrl |
301 applicationActive:applicationIsActive | 300 applicationActive:applicationIsActive |
302 options:options | 301 options:options |
303 tabOpener:tabOpener | 302 tabOpener:tabOpener |
304 startupInformation:controller]; | 303 startupInformation:controller]; |
305 | 304 |
306 // Tests. | 305 // Tests. |
307 EXPECT_EQ(isValid, result); | 306 EXPECT_EQ(isValid, result); |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 [URLOpener handleLaunchOptions:launchOptions | 474 [URLOpener handleLaunchOptions:launchOptions |
476 applicationActive:NO | 475 applicationActive:NO |
477 tabOpener:tabOpenerMock | 476 tabOpener:tabOpenerMock |
478 startupInformation:startupInformationMock | 477 startupInformation:startupInformationMock |
479 appState:appStateMock]; | 478 appState:appStateMock]; |
480 | 479 |
481 // Test. | 480 // Test. |
482 EXPECT_TRUE(hasBeenCalled); | 481 EXPECT_TRUE(hasBeenCalled); |
483 EXPECT_OCMOCK_VERIFY(startupInformationMock); | 482 EXPECT_OCMOCK_VERIFY(startupInformationMock); |
484 } | 483 } |
OLD | NEW |