| OLD | NEW |
| (Empty) |
| 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 "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" | |
| 18 | |
| 19 @interface TaskManagerWindowController(UnitTest) | |
| 20 | |
| 21 - (void)toggleColumn:(NSMenuItem*)sender; | |
| 22 | |
| 23 @end | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 class TestResource : public task_manager::Resource { | |
| 28 public: | |
| 29 TestResource(const base::string16& title, pid_t pid) | |
| 30 : title_(title), pid_(pid) {} | |
| 31 base::string16 GetTitle() const override { return title_; } | |
| 32 base::string16 GetProfileName() const override { return base::string16(); } | |
| 33 gfx::ImageSkia GetIcon() const override { return gfx::ImageSkia(); } | |
| 34 base::ProcessHandle GetProcess() const override { return pid_; } | |
| 35 int GetUniqueChildProcessId() const override { | |
| 36 // In reality the unique child process ID is not the actual process ID, | |
| 37 // but for testing purposes it shouldn't make difference. | |
| 38 return static_cast<int>(base::GetCurrentProcId()); | |
| 39 } | |
| 40 Type GetType() const override { return RENDERER; } | |
| 41 bool SupportNetworkUsage() const override { return false; } | |
| 42 void SetSupportNetworkUsage() override { NOTREACHED(); } | |
| 43 void Refresh() override {} | |
| 44 base::string16 title_; | |
| 45 base::string16 profile_name_; | |
| 46 pid_t pid_; | |
| 47 }; | |
| 48 | |
| 49 } // namespace | |
| 50 | |
| 51 class TaskManagerWindowControllerTest : public CocoaTest { | |
| 52 content::TestBrowserThreadBundle thread_bundle_; | |
| 53 }; | |
| 54 | |
| 55 // Test creation, to ensure nothing leaks or crashes. | |
| 56 TEST_F(TaskManagerWindowControllerTest, Init) { | |
| 57 TaskManager task_manager; | |
| 58 TaskManagerMac* bridge(new TaskManagerMac(&task_manager)); | |
| 59 TaskManagerWindowController* controller = bridge->cocoa_controller(); | |
| 60 | |
| 61 // Releases the controller, which in turn deletes |bridge|. | |
| 62 [controller close]; | |
| 63 } | |
| 64 | |
| 65 TEST_F(TaskManagerWindowControllerTest, Sort) { | |
| 66 TaskManager task_manager; | |
| 67 | |
| 68 TestResource resource1(base::UTF8ToUTF16("zzz"), 1); | |
| 69 TestResource resource2(base::UTF8ToUTF16("zzb"), 2); | |
| 70 TestResource resource3(base::UTF8ToUTF16("zza"), 2); | |
| 71 | |
| 72 task_manager.AddResource(&resource1); | |
| 73 task_manager.AddResource(&resource2); | |
| 74 task_manager.AddResource(&resource3); // Will be in the same group as 2. | |
| 75 | |
| 76 TaskManagerMac* bridge(new TaskManagerMac(&task_manager)); | |
| 77 TaskManagerWindowController* controller = bridge->cocoa_controller(); | |
| 78 NSTableView* table = [controller tableView]; | |
| 79 ASSERT_EQ(3, [controller numberOfRowsInTableView:table]); | |
| 80 | |
| 81 // Test that table is sorted on title. | |
| 82 NSTableColumn* title_column = [table tableColumnWithIdentifier: | |
| 83 [NSString stringWithFormat:@"%d", IDS_TASK_MANAGER_TASK_COLUMN]]; | |
| 84 NSCell* cell; | |
| 85 cell = [controller tableView:table dataCellForTableColumn:title_column row:0]; | |
| 86 EXPECT_NSEQ(@"zzb", [cell title]); | |
| 87 cell = [controller tableView:table dataCellForTableColumn:title_column row:1]; | |
| 88 EXPECT_NSEQ(@"zza", [cell title]); | |
| 89 cell = [controller tableView:table dataCellForTableColumn:title_column row:2]; | |
| 90 EXPECT_NSEQ(@"zzz", [cell title]); | |
| 91 | |
| 92 // Releases the controller, which in turn deletes |bridge|. | |
| 93 [controller close]; | |
| 94 | |
| 95 task_manager.RemoveResource(&resource1); | |
| 96 task_manager.RemoveResource(&resource2); | |
| 97 task_manager.RemoveResource(&resource3); | |
| 98 } | |
| 99 | |
| 100 TEST_F(TaskManagerWindowControllerTest, SelectionAdaptsToSorting) { | |
| 101 TaskManager task_manager; | |
| 102 | |
| 103 TestResource resource1(base::UTF8ToUTF16("yyy"), 1); | |
| 104 TestResource resource2(base::UTF8ToUTF16("aaa"), 2); | |
| 105 | |
| 106 task_manager.AddResource(&resource1); | |
| 107 task_manager.AddResource(&resource2); | |
| 108 | |
| 109 TaskManagerMac* bridge(new TaskManagerMac(&task_manager)); | |
| 110 TaskManagerWindowController* controller = bridge->cocoa_controller(); | |
| 111 NSTableView* table = [controller tableView]; | |
| 112 ASSERT_EQ(2, [controller numberOfRowsInTableView:table]); | |
| 113 | |
| 114 // Select row 0 in the table (corresponds to row 1 in the model). | |
| 115 [table selectRowIndexes:[NSIndexSet indexSetWithIndex:0] | |
| 116 byExtendingSelection:NO]; | |
| 117 | |
| 118 // Change the name of resource2 so that it becomes row 1 in the table. | |
| 119 resource2.title_ = base::UTF8ToUTF16("zzz"); | |
| 120 bridge->task_manager()->model()->Refresh(); | |
| 121 bridge->OnItemsChanged(1, 1); | |
| 122 | |
| 123 // Check that the selection has moved to row 1. | |
| 124 NSIndexSet* selection = [table selectedRowIndexes]; | |
| 125 ASSERT_EQ(1u, [selection count]); | |
| 126 EXPECT_EQ(1u, [selection firstIndex]); | |
| 127 | |
| 128 // Releases the controller, which in turn deletes |bridge|. | |
| 129 [controller close]; | |
| 130 | |
| 131 task_manager.RemoveResource(&resource1); | |
| 132 task_manager.RemoveResource(&resource2); | |
| 133 } | |
| 134 | |
| 135 TEST_F(TaskManagerWindowControllerTest, EnsureNewPrimarySortColumn) { | |
| 136 TaskManager task_manager; | |
| 137 | |
| 138 // Add a couple rows of data. | |
| 139 TestResource resource1(base::UTF8ToUTF16("yyy"), 1); | |
| 140 TestResource resource2(base::UTF8ToUTF16("aaa"), 2); | |
| 141 | |
| 142 task_manager.AddResource(&resource1); | |
| 143 task_manager.AddResource(&resource2); | |
| 144 | |
| 145 TaskManagerMac* bridge(new TaskManagerMac(&task_manager)); | |
| 146 TaskManagerWindowController* controller = bridge->cocoa_controller(); | |
| 147 NSTableView* table = [controller tableView]; | |
| 148 ASSERT_EQ(2, [controller numberOfRowsInTableView:table]); | |
| 149 | |
| 150 // Locate the current first visible column. | |
| 151 NSTableColumn* firstVisibleColumn = nil; | |
| 152 for (NSTableColumn* nextColumn in [table tableColumns]) { | |
| 153 if (![nextColumn isHidden]) { | |
| 154 firstVisibleColumn = nextColumn; | |
| 155 break; | |
| 156 } | |
| 157 } | |
| 158 ASSERT_TRUE(firstVisibleColumn != nil); | |
| 159 | |
| 160 // Make the first visible column the primary sort column. | |
| 161 NSSortDescriptor* sortDescriptor = | |
| 162 [firstVisibleColumn sortDescriptorPrototype]; | |
| 163 ASSERT_TRUE(sortDescriptor != nil); | |
| 164 [table setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]]; | |
| 165 | |
| 166 // Toggle the first visible column so that it's no longer visible, and make | |
| 167 // sure a different column is now the primary sort column. | |
| 168 NSMenuItem* menuItem = [[[NSMenuItem alloc] | |
| 169 initWithTitle:@"Temp" | |
| 170 action:@selector(toggleColumn:) | |
| 171 keyEquivalent:@""] autorelease]; | |
| 172 [menuItem setRepresentedObject:firstVisibleColumn]; | |
| 173 [menuItem setState:NSOnState]; | |
| 174 [controller toggleColumn:menuItem]; | |
| 175 | |
| 176 NSTableColumn* newFirstVisibleColumn = nil; | |
| 177 for (NSTableColumn* nextColumn in [table tableColumns]) { | |
| 178 if (![nextColumn isHidden]) { | |
| 179 newFirstVisibleColumn = nextColumn; | |
| 180 break; | |
| 181 } | |
| 182 } | |
| 183 ASSERT_TRUE(newFirstVisibleColumn != nil); | |
| 184 ASSERT_TRUE(newFirstVisibleColumn != firstVisibleColumn); | |
| 185 NSSortDescriptor* newFirstSortDescriptor = | |
| 186 [[table sortDescriptors] objectAtIndex:0]; | |
| 187 EXPECT_TRUE([newFirstSortDescriptor isEqual: | |
| 188 [newFirstVisibleColumn sortDescriptorPrototype]]); | |
| 189 | |
| 190 // Release the controller, which in turn deletes |bridge|. | |
| 191 [controller close]; | |
| 192 | |
| 193 task_manager.RemoveResource(&resource1); | |
| 194 task_manager.RemoveResource(&resource2); | |
| 195 } | |
| 196 | |
| 197 TEST_F(TaskManagerWindowControllerTest, EnsureOneColumnVisible) { | |
| 198 TaskManager task_manager; | |
| 199 | |
| 200 // Add a couple rows of data. | |
| 201 TestResource resource1(base::UTF8ToUTF16("yyy"), 1); | |
| 202 TestResource resource2(base::UTF8ToUTF16("aaa"), 2); | |
| 203 | |
| 204 task_manager.AddResource(&resource1); | |
| 205 task_manager.AddResource(&resource2); | |
| 206 | |
| 207 TaskManagerMac* bridge(new TaskManagerMac(&task_manager)); | |
| 208 TaskManagerWindowController* controller = bridge->cocoa_controller(); | |
| 209 NSTableView* table = [controller tableView]; | |
| 210 ASSERT_EQ(2, [controller numberOfRowsInTableView:table]); | |
| 211 | |
| 212 // Toggle each visible column so that it's not visible. | |
| 213 NSMenuItem* menuItem = [[[NSMenuItem alloc] | |
| 214 initWithTitle:@"Temp" | |
| 215 action:@selector(toggleColumn:) | |
| 216 keyEquivalent:@""] autorelease]; | |
| 217 for (NSTableColumn* nextColumn in [table tableColumns]) { | |
| 218 if (![nextColumn isHidden]) { | |
| 219 [menuItem setState:NSOnState]; | |
| 220 [menuItem setRepresentedObject:nextColumn]; | |
| 221 [controller toggleColumn:menuItem]; | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 // Locate the one column that should still be visible. | |
| 226 NSTableColumn* firstVisibleColumn = nil; | |
| 227 for (NSTableColumn* nextColumn in [table tableColumns]) { | |
| 228 if (![nextColumn isHidden]) { | |
| 229 firstVisibleColumn = nextColumn; | |
| 230 break; | |
| 231 } | |
| 232 } | |
| 233 EXPECT_TRUE(firstVisibleColumn != nil); | |
| 234 | |
| 235 // Release the controller, which in turn deletes |bridge|. | |
| 236 [controller close]; | |
| 237 | |
| 238 task_manager.RemoveResource(&resource1); | |
| 239 task_manager.RemoveResource(&resource2); | |
| 240 } | |
| OLD | NEW |