| 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 "chrome/browser/memory/tab_manager.h" | 5 #include "chrome/browser/memory/tab_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 // Configure the TabManager for testing. | 468 // Configure the TabManager for testing. |
| 469 tabstrip.AddObserver(&tm); | 469 tabstrip.AddObserver(&tm); |
| 470 tm.test_tab_strip_models_.push_back( | 470 tm.test_tab_strip_models_.push_back( |
| 471 TabManager::TestTabStripModel(&tabstrip, false /* !is_app */)); | 471 TabManager::TestTabStripModel(&tabstrip, false /* !is_app */)); |
| 472 tm.test_tick_clock_ = &test_clock; | 472 tm.test_tick_clock_ = &test_clock; |
| 473 tm.task_runner_ = task_runner; | 473 tm.task_runner_ = task_runner; |
| 474 tm.notify_renderer_process_ = base::Bind( | 474 tm.notify_renderer_process_ = base::Bind( |
| 475 &TabManagerTest::NotifyRendererProcess, base::Unretained(this)); | 475 &TabManagerTest::NotifyRendererProcess, base::Unretained(this)); |
| 476 | 476 |
| 477 // Create two dummy tabs. | 477 // Create two dummy tabs. |
| 478 auto tab0 = CreateWebContents(); |
| 478 auto tab1 = CreateWebContents(); | 479 auto tab1 = CreateWebContents(); |
| 479 auto tab2 = CreateWebContents(); | 480 auto tab2 = CreateWebContents(); |
| 480 tabstrip.AppendWebContents(tab1, true); // Foreground tab. | 481 tabstrip.AppendWebContents(tab0, true); // Foreground tab. |
| 481 tabstrip.AppendWebContents(tab2, false); // Opened in background. | 482 tabstrip.AppendWebContents(tab1, false); // Background tab. |
| 483 tabstrip.AppendWebContents(tab2, false); // Background tab. |
| 482 const content::RenderProcessHost* renderer1 = tab1->GetRenderProcessHost(); | 484 const content::RenderProcessHost* renderer1 = tab1->GetRenderProcessHost(); |
| 483 const content::RenderProcessHost* renderer2 = tab2->GetRenderProcessHost(); | 485 const content::RenderProcessHost* renderer2 = tab2->GetRenderProcessHost(); |
| 484 | 486 |
| 487 // Make sure that tab2 has a lower priority than tab1 by its access time. |
| 488 test_clock.Advance(base::TimeDelta::FromMilliseconds(1)); |
| 489 tab2->SetLastActiveTime(test_clock.NowTicks()); |
| 490 test_clock.Advance(base::TimeDelta::FromMilliseconds(1)); |
| 491 tab1->SetLastActiveTime(test_clock.NowTicks()); |
| 492 |
| 485 // Expect that the tab manager has not yet encountered memory pressure. | 493 // Expect that the tab manager has not yet encountered memory pressure. |
| 486 EXPECT_FALSE(tm.under_memory_pressure_); | 494 EXPECT_FALSE(tm.under_memory_pressure_); |
| 487 EXPECT_EQ(0u, tm.notified_renderers_.size()); | 495 EXPECT_EQ(0u, tm.notified_renderers_.size()); |
| 488 | 496 |
| 489 // Simulate a memory pressure situation that will immediately end. This should | 497 // Simulate a memory pressure situation that will immediately end. This should |
| 490 // cause no notifications or scheduled tasks. | 498 // cause no notifications or scheduled tasks. |
| 491 auto level = base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; | 499 auto level = base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; |
| 492 tm.get_current_pressure_level_ = base::Bind(&ReturnNoPressure); | 500 tm.get_current_pressure_level_ = base::Bind(&ReturnNoPressure); |
| 493 tm.OnMemoryPressure(level); | 501 tm.OnMemoryPressure(level); |
| 494 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); | 502 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); |
| 495 EXPECT_FALSE(tm.under_memory_pressure_); | 503 EXPECT_FALSE(tm.under_memory_pressure_); |
| 496 EXPECT_EQ(0u, task_runner->size()); | 504 EXPECT_EQ(0u, task_runner->size()); |
| 497 EXPECT_EQ(0u, tm.notified_renderers_.size()); | 505 EXPECT_EQ(0u, tm.notified_renderers_.size()); |
| 498 | 506 |
| 499 // START OF MEMORY PRESSURE | 507 // START OF MEMORY PRESSURE |
| 500 | 508 |
| 501 // Simulate a memory pressure situation that persists. This should cause a | 509 // Simulate a memory pressure situation that persists. This should cause a |
| 502 // task to be scheduled. | 510 // task to be scheduled, and a background renderer to be notified. |
| 503 tm.get_current_pressure_level_ = base::Bind( | 511 tm.get_current_pressure_level_ = base::Bind( |
| 504 &ReturnSpecifiedPressure, base::Unretained(&level)); | 512 &ReturnSpecifiedPressure, base::Unretained(&level)); |
| 505 EXPECT_CALL(mock_task_runner, PostDelayedTask( | 513 EXPECT_CALL(mock_task_runner, PostDelayedTask( |
| 506 testing::_, | 514 testing::_, |
| 507 testing::_, | 515 testing::_, |
| 508 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); | 516 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); |
| 509 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level)); | 517 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level)); |
| 510 tm.OnMemoryPressure(level); | 518 tm.OnMemoryPressure(level); |
| 511 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); | 519 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); |
| 512 testing::Mock::VerifyAndClearExpectations(this); | 520 testing::Mock::VerifyAndClearExpectations(this); |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 577 EXPECT_EQ(0u, task_runner->size()); | 585 EXPECT_EQ(0u, task_runner->size()); |
| 578 EXPECT_EQ(0u, tm.notified_renderers_.size()); | 586 EXPECT_EQ(0u, tm.notified_renderers_.size()); |
| 579 | 587 |
| 580 | 588 |
| 581 // Clean up the tabstrip. | 589 // Clean up the tabstrip. |
| 582 tabstrip.CloseAllTabs(); | 590 tabstrip.CloseAllTabs(); |
| 583 ASSERT_TRUE(tabstrip.empty()); | 591 ASSERT_TRUE(tabstrip.empty()); |
| 584 } | 592 } |
| 585 | 593 |
| 586 } // namespace memory | 594 } // namespace memory |
| OLD | NEW |