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 "testing/gtest/include/gtest/gtest.h"
14 #import "testing/gtest_mac.h"
15 #include "testing/platform_test.h"
16 #include "ui/gfx/image/image_skia.h"
20 class TestResource : public task_manager::Resource {
22 TestResource(const base::string16& title, pid_t pid)
23 : title_(title), pid_(pid) {}
24 base::string16 GetTitle() const override { return title_; }
25 base::string16 GetProfileName() const override { return base::string16(); }
26 gfx::ImageSkia GetIcon() const override { return gfx::ImageSkia(); }
27 base::ProcessHandle GetProcess() const override { return pid_; }
28 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());
33 Type GetType() const override { return RENDERER; }
34 bool SupportNetworkUsage() const override { return false; }
35 void SetSupportNetworkUsage() override { NOTREACHED(); }
36 void Refresh() override {}
37 base::string16 title_;
38 base::string16 profile_name_;
44 class TaskManagerWindowControllerTest : public CocoaTest {
47 // Test creation, to ensure nothing leaks or crashes.
48 TEST_F(TaskManagerWindowControllerTest, Init) {
49 TaskManager task_manager;
50 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
51 TaskManagerWindowController* controller = bridge->cocoa_controller();
53 // Releases the controller, which in turn deletes |bridge|.
57 TEST_F(TaskManagerWindowControllerTest, Sort) {
58 TaskManager task_manager;
60 TestResource resource1(base::UTF8ToUTF16("zzz"), 1);
61 TestResource resource2(base::UTF8ToUTF16("zzb"), 2);
62 TestResource resource3(base::UTF8ToUTF16("zza"), 2);
64 task_manager.AddResource(&resource1);
65 task_manager.AddResource(&resource2);
66 task_manager.AddResource(&resource3); // Will be in the same group as 2.
68 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
69 TaskManagerWindowController* controller = bridge->cocoa_controller();
70 NSTableView* table = [controller tableView];
71 ASSERT_EQ(3, [controller numberOfRowsInTableView:table]);
73 // Test that table is sorted on title.
74 NSTableColumn* title_column = [table tableColumnWithIdentifier:
75 [NSString stringWithFormat:@"%d", IDS_TASK_MANAGER_TASK_COLUMN]];
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]);
84 // Releases the controller, which in turn deletes |bridge|.
87 task_manager.RemoveResource(&resource1);
88 task_manager.RemoveResource(&resource2);
89 task_manager.RemoveResource(&resource3);
92 TEST_F(TaskManagerWindowControllerTest, SelectionAdaptsToSorting) {
93 TaskManager task_manager;
95 TestResource resource1(base::UTF8ToUTF16("yyy"), 1);
96 TestResource resource2(base::UTF8ToUTF16("aaa"), 2);
98 task_manager.AddResource(&resource1);
99 task_manager.AddResource(&resource2);
101 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
102 TaskManagerWindowController* controller = bridge->cocoa_controller();
103 NSTableView* table = [controller tableView];
104 ASSERT_EQ(2, [controller numberOfRowsInTableView:table]);
106 // Select row 0 in the table (corresponds to row 1 in the model).
107 [table selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
108 byExtendingSelection:NO];
110 // Change the name of resource2 so that it becomes row 1 in the table.
111 resource2.title_ = base::UTF8ToUTF16("zzz");
112 bridge->task_manager()->model()->Refresh();
113 bridge->OnItemsChanged(1, 1);
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]);
120 // Releases the controller, which in turn deletes |bridge|.
123 task_manager.RemoveResource(&resource1);
124 task_manager.RemoveResource(&resource2);