Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / task_management / task_manager_observer_unittest.cc
blob80d308c89f4b7865c2cf09541e69324380838283
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/timer/mock_timer.h"
6 #include "chrome/browser/task_management/task_manager_interface.h"
7 #include "testing/gtest/include/gtest/gtest.h"
9 namespace task_management {
11 namespace {
13 // This is a partial stub implementation to test the behavior of the base class
14 // TaskManagerInterface in response to adding and removing observers.
15 class TestTaskManager : public TaskManagerInterface {
16 public:
17 TestTaskManager()
18 : TaskManagerInterface(),
19 handle_(),
20 id_(),
21 title_(),
22 icon_(),
23 ids_() {
24 set_timer_for_testing(scoped_ptr<base::Timer>(new base::MockTimer(true,
25 true)));
27 ~TestTaskManager() override {}
29 // task_management::TaskManagerInterface:
30 void ActivateTask(TaskId task_id) override {}
31 double GetCpuUsage(TaskId task_id) const override { return 0.0; }
32 int64 GetPhysicalMemoryUsage(TaskId task_id) const override { return -1; }
33 int64 GetPrivateMemoryUsage(TaskId task_id) const override { return -1; }
34 int64 GetSharedMemoryUsage(TaskId task_id) const override { return -1; }
35 int64 GetGpuMemoryUsage(TaskId task_id, bool* has_duplicates) const override {
36 return -1;
38 int GetIdleWakeupsPerSecond(TaskId task_id) const override { return -1; }
39 int GetNaClDebugStubPort(TaskId task_id) const override { return -1; }
40 void GetGDIHandles(TaskId task_id,
41 int64* current,
42 int64* peak) const override {}
43 void GetUSERHandles(TaskId task_id,
44 int64* current,
45 int64* peak) const override {}
46 const base::string16& GetTitle(TaskId task_id) const override {
47 return title_;
49 base::string16 GetProfileName(TaskId task_id) const override {
50 return base::string16();
52 const gfx::ImageSkia& GetIcon(TaskId task_id) const override { return icon_; }
53 const base::ProcessHandle& GetProcessHandle(TaskId task_id) const override {
54 return handle_;
56 const base::ProcessId& GetProcessId(TaskId task_id) const override {
57 return id_;
59 Task::Type GetType(TaskId task_id) const override { return Task::UNKNOWN; }
60 int64 GetNetworkUsage(TaskId task_id) const override { return -1; }
61 int64 GetSqliteMemoryUsed(TaskId task_id) const override { return -1; }
62 bool GetV8Memory(TaskId task_id,
63 int64* allocated,
64 int64* used) const override { return false; }
65 bool GetWebCacheStats(
66 TaskId task_id,
67 blink::WebCache::ResourceTypeStats* stats) const override {
68 return false;
70 const TaskIdList& GetTaskIdsList() const override {
71 return ids_;
73 size_t GetNumberOfTasksOnSameProcess(TaskId task_id) const override {
74 return 1;
77 base::TimeDelta GetRefreshTime() {
78 return GetCurrentRefreshTime();
81 int64 GetEnabledFlags() {
82 return enabled_resources_flags();
85 protected:
86 // task_management::TaskManager:
87 void Refresh() override {}
88 void StartUpdating() override {}
89 void StopUpdating() override {}
91 private:
92 base::ProcessHandle handle_;
93 base::ProcessId id_;
94 base::string16 title_;
95 gfx::ImageSkia icon_;
96 TaskIdList ids_;
98 DISALLOW_COPY_AND_ASSIGN(TestTaskManager);
101 // Defines a concrete observer that will be used for testing.
102 class TestObserver : public TaskManagerObserver {
103 public:
104 TestObserver(base::TimeDelta refresh_time, int64 resources_flags)
105 : TaskManagerObserver(refresh_time, resources_flags) {
108 ~TestObserver() override {}
110 // task_management::TaskManagerObserver:
111 void OnTaskAdded(TaskId id) override {}
112 void OnTaskToBeRemoved(TaskId id) override {}
113 void OnTasksRefreshed(const TaskIdList& task_ids) override {}
115 private:
116 DISALLOW_COPY_AND_ASSIGN(TestObserver);
119 // Defines a test to validate the behavior of the task manager in response to
120 // adding and removing different kind of observers.
121 class TaskManagerObserverTest : public testing::Test {
122 public:
123 TaskManagerObserverTest() {}
124 ~TaskManagerObserverTest() override {}
126 TestTaskManager& task_manager() { return task_manager_; }
128 private:
129 TestTaskManager task_manager_;
131 DISALLOW_COPY_AND_ASSIGN(TaskManagerObserverTest);
134 } // namespace
136 // Validates that the minimum refresh time to be requested is one second. Also
137 // validates the desired resource flags.
138 TEST_F(TaskManagerObserverTest, Basic) {
139 base::TimeDelta refresh_time1 = base::TimeDelta::FromSeconds(2);
140 base::TimeDelta refresh_time2 = base::TimeDelta::FromMilliseconds(999);
141 int64 flags1 = RefreshType::REFRESH_TYPE_CPU |
142 RefreshType::REFRESH_TYPE_WEBCACHE_STATS |
143 RefreshType::REFRESH_TYPE_HANDLES;
144 int64 flags2 = RefreshType::REFRESH_TYPE_MEMORY |
145 RefreshType::REFRESH_TYPE_NACL;
147 TestObserver observer1(refresh_time1, flags1);
148 TestObserver observer2(refresh_time2, flags2);
150 EXPECT_EQ(refresh_time1, observer1.desired_refresh_time());
151 EXPECT_EQ(base::TimeDelta::FromSeconds(1), observer2.desired_refresh_time());
152 EXPECT_EQ(flags1, observer1.desired_resources_flags());
153 EXPECT_EQ(flags2, observer2.desired_resources_flags());
156 // Validates the behavior of the task manager in response to adding and
157 // removing observers.
158 TEST_F(TaskManagerObserverTest, TaskManagerResponseToObservers) {
159 EXPECT_EQ(base::TimeDelta::Max(), task_manager().GetRefreshTime());
160 EXPECT_EQ(0, task_manager().GetEnabledFlags());
162 // Add a bunch of observers and make sure the task manager responds correctly.
163 base::TimeDelta refresh_time1 = base::TimeDelta::FromSeconds(3);
164 base::TimeDelta refresh_time2 = base::TimeDelta::FromSeconds(10);
165 base::TimeDelta refresh_time3 = base::TimeDelta::FromSeconds(3);
166 base::TimeDelta refresh_time4 = base::TimeDelta::FromSeconds(2);
167 int64 flags1 = RefreshType::REFRESH_TYPE_CPU |
168 RefreshType::REFRESH_TYPE_WEBCACHE_STATS |
169 RefreshType::REFRESH_TYPE_HANDLES;
170 int64 flags2 = RefreshType::REFRESH_TYPE_MEMORY |
171 RefreshType::REFRESH_TYPE_NACL;
172 int64 flags3 = RefreshType::REFRESH_TYPE_MEMORY |
173 RefreshType::REFRESH_TYPE_CPU;
174 int64 flags4 = RefreshType::REFRESH_TYPE_GPU_MEMORY;
176 TestObserver observer1(refresh_time1, flags1);
177 TestObserver observer2(refresh_time2, flags2);
178 TestObserver observer3(refresh_time3, flags3);
179 TestObserver observer4(refresh_time4, flags4);
181 task_manager().AddObserver(&observer1);
182 task_manager().AddObserver(&observer2);
183 task_manager().AddObserver(&observer3);
184 task_manager().AddObserver(&observer4);
186 EXPECT_EQ(refresh_time4, task_manager().GetRefreshTime());
187 EXPECT_EQ(flags1 | flags2 | flags3 | flags4,
188 task_manager().GetEnabledFlags());
190 // Removing observers should also reflect on the refresh time and resource
191 // flags.
192 task_manager().RemoveObserver(&observer4);
193 EXPECT_EQ(refresh_time3, task_manager().GetRefreshTime());
194 EXPECT_EQ(flags1 | flags2 | flags3, task_manager().GetEnabledFlags());
195 task_manager().RemoveObserver(&observer3);
196 EXPECT_EQ(refresh_time1, task_manager().GetRefreshTime());
197 EXPECT_EQ(flags1 | flags2, task_manager().GetEnabledFlags());
198 task_manager().RemoveObserver(&observer2);
199 EXPECT_EQ(refresh_time1, task_manager().GetRefreshTime());
200 EXPECT_EQ(flags1, task_manager().GetEnabledFlags());
201 task_manager().RemoveObserver(&observer1);
202 EXPECT_EQ(base::TimeDelta::Max(), task_manager().GetRefreshTime());
203 EXPECT_EQ(0, task_manager().GetEnabledFlags());
206 } // namespace task_management