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
{
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
{
17 TestTaskManager() : TaskManagerInterface() {
18 set_timer_for_testing(scoped_ptr
<base::Timer
>(new base::MockTimer(true,
21 ~TestTaskManager() override
{}
23 // task_management::TaskManagerInterface:
24 void ActivateTask(TaskId task_id
) override
{}
25 double GetCpuUsage(TaskId task_id
) const override
{ return 0.0; }
26 int64
GetPhysicalMemoryUsage(TaskId task_id
) const override
{ return -1; }
27 int64
GetPrivateMemoryUsage(TaskId task_id
) const override
{ return -1; }
28 int64
GetSharedMemoryUsage(TaskId task_id
) const override
{ return -1; }
29 int64
GetGpuMemoryUsage(TaskId task_id
, bool* has_duplicates
) const override
{
32 int GetIdleWakeupsPerSecond(TaskId task_id
) const override
{ return -1; }
33 int GetNaClDebugStubPort(TaskId task_id
) const override
{ return -1; }
34 void GetGDIHandles(TaskId task_id
,
36 int64
* peak
) const override
{}
37 void GetUSERHandles(TaskId task_id
,
39 int64
* peak
) const override
{}
40 const base::string16
& GetTitle(TaskId task_id
) const override
{
43 base::string16
GetProfileName(TaskId task_id
) const override
{
44 return base::string16();
46 const gfx::ImageSkia
& GetIcon(TaskId task_id
) const override
{ return icon_
; }
47 const base::ProcessHandle
& GetProcessHandle(TaskId task_id
) const override
{
50 const base::ProcessId
& GetProcessId(TaskId task_id
) const override
{
53 Task::Type
GetType(TaskId task_id
) const override
{ return Task::UNKNOWN
; }
54 int64
GetNetworkUsage(TaskId task_id
) const override
{ return -1; }
55 int64
GetSqliteMemoryUsed(TaskId task_id
) const override
{ return -1; }
56 bool GetV8Memory(TaskId task_id
,
58 int64
* used
) const override
{ return false; }
59 bool GetWebCacheStats(
61 blink::WebCache::ResourceTypeStats
* stats
) const override
{
65 base::TimeDelta
GetRefreshTime() {
66 return GetCurrentRefreshTime();
69 int64
GetEnabledFlags() {
70 return enabled_resources_flags();
74 // task_management::TaskManager:
75 void Refresh() override
{}
78 base::ProcessHandle handle_
;
80 base::string16 title_
;
83 DISALLOW_COPY_AND_ASSIGN(TestTaskManager
);
86 // Defines a concrete observer that will be used for testing.
87 class TestObserver
: public TaskManagerObserver
{
89 TestObserver(base::TimeDelta refresh_time
, int64 resources_flags
)
90 : TaskManagerObserver(refresh_time
, resources_flags
) {
93 ~TestObserver() override
{}
95 // task_management::TaskManagerObserver:
96 void OnTaskAdded(TaskId id
) override
{}
97 void OnTaskToBeRemoved(TaskId id
) override
{}
98 void OnTasksRefreshed(const TaskIdList
& task_ids
) override
{}
101 DISALLOW_COPY_AND_ASSIGN(TestObserver
);
104 // Defines a test to validate the behavior of the task manager in response to
105 // adding and removing different kind of observers.
106 class TaskManagerObserverTest
: public testing::Test
{
108 TaskManagerObserverTest() {}
109 ~TaskManagerObserverTest() override
{}
111 TestTaskManager
& task_manager() { return task_manager_
; }
114 TestTaskManager task_manager_
;
116 DISALLOW_COPY_AND_ASSIGN(TaskManagerObserverTest
);
121 // Validates that the minimum refresh time to be requested is one second. Also
122 // validates the desired resource flags.
123 TEST_F(TaskManagerObserverTest
, Basic
) {
124 base::TimeDelta refresh_time1
= base::TimeDelta::FromSeconds(2);
125 base::TimeDelta refresh_time2
= base::TimeDelta::FromMilliseconds(999);
126 int64 flags1
= RefreshType::REFRESH_TYPE_CPU
|
127 RefreshType::REFRESH_TYPE_WEBCACHE_STATS
|
128 RefreshType::REFRESH_TYPE_HANDLES
;
129 int64 flags2
= RefreshType::REFRESH_TYPE_MEMORY
|
130 RefreshType::REFRESH_TYPE_NACL
;
132 TestObserver
observer1(refresh_time1
, flags1
);
133 TestObserver
observer2(refresh_time2
, flags2
);
135 EXPECT_EQ(refresh_time1
, observer1
.desired_refresh_time());
136 EXPECT_EQ(base::TimeDelta::FromSeconds(1), observer2
.desired_refresh_time());
137 EXPECT_EQ(flags1
, observer1
.desired_resources_flags());
138 EXPECT_EQ(flags2
, observer2
.desired_resources_flags());
141 // Validates the behavior of the task manager in response to adding and
142 // removing observers.
143 TEST_F(TaskManagerObserverTest
, TaskManagerResponseToObservers
) {
144 EXPECT_EQ(base::TimeDelta::Max(), task_manager().GetRefreshTime());
145 EXPECT_EQ(0, task_manager().GetEnabledFlags());
147 // Add a bunch of observers and make sure the task manager responds correctly.
148 base::TimeDelta refresh_time1
= base::TimeDelta::FromSeconds(3);
149 base::TimeDelta refresh_time2
= base::TimeDelta::FromSeconds(10);
150 base::TimeDelta refresh_time3
= base::TimeDelta::FromSeconds(3);
151 base::TimeDelta refresh_time4
= base::TimeDelta::FromSeconds(2);
152 int64 flags1
= RefreshType::REFRESH_TYPE_CPU
|
153 RefreshType::REFRESH_TYPE_WEBCACHE_STATS
|
154 RefreshType::REFRESH_TYPE_HANDLES
;
155 int64 flags2
= RefreshType::REFRESH_TYPE_MEMORY
|
156 RefreshType::REFRESH_TYPE_NACL
;
157 int64 flags3
= RefreshType::REFRESH_TYPE_MEMORY
|
158 RefreshType::REFRESH_TYPE_CPU
;
159 int64 flags4
= RefreshType::REFRESH_TYPE_GPU_MEMORY
;
161 TestObserver
observer1(refresh_time1
, flags1
);
162 TestObserver
observer2(refresh_time2
, flags2
);
163 TestObserver
observer3(refresh_time3
, flags3
);
164 TestObserver
observer4(refresh_time4
, flags4
);
166 task_manager().AddObserver(&observer1
);
167 task_manager().AddObserver(&observer2
);
168 task_manager().AddObserver(&observer3
);
169 task_manager().AddObserver(&observer4
);
171 EXPECT_EQ(refresh_time4
, task_manager().GetRefreshTime());
172 EXPECT_EQ(flags1
| flags2
| flags3
| flags4
,
173 task_manager().GetEnabledFlags());
175 // Removing observers should also reflect on the refresh time and resource
177 task_manager().RemoveObserver(&observer4
);
178 EXPECT_EQ(refresh_time3
, task_manager().GetRefreshTime());
179 EXPECT_EQ(flags1
| flags2
| flags3
, task_manager().GetEnabledFlags());
180 task_manager().RemoveObserver(&observer3
);
181 EXPECT_EQ(refresh_time1
, task_manager().GetRefreshTime());
182 EXPECT_EQ(flags1
| flags2
, task_manager().GetEnabledFlags());
183 task_manager().RemoveObserver(&observer2
);
184 EXPECT_EQ(refresh_time1
, task_manager().GetRefreshTime());
185 EXPECT_EQ(flags1
, task_manager().GetEnabledFlags());
186 task_manager().RemoveObserver(&observer1
);
187 EXPECT_EQ(base::TimeDelta::Max(), task_manager().GetRefreshTime());
188 EXPECT_EQ(0, task_manager().GetEnabledFlags());
191 } // namespace task_management