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

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

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: rebase Created 3 years, 9 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 "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
11 #include "base/callback_helpers.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/macros.h" 13 #include "base/macros.h"
13 #include "base/memory/ptr_util.h" 14 #include "base/memory/ptr_util.h"
14 #include "base/metrics/field_trial.h" 15 #include "base/metrics/field_trial.h"
15 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
16 #include "base/test/mock_entropy_provider.h" 17 #include "base/test/mock_entropy_provider.h"
17 #include "base/test/simple_test_tick_clock.h" 18 #include "base/test/simple_test_tick_clock.h"
18 #include "base/time/time.h" 19 #include "base/time/time.h"
19 #include "build/build_config.h" 20 #include "build/build_config.h"
20 #include "chrome/browser/memory/tab_manager_web_contents_data.h" 21 #include "chrome/browser/memory/tab_manager_web_contents_data.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 WebContents* new_contents_; 83 WebContents* new_contents_;
83 84
84 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver); 85 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver);
85 }; 86 };
86 87
87 // A mock task runner. This isn't directly a TaskRunner as the reference 88 // A mock task runner. This isn't directly a TaskRunner as the reference
88 // counting confuses gmock. 89 // counting confuses gmock.
89 class LenientMockTaskRunner { 90 class LenientMockTaskRunner {
90 public: 91 public:
91 LenientMockTaskRunner() {} 92 LenientMockTaskRunner() {}
92 MOCK_METHOD3(PostDelayedTask, 93 MOCK_METHOD2(PostDelayedTask,
93 bool(const tracked_objects::Location&, 94 bool(const tracked_objects::Location&, base::TimeDelta));
94 const base::Closure&, 95
95 base::TimeDelta));
96 private: 96 private:
97 DISALLOW_COPY_AND_ASSIGN(LenientMockTaskRunner); 97 DISALLOW_COPY_AND_ASSIGN(LenientMockTaskRunner);
98 }; 98 };
99 using MockTaskRunner = testing::StrictMock<LenientMockTaskRunner>; 99 using MockTaskRunner = testing::StrictMock<LenientMockTaskRunner>;
100 100
101 // Represents a pending task. 101 // Represents a pending task.
102 using Task = std::pair<base::TimeTicks, base::Closure>; 102 using Task = std::pair<base::TimeTicks, base::Closure>;
103 103
104 // Comparator used for sorting Task objects. Can't use std::pair's default 104 // Comparator used for sorting Task objects. Can't use std::pair's default
105 // comparison operators because Closure's are comparable. 105 // comparison operators because Closure's are comparable.
106 struct TaskComparator { 106 struct TaskComparator {
107 bool operator()(const Task& t1, const Task& t2) const { 107 bool operator()(const Task& t1, const Task& t2) const {
108 // Reverse the sort order so this can be used with a min-heap. 108 // Reverse the sort order so this can be used with a min-heap.
109 return t1.first > t2.first; 109 return t1.first > t2.first;
110 } 110 }
111 }; 111 };
112 112
113 // A TaskRunner implementation that delegates to a MockTaskRunner for asserting 113 // A TaskRunner implementation that delegates to a MockTaskRunner for asserting
114 // on task insertion order, and which stores tasks for actual later execution. 114 // on task insertion order, and which stores tasks for actual later execution.
115 class TaskRunnerProxy : public base::TaskRunner { 115 class TaskRunnerProxy : public base::TaskRunner {
116 public: 116 public:
117 TaskRunnerProxy(MockTaskRunner* mock, 117 TaskRunnerProxy(MockTaskRunner* mock,
118 base::SimpleTestTickClock* clock) 118 base::SimpleTestTickClock* clock)
119 : mock_(mock), clock_(clock) {} 119 : mock_(mock), clock_(clock) {}
120 bool RunsTasksOnCurrentThread() const override { return true; } 120 bool RunsTasksOnCurrentThread() const override { return true; }
121 bool PostDelayedTask(const tracked_objects::Location& location, 121 bool PostDelayedTask(const tracked_objects::Location& location,
122 const base::Closure& closure, 122 base::Closure closure,
123 base::TimeDelta delta) override { 123 base::TimeDelta delta) override {
124 mock_->PostDelayedTask(location, closure, delta); 124 mock_->PostDelayedTask(location, delta);
125 base::TimeTicks when = clock_->NowTicks() + delta; 125 base::TimeTicks when = clock_->NowTicks() + delta;
126 tasks_.push_back(Task(when, closure)); 126 tasks_.push_back(Task(when, std::move(closure)));
127 // Use 'greater' comparator to make this a min heap. 127 // Use 'greater' comparator to make this a min heap.
128 std::push_heap(tasks_.begin(), tasks_.end(), TaskComparator()); 128 std::push_heap(tasks_.begin(), tasks_.end(), TaskComparator());
129 return true; 129 return true;
130 } 130 }
131 131
132 // Runs tasks up to the current time. Returns the number of tasks executed. 132 // Runs tasks up to the current time. Returns the number of tasks executed.
133 size_t RunTasks() { 133 size_t RunTasks() {
134 base::TimeTicks now = clock_->NowTicks(); 134 base::TimeTicks now = clock_->NowTicks();
135 size_t count = 0; 135 size_t count = 0;
136 while (!tasks_.empty() && tasks_.front().first <= now) { 136 while (!tasks_.empty() && tasks_.front().first <= now) {
137 tasks_.front().second.Run(); 137 base::ResetAndReturn(&tasks_.front().second).Run();
138 std::pop_heap(tasks_.begin(), tasks_.end(), TaskComparator()); 138 std::pop_heap(tasks_.begin(), tasks_.end(), TaskComparator());
139 tasks_.pop_back(); 139 tasks_.pop_back();
140 ++count; 140 ++count;
141 } 141 }
142 return count; 142 return count;
143 } 143 }
144 144
145 // Advances the clock and runs the next task in the queue. Can run more than 145 // Advances the clock and runs the next task in the queue. Can run more than
146 // one task if multiple tasks have the same scheduled time. 146 // one task if multiple tasks have the same scheduled time.
147 size_t RunNextTask() { 147 size_t RunNextTask() {
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 EXPECT_EQ(0u, tm.notified_renderers_.size()); 586 EXPECT_EQ(0u, tm.notified_renderers_.size());
587 587
588 // START OF MEMORY PRESSURE 588 // START OF MEMORY PRESSURE
589 589
590 // Simulate a memory pressure situation that persists. This should cause a 590 // Simulate a memory pressure situation that persists. This should cause a
591 // task to be scheduled, and a background renderer to be notified. 591 // task to be scheduled, and a background renderer to be notified.
592 tm.get_current_pressure_level_ = base::Bind( 592 tm.get_current_pressure_level_ = base::Bind(
593 &ReturnSpecifiedPressure, base::Unretained(&level)); 593 &ReturnSpecifiedPressure, base::Unretained(&level));
594 EXPECT_CALL(mock_task_runner, PostDelayedTask( 594 EXPECT_CALL(mock_task_runner, PostDelayedTask(
595 testing::_, 595 testing::_,
596 testing::_,
597 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); 596 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds)));
598 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level)); 597 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level));
599 tm.OnMemoryPressure(level); 598 tm.OnMemoryPressure(level);
600 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); 599 testing::Mock::VerifyAndClearExpectations(&mock_task_runner);
601 testing::Mock::VerifyAndClearExpectations(this); 600 testing::Mock::VerifyAndClearExpectations(this);
602 EXPECT_TRUE(tm.under_memory_pressure_); 601 EXPECT_TRUE(tm.under_memory_pressure_);
603 EXPECT_EQ(1u, task_runner->size()); 602 EXPECT_EQ(1u, task_runner->size());
604 EXPECT_EQ(1u, tm.notified_renderers_.size()); 603 EXPECT_EQ(1u, tm.notified_renderers_.size());
605 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer2)); 604 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer2));
606 605
607 // REPEATED MEMORY PRESSURE SIGNAL 606 // REPEATED MEMORY PRESSURE SIGNAL
608 607
609 // Simulate another memory pressure event. This should not cause any 608 // Simulate another memory pressure event. This should not cause any
610 // additional tasks to be scheduled, nor any further renderer notifications. 609 // additional tasks to be scheduled, nor any further renderer notifications.
611 tm.OnMemoryPressure(level); 610 tm.OnMemoryPressure(level);
612 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); 611 testing::Mock::VerifyAndClearExpectations(&mock_task_runner);
613 testing::Mock::VerifyAndClearExpectations(this); 612 testing::Mock::VerifyAndClearExpectations(this);
614 EXPECT_TRUE(tm.under_memory_pressure_); 613 EXPECT_TRUE(tm.under_memory_pressure_);
615 EXPECT_EQ(1u, task_runner->size()); 614 EXPECT_EQ(1u, task_runner->size());
616 EXPECT_EQ(1u, tm.notified_renderers_.size()); 615 EXPECT_EQ(1u, tm.notified_renderers_.size());
617 616
618 // FIRST SCHEDULED NOTIFICATION 617 // FIRST SCHEDULED NOTIFICATION
619 618
620 // Run the scheduled task. This should cause another notification, but this 619 // Run the scheduled task. This should cause another notification, but this
621 // time to the foreground tab. It should also cause another scheduled event. 620 // time to the foreground tab. It should also cause another scheduled event.
622 EXPECT_CALL(mock_task_runner, PostDelayedTask( 621 EXPECT_CALL(mock_task_runner, PostDelayedTask(
623 testing::_, 622 testing::_,
624 testing::_,
625 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); 623 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds)));
626 EXPECT_CALL(*this, NotifyRendererProcess(renderer1, level)); 624 EXPECT_CALL(*this, NotifyRendererProcess(renderer1, level));
627 EXPECT_EQ(1u, task_runner->RunNextTask()); 625 EXPECT_EQ(1u, task_runner->RunNextTask());
628 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); 626 testing::Mock::VerifyAndClearExpectations(&mock_task_runner);
629 testing::Mock::VerifyAndClearExpectations(this); 627 testing::Mock::VerifyAndClearExpectations(this);
630 EXPECT_TRUE(tm.under_memory_pressure_); 628 EXPECT_TRUE(tm.under_memory_pressure_);
631 EXPECT_EQ(1u, task_runner->size()); 629 EXPECT_EQ(1u, task_runner->size());
632 EXPECT_EQ(2u, tm.notified_renderers_.size()); 630 EXPECT_EQ(2u, tm.notified_renderers_.size());
633 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer1)); 631 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer1));
634 632
635 // SECOND SCHEDULED NOTIFICATION 633 // SECOND SCHEDULED NOTIFICATION
636 634
637 // Run the scheduled task. This should cause another notification, to the 635 // Run the scheduled task. This should cause another notification, to the
638 // background tab. It should also cause another scheduled event. 636 // background tab. It should also cause another scheduled event.
639 EXPECT_CALL(mock_task_runner, PostDelayedTask( 637 EXPECT_CALL(mock_task_runner, PostDelayedTask(
640 testing::_, 638 testing::_,
641 testing::_,
642 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); 639 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds)));
643 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level)); 640 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level));
644 EXPECT_EQ(1u, task_runner->RunNextTask()); 641 EXPECT_EQ(1u, task_runner->RunNextTask());
645 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); 642 testing::Mock::VerifyAndClearExpectations(&mock_task_runner);
646 testing::Mock::VerifyAndClearExpectations(this); 643 testing::Mock::VerifyAndClearExpectations(this);
647 EXPECT_TRUE(tm.under_memory_pressure_); 644 EXPECT_TRUE(tm.under_memory_pressure_);
648 EXPECT_EQ(1u, task_runner->size()); 645 EXPECT_EQ(1u, task_runner->size());
649 EXPECT_EQ(1u, tm.notified_renderers_.size()); 646 EXPECT_EQ(1u, tm.notified_renderers_.size());
650 647
651 // Expect that the background tab has been notified. The list of notified 648 // Expect that the background tab has been notified. The list of notified
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
773 tabstrip.ActivateTabAt(0, true /* user_gesture */); 770 tabstrip.ActivateTabAt(0, true /* user_gesture */);
774 EXPECT_EQ(TabManager::RUNNING, 771 EXPECT_EQ(TabManager::RUNNING,
775 tab_manager.GetWebContentsData(tab1)->GetPurgeAndSuspendState()); 772 tab_manager.GetWebContentsData(tab1)->GetPurgeAndSuspendState());
776 773
777 // Clean up the tabstrip. 774 // Clean up the tabstrip.
778 tabstrip.CloseAllTabs(); 775 tabstrip.CloseAllTabs();
779 EXPECT_TRUE(tabstrip.empty()); 776 EXPECT_TRUE(tabstrip.empty());
780 } 777 }
781 778
782 } // namespace memory 779 } // namespace memory
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698