Chromium Code Reviews| Index: chrome/browser/memory/tab_manager_unittest.cc |
| diff --git a/chrome/browser/memory/tab_manager_unittest.cc b/chrome/browser/memory/tab_manager_unittest.cc |
| index c20c3946fc9d2b29eb99a79ca8ccaea6b7079005..5fd7b77ff3e94375cca8756ad541df3e19bf01e9 100644 |
| --- a/chrome/browser/memory/tab_manager_unittest.cc |
| +++ b/chrome/browser/memory/tab_manager_unittest.cc |
| @@ -27,7 +27,9 @@ |
| #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| #include "chrome/test/base/testing_profile.h" |
| #include "components/variations/variations_associated_data.h" |
| +#include "content/public/browser/render_process_host.h" |
| #include "content/public/browser/web_contents.h" |
| +#include "content/public/test/mock_render_process_host.h" |
| #include "content/public/test/web_contents_tester.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -672,7 +674,15 @@ TEST_F(TabManagerTest, MAYBE_ChildProcessNotifications) { |
| ASSERT_TRUE(tabstrip.empty()); |
| } |
| -TEST_F(TabManagerTest, NextPurgeAndSuspendState) { |
| +TEST_F(TabManagerTest, DefaultTimeToPurgeInCorrectRange) { |
| + TabManager tab_manager; |
| + base::TimeDelta time_to_purge = |
| + tab_manager.GetTimeToPurge(TabManager::kDefaultMinTimeToPurge); |
| + EXPECT_GE(time_to_purge, base::TimeDelta::FromMinutes(30)); |
| + EXPECT_LT(time_to_purge, base::TimeDelta::FromMinutes(60)); |
|
chrisha
2017/03/15 17:42:41
Ditto for using constants and not magic numbers.
tasak
2017/03/16 04:39:48
I would like to use magic numbers here to check th
|
| +} |
| + |
| +TEST_F(TabManagerTest, ShouldPurgeAtDefaultTime) { |
| TabManager tab_manager; |
| TabStripDummyDelegate delegate; |
| TabStripModel tabstrip(&delegate, profile()); |
| @@ -681,102 +691,67 @@ TEST_F(TabManagerTest, NextPurgeAndSuspendState) { |
| WebContents* test_contents = CreateWebContents(); |
| tabstrip.AppendWebContents(test_contents, false); |
| - // Use default time-to-first-purge defined in TabManager. |
| - base::TimeDelta threshold = TabManager::kDefaultTimeToFirstPurge; |
| base::SimpleTestTickClock test_clock; |
| + tab_manager.set_test_tick_clock(&test_clock); |
| + tab_manager.GetWebContentsData(test_contents)->set_is_purged(false); |
| tab_manager.GetWebContentsData(test_contents) |
| - ->SetPurgeAndSuspendState(TabManager::RUNNING); |
| + ->SetLastInactiveTime(test_clock.NowTicks()); |
| tab_manager.GetWebContentsData(test_contents) |
| - ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks()); |
| + ->set_time_to_purge(base::TimeDelta::FromMinutes(1)); |
| - // Wait 30 minutes and verify that the tab is still RUNNING. |
| - test_clock.Advance(base::TimeDelta::FromMinutes(30)); |
| - EXPECT_EQ(TabManager::RUNNING, |
| - tab_manager.GetNextPurgeAndSuspendState( |
| - test_contents, test_clock.NowTicks(), threshold)); |
| + // Wait 1 minute and verify that the tab is still not to be purged. |
| + test_clock.Advance(base::TimeDelta::FromMinutes(1)); |
| + EXPECT_FALSE(tab_manager.ShouldPurgeNow(test_contents)); |
| - // Wait another second and verify that it is now SUSPENDED. |
| + // Wait another 1 second and verify that it should be purged now . |
| test_clock.Advance(base::TimeDelta::FromSeconds(1)); |
| - EXPECT_EQ(TabManager::SUSPENDED, |
| - tab_manager.GetNextPurgeAndSuspendState( |
| - test_contents, test_clock.NowTicks(), threshold)); |
| + EXPECT_TRUE(tab_manager.ShouldPurgeNow(test_contents)); |
| + tab_manager.GetWebContentsData(test_contents)->set_is_purged(true); |
| tab_manager.GetWebContentsData(test_contents) |
| - ->SetPurgeAndSuspendState(TabManager::SUSPENDED); |
| - tab_manager.GetWebContentsData(test_contents) |
| - ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks()); |
| + ->SetLastInactiveTime(test_clock.NowTicks()); |
| - test_clock.Advance(base::TimeDelta::FromSeconds(1200)); |
| - EXPECT_EQ(TabManager::SUSPENDED, |
| - tab_manager.GetNextPurgeAndSuspendState( |
| - test_contents, test_clock.NowTicks(), threshold)); |
| - |
| - test_clock.Advance(base::TimeDelta::FromSeconds(1)); |
| - EXPECT_EQ(TabManager::RESUMED, |
| - tab_manager.GetNextPurgeAndSuspendState( |
| - test_contents, test_clock.NowTicks(), threshold)); |
| - |
| - tab_manager.GetWebContentsData(test_contents) |
| - ->SetPurgeAndSuspendState(TabManager::RESUMED); |
| - tab_manager.GetWebContentsData(test_contents) |
| - ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks()); |
| - |
| - test_clock.Advance(base::TimeDelta::FromSeconds(10)); |
| - EXPECT_EQ(TabManager::RESUMED, |
| - tab_manager.GetNextPurgeAndSuspendState( |
| - test_contents, test_clock.NowTicks(), threshold)); |
| - |
| - test_clock.Advance(base::TimeDelta::FromSeconds(1)); |
| - EXPECT_EQ(TabManager::SUSPENDED, |
| - tab_manager.GetNextPurgeAndSuspendState( |
| - test_contents, test_clock.NowTicks(), threshold)); |
| - |
| - // Clean up the tabstrip. |
| - tabstrip.CloseAllTabs(); |
| - EXPECT_TRUE(tabstrip.empty()); |
| + // Wait 1 day and verify that the tab is still be purged. |
| + test_clock.Advance(base::TimeDelta::FromHours(24)); |
| + EXPECT_FALSE(tab_manager.ShouldPurgeNow(test_contents)); |
| } |
| -TEST_F(TabManagerTest, ActivateTabResetPurgeAndSuspendState) { |
| +TEST_F(TabManagerTest, ActivateTabResetPurgeState) { |
| TabManager tab_manager; |
| TabStripDummyDelegate delegate; |
| TabStripModel tabstrip(&delegate, profile()); |
| tabstrip.AddObserver(&tab_manager); |
| + tab_manager.test_tab_strip_models_.push_back( |
| + TabManager::TestTabStripModel(&tabstrip, false /* !is_app */)); |
| + |
| + base::SimpleTestTickClock test_clock; |
| + tab_manager.set_test_tick_clock(&test_clock); |
| WebContents* tab1 = CreateWebContents(); |
| WebContents* tab2 = CreateWebContents(); |
| tabstrip.AppendWebContents(tab1, true); |
| tabstrip.AppendWebContents(tab2, false); |
| - base::SimpleTestTickClock test_clock; |
| - |
| - // Initially PurgeAndSuspend state should be RUNNING. |
| - EXPECT_EQ(TabManager::RUNNING, |
| - tab_manager.GetWebContentsData(tab2)->GetPurgeAndSuspendState()); |
| - |
| - tab_manager.GetWebContentsData(tab2)->SetPurgeAndSuspendState( |
| - TabManager::SUSPENDED); |
| - tab_manager.GetWebContentsData(tab2) |
| - ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks()); |
| - |
| - // Activate tab2. Tab2's PurgeAndSuspend state should be RUNNING. |
| + tab_manager.GetWebContentsData(tab2)->SetLastInactiveTime( |
| + test_clock.NowTicks()); |
| + static_cast<content::MockRenderProcessHost*>(tab2->GetRenderProcessHost()) |
| + ->set_is_process_backgrounded(true); |
| + EXPECT_TRUE(tab2->GetRenderProcessHost()->IsProcessBackgrounded()); |
| + |
| + // Initially PurgeAndSuspend state should be NOT_PURGED. |
| + EXPECT_FALSE(tab_manager.GetWebContentsData(tab2)->is_purged()); |
| + tab_manager.GetWebContentsData(tab2)->set_time_to_purge( |
| + base::TimeDelta::FromMinutes(1)); |
| + test_clock.Advance(base::TimeDelta::FromMinutes(2)); |
| + tab_manager.PurgeBackgroundedTabsIfNeeded(); |
| + // Since tab2 is kept inactive and background for more than time-to-purge, |
| + // tab2 should be purged. |
| + EXPECT_TRUE(tab_manager.GetWebContentsData(tab2)->is_purged()); |
| + |
| + // Activate tab2. Tab2's PurgeAndSuspend state should be NOT_PURGED. |
| tabstrip.ActivateTabAt(1, true /* user_gesture */); |
| - EXPECT_EQ(TabManager::RUNNING, |
| - tab_manager.GetWebContentsData(tab2)->GetPurgeAndSuspendState()); |
| - |
| - tab_manager.GetWebContentsData(tab1)->SetPurgeAndSuspendState( |
| - TabManager::RESUMED); |
| - tab_manager.GetWebContentsData(tab1) |
| - ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks()); |
| - |
| - // Activate tab1. Tab1's PurgeAndSuspend state should be RUNNING. |
| - tabstrip.ActivateTabAt(0, true /* user_gesture */); |
| - EXPECT_EQ(TabManager::RUNNING, |
| - tab_manager.GetWebContentsData(tab1)->GetPurgeAndSuspendState()); |
| - |
| - // Clean up the tabstrip. |
| - tabstrip.CloseAllTabs(); |
| - EXPECT_TRUE(tabstrip.empty()); |
| + EXPECT_FALSE(tab_manager.GetWebContentsData(tab2)->is_purged()); |
| } |
| } // namespace memory |