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