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 "base/command_line.h" |
| 6 #include "base/memory/memory_pressure_listener.h" |
5 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
6 #include "chrome/browser/browser_process_platform_part_chromeos.h" | 8 #include "chrome/browser/browser_process_platform_part_chromeos.h" |
7 #include "chrome/browser/chromeos/memory/oom_priority_manager.h" | 9 #include "chrome/browser/chromeos/memory/oom_priority_manager.h" |
8 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
9 #include "chrome/browser/ui/browser_commands.h" | 11 #include "chrome/browser/ui/browser_commands.h" |
10 #include "chrome/browser/ui/find_bar/find_bar_controller.h" | 12 #include "chrome/browser/ui/find_bar/find_bar_controller.h" |
11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
12 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 15 #include "chrome/test/base/in_process_browser_test.h" |
| 16 #include "chromeos/chromeos_switches.h" |
14 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
15 #include "content/public/browser/notification_types.h" | 18 #include "content/public/browser/notification_types.h" |
16 #include "content/public/test/test_utils.h" | 19 #include "content/public/test/test_utils.h" |
17 #include "url/gurl.h" | 20 #include "url/gurl.h" |
18 | 21 |
19 using content::OpenURLParams; | 22 using content::OpenURLParams; |
20 | 23 |
21 namespace { | 24 namespace { |
22 | 25 |
23 typedef InProcessBrowserTest OomPriorityManagerTest; | 26 typedef InProcessBrowserTest OomPriorityManagerTest; |
24 | 27 |
| 28 class OomPriorityManagerUsingPressureListenerTest |
| 29 : public InProcessBrowserTest { |
| 30 public: |
| 31 OomPriorityManagerUsingPressureListenerTest() {} |
| 32 ~OomPriorityManagerUsingPressureListenerTest() override {} |
| 33 |
| 34 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 35 command_line->AppendSwitch( |
| 36 chromeos::switches::kUseMemoryPressureSystemChromeOS); |
| 37 } |
| 38 |
| 39 private: |
| 40 DISALLOW_COPY_AND_ASSIGN(OomPriorityManagerUsingPressureListenerTest); |
| 41 }; |
| 42 |
| 43 |
25 IN_PROC_BROWSER_TEST_F(OomPriorityManagerTest, OomPriorityManagerBasics) { | 44 IN_PROC_BROWSER_TEST_F(OomPriorityManagerTest, OomPriorityManagerBasics) { |
26 using content::WindowedNotificationObserver; | 45 using content::WindowedNotificationObserver; |
27 | 46 |
28 chromeos::OomPriorityManager* oom_priority_manager = | 47 chromeos::OomPriorityManager* oom_priority_manager = |
29 g_browser_process->platform_part()->oom_priority_manager(); | 48 g_browser_process->platform_part()->oom_priority_manager(); |
30 EXPECT_FALSE(oom_priority_manager->recent_tab_discard()); | 49 EXPECT_FALSE(oom_priority_manager->recent_tab_discard()); |
31 | 50 |
32 // Get three tabs open. | 51 // Get three tabs open. |
33 WindowedNotificationObserver load1( | 52 WindowedNotificationObserver load1( |
34 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 53 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 EXPECT_TRUE(chrome::CanGoForward(browser())); | 171 EXPECT_TRUE(chrome::CanGoForward(browser())); |
153 WindowedNotificationObserver back2( | 172 WindowedNotificationObserver back2( |
154 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 173 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
155 content::NotificationService::AllSources()); | 174 content::NotificationService::AllSources()); |
156 chrome::GoBack(browser(), CURRENT_TAB); | 175 chrome::GoBack(browser(), CURRENT_TAB); |
157 back2.Wait(); | 176 back2.Wait(); |
158 EXPECT_FALSE(chrome::CanGoBack(browser())); | 177 EXPECT_FALSE(chrome::CanGoBack(browser())); |
159 EXPECT_TRUE(chrome::CanGoForward(browser())); | 178 EXPECT_TRUE(chrome::CanGoForward(browser())); |
160 } | 179 } |
161 | 180 |
| 181 // Test that the MemoryPressureListener event is properly triggering a tab |
| 182 // discard upon |MEMORY_PRESSURE_LEVEL_CRITICAL| event. |
| 183 IN_PROC_BROWSER_TEST_F(OomPriorityManagerUsingPressureListenerTest, |
| 184 OomPressureListener) { |
| 185 chromeos::OomPriorityManager* oom_priority_manager = |
| 186 g_browser_process->platform_part()->oom_priority_manager(); |
| 187 // Get three tabs open. |
| 188 content::WindowedNotificationObserver load1( |
| 189 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 190 content::NotificationService::AllSources()); |
| 191 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(), |
| 192 CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false); |
| 193 browser()->OpenURL(open1); |
| 194 load1.Wait(); |
| 195 |
| 196 content::WindowedNotificationObserver load2( |
| 197 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 198 content::NotificationService::AllSources()); |
| 199 OpenURLParams open2(GURL(chrome::kChromeUICreditsURL), content::Referrer(), |
| 200 NEW_FOREGROUND_TAB, ui::PAGE_TRANSITION_TYPED, |
| 201 false); |
| 202 browser()->OpenURL(open2); |
| 203 load2.Wait(); |
| 204 EXPECT_FALSE(oom_priority_manager->recent_tab_discard()); |
| 205 |
| 206 // Nothing should happen with a moderate memory pressure event. |
| 207 base::MemoryPressureListener::NotifyMemoryPressure( |
| 208 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE); |
| 209 EXPECT_FALSE(oom_priority_manager->recent_tab_discard()); |
| 210 |
| 211 // A critical memory pressure event should discard a tab. |
| 212 base::MemoryPressureListener::NotifyMemoryPressure( |
| 213 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL); |
| 214 // Coming here, an asynchronous operation will collect system stats. Once in, |
| 215 // a tab should get discarded. As such we need to give it 10s time to discard. |
| 216 const int kTimeoutTimeInMS = 10000; |
| 217 const int kIntervalTimeInMS = 5; |
| 218 int timeout = kTimeoutTimeInMS / kIntervalTimeInMS; |
| 219 while (--timeout) { |
| 220 usleep(kIntervalTimeInMS * 1000); |
| 221 base::RunLoop().RunUntilIdle(); |
| 222 if (oom_priority_manager->recent_tab_discard()) |
| 223 break; |
| 224 } |
| 225 EXPECT_TRUE(oom_priority_manager->recent_tab_discard()); |
| 226 } |
| 227 |
162 } // namespace | 228 } // namespace |
OLD | NEW |