Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 "base/base_switches.h" | 5 #include "base/base_switches.h" |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/memory/memory_pressure_listener.h" | 7 #include "base/memory/memory_pressure_listener.h" |
| 8 #include "base/test/simple_test_tick_clock.h" | 8 #include "base/test/simple_test_tick_clock.h" |
| 9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
| 10 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| (...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 474 EXPECT_FALSE(tab_manager->DiscardTabImpl()); | 474 EXPECT_FALSE(tab_manager->DiscardTabImpl()); |
| 475 | 475 |
| 476 // Reset auto-discardable state to true. | 476 // Reset auto-discardable state to true. |
| 477 tab_manager->SetTabAutoDiscardableState(tsm->GetWebContentsAt(0), true); | 477 tab_manager->SetTabAutoDiscardableState(tsm->GetWebContentsAt(0), true); |
| 478 | 478 |
| 479 // Now it should be able to discard the tab. | 479 // Now it should be able to discard the tab. |
| 480 EXPECT_TRUE(tab_manager->DiscardTabImpl()); | 480 EXPECT_TRUE(tab_manager->DiscardTabImpl()); |
| 481 EXPECT_TRUE(tab_manager->IsTabDiscarded(tsm->GetWebContentsAt(0))); | 481 EXPECT_TRUE(tab_manager->IsTabDiscarded(tsm->GetWebContentsAt(0))); |
| 482 } | 482 } |
| 483 | 483 |
| 484 IN_PROC_BROWSER_TEST_F(TabManagerTest, PurgeBackgroundRenderer) { | |
| 485 TabManager* tab_manager = g_browser_process->GetTabManager(); | |
| 486 | |
| 487 base::SimpleTestTickClock test_clock_; | |
| 488 tab_manager->set_test_tick_clock(&test_clock_); | |
| 489 | |
| 490 // Get three tabs open. | |
| 491 content::WindowedNotificationObserver load1( | |
| 492 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 493 content::NotificationService::AllSources()); | |
| 494 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(), | |
| 495 WindowOpenDisposition::CURRENT_TAB, | |
| 496 ui::PAGE_TRANSITION_TYPED, false); | |
| 497 browser()->OpenURL(open1); | |
| 498 load1.Wait(); | |
| 499 | |
| 500 content::WindowedNotificationObserver load2( | |
| 501 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 502 content::NotificationService::AllSources()); | |
| 503 OpenURLParams open2(GURL(chrome::kChromeUICreditsURL), content::Referrer(), | |
| 504 WindowOpenDisposition::NEW_FOREGROUND_TAB, | |
| 505 ui::PAGE_TRANSITION_TYPED, false); | |
| 506 browser()->OpenURL(open2); | |
| 507 load2.Wait(); | |
| 508 | |
| 509 content::WindowedNotificationObserver load3( | |
| 510 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 511 content::NotificationService::AllSources()); | |
| 512 OpenURLParams open3(GURL(chrome::kChromeUITermsURL), content::Referrer(), | |
| 513 WindowOpenDisposition::NEW_FOREGROUND_TAB, | |
| 514 ui::PAGE_TRANSITION_TYPED, false); | |
| 515 browser()->OpenURL(open3); | |
| 516 load3.Wait(); | |
| 517 | |
| 518 auto* tsm = browser()->tab_strip_model(); | |
| 519 TabManager::WebContentsData* tab1_contents_data = | |
| 520 tab_manager->GetWebContentsData(tsm->GetWebContentsAt(0)); | |
| 521 TabManager::WebContentsData* tab2_contents_data = | |
| 522 tab_manager->GetWebContentsData(tsm->GetWebContentsAt(1)); | |
| 523 TabManager::WebContentsData* tab3_contents_data = | |
| 524 tab_manager->GetWebContentsData(tsm->GetWebContentsAt(2)); | |
| 525 | |
| 526 // The time-to-purge initialized at ActiveTabChanged should be in the | |
| 527 // right default range. | |
| 528 EXPECT_GE(tab1_contents_data->time_to_purge(), | |
| 529 base::TimeDelta::FromMinutes(30)); | |
|
chrisha
2017/03/15 17:42:41
You should be referencing the class constants here
tasak
2017/03/16 04:39:48
I know. What I really want to do here is that "man
| |
| 530 EXPECT_LT(tab1_contents_data->time_to_purge(), | |
| 531 base::TimeDelta::FromMinutes(60)); | |
| 532 EXPECT_GE(tab2_contents_data->time_to_purge(), | |
| 533 base::TimeDelta::FromMinutes(30)); | |
| 534 EXPECT_LT(tab2_contents_data->time_to_purge(), | |
| 535 base::TimeDelta::FromMinutes(60)); | |
| 536 EXPECT_GE(tab3_contents_data->time_to_purge(), | |
| 537 base::TimeDelta::FromMinutes(30)); | |
| 538 EXPECT_LT(tab3_contents_data->time_to_purge(), | |
| 539 base::TimeDelta::FromMinutes(60)); | |
| 540 | |
| 541 // To make it easy to test, configure time-to-purge here. | |
| 542 base::TimeDelta time_to_purge1 = base::TimeDelta::FromMinutes(30); | |
| 543 base::TimeDelta time_to_purge2 = base::TimeDelta::FromMinutes(40); | |
| 544 tab1_contents_data->set_time_to_purge(time_to_purge1); | |
| 545 tab2_contents_data->set_time_to_purge(time_to_purge2); | |
| 546 tab3_contents_data->set_time_to_purge(time_to_purge1); | |
| 547 | |
| 548 // No tabs are not purged yet. | |
| 549 ASSERT_FALSE(tab1_contents_data->is_purged()); | |
| 550 ASSERT_FALSE(tab2_contents_data->is_purged()); | |
| 551 ASSERT_FALSE(tab3_contents_data->is_purged()); | |
| 552 | |
| 553 // Advance the clock for time_to_purge1. | |
| 554 test_clock_.Advance(time_to_purge1); | |
| 555 tab_manager->PurgeBackgroundedTabsIfNeeded(); | |
| 556 | |
| 557 ASSERT_FALSE(tab1_contents_data->is_purged()); | |
| 558 ASSERT_FALSE(tab2_contents_data->is_purged()); | |
| 559 ASSERT_FALSE(tab3_contents_data->is_purged()); | |
| 560 | |
| 561 // Advance the clock for 1 minutes. | |
| 562 test_clock_.Advance(base::TimeDelta::FromMinutes(1)); | |
| 563 tab_manager->PurgeBackgroundedTabsIfNeeded(); | |
| 564 | |
| 565 // Since tab1 is kept inactive and background for more than | |
| 566 // time_to_purge1, tab1 should be purged. | |
| 567 ASSERT_TRUE(tab1_contents_data->is_purged()); | |
| 568 ASSERT_FALSE(tab2_contents_data->is_purged()); | |
| 569 ASSERT_FALSE(tab3_contents_data->is_purged()); | |
| 570 | |
| 571 // Advance the clock. | |
| 572 test_clock_.Advance(time_to_purge2 - time_to_purge1); | |
| 573 tab_manager->PurgeBackgroundedTabsIfNeeded(); | |
| 574 | |
| 575 // Since tab2 is kept inactive and background for more than | |
| 576 // time_to_purge2, tab1 should be purged. | |
| 577 // Since tab3 is active, tab3 should not be purged. | |
| 578 ASSERT_TRUE(tab1_contents_data->is_purged()); | |
| 579 ASSERT_TRUE(tab2_contents_data->is_purged()); | |
| 580 ASSERT_FALSE(tab3_contents_data->is_purged()); | |
| 581 | |
| 582 tsm->CloseAllTabs(); | |
| 583 } | |
| 584 | |
| 484 } // namespace memory | 585 } // namespace memory |
| 485 | 586 |
| 486 #endif // OS_WIN || OS_MAXOSX || OS_LINUX | 587 #endif // OS_WIN || OS_MAXOSX || OS_LINUX |
| OLD | NEW |