• 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#import "chrome/browser/ui/cocoa/tab_view_picker_table.h"
8
9#import "base/mac/cocoa_protocols.h"
10#include "base/memory/scoped_nsobject.h"
11#import "chrome/browser/ui/cocoa/cocoa_test_helper.h"
12#include "testing/gtest/include/gtest/gtest.h"
13#import "testing/gtest_mac.h"
14#include "testing/platform_test.h"
15
16@interface TabViewPickerTableTestPing : NSObject <NSTabViewDelegate> {
17 @public
18  BOOL didSelectItemCalled_;
19}
20@end
21
22@implementation TabViewPickerTableTestPing
23- (void)         tabView:(NSTabView*)tabView
24    didSelectTabViewItem:(NSTabViewItem*)tabViewItem {
25  didSelectItemCalled_ = YES;
26}
27@end
28
29namespace {
30
31class TabViewPickerTableTest : public CocoaTest {
32 public:
33  TabViewPickerTableTest() {
34    // Initialize picker table.
35    NSRect frame = NSMakeRect(0, 0, 30, 50);
36    scoped_nsobject<TabViewPickerTable> view(
37        [[TabViewPickerTable alloc] initWithFrame:frame]);
38    view_ = view.get();
39    [view_ setAllowsEmptySelection:NO];
40    [view_ setAllowsMultipleSelection:NO];
41    [view_ addTableColumn:
42        [[[NSTableColumn alloc] initWithIdentifier:nil] autorelease]];
43    [[test_window() contentView] addSubview:view_];
44
45    // Initialize source tab view, with delegate.
46    frame = NSMakeRect(30, 0, 50, 50);
47    scoped_nsobject<NSTabView> tabView(
48        [[NSTabView alloc] initWithFrame:frame]);
49    tabView_ = tabView.get();
50
51    scoped_nsobject<NSTabViewItem> item1(
52        [[NSTabViewItem alloc] initWithIdentifier:nil]);
53    [item1 setLabel:@"label 1"];
54    [tabView_ addTabViewItem:item1];
55
56    scoped_nsobject<NSTabViewItem> item2(
57        [[NSTabViewItem alloc] initWithIdentifier:nil]);
58    [item2 setLabel:@"label 2"];
59    [tabView_ addTabViewItem:item2];
60
61    [tabView_ selectTabViewItemAtIndex:1];
62    [[test_window() contentView] addSubview:tabView_];
63
64    ping_.reset([TabViewPickerTableTestPing new]);
65    [tabView_ setDelegate:ping_.get()];
66
67    // Simulate nib loading.
68    view_->tabView_ = tabView_;
69    [view_ awakeFromNib];
70  }
71
72  TabViewPickerTable* view_;
73  NSTabView* tabView_;
74  scoped_nsobject<TabViewPickerTableTestPing> ping_;
75};
76
77TEST_VIEW(TabViewPickerTableTest, view_)
78
79TEST_F(TabViewPickerTableTest, TestInitialSelectionCorrect) {
80  EXPECT_EQ(1, [view_ selectedRow]);
81}
82
83TEST_F(TabViewPickerTableTest, TestSelectionUpdates) {
84  [tabView_ selectTabViewItemAtIndex:0];
85  EXPECT_EQ(0, [view_ selectedRow]);
86
87  [tabView_ selectTabViewItemAtIndex:1];
88  EXPECT_EQ(1, [view_ selectedRow]);
89}
90
91TEST_F(TabViewPickerTableTest, TestDelegateStillWorks) {
92  EXPECT_FALSE(ping_.get()->didSelectItemCalled_);
93  [tabView_ selectTabViewItemAtIndex:0];
94  EXPECT_TRUE(ping_.get()->didSelectItemCalled_);
95}
96
97TEST_F(TabViewPickerTableTest, RowsCorrect) {
98  EXPECT_EQ(2, [view_ numberOfRows]);
99  EXPECT_EQ(2,
100            [[view_ dataSource] outlineView:view_ numberOfChildrenOfItem:nil]);
101
102  id item;
103  item = [[view_ dataSource] outlineView:view_ child:0 ofItem:nil];
104  EXPECT_NSEQ(@"label 1",
105              [[view_ dataSource] outlineView:view_
106                    objectValueForTableColumn:nil  // ignored
107                                       byItem:item]);
108  item = [[view_ dataSource] outlineView:view_ child:1 ofItem:nil];
109  EXPECT_NSEQ(@"label 2",
110              [[view_ dataSource] outlineView:view_
111                    objectValueForTableColumn:nil  // ignored
112                                       byItem:item]);
113}
114
115TEST_F(TabViewPickerTableTest, TestListUpdatesTabView) {
116  [view_   selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
117      byExtendingSelection:NO];
118  EXPECT_EQ(0, [view_ selectedRow]);  // sanity
119  EXPECT_EQ(0, [tabView_ indexOfTabViewItem:[tabView_ selectedTabViewItem]]);
120}
121
122TEST_F(TabViewPickerTableTest, TestWithHeadingNotEmpty) {
123  [view_ setHeading:@"disregard this"];
124
125  EXPECT_EQ(2, [view_ selectedRow]);
126
127  [tabView_ selectTabViewItemAtIndex:0];
128  EXPECT_EQ(1, [view_ selectedRow]);
129  [tabView_ selectTabViewItemAtIndex:1];
130  EXPECT_EQ(2, [view_ selectedRow]);
131
132  [view_   selectRowIndexes:[NSIndexSet indexSetWithIndex:1]
133      byExtendingSelection:NO];
134  EXPECT_EQ(1, [view_ selectedRow]);  // sanity
135  EXPECT_EQ(0, [tabView_ indexOfTabViewItem:[tabView_ selectedTabViewItem]]);
136}
137
138}  // namespace
139