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

Side by Side Diff: chrome/browser/sessions/session_restore_browsertest.cc

Issue 1131373003: [Session restore] Add MRU logic to loading of background pages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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 #include <algorithm>
5 #include <vector> 6 #include <vector>
6 7
7 #include "base/command_line.h" 8 #include "base/command_line.h"
8 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
9 #include "base/memory/memory_pressure_listener.h" 10 #include "base/memory/memory_pressure_listener.h"
10 #include "base/process/launch.h" 11 #include "base/process/launch.h"
11 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h" 13 #include "base/time/time.h"
13 #include "chrome/browser/browser_process.h" 14 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chrome_notification_types.h" 15 #include "chrome/browser/chrome_notification_types.h"
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 } 206 }
206 } 207 }
207 208
208 GURL url1_; 209 GURL url1_;
209 GURL url2_; 210 GURL url2_;
210 GURL url3_; 211 GURL url3_;
211 212
212 const BrowserList* active_browser_list_; 213 const BrowserList* active_browser_list_;
213 }; 214 };
214 215
215 // Activates the smart restore behaviour and can track the loading of tabs. 216 // Activates the smart restore behaviour in "simple" mode and tracks the loading
216 class SmartSessionRestoreTest : public SessionRestoreTest, 217 // of tabs.
217 public content::NotificationObserver { 218 class SmartSessionRestoreSimpleTest : public SessionRestoreTest,
219 public content::NotificationObserver {
218 public: 220 public:
219 SmartSessionRestoreTest() {} 221 SmartSessionRestoreSimpleTest() {}
220 void StartObserving(int num_tabs) { 222 void StartObserving(int num_tabs) {
223 // Start by clearing everything so it can be reused in the same test.
224 web_contents_.clear();
225 registrar_.RemoveAll();
221 num_tabs_ = num_tabs; 226 num_tabs_ = num_tabs;
222 registrar_.Add(this, content::NOTIFICATION_LOAD_START, 227 registrar_.Add(this, content::NOTIFICATION_LOAD_START,
223 content::NotificationService::AllSources()); 228 content::NotificationService::AllSources());
224 } 229 }
225 void Observe(int type, 230 void Observe(int type,
226 const content::NotificationSource& source, 231 const content::NotificationSource& source,
227 const content::NotificationDetails& details) override { 232 const content::NotificationDetails& details) override {
228 switch (type) { 233 switch (type) {
229 case content::NOTIFICATION_LOAD_START: { 234 case content::NOTIFICATION_LOAD_START: {
230 content::NavigationController* controller = 235 content::NavigationController* controller =
(...skipping 23 matching lines...) Expand all
254 "IntelligentSessionRestore.TestGroup:PrioritizeTabs/simple"); 259 "IntelligentSessionRestore.TestGroup:PrioritizeTabs/simple");
255 } 260 }
256 261
257 private: 262 private:
258 content::NotificationRegistrar registrar_; 263 content::NotificationRegistrar registrar_;
259 // Ordered by load start order. 264 // Ordered by load start order.
260 std::vector<content::WebContents*> web_contents_; 265 std::vector<content::WebContents*> web_contents_;
261 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; 266 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
262 int num_tabs_; 267 int num_tabs_;
263 268
264 DISALLOW_COPY_AND_ASSIGN(SmartSessionRestoreTest); 269 DISALLOW_COPY_AND_ASSIGN(SmartSessionRestoreSimpleTest);
270 };
271
272 class SmartSessionRestoreMRUTest : public SmartSessionRestoreSimpleTest {
273 public:
274 SmartSessionRestoreMRUTest() {}
275
276 protected:
277 void SetUpCommandLine(base::CommandLine* command_line) override {
278 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
279 switches::kForceFieldTrials, "IntelligentSessionRestore/TestGroup/");
280 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII(
281 switches::kForceFieldTrialParams,
282 "IntelligentSessionRestore.TestGroup:PrioritizeTabs/mru");
sky 2015/05/12 21:25:30 Ouch. Is the old one dumb?
Georges Khalil 2015/05/15 16:55:30 No comments :)
283 }
284
285 private:
286 DISALLOW_COPY_AND_ASSIGN(SmartSessionRestoreMRUTest);
265 }; 287 };
266 288
267 // Verifies that restored tabs have a root window. This is important 289 // Verifies that restored tabs have a root window. This is important
268 // otherwise the wrong information is communicated to the renderer. 290 // otherwise the wrong information is communicated to the renderer.
269 // (http://crbug.com/342672). 291 // (http://crbug.com/342672).
270 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoredTabsShouldHaveWindow) { 292 IN_PROC_BROWSER_TEST_F(SessionRestoreTest, RestoredTabsShouldHaveWindow) {
271 // Create tabs. 293 // Create tabs.
272 ui_test_utils::NavigateToURLWithDisposition( 294 ui_test_utils::NavigateToURLWithDisposition(
273 browser(), 295 browser(),
274 GURL(url::kAboutBlankURL), 296 GURL(url::kAboutBlankURL),
(...skipping 1051 matching lines...) Expand 10 before | Expand all | Expand 10 after
1326 &browser()->tab_strip_model()->GetActiveWebContents()->GetController(); 1348 &browser()->tab_strip_model()->GetActiveWebContents()->GetController();
1327 EXPECT_TRUE( 1349 EXPECT_TRUE(
1328 controller->GetDefaultSessionStorageNamespace()->should_persist()); 1350 controller->GetDefaultSessionStorageNamespace()->should_persist());
1329 1351
1330 // Quit and restore. Check that no extra tabs were created. 1352 // Quit and restore. Check that no extra tabs were created.
1331 Browser* new_browser = QuitBrowserAndRestore(browser(), 1); 1353 Browser* new_browser = QuitBrowserAndRestore(browser(), 1);
1332 ASSERT_EQ(1u, active_browser_list_->size()); 1354 ASSERT_EQ(1u, active_browser_list_->size());
1333 EXPECT_EQ(1, new_browser->tab_strip_model()->count()); 1355 EXPECT_EQ(1, new_browser->tab_strip_model()->count());
1334 } 1356 }
1335 1357
1336 IN_PROC_BROWSER_TEST_F(SmartSessionRestoreTest, CorrectLoadingOrder) { 1358 IN_PROC_BROWSER_TEST_F(SmartSessionRestoreSimpleTest, CorrectLoadingOrder) {
1337 ASSERT_EQ(SessionRestore::SMART_RESTORE_MODE_SIMPLE, 1359 ASSERT_EQ(SessionRestore::SMART_RESTORE_MODE_SIMPLE,
1338 SessionRestore::GetSmartRestoreMode()); 1360 SessionRestore::GetSmartRestoreMode());
1339 1361
1340 const int NumTabs = 6; 1362 const int NumTabs = 6;
1341 1363
1342 // Start observing the loading of tabs, to make sure the order is correct. 1364 // Start observing the loading of tabs, to make sure the order is correct.
1343 StartObserving(NumTabs); 1365 StartObserving(NumTabs);
1344 1366
1345 struct TabInfo { 1367 struct TabInfo {
1346 GURL url; 1368 GURL url;
(...skipping 25 matching lines...) Expand all
1372 sync_data.set_tab_visual_index(0); 1394 sync_data.set_tab_visual_index(0);
1373 sync_data.set_current_navigation_index(0); 1395 sync_data.set_current_navigation_index(0);
1374 sync_data.add_navigation()->CopyFrom(nav.ToSyncData()); 1396 sync_data.add_navigation()->CopyFrom(nav.ToSyncData());
1375 sync_data.set_pinned(tab_info[i].pinned); 1397 sync_data.set_pinned(tab_info[i].pinned);
1376 tab[i].SetFromSyncData(sync_data, base::Time::Now()); 1398 tab[i].SetFromSyncData(sync_data, base::Time::Now());
1377 window.tabs.push_back(tab + i); 1399 window.tabs.push_back(tab + i);
1378 } 1400 }
1379 1401
1380 session.push_back(&window); 1402 session.push_back(&window);
1381 Profile* profile = browser()->profile(); 1403 Profile* profile = browser()->profile();
1382 ui_test_utils::BrowserAddedObserver window_observer;
1383 std::vector<Browser*> browsers = SessionRestore::RestoreForeignSessionWindows( 1404 std::vector<Browser*> browsers = SessionRestore::RestoreForeignSessionWindows(
1384 profile, browser()->host_desktop_type(), session.begin(), session.end()); 1405 profile, browser()->host_desktop_type(), session.begin(), session.end());
1385 1406
1386 ASSERT_EQ(1u, browsers.size()); 1407 ASSERT_EQ(1u, browsers.size());
1387 ASSERT_TRUE(browsers[0]); 1408 ASSERT_TRUE(browsers[0]);
1388 ASSERT_EQ(NumTabs, browsers[0]->tab_strip_model()->count()); 1409 ASSERT_EQ(NumTabs, browsers[0]->tab_strip_model()->count());
1389 1410
1390 WaitForAllTabsToStartLoading(); 1411 WaitForAllTabsToStartLoading();
1391 1412
1392 ASSERT_EQ(static_cast<size_t>(NumTabs), web_contents().size()); 1413 ASSERT_EQ(static_cast<size_t>(NumTabs), web_contents().size());
1393 1414
1394 // Make sure that contents are loaded in the correct order, ie. each tab rank 1415 // Make sure that contents are loaded in the correct order, ie. each tab rank
1395 // is higher that its preceding one. 1416 // is higher that its preceding one.
1396 std::map<GURL, int> ranks; 1417 std::map<GURL, int> ranks;
1397 for (auto t : tab_info) 1418 for (auto t : tab_info)
1398 ranks[t.url] = t.expected_load_order; 1419 ranks[t.url] = t.expected_load_order;
1399 for (size_t i = 1; i < web_contents().size(); i++) { 1420 for (size_t i = 1; i < web_contents().size(); i++) {
1400 int current_rank = ranks[web_contents()[i]->GetLastCommittedURL()]; 1421 int current_rank = ranks[web_contents()[i]->GetLastCommittedURL()];
1401 int previous_rank = ranks[web_contents()[i - 1]->GetLastCommittedURL()]; 1422 int previous_rank = ranks[web_contents()[i - 1]->GetLastCommittedURL()];
1402 ASSERT_LT(previous_rank, current_rank); 1423 ASSERT_LT(previous_rank, current_rank);
1403 } 1424 }
1404 1425
1405 // The SessionWindow destructor deletes the tabs, so we have to clear them 1426 // The SessionWindow destructor deletes the tabs, so we have to clear them
1406 // here to avoid a crash. 1427 // here to avoid a crash.
1407 window.tabs.clear(); 1428 window.tabs.clear();
1408 } 1429 }
1430
1431 IN_PROC_BROWSER_TEST_F(SmartSessionRestoreMRUTest, CorrectLoadingOrder) {
1432 const int NumTabs = 6;
sky 2015/05/12 21:25:30 num_tabs
Georges Khalil 2015/05/15 16:55:30 Oops, done. Fixed the other test too.
1433
1434 GURL urls[] = {GURL("http://google.com/1"),
1435 GURL("http://google.com/2"),
1436 GURL("http://google.com/3"),
1437 GURL("http://google.com/4"),
1438 GURL("http://google.com/5"),
1439 GURL("http://google.com/6")};
1440
1441 // Create a random activation order so that running the test enough times
1442 // gives us complete coverage.
1443 std::vector<int> activation_order;
1444 for (int i = 0; i < NumTabs; i++)
1445 activation_order.push_back(i);
1446 std::random_shuffle(activation_order.begin(), activation_order.end());
sky 2015/05/12 21:25:30 It's not a good idea to randomize test data. If th
Georges Khalil 2015/05/15 16:55:30 Acknowledged. Changed to a fixed order.
1447
1448 // Replace the first tab and add the other tabs.
1449 ui_test_utils::NavigateToURL(browser(), urls[0]);
1450 for (int i = 1; i < NumTabs; i++) {
1451 ui_test_utils::NavigateToURLWithDisposition(
1452 browser(), urls[i], NEW_FOREGROUND_TAB,
1453 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
1454 }
1455
1456 ASSERT_EQ(NumTabs, browser()->tab_strip_model()->count());
1457
1458 // Activate the tabs one by one following the random activation order.
1459 for (auto i : activation_order) {
sky 2015/05/12 21:25:30 nit: no {}
Georges Khalil 2015/05/15 16:55:30 Done.
1460 browser()->tab_strip_model()->ActivateTabAt(i, true);
1461 }
1462
1463 // Close the browser.
1464 g_browser_process->AddRefModule();
1465 CloseBrowserSynchronously(browser());
1466
1467 StartObserving(NumTabs);
1468
1469 // Create a new window, which should trigger session restore.
1470 ui_test_utils::BrowserAddedObserver window_observer;
1471 chrome::NewEmptyWindow(browser()->profile(),
1472 chrome::HOST_DESKTOP_TYPE_NATIVE);
1473 Browser* new_browser = window_observer.WaitForSingleNewBrowser();
1474 WaitForAllTabsToStartLoading();
1475 g_browser_process->ReleaseModule();
1476
1477 ASSERT_EQ(static_cast<size_t>(NumTabs), web_contents().size());
1478 // Test that we have observed the tabs being loaded in the inverse order of
1479 // their activation (MRU).
1480 for (size_t i = 0; i < web_contents().size(); i++) {
1481 GURL expected_url = urls[activation_order[NumTabs - i - 1]];
1482 ASSERT_EQ(expected_url, web_contents()[i]->GetLastCommittedURL());
1483 }
1484
1485 // Close the browser and open it again, to trigger another session restore.
1486 // The goal is to ensure that activation time is persisted between session
1487 // restores.
1488
1489 // Close the browser.
1490 g_browser_process->AddRefModule();
1491 CloseBrowserSynchronously(new_browser);
1492
1493 StartObserving(NumTabs);
1494
1495 // Create a new window, which should trigger session restore.
1496 ui_test_utils::BrowserAddedObserver window_observer2;
1497 chrome::NewEmptyWindow(browser()->profile(),
1498 chrome::HOST_DESKTOP_TYPE_NATIVE);
1499 window_observer2.WaitForSingleNewBrowser();
1500 WaitForAllTabsToStartLoading();
1501 g_browser_process->ReleaseModule();
1502
1503 ASSERT_EQ(static_cast<size_t>(NumTabs), web_contents().size());
1504 // Test that we have observed the tabs being loaded in the inverse order of
1505 // their activation (MRU).
1506 for (size_t i = 0; i < web_contents().size(); i++) {
1507 GURL expected_url = urls[activation_order[NumTabs - i - 1]];
1508 ASSERT_EQ(expected_url, web_contents()[i]->GetLastCommittedURL());
1509 }
1510 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698