• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright (c) 2011 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/memory/scoped_nsobject.h"
9#include "base/utf_string_conversions.h"
10#import "chrome/browser/ui/cocoa/task_manager_mac.h"
11#import "chrome/browser/ui/cocoa/cocoa_test_helper.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 "third_party/skia/include/core/SkBitmap.h"
17
18namespace {
19
20class TestResource : public TaskManager::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 SkBitmap GetIcon() const OVERRIDE { return SkBitmap(); }
25  virtual base::ProcessHandle GetProcess() const OVERRIDE { return pid_; }
26  virtual Type GetType() const OVERRIDE { return RENDERER; }
27  virtual bool SupportNetworkUsage() const OVERRIDE { return false; }
28  virtual void SetSupportNetworkUsage() OVERRIDE { NOTREACHED(); }
29  virtual void Refresh() OVERRIDE {}
30  string16 title_;
31  pid_t pid_;
32};
33
34}  // namespace
35
36class TaskManagerWindowControllerTest : public CocoaTest {
37};
38
39// Test creation, to ensure nothing leaks or crashes.
40TEST_F(TaskManagerWindowControllerTest, Init) {
41  TaskManager task_manager;
42  TaskManagerMac* bridge(new TaskManagerMac(&task_manager, false));
43  TaskManagerWindowController* controller = bridge->cocoa_controller();
44
45  // Releases the controller, which in turn deletes |bridge|.
46  [controller close];
47}
48
49TEST_F(TaskManagerWindowControllerTest, Sort) {
50  TaskManager task_manager;
51
52  TestResource resource1(UTF8ToUTF16("zzz"), 1);
53  TestResource resource2(UTF8ToUTF16("zzb"), 2);
54  TestResource resource3(UTF8ToUTF16("zza"), 2);
55
56  task_manager.AddResource(&resource1);
57  task_manager.AddResource(&resource2);
58  task_manager.AddResource(&resource3);  // Will be in the same group as 2.
59
60  TaskManagerMac* bridge(new TaskManagerMac(&task_manager, false));
61  TaskManagerWindowController* controller = bridge->cocoa_controller();
62  NSTableView* table = [controller tableView];
63  ASSERT_EQ(3, [controller numberOfRowsInTableView:table]);
64
65  // Test that table is sorted on title.
66  NSTableColumn* title_column = [table tableColumnWithIdentifier:
67      [NSNumber numberWithInt:IDS_TASK_MANAGER_PAGE_COLUMN]];
68  NSCell* cell;
69  cell = [controller tableView:table dataCellForTableColumn:title_column row:0];
70  EXPECT_NSEQ(@"zzb", [cell title]);
71  cell = [controller tableView:table dataCellForTableColumn:title_column row:1];
72  EXPECT_NSEQ(@"zza", [cell title]);
73  cell = [controller tableView:table dataCellForTableColumn:title_column row:2];
74  EXPECT_NSEQ(@"zzz", [cell title]);
75
76  // Releases the controller, which in turn deletes |bridge|.
77  [controller close];
78
79  task_manager.RemoveResource(&resource1);
80  task_manager.RemoveResource(&resource2);
81  task_manager.RemoveResource(&resource3);
82}
83
84TEST_F(TaskManagerWindowControllerTest, SelectionAdaptsToSorting) {
85  TaskManager task_manager;
86
87  TestResource resource1(UTF8ToUTF16("yyy"), 1);
88  TestResource resource2(UTF8ToUTF16("aaa"), 2);
89
90  task_manager.AddResource(&resource1);
91  task_manager.AddResource(&resource2);
92
93  TaskManagerMac* bridge(new TaskManagerMac(&task_manager, false));
94  TaskManagerWindowController* controller = bridge->cocoa_controller();
95  NSTableView* table = [controller tableView];
96  ASSERT_EQ(2, [controller numberOfRowsInTableView:table]);
97
98  // Select row 0 in the table (corresponds to row 1 in the model).
99  [table  selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
100      byExtendingSelection:NO];
101
102  // Change the name of resource2 so that it becomes row 1 in the table.
103  resource2.title_ = UTF8ToUTF16("zzz");
104  bridge->OnItemsChanged(1, 1);
105
106  // Check that the selection has moved to row 1.
107  NSIndexSet* selection = [table selectedRowIndexes];
108  ASSERT_EQ(1u, [selection count]);
109  EXPECT_EQ(1u, [selection firstIndex]);
110
111  // Releases the controller, which in turn deletes |bridge|.
112  [controller close];
113
114  task_manager.RemoveResource(&resource1);
115  task_manager.RemoveResource(&resource2);
116}
117