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.
5 #include "base/command_line.h"
6 #include "base/memory/memory_pressure_listener.h"
7 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/memory/oom_priority_manager.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/find_bar/find_bar_controller.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/common/url_constants.h"
14 #include "chrome/test/base/in_process_browser_test.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/notification_types.h"
17 #include "content/public/test/test_utils.h"
20 using content::OpenURLParams
;
22 // OomPriorityManager is only active on Chrome OS.
23 #if defined(OS_CHROMEOS)
28 typedef InProcessBrowserTest OomPriorityManagerTest
;
30 IN_PROC_BROWSER_TEST_F(OomPriorityManagerTest
, OomPriorityManagerBasics
) {
31 using content::WindowedNotificationObserver
;
32 OomPriorityManager
* oom_priority_manager
=
33 g_browser_process
->GetOomPriorityManager();
34 ASSERT_TRUE(oom_priority_manager
);
35 EXPECT_FALSE(oom_priority_manager
->recent_tab_discard());
37 // Get three tabs open.
38 WindowedNotificationObserver
load1(
39 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
40 content::NotificationService::AllSources());
41 OpenURLParams
open1(GURL(chrome::kChromeUIAboutURL
), content::Referrer(),
42 CURRENT_TAB
, ui::PAGE_TRANSITION_TYPED
, false);
43 browser()->OpenURL(open1
);
46 WindowedNotificationObserver
load2(
47 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
48 content::NotificationService::AllSources());
49 OpenURLParams
open2(GURL(chrome::kChromeUICreditsURL
), content::Referrer(),
50 NEW_FOREGROUND_TAB
, ui::PAGE_TRANSITION_TYPED
, false);
51 browser()->OpenURL(open2
);
54 WindowedNotificationObserver
load3(
55 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
56 content::NotificationService::AllSources());
57 OpenURLParams
open3(GURL(chrome::kChromeUITermsURL
), content::Referrer(),
58 NEW_FOREGROUND_TAB
, ui::PAGE_TRANSITION_TYPED
, false);
59 browser()->OpenURL(open3
);
62 EXPECT_EQ(3, browser()->tab_strip_model()->count());
64 // Navigate the current (third) tab to a different URL, so we can test
65 // back/forward later.
66 WindowedNotificationObserver
load4(
67 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
68 content::NotificationService::AllSources());
69 OpenURLParams
open4(GURL(chrome::kChromeUIVersionURL
), content::Referrer(),
70 CURRENT_TAB
, ui::PAGE_TRANSITION_TYPED
, false);
71 browser()->OpenURL(open4
);
74 // Navigate the third tab again, such that we have three navigation entries.
75 WindowedNotificationObserver
load5(
76 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
77 content::NotificationService::AllSources());
78 OpenURLParams
open5(GURL("chrome://dns"), content::Referrer(), CURRENT_TAB
,
79 ui::PAGE_TRANSITION_TYPED
, false);
80 browser()->OpenURL(open5
);
83 EXPECT_EQ(3, browser()->tab_strip_model()->count());
85 // Discard a tab. It should kill the first tab, since it was the oldest
86 // and was not selected.
87 EXPECT_TRUE(oom_priority_manager
->DiscardTab());
88 EXPECT_EQ(3, browser()->tab_strip_model()->count());
89 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0));
90 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1));
91 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2));
92 EXPECT_TRUE(oom_priority_manager
->recent_tab_discard());
94 // Run discard again, make sure it kills the second tab.
95 EXPECT_TRUE(oom_priority_manager
->DiscardTab());
96 EXPECT_EQ(3, browser()->tab_strip_model()->count());
97 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0));
98 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(1));
99 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2));
101 // Kill the third tab. It should not kill the last tab, since it is active
103 EXPECT_FALSE(oom_priority_manager
->DiscardTab());
104 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0));
105 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(1));
106 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2));
108 // Kill the third tab after making second tab active.
109 browser()->tab_strip_model()->ActivateTabAt(1, true);
110 EXPECT_EQ(1, browser()->tab_strip_model()->active_index());
111 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1));
112 browser()->tab_strip_model()->DiscardWebContentsAt(2);
113 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(2));
115 // Force creation of the FindBarController.
116 browser()->GetFindBarController();
118 // Select the first tab. It should reload.
119 WindowedNotificationObserver
reload1(
120 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
121 content::NotificationService::AllSources());
122 chrome::SelectNumberedTab(browser(), 0);
124 // Make sure the FindBarController gets the right WebContents.
125 EXPECT_EQ(browser()->GetFindBarController()->web_contents(),
126 browser()->tab_strip_model()->GetActiveWebContents());
127 EXPECT_EQ(0, browser()->tab_strip_model()->active_index());
128 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(0));
129 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1));
130 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(2));
132 // Select the third tab. It should reload.
133 WindowedNotificationObserver
reload2(
134 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
135 content::NotificationService::AllSources());
136 chrome::SelectNumberedTab(browser(), 2);
138 EXPECT_EQ(2, browser()->tab_strip_model()->active_index());
139 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(0));
140 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1));
141 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2));
143 // Navigate the third tab back twice. We used to crash here due to
145 EXPECT_TRUE(chrome::CanGoBack(browser()));
146 EXPECT_FALSE(chrome::CanGoForward(browser()));
147 WindowedNotificationObserver
back1(
148 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
149 content::NotificationService::AllSources());
150 chrome::GoBack(browser(), CURRENT_TAB
);
152 EXPECT_TRUE(chrome::CanGoBack(browser()));
153 EXPECT_TRUE(chrome::CanGoForward(browser()));
154 WindowedNotificationObserver
back2(
155 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
156 content::NotificationService::AllSources());
157 chrome::GoBack(browser(), CURRENT_TAB
);
159 EXPECT_FALSE(chrome::CanGoBack(browser()));
160 EXPECT_TRUE(chrome::CanGoForward(browser()));
163 // Test that the MemoryPressureListener event is properly triggering a tab
164 // discard upon |MEMORY_PRESSURE_LEVEL_CRITICAL| event.
165 IN_PROC_BROWSER_TEST_F(OomPriorityManagerTest
, OomPressureListener
) {
166 OomPriorityManager
* oom_priority_manager
=
167 g_browser_process
->GetOomPriorityManager();
168 ASSERT_TRUE(oom_priority_manager
);
170 // Get three tabs open.
171 content::WindowedNotificationObserver
load1(
172 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
173 content::NotificationService::AllSources());
174 OpenURLParams
open1(GURL(chrome::kChromeUIAboutURL
), content::Referrer(),
175 CURRENT_TAB
, ui::PAGE_TRANSITION_TYPED
, false);
176 browser()->OpenURL(open1
);
179 content::WindowedNotificationObserver
load2(
180 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
181 content::NotificationService::AllSources());
182 OpenURLParams
open2(GURL(chrome::kChromeUICreditsURL
), content::Referrer(),
183 NEW_FOREGROUND_TAB
, ui::PAGE_TRANSITION_TYPED
, false);
184 browser()->OpenURL(open2
);
186 EXPECT_FALSE(oom_priority_manager
->recent_tab_discard());
188 // Nothing should happen with a moderate memory pressure event.
189 base::MemoryPressureListener::NotifyMemoryPressure(
190 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE
);
191 EXPECT_FALSE(oom_priority_manager
->recent_tab_discard());
193 // A critical memory pressure event should discard a tab.
194 base::MemoryPressureListener::NotifyMemoryPressure(
195 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL
);
196 // Coming here, an asynchronous operation will collect system stats. Once in,
197 // a tab should get discarded. As such we need to give it 10s time to discard.
198 const int kTimeoutTimeInMS
= 10000;
199 const int kIntervalTimeInMS
= 5;
200 int timeout
= kTimeoutTimeInMS
/ kIntervalTimeInMS
;
202 base::PlatformThread::Sleep(
203 base::TimeDelta::FromMilliseconds(kIntervalTimeInMS
));
204 base::RunLoop().RunUntilIdle();
205 if (oom_priority_manager
->recent_tab_discard())
208 EXPECT_TRUE(oom_priority_manager
->recent_tab_discard());
212 } // namespace memory
214 #endif // defined(OS_CHROMEOS)