- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / task_manager_mac_unittest.mm
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.
4
5 #import <Cocoa/Cocoa.h>
6
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"
17
18 namespace {
19
20 class TestResource : public task_manager::Resource {
21  public:
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());
31   }
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 {}
36   string16 title_;
37   string16 profile_name_;
38   pid_t pid_;
39 };
40
41 }  // namespace
42
43 class TaskManagerWindowControllerTest : public CocoaTest {
44 };
45
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();
51
52   // Releases the controller, which in turn deletes |bridge|.
53   [controller close];
54 }
55
56 TEST_F(TaskManagerWindowControllerTest, Sort) {
57   TaskManager task_manager;
58
59   TestResource resource1(UTF8ToUTF16("zzz"), 1);
60   TestResource resource2(UTF8ToUTF16("zzb"), 2);
61   TestResource resource3(UTF8ToUTF16("zza"), 2);
62
63   task_manager.AddResource(&resource1);
64   task_manager.AddResource(&resource2);
65   task_manager.AddResource(&resource3);  // Will be in the same group as 2.
66
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]);
71
72   // Test that table is sorted on title.
73   NSTableColumn* title_column = [table tableColumnWithIdentifier:
74       [NSString stringWithFormat:@"%d", IDS_TASK_MANAGER_TASK_COLUMN]];
75   NSCell* cell;
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]);
82
83   // Releases the controller, which in turn deletes |bridge|.
84   [controller close];
85
86   task_manager.RemoveResource(&resource1);
87   task_manager.RemoveResource(&resource2);
88   task_manager.RemoveResource(&resource3);
89 }
90
91 TEST_F(TaskManagerWindowControllerTest, SelectionAdaptsToSorting) {
92   TaskManager task_manager;
93
94   TestResource resource1(UTF8ToUTF16("yyy"), 1);
95   TestResource resource2(UTF8ToUTF16("aaa"), 2);
96
97   task_manager.AddResource(&resource1);
98   task_manager.AddResource(&resource2);
99
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]);
104
105   // Select row 0 in the table (corresponds to row 1 in the model).
106   [table selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
107      byExtendingSelection:NO];
108
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);
113
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]);
118
119   // Releases the controller, which in turn deletes |bridge|.
120   [controller close];
121
122   task_manager.RemoveResource(&resource1);
123   task_manager.RemoveResource(&resource2);
124 }