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

Side by Side Diff: chrome/browser/memory/tab_manager_unittest.cc

Issue 2711093002: Purge once random minutes(between 30min and 60min) after backgrounded. (Closed)
Patch Set: Fixed. 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 (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 "chrome/browser/memory/tab_manager.h" 5 #include "chrome/browser/memory/tab_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 654 matching lines...) Expand 10 before | Expand all | Expand 10 after
665 EXPECT_FALSE(tm.under_memory_pressure_); 665 EXPECT_FALSE(tm.under_memory_pressure_);
666 EXPECT_EQ(0u, task_runner->size()); 666 EXPECT_EQ(0u, task_runner->size());
667 EXPECT_EQ(0u, tm.notified_renderers_.size()); 667 EXPECT_EQ(0u, tm.notified_renderers_.size());
668 668
669 669
670 // Clean up the tabstrip. 670 // Clean up the tabstrip.
671 tabstrip.CloseAllTabs(); 671 tabstrip.CloseAllTabs();
672 ASSERT_TRUE(tabstrip.empty()); 672 ASSERT_TRUE(tabstrip.empty());
673 } 673 }
674 674
675 TEST_F(TabManagerTest, NextPurgeAndSuspendState) { 675 TEST_F(TabManagerTest, NextPurgeState) {
Wez 2017/03/02 02:28:32 NextPurgeState was the name of the function - you
tasak 2017/03/02 09:22:47 Renamed to be ShouldPurgeAtDefaultTime.
Wez 2017/03/03 21:06:51 Acknowledged.
676 TabManager tab_manager; 676 TabManager tab_manager;
677 TabStripDummyDelegate delegate; 677 TabStripDummyDelegate delegate;
678 TabStripModel tabstrip(&delegate, profile()); 678 TabStripModel tabstrip(&delegate, profile());
679 tabstrip.AddObserver(&tab_manager); 679 tabstrip.AddObserver(&tab_manager);
680 680
681 WebContents* test_contents = CreateWebContents(); 681 WebContents* test_contents = CreateWebContents();
682 tabstrip.AppendWebContents(test_contents, false); 682 tabstrip.AppendWebContents(test_contents, false);
683 683
684 // Use default time-to-first-purge defined in TabManager.
685 base::TimeDelta threshold = TabManager::kDefaultTimeToFirstPurge;
686 base::SimpleTestTickClock test_clock; 684 base::SimpleTestTickClock test_clock;
Wez 2017/03/02 02:28:32 You probably want to tab_manager.set_test_tick_clo
tasak 2017/03/02 09:22:47 Done.
687 685
686 tab_manager.GetWebContentsData(test_contents)->SetPurgeState(false);
688 tab_manager.GetWebContentsData(test_contents) 687 tab_manager.GetWebContentsData(test_contents)
689 ->SetPurgeAndSuspendState(TabManager::RUNNING); 688 ->SetLastInactiveTime(test_clock.NowTicks());
689 // Use default min-time-to-purge defined in TabManager.
690 base::TimeDelta time_to_purge =
691 tab_manager.GetTimeToPurge(TabManager::kDefaultMinTimeToPurge);
692 tab_manager.GetWebContentsData(test_contents)->SetTimeToPurge(time_to_purge);
693
694 // Wait 30 minutes and verify that the tab is still NOT_PURGED.
695 test_clock.Advance(base::TimeDelta::FromMinutes(30));
696 EXPECT_FALSE(
697 tab_manager.ShouldPurgeAtTime(test_contents, test_clock.NowTicks()));
698
699 // Wait another 30 minutes and verify that it should be purged now .
700 test_clock.Advance(base::TimeDelta::FromMinutes(30));
701 EXPECT_TRUE(
702 tab_manager.ShouldPurgeAtTime(test_contents, test_clock.NowTicks()));
703
704 tab_manager.GetWebContentsData(test_contents)->SetPurgeState(true);
690 tab_manager.GetWebContentsData(test_contents) 705 tab_manager.GetWebContentsData(test_contents)
691 ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks()); 706 ->SetLastInactiveTime(test_clock.NowTicks());
692 707
693 // Wait 30 minutes and verify that the tab is still RUNNING. 708 test_clock.Advance(base::TimeDelta::FromMinutes(120));
694 test_clock.Advance(base::TimeDelta::FromMinutes(30)); 709 EXPECT_FALSE(
695 EXPECT_EQ(TabManager::RUNNING, 710 tab_manager.ShouldPurgeAtTime(test_contents, test_clock.NowTicks()));
696 tab_manager.GetNextPurgeAndSuspendState(
697 test_contents, test_clock.NowTicks(), threshold));
698
699 // Wait another second and verify that it is now SUSPENDED.
700 test_clock.Advance(base::TimeDelta::FromSeconds(1));
701 EXPECT_EQ(TabManager::SUSPENDED,
702 tab_manager.GetNextPurgeAndSuspendState(
703 test_contents, test_clock.NowTicks(), threshold));
704
705 tab_manager.GetWebContentsData(test_contents)
706 ->SetPurgeAndSuspendState(TabManager::SUSPENDED);
707 tab_manager.GetWebContentsData(test_contents)
708 ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks());
709
710 test_clock.Advance(base::TimeDelta::FromSeconds(1200));
711 EXPECT_EQ(TabManager::SUSPENDED,
712 tab_manager.GetNextPurgeAndSuspendState(
713 test_contents, test_clock.NowTicks(), threshold));
714
715 test_clock.Advance(base::TimeDelta::FromSeconds(1));
716 EXPECT_EQ(TabManager::RESUMED,
717 tab_manager.GetNextPurgeAndSuspendState(
718 test_contents, test_clock.NowTicks(), threshold));
719
720 tab_manager.GetWebContentsData(test_contents)
721 ->SetPurgeAndSuspendState(TabManager::RESUMED);
722 tab_manager.GetWebContentsData(test_contents)
723 ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks());
724
725 test_clock.Advance(base::TimeDelta::FromSeconds(10));
726 EXPECT_EQ(TabManager::RESUMED,
727 tab_manager.GetNextPurgeAndSuspendState(
728 test_contents, test_clock.NowTicks(), threshold));
729
730 test_clock.Advance(base::TimeDelta::FromSeconds(1));
731 EXPECT_EQ(TabManager::SUSPENDED,
732 tab_manager.GetNextPurgeAndSuspendState(
733 test_contents, test_clock.NowTicks(), threshold));
734 711
735 // Clean up the tabstrip. 712 // Clean up the tabstrip.
736 tabstrip.CloseAllTabs(); 713 tabstrip.CloseAllTabs();
737 EXPECT_TRUE(tabstrip.empty()); 714 EXPECT_TRUE(tabstrip.empty());
738 } 715 }
739 716
740 TEST_F(TabManagerTest, ActivateTabResetPurgeAndSuspendState) { 717 TEST_F(TabManagerTest, ActivateTabResetPurgeState) {
741 TabManager tab_manager; 718 TabManager tab_manager;
742 TabStripDummyDelegate delegate; 719 TabStripDummyDelegate delegate;
743 TabStripModel tabstrip(&delegate, profile()); 720 TabStripModel tabstrip(&delegate, profile());
744 tabstrip.AddObserver(&tab_manager); 721 tabstrip.AddObserver(&tab_manager);
745 722
746 WebContents* tab1 = CreateWebContents(); 723 WebContents* tab1 = CreateWebContents();
747 WebContents* tab2 = CreateWebContents(); 724 WebContents* tab2 = CreateWebContents();
748 tabstrip.AppendWebContents(tab1, true); 725 tabstrip.AppendWebContents(tab1, true);
749 tabstrip.AppendWebContents(tab2, false); 726 tabstrip.AppendWebContents(tab2, false);
750 727
751 base::SimpleTestTickClock test_clock; 728 base::SimpleTestTickClock test_clock;
752 729
753 // Initially PurgeAndSuspend state should be RUNNING. 730 // Initially PurgeAndSuspend state should be NOT_PURGED.
754 EXPECT_EQ(TabManager::RUNNING, 731 EXPECT_FALSE(tab_manager.GetWebContentsData(tab2)->IsPurged());
755 tab_manager.GetWebContentsData(tab2)->GetPurgeAndSuspendState());
756 732
757 tab_manager.GetWebContentsData(tab2)->SetPurgeAndSuspendState( 733 tab_manager.GetWebContentsData(tab2)->SetPurgeState(true);
758 TabManager::SUSPENDED); 734 tab_manager.GetWebContentsData(tab2)->SetLastInactiveTime(
759 tab_manager.GetWebContentsData(tab2) 735 test_clock.NowTicks());
760 ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks());
761 736
762 // Activate tab2. Tab2's PurgeAndSuspend state should be RUNNING. 737 // Activate tab2. Tab2's PurgeAndSuspend state should be NOT_PURGED.
763 tabstrip.ActivateTabAt(1, true /* user_gesture */); 738 tabstrip.ActivateTabAt(1, true /* user_gesture */);
764 EXPECT_EQ(TabManager::RUNNING, 739 EXPECT_FALSE(tab_manager.GetWebContentsData(tab2)->IsPurged());
765 tab_manager.GetWebContentsData(tab2)->GetPurgeAndSuspendState());
766
767 tab_manager.GetWebContentsData(tab1)->SetPurgeAndSuspendState(
768 TabManager::RESUMED);
769 tab_manager.GetWebContentsData(tab1)
770 ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks());
771
772 // Activate tab1. Tab1's PurgeAndSuspend state should be RUNNING.
773 tabstrip.ActivateTabAt(0, true /* user_gesture */);
774 EXPECT_EQ(TabManager::RUNNING,
775 tab_manager.GetWebContentsData(tab1)->GetPurgeAndSuspendState());
776 740
777 // Clean up the tabstrip. 741 // Clean up the tabstrip.
778 tabstrip.CloseAllTabs(); 742 tabstrip.CloseAllTabs();
779 EXPECT_TRUE(tabstrip.empty()); 743 EXPECT_TRUE(tabstrip.empty());
780 } 744 }
781 745
782 } // namespace memory 746 } // namespace memory
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698