| 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 "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 Loading... |
| 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) { |
| 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; |
| 687 | 685 |
| 688 tab_manager.GetWebContentsData(test_contents) | 686 tab_manager.GetWebContentsData(test_contents) |
| 689 ->SetPurgeAndSuspendState(TabManager::RUNNING); | 687 ->SetPurgeState(TabManager::NOT_PURGED); |
| 690 tab_manager.GetWebContentsData(test_contents) | 688 tab_manager.GetWebContentsData(test_contents) |
| 691 ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks()); | 689 ->SetLastInactiveTime(test_clock.NowTicks()); |
| 690 // Use default time-to-first-purge defined in TabManager. |
| 691 tab_manager.GetWebContentsData(test_contents) |
| 692 ->SetTimeToFirstPurge(TabManager::kDefaultTimeToFirstPurge); |
| 692 | 693 |
| 693 // Wait 30 minutes and verify that the tab is still RUNNING. | 694 // Wait 30 minutes and verify that the tab is still NOT_PURGED. |
| 694 test_clock.Advance(base::TimeDelta::FromMinutes(30)); | 695 test_clock.Advance(base::TimeDelta::FromMinutes(30)); |
| 695 EXPECT_EQ(TabManager::RUNNING, | 696 EXPECT_EQ( |
| 696 tab_manager.GetNextPurgeAndSuspendState( | 697 TabManager::NOT_PURGED, |
| 697 test_contents, test_clock.NowTicks(), threshold)); | 698 tab_manager.GetNextPurgeState(test_contents, test_clock.NowTicks())); |
| 698 | 699 |
| 699 // Wait another second and verify that it is now SUSPENDED. | 700 // Wait another second and verify that it is now SUSPENDED. |
| 700 test_clock.Advance(base::TimeDelta::FromSeconds(1)); | 701 test_clock.Advance(base::TimeDelta::FromSeconds(1)); |
| 701 EXPECT_EQ(TabManager::SUSPENDED, | 702 EXPECT_EQ( |
| 702 tab_manager.GetNextPurgeAndSuspendState( | 703 TabManager::PURGED, |
| 703 test_contents, test_clock.NowTicks(), threshold)); | 704 tab_manager.GetNextPurgeState(test_contents, test_clock.NowTicks())); |
| 704 | 705 |
| 705 tab_manager.GetWebContentsData(test_contents) | 706 tab_manager.GetWebContentsData(test_contents) |
| 706 ->SetPurgeAndSuspendState(TabManager::SUSPENDED); | 707 ->SetPurgeState(TabManager::PURGED); |
| 707 tab_manager.GetWebContentsData(test_contents) | 708 tab_manager.GetWebContentsData(test_contents) |
| 708 ->SetLastPurgeAndSuspendModifiedTimeForTesting(test_clock.NowTicks()); | 709 ->SetLastInactiveTime(test_clock.NowTicks()); |
| 709 | 710 |
| 710 test_clock.Advance(base::TimeDelta::FromSeconds(1200)); | 711 test_clock.Advance(base::TimeDelta::FromSeconds(1200)); |
| 711 EXPECT_EQ(TabManager::SUSPENDED, | 712 EXPECT_EQ( |
| 712 tab_manager.GetNextPurgeAndSuspendState( | 713 TabManager::PURGED, |
| 713 test_contents, test_clock.NowTicks(), threshold)); | 714 tab_manager.GetNextPurgeState(test_contents, test_clock.NowTicks())); |
| 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 | 715 |
| 735 // Clean up the tabstrip. | 716 // Clean up the tabstrip. |
| 736 tabstrip.CloseAllTabs(); | 717 tabstrip.CloseAllTabs(); |
| 737 EXPECT_TRUE(tabstrip.empty()); | 718 EXPECT_TRUE(tabstrip.empty()); |
| 738 } | 719 } |
| 739 | 720 |
| 740 TEST_F(TabManagerTest, ActivateTabResetPurgeAndSuspendState) { | 721 TEST_F(TabManagerTest, ActivateTabResetPurgeState) { |
| 741 TabManager tab_manager; | 722 TabManager tab_manager; |
| 742 TabStripDummyDelegate delegate; | 723 TabStripDummyDelegate delegate; |
| 743 TabStripModel tabstrip(&delegate, profile()); | 724 TabStripModel tabstrip(&delegate, profile()); |
| 744 tabstrip.AddObserver(&tab_manager); | 725 tabstrip.AddObserver(&tab_manager); |
| 745 | 726 |
| 746 WebContents* tab1 = CreateWebContents(); | 727 WebContents* tab1 = CreateWebContents(); |
| 747 WebContents* tab2 = CreateWebContents(); | 728 WebContents* tab2 = CreateWebContents(); |
| 748 tabstrip.AppendWebContents(tab1, true); | 729 tabstrip.AppendWebContents(tab1, true); |
| 749 tabstrip.AppendWebContents(tab2, false); | 730 tabstrip.AppendWebContents(tab2, false); |
| 750 | 731 |
| 751 base::SimpleTestTickClock test_clock; | 732 base::SimpleTestTickClock test_clock; |
| 752 | 733 |
| 753 // Initially PurgeAndSuspend state should be RUNNING. | 734 // Initially PurgeAndSuspend state should be NOT_PURGED. |
| 754 EXPECT_EQ(TabManager::RUNNING, | 735 EXPECT_EQ(TabManager::NOT_PURGED, |
| 755 tab_manager.GetWebContentsData(tab2)->GetPurgeAndSuspendState()); | 736 tab_manager.GetWebContentsData(tab2)->GetPurgeState()); |
| 756 | 737 |
| 757 tab_manager.GetWebContentsData(tab2)->SetPurgeAndSuspendState( | 738 tab_manager.GetWebContentsData(tab2)->SetPurgeState(TabManager::PURGED); |
| 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_EQ(TabManager::NOT_PURGED, |
| 765 tab_manager.GetWebContentsData(tab2)->GetPurgeAndSuspendState()); | 745 tab_manager.GetWebContentsData(tab2)->GetPurgeState()); |
| 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 | 746 |
| 777 // Clean up the tabstrip. | 747 // Clean up the tabstrip. |
| 778 tabstrip.CloseAllTabs(); | 748 tabstrip.CloseAllTabs(); |
| 779 EXPECT_TRUE(tabstrip.empty()); | 749 EXPECT_TRUE(tabstrip.empty()); |
| 780 } | 750 } |
| 781 | 751 |
| 782 } // namespace memory | 752 } // namespace memory |
| OLD | NEW |