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

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: Added a browser test. 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, DefaultTimeToPurgeInCorrectRange) {
676 TabManager tab_manager;
677 base::TimeDelta time_to_purge =
678 tab_manager.GetTimeToPurge(TabManager::kDefaultMinTimeToPurge);
679 EXPECT_GE(time_to_purge, base::TimeDelta::FromMinutes(30));
680 EXPECT_LT(time_to_purge, base::TimeDelta::FromMinutes(60));
681 }
682
683 TEST_F(TabManagerTest, ShouldPurgeAtDefaultTime) {
676 TabManager tab_manager; 684 TabManager tab_manager;
677 TabStripDummyDelegate delegate; 685 TabStripDummyDelegate delegate;
678 TabStripModel tabstrip(&delegate, profile()); 686 TabStripModel tabstrip(&delegate, profile());
679 tabstrip.AddObserver(&tab_manager); 687 tabstrip.AddObserver(&tab_manager);
680 688
681 WebContents* test_contents = CreateWebContents(); 689 WebContents* test_contents = CreateWebContents();
682 tabstrip.AppendWebContents(test_contents, false); 690 tabstrip.AppendWebContents(test_contents, false);
683 691
684 // Use default time-to-first-purge defined in TabManager.
685 base::TimeDelta threshold = TabManager::kDefaultTimeToFirstPurge;
686 base::SimpleTestTickClock test_clock; 692 base::SimpleTestTickClock test_clock;
693 tab_manager.set_test_tick_clock(&test_clock);
687 694
695 tab_manager.GetWebContentsData(test_contents)->set_is_purged(false);
688 tab_manager.GetWebContentsData(test_contents) 696 tab_manager.GetWebContentsData(test_contents)
689 ->SetPurgeAndSuspendState(TabManager::RUNNING); 697 ->SetLastInactiveTime(test_clock.NowTicks());
690 tab_manager.GetWebContentsData(test_contents) 698 tab_manager.GetWebContentsData(test_contents)
691 ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks()); 699 ->set_time_to_purge(base::TimeDelta::FromMinutes(1));
692 700
693 // Wait 30 minutes and verify that the tab is still RUNNING. 701 // Wait 1 minute and verify that the tab is still not to be purged.
694 test_clock.Advance(base::TimeDelta::FromMinutes(30)); 702 test_clock.Advance(base::TimeDelta::FromMinutes(1));
695 EXPECT_EQ(TabManager::RUNNING, 703 EXPECT_FALSE(tab_manager.ShouldPurgeAtTime(test_contents));
696 tab_manager.GetNextPurgeAndSuspendState(
697 test_contents, test_clock.NowTicks(), threshold));
698 704
699 // Wait another second and verify that it is now SUSPENDED. 705 // Wait another 1 second and verify that it should be purged now .
700 test_clock.Advance(base::TimeDelta::FromSeconds(1)); 706 test_clock.Advance(base::TimeDelta::FromSeconds(1));
701 EXPECT_EQ(TabManager::SUSPENDED, 707 EXPECT_TRUE(tab_manager.ShouldPurgeAtTime(test_contents));
702 tab_manager.GetNextPurgeAndSuspendState(
703 test_contents, test_clock.NowTicks(), threshold));
704 708
709 tab_manager.GetWebContentsData(test_contents)->set_is_purged(true);
705 tab_manager.GetWebContentsData(test_contents) 710 tab_manager.GetWebContentsData(test_contents)
706 ->SetPurgeAndSuspendState(TabManager::SUSPENDED); 711 ->SetLastInactiveTime(test_clock.NowTicks());
707 tab_manager.GetWebContentsData(test_contents)
708 ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks());
709 712
710 test_clock.Advance(base::TimeDelta::FromSeconds(1200)); 713 // Wait 1 day and verify that the tab is still be purged.
711 EXPECT_EQ(TabManager::SUSPENDED, 714 test_clock.Advance(base::TimeDelta::FromMinutes(60 * 24));
Wez 2017/03/03 21:06:51 You can use FromHours(24) here.
tasak 2017/03/06 10:30:19 Done.
712 tab_manager.GetNextPurgeAndSuspendState( 715 EXPECT_FALSE(tab_manager.ShouldPurgeAtTime(test_contents));
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 716
735 // Clean up the tabstrip. 717 // Clean up the tabstrip.
736 tabstrip.CloseAllTabs(); 718 tabstrip.CloseAllTabs();
737 EXPECT_TRUE(tabstrip.empty()); 719 EXPECT_TRUE(tabstrip.empty());
738 } 720 }
739 721
740 TEST_F(TabManagerTest, ActivateTabResetPurgeAndSuspendState) { 722 TEST_F(TabManagerTest, ActivateTabResetPurgeState) {
741 TabManager tab_manager; 723 TabManager tab_manager;
742 TabStripDummyDelegate delegate; 724 TabStripDummyDelegate delegate;
743 TabStripModel tabstrip(&delegate, profile()); 725 TabStripModel tabstrip(&delegate, profile());
744 tabstrip.AddObserver(&tab_manager); 726 tabstrip.AddObserver(&tab_manager);
745 727
746 WebContents* tab1 = CreateWebContents(); 728 WebContents* tab1 = CreateWebContents();
747 WebContents* tab2 = CreateWebContents(); 729 WebContents* tab2 = CreateWebContents();
748 tabstrip.AppendWebContents(tab1, true); 730 tabstrip.AppendWebContents(tab1, true);
749 tabstrip.AppendWebContents(tab2, false); 731 tabstrip.AppendWebContents(tab2, false);
750 732
751 base::SimpleTestTickClock test_clock; 733 base::SimpleTestTickClock test_clock;
752 734
753 // Initially PurgeAndSuspend state should be RUNNING. 735 // Initially PurgeAndSuspend state should be NOT_PURGED.
754 EXPECT_EQ(TabManager::RUNNING, 736 EXPECT_FALSE(tab_manager.GetWebContentsData(tab2)->is_purged());
755 tab_manager.GetWebContentsData(tab2)->GetPurgeAndSuspendState());
756 737
757 tab_manager.GetWebContentsData(tab2)->SetPurgeAndSuspendState( 738 tab_manager.GetWebContentsData(tab2)->set_is_purged(true);
Wez 2017/03/03 21:06:51 You're not actually going through the normal purge
tasak 2017/03/06 10:30:19 Done. However, what I want to do here is just chec
758 TabManager::SUSPENDED); 739 tab_manager.GetWebContentsData(tab2)->SetLastInactiveTime(
759 tab_manager.GetWebContentsData(tab2) 740 test_clock.NowTicks());
760 ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks());
761 741
762 // Activate tab2. Tab2's PurgeAndSuspend state should be RUNNING. 742 // Activate tab2. Tab2's PurgeAndSuspend state should be NOT_PURGED.
763 tabstrip.ActivateTabAt(1, true /* user_gesture */); 743 tabstrip.ActivateTabAt(1, true /* user_gesture */);
764 EXPECT_EQ(TabManager::RUNNING, 744 EXPECT_FALSE(tab_manager.GetWebContentsData(tab2)->is_purged());
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 745
777 // Clean up the tabstrip. 746 // Clean up the tabstrip.
778 tabstrip.CloseAllTabs(); 747 tabstrip.CloseAllTabs();
779 EXPECT_TRUE(tabstrip.empty()); 748 EXPECT_TRUE(tabstrip.empty());
Wez 2017/03/03 21:06:51 nit: Do you need to expect this? Isn't it part of
tasak 2017/03/06 10:30:19 Removed.
780 } 749 }
781 750
782 } // namespace memory 751 } // namespace memory
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698