task_manager_mac_unittest.mm revision 868fa2fe829687343ffae624259930155e16dbd8
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#import <Cocoa/Cocoa.h>
6
7#include "base/compiler_specific.h"
8#include "base/memory/scoped_nsobject.h"
9#include "base/strings/utf_string_conversions.h"
10#include "chrome/browser/task_manager/resource_provider.h"
11#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
12#import "chrome/browser/ui/cocoa/task_manager_mac.h"
13#include "grit/generated_resources.h"
14#include "testing/gtest/include/gtest/gtest.h"
15#import "testing/gtest_mac.h"
16#include "testing/platform_test.h"
17#include "ui/gfx/image/image_skia.h"
18
19namespace {
20
21class TestResource : public task_manager::Resource {
22 public:
23  TestResource(const string16& title, pid_t pid) : title_(title), pid_(pid) {}
24  virtual string16 GetTitle() const OVERRIDE { return title_; }
25  virtual string16 GetProfileName() const OVERRIDE { return string16(); }
26  virtual gfx::ImageSkia GetIcon() const OVERRIDE { return gfx::ImageSkia(); }
27  virtual base::ProcessHandle GetProcess() const OVERRIDE { return pid_; }
28  virtual int GetUniqueChildProcessId() const OVERRIDE {
29    // In reality the unique child process ID is not the actual process ID,
30    // but for testing purposes it shouldn't make difference.
31    return static_cast<int>(base::GetCurrentProcId());
32  }
33  virtual Type GetType() const OVERRIDE { return RENDERER; }
34  virtual bool SupportNetworkUsage() const OVERRIDE { return false; }
35  virtual void SetSupportNetworkUsage() OVERRIDE { NOTREACHED(); }
36  virtual void Refresh() OVERRIDE {}
37  string16 title_;
38  string16 profile_name_;
39  pid_t pid_;
40};
41
42}  // namespace
43
44class TaskManagerWindowControllerTest : public CocoaTest {
45};
46
47// Test creation, to ensure nothing leaks or crashes.
48TEST_F(TaskManagerWindowControllerTest, Init) {
49  TaskManager task_manager;
50  TaskManagerMac* bridge(new TaskManagerMac(&task_manager, false));
51  TaskManagerWindowController* controller = bridge->cocoa_controller();
52
53  // Releases the controller, which in turn deletes |bridge|.
54  [controller close];
55}
56
57TEST_F(TaskManagerWindowControllerTest, Sort) {
58  TaskManager task_manager;
59
60  TestResource resource1(UTF8ToUTF16("zzz"), 1);
61  TestResource resource2(UTF8ToUTF16("zzb"), 2);
62  TestResource resource3(UTF8ToUTF16("zza"), 2);
63
64  task_manager.AddResource(&resource1);
65  task_manager.AddResource(&resource2);
66  task_manager.AddResource(&resource3);  // Will be in the same group as 2.
67
68  TaskManagerMac* bridge(new TaskManagerMac(&task_manager, false));
69  TaskManagerWindowController* controller = bridge->cocoa_controller();
70  NSTableView* table = [controller tableView];
71  ASSERT_EQ(3, [controller numberOfRowsInTableView:table]);
72
73  // Test that table is sorted on title.
74  NSTableColumn* title_column = [table tableColumnWithIdentifier:
75      [NSString stringWithFormat:@"%d", IDS_TASK_MANAGER_TASK_COLUMN]];
76  NSCell* cell;
77  cell = [controller tableView:table dataCellForTableColumn:title_column row:0];
78  EXPECT_NSEQ(@"zzb", [cell title]);
79  cell = [controller tableView:table dataCellForTableColumn:title_column row:1];
80  EXPECT_NSEQ(@"zza", [cell title]);
81  cell = [controller tableView:table dataCellForTableColumn:title_column row:2];
82  EXPECT_NSEQ(@"zzz", [cell title]);
83
84  // Releases the controller, which in turn deletes |bridge|.
85  [controller close];
86
87  task_manager.RemoveResource(&resource1);
88  task_manager.RemoveResource(&resource2);
89  task_manager.RemoveResource(&resource3);
90}
91
92TEST_F(TaskManagerWindowControllerTest, SelectionAdaptsToSorting) {
93  TaskManager task_manager;
94
95  TestResource resource1(UTF8ToUTF16("yyy"), 1);
96  TestResource resource2(UTF8ToUTF16("aaa"), 2);
97
98  task_manager.AddResource(&resource1);
99  task_manager.AddResource(&resource2);
100
101  TaskManagerMac* bridge(new TaskManagerMac(&task_manager, false));
102  TaskManagerWindowController* controller = bridge->cocoa_controller();
103  NSTableView* table = [controller tableView];
104  ASSERT_EQ(2, [controller numberOfRowsInTableView:table]);
105
106  // Select row 0 in the table (corresponds to row 1 in the model).
107  [table selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
108     byExtendingSelection:NO];
109
110  // Change the name of resource2 so that it becomes row 1 in the table.
111  resource2.title_ = UTF8ToUTF16("zzz");
112  bridge->task_manager()->model()->Refresh();
113  bridge->OnItemsChanged(1, 1);
114
115  // Check that the selection has moved to row 1.
116  NSIndexSet* selection = [table selectedRowIndexes];
117  ASSERT_EQ(1u, [selection count]);
118  EXPECT_EQ(1u, [selection firstIndex]);
119
120  // Releases the controller, which in turn deletes |bridge|.
121  [controller close];
122
123  task_manager.RemoveResource(&resource1);
124  task_manager.RemoveResource(&resource2);
125}
126