oom_priority_manager_browsertest.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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 3// found in the LICENSE file. 4 5#include "chrome/browser/browser_process.h" 6#include "chrome/browser/chromeos/memory/oom_priority_manager.h" 7#include "chrome/browser/ui/browser.h" 8#include "chrome/browser/ui/browser_commands.h" 9#include "chrome/browser/ui/find_bar/find_bar_controller.h" 10#include "chrome/browser/ui/tabs/tab_strip_model.h" 11#include "chrome/common/url_constants.h" 12#include "chrome/test/base/in_process_browser_test.h" 13#include "content/public/browser/notification_service.h" 14#include "content/public/browser/notification_types.h" 15#include "content/public/test/test_utils.h" 16#include "googleurl/src/gurl.h" 17 18using content::OpenURLParams; 19 20namespace { 21 22typedef InProcessBrowserTest OomPriorityManagerTest; 23 24IN_PROC_BROWSER_TEST_F(OomPriorityManagerTest, OomPriorityManagerBasics) { 25 using content::WindowedNotificationObserver; 26 27 chromeos::OomPriorityManager* oom_priority_manager = 28 g_browser_process->oom_priority_manager(); 29 EXPECT_FALSE(oom_priority_manager->recent_tab_discard()); 30 31 // Get three tabs open. 32 WindowedNotificationObserver load1( 33 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 34 content::NotificationService::AllSources()); 35 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(), 36 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false); 37 browser()->OpenURL(open1); 38 load1.Wait(); 39 40 WindowedNotificationObserver load2( 41 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 42 content::NotificationService::AllSources()); 43 OpenURLParams open2(GURL(chrome::kChromeUICreditsURL), content::Referrer(), 44 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, 45 false); 46 browser()->OpenURL(open2); 47 load2.Wait(); 48 49 WindowedNotificationObserver load3( 50 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 51 content::NotificationService::AllSources()); 52 OpenURLParams open3(GURL(chrome::kChromeUITermsURL), content::Referrer(), 53 NEW_FOREGROUND_TAB, content::PAGE_TRANSITION_TYPED, 54 false); 55 browser()->OpenURL(open3); 56 load3.Wait(); 57 58 EXPECT_EQ(3, browser()->tab_strip_model()->count()); 59 60 // Navigate the current (third) tab to a different URL, so we can test 61 // back/forward later. 62 WindowedNotificationObserver load4( 63 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 64 content::NotificationService::AllSources()); 65 OpenURLParams open4(GURL(chrome::kChromeUIVersionURL), content::Referrer(), 66 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, 67 false); 68 browser()->OpenURL(open4); 69 load4.Wait(); 70 71 // Navigate the third tab again, such that we have three navigation entries. 72 WindowedNotificationObserver load5( 73 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 74 content::NotificationService::AllSources()); 75 OpenURLParams open5(GURL("chrome://dns"), content::Referrer(), 76 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, 77 false); 78 browser()->OpenURL(open5); 79 load5.Wait(); 80 81 EXPECT_EQ(3, browser()->tab_strip_model()->count()); 82 83 // Discard a tab. It should kill the first tab, since it was the oldest 84 // and was not selected. 85 EXPECT_TRUE(g_browser_process->oom_priority_manager()->DiscardTab()); 86 EXPECT_EQ(3, browser()->tab_strip_model()->count()); 87 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0)); 88 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1)); 89 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2)); 90 EXPECT_TRUE(oom_priority_manager->recent_tab_discard()); 91 92 // Run discard again, make sure it kills the second tab. 93 EXPECT_TRUE(g_browser_process->oom_priority_manager()->DiscardTab()); 94 EXPECT_EQ(3, browser()->tab_strip_model()->count()); 95 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0)); 96 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(1)); 97 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2)); 98 99 // Kill the third tab. It should not kill the last tab, since it is active 100 // tab. 101 EXPECT_FALSE(g_browser_process->oom_priority_manager()->DiscardTab()); 102 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0)); 103 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(1)); 104 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2)); 105 106 // Kill the third tab after making second tab active. 107 browser()->tab_strip_model()->ActivateTabAt(1, true); 108 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); 109 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1)); 110 browser()->tab_strip_model()->DiscardWebContentsAt(2); 111 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(2)); 112 113 // Force creation of the FindBarController. 114 browser()->GetFindBarController(); 115 116 // Select the first tab. It should reload. 117 WindowedNotificationObserver reload1( 118 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 119 content::NotificationService::AllSources()); 120 chrome::SelectNumberedTab(browser(), 0); 121 reload1.Wait(); 122 // Make sure the FindBarController gets the right WebContents. 123 EXPECT_EQ(browser()->GetFindBarController()->web_contents(), 124 browser()->tab_strip_model()->GetActiveWebContents()); 125 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); 126 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(0)); 127 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1)); 128 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(2)); 129 130 // Select the third tab. It should reload. 131 WindowedNotificationObserver reload2( 132 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 133 content::NotificationService::AllSources()); 134 chrome::SelectNumberedTab(browser(), 2); 135 reload2.Wait(); 136 EXPECT_EQ(2, browser()->tab_strip_model()->active_index()); 137 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(0)); 138 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1)); 139 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2)); 140 141 // Navigate the third tab back twice. We used to crash here due to 142 // crbug.com/121373. 143 EXPECT_TRUE(chrome::CanGoBack(browser())); 144 EXPECT_FALSE(chrome::CanGoForward(browser())); 145 WindowedNotificationObserver back1( 146 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 147 content::NotificationService::AllSources()); 148 chrome::GoBack(browser(), CURRENT_TAB); 149 back1.Wait(); 150 EXPECT_TRUE(chrome::CanGoBack(browser())); 151 EXPECT_TRUE(chrome::CanGoForward(browser())); 152 WindowedNotificationObserver back2( 153 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 154 content::NotificationService::AllSources()); 155 chrome::GoBack(browser(), CURRENT_TAB); 156 back2.Wait(); 157 EXPECT_FALSE(chrome::CanGoBack(browser())); 158 EXPECT_TRUE(chrome::CanGoForward(browser())); 159} 160 161} // namespace 162