| 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 // TODO(georgesak): Retrieve this value from tab_manager.h once it becomes a |
| 298 // constant (as of now, it gets set through variations). |
| 299 const int kProtectionTime = 5; |
| 295 TabManager* tab_manager = g_browser_process->GetTabManager(); | 300 TabManager* tab_manager = g_browser_process->GetTabManager(); |
| 296 ASSERT_TRUE(tab_manager); | 301 ASSERT_TRUE(tab_manager); |
| 297 | 302 |
| 298 base::SimpleTestTickClock test_clock_; | 303 base::SimpleTestTickClock test_clock_; |
| 299 tab_manager->set_test_tick_clock(&test_clock_); | 304 tab_manager->set_test_tick_clock(&test_clock_); |
| 300 | 305 |
| 306 auto tsm = browser()->tab_strip_model(); |
| 307 |
| 301 // Set the minimum time of protection. | 308 // Set the minimum time of protection. |
| 302 tab_manager->minimum_protection_time_ = | 309 tab_manager->minimum_protection_time_ = |
| 303 base::TimeDelta::FromMinutes(protection_time); | 310 base::TimeDelta::FromMinutes(kProtectionTime); |
| 304 | 311 |
| 305 // Open 2 tabs, the second one being in the background. | 312 // Open 2 tabs, the second one being in the background. |
| 306 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)); | 313 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)); |
| 307 ui_test_utils::NavigateToURLWithDisposition( | 314 ui_test_utils::NavigateToURLWithDisposition( |
| 308 browser(), GURL(chrome::kChromeUIAboutURL), NEW_BACKGROUND_TAB, | 315 browser(), GURL(chrome::kChromeUIAboutURL), NEW_BACKGROUND_TAB, |
| 309 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 316 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 310 | 317 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 | 318 |
| 316 // Advance the clock for less than the protection time. | 319 // Advance the clock for less than the protection time. |
| 317 test_clock_.Advance(base::TimeDelta::FromMinutes(protection_time / 2)); | 320 test_clock_.Advance(base::TimeDelta::FromMinutes(kProtectionTime / 2)); |
| 318 | 321 |
| 319 // Should not be able to discard a tab. | 322 // Should not be able to discard a tab. |
| 320 ASSERT_FALSE(tab_manager->DiscardTab()); | 323 ASSERT_FALSE(tab_manager->DiscardTab()); |
| 321 | 324 |
| 322 // Advance the clock for more than the protection time. | 325 // Advance the clock for more than the protection time. |
| 323 test_clock_.Advance(base::TimeDelta::FromMinutes(protection_time / 2 + 2)); | 326 test_clock_.Advance(base::TimeDelta::FromMinutes(kProtectionTime / 2 + 2)); |
| 324 | 327 |
| 325 // Should be able to discard the background tab now. | 328 // Should be able to discard the background tab now. |
| 326 EXPECT_TRUE(tab_manager->DiscardTab()); | 329 EXPECT_TRUE(tab_manager->DiscardTab()); |
| 330 |
| 331 // Activate the 2nd tab. |
| 332 tsm->ActivateTabAt(1, true); |
| 333 EXPECT_EQ(1, tsm->active_index()); |
| 334 |
| 335 // Advance the clock for less than the protection time. |
| 336 test_clock_.Advance(base::TimeDelta::FromMinutes(kProtectionTime / 2)); |
| 337 |
| 338 // Should not be able to discard a tab. |
| 339 ASSERT_FALSE(tab_manager->DiscardTab()); |
| 340 |
| 341 // Advance the clock for more than the protection time. |
| 342 test_clock_.Advance(base::TimeDelta::FromMinutes(kProtectionTime / 2 + 2)); |
| 343 |
| 344 // Should be able to discard the background tab now. |
| 345 EXPECT_TRUE(tab_manager->DiscardTab()); |
| 346 |
| 347 // This is necessary otherwise the test crashes in |
| 348 // WebContentsData::WebContentsDestroyed. |
| 349 tsm->CloseAllTabs(); |
| 327 } | 350 } |
| 328 | 351 |
| 329 // Makes sure that tabs using media devices are protected. | 352 // Makes sure that tabs using media devices are protected. |
| 330 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectVideoTabs) { | 353 IN_PROC_BROWSER_TEST_F(TabManagerTest, ProtectVideoTabs) { |
| 331 TabManager* tab_manager = g_browser_process->GetTabManager(); | 354 TabManager* tab_manager = g_browser_process->GetTabManager(); |
| 332 ASSERT_TRUE(tab_manager); | 355 ASSERT_TRUE(tab_manager); |
| 333 | 356 |
| 357 // Disable the protection of recent tabs. |
| 358 tab_manager->minimum_protection_time_ = base::TimeDelta::FromMinutes(0); |
| 359 |
| 334 // Open 2 tabs, the second one being in the background. | 360 // Open 2 tabs, the second one being in the background. |
| 335 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)); | 361 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUIAboutURL)); |
| 336 ui_test_utils::NavigateToURLWithDisposition( | 362 ui_test_utils::NavigateToURLWithDisposition( |
| 337 browser(), GURL(chrome::kChromeUIAboutURL), NEW_BACKGROUND_TAB, | 363 browser(), GURL(chrome::kChromeUIAboutURL), NEW_BACKGROUND_TAB, |
| 338 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | 364 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); |
| 339 | 365 |
| 340 auto tab = browser()->tab_strip_model()->GetWebContentsAt(1); | 366 auto tab = browser()->tab_strip_model()->GetWebContentsAt(1); |
| 341 | 367 |
| 342 // Simulate that a video stream is now being captured. | 368 // Simulate that a video stream is now being captured. |
| 343 content::MediaStreamDevice fake_media_device( | 369 content::MediaStreamDevice fake_media_device( |
| (...skipping 14 matching lines...) Expand all Loading... |
| 358 // Remove the video stream. | 384 // Remove the video stream. |
| 359 video_stream_ui.reset(); | 385 video_stream_ui.reset(); |
| 360 | 386 |
| 361 // Should be able to discard the background tab now. | 387 // Should be able to discard the background tab now. |
| 362 EXPECT_TRUE(tab_manager->DiscardTab()); | 388 EXPECT_TRUE(tab_manager->DiscardTab()); |
| 363 } | 389 } |
| 364 | 390 |
| 365 } // namespace memory | 391 } // namespace memory |
| 366 | 392 |
| 367 #endif // OS_WIN || OS_CHROMEOS | 393 #endif // OS_WIN || OS_CHROMEOS |
| OLD | NEW |