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.
5 #import <Cocoa/Cocoa.h>
7 #include "base/compiler_specific.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/task_manager/resource_provider.h"
10 #import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
11 #import "chrome/browser/ui/cocoa/task_manager_mac.h"
12 #include "chrome/grit/generated_resources.h"
13 #include "content/public/test/test_browser_thread_bundle.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"
19 @interface TaskManagerWindowController(UnitTest)
21 - (void)toggleColumn:(NSMenuItem*)sender;
27 class TestResource : public task_manager::Resource {
29 TestResource(const base::string16& title, pid_t pid)
30 : title_(title), pid_(pid) {}
31 base::string16 GetTitle() const override { return title_; }
32 base::string16 GetProfileName() const override { return base::string16(); }
33 gfx::ImageSkia GetIcon() const override { return gfx::ImageSkia(); }
34 base::ProcessHandle GetProcess() const override { return pid_; }
35 int GetUniqueChildProcessId() const override {
36 // In reality the unique child process ID is not the actual process ID,
37 // but for testing purposes it shouldn't make difference.
38 return static_cast<int>(base::GetCurrentProcId());
40 Type GetType() const override { return RENDERER; }
41 bool SupportNetworkUsage() const override { return false; }
42 void SetSupportNetworkUsage() override { NOTREACHED(); }
43 void Refresh() override {}
44 base::string16 title_;
45 base::string16 profile_name_;
51 class TaskManagerWindowControllerTest : public CocoaTest {
52 content::TestBrowserThreadBundle thread_bundle_;
55 // Test creation, to ensure nothing leaks or crashes.
56 TEST_F(TaskManagerWindowControllerTest, Init) {
57 TaskManager task_manager;
58 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
59 TaskManagerWindowController* controller = bridge->cocoa_controller();
61 // Releases the controller, which in turn deletes |bridge|.
65 TEST_F(TaskManagerWindowControllerTest, Sort) {
66 TaskManager task_manager;
68 TestResource resource1(base::UTF8ToUTF16("zzz"), 1);
69 TestResource resource2(base::UTF8ToUTF16("zzb"), 2);
70 TestResource resource3(base::UTF8ToUTF16("zza"), 2);
72 task_manager.AddResource(&resource1);
73 task_manager.AddResource(&resource2);
74 task_manager.AddResource(&resource3); // Will be in the same group as 2.
76 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
77 TaskManagerWindowController* controller = bridge->cocoa_controller();
78 NSTableView* table = [controller tableView];
79 ASSERT_EQ(3, [controller numberOfRowsInTableView:table]);
81 // Test that table is sorted on title.
82 NSTableColumn* title_column = [table tableColumnWithIdentifier:
83 [NSString stringWithFormat:@"%d", IDS_TASK_MANAGER_TASK_COLUMN]];
85 cell = [controller tableView:table dataCellForTableColumn:title_column row:0];
86 EXPECT_NSEQ(@"zzb", [cell title]);
87 cell = [controller tableView:table dataCellForTableColumn:title_column row:1];
88 EXPECT_NSEQ(@"zza", [cell title]);
89 cell = [controller tableView:table dataCellForTableColumn:title_column row:2];
90 EXPECT_NSEQ(@"zzz", [cell title]);
92 // Releases the controller, which in turn deletes |bridge|.
95 task_manager.RemoveResource(&resource1);
96 task_manager.RemoveResource(&resource2);
97 task_manager.RemoveResource(&resource3);
100 TEST_F(TaskManagerWindowControllerTest, SelectionAdaptsToSorting) {
101 TaskManager task_manager;
103 TestResource resource1(base::UTF8ToUTF16("yyy"), 1);
104 TestResource resource2(base::UTF8ToUTF16("aaa"), 2);
106 task_manager.AddResource(&resource1);
107 task_manager.AddResource(&resource2);
109 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
110 TaskManagerWindowController* controller = bridge->cocoa_controller();
111 NSTableView* table = [controller tableView];
112 ASSERT_EQ(2, [controller numberOfRowsInTableView:table]);
114 // Select row 0 in the table (corresponds to row 1 in the model).
115 [table selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
116 byExtendingSelection:NO];
118 // Change the name of resource2 so that it becomes row 1 in the table.
119 resource2.title_ = base::UTF8ToUTF16("zzz");
120 bridge->task_manager()->model()->Refresh();
121 bridge->OnItemsChanged(1, 1);
123 // Check that the selection has moved to row 1.
124 NSIndexSet* selection = [table selectedRowIndexes];
125 ASSERT_EQ(1u, [selection count]);
126 EXPECT_EQ(1u, [selection firstIndex]);
128 // Releases the controller, which in turn deletes |bridge|.
131 task_manager.RemoveResource(&resource1);
132 task_manager.RemoveResource(&resource2);
135 TEST_F(TaskManagerWindowControllerTest, EnsureNewPrimarySortColumn) {
136 TaskManager task_manager;
138 // Add a couple rows of data.
139 TestResource resource1(base::UTF8ToUTF16("yyy"), 1);
140 TestResource resource2(base::UTF8ToUTF16("aaa"), 2);
142 task_manager.AddResource(&resource1);
143 task_manager.AddResource(&resource2);
145 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
146 TaskManagerWindowController* controller = bridge->cocoa_controller();
147 NSTableView* table = [controller tableView];
148 ASSERT_EQ(2, [controller numberOfRowsInTableView:table]);
150 // Locate the current first visible column.
151 NSTableColumn* firstVisibleColumn = nil;
152 for (NSTableColumn* nextColumn in [table tableColumns]) {
153 if (![nextColumn isHidden]) {
154 firstVisibleColumn = nextColumn;
158 ASSERT_TRUE(firstVisibleColumn != nil);
160 // Make the first visible column the primary sort column.
161 NSSortDescriptor* sortDescriptor =
162 [firstVisibleColumn sortDescriptorPrototype];
163 ASSERT_TRUE(sortDescriptor != nil);
164 [table setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
166 // Toggle the first visible column so that it's no longer visible, and make
167 // sure a different column is now the primary sort column.
168 NSMenuItem* menuItem = [[[NSMenuItem alloc]
169 initWithTitle:@"Temp"
170 action:@selector(toggleColumn:)
171 keyEquivalent:@""] autorelease];
172 [menuItem setRepresentedObject:firstVisibleColumn];
173 [menuItem setState:NSOnState];
174 [controller toggleColumn:menuItem];
176 NSTableColumn* newFirstVisibleColumn = nil;
177 for (NSTableColumn* nextColumn in [table tableColumns]) {
178 if (![nextColumn isHidden]) {
179 newFirstVisibleColumn = nextColumn;
183 ASSERT_TRUE(newFirstVisibleColumn != nil);
184 ASSERT_TRUE(newFirstVisibleColumn != firstVisibleColumn);
185 NSSortDescriptor* newFirstSortDescriptor =
186 [[table sortDescriptors] objectAtIndex:0];
187 EXPECT_TRUE([newFirstSortDescriptor isEqual:
188 [newFirstVisibleColumn sortDescriptorPrototype]]);
190 // Release the controller, which in turn deletes |bridge|.
193 task_manager.RemoveResource(&resource1);
194 task_manager.RemoveResource(&resource2);
197 TEST_F(TaskManagerWindowControllerTest, EnsureOneColumnVisible) {
198 TaskManager task_manager;
200 // Add a couple rows of data.
201 TestResource resource1(base::UTF8ToUTF16("yyy"), 1);
202 TestResource resource2(base::UTF8ToUTF16("aaa"), 2);
204 task_manager.AddResource(&resource1);
205 task_manager.AddResource(&resource2);
207 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
208 TaskManagerWindowController* controller = bridge->cocoa_controller();
209 NSTableView* table = [controller tableView];
210 ASSERT_EQ(2, [controller numberOfRowsInTableView:table]);
212 // Toggle each visible column so that it's not visible.
213 NSMenuItem* menuItem = [[[NSMenuItem alloc]
214 initWithTitle:@"Temp"
215 action:@selector(toggleColumn:)
216 keyEquivalent:@""] autorelease];
217 for (NSTableColumn* nextColumn in [table tableColumns]) {
218 if (![nextColumn isHidden]) {
219 [menuItem setState:NSOnState];
220 [menuItem setRepresentedObject:nextColumn];
221 [controller toggleColumn:menuItem];
225 // Locate the one column that should still be visible.
226 NSTableColumn* firstVisibleColumn = nil;
227 for (NSTableColumn* nextColumn in [table tableColumns]) {
228 if (![nextColumn isHidden]) {
229 firstVisibleColumn = nextColumn;
233 EXPECT_TRUE(firstVisibleColumn != nil);
235 // Release the controller, which in turn deletes |bridge|.
238 task_manager.RemoveResource(&resource1);
239 task_manager.RemoveResource(&resource2);