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
3 // found in the LICENSE file.
5 #include "chrome/browser/task_manager/task_manager.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/task_manager/resource_provider.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/gfx/image/image_skia.h"
17 using base::ASCIIToUTF16
;
21 #if defined(OS_MACOSX)
22 // From task_manager.cc:
23 // Activity Monitor shows %cpu with one decimal digit -- be
24 // consistent with that.
25 const char* kZeroCPUUsage
= "0.0";
27 const char* kZeroCPUUsage
= "0";
30 class TestResource
: public task_manager::Resource
{
32 TestResource() : refresh_called_(false) {}
34 base::string16
GetTitle() const override
{
35 return ASCIIToUTF16("test title");
37 base::string16
GetProfileName() const override
{
38 return ASCIIToUTF16("test profile");
40 gfx::ImageSkia
GetIcon() const override
{ return gfx::ImageSkia(); }
41 base::ProcessHandle
GetProcess() const override
{
42 return base::GetCurrentProcessHandle();
44 int GetUniqueChildProcessId() const override
{
45 // In reality the unique child process ID is not the actual process ID,
46 // but for testing purposes it shouldn't make difference.
47 return static_cast<int>(base::GetCurrentProcId());
49 Type
GetType() const override
{ return RENDERER
; }
50 bool SupportNetworkUsage() const override
{ return false; }
51 void SetSupportNetworkUsage() override
{ NOTREACHED(); }
52 void Refresh() override
{ refresh_called_
= true; }
53 bool refresh_called() const { return refresh_called_
; }
54 void set_refresh_called(bool refresh_called
) {
55 refresh_called_
= refresh_called
;
64 class TaskManagerTest
: public testing::Test
{
67 TEST_F(TaskManagerTest
, Basic
) {
68 TaskManager task_manager
;
69 TaskManagerModel
* model
= task_manager
.model_
.get();
70 EXPECT_EQ(0, model
->ResourceCount());
73 TEST_F(TaskManagerTest
, Resources
) {
74 TaskManager task_manager
;
75 TaskManagerModel
* model
= task_manager
.model_
.get();
77 TestResource resource1
, resource2
;
79 task_manager
.AddResource(&resource1
);
80 ASSERT_EQ(1, model
->ResourceCount());
81 EXPECT_TRUE(model
->IsResourceFirstInGroup(0));
82 EXPECT_EQ(ASCIIToUTF16("test title"), model
->GetResourceTitle(0));
83 EXPECT_EQ(ASCIIToUTF16("test profile"), model
->GetResourceProfileName(0));
84 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT
),
85 model
->GetResourceNetworkUsage(0));
86 EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage
), model
->GetResourceCPUUsage(0));
88 task_manager
.AddResource(&resource2
); // Will be in the same group.
89 ASSERT_EQ(2, model
->ResourceCount());
90 EXPECT_TRUE(model
->IsResourceFirstInGroup(0));
91 EXPECT_FALSE(model
->IsResourceFirstInGroup(1));
92 EXPECT_EQ(ASCIIToUTF16("test title"), model
->GetResourceTitle(1));
93 EXPECT_EQ(ASCIIToUTF16("test profile"), model
->GetResourceProfileName(1));
94 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT
).c_str(),
95 model
->GetResourceNetworkUsage(1));
96 EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage
), model
->GetResourceCPUUsage(1));
98 task_manager
.RemoveResource(&resource1
);
99 // Now resource2 will be first in group.
100 ASSERT_EQ(1, model
->ResourceCount());
101 EXPECT_TRUE(model
->IsResourceFirstInGroup(0));
102 EXPECT_EQ(ASCIIToUTF16("test title"), model
->GetResourceTitle(0));
103 EXPECT_EQ(ASCIIToUTF16("test profile"), model
->GetResourceProfileName(0));
104 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT
),
105 model
->GetResourceNetworkUsage(0));
106 EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage
), model
->GetResourceCPUUsage(0));
108 task_manager
.RemoveResource(&resource2
);
109 EXPECT_EQ(0, model
->ResourceCount());
112 // Tests that the model is calling Refresh() on its resources.
113 TEST_F(TaskManagerTest
, RefreshCalled
) {
114 base::MessageLoop loop
;
115 TaskManager task_manager
;
116 TaskManagerModel
* model
= task_manager
.model_
.get();
117 TestResource resource
;
119 task_manager
.AddResource(&resource
);
120 ASSERT_FALSE(resource
.refresh_called());
121 model
->update_state_
= TaskManagerModel::TASK_PENDING
;
123 ASSERT_TRUE(resource
.refresh_called());
124 task_manager
.RemoveResource(&resource
);