Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(428)

Side by Side Diff: chrome/browser/memory/tab_manager_browsertest.cc

Issue 2245203002: Clean up defensive TabManager handling (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: nit Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/base_switches.h" 5 #include "base/base_switches.h"
6 #include "base/command_line.h" 6 #include "base/command_line.h"
7 #include "base/memory/memory_pressure_listener.h" 7 #include "base/memory/memory_pressure_listener.h"
8 #include "base/test/simple_test_tick_clock.h" 8 #include "base/test/simple_test_tick_clock.h"
9 #include "build/build_config.h" 9 #include "build/build_config.h"
10 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
(...skipping 30 matching lines...) Expand all
41 #if !defined(OS_LINUX) 41 #if !defined(OS_LINUX)
42 command_line->AppendSwitchASCII(switches::kEnableFeatures, 42 command_line->AppendSwitchASCII(switches::kEnableFeatures,
43 features::kAutomaticTabDiscarding.name); 43 features::kAutomaticTabDiscarding.name);
44 #endif 44 #endif
45 } 45 }
46 }; 46 };
47 47
48 IN_PROC_BROWSER_TEST_F(TabManagerTest, TabManagerBasics) { 48 IN_PROC_BROWSER_TEST_F(TabManagerTest, TabManagerBasics) {
49 using content::WindowedNotificationObserver; 49 using content::WindowedNotificationObserver;
50 TabManager* tab_manager = g_browser_process->GetTabManager(); 50 TabManager* tab_manager = g_browser_process->GetTabManager();
51 ASSERT_TRUE(tab_manager);
52 EXPECT_FALSE(tab_manager->recent_tab_discard()); 51 EXPECT_FALSE(tab_manager->recent_tab_discard());
53 52
54 // Disable the protection of recent tabs. 53 // Disable the protection of recent tabs.
55 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0); 54 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0);
56 55
57 // Get three tabs open. 56 // Get three tabs open.
58 WindowedNotificationObserver load1( 57 WindowedNotificationObserver load1(
59 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 58 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
60 content::NotificationService::AllSources()); 59 content::NotificationService::AllSources());
61 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(), 60 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(),
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 EXPECT_TRUE(chrome::CanGoForward(browser())); 180 EXPECT_TRUE(chrome::CanGoForward(browser()));
182 } 181 }
183 182
184 // On Linux, memory pressure listener is not implemented yet. 183 // On Linux, memory pressure listener is not implemented yet.
185 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) 184 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS)
186 185
187 // Test that the MemoryPressureListener event is properly triggering a tab 186 // Test that the MemoryPressureListener event is properly triggering a tab
188 // discard upon |MEMORY_PRESSURE_LEVEL_CRITICAL| event. 187 // discard upon |MEMORY_PRESSURE_LEVEL_CRITICAL| event.
189 IN_PROC_BROWSER_TEST_F(TabManagerTest, OomPressureListener) { 188 IN_PROC_BROWSER_TEST_F(TabManagerTest, OomPressureListener) {
190 TabManager* tab_manager = g_browser_process->GetTabManager(); 189 TabManager* tab_manager = g_browser_process->GetTabManager();
191 ASSERT_TRUE(tab_manager);
192 190
193 // Disable the protection of recent tabs. 191 // Disable the protection of recent tabs.
194 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0); 192 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0);
195 193
196 // Get three tabs open. 194 // Get three tabs open.
197 content::WindowedNotificationObserver load1( 195 content::WindowedNotificationObserver load1(
198 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 196 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
199 content::NotificationService::AllSources()); 197 content::NotificationService::AllSources());
200 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(), 198 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(),
201 CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false); 199 CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false);
(...skipping 29 matching lines...) Expand all
231 if (tab_manager->recent_tab_discard()) 229 if (tab_manager->recent_tab_discard())
232 break; 230 break;
233 } 231 }
234 EXPECT_TRUE(tab_manager->recent_tab_discard()); 232 EXPECT_TRUE(tab_manager->recent_tab_discard());
235 } 233 }
236 234
237 #endif 235 #endif
238 236
239 IN_PROC_BROWSER_TEST_F(TabManagerTest, InvalidOrEmptyURL) { 237 IN_PROC_BROWSER_TEST_F(TabManagerTest, InvalidOrEmptyURL) {
240 TabManager* tab_manager = g_browser_process->GetTabManager(); 238 TabManager* tab_manager = g_browser_process->GetTabManager();
241 ASSERT_TRUE(tab_manager);
242 239
243 // Disable the protection of recent tabs. 240 // Disable the protection of recent tabs.
244 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0); 241 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0);
245 242
246 // Open two tabs. Wait for the foreground one to load but do not wait for the 243 // Open two tabs. Wait for the foreground one to load but do not wait for the
247 // background one. 244 // background one.
248 content::WindowedNotificationObserver load1( 245 content::WindowedNotificationObserver load1(
249 content::NOTIFICATION_NAV_ENTRY_COMMITTED, 246 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
250 content::NotificationService::AllSources()); 247 content::NotificationService::AllSources());
251 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(), 248 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(),
(...skipping 15 matching lines...) Expand all
267 EXPECT_FALSE(tab_manager->DiscardTabImpl()); 264 EXPECT_FALSE(tab_manager->DiscardTabImpl());
268 265
269 // Wait for the background tab to load which then allows it to be discarded. 266 // Wait for the background tab to load which then allows it to be discarded.
270 load2.Wait(); 267 load2.Wait();
271 EXPECT_TRUE(tab_manager->DiscardTabImpl()); 268 EXPECT_TRUE(tab_manager->DiscardTabImpl());
272 } 269 }
273 270
274 // Makes sure that PDF pages are protected. 271 // Makes sure that PDF pages are protected.
275 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectPDFPages) { 272 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectPDFPages) {
276 TabManager* tab_manager = g_browser_process->GetTabManager(); 273 TabManager* tab_manager = g_browser_process->GetTabManager();
277 ASSERT_TRUE(tab_manager);
278 274
279 // Start the embedded test server so we can get served the required PDF page. 275 // Start the embedded test server so we can get served the required PDF page.
280 ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); 276 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
281 embedded_test_server()->StartAcceptingConnections(); 277 embedded_test_server()->StartAcceptingConnections();
282 278
283 // Get two tabs open, the first one being a PDF page and the second one being 279 // Get two tabs open, the first one being a PDF page and the second one being
284 // the foreground tab. 280 // the foreground tab.
285 GURL url1 = embedded_test_server()->GetURL("/pdf/test.pdf"); 281 GURL url1 = embedded_test_server()->GetURL("/pdf/test.pdf");
286 ui_test_utils::NavigateToURL(browser(), url1); 282 ui_test_utils::NavigateToURL(browser(), url1);
287 283
288 GURL url2(chrome::kChromeUIAboutURL); 284 GURL url2(chrome::kChromeUIAboutURL);
289 ui_test_utils::NavigateToURLWithDisposition( 285 ui_test_utils::NavigateToURLWithDisposition(
290 browser(), url2, NEW_FOREGROUND_TAB, 286 browser(), url2, NEW_FOREGROUND_TAB,
291 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 287 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
292 288
293 // No discarding should be possible as the only background tab is displaying a 289 // No discarding should be possible as the only background tab is displaying a
294 // PDF page, hence protected. 290 // PDF page, hence protected.
295 EXPECT_FALSE(tab_manager->DiscardTabImpl()); 291 EXPECT_FALSE(tab_manager->DiscardTabImpl());
296 } 292 }
297 293
298 // Makes sure that recently opened or used tabs are protected, depending on the 294 // Makes sure that recently opened or used tabs are protected, depending on the
299 // value of of |minimum_protection_time_|. 295 // value of of |minimum_protection_time_|.
300 // TODO(georgesak): Move this to a unit test instead (requires change to API). 296 // TODO(georgesak): Move this to a unit test instead (requires change to API).
301 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectRecentlyUsedTabs) { 297 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectRecentlyUsedTabs) {
302 // TODO(georgesak): Retrieve this value from tab_manager.h once it becomes a 298 // TODO(georgesak): Retrieve this value from tab_manager.h once it becomes a
303 // constant (as of now, it gets set through variations). 299 // constant (as of now, it gets set through variations).
304 const int kProtectionTime = 5; 300 const int kProtectionTime = 5;
305 TabManager* tab_manager = g_browser_process->GetTabManager(); 301 TabManager* tab_manager = g_browser_process->GetTabManager();
306 ASSERT_TRUE(tab_manager);
307 302
308 base::SimpleTestTickClock test_clock_; 303 base::SimpleTestTickClock test_clock_;
309 tab_manager->set_test_tick_clock(&test_clock_); 304 tab_manager->set_test_tick_clock(&test_clock_);
310 305
311 auto* tsm = browser()->tab_strip_model(); 306 auto* tsm = browser()->tab_strip_model();
312 307
313 // Set the minimum time of protection. 308 // Set the minimum time of protection.
314 tab_manager->minimum_protection_time_ = 309 tab_manager->minimum_protection_time_ =
315 base::TimeDelta::FromMinutes(kProtectionTime); 310 base::TimeDelta::FromMinutes(kProtectionTime);
316 311
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 EXPECT_TRUE(tab_manager->DiscardTabImpl()); 345 EXPECT_TRUE(tab_manager->DiscardTabImpl());
351 346
352 // This is necessary otherwise the test crashes in 347 // This is necessary otherwise the test crashes in
353 // WebContentsData::WebContentsDestroyed. 348 // WebContentsData::WebContentsDestroyed.
354 tsm->CloseAllTabs(); 349 tsm->CloseAllTabs();
355 } 350 }
356 351
357 // Makes sure that tabs using media devices are protected. 352 // Makes sure that tabs using media devices are protected.
358 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectVideoTabs) { 353 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectVideoTabs) {
359 TabManager* tab_manager = g_browser_process->GetTabManager(); 354 TabManager* tab_manager = g_browser_process->GetTabManager();
360 ASSERT_TRUE(tab_manager);
361 355
362 // Disable the protection of recent tabs. 356 // Disable the protection of recent tabs.
363 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0); 357 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0);
364 358
365 // Open 2 tabs, the second one being in the background. 359 // Open 2 tabs, the second one being in the background.
366 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)); 360 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL));
367 ui_test_utils::NavigateToURLWithDisposition( 361 ui_test_utils::NavigateToURLWithDisposition(
368 browser(), GURL(chrome::kChromeUIAboutURL), NEW_BACKGROUND_TAB, 362 browser(), GURL(chrome::kChromeUIAboutURL), NEW_BACKGROUND_TAB,
369 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); 363 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
370 364
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
429 tab_manager->SetTabAutoDiscardableState(tsm->GetWebContentsAt(0), true); 423 tab_manager->SetTabAutoDiscardableState(tsm->GetWebContentsAt(0), true);
430 424
431 // Now it should be able to discard the tab. 425 // Now it should be able to discard the tab.
432 EXPECT_TRUE(tab_manager->DiscardTabImpl()); 426 EXPECT_TRUE(tab_manager->DiscardTabImpl());
433 EXPECT_TRUE(tab_manager->IsTabDiscarded(tsm->GetWebContentsAt(0))); 427 EXPECT_TRUE(tab_manager->IsTabDiscarded(tsm->GetWebContentsAt(0)));
434 } 428 }
435 429
436 } // namespace memory 430 } // namespace memory
437 431
438 #endif // OS_WIN || OS_MAXOSX || OS_LINUX 432 #endif // OS_WIN || OS_MAXOSX || OS_LINUX
OLDNEW
« no previous file with comments | « chrome/browser/memory/tab_manager.cc ('k') | chrome/browser/memory/tab_manager_observer_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698