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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
// Copyright (c) 2011 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 <vector>
#import "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/cocoa/cocoa_profile_test.h"
#import "chrome/browser/ui/cocoa/new_tab_button.h"
#import "chrome/browser/ui/cocoa/tabs/tab_strip_controller.h"
#import "chrome/browser/ui/cocoa/tabs/tab_strip_view.h"
#import "chrome/browser/ui/cocoa/tabs/tab_view.h"
#import "chrome/browser/ui/cocoa/tabs/tab_controller.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "content/browser/site_instance.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
@interface TestTabStripControllerDelegate
: NSObject<TabStripControllerDelegate> {
}
@end
@implementation TestTabStripControllerDelegate
- (void)onActivateTabWithContents:(TabContents*)contents {
}
- (void)onReplaceTabWithContents:(TabContents*)contents {
}
- (void)onTabChanged:(TabStripModelObserver::TabChangeType)change
withContents:(TabContents*)contents {
}
- (void)onTabDetachedWithContents:(TabContents*)contents {
}
@end
@interface TabStripController (Test)
- (void)mouseMoved:(NSEvent*)event;
@end
@implementation TabView (Test)
- (TabController*)getController {
return controller_;
}
@end
namespace {
// Stub model delegate
class TestTabStripDelegate : public TabStripModelDelegate {
public:
virtual TabContentsWrapper* AddBlankTab(bool foreground) {
return NULL;
}
virtual TabContentsWrapper* AddBlankTabAt(int index, bool foreground) {
return NULL;
}
virtual Browser* CreateNewStripWithContents(TabContentsWrapper* contents,
const gfx::Rect& window_bounds,
const DockInfo& dock_info,
bool maximize) {
return NULL;
}
virtual void ContinueDraggingDetachedTab(TabContentsWrapper* contents,
const gfx::Rect& window_bounds,
const gfx::Rect& tab_bounds) {
}
virtual int GetDragActions() const {
return 0;
}
virtual TabContentsWrapper* CreateTabContentsForURL(
const GURL& url,
const content::Referrer& referrer,
Profile* profile,
content::PageTransition transition,
bool defer_load,
SiteInstance* instance) const {
return NULL;
}
virtual bool CanDuplicateContentsAt(int index) { return true; }
virtual void DuplicateContentsAt(int index) { }
virtual void CloseFrameAfterDragSession() { }
virtual void CreateHistoricalTab(TabContentsWrapper* contents) { }
virtual bool RunUnloadListenerBeforeClosing(TabContentsWrapper* contents) {
return true;
}
virtual bool CanRestoreTab() {
return true;
}
virtual void RestoreTab() {}
virtual bool CanCloseContents(std::vector<int>* indices) { return true; }
virtual bool CanBookmarkAllTabs() const { return false; }
virtual bool CanCloseTab() const { return true; }
virtual void BookmarkAllTabs() {}
virtual bool LargeIconsPermitted() const { return true; }
};
class TabStripControllerTest : public CocoaProfileTest {
public:
virtual void SetUp() {
CocoaProfileTest::SetUp();
ASSERT_TRUE(browser());
BrowserWindow* browser_window = CreateBrowserWindow();
NSWindow* window = browser_window->GetNativeHandle();
NSView* parent = [window contentView];
NSRect content_frame = [parent frame];
// Create the "switch view" (view that gets changed out when a tab
// switches).
NSRect switch_frame = NSMakeRect(0, 0, content_frame.size.width, 500);
scoped_nsobject<NSView> switch_view(
[[NSView alloc] initWithFrame:switch_frame]);
[parent addSubview:switch_view.get()];
// Create the tab strip view. It's expected to have a child button in it
// already as the "new tab" button so create that too.
NSRect strip_frame = NSMakeRect(0, NSMaxY(switch_frame),
content_frame.size.width, 30);
tab_strip_.reset(
[[TabStripView alloc] initWithFrame:strip_frame]);
[parent addSubview:tab_strip_.get()];
NSRect button_frame = NSMakeRect(0, 0, 15, 15);
scoped_nsobject<NewTabButton> new_tab_button(
[[NewTabButton alloc] initWithFrame:button_frame]);
[tab_strip_ addSubview:new_tab_button.get()];
[tab_strip_ setNewTabButton:new_tab_button.get()];
delegate_.reset(new TestTabStripDelegate());
model_ = browser()->tabstrip_model();
controller_delegate_.reset([TestTabStripControllerDelegate alloc]);
controller_.reset([[TabStripController alloc]
initWithView:static_cast<TabStripView*>(tab_strip_.get())
switchView:switch_view.get()
browser:browser()
delegate:controller_delegate_.get()]);
}
virtual void TearDown() {
// The call to CocoaTest::TearDown() deletes the Browser and TabStripModel
// objects, so we first have to delete the controller, which refers to them.
controller_.reset();
model_ = NULL;
CocoaProfileTest::TearDown();
}
scoped_ptr<TestTabStripDelegate> delegate_;
TabStripModel* model_;
scoped_nsobject<TestTabStripControllerDelegate> controller_delegate_;
scoped_nsobject<TabStripController> controller_;
scoped_nsobject<TabStripView> tab_strip_;
};
// Test adding and removing tabs and making sure that views get added to
// the tab strip.
TEST_F(TabStripControllerTest, AddRemoveTabs) {
EXPECT_TRUE(model_->empty());
SiteInstance* instance = SiteInstance::CreateSiteInstance(profile());
TabContentsWrapper* tab_contents =
Browser::TabContentsFactory(profile(), instance,
MSG_ROUTING_NONE, NULL, NULL);
model_->AppendTabContents(tab_contents, true);
EXPECT_EQ(model_->count(), 1);
}
TEST_F(TabStripControllerTest, SelectTab) {
// TODO(pinkerton): Implement http://crbug.com/10899
}
TEST_F(TabStripControllerTest, RearrangeTabs) {
// TODO(pinkerton): Implement http://crbug.com/10899
}
// Test that changing the number of tabs broadcasts a
// kTabStripNumberOfTabsChanged notifiction.
TEST_F(TabStripControllerTest, Notifications) {
// TODO(pinkerton): Implement http://crbug.com/10899
}
TEST_F(TabStripControllerTest, CorrectToolTipText) {
// Create tab 1.
SiteInstance* instance = SiteInstance::CreateSiteInstance(profile());
TabContentsWrapper* tab_contents =
Browser::TabContentsFactory(profile(), instance,
MSG_ROUTING_NONE, NULL, NULL);
model_->AppendTabContents(tab_contents, true);
// Create tab 2.
SiteInstance* instance2 = SiteInstance::CreateSiteInstance(profile());
TabContentsWrapper* tab_contents2 =
Browser::TabContentsFactory(profile(), instance2,
MSG_ROUTING_NONE, NULL, NULL);
model_->AppendTabContents(tab_contents2, true);
// Set tab 1 tooltip.
TabView* tab1 = (TabView*)[controller_.get() viewAtIndex:0];
[tab1 setToolTip:@"Tab1"];
// Set tab 2 tooltip.
TabView* tab2 = (TabView*)[controller_.get() viewAtIndex:1];
[tab2 setToolTip:@"Tab2"];
EXPECT_FALSE([tab1 getController].selected);
EXPECT_TRUE([tab2 getController].selected);
// Check that there's no tooltip yet.
EXPECT_FALSE([controller_ view:nil
stringForToolTip:nil
point:NSMakePoint(0,0)
userData:nil]);
// Set up mouse event on overlap of tab1 + tab2.
NSWindow* window = test_window();
NSEvent* current = [NSApp currentEvent];
NSRect tab_strip_frame = [tab_strip_.get() frame];
// Hover over overlap between tab 1 and 2.
NSEvent* event = [NSEvent mouseEventWithType:NSMouseMoved
location:NSMakePoint(
275, NSMinY(tab_strip_frame) + 1)
modifierFlags:0
timestamp:[current timestamp]
windowNumber:[window windowNumber]
context:nil
eventNumber:0
clickCount:1
pressure:1.0];
[controller_.get() mouseMoved:event];
EXPECT_STREQ("Tab2",
[[controller_ view:nil
stringForToolTip:nil
point:NSMakePoint(0,0)
userData:nil] cStringUsingEncoding:NSASCIIStringEncoding]);
// Hover over tab 1.
event = [NSEvent mouseEventWithType:NSMouseMoved
location:NSMakePoint(260,
NSMinY(tab_strip_frame) + 1)
modifierFlags:0
timestamp:[current timestamp]
windowNumber:[window windowNumber]
context:nil
eventNumber:0
clickCount:1
pressure:1.0];
[controller_.get() mouseMoved:event];
EXPECT_STREQ("Tab1",
[[controller_ view:nil
stringForToolTip:nil
point:NSMakePoint(0,0)
userData:nil] cStringUsingEncoding:NSASCIIStringEncoding]);
// Hover over tab 2.
event = [NSEvent mouseEventWithType:NSMouseMoved
location:NSMakePoint(290,
NSMinY(tab_strip_frame) + 1)
modifierFlags:0
timestamp:[current timestamp]
windowNumber:[window windowNumber]
context:nil
eventNumber:0
clickCount:1
pressure:1.0];
[controller_.get() mouseMoved:event];
EXPECT_STREQ("Tab2",
[[controller_ view:nil
stringForToolTip:nil
point:NSMakePoint(0,0)
userData:nil] cStringUsingEncoding:NSASCIIStringEncoding]);
}
} // namespace
|