Chromium Code Reviews| 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/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 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 228 if (tab_manager->recent_tab_discard()) | 228 if (tab_manager->recent_tab_discard()) |
| 229 break; | 229 break; |
| 230 } | 230 } |
| 231 EXPECT_TRUE(tab_manager->recent_tab_discard()); | 231 EXPECT_TRUE(tab_manager->recent_tab_discard()); |
| 232 } | 232 } |
| 233 | 233 |
| 234 IN_PROC_BROWSER_TEST_F(TabManagerTest, InvalidOrEmptyURL) { | 234 IN_PROC_BROWSER_TEST_F(TabManagerTest, InvalidOrEmptyURL) { |
| 235 TabManager* tab_manager = g_browser_process->GetTabManager(); | 235 TabManager* tab_manager = g_browser_process->GetTabManager(); |
| 236 ASSERT_TRUE(tab_manager); | 236 ASSERT_TRUE(tab_manager); |
| 237 | 237 |
| 238 // Disable the protection of recent tabs. | |
| 239 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0); | |
| 240 | |
| 238 // Open two tabs. Wait for the foreground one to load but do not wait for the | 241 // Open two tabs. Wait for the foreground one to load but do not wait for the |
| 239 // background one. | 242 // background one. |
| 240 content::WindowedNotificationObserver load1( | 243 content::WindowedNotificationObserver load1( |
| 241 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 244 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 242 content::NotificationService::AllSources()); | 245 content::NotificationService::AllSources()); |
| 243 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(), | 246 OpenURLParams open1(GURL(chrome::kChromeUIAboutURL), content::Referrer(), |
| 244 CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false); | 247 CURRENT_TAB, ui::PAGE_TRANSITION_TYPED, false); |
| 245 browser()->OpenURL(open1); | 248 browser()->OpenURL(open1); |
| 246 load1.Wait(); | 249 load1.Wait(); |
| 247 | 250 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 280 GURL url2(chrome::kChromeUIAboutURL); | 283 GURL url2(chrome::kChromeUIAboutURL); |
| 281 ui_test_utils::NavigateToURLWithDisposition( | 284 ui_test_utils::NavigateToURLWithDisposition( |
| 282 browser(), url2, NEW_FOREGROUND_TAB, | 285 browser(), url2, NEW_FOREGROUND_TAB, |
| 283 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 286 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 284 | 287 |
| 285 // No discarding should be possible as the only background tab is displaying a | 288 // No discarding should be possible as the only background tab is displaying a |
| 286 // PDF page, hence protected. | 289 // PDF page, hence protected. |
| 287 EXPECT_FALSE(tab_manager->DiscardTab()); | 290 EXPECT_FALSE(tab_manager->DiscardTab()); |
| 288 } | 291 } |
| 289 | 292 |
| 290 // Makes sure that recently used tabs are protected, depending on the value of | 293 // Makes sure that recently opened or used tabs are protected, depending on the |
| 291 // of |minimum_protection_time_|. | 294 // value of of |minimum_protection_time_|. |
| 292 // TODO(georgesak): Move this to a unit test instead (requires change to API). | 295 // TODO(georgesak): Move this to a unit test instead (requires change to API). |
| 293 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectRecentlyUsedTabs) { | 296 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectRecentlyUsedTabs) { |
| 294 const int protection_time = 5; | 297 const int kProtectionTime = 5; |
|
chrisha
2016/03/29 12:48:40
Maybe expose this constant from the tab_manager.h,
Georges Khalil
2016/03/29 14:09:54
There's no constant in tab_manager.h as this value
| |
| 295 TabManager* tab_manager = g_browser_process->GetTabManager(); | 298 TabManager* tab_manager = g_browser_process->GetTabManager(); |
| 296 ASSERT_TRUE(tab_manager); | 299 ASSERT_TRUE(tab_manager); |
| 297 | 300 |
| 298 base::SimpleTestTickClock test_clock_; | 301 base::SimpleTestTickClock test_clock_; |
| 299 tab_manager->set_test_tick_clock(&test_clock_); | 302 tab_manager->set_test_tick_clock(&test_clock_); |
| 300 | 303 |
| 304 auto tsm = browser()->tab_strip_model(); | |
| 305 | |
| 301 // Set the minimum time of protection. | 306 // Set the minimum time of protection. |
| 302 tab_manager->minimum_protection_time_ = | 307 tab_manager->minimum_protection_time_ = |
| 303 base::TimeDelta::FromMinutes(protection_time); | 308 base::TimeDelta::FromMinutes(kProtectionTime); |
| 304 | 309 |
| 305 // Open 2 tabs, the second one being in the background. | 310 // Open 2 tabs, the second one being in the background. |
| 306 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)); | 311 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)); |
| 307 ui_test_utils::NavigateToURLWithDisposition( | 312 ui_test_utils::NavigateToURLWithDisposition( |
| 308 browser(), GURL(chrome::kChromeUIAboutURL), NEW_BACKGROUND_TAB, | 313 browser(), GURL(chrome::kChromeUIAboutURL), NEW_BACKGROUND_TAB, |
| 309 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 314 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 310 | 315 EXPECT_EQ(2, tsm->count()); |
| 311 // Set the last inactive time of the background tab. | |
| 312 auto tab = browser()->tab_strip_model()->GetWebContentsAt(1); | |
| 313 tab_manager->GetWebContentsData(tab) | |
| 314 ->SetLastInactiveTime(test_clock_.NowTicks()); | |
| 315 | 316 |
| 316 // Advance the clock for less than the protection time. | 317 // Advance the clock for less than the protection time. |
| 317 test_clock_.Advance(base::TimeDelta::FromMinutes(protection_time / 2)); | 318 test_clock_.Advance(base::TimeDelta::FromMinutes(kProtectionTime / 2)); |
| 318 | 319 |
| 319 // Should not be able to discard a tab. | 320 // Should not be able to discard a tab. |
| 320 ASSERT_FALSE(tab_manager->DiscardTab()); | 321 ASSERT_FALSE(tab_manager->DiscardTab()); |
| 321 | 322 |
| 322 // Advance the clock for more than the protection time. | 323 // Advance the clock for more than the protection time. |
| 323 test_clock_.Advance(base::TimeDelta::FromMinutes(protection_time / 2 + 2)); | 324 test_clock_.Advance(base::TimeDelta::FromMinutes(kProtectionTime / 2 + 2)); |
| 324 | 325 |
| 325 // Should be able to discard the background tab now. | 326 // Should be able to discard the background tab now. |
| 326 EXPECT_TRUE(tab_manager->DiscardTab()); | 327 EXPECT_TRUE(tab_manager->DiscardTab()); |
| 328 | |
| 329 // Activate the 2nd tab. | |
| 330 tsm->ActivateTabAt(1, true); | |
| 331 EXPECT_EQ(1, tsm->active_index()); | |
| 332 | |
| 333 // Advance the clock for less than the protection time. | |
| 334 test_clock_.Advance(base::TimeDelta::FromMinutes(kProtectionTime / 2)); | |
| 335 | |
| 336 // Should not be able to discard a tab. | |
| 337 ASSERT_FALSE(tab_manager->DiscardTab()); | |
| 338 | |
| 339 // Advance the clock for more than the protection time. | |
| 340 test_clock_.Advance(base::TimeDelta::FromMinutes(kProtectionTime / 2 + 2)); | |
| 341 | |
| 342 // Should be able to discard the background tab now. | |
| 343 EXPECT_TRUE(tab_manager->DiscardTab()); | |
| 344 | |
| 345 // This is necessary otherwise the test crashes in | |
| 346 // WebContentsData::WebContentsDestroyed. | |
| 347 tsm->CloseAllTabs(); | |
| 327 } | 348 } |
| 328 | 349 |
| 329 // Makes sure that tabs using media devices are protected. | 350 // Makes sure that tabs using media devices are protected. |
| 330 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectVideoTabs) { | 351 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectVideoTabs) { |
| 331 TabManager* tab_manager = g_browser_process->GetTabManager(); | 352 TabManager* tab_manager = g_browser_process->GetTabManager(); |
| 332 ASSERT_TRUE(tab_manager); | 353 ASSERT_TRUE(tab_manager); |
| 333 | 354 |
| 355 // Disable the protection of recent tabs. | |
| 356 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0); | |
| 357 | |
| 334 // Open 2 tabs, the second one being in the background. | 358 // Open 2 tabs, the second one being in the background. |
| 335 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)); | 359 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)); |
| 336 ui_test_utils::NavigateToURLWithDisposition( | 360 ui_test_utils::NavigateToURLWithDisposition( |
| 337 browser(), GURL(chrome::kChromeUIAboutURL), NEW_BACKGROUND_TAB, | 361 browser(), GURL(chrome::kChromeUIAboutURL), NEW_BACKGROUND_TAB, |
| 338 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 362 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 339 | 363 |
| 340 auto tab = browser()->tab_strip_model()->GetWebContentsAt(1); | 364 auto tab = browser()->tab_strip_model()->GetWebContentsAt(1); |
| 341 | 365 |
| 342 // Simulate that a video stream is now being captured. | 366 // Simulate that a video stream is now being captured. |
| 343 content::MediaStreamDevice fake_media_device( | 367 content::MediaStreamDevice fake_media_device( |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 358 // Remove the video stream. | 382 // Remove the video stream. |
| 359 video_stream_ui.reset(); | 383 video_stream_ui.reset(); |
| 360 | 384 |
| 361 // Should be able to discard the background tab now. | 385 // Should be able to discard the background tab now. |
| 362 EXPECT_TRUE(tab_manager->DiscardTab()); | 386 EXPECT_TRUE(tab_manager->DiscardTab()); |
| 363 } | 387 } |
| 364 | 388 |
| 365 } // namespace memory | 389 } // namespace memory |
| 366 | 390 |
| 367 #endif // OS_WIN || OS_CHROMEOS | 391 #endif // OS_WIN || OS_CHROMEOS |
| OLD | NEW |