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