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 "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 virtual base::string16 GetTitle() const OVERRIDE { return title_; }
25 virtual base::string16 GetProfileName() const OVERRIDE {
26 return base::string16();
28 virtual gfx::ImageSkia GetIcon() const OVERRIDE { return gfx::ImageSkia(); }
29 virtual base::ProcessHandle GetProcess() const OVERRIDE { return pid_; }
30 virtual int GetUniqueChildProcessId() const OVERRIDE {
31 // In reality the unique child process ID is not the actual process ID,
32 // but for testing purposes it shouldn't make difference.
33 return static_cast<int>(base::GetCurrentProcId());
35 virtual Type GetType() const OVERRIDE { return RENDERER; }
36 virtual bool SupportNetworkUsage() const OVERRIDE { return false; }
37 virtual void SetSupportNetworkUsage() OVERRIDE { NOTREACHED(); }
38 virtual void Refresh() OVERRIDE {}
39 base::string16 title_;
40 base::string16 profile_name_;
46 class TaskManagerWindowControllerTest : public CocoaTest {
49 // Test creation, to ensure nothing leaks or crashes.
50 TEST_F(TaskManagerWindowControllerTest, Init) {
51 TaskManager task_manager;
52 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
53 TaskManagerWindowController* controller = bridge->cocoa_controller();
55 // Releases the controller, which in turn deletes |bridge|.
59 TEST_F(TaskManagerWindowControllerTest, Sort) {
60 TaskManager task_manager;
62 TestResource resource1(base::UTF8ToUTF16("zzz"), 1);
63 TestResource resource2(base::UTF8ToUTF16("zzb"), 2);
64 TestResource resource3(base::UTF8ToUTF16("zza"), 2);
66 task_manager.AddResource(&resource1);
67 task_manager.AddResource(&resource2);
68 task_manager.AddResource(&resource3); // Will be in the same group as 2.
70 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
71 TaskManagerWindowController* controller = bridge->cocoa_controller();
72 NSTableView* table = [controller tableView];
73 ASSERT_EQ(3, [controller numberOfRowsInTableView:table]);
75 // Test that table is sorted on title.
76 NSTableColumn* title_column = [table tableColumnWithIdentifier:
77 [NSString stringWithFormat:@"%d", IDS_TASK_MANAGER_TASK_COLUMN]];
79 cell = [controller tableView:table dataCellForTableColumn:title_column row:0];
80 EXPECT_NSEQ(@"zzb", [cell title]);
81 cell = [controller tableView:table dataCellForTableColumn:title_column row:1];
82 EXPECT_NSEQ(@"zza", [cell title]);
83 cell = [controller tableView:table dataCellForTableColumn:title_column row:2];
84 EXPECT_NSEQ(@"zzz", [cell title]);
86 // Releases the controller, which in turn deletes |bridge|.
89 task_manager.RemoveResource(&resource1);
90 task_manager.RemoveResource(&resource2);
91 task_manager.RemoveResource(&resource3);
94 TEST_F(TaskManagerWindowControllerTest, SelectionAdaptsToSorting) {
95 TaskManager task_manager;
97 TestResource resource1(base::UTF8ToUTF16("yyy"), 1);
98 TestResource resource2(base::UTF8ToUTF16("aaa"), 2);
100 task_manager.AddResource(&resource1);
101 task_manager.AddResource(&resource2);
103 TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
104 TaskManagerWindowController* controller = bridge->cocoa_controller();
105 NSTableView* table = [controller tableView];
106 ASSERT_EQ(2, [controller numberOfRowsInTableView:table]);
108 // Select row 0 in the table (corresponds to row 1 in the model).
109 [table selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
110 byExtendingSelection:NO];
112 // Change the name of resource2 so that it becomes row 1 in the table.
113 resource2.title_ = base::UTF8ToUTF16("zzz");
114 bridge->task_manager()->model()->Refresh();
115 bridge->OnItemsChanged(1, 1);
117 // Check that the selection has moved to row 1.
118 NSIndexSet* selection = [table selectedRowIndexes];
119 ASSERT_EQ(1u, [selection count]);
120 EXPECT_EQ(1u, [selection firstIndex]);
122 // Releases the controller, which in turn deletes |bridge|.
125 task_manager.RemoveResource(&resource1);
126 task_manager.RemoveResource(&resource2);