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. 4 5 #include "chrome/browser/task_manager/task_manager.h" 6 7 #include <string> 8 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" 16 17 using base::ASCIIToUTF16; 18 19 namespace { 20 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"; 26 #else 27 const char* kZeroCPUUsage = "0"; 28 #endif 29 30 class TestResource : public task_manager::Resource { 31 public: 32 TestResource() : refresh_called_(false) {} 33 34 virtual base::string16 GetTitle() const OVERRIDE { 35 return ASCIIToUTF16("test title"); 36 } 37 virtual base::string16 GetProfileName() const OVERRIDE { 38 return ASCIIToUTF16("test profile"); 39 } 40 virtual gfx::ImageSkia GetIcon() const OVERRIDE { return gfx::ImageSkia(); } 41 virtual base::ProcessHandle GetProcess() const OVERRIDE { 42 return base::GetCurrentProcessHandle(); 43 } 44 virtual 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()); 48 } 49 virtual Type GetType() const OVERRIDE { return RENDERER; } 50 virtual bool SupportNetworkUsage() const OVERRIDE { return false; } 51 virtual void SetSupportNetworkUsage() OVERRIDE { NOTREACHED(); } 52 virtual 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; 56 } 57 58 private: 59 bool refresh_called_; 60 }; 61 62 } // namespace 63 64 class TaskManagerTest : public testing::Test { 65 }; 66 67 TEST_F(TaskManagerTest, Basic) { 68 TaskManager task_manager; 69 TaskManagerModel* model = task_manager.model_.get(); 70 EXPECT_EQ(0, model->ResourceCount()); 71 } 72 73 TEST_F(TaskManagerTest, Resources) { 74 TaskManager task_manager; 75 TaskManagerModel* model = task_manager.model_.get(); 76 77 TestResource resource1, resource2; 78 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)); 87 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)); 97 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)); 107 108 task_manager.RemoveResource(&resource2); 109 EXPECT_EQ(0, model->ResourceCount()); 110 } 111 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; 118 119 task_manager.AddResource(&resource); 120 ASSERT_FALSE(resource.refresh_called()); 121 model->update_state_ = TaskManagerModel::TASK_PENDING; 122 model->Refresh(); 123 ASSERT_TRUE(resource.refresh_called()); 124 task_manager.RemoveResource(&resource); 125 } 126