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

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

Powered by Google App Engine
This is Rietveld 408576698