summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/tab_view_picker_table_unittest.mm
blob: 08459d2da37e8a4ba0566ebee6bf4dee68acbee4 (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
116
117
// Copyright (c) 2010 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>

#import "chrome/browser/cocoa/tab_view_picker_table.h"

#import "base/cocoa_protocols_mac.h"
#include "base/scoped_nsobject.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"

@interface TabViewPickerTableTestPing : NSObject <NSTabViewDelegate> {
 @public
  BOOL didSelectItemCalled_;
}
@end

@implementation TabViewPickerTableTestPing
- (void)         tabView:(NSTabView*)tabView
    didSelectTabViewItem:(NSTabViewItem*)tabViewItem {
  didSelectItemCalled_ = YES;
}
@end

namespace {

class TabViewPickerTableTest : public CocoaTest {
 public:
  TabViewPickerTableTest() {
    // Initialize picker table.
    NSRect frame = NSMakeRect(0, 0, 30, 50);
    scoped_nsobject<TabViewPickerTable> view(
        [[TabViewPickerTable alloc] initWithFrame:frame]);
    view_ = view.get();
    [view_ setAllowsEmptySelection:NO];
    [view_ setAllowsMultipleSelection:NO];
    [view_ addTableColumn:
        [[[NSTableColumn alloc] initWithIdentifier:nil] autorelease]];
    [[test_window() contentView] addSubview:view_];

    // Initialize source tab view, with delegate.
    frame = NSMakeRect(30, 0, 50, 50);
    scoped_nsobject<NSTabView> tabView(
        [[NSTabView alloc] initWithFrame:frame]);
    tabView_ = tabView.get();

    scoped_nsobject<NSTabViewItem> item1(
        [[NSTabViewItem alloc] initWithIdentifier:nil]);
    [item1 setLabel:@"label 1"];
    [tabView_ addTabViewItem:item1];

    scoped_nsobject<NSTabViewItem> item2(
        [[NSTabViewItem alloc] initWithIdentifier:nil]);
    [item2 setLabel:@"label 2"];
    [tabView_ addTabViewItem:item2];

    [tabView_ selectTabViewItemAtIndex:1];
    [[test_window() contentView] addSubview:tabView_];

    ping_.reset([TabViewPickerTableTestPing new]);
    [tabView_ setDelegate:ping_.get()];

    // Simulate nib loading.
    view_->tabView_ = tabView_;
    [view_ awakeFromNib];
  }

  TabViewPickerTable* view_;
  NSTabView* tabView_;
  scoped_nsobject<TabViewPickerTableTestPing> ping_;
};

TEST_VIEW(TabViewPickerTableTest, view_)

TEST_F(TabViewPickerTableTest, TestInitialSelectionCorrect) {
  EXPECT_EQ(1, [view_ selectedRow]);
}

TEST_F(TabViewPickerTableTest, TestSelectionUpdates) {
  [tabView_ selectTabViewItemAtIndex:0];
  EXPECT_EQ(0, [view_ selectedRow]);

  [tabView_ selectTabViewItemAtIndex:1];
  EXPECT_EQ(1, [view_ selectedRow]);
}

TEST_F(TabViewPickerTableTest, TestDelegateStillWorks) {
  EXPECT_FALSE(ping_.get()->didSelectItemCalled_);
  [tabView_ selectTabViewItemAtIndex:0];
  EXPECT_TRUE(ping_.get()->didSelectItemCalled_);
}

TEST_F(TabViewPickerTableTest, RowsCorrect) {
  EXPECT_EQ(2, [view_ numberOfRows]);
  EXPECT_EQ(2, [[view_ dataSource] numberOfRowsInTableView:view_]);

  EXPECT_TRUE([@"label 1" isEqualToString:[[view_ dataSource]
                      tableView:view_
      objectValueForTableColumn:nil  // ignored
                            row:0]]);
  EXPECT_TRUE([@"label 2" isEqualToString:[[view_ dataSource]
                      tableView:view_
      objectValueForTableColumn:nil  // ignored
                            row:1]]);
}

TEST_F(TabViewPickerTableTest, TestListUpdatesTabView) {
  [view_   selectRowIndexes:[NSIndexSet indexSetWithIndex:0]
      byExtendingSelection:NO];
  EXPECT_EQ(0, [view_ selectedRow]);  // sanity
  EXPECT_EQ(0, [tabView_ indexOfTabViewItem:[tabView_ selectedTabViewItem]]);
}

}  // namespace