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"
21 class TestResource : public task_manager::Resource {
23 TestResource(const base::string16& title, pid_t pid)
24 : title_(title), pid_(pid) {}
25 base::string16 GetTitle() const override { return title_; }
26 base::string16 GetProfileName() const override { return base::string16(); }
27 gfx::ImageSkia GetIcon() const override { return gfx::ImageSkia(); }
28 base::ProcessHandle GetProcess() const override { return pid_; }
29 int GetUniqueChildProcessId() const override {
30 // In reality the unique child process ID is not the actual process ID,
31 // but for testing purposes it shouldn't make difference.
32 return static_cast<int>(base::GetCurrentProcId());
34 Type GetType() const override { return RENDERER; }
35 bool SupportNetworkUsage() const override { return false; }
36 void SetSupportNetworkUsage() override { NOTREACHED(); }
37 void Refresh() override {}
38 base::string16 title_;
39 base::string16 profile_name_;
45 class TaskManagerWindowControllerTest : public CocoaTest {
46 content::TestBrowserThreadBundle thread_bundle_;
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);