1 // Copyright (c) 2011 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.
4
5 #include "chrome/browser/task_manager/task_manager.h"
6
7 #include <string>
8
9 #include "base/message_loop.h"
10 #include "base/process_util.h"
11 #include "base/utf_string_conversions.h"
12 #include "grit/chromium_strings.h"
13 #include "grit/generated_resources.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "third_party/skia/include/core/SkBitmap.h"
16 #include "ui/base/l10n/l10n_util.h"
17
18 namespace {
19
20 #if defined(OS_MACOSX)
21 // From task_manager.cc:
22 // Activity Monitor shows %cpu with one decimal digit -- be
23 // consistent with that.
24 const char* kZeroCPUUsage = "0.0";
25 #else
26 const char* kZeroCPUUsage = "0";
27 #endif
28
29 class TestResource : public TaskManager::Resource {
30 public:
TestResource()31 TestResource() : refresh_called_(false) {}
32
GetTitle() const33 virtual string16 GetTitle() const OVERRIDE {
34 return ASCIIToUTF16("test title");
35 }
GetIcon() const36 virtual SkBitmap GetIcon() const { return SkBitmap(); }
GetProcess() const37 virtual base::ProcessHandle GetProcess() const {
38 return base::GetCurrentProcessHandle();
39 }
GetType() const40 virtual Type GetType() const { return RENDERER; }
SupportNetworkUsage() const41 virtual bool SupportNetworkUsage() const { return false; }
SetSupportNetworkUsage()42 virtual void SetSupportNetworkUsage() { NOTREACHED(); }
Refresh()43 virtual void Refresh() { refresh_called_ = true; }
refresh_called() const44 bool refresh_called() const { return refresh_called_; }
set_refresh_called(bool refresh_called)45 void set_refresh_called(bool refresh_called) {
46 refresh_called_ = refresh_called;
47 }
48
49 private:
50 bool refresh_called_;
51 };
52
53 } // namespace
54
55 class TaskManagerTest : public testing::Test {
56 };
57
TEST_F(TaskManagerTest,Basic)58 TEST_F(TaskManagerTest, Basic) {
59 TaskManager task_manager;
60 TaskManagerModel* model = task_manager.model_;
61 EXPECT_EQ(0, model->ResourceCount());
62 }
63
TEST_F(TaskManagerTest,Resources)64 TEST_F(TaskManagerTest, Resources) {
65 TaskManager task_manager;
66 TaskManagerModel* model = task_manager.model_;
67
68 TestResource resource1, resource2;
69
70 task_manager.AddResource(&resource1);
71 ASSERT_EQ(1, model->ResourceCount());
72 EXPECT_TRUE(model->IsResourceFirstInGroup(0));
73 EXPECT_EQ(ASCIIToUTF16("test title"), model->GetResourceTitle(0));
74 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT),
75 model->GetResourceNetworkUsage(0));
76 EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage), model->GetResourceCPUUsage(0));
77
78 task_manager.AddResource(&resource2); // Will be in the same group.
79 ASSERT_EQ(2, model->ResourceCount());
80 EXPECT_TRUE(model->IsResourceFirstInGroup(0));
81 EXPECT_FALSE(model->IsResourceFirstInGroup(1));
82 EXPECT_EQ(ASCIIToUTF16("test title"), model->GetResourceTitle(1));
83 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT).c_str(),
84 model->GetResourceNetworkUsage(1));
85 EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage), model->GetResourceCPUUsage(1));
86
87 task_manager.RemoveResource(&resource1);
88 // Now resource2 will be first in group.
89 ASSERT_EQ(1, model->ResourceCount());
90 EXPECT_TRUE(model->IsResourceFirstInGroup(0));
91 EXPECT_EQ(ASCIIToUTF16("test title"), model->GetResourceTitle(0));
92 EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT),
93 model->GetResourceNetworkUsage(0));
94 EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage), model->GetResourceCPUUsage(0));
95
96 task_manager.RemoveResource(&resource2);
97 EXPECT_EQ(0, model->ResourceCount());
98 }
99
100 // Tests that the model is calling Refresh() on its resources.
TEST_F(TaskManagerTest,RefreshCalled)101 TEST_F(TaskManagerTest, RefreshCalled) {
102 MessageLoop loop;
103 TaskManager task_manager;
104 TaskManagerModel* model = task_manager.model_;
105 TestResource resource;
106
107 task_manager.AddResource(&resource);
108 ASSERT_FALSE(resource.refresh_called());
109 model->update_state_ = TaskManagerModel::TASK_PENDING;
110 model->Refresh();
111 ASSERT_TRUE(resource.refresh_called());
112 task_manager.RemoveResource(&resource);
113 }
114