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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
// 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/basictypes.h"
#include "base/scoped_nsobject.h"
#import "chrome/browser/cocoa/bookmark_bubble_controller.h"
#include "chrome/browser/cocoa/browser_test_helper.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#import "chrome/browser/cocoa/info_bubble_window.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
namespace {
class BookmarkBubbleControllerTest : public CocoaTest {
public:
static int edits_;
BrowserTestHelper helper_;
BookmarkBubbleController* controller_;
BookmarkBubbleControllerTest() : controller_(nil) {
edits_ = 0;
}
virtual void TearDown() {
[controller_ close];
CocoaTest::TearDown();
}
// Returns a controller but ownership not transferred.
// Only one of these will be valid at a time.
BookmarkBubbleController* ControllerForNode(const BookmarkNode* node) {
if (controller_)
[controller_ close];
controller_ = [[BookmarkBubbleController alloc]
initWithParentWindow:test_window()
topLeftForBubble:TopLeftForBubble()
model:helper_.profile()->GetBookmarkModel()
node:node
alreadyBookmarked:YES];
EXPECT_TRUE([controller_ window]);
[controller_ showWindow:nil];
return controller_;
}
BookmarkModel* GetBookmarkModel() {
return helper_.profile()->GetBookmarkModel();
}
bool IsWindowClosing() {
return [static_cast<InfoBubbleWindow*>([controller_ window]) isClosing];
}
NSPoint TopLeftForBubble() {
return NSMakePoint(10, 300);
}
};
// Confirm basics about the bubble window (e.g. that it is inside the
// parent window)
TEST_F(BookmarkBubbleControllerTest, TestBubbleWindow) {
BookmarkModel* model = GetBookmarkModel();
const BookmarkNode* node = model->AddURL(model->GetBookmarkBarNode(),
0,
L"Bookie markie title",
GURL("http://www.google.com"));
BookmarkBubbleController* controller = ControllerForNode(node);
EXPECT_TRUE(controller);
NSWindow* window = [controller window];
EXPECT_TRUE(window);
EXPECT_TRUE(NSContainsRect([test_window() frame],
[window frame]));
}
// Test that we can handle closing the parent window
TEST_F(BookmarkBubbleControllerTest, TestClosingParentWindow) {
BookmarkModel* model = GetBookmarkModel();
const BookmarkNode* node = model->AddURL(model->GetBookmarkBarNode(),
0,
L"Bookie markie title",
GURL("http://www.google.com"));
BookmarkBubbleController* controller = ControllerForNode(node);
EXPECT_TRUE(controller);
NSWindow* window = [controller window];
EXPECT_TRUE(window);
base::ScopedNSAutoreleasePool pool;
[test_window() performClose:NSApp];
}
// Confirm population of folder list
TEST_F(BookmarkBubbleControllerTest, TestFillInFolder) {
// Create some folders, including a nested folder
BookmarkModel* model = GetBookmarkModel();
EXPECT_TRUE(model);
const BookmarkNode* bookmarkBarNode = model->GetBookmarkBarNode();
EXPECT_TRUE(bookmarkBarNode);
const BookmarkNode* node1 = model->AddGroup(bookmarkBarNode, 0, L"one");
EXPECT_TRUE(node1);
const BookmarkNode* node2 = model->AddGroup(bookmarkBarNode, 1, L"two");
EXPECT_TRUE(node2);
const BookmarkNode* node3 = model->AddGroup(bookmarkBarNode, 2, L"three");
EXPECT_TRUE(node3);
const BookmarkNode* node4 = model->AddGroup(node2, 0, L"sub");
EXPECT_TRUE(node4);
const BookmarkNode* node5 =
model->AddURL(node1, 0, L"title1", GURL("http://www.google.com"));
EXPECT_TRUE(node5);
const BookmarkNode* node6 =
model->AddURL(node3, 0, L"title2", GURL("http://www.google.com"));
EXPECT_TRUE(node6);
const BookmarkNode* node7 =
model->AddURL(node4, 0, L"title3", GURL("http://www.google.com/reader"));
EXPECT_TRUE(node7);
BookmarkBubbleController* controller = ControllerForNode(node4);
EXPECT_TRUE(controller);
NSArray* titles =
[[[controller folderPopUpButton] itemArray] valueForKey:@"title"];
EXPECT_TRUE([titles containsObject:@"one"]);
EXPECT_TRUE([titles containsObject:@"two"]);
EXPECT_TRUE([titles containsObject:@"three"]);
EXPECT_TRUE([titles containsObject:@"sub"]);
EXPECT_FALSE([titles containsObject:@"title1"]);
EXPECT_FALSE([titles containsObject:@"title2"]);
}
// Confirm ability to handle folders with blank name.
TEST_F(BookmarkBubbleControllerTest, TestFolderWithBlankName) {
// Create some folders, including a nested folder
BookmarkModel* model = GetBookmarkModel();
EXPECT_TRUE(model);
const BookmarkNode* bookmarkBarNode = model->GetBookmarkBarNode();
EXPECT_TRUE(bookmarkBarNode);
const BookmarkNode* node1 = model->AddGroup(bookmarkBarNode, 0, L"one");
EXPECT_TRUE(node1);
const BookmarkNode* node2 = model->AddGroup(bookmarkBarNode, 1, L"");
EXPECT_TRUE(node2);
const BookmarkNode* node3 = model->AddGroup(bookmarkBarNode, 2, L"three");
EXPECT_TRUE(node3);
const BookmarkNode* node2_1 =
model->AddURL(node2, 0, L"title1", GURL("http://www.google.com"));
EXPECT_TRUE(node2_1);
BookmarkBubbleController* controller = ControllerForNode(node1);
EXPECT_TRUE(controller);
// One of the items should be blank and its node should be node2.
NSArray* items = [[controller folderPopUpButton] itemArray];
EXPECT_GT([items count], 4U);
BOOL blankFolderFound = NO;
for (NSMenuItem* item in [[controller folderPopUpButton] itemArray]) {
if ([[item title] length] == 0 &&
static_cast<const BookmarkNode*>([[item representedObject]
pointerValue]) == node2) {
blankFolderFound = YES;
break;
}
}
EXPECT_TRUE(blankFolderFound);
}
// Click on edit; bubble gets closed.
TEST_F(BookmarkBubbleControllerTest, TestEdit) {
BookmarkModel* model = GetBookmarkModel();
const BookmarkNode* node = model->AddURL(model->GetBookmarkBarNode(),
0,
L"Bookie markie title",
GURL("http://www.google.com"));
BookmarkBubbleController* controller = ControllerForNode(node);
EXPECT_TRUE(controller);
EXPECT_EQ(edits_, 0);
EXPECT_FALSE(IsWindowClosing());
[controller edit:controller];
EXPECT_EQ(edits_, 1);
EXPECT_TRUE(IsWindowClosing());
}
// CallClose; bubble gets closed.
TEST_F(BookmarkBubbleControllerTest, TestClose) {
BookmarkModel* model = GetBookmarkModel();
const BookmarkNode* node = model->AddURL(model->GetBookmarkBarNode(),
0,
L"Bookie markie title",
GURL("http://www.google.com"));
EXPECT_EQ(edits_, 0);
BookmarkBubbleController* controller = ControllerForNode(node);
EXPECT_TRUE(controller);
EXPECT_FALSE(IsWindowClosing());
[controller ok:controller];
EXPECT_EQ(edits_, 0);
EXPECT_TRUE(IsWindowClosing());
}
// User changes title and parent folder in the UI
TEST_F(BookmarkBubbleControllerTest, TestUserEdit) {
BookmarkModel* model = GetBookmarkModel();
EXPECT_TRUE(model);
const BookmarkNode* bookmarkBarNode = model->GetBookmarkBarNode();
EXPECT_TRUE(bookmarkBarNode);
const BookmarkNode* node = model->AddURL(bookmarkBarNode,
0,
L"short-title",
GURL("http://www.google.com"));
const BookmarkNode* grandma = model->AddGroup(bookmarkBarNode, 0, L"grandma");
EXPECT_TRUE(grandma);
const BookmarkNode* grandpa = model->AddGroup(bookmarkBarNode, 0, L"grandpa");
EXPECT_TRUE(grandpa);
BookmarkBubbleController* controller = ControllerForNode(node);
EXPECT_TRUE(controller);
// simulate a user edit
[controller setTitle:@"oops" parentFolder:grandma];
[controller edit:controller];
// Make sure bookmark has changed
EXPECT_EQ(node->GetTitle(), L"oops");
EXPECT_EQ(node->GetParent()->GetTitle(), L"grandma");
}
// Confirm happiness with parent nodes that have the same name.
TEST_F(BookmarkBubbleControllerTest, TestNewParentSameName) {
BookmarkModel* model = GetBookmarkModel();
EXPECT_TRUE(model);
const BookmarkNode* bookmarkBarNode = model->GetBookmarkBarNode();
EXPECT_TRUE(bookmarkBarNode);
for (int i=0; i<2; i++) {
const BookmarkNode* node = model->AddURL(bookmarkBarNode,
0,
L"short-title",
GURL("http://www.google.com"));
EXPECT_TRUE(node);
const BookmarkNode* group = model->AddGroup(bookmarkBarNode, 0, L"NAME");
EXPECT_TRUE(group);
group = model->AddGroup(bookmarkBarNode, 0, L"NAME");
EXPECT_TRUE(group);
group = model->AddGroup(bookmarkBarNode, 0, L"NAME");
EXPECT_TRUE(group);
BookmarkBubbleController* controller = ControllerForNode(node);
EXPECT_TRUE(controller);
// simulate a user edit
[controller setParentFolderSelection:bookmarkBarNode->GetChild(i)];
[controller edit:controller];
// Make sure bookmark has changed, and that the parent is what we
// expect. This proves nobody did searching based on name.
EXPECT_EQ(node->GetParent(), bookmarkBarNode->GetChild(i));
}
}
// Confirm happiness with nodes with the same Name
TEST_F(BookmarkBubbleControllerTest, TestDuplicateNodeNames) {
BookmarkModel* model = GetBookmarkModel();
const BookmarkNode* bookmarkBarNode = model->GetBookmarkBarNode();
EXPECT_TRUE(bookmarkBarNode);
const BookmarkNode* node1 = model->AddGroup(bookmarkBarNode, 0, L"NAME");
EXPECT_TRUE(node1);
const BookmarkNode* node2 = model->AddGroup(bookmarkBarNode, 0, L"NAME");
EXPECT_TRUE(node2);
BookmarkBubbleController* controller = ControllerForNode(bookmarkBarNode);
EXPECT_TRUE(controller);
NSPopUpButton* button = [controller folderPopUpButton];
[controller setParentFolderSelection:node1];
NSMenuItem* item = [button selectedItem];
id itemObject = [item representedObject];
EXPECT_TRUE([itemObject isEqual:[NSValue valueWithPointer:node1]]);
[controller setParentFolderSelection:node2];
item = [button selectedItem];
itemObject = [item representedObject];
EXPECT_TRUE([itemObject isEqual:[NSValue valueWithPointer:node2]]);
}
// Click the "remove" button
TEST_F(BookmarkBubbleControllerTest, TestRemove) {
BookmarkModel* model = GetBookmarkModel();
GURL gurl("http://www.google.com");
const BookmarkNode* node = model->AddURL(model->GetBookmarkBarNode(),
0,
L"Bookie markie title",
gurl);
BookmarkBubbleController* controller = ControllerForNode(node);
EXPECT_TRUE(controller);
EXPECT_TRUE(model->IsBookmarked(gurl));
[controller remove:controller];
EXPECT_FALSE(model->IsBookmarked(gurl));
EXPECT_TRUE(IsWindowClosing());
}
// Confirm picking "choose another folder" caused edit: to be called.
TEST_F(BookmarkBubbleControllerTest, PopUpSelectionChanged) {
BookmarkModel* model = GetBookmarkModel();
GURL gurl("http://www.google.com");
const BookmarkNode* node = model->AddURL(model->GetBookmarkBarNode(),
0, L"super-title",
gurl);
BookmarkBubbleController* controller = ControllerForNode(node);
EXPECT_TRUE(controller);
NSPopUpButton* button = [controller folderPopUpButton];
[button selectItemWithTitle:[[controller class] chooseAnotherFolderString]];
EXPECT_EQ(edits_, 0);
[button sendAction:[button action] to:[button target]];
EXPECT_EQ(edits_, 1);
}
// Create a controller that simulates the bookmark just now being created by
// the user clicking the star, then sending the "cancel" command to represent
// them pressing escape. The bookmark should not be there.
TEST_F(BookmarkBubbleControllerTest, EscapeRemovesNewBookmark) {
BookmarkModel* model = GetBookmarkModel();
GURL gurl("http://www.google.com");
const BookmarkNode* node = model->AddURL(model->GetBookmarkBarNode(),
0,
L"Bookie markie title",
gurl);
BookmarkBubbleController* controller =
[[BookmarkBubbleController alloc]
initWithParentWindow:test_window()
topLeftForBubble:TopLeftForBubble()
model:helper_.profile()->GetBookmarkModel()
node:node
alreadyBookmarked:NO]; // The last param is the key difference.
EXPECT_TRUE([controller window]);
// Calls release on controller.
[controller cancel:nil];
EXPECT_FALSE(model->IsBookmarked(gurl));
}
// Create a controller where the bookmark already existed prior to clicking
// the star and test that sending a cancel command doesn't change the state
// of the bookmark.
TEST_F(BookmarkBubbleControllerTest, EscapeDoesntTouchExistingBookmark) {
BookmarkModel* model = GetBookmarkModel();
GURL gurl("http://www.google.com");
const BookmarkNode* node = model->AddURL(model->GetBookmarkBarNode(),
0,
L"Bookie markie title",
gurl);
BookmarkBubbleController* controller = ControllerForNode(node);
EXPECT_TRUE(controller);
[(id)controller cancel:nil];
EXPECT_TRUE(model->IsBookmarked(gurl));
}
} // namespace
@implementation NSApplication (BookmarkBubbleUnitTest)
// Add handler for the editBookmarkNode: action to NSApp for testing purposes.
// Normally this would be sent up the responder tree correctly, but since
// tests run in the background, key window and main window are never set on
// NSApplication. Adding it to NSApplication directly removes the need for
// worrying about what the current window with focus is.
- (void)editBookmarkNode:(id)sender {
EXPECT_TRUE([sender respondsToSelector:@selector(node)]);
BookmarkBubbleControllerTest::edits_++;
}
@end
|