summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/task_manager_mac_unittest.mm
blob: c82928bd5fb9d758f3d252f20f3a82dc6aadb121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import <Cocoa/Cocoa.h>

#include "base/scoped_nsobject.h"
#include "base/utf_string_conversions.h"
#import "chrome/browser/cocoa/task_manager_mac.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#include "grit/generated_resources.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#include "third_party/skia/include/core/SkBitmap.h"

namespace {

class TestResource : public TaskManager::Resource {
 public:
  TestResource(const string16& title, pid_t pid) : title_(title), pid_(pid) {}
  virtual std::wstring GetTitle() const { return UTF16ToWide(title_); }
  virtual SkBitmap GetIcon() const { return SkBitmap(); }
  virtual base::ProcessHandle GetProcess() const { return pid_; }
  virtual Type GetType() const { return RENDERER; }
  virtual bool SupportNetworkUsage() const { return false; }
  virtual void SetSupportNetworkUsage() { NOTREACHED(); }
  virtual void Refresh() {}
  string16 title_;
  pid_t pid_;
};

}  // namespace

class TaskManagerWindowControllerTest : public CocoaTest {
};

// Test creation, to ensure nothing leaks or crashes.
TEST_F(TaskManagerWindowControllerTest, Init) {
  TaskManager task_manager;
  TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
  TaskManagerWindowController* controller = bridge->cocoa_controller();

  // Releases the controller, which in turn deletes |bridge|.
  [controller close];
}

TEST_F(TaskManagerWindowControllerTest, Sort) {
  TaskManager task_manager;

  TestResource resource1(UTF8ToUTF16("zzz"), 1);
  TestResource resource2(UTF8ToUTF16("zzb"), 2);
  TestResource resource3(UTF8ToUTF16("zza"), 2);

  task_manager.AddResource(&resource1);
  task_manager.AddResource(&resource2);
  task_manager.AddResource(&resource3);  // Will be in the same group as 2.

  TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
  TaskManagerWindowController* controller = bridge->cocoa_controller();
  NSTableView* table = [controller tableView];
  ASSERT_EQ(3, [controller numberOfRowsInTableView:table]);

  // Test that table is sorted on title.
  NSTableColumn* title_column = [table tableColumnWithIdentifier:
      [NSNumber numberWithInt:IDS_TASK_MANAGER_PAGE_COLUMN]];
  NSCell* cell;
  cell = [controller tableView:table dataCellForTableColumn:title_column row:0];
  EXPECT_NSEQ(@"zzb", [cell title]);
  cell = [controller tableView:table dataCellForTableColumn:title_column row:1];
  EXPECT_NSEQ(@"zza", [cell title]);
  cell = [controller tableView:table dataCellForTableColumn:title_column row:2];
  EXPECT_NSEQ(@"zzz", [cell title]);

  // Releases the controller, which in turn deletes |bridge|.
  [controller close];

  task_manager.RemoveResource(&resource1);
  task_manager.RemoveResource(&resource2);
  task_manager.RemoveResource(&resource3);
}

TEST_F(TaskManagerWindowControllerTest, SelectionAdaptsToSorting) {
  TaskManager task_manager;

  TestResource resource1(UTF8ToUTF16("yyy"), 1);
  TestResource resource2(UTF8ToUTF16("aaa"), 2);

  task_manager.AddResource(&resource1);
  task_manager.AddResource(&resource2);

  TaskManagerMac* bridge(new TaskManagerMac(&task_manager));
  TaskManagerWindowController* controller = bridge->cocoa_controller();
  NSTableView* table = [controller tableView];
  ASSERT_EQ(2, [controller numberOfRowsInTableView:table]);

  // Select row 0 in the table (corresponds to row 1 in the model).
  [table  selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
      byExtendingSelection:NO];

  // Change the name of resource2 so that it becomes row 1 in the table.
  resource2.title_ = UTF8ToUTF16("zzz");
  bridge->OnItemsChanged(1, 1);

  // Check that the selection has moved to row 1.
  NSIndexSet* selection = [table selectedRowIndexes];
  ASSERT_EQ(1u, [selection count]);
  EXPECT_EQ(1u, [selection firstIndex]);

  // Releases the controller, which in turn deletes |bridge|.
  [controller close];

  task_manager.RemoveResource(&resource1);
  task_manager.RemoveResource(&resource2);
}