Home | History | Annotate | Download | only in task_manager
      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 "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"
     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 task_manager::Resource {
     30  public:
     31   TestResource() : refresh_called_(false) {}
     32 
     33   virtual base::string16 GetTitle() const OVERRIDE {
     34     return ASCIIToUTF16("test title");
     35   }
     36   virtual base::string16 GetProfileName() const OVERRIDE {
     37     return ASCIIToUTF16("test profile");
     38   }
     39   virtual gfx::ImageSkia GetIcon() const OVERRIDE { return gfx::ImageSkia(); }
     40   virtual base::ProcessHandle GetProcess() const OVERRIDE {
     41     return base::GetCurrentProcessHandle();
     42   }
     43   virtual int GetUniqueChildProcessId() const OVERRIDE {
     44     // In reality the unique child process ID is not the actual process ID,
     45     // but for testing purposes it shouldn't make difference.
     46     return static_cast<int>(base::GetCurrentProcId());
     47   }
     48   virtual Type GetType() const OVERRIDE { return RENDERER; }
     49   virtual bool SupportNetworkUsage() const OVERRIDE { return false; }
     50   virtual void SetSupportNetworkUsage() OVERRIDE { NOTREACHED(); }
     51   virtual void Refresh() OVERRIDE { refresh_called_ = true; }
     52   bool refresh_called() const { return refresh_called_; }
     53   void set_refresh_called(bool refresh_called) {
     54     refresh_called_ = refresh_called;
     55   }
     56 
     57  private:
     58   bool refresh_called_;
     59 };
     60 
     61 }  // namespace
     62 
     63 class TaskManagerTest : public testing::Test {
     64 };
     65 
     66 TEST_F(TaskManagerTest, Basic) {
     67   TaskManager task_manager;
     68   TaskManagerModel* model = task_manager.model_.get();
     69   EXPECT_EQ(0, model->ResourceCount());
     70 }
     71 
     72 TEST_F(TaskManagerTest, Resources) {
     73   TaskManager task_manager;
     74   TaskManagerModel* model = task_manager.model_.get();
     75 
     76   TestResource resource1, resource2;
     77 
     78   task_manager.AddResource(&resource1);
     79   ASSERT_EQ(1, model->ResourceCount());
     80   EXPECT_TRUE(model->IsResourceFirstInGroup(0));
     81   EXPECT_EQ(ASCIIToUTF16("test title"), model->GetResourceTitle(0));
     82   EXPECT_EQ(ASCIIToUTF16("test profile"), model->GetResourceProfileName(0));
     83   EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT),
     84             model->GetResourceNetworkUsage(0));
     85   EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage), model->GetResourceCPUUsage(0));
     86 
     87   task_manager.AddResource(&resource2);  // Will be in the same group.
     88   ASSERT_EQ(2, model->ResourceCount());
     89   EXPECT_TRUE(model->IsResourceFirstInGroup(0));
     90   EXPECT_FALSE(model->IsResourceFirstInGroup(1));
     91   EXPECT_EQ(ASCIIToUTF16("test title"), model->GetResourceTitle(1));
     92   EXPECT_EQ(ASCIIToUTF16("test profile"), model->GetResourceProfileName(1));
     93   EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT).c_str(),
     94             model->GetResourceNetworkUsage(1));
     95   EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage), model->GetResourceCPUUsage(1));
     96 
     97   task_manager.RemoveResource(&resource1);
     98   // Now resource2 will be first in group.
     99   ASSERT_EQ(1, model->ResourceCount());
    100   EXPECT_TRUE(model->IsResourceFirstInGroup(0));
    101   EXPECT_EQ(ASCIIToUTF16("test title"), model->GetResourceTitle(0));
    102   EXPECT_EQ(ASCIIToUTF16("test profile"), model->GetResourceProfileName(0));
    103   EXPECT_EQ(l10n_util::GetStringUTF16(IDS_TASK_MANAGER_NA_CELL_TEXT),
    104             model->GetResourceNetworkUsage(0));
    105   EXPECT_EQ(ASCIIToUTF16(kZeroCPUUsage), model->GetResourceCPUUsage(0));
    106 
    107   task_manager.RemoveResource(&resource2);
    108   EXPECT_EQ(0, model->ResourceCount());
    109 }
    110 
    111 // Tests that the model is calling Refresh() on its resources.
    112 TEST_F(TaskManagerTest, RefreshCalled) {
    113   base::MessageLoop loop;
    114   TaskManager task_manager;
    115   TaskManagerModel* model = task_manager.model_.get();
    116   TestResource resource;
    117 
    118   task_manager.AddResource(&resource);
    119   ASSERT_FALSE(resource.refresh_called());
    120   model->update_state_ = TaskManagerModel::TASK_PENDING;
    121   model->Refresh();
    122   ASSERT_TRUE(resource.refresh_called());
    123   task_manager.RemoveResource(&resource);
    124 }
    125