Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: ios/chrome/browser/tabs/tab_unittest.mm

Issue 2775623002: [ios] WebStateList owns all WebState it manages. (Closed)
Patch Set: Fix ios_chrome_unittests. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 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 #import <UIKit/UIKit.h> 5 #import <UIKit/UIKit.h>
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/ios/block_types.h" 11 #include "base/ios/block_types.h"
12 #import "base/ios/weak_nsobject.h"
12 #include "base/mac/scoped_nsobject.h" 13 #include "base/mac/scoped_nsobject.h"
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
15 #include "base/run_loop.h" 16 #include "base/run_loop.h"
16 #include "base/strings/stringprintf.h" 17 #include "base/strings/stringprintf.h"
17 #include "base/strings/sys_string_conversions.h" 18 #include "base/strings/sys_string_conversions.h"
18 #include "components/bookmarks/test/bookmark_test_helpers.h" 19 #include "components/bookmarks/test/bookmark_test_helpers.h"
19 #include "components/history/core/browser/history_service.h" 20 #include "components/history/core/browser/history_service.h"
20 #include "components/keyed_service/core/service_access_type.h" 21 #include "components/keyed_service/core/service_access_type.h"
21 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" 22 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h"
22 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h" 23 #include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
23 #include "ios/chrome/browser/browser_state/test_chrome_browser_state_manager.h" 24 #include "ios/chrome/browser/browser_state/test_chrome_browser_state_manager.h"
24 #import "ios/chrome/browser/chrome_url_util.h" 25 #import "ios/chrome/browser/chrome_url_util.h"
25 #include "ios/chrome/browser/history/history_service_factory.h" 26 #include "ios/chrome/browser/history/history_service_factory.h"
27 #import "ios/chrome/browser/tabs/legacy_tab_helper.h"
26 #import "ios/chrome/browser/tabs/tab.h" 28 #import "ios/chrome/browser/tabs/tab.h"
29 #import "ios/chrome/browser/tabs/tab_helper_util.h"
27 #import "ios/chrome/browser/tabs/tab_model.h" 30 #import "ios/chrome/browser/tabs/tab_model.h"
28 #import "ios/chrome/browser/tabs/tab_private.h" 31 #import "ios/chrome/browser/tabs/tab_private.h"
29 #import "ios/chrome/browser/ui/open_in_controller.h" 32 #import "ios/chrome/browser/ui/open_in_controller.h"
30 #import "ios/chrome/browser/ui/open_in_controller_testing.h" 33 #import "ios/chrome/browser/ui/open_in_controller_testing.h"
31 #import "ios/chrome/browser/web/external_app_launcher.h" 34 #import "ios/chrome/browser/web/external_app_launcher.h"
32 #include "ios/chrome/test/block_cleanup_test.h" 35 #include "ios/chrome/test/block_cleanup_test.h"
33 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_provider.h" 36 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_provider.h"
34 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_state_manager .h" 37 #include "ios/chrome/test/ios_chrome_scoped_testing_chrome_browser_state_manager .h"
35 #include "ios/chrome/test/ios_chrome_scoped_testing_local_state.h" 38 #include "ios/chrome/test/ios_chrome_scoped_testing_local_state.h"
36 #import "ios/public/provider/chrome/browser/native_app_launcher/fake_native_app_ metadata.h" 39 #import "ios/public/provider/chrome/browser/native_app_launcher/fake_native_app_ metadata.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 return [tabsForTesting_ indexOfObject:tab]; 91 return [tabsForTesting_ indexOfObject:tab];
89 } 92 }
90 93
91 - (NSUInteger)count { 94 - (NSUInteger)count {
92 return [tabsForTesting_ count]; 95 return [tabsForTesting_ count];
93 } 96 }
94 97
95 - (void)closeTabAtIndex:(NSUInteger)index { 98 - (void)closeTabAtIndex:(NSUInteger)index {
96 [tabsForTesting_ removeObjectAtIndex:index]; 99 [tabsForTesting_ removeObjectAtIndex:index];
97 } 100 }
98
99 - (void)didCloseTab:(Tab*)closedTab {
100 }
101 @end 101 @end
102 102
103 @interface ExternalAppLauncherMock : OCMockComplexTypeHelper 103 @interface ExternalAppLauncherMock : OCMockComplexTypeHelper
104 @end 104 @end
105 105
106 @implementation ExternalAppLauncherMock 106 @implementation ExternalAppLauncherMock
107 typedef BOOL (^openURLBlockType)(const GURL&, BOOL); 107 typedef BOOL (^openURLBlockType)(const GURL&, BOOL);
108 108
109 - (BOOL)openURL:(const GURL&)url linkClicked:(BOOL)linkClicked { 109 - (BOOL)openURL:(const GURL&)url linkClicked:(BOOL)linkClicked {
110 return static_cast<openURLBlockType>([self blockForSelector:_cmd])( 110 return static_cast<openURLBlockType>([self blockForSelector:_cmd])(
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 history_service_ = ios::HistoryServiceFactory::GetForBrowserState( 187 history_service_ = ios::HistoryServiceFactory::GetForBrowserState(
188 chrome_browser_state_.get(), ServiceAccessType::EXPLICIT_ACCESS); 188 chrome_browser_state_.get(), ServiceAccessType::EXPLICIT_ACCESS);
189 189
190 ios::ChromeBrowserState* browser_state = chrome_browser_state_.get(); 190 ios::ChromeBrowserState* browser_state = chrome_browser_state_.get();
191 if (UseOffTheRecordBrowserState()) 191 if (UseOffTheRecordBrowserState())
192 browser_state = browser_state->GetOffTheRecordChromeBrowserState(); 192 browser_state = browser_state->GetOffTheRecordChromeBrowserState();
193 193
194 mock_web_controller_ = 194 mock_web_controller_ =
195 [OCMockObject niceMockForClass:[CRWWebController class]]; 195 [OCMockObject niceMockForClass:[CRWWebController class]];
196 web::WebState::CreateParams create_params(browser_state); 196 web::WebState::CreateParams create_params(browser_state);
197 auto web_state_impl = base::MakeUnique<WebStateImpl>(create_params); 197 web_state_impl_ = base::MakeUnique<WebStateImpl>(create_params);
198 web_state_impl->SetWebController(mock_web_controller_); 198 web_state_impl_->SetWebController(mock_web_controller_);
199 web_state_impl->GetNavigationManagerImpl().InitializeSession(); 199 web_state_impl_->GetNavigationManagerImpl().InitializeSession();
200 web_state_impl_ = web_state_impl.get(); 200 web::WebStateImpl* web_state_impl = web_state_impl_.get();
201 [[[static_cast<OCMockObject*>(mock_web_controller_) stub] 201 [[[static_cast<OCMockObject*>(mock_web_controller_) stub]
202 andReturnValue:OCMOCK_VALUE(web_state_impl_)] webStateImpl]; 202 andReturnValue:OCMOCK_VALUE(web_state_impl)] webStateImpl];
203 web_controller_view_.reset([[UIView alloc] init]); 203 web_controller_view_.reset([[UIView alloc] init]);
204 [[[static_cast<OCMockObject*>(mock_web_controller_) stub] 204 [[[static_cast<OCMockObject*>(mock_web_controller_) stub]
205 andReturn:web_controller_view_.get()] view]; 205 andReturn:web_controller_view_.get()] view];
206 tab_.reset([[Tab alloc] initWithWebState:std::move(web_state_impl) 206 LegacyTabHelper::CreateForWebState(web_state_impl_.get());
207 model:nil 207 tab_.reset(LegacyTabHelper::GetTabForWebState(web_state_impl_.get()));
208 attachTabHelpers:NO]);
209 web::NavigationManager::WebLoadParams load_params( 208 web::NavigationManager::WebLoadParams load_params(
210 GURL("chrome://version/")); 209 GURL("chrome://version/"));
211 [[tab_ webController] loadWithParams:load_params]; 210 [[tab_ webController] loadWithParams:load_params];
212 211
213 // There should be no entries in the history at this point. 212 // There should be no entries in the history at this point.
214 history::QueryResults results; 213 history::QueryResults results;
215 QueryAllHistory(&results); 214 QueryAllHistory(&results);
216 EXPECT_EQ(0UL, results.size()); 215 EXPECT_EQ(0UL, results.size());
217 mock_external_app_launcher_.reset([[ExternalAppLauncherMock alloc] 216 mock_external_app_launcher_.reset([[ExternalAppLauncherMock alloc]
218 initWithRepresentedObject: 217 initWithRepresentedObject:
219 [OCMockObject mockForClass:[ExternalAppLauncher class]]]); 218 [OCMockObject mockForClass:[ExternalAppLauncher class]]]);
220 [tab_ replaceExternalAppLauncher:mock_external_app_launcher_]; 219 [tab_ replaceExternalAppLauncher:mock_external_app_launcher_];
221 } 220 }
222 221
223 void TearDown() override { 222 void TearDown() override {
224 [tab_ close]; 223 // Ensure that the Tab is destroyed before the autorelease pool is cleared.
225 224 web_state_impl_.reset();
226 BlockCleanupTest::TearDown(); 225 BlockCleanupTest::TearDown();
227 } 226 }
228 227
229 void BrowseTo(const GURL& userUrl, const GURL& redirectUrl, NSString* title) { 228 void BrowseTo(const GURL& userUrl, const GURL& redirectUrl, NSString* title) {
230 DCHECK_EQ(tab_.get().webState, web_state_impl_); 229 DCHECK_EQ(tab_.get().webState, web_state_impl_.get());
231 230
232 [tab_ webWillAddPendingURL:userUrl transition:ui::PAGE_TRANSITION_TYPED]; 231 [tab_ webWillAddPendingURL:userUrl transition:ui::PAGE_TRANSITION_TYPED];
233 web_state_impl_->OnProvisionalNavigationStarted(userUrl); 232 web_state_impl_->OnProvisionalNavigationStarted(userUrl);
234 [tab_ webWillAddPendingURL:redirectUrl 233 [tab_ webWillAddPendingURL:redirectUrl
235 transition:ui::PAGE_TRANSITION_CLIENT_REDIRECT]; 234 transition:ui::PAGE_TRANSITION_CLIENT_REDIRECT];
236 235
237 web::Referrer empty_referrer; 236 web::Referrer empty_referrer;
238 [tab_ navigationManagerImpl]->AddPendingItem( 237 [tab_ navigationManagerImpl]->AddPendingItem(
239 redirectUrl, empty_referrer, ui::PAGE_TRANSITION_CLIENT_REDIRECT, 238 redirectUrl, empty_referrer, ui::PAGE_TRANSITION_CLIENT_REDIRECT,
240 web::NavigationInitiationType::RENDERER_INITIATED); 239 web::NavigationInitiationType::RENDERER_INITIATED);
241 240
242 web_state_impl_->OnProvisionalNavigationStarted(redirectUrl); 241 web_state_impl_->OnProvisionalNavigationStarted(redirectUrl);
243 [[tab_ navigationManagerImpl]->GetSessionController() commitPendingItem]; 242 [[tab_ navigationManagerImpl]->GetSessionController() commitPendingItem];
244 [[tab_ webController] webStateImpl]->UpdateHttpResponseHeaders(redirectUrl); 243 [[tab_ webController] webStateImpl]->UpdateHttpResponseHeaders(redirectUrl);
245 [[tab_ webController] webStateImpl]->OnNavigationCommitted(redirectUrl); 244 [[tab_ webController] webStateImpl]->OnNavigationCommitted(redirectUrl);
246 245
247 base::string16 new_title = base::SysNSStringToUTF16(title); 246 base::string16 new_title = base::SysNSStringToUTF16(title);
248 [tab_ navigationManager]->GetLastCommittedItem()->SetTitle(new_title); 247 [tab_ navigationManager]->GetLastCommittedItem()->SetTitle(new_title);
249 248
250 web_state_impl_->OnTitleChanged(); 249 web_state_impl_->OnTitleChanged();
251 [[[(id)mock_web_controller_ expect] 250 [[[(id)mock_web_controller_ expect]
252 andReturnValue:OCMOCK_VALUE(kPageLoaded)] loadPhase]; 251 andReturnValue:OCMOCK_VALUE(kPageLoaded)] loadPhase];
253 web_state_impl_->OnPageLoaded(redirectUrl, true); 252 web_state_impl_->OnPageLoaded(redirectUrl, true);
254 } 253 }
255 254
256 void BrowseToNewTab() { 255 void BrowseToNewTab() {
257 DCHECK_EQ(tab_.get().webState, web_state_impl_); 256 DCHECK_EQ(tab_.get().webState, web_state_impl_.get());
258 const GURL url(kNewTabUrl); 257 const GURL url(kNewTabUrl);
259 // TODO(crbug.com/661992): This will not work with a mock CRWWebController. 258 // TODO(crbug.com/661992): This will not work with a mock CRWWebController.
260 // The only test that uses it is currently disabled. 259 // The only test that uses it is currently disabled.
261 web::NavigationManager::WebLoadParams params(url); 260 web::NavigationManager::WebLoadParams params(url);
262 params.transition_type = ui::PAGE_TRANSITION_TYPED; 261 params.transition_type = ui::PAGE_TRANSITION_TYPED;
263 [[tab_ webController] loadWithParams:params]; 262 [[tab_ webController] loadWithParams:params];
264 [[[(id)mock_web_controller_ expect] 263 [[[(id)mock_web_controller_ expect]
265 andReturnValue:OCMOCK_VALUE(kPageLoading)] loadPhase]; 264 andReturnValue:OCMOCK_VALUE(kPageLoading)] loadPhase];
266 [[[(id)mock_web_controller_ expect] 265 [[[(id)mock_web_controller_ expect]
267 andReturnValue:OCMOCK_VALUE(kPageLoaded)] loadPhase]; 266 andReturnValue:OCMOCK_VALUE(kPageLoaded)] loadPhase];
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 } 311 }
313 } 312 }
314 #endif 313 #endif
315 314
316 virtual bool UseOffTheRecordBrowserState() const { return false; } 315 virtual bool UseOffTheRecordBrowserState() const { return false; }
317 316
318 protected: 317 protected:
319 web::TestWebThreadBundle thread_bundle_; 318 web::TestWebThreadBundle thread_bundle_;
320 IOSChromeScopedTestingChromeBrowserStateManager scoped_browser_state_manager_; 319 IOSChromeScopedTestingChromeBrowserStateManager scoped_browser_state_manager_;
321 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; 320 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
322 base::scoped_nsobject<Tab> tab_; 321 std::unique_ptr<web::WebStateImpl> web_state_impl_;
323 web::WebStateImpl* web_state_impl_;
324 history::HistoryService* history_service_; // weak 322 history::HistoryService* history_service_; // weak
325 CRWWebController* mock_web_controller_; // weak 323 CRWWebController* mock_web_controller_; // weak
326 base::scoped_nsobject<UIView> web_controller_view_; 324 base::scoped_nsobject<UIView> web_controller_view_;
327 base::scoped_nsobject<ArrayTabModel> tabModel_; 325 base::scoped_nsobject<ArrayTabModel> tabModel_;
328 base::scoped_nsobject<id> mock_external_app_launcher_; 326 base::scoped_nsobject<id> mock_external_app_launcher_;
327 base::WeakNSObject<Tab> tab_;
329 }; 328 };
330 329
331 TEST_F(TabTest, AddToHistoryWithRedirect) { 330 TEST_F(TabTest, AddToHistoryWithRedirect) {
332 BrowseTo(GURL(kGoogleUserUrl), GURL(kGoogleRedirectUrl), kGoogleTitle); 331 BrowseTo(GURL(kGoogleUserUrl), GURL(kGoogleRedirectUrl), kGoogleTitle);
333 history::QueryResults results; 332 history::QueryResults results;
334 QueryAllHistory(&results); 333 QueryAllHistory(&results);
335 EXPECT_EQ(1U, results.size()); 334 EXPECT_EQ(1U, results.size());
336 CheckHistoryResult(results[0], GURL(kGoogleRedirectUrl), kGoogleTitle); 335 CheckHistoryResult(results[0], GURL(kGoogleRedirectUrl), kGoogleTitle);
337 CheckCurrentItem(results[0]); 336 CheckCurrentItem(results[0]);
338 } 337 }
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 [[FakeNativeAppMetadata alloc] init]); 550 [[FakeNativeAppMetadata alloc] init]);
552 551
553 // Turn auto open on. 552 // Turn auto open on.
554 [metadata setShouldAutoOpenLinks:YES]; 553 [metadata setShouldAutoOpenLinks:YES];
555 int expectedCallCount = 2; 554 int expectedCallCount = 2;
556 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); 555 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO);
557 EXPECT_FALSE([metadata shouldAutoOpenLinks]); 556 EXPECT_FALSE([metadata shouldAutoOpenLinks]);
558 } 557 }
559 558
560 } // namespace 559 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698