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

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: s/base::ResetAndReturn/std::move/ 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 WebContents* new_contents_; 85 WebContents* new_contents_;
85 86
86 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver); 87 DISALLOW_COPY_AND_ASSIGN(MockTabStripModelObserver);
87 }; 88 };
88 89
89 // A mock task runner. This isn't directly a TaskRunner as the reference 90 // A mock task runner. This isn't directly a TaskRunner as the reference
90 // counting confuses gmock. 91 // counting confuses gmock.
91 class LenientMockTaskRunner { 92 class LenientMockTaskRunner {
92 public: 93 public:
93 LenientMockTaskRunner() {} 94 LenientMockTaskRunner() {}
94 MOCK_METHOD3(PostDelayedTask, 95 MOCK_METHOD2(PostDelayedTask,
95 bool(const tracked_objects::Location&, 96 bool(const tracked_objects::Location&, base::TimeDelta));
96 const base::Closure&, 97
97 base::TimeDelta));
98 private: 98 private:
99 DISALLOW_COPY_AND_ASSIGN(LenientMockTaskRunner); 99 DISALLOW_COPY_AND_ASSIGN(LenientMockTaskRunner);
100 }; 100 };
101 using MockTaskRunner = testing::StrictMock<LenientMockTaskRunner>; 101 using MockTaskRunner = testing::StrictMock<LenientMockTaskRunner>;
102 102
103 // Represents a pending task. 103 // Represents a pending task.
104 using Task = std::pair<base::TimeTicks, base::Closure>; 104 using Task = std::pair<base::TimeTicks, base::Closure>;
105 105
106 // Comparator used for sorting Task objects. Can't use std::pair's default 106 // Comparator used for sorting Task objects. Can't use std::pair's default
107 // comparison operators because Closure's are comparable. 107 // comparison operators because Closure's are comparable.
108 struct TaskComparator { 108 struct TaskComparator {
109 bool operator()(const Task& t1, const Task& t2) const { 109 bool operator()(const Task& t1, const Task& t2) const {
110 // Reverse the sort order so this can be used with a min-heap. 110 // Reverse the sort order so this can be used with a min-heap.
111 return t1.first > t2.first; 111 return t1.first > t2.first;
112 } 112 }
113 }; 113 };
114 114
115 // A TaskRunner implementation that delegates to a MockTaskRunner for asserting 115 // A TaskRunner implementation that delegates to a MockTaskRunner for asserting
116 // on task insertion order, and which stores tasks for actual later execution. 116 // on task insertion order, and which stores tasks for actual later execution.
117 class TaskRunnerProxy : public base::TaskRunner { 117 class TaskRunnerProxy : public base::TaskRunner {
118 public: 118 public:
119 TaskRunnerProxy(MockTaskRunner* mock, 119 TaskRunnerProxy(MockTaskRunner* mock,
120 base::SimpleTestTickClock* clock) 120 base::SimpleTestTickClock* clock)
121 : mock_(mock), clock_(clock) {} 121 : mock_(mock), clock_(clock) {}
122 bool RunsTasksOnCurrentThread() const override { return true; } 122 bool RunsTasksOnCurrentThread() const override { return true; }
123 bool PostDelayedTask(const tracked_objects::Location& location, 123 bool PostDelayedTask(const tracked_objects::Location& location,
124 const base::Closure& closure, 124 base::Closure closure,
125 base::TimeDelta delta) override { 125 base::TimeDelta delta) override {
126 mock_->PostDelayedTask(location, closure, delta); 126 mock_->PostDelayedTask(location, delta);
127 base::TimeTicks when = clock_->NowTicks() + delta; 127 base::TimeTicks when = clock_->NowTicks() + delta;
128 tasks_.push_back(Task(when, closure)); 128 tasks_.push_back(Task(when, std::move(closure)));
129 // Use 'greater' comparator to make this a min heap. 129 // Use 'greater' comparator to make this a min heap.
130 std::push_heap(tasks_.begin(), tasks_.end(), TaskComparator()); 130 std::push_heap(tasks_.begin(), tasks_.end(), TaskComparator());
131 return true; 131 return true;
132 } 132 }
133 133
134 // Runs tasks up to the current time. Returns the number of tasks executed. 134 // Runs tasks up to the current time. Returns the number of tasks executed.
135 size_t RunTasks() { 135 size_t RunTasks() {
136 base::TimeTicks now = clock_->NowTicks(); 136 base::TimeTicks now = clock_->NowTicks();
137 size_t count = 0; 137 size_t count = 0;
138 while (!tasks_.empty() && tasks_.front().first <= now) { 138 while (!tasks_.empty() && tasks_.front().first <= now) {
139 tasks_.front().second.Run(); 139 std::move(tasks_.front().second).Run();
sky 2017/03/22 17:34:11 ?
140 std::pop_heap(tasks_.begin(), tasks_.end(), TaskComparator()); 140 std::pop_heap(tasks_.begin(), tasks_.end(), TaskComparator());
141 tasks_.pop_back(); 141 tasks_.pop_back();
142 ++count; 142 ++count;
143 } 143 }
144 return count; 144 return count;
145 } 145 }
146 146
147 // Advances the clock and runs the next task in the queue. Can run more than 147 // Advances the clock and runs the next task in the queue. Can run more than
148 // one task if multiple tasks have the same scheduled time. 148 // one task if multiple tasks have the same scheduled time.
149 size_t RunNextTask() { 149 size_t RunNextTask() {
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after
588 EXPECT_EQ(0u, tm.notified_renderers_.size()); 588 EXPECT_EQ(0u, tm.notified_renderers_.size());
589 589
590 // START OF MEMORY PRESSURE 590 // START OF MEMORY PRESSURE
591 591
592 // Simulate a memory pressure situation that persists. This should cause a 592 // Simulate a memory pressure situation that persists. This should cause a
593 // task to be scheduled, and a background renderer to be notified. 593 // task to be scheduled, and a background renderer to be notified.
594 tm.get_current_pressure_level_ = base::Bind( 594 tm.get_current_pressure_level_ = base::Bind(
595 &ReturnSpecifiedPressure, base::Unretained(&level)); 595 &ReturnSpecifiedPressure, base::Unretained(&level));
596 EXPECT_CALL(mock_task_runner, PostDelayedTask( 596 EXPECT_CALL(mock_task_runner, PostDelayedTask(
597 testing::_, 597 testing::_,
598 testing::_,
599 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); 598 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds)));
600 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level)); 599 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level));
601 tm.OnMemoryPressure(level); 600 tm.OnMemoryPressure(level);
602 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); 601 testing::Mock::VerifyAndClearExpectations(&mock_task_runner);
603 testing::Mock::VerifyAndClearExpectations(this); 602 testing::Mock::VerifyAndClearExpectations(this);
604 EXPECT_TRUE(tm.under_memory_pressure_); 603 EXPECT_TRUE(tm.under_memory_pressure_);
605 EXPECT_EQ(1u, task_runner->size()); 604 EXPECT_EQ(1u, task_runner->size());
606 EXPECT_EQ(1u, tm.notified_renderers_.size()); 605 EXPECT_EQ(1u, tm.notified_renderers_.size());
607 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer2)); 606 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer2));
608 607
609 // REPEATED MEMORY PRESSURE SIGNAL 608 // REPEATED MEMORY PRESSURE SIGNAL
610 609
611 // Simulate another memory pressure event. This should not cause any 610 // Simulate another memory pressure event. This should not cause any
612 // additional tasks to be scheduled, nor any further renderer notifications. 611 // additional tasks to be scheduled, nor any further renderer notifications.
613 tm.OnMemoryPressure(level); 612 tm.OnMemoryPressure(level);
614 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); 613 testing::Mock::VerifyAndClearExpectations(&mock_task_runner);
615 testing::Mock::VerifyAndClearExpectations(this); 614 testing::Mock::VerifyAndClearExpectations(this);
616 EXPECT_TRUE(tm.under_memory_pressure_); 615 EXPECT_TRUE(tm.under_memory_pressure_);
617 EXPECT_EQ(1u, task_runner->size()); 616 EXPECT_EQ(1u, task_runner->size());
618 EXPECT_EQ(1u, tm.notified_renderers_.size()); 617 EXPECT_EQ(1u, tm.notified_renderers_.size());
619 618
620 // FIRST SCHEDULED NOTIFICATION 619 // FIRST SCHEDULED NOTIFICATION
621 620
622 // Run the scheduled task. This should cause another notification, but this 621 // Run the scheduled task. This should cause another notification, but this
623 // time to the foreground tab. It should also cause another scheduled event. 622 // time to the foreground tab. It should also cause another scheduled event.
624 EXPECT_CALL(mock_task_runner, PostDelayedTask( 623 EXPECT_CALL(mock_task_runner, PostDelayedTask(
625 testing::_, 624 testing::_,
626 testing::_,
627 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); 625 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds)));
628 EXPECT_CALL(*this, NotifyRendererProcess(renderer1, level)); 626 EXPECT_CALL(*this, NotifyRendererProcess(renderer1, level));
629 EXPECT_EQ(1u, task_runner->RunNextTask()); 627 EXPECT_EQ(1u, task_runner->RunNextTask());
630 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); 628 testing::Mock::VerifyAndClearExpectations(&mock_task_runner);
631 testing::Mock::VerifyAndClearExpectations(this); 629 testing::Mock::VerifyAndClearExpectations(this);
632 EXPECT_TRUE(tm.under_memory_pressure_); 630 EXPECT_TRUE(tm.under_memory_pressure_);
633 EXPECT_EQ(1u, task_runner->size()); 631 EXPECT_EQ(1u, task_runner->size());
634 EXPECT_EQ(2u, tm.notified_renderers_.size()); 632 EXPECT_EQ(2u, tm.notified_renderers_.size());
635 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer1)); 633 EXPECT_EQ(1u, tm.notified_renderers_.count(renderer1));
636 634
637 // SECOND SCHEDULED NOTIFICATION 635 // SECOND SCHEDULED NOTIFICATION
638 636
639 // Run the scheduled task. This should cause another notification, to the 637 // Run the scheduled task. This should cause another notification, to the
640 // background tab. It should also cause another scheduled event. 638 // background tab. It should also cause another scheduled event.
641 EXPECT_CALL(mock_task_runner, PostDelayedTask( 639 EXPECT_CALL(mock_task_runner, PostDelayedTask(
642 testing::_, 640 testing::_,
643 testing::_,
644 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds))); 641 base::TimeDelta::FromSeconds(tm.kRendererNotificationDelayInSeconds)));
645 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level)); 642 EXPECT_CALL(*this, NotifyRendererProcess(renderer2, level));
646 EXPECT_EQ(1u, task_runner->RunNextTask()); 643 EXPECT_EQ(1u, task_runner->RunNextTask());
647 testing::Mock::VerifyAndClearExpectations(&mock_task_runner); 644 testing::Mock::VerifyAndClearExpectations(&mock_task_runner);
648 testing::Mock::VerifyAndClearExpectations(this); 645 testing::Mock::VerifyAndClearExpectations(this);
649 EXPECT_TRUE(tm.under_memory_pressure_); 646 EXPECT_TRUE(tm.under_memory_pressure_);
650 EXPECT_EQ(1u, task_runner->size()); 647 EXPECT_EQ(1u, task_runner->size());
651 EXPECT_EQ(1u, tm.notified_renderers_.size()); 648 EXPECT_EQ(1u, tm.notified_renderers_.size());
652 649
653 // Expect that the background tab has been notified. The list of notified 650 // Expect that the background tab has been notified. The list of notified
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
748 // Since tab2 is kept inactive and background for more than time-to-purge, 745 // Since tab2 is kept inactive and background for more than time-to-purge,
749 // tab2 should be purged. 746 // tab2 should be purged.
750 EXPECT_TRUE(tab_manager.GetWebContentsData(tab2)->is_purged()); 747 EXPECT_TRUE(tab_manager.GetWebContentsData(tab2)->is_purged());
751 748
752 // Activate tab2. Tab2's PurgeAndSuspend state should be NOT_PURGED. 749 // Activate tab2. Tab2's PurgeAndSuspend state should be NOT_PURGED.
753 tabstrip.ActivateTabAt(1, true /* user_gesture */); 750 tabstrip.ActivateTabAt(1, true /* user_gesture */);
754 EXPECT_FALSE(tab_manager.GetWebContentsData(tab2)->is_purged()); 751 EXPECT_FALSE(tab_manager.GetWebContentsData(tab2)->is_purged());
755 } 752 }
756 753
757 } // namespace memory 754 } // namespace memory
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698