| 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 <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 WebContents* new_contents_; | 80 WebContents* new_contents_; |
| 81 | 81 |
| 82 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver); | 82 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver); |
| 83 }; | 83 }; |
| 84 | 84 |
| 85 // A mock task runner. This isn't directly a TaskRunner as the reference | 85 // A mock task runner. This isn't directly a TaskRunner as the reference |
| 86 // counting confuses gmock. | 86 // counting confuses gmock. |
| 87 class LenientMockTaskRunner { | 87 class LenientMockTaskRunner { |
| 88 public: | 88 public: |
| 89 LenientMockTaskRunner() {} | 89 LenientMockTaskRunner() {} |
| 90 MOCK_METHOD3(PostDelayedTask, | 90 MOCK_METHOD1(PostDelayedTask, bool(base::TimeDelta)); |
| 91 bool(const tracked_objects::Location&, | 91 |
| 92 const base::Closure&, | |
| 93 base::TimeDelta)); | |
| 94 private: | 92 private: |
| 95 DISALLOW_COPY_AND_ASSIGN(LenientMockTaskRunner); | 93 DISALLOW_COPY_AND_ASSIGN(LenientMockTaskRunner); |
| 96 }; | 94 }; |
| 97 using MockTaskRunner = testing::StrictMock<LenientMockTaskRunner>; | 95 using MockTaskRunner = testing::StrictMock<LenientMockTaskRunner>; |
| 98 | 96 |
| 99 // Represents a pending task. | 97 // Represents a pending task. |
| 100 using Task = std::pair<base::TimeTicks, base::Closure>; | 98 using Task = std::pair<base::TimeTicks, base::OnceClosure>; |
| 101 | 99 |
| 102 // Comparator used for sorting Task objects. Can't use std::pair's default | 100 // Comparator used for sorting Task objects. Can't use std::pair's default |
| 103 // comparison operators because Closure's are comparable. | 101 // comparison operators because Closure's are comparable. |
| 104 struct TaskComparator { | 102 struct TaskComparator { |
| 105 bool operator()(const Task& t1, const Task& t2) const { | 103 bool operator()(const Task& t1, const Task& t2) const { |
| 106 // Reverse the sort order so this can be used with a min-heap. | 104 // Reverse the sort order so this can be used with a min-heap. |
| 107 return t1.first > t2.first; | 105 return t1.first > t2.first; |
| 108 } | 106 } |
| 109 }; | 107 }; |
| 110 | 108 |
| 111 // A TaskRunner implementation that delegates to a MockTaskRunner for asserting | 109 // A TaskRunner implementation that delegates to a MockTaskRunner for asserting |
| 112 // on task insertion order, and which stores tasks for actual later execution. | 110 // on task insertion order, and which stores tasks for actual later execution. |
| 113 class TaskRunnerProxy : public base::TaskRunner { | 111 class TaskRunnerProxy : public base::TaskRunner { |
| 114 public: | 112 public: |
| 115 TaskRunnerProxy(MockTaskRunner* mock, | 113 TaskRunnerProxy(MockTaskRunner* mock, |
| 116 base::SimpleTestTickClock* clock) | 114 base::SimpleTestTickClock* clock) |
| 117 : mock_(mock), clock_(clock) {} | 115 : mock_(mock), clock_(clock) {} |
| 118 bool RunsTasksOnCurrentThread() const override { return true; } | 116 bool RunsTasksOnCurrentThread() const override { return true; } |
| 119 bool PostDelayedTask(const tracked_objects::Location& location, | 117 bool PostDelayedTask(const tracked_objects::Location& location, |
| 120 const base::Closure& closure, | 118 base::OnceClosure closure, |
| 121 base::TimeDelta delta) override { | 119 base::TimeDelta delta) override { |
| 122 mock_->PostDelayedTask(location, closure, delta); | 120 mock_->PostDelayedTask(delta); |
| 123 base::TimeTicks when = clock_->NowTicks() + delta; | 121 base::TimeTicks when = clock_->NowTicks() + delta; |
| 124 tasks_.push_back(Task(when, closure)); | 122 tasks_.push_back(Task(when, std::move(closure))); |
| 125 // Use 'greater' comparator to make this a min heap. | 123 // Use 'greater' comparator to make this a min heap. |
| 126 std::push_heap(tasks_.begin(), tasks_.end(), TaskComparator()); | 124 std::push_heap(tasks_.begin(), tasks_.end(), TaskComparator()); |
| 127 return true; | 125 return true; |
| 128 } | 126 } |
| 129 | 127 |
| 130 // Runs tasks up to the current time. Returns the number of tasks executed. | 128 // Runs tasks up to the current time. Returns the number of tasks executed. |
| 131 size_t RunTasks() { | 129 size_t RunTasks() { |
| 132 base::TimeTicks now = clock_->NowTicks(); | 130 base::TimeTicks now = clock_->NowTicks(); |
| 133 size_t count = 0; | 131 size_t count = 0; |
| 134 while (!tasks_.empty() && tasks_.front().first <= now) { | 132 while (!tasks_.empty() && tasks_.front().first <= now) { |
| 135 tasks_.front().second.Run(); | 133 std::move(tasks_.front().second).Run(); |
| 136 std::pop_heap(tasks_.begin(), tasks_.end(), TaskComparator()); | 134 std::pop_heap(tasks_.begin(), tasks_.end(), TaskComparator()); |
| 137 tasks_.pop_back(); | 135 tasks_.pop_back(); |
| 138 ++count; | 136 ++count; |
| 139 } | 137 } |
| 140 return count; | 138 return count; |
| 141 } | 139 } |
| 142 | 140 |
| 143 // Advances the clock and runs the next task in the queue. Can run more than | 141 // Advances the clock and runs the next task in the queue. Can run more than |
| 144 // one task if multiple tasks have the same scheduled time. | 142 // one task if multiple tasks have the same scheduled time. |
| 145 size_t RunNextTask() { | 143 size_t RunNextTask() { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 159 | 157 |
| 160 size_t size() const { return tasks_.size(); } | 158 size_t size() const { return tasks_.size(); } |
| 161 | 159 |
| 162 private: | 160 private: |
| 163 ~TaskRunnerProxy() override {} | 161 ~TaskRunnerProxy() override {} |
| 164 | 162 |
| 165 MockTaskRunner* mock_; | 163 MockTaskRunner* mock_; |
| 166 base::SimpleTestTickClock* clock_; | 164 base::SimpleTestTickClock* clock_; |
| 167 | 165 |
| 168 // A min-heap of outstanding tasks. | 166 // A min-heap of outstanding tasks. |
| 169 using Task = std::pair<base::TimeTicks, base::Closure>; | 167 using Task = std::pair<base::TimeTicks, base::OnceClosure>; |
| 170 std::vector<Task> tasks_; | 168 std::vector<Task> tasks_; |
| 171 | 169 |
| 172 DISALLOW_COPY_AND_ASSIGN(TaskRunnerProxy); | 170 DISALLOW_COPY_AND_ASSIGN(TaskRunnerProxy); |
| 173 }; | 171 }; |
| 174 | 172 |
| 175 enum TestIndicies { | 173 enum TestIndicies { |
| 176 kSelected, | 174 kSelected, |
| 177 kAutoDiscardable, | 175 kAutoDiscardable, |
| 178 kPinned, | 176 kPinned, |
| 179 kApp, | 177 kApp, |
| (...skipping 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 EXPECT_EQ(0u, task_runner->size()); | 578 EXPECT_EQ(0u, task_runner->size()); |
| 581 EXPECT_EQ(0u, tm.notified_renderers_.size()); | 579 EXPECT_EQ(0u, tm.notified_renderers_.size()); |
| 582 | 580 |
| 583 // START OF MEMORY PRESSURE | 581 // START OF MEMORY PRESSURE |
| 584 | 582 |
| 585 // Simulate a memory pressure situation that persists. This should cause a | 583 // Simulate a memory pressure situation that persists. This should cause a |
| 586 // task to be scheduled, and a background renderer to be notified. | 584 // task to be scheduled, and a background renderer to be notified. |
| 587 tm.get_current_pressure_level_ = base::Bind( | 585 tm.get_current_pressure_level_ = base::Bind( |
| 588 &ReturnSpecifiedPressure, base::Unretained(&level)); | 586 &ReturnSpecifiedPressure, base::Unretained(&level)); |
| 589 EXPECT_CALL(mock_task_runner, PostDelayedTask( | 587 EXPECT_CALL(mock_task_runner, PostDelayedTask( |
| 590 testing::_, | |
| 591 testing::_, | |
| 592 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); | 588 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); |
| 593 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level)); | 589 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level)); |
| 594 tm.OnMemoryPressure(level); | 590 tm.OnMemoryPressure(level); |
| 595 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); | 591 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); |
| 596 testing::Mock::VerifyAndClearExpectations(this); | 592 testing::Mock::VerifyAndClearExpectations(this); |
| 597 EXPECT_TRUE(tm.under_memory_pressure_); | 593 EXPECT_TRUE(tm.under_memory_pressure_); |
| 598 EXPECT_EQ(1u, task_runner->size()); | 594 EXPECT_EQ(1u, task_runner->size()); |
| 599 EXPECT_EQ(1u, tm.notified_renderers_.size()); | 595 EXPECT_EQ(1u, tm.notified_renderers_.size()); |
| 600 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer2)); | 596 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer2)); |
| 601 | 597 |
| 602 // REPEATED MEMORY PRESSURE SIGNAL | 598 // REPEATED MEMORY PRESSURE SIGNAL |
| 603 | 599 |
| 604 // Simulate another memory pressure event. This should not cause any | 600 // Simulate another memory pressure event. This should not cause any |
| 605 // additional tasks to be scheduled, nor any further renderer notifications. | 601 // additional tasks to be scheduled, nor any further renderer notifications. |
| 606 tm.OnMemoryPressure(level); | 602 tm.OnMemoryPressure(level); |
| 607 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); | 603 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); |
| 608 testing::Mock::VerifyAndClearExpectations(this); | 604 testing::Mock::VerifyAndClearExpectations(this); |
| 609 EXPECT_TRUE(tm.under_memory_pressure_); | 605 EXPECT_TRUE(tm.under_memory_pressure_); |
| 610 EXPECT_EQ(1u, task_runner->size()); | 606 EXPECT_EQ(1u, task_runner->size()); |
| 611 EXPECT_EQ(1u, tm.notified_renderers_.size()); | 607 EXPECT_EQ(1u, tm.notified_renderers_.size()); |
| 612 | 608 |
| 613 // FIRST SCHEDULED NOTIFICATION | 609 // FIRST SCHEDULED NOTIFICATION |
| 614 | 610 |
| 615 // Run the scheduled task. This should cause another notification, but this | 611 // Run the scheduled task. This should cause another notification, but this |
| 616 // time to the foreground tab. It should also cause another scheduled event. | 612 // time to the foreground tab. It should also cause another scheduled event. |
| 617 EXPECT_CALL(mock_task_runner, PostDelayedTask( | 613 EXPECT_CALL(mock_task_runner, PostDelayedTask( |
| 618 testing::_, | |
| 619 testing::_, | |
| 620 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); | 614 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); |
| 621 EXPECT_CALL(*this, NotifyRendererProcess(renderer1, level)); | 615 EXPECT_CALL(*this, NotifyRendererProcess(renderer1, level)); |
| 622 EXPECT_EQ(1u, task_runner->RunNextTask()); | 616 EXPECT_EQ(1u, task_runner->RunNextTask()); |
| 623 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); | 617 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); |
| 624 testing::Mock::VerifyAndClearExpectations(this); | 618 testing::Mock::VerifyAndClearExpectations(this); |
| 625 EXPECT_TRUE(tm.under_memory_pressure_); | 619 EXPECT_TRUE(tm.under_memory_pressure_); |
| 626 EXPECT_EQ(1u, task_runner->size()); | 620 EXPECT_EQ(1u, task_runner->size()); |
| 627 EXPECT_EQ(2u, tm.notified_renderers_.size()); | 621 EXPECT_EQ(2u, tm.notified_renderers_.size()); |
| 628 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer1)); | 622 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer1)); |
| 629 | 623 |
| 630 // SECOND SCHEDULED NOTIFICATION | 624 // SECOND SCHEDULED NOTIFICATION |
| 631 | 625 |
| 632 // Run the scheduled task. This should cause another notification, to the | 626 // Run the scheduled task. This should cause another notification, to the |
| 633 // background tab. It should also cause another scheduled event. | 627 // background tab. It should also cause another scheduled event. |
| 634 EXPECT_CALL(mock_task_runner, PostDelayedTask( | 628 EXPECT_CALL(mock_task_runner, PostDelayedTask( |
| 635 testing::_, | |
| 636 testing::_, | |
| 637 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); | 629 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); |
| 638 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level)); | 630 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level)); |
| 639 EXPECT_EQ(1u, task_runner->RunNextTask()); | 631 EXPECT_EQ(1u, task_runner->RunNextTask()); |
| 640 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); | 632 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); |
| 641 testing::Mock::VerifyAndClearExpectations(this); | 633 testing::Mock::VerifyAndClearExpectations(this); |
| 642 EXPECT_TRUE(tm.under_memory_pressure_); | 634 EXPECT_TRUE(tm.under_memory_pressure_); |
| 643 EXPECT_EQ(1u, task_runner->size()); | 635 EXPECT_EQ(1u, task_runner->size()); |
| 644 EXPECT_EQ(1u, tm.notified_renderers_.size()); | 636 EXPECT_EQ(1u, tm.notified_renderers_.size()); |
| 645 | 637 |
| 646 // Expect that the background tab has been notified. The list of notified | 638 // Expect that the background tab has been notified. The list of notified |
| (...skipping 14 matching lines...) Expand all Loading... |
| 661 EXPECT_EQ(0u, task_runner->size()); | 653 EXPECT_EQ(0u, task_runner->size()); |
| 662 EXPECT_EQ(0u, tm.notified_renderers_.size()); | 654 EXPECT_EQ(0u, tm.notified_renderers_.size()); |
| 663 | 655 |
| 664 | 656 |
| 665 // Clean up the tabstrip. | 657 // Clean up the tabstrip. |
| 666 tabstrip.CloseAllTabs(); | 658 tabstrip.CloseAllTabs(); |
| 667 ASSERT_TRUE(tabstrip.empty()); | 659 ASSERT_TRUE(tabstrip.empty()); |
| 668 } | 660 } |
| 669 | 661 |
| 670 } // namespace memory | 662 } // namespace memory |
| OLD | NEW |