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 "grit/chromium_strings.h"
13 #include "grit/generated_resources.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/gfx/image/image_skia.h"
18 using base::ASCIIToUTF16
;
22 #if defined(OS_MACOSX)
23 // From task_manager.cc:
24 // Activity Monitor shows %cpu with one decimal digit -- be
25 // consistent with that.
26 const char* kZeroCPUUsage
= "0.0";
28 const char* kZeroCPUUsage
= "0";
31 class TestResource
: public task_manager::Resource
{
33 TestResource() : refresh_called_(false) {}
35 virtual base::string16
GetTitle() const OVERRIDE
{
36 return ASCIIToUTF16("test title");
38 virtual base::string16
GetProfileName() const OVERRIDE
{
39 return ASCIIToUTF16("test profile");
41 virtual gfx::ImageSkia
GetIcon() const OVERRIDE
{ return gfx::ImageSkia(); }
42 virtual base::ProcessHandle
GetProcess() const OVERRIDE
{
43 return base::GetCurrentProcessHandle();
45 virtual int GetUniqueChildProcessId() const OVERRIDE
{
46 // In reality the unique child process ID is not the actual process ID,
47 // but for testing purposes it shouldn't make difference.
48 return static_cast<int>(base::GetCurrentProcId());
50 virtual Type
GetType() const OVERRIDE
{ return RENDERER
; }
51 virtual bool SupportNetworkUsage() const OVERRIDE
{ return false; }
52 virtual void SetSupportNetworkUsage() OVERRIDE
{ NOTREACHED(); }
53 virtual void Refresh() OVERRIDE
{ refresh_called_
= true; }
54 bool refresh_called() const { return refresh_called_
; }
55 void set_refresh_called(bool refresh_called
) {
56 refresh_called_
= refresh_called
;
65 class TaskManagerTest
: public testing::Test
{
68 TEST_F(TaskManagerTest
, Basic
) {
69 TaskManager task_manager
;
70 TaskManagerModel
* model
= task_manager
.model_
.get();
71 EXPECT_EQ(0, model
->ResourceCount());
74 TEST_F(TaskManagerTest
, Resources
) {
75 TaskManager task_manager
;
76 TaskManagerModel
* model
= task_manager
.model_
.get();
78 TestResource resource1
, resource2
;
80 task_manager
.AddResource(&resource1
);
81 ASSERT_EQ(1, model
->ResourceCount());
82 EXPECT_TRUE(model
->IsResourceFirstInGroup(0));
83 EXPECT_EQ(ASCIIToUTF16("test title"), model
->GetResourceTitle(0));
84 EXPECT_EQ(ASCIIToUTF16("test profile"), model
->GetResourceProfileName(0));
85 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT
),
86 model
->GetResourceNetworkUsage(0));
87 EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage
), model
->GetResourceCPUUsage(0));
89 task_manager
.AddResource(&resource2
); // Will be in the same group.
90 ASSERT_EQ(2, model
->ResourceCount());
91 EXPECT_TRUE(model
->IsResourceFirstInGroup(0));
92 EXPECT_FALSE(model
->IsResourceFirstInGroup(1));
93 EXPECT_EQ(ASCIIToUTF16("test title"), model
->GetResourceTitle(1));
94 EXPECT_EQ(ASCIIToUTF16("test profile"), model
->GetResourceProfileName(1));
95 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT
).c_str(),
96 model
->GetResourceNetworkUsage(1));
97 EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage
), model
->GetResourceCPUUsage(1));
99 task_manager
.RemoveResource(&resource1
);
100 // Now resource2 will be first in group.
101 ASSERT_EQ(1, model
->ResourceCount());
102 EXPECT_TRUE(model
->IsResourceFirstInGroup(0));
103 EXPECT_EQ(ASCIIToUTF16("test title"), model
->GetResourceTitle(0));
104 EXPECT_EQ(ASCIIToUTF16("test profile"), model
->GetResourceProfileName(0));
105 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT
),
106 model
->GetResourceNetworkUsage(0));
107 EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage
), model
->GetResourceCPUUsage(0));
109 task_manager
.RemoveResource(&resource2
);
110 EXPECT_EQ(0, model
->ResourceCount());
113 // Tests that the model is calling Refresh() on its resources.
114 TEST_F(TaskManagerTest
, RefreshCalled
) {
115 base::MessageLoop loop
;
116 TaskManager task_manager
;
117 TaskManagerModel
* model
= task_manager
.model_
.get();
118 TestResource resource
;
120 task_manager
.AddResource(&resource
);
121 ASSERT_FALSE(resource
.refresh_called());
122 model
->update_state_
= TaskManagerModel::TASK_PENDING
;
124 ASSERT_TRUE(resource
.refresh_called());
125 task_manager
.RemoveResource(&resource
);