OLD | NEW |
| (Empty) |
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 "base/command_line.h" | |
6 #include "base/memory/memory_pressure_listener.h" | |
7 #include "chrome/browser/browser_process.h" | |
8 #include "chrome/browser/browser_process_platform_part_chromeos.h" | |
9 #include "chrome/browser/chromeos/memory/oom_priority_manager.h" | |
10 #include "chrome/browser/ui/browser.h" | |
11 #include "chrome/browser/ui/browser_commands.h" | |
12 #include "chrome/browser/ui/find_bar/find_bar_controller.h" | |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
14 #include "chrome/common/url_constants.h" | |
15 #include "chrome/test/base/in_process_browser_test.h" | |
16 #include "chromeos/chromeos_switches.h" | |
17 #include "content/public/browser/notification_service.h" | |
18 #include "content/public/browser/notification_types.h" | |
19 #include "content/public/test/test_utils.h" | |
20 #include "url/gurl.h" | |
21 | |
22 using content::OpenURLParams; | |
23 | |
24 namespace { | |
25 | |
26 typedef InProcessBrowserTest OomPriorityManagerTest; | |
27 | |
28 IN_PROC_BROWSER_TEST_F(OomPriorityManagerTest, OomPriorityManagerBasics) { | |
29 using content::WindowedNotificationObserver; | |
30 | |
31 chromeos::OomPriorityManager* oom_priority_manager = | |
32 g_browser_process->platform_part()->oom_priority_manager(); | |
33 EXPECT_FALSE(oom_priority_manager->recent_tab_discard()); | |
34 | |
35 // Get three tabs open. | |
36 WindowedNotificationObserver load1( | |
37 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
38 content::NotificationService::AllSources()); | |
39 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(), | |
40 CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false); | |
41 browser()->OpenURL(open1); | |
42 load1.Wait(); | |
43 | |
44 WindowedNotificationObserver load2( | |
45 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
46 content::NotificationService::AllSources()); | |
47 OpenURLParams open2(GURL(chrome::kChromeUICreditsURL), content::Referrer(), | |
48 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_TYPED, | |
49 false); | |
50 browser()->OpenURL(open2); | |
51 load2.Wait(); | |
52 | |
53 WindowedNotificationObserver load3( | |
54 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
55 content::NotificationService::AllSources()); | |
56 OpenURLParams open3(GURL(chrome::kChromeUITermsURL), content::Referrer(), | |
57 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_TYPED, | |
58 false); | |
59 browser()->OpenURL(open3); | |
60 load3.Wait(); | |
61 | |
62 EXPECT_EQ(3, browser()->tab_strip_model()->count()); | |
63 | |
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, | |
71 false); | |
72 browser()->OpenURL(open4); | |
73 load4.Wait(); | |
74 | |
75 // Navigate the third tab again, such that we have three navigation entries. | |
76 WindowedNotificationObserver load5( | |
77 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
78 content::NotificationService::AllSources()); | |
79 OpenURLParams open5(GURL("chrome://dns"), content::Referrer(), | |
80 CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, | |
81 false); | |
82 browser()->OpenURL(open5); | |
83 load5.Wait(); | |
84 | |
85 EXPECT_EQ(3, browser()->tab_strip_model()->count()); | |
86 | |
87 // Discard a tab. It should kill the first tab, since it was the oldest | |
88 // and was not selected. | |
89 EXPECT_TRUE(oom_priority_manager->DiscardTab()); | |
90 EXPECT_EQ(3, browser()->tab_strip_model()->count()); | |
91 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0)); | |
92 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1)); | |
93 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2)); | |
94 EXPECT_TRUE(oom_priority_manager->recent_tab_discard()); | |
95 | |
96 // Run discard again, make sure it kills the second tab. | |
97 EXPECT_TRUE(oom_priority_manager->DiscardTab()); | |
98 EXPECT_EQ(3, browser()->tab_strip_model()->count()); | |
99 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0)); | |
100 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(1)); | |
101 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2)); | |
102 | |
103 // Kill the third tab. It should not kill the last tab, since it is active | |
104 // tab. | |
105 EXPECT_FALSE(oom_priority_manager->DiscardTab()); | |
106 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(0)); | |
107 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(1)); | |
108 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2)); | |
109 | |
110 // Kill the third tab after making second tab active. | |
111 browser()->tab_strip_model()->ActivateTabAt(1, true); | |
112 EXPECT_EQ(1, browser()->tab_strip_model()->active_index()); | |
113 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1)); | |
114 browser()->tab_strip_model()->DiscardWebContentsAt(2); | |
115 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(2)); | |
116 | |
117 // Force creation of the FindBarController. | |
118 browser()->GetFindBarController(); | |
119 | |
120 // Select the first tab. It should reload. | |
121 WindowedNotificationObserver reload1( | |
122 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
123 content::NotificationService::AllSources()); | |
124 chrome::SelectNumberedTab(browser(), 0); | |
125 reload1.Wait(); | |
126 // Make sure the FindBarController gets the right WebContents. | |
127 EXPECT_EQ(browser()->GetFindBarController()->web_contents(), | |
128 browser()->tab_strip_model()->GetActiveWebContents()); | |
129 EXPECT_EQ(0, browser()->tab_strip_model()->active_index()); | |
130 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(0)); | |
131 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1)); | |
132 EXPECT_TRUE(browser()->tab_strip_model()->IsTabDiscarded(2)); | |
133 | |
134 // Select the third tab. It should reload. | |
135 WindowedNotificationObserver reload2( | |
136 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
137 content::NotificationService::AllSources()); | |
138 chrome::SelectNumberedTab(browser(), 2); | |
139 reload2.Wait(); | |
140 EXPECT_EQ(2, browser()->tab_strip_model()->active_index()); | |
141 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(0)); | |
142 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(1)); | |
143 EXPECT_FALSE(browser()->tab_strip_model()->IsTabDiscarded(2)); | |
144 | |
145 // Navigate the third tab back twice. We used to crash here due to | |
146 // crbug.com/121373. | |
147 EXPECT_TRUE(chrome::CanGoBack(browser())); | |
148 EXPECT_FALSE(chrome::CanGoForward(browser())); | |
149 WindowedNotificationObserver back1( | |
150 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
151 content::NotificationService::AllSources()); | |
152 chrome::GoBack(browser(), CURRENT_TAB); | |
153 back1.Wait(); | |
154 EXPECT_TRUE(chrome::CanGoBack(browser())); | |
155 EXPECT_TRUE(chrome::CanGoForward(browser())); | |
156 WindowedNotificationObserver back2( | |
157 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
158 content::NotificationService::AllSources()); | |
159 chrome::GoBack(browser(), CURRENT_TAB); | |
160 back2.Wait(); | |
161 EXPECT_FALSE(chrome::CanGoBack(browser())); | |
162 EXPECT_TRUE(chrome::CanGoForward(browser())); | |
163 } | |
164 | |
165 // Test that the MemoryPressureListener event is properly triggering a tab | |
166 // discard upon |MEMORY_PRESSURE_LEVEL_CRITICAL| event. | |
167 IN_PROC_BROWSER_TEST_F(OomPriorityManagerTest, OomPressureListener) { | |
168 chromeos::OomPriorityManager* oom_priority_manager = | |
169 g_browser_process->platform_part()->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); | |
177 load1.Wait(); | |
178 | |
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, | |
184 false); | |
185 browser()->OpenURL(open2); | |
186 load2.Wait(); | |
187 EXPECT_FALSE(oom_priority_manager->recent_tab_discard()); | |
188 | |
189 // Nothing should happen with a moderate memory pressure event. | |
190 base::MemoryPressureListener::NotifyMemoryPressure( | |
191 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); | |
192 EXPECT_FALSE(oom_priority_manager->recent_tab_discard()); | |
193 | |
194 // A critical memory pressure event should discard a tab. | |
195 base::MemoryPressureListener::NotifyMemoryPressure( | |
196 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); | |
197 // Coming here, an asynchronous operation will collect system stats. Once in, | |
198 // a tab should get discarded. As such we need to give it 10s time to discard. | |
199 const int kTimeoutTimeInMS = 10000; | |
200 const int kIntervalTimeInMS = 5; | |
201 int timeout = kTimeoutTimeInMS / kIntervalTimeInMS; | |
202 while (--timeout) { | |
203 usleep(kIntervalTimeInMS * 1000); | |
204 base::RunLoop().RunUntilIdle(); | |
205 if (oom_priority_manager->recent_tab_discard()) | |
206 break; | |
207 } | |
208 EXPECT_TRUE(oom_priority_manager->recent_tab_discard()); | |
209 } | |
210 | |
211 } // namespace | |
OLD | NEW |