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

Side by Side Diff: components/memory_pressure/memory_pressure_monitor_unittest.cc

Issue 2726523002: Pass Callback to TaskRunner by value and consume it on invocation (1) (Closed)
Patch Set: erase Closure* Created 3 years, 8 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/memory_pressure/memory_pressure_monitor.h" 5 #include "components/memory_pressure/memory_pressure_monitor.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/task_runner.h" 11 #include "base/task_runner.h"
11 #include "base/test/simple_test_tick_clock.h" 12 #include "base/test/simple_test_tick_clock.h"
12 #include "base/tracked_objects.h" 13 #include "base/tracked_objects.h"
13 #include "components/memory_pressure/memory_pressure_stats_collector.h" 14 #include "components/memory_pressure/memory_pressure_stats_collector.h"
14 #include "components/memory_pressure/test_memory_pressure_calculator.h" 15 #include "components/memory_pressure/test_memory_pressure_calculator.h"
15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
(...skipping 10 matching lines...) Expand all
28 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; 29 MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL;
29 30
30 using testing::_; 31 using testing::_;
31 32
32 } // namespace 33 } // namespace
33 34
34 // A mock task runner. This isn't directly a TaskRunner as the reference 35 // A mock task runner. This isn't directly a TaskRunner as the reference
35 // counting confuses gmock. 36 // counting confuses gmock.
36 class LenientMockTaskRunner { 37 class LenientMockTaskRunner {
37 public: 38 public:
38 MOCK_METHOD3(PostDelayedTask, 39 MOCK_METHOD2(PostDelayedTask,
39 bool(const tracked_objects::Location&, 40 bool(const tracked_objects::Location&, base::TimeDelta));
40 const base::Closure&,
41 base::TimeDelta));
42 }; 41 };
43 using MockTaskRunner = testing::StrictMock<LenientMockTaskRunner>; 42 using MockTaskRunner = testing::StrictMock<LenientMockTaskRunner>;
44 43
45 // A TaskRunner implementation that wraps a MockTaskRunner. 44 // A TaskRunner implementation that wraps a MockTaskRunner.
46 class TaskRunnerProxy : public base::TaskRunner { 45 class TaskRunnerProxy : public base::TaskRunner {
47 public: 46 public:
48 // The provided |mock| must outlive this object. 47 // The provided |mock| must outlive this object.
49 explicit TaskRunnerProxy(MockTaskRunner* mock) : mock_(mock) {} 48 explicit TaskRunnerProxy(MockTaskRunner* mock) : mock_(mock) {}
50 bool RunsTasksOnCurrentThread() const override { return true; } 49 bool RunsTasksOnCurrentThread() const override { return true; }
51 bool PostDelayedTask(const tracked_objects::Location& location, 50 bool PostDelayedTask(const tracked_objects::Location& location,
52 const base::Closure& closure, 51 base::Closure closure,
53 base::TimeDelta delta) override { 52 base::TimeDelta delta) override {
54 return mock_->PostDelayedTask(location, closure, delta); 53 return mock_->PostDelayedTask(location, delta);
55 } 54 }
56 55
57 private: 56 private:
58 MockTaskRunner* mock_; 57 MockTaskRunner* mock_;
59 ~TaskRunnerProxy() override {} 58 ~TaskRunnerProxy() override {}
60 }; 59 };
61 60
62 class TestMemoryPressureMonitor : public MemoryPressureMonitor { 61 class TestMemoryPressureMonitor : public MemoryPressureMonitor {
63 public: 62 public:
64 // Expose the callback that is used for scheduled checks. 63 // Expose the callback that is used for scheduled checks.
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 #endif // !MEMORY_PRESSURE_IS_POLLING 161 #endif // !MEMORY_PRESSURE_IS_POLLING
163 162
164 // Advances the tick clock by the given number of milliseconds. 163 // Advances the tick clock by the given number of milliseconds.
165 void Tick(int ms) { 164 void Tick(int ms) {
166 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(ms)); 165 tick_clock_.Advance(base::TimeDelta::FromMilliseconds(ms));
167 } 166 }
168 167
169 // Sets expectations for tasks scheduled via |mock_task_runner_|. 168 // Sets expectations for tasks scheduled via |mock_task_runner_|.
170 void ExpectTaskPosted(int delay_ms) { 169 void ExpectTaskPosted(int delay_ms) {
171 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(delay_ms); 170 base::TimeDelta delay = base::TimeDelta::FromMilliseconds(delay_ms);
172 EXPECT_CALL(mock_task_runner_, PostDelayedTask(_, _, delay)) 171 EXPECT_CALL(mock_task_runner_, PostDelayedTask(_, delay))
173 .WillOnce(testing::Return(true)); 172 .WillOnce(testing::Return(true));
174 } 173 }
175 174
176 // Sets up expectations for calls to |mock_dispatch_|. 175 // Sets up expectations for calls to |mock_dispatch_|.
177 void ExpectDispatch(MemoryPressureLevel level) { 176 void ExpectDispatch(MemoryPressureLevel level) {
178 EXPECT_CALL(mock_dispatch_, Dispatch(level)); 177 EXPECT_CALL(mock_dispatch_, Dispatch(level));
179 } 178 }
180 void ExpectDispatchModerate() { 179 void ExpectDispatchModerate() {
181 EXPECT_CALL(mock_dispatch_, Dispatch(MEMORY_PRESSURE_LEVEL_MODERATE)); 180 EXPECT_CALL(mock_dispatch_, Dispatch(MEMORY_PRESSURE_LEVEL_MODERATE));
182 } 181 }
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 ExpectDispatchCritical(); 380 ExpectDispatchCritical();
382 monitor_->OnMemoryPressureChanged(MEMORY_PRESSURE_LEVEL_CRITICAL); 381 monitor_->OnMemoryPressureChanged(MEMORY_PRESSURE_LEVEL_CRITICAL);
383 VerifyAndClearExpectations(); 382 VerifyAndClearExpectations();
384 ExpectCritical(); 383 ExpectCritical();
385 EXPECT_EQ(2u, monitor_->scheduled_checks().size()); 384 EXPECT_EQ(2u, monitor_->scheduled_checks().size());
386 } 385 }
387 386
388 #endif // !MEMORY_PRESSURE_IS_POLLING 387 #endif // !MEMORY_PRESSURE_IS_POLLING
389 388
390 } // namespace memory_pressure 389 } // namespace memory_pressure
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698