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

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

Issue 2775623002: [ios] WebStateList owns all WebState it manages. (Closed)
Patch Set: Fix gn check Created 3 years, 8 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
« no previous file with comments | « ios/chrome/browser/tabs/tab_private.h ('k') | ios/chrome/browser/ui/BUILD.gn » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 web::NavigationManager::UserAgentOverrideOption::INHERIT); 240 web::NavigationManager::UserAgentOverrideOption::INHERIT);
242 241
243 web_state_impl_->OnProvisionalNavigationStarted(redirectUrl); 242 web_state_impl_->OnProvisionalNavigationStarted(redirectUrl);
244 [[tab_ navigationManagerImpl]->GetSessionController() commitPendingItem]; 243 [[tab_ navigationManagerImpl]->GetSessionController() commitPendingItem];
245 [[tab_ webController] webStateImpl]->UpdateHttpResponseHeaders(redirectUrl); 244 [[tab_ webController] webStateImpl]->UpdateHttpResponseHeaders(redirectUrl);
246 [[tab_ webController] webStateImpl]->OnNavigationCommitted(redirectUrl); 245 [[tab_ webController] webStateImpl]->OnNavigationCommitted(redirectUrl);
247 246
248 base::string16 new_title = base::SysNSStringToUTF16(title); 247 base::string16 new_title = base::SysNSStringToUTF16(title);
249 [tab_ navigationManager]->GetLastCommittedItem()->SetTitle(new_title); 248 [tab_ navigationManager]->GetLastCommittedItem()->SetTitle(new_title);
250 249
251 web_state_impl_->OnTitleChanged(); 250 web_state_impl_->OnTitleChanged();
252 [[[(id)mock_web_controller_ expect] 251 [[[(id)mock_web_controller_ expect]
253 andReturnValue:OCMOCK_VALUE(kPageLoaded)] loadPhase]; 252 andReturnValue:OCMOCK_VALUE(kPageLoaded)] loadPhase];
254 web_state_impl_->OnPageLoaded(redirectUrl, true); 253 web_state_impl_->OnPageLoaded(redirectUrl, true);
255 } 254 }
256 255
257 void BrowseToNewTab() { 256 void BrowseToNewTab() {
258 DCHECK_EQ(tab_.get().webState, web_state_impl_); 257 DCHECK_EQ(tab_.get().webState, web_state_impl_.get());
259 const GURL url(kNewTabUrl); 258 const GURL url(kNewTabUrl);
260 // TODO(crbug.com/661992): This will not work with a mock CRWWebController. 259 // TODO(crbug.com/661992): This will not work with a mock CRWWebController.
261 // The only test that uses it is currently disabled. 260 // The only test that uses it is currently disabled.
262 web::NavigationManager::WebLoadParams params(url); 261 web::NavigationManager::WebLoadParams params(url);
263 params.transition_type = ui::PAGE_TRANSITION_TYPED; 262 params.transition_type = ui::PAGE_TRANSITION_TYPED;
264 [[tab_ webController] loadWithParams:params]; 263 [[tab_ webController] loadWithParams:params];
265 [[[(id)mock_web_controller_ expect] 264 [[[(id)mock_web_controller_ expect]
266 andReturnValue:OCMOCK_VALUE(kPageLoading)] loadPhase]; 265 andReturnValue:OCMOCK_VALUE(kPageLoading)] loadPhase];
267 [[[(id)mock_web_controller_ expect] 266 [[[(id)mock_web_controller_ expect]
268 andReturnValue:OCMOCK_VALUE(kPageLoaded)] loadPhase]; 267 andReturnValue:OCMOCK_VALUE(kPageLoaded)] loadPhase];
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
313 } 312 }
314 } 313 }
315 #endif 314 #endif
316 315
317 virtual bool UseOffTheRecordBrowserState() const { return false; } 316 virtual bool UseOffTheRecordBrowserState() const { return false; }
318 317
319 protected: 318 protected:
320 web::TestWebThreadBundle thread_bundle_; 319 web::TestWebThreadBundle thread_bundle_;
321 IOSChromeScopedTestingChromeBrowserStateManager scoped_browser_state_manager_; 320 IOSChromeScopedTestingChromeBrowserStateManager scoped_browser_state_manager_;
322 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_; 321 std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
323 base::scoped_nsobject<Tab> tab_; 322 std::unique_ptr<web::WebStateImpl> web_state_impl_;
324 web::WebStateImpl* web_state_impl_;
325 history::HistoryService* history_service_; // weak 323 history::HistoryService* history_service_; // weak
326 CRWWebController* mock_web_controller_; // weak 324 CRWWebController* mock_web_controller_; // weak
327 base::scoped_nsobject<UIView> web_controller_view_; 325 base::scoped_nsobject<UIView> web_controller_view_;
328 base::scoped_nsobject<ArrayTabModel> tabModel_; 326 base::scoped_nsobject<ArrayTabModel> tabModel_;
329 base::scoped_nsobject<id> mock_external_app_launcher_; 327 base::scoped_nsobject<id> mock_external_app_launcher_;
328 base::WeakNSObject<Tab> tab_;
330 }; 329 };
331 330
332 TEST_F(TabTest, AddToHistoryWithRedirect) { 331 TEST_F(TabTest, AddToHistoryWithRedirect) {
333 BrowseTo(GURL(kGoogleUserUrl), GURL(kGoogleRedirectUrl), kGoogleTitle); 332 BrowseTo(GURL(kGoogleUserUrl), GURL(kGoogleRedirectUrl), kGoogleTitle);
334 history::QueryResults results; 333 history::QueryResults results;
335 QueryAllHistory(&results); 334 QueryAllHistory(&results);
336 EXPECT_EQ(1U, results.size()); 335 EXPECT_EQ(1U, results.size());
337 CheckHistoryResult(results[0], GURL(kGoogleRedirectUrl), kGoogleTitle); 336 CheckHistoryResult(results[0], GURL(kGoogleRedirectUrl), kGoogleTitle);
338 CheckCurrentItem(results[0]); 337 CheckCurrentItem(results[0]);
339 } 338 }
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 [[FakeNativeAppMetadata alloc] init]); 551 [[FakeNativeAppMetadata alloc] init]);
553 552
554 // Turn auto open on. 553 // Turn auto open on.
555 [metadata setShouldAutoOpenLinks:YES]; 554 [metadata setShouldAutoOpenLinks:YES];
556 int expectedCallCount = 2; 555 int expectedCallCount = 2;
557 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO); 556 TestOpenNativeAppURL(testURL, NO, expectedCallCount, NO);
558 EXPECT_FALSE([metadata shouldAutoOpenLinks]); 557 EXPECT_FALSE([metadata shouldAutoOpenLinks]);
559 } 558 }
560 559
561 } // namespace 560 } // namespace
OLDNEW
« no previous file with comments | « ios/chrome/browser/tabs/tab_private.h ('k') | ios/chrome/browser/ui/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698