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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
// 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>
#include "base/string16.h"
#include "base/sys_string_conversions.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/bookmarks/bookmark_model.h"
#import "chrome/browser/cocoa/bookmarks/bookmark_editor_controller.h"
#include "chrome/browser/cocoa/browser_test_helper.h"
#import "chrome/browser/cocoa/cocoa_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
class BookmarkEditorControllerTest : public CocoaTest {
public:
BrowserTestHelper browser_helper_;
const BookmarkNode* default_node_;
const BookmarkNode* default_parent_;
const char* default_name_;
string16 default_title_;
BookmarkEditorController* controller_;
virtual void SetUp() {
CocoaTest::SetUp();
BookmarkModel* model = browser_helper_.profile()->GetBookmarkModel();
default_parent_ = model->GetBookmarkBarNode();
default_name_ = "http://www.zim-bop-a-dee.com/";
default_title_ = ASCIIToUTF16("ooh title");
const BookmarkNode* default_node = model->AddURL(default_parent_, 0,
default_title_,
GURL(default_name_));
controller_ = [[BookmarkEditorController alloc]
initWithParentWindow:test_window()
profile:browser_helper_.profile()
parent:default_parent_
node:default_node
configuration:BookmarkEditor::NO_TREE];
[controller_ runAsModalSheet];
}
virtual void TearDown() {
controller_ = NULL;
CocoaTest::TearDown();
}
};
TEST_F(BookmarkEditorControllerTest, NoEdit) {
[controller_ cancel:nil];
ASSERT_EQ(default_parent_->GetChildCount(), 1);
const BookmarkNode* child = default_parent_->GetChild(0);
EXPECT_EQ(child->GetTitle(), default_title_);
EXPECT_EQ(child->GetURL(), GURL(default_name_));
}
TEST_F(BookmarkEditorControllerTest, EditTitle) {
[controller_ setDisplayName:@"whamma jamma bamma"];
[controller_ ok:nil];
ASSERT_EQ(default_parent_->GetChildCount(), 1);
const BookmarkNode* child = default_parent_->GetChild(0);
EXPECT_EQ(child->GetTitle(), ASCIIToUTF16("whamma jamma bamma"));
EXPECT_EQ(child->GetURL(), GURL(default_name_));
}
TEST_F(BookmarkEditorControllerTest, EditURL) {
EXPECT_TRUE([controller_ okButtonEnabled]);
[controller_ setDisplayURL:@"http://yellow-sneakers.com/"];
EXPECT_TRUE([controller_ okButtonEnabled]);
[controller_ ok:nil];
ASSERT_EQ(default_parent_->GetChildCount(), 1);
const BookmarkNode* child = default_parent_->GetChild(0);
EXPECT_EQ(child->GetTitle(), default_title_);
EXPECT_EQ(child->GetURL(), GURL("http://yellow-sneakers.com/"));
}
TEST_F(BookmarkEditorControllerTest, EditAndFixPrefix) {
[controller_ setDisplayURL:@"x"];
[controller_ ok:nil];
ASSERT_EQ(default_parent_->GetChildCount(), 1);
const BookmarkNode* child = default_parent_->GetChild(0);
EXPECT_TRUE(child->GetURL().is_valid());
}
TEST_F(BookmarkEditorControllerTest, NodeDeleted) {
// Delete the bookmark being edited and verify the sheet cancels itself:
ASSERT_TRUE([test_window() attachedSheet]);
BookmarkModel* model = browser_helper_.profile()->GetBookmarkModel();
model->Remove(default_parent_, 0);
ASSERT_FALSE([test_window() attachedSheet]);
}
TEST_F(BookmarkEditorControllerTest, EditAndConfirmOKButton) {
// Confirm OK button enabled/disabled as appropriate:
// First test the URL.
EXPECT_TRUE([controller_ okButtonEnabled]);
[controller_ setDisplayURL:@""];
EXPECT_FALSE([controller_ okButtonEnabled]);
[controller_ setDisplayURL:@"http://www.cnn.com"];
EXPECT_TRUE([controller_ okButtonEnabled]);
// Then test the name.
[controller_ setDisplayName:@""];
EXPECT_TRUE([controller_ okButtonEnabled]);
[controller_ setDisplayName:@" "];
EXPECT_TRUE([controller_ okButtonEnabled]);
// Then little mix of both.
[controller_ setDisplayName:@"name"];
EXPECT_TRUE([controller_ okButtonEnabled]);
[controller_ setDisplayURL:@""];
EXPECT_FALSE([controller_ okButtonEnabled]);
[controller_ cancel:nil];
}
TEST_F(BookmarkEditorControllerTest, GoodAndBadURLsChangeColor) {
// Confirm that the background color of the URL edit field changes
// based on whether it contains a valid or invalid URL.
[controller_ setDisplayURL:@"http://www.cnn.com"];
NSColor *urlColorA = [controller_ urlFieldColor];
EXPECT_TRUE(urlColorA);
[controller_ setDisplayURL:@""];
NSColor *urlColorB = [controller_ urlFieldColor];
EXPECT_TRUE(urlColorB);
EXPECT_NSNE(urlColorA, urlColorB);
[controller_ setDisplayURL:@"http://www.google.com"];
[controller_ cancel:nil];
urlColorB = [controller_ urlFieldColor];
EXPECT_TRUE(urlColorB);
EXPECT_NSEQ(urlColorA, urlColorB);
}
class BookmarkEditorControllerNoNodeTest : public CocoaTest {
public:
BrowserTestHelper browser_helper_;
BookmarkEditorController* controller_;
virtual void SetUp() {
CocoaTest::SetUp();
BookmarkModel* model = browser_helper_.profile()->GetBookmarkModel();
const BookmarkNode* parent = model->GetBookmarkBarNode();
controller_ = [[BookmarkEditorController alloc]
initWithParentWindow:test_window()
profile:browser_helper_.profile()
parent:parent
node:NULL
configuration:BookmarkEditor::NO_TREE];
[controller_ runAsModalSheet];
}
virtual void TearDown() {
controller_ = NULL;
CocoaTest::TearDown();
}
};
TEST_F(BookmarkEditorControllerNoNodeTest, NoNodeNoTree) {
EXPECT_EQ(@"", [controller_ displayName]);
EXPECT_EQ(@"", [controller_ displayURL]);
EXPECT_FALSE([controller_ okButtonEnabled]);
[controller_ cancel:nil];
}
class BookmarkEditorControllerYesNodeTest : public CocoaTest {
public:
BrowserTestHelper browser_helper_;
string16 default_title_;
const char* url_name_;
BookmarkEditorController* controller_;
virtual void SetUp() {
CocoaTest::SetUp();
BookmarkModel* model = browser_helper_.profile()->GetBookmarkModel();
const BookmarkNode* parent = model->GetBookmarkBarNode();
default_title_ = ASCIIToUTF16("wooh title");
url_name_ = "http://www.zoom-baby-doo-da.com/";
const BookmarkNode* node = model->AddURL(parent, 0, default_title_,
GURL(url_name_));
controller_ = [[BookmarkEditorController alloc]
initWithParentWindow:test_window()
profile:browser_helper_.profile()
parent:parent
node:node
configuration:BookmarkEditor::NO_TREE];
[controller_ runAsModalSheet];
}
virtual void TearDown() {
controller_ = NULL;
CocoaTest::TearDown();
}
};
TEST_F(BookmarkEditorControllerYesNodeTest, YesNodeShowTree) {
EXPECT_NSEQ(base::SysUTF16ToNSString(default_title_),
[controller_ displayName]);
EXPECT_NSEQ([NSString stringWithCString:url_name_
encoding:NSUTF8StringEncoding],
[controller_ displayURL]);
[controller_ cancel:nil];
}
class BookmarkEditorControllerTreeTest : public CocoaTest {
public:
BrowserTestHelper browser_helper_;
BookmarkEditorController* controller_;
const BookmarkNode* group_a_;
const BookmarkNode* group_b_;
const BookmarkNode* group_bb_;
const BookmarkNode* group_c_;
const BookmarkNode* bookmark_bb_3_;
GURL bb3_url_1_;
GURL bb3_url_2_;
BookmarkEditorControllerTreeTest() {
// Set up a small bookmark hierarchy, which will look as follows:
// a b c d
// a-0 b-0 c-0
// a-1 bb-0 c-1
// a-2 bb-1 c-2
// bb-2
// bb-3
// bb-4
// b-1
// b-2
BookmarkModel& model(*(browser_helper_.profile()->GetBookmarkModel()));
const BookmarkNode* root = model.GetBookmarkBarNode();
group_a_ = model.AddGroup(root, 0, ASCIIToUTF16("a"));
model.AddURL(group_a_, 0, ASCIIToUTF16("a-0"), GURL("http://a-0.com"));
model.AddURL(group_a_, 1, ASCIIToUTF16("a-1"), GURL("http://a-1.com"));
model.AddURL(group_a_, 2, ASCIIToUTF16("a-2"), GURL("http://a-2.com"));
group_b_ = model.AddGroup(root, 1, ASCIIToUTF16("b"));
model.AddURL(group_b_, 0, ASCIIToUTF16("b-0"), GURL("http://b-0.com"));
group_bb_ = model.AddGroup(group_b_, 1, ASCIIToUTF16("bb"));
model.AddURL(group_bb_, 0, ASCIIToUTF16("bb-0"), GURL("http://bb-0.com"));
model.AddURL(group_bb_, 1, ASCIIToUTF16("bb-1"), GURL("http://bb-1.com"));
model.AddURL(group_bb_, 2, ASCIIToUTF16("bb-2"), GURL("http://bb-2.com"));
// To find it later, this bookmark name must always have a URL
// of http://bb-3.com or https://bb-3.com
bb3_url_1_ = GURL("http://bb-3.com");
bb3_url_2_ = GURL("https://bb-3.com");
bookmark_bb_3_ = model.AddURL(group_bb_, 3, ASCIIToUTF16("bb-3"),
bb3_url_1_);
model.AddURL(group_bb_, 4, ASCIIToUTF16("bb-4"), GURL("http://bb-4.com"));
model.AddURL(group_b_, 2, ASCIIToUTF16("b-1"), GURL("http://b-2.com"));
model.AddURL(group_b_, 3, ASCIIToUTF16("b-2"), GURL("http://b-3.com"));
group_c_ = model.AddGroup(root, 2, ASCIIToUTF16("c"));
model.AddURL(group_c_, 0, ASCIIToUTF16("c-0"), GURL("http://c-0.com"));
model.AddURL(group_c_, 1, ASCIIToUTF16("c-1"), GURL("http://c-1.com"));
model.AddURL(group_c_, 2, ASCIIToUTF16("c-2"), GURL("http://c-2.com"));
model.AddURL(group_c_, 3, ASCIIToUTF16("c-3"), GURL("http://c-3.com"));
model.AddURL(root, 3, ASCIIToUTF16("d"), GURL("http://d-0.com"));
}
virtual BookmarkEditorController* CreateController() {
return [[BookmarkEditorController alloc]
initWithParentWindow:test_window()
profile:browser_helper_.profile()
parent:group_bb_
node:bookmark_bb_3_
configuration:BookmarkEditor::SHOW_TREE];
}
virtual void SetUp() {
controller_ = CreateController();
[controller_ runAsModalSheet];
}
virtual void TearDown() {
controller_ = NULL;
CocoaTest::TearDown();
}
// After changing a node, pointers to the node may be invalid. This
// is because the node itself may not be updated; it may removed and
// a new one is added in that location. (Implementation detail of
// BookmarkEditorController). This method updates the class's
// bookmark_bb_3_ so that it points to the new node for testing.
void UpdateBB3() {
std::vector<const BookmarkNode*> nodes;
BookmarkModel* model = browser_helper_.profile()->GetBookmarkModel();
model->GetNodesByURL(bb3_url_1_, &nodes);
if (nodes.size() == 0)
model->GetNodesByURL(bb3_url_2_, &nodes);
DCHECK(nodes.size());
bookmark_bb_3_ = nodes[0];
}
};
TEST_F(BookmarkEditorControllerTreeTest, VerifyBookmarkTestModel) {
BookmarkModel& model(*(browser_helper_.profile()->GetBookmarkModel()));
model.root_node();
const BookmarkNode& root(*model.GetBookmarkBarNode());
EXPECT_EQ(4, root.GetChildCount());
const BookmarkNode* child = root.GetChild(0);
EXPECT_EQ(3, child->GetChildCount());
const BookmarkNode* subchild = child->GetChild(0);
EXPECT_EQ(0, subchild->GetChildCount());
subchild = child->GetChild(1);
EXPECT_EQ(0, subchild->GetChildCount());
subchild = child->GetChild(2);
EXPECT_EQ(0, subchild->GetChildCount());
child = root.GetChild(1);
EXPECT_EQ(4, child->GetChildCount());
subchild = child->GetChild(0);
EXPECT_EQ(0, subchild->GetChildCount());
subchild = child->GetChild(1);
EXPECT_EQ(5, subchild->GetChildCount());
const BookmarkNode* subsubchild = subchild->GetChild(0);
EXPECT_EQ(0, subsubchild->GetChildCount());
subsubchild = subchild->GetChild(1);
EXPECT_EQ(0, subsubchild->GetChildCount());
subsubchild = subchild->GetChild(2);
EXPECT_EQ(0, subsubchild->GetChildCount());
subsubchild = subchild->GetChild(3);
EXPECT_EQ(0, subsubchild->GetChildCount());
subsubchild = subchild->GetChild(4);
EXPECT_EQ(0, subsubchild->GetChildCount());
subchild = child->GetChild(2);
EXPECT_EQ(0, subchild->GetChildCount());
subchild = child->GetChild(3);
EXPECT_EQ(0, subchild->GetChildCount());
child = root.GetChild(2);
EXPECT_EQ(4, child->GetChildCount());
subchild = child->GetChild(0);
EXPECT_EQ(0, subchild->GetChildCount());
subchild = child->GetChild(1);
EXPECT_EQ(0, subchild->GetChildCount());
subchild = child->GetChild(2);
EXPECT_EQ(0, subchild->GetChildCount());
subchild = child->GetChild(3);
EXPECT_EQ(0, subchild->GetChildCount());
child = root.GetChild(3);
EXPECT_EQ(0, child->GetChildCount());
[controller_ cancel:nil];
}
TEST_F(BookmarkEditorControllerTreeTest, RenameBookmarkInPlace) {
const BookmarkNode* oldParent = bookmark_bb_3_->GetParent();
[controller_ setDisplayName:@"NEW NAME"];
[controller_ ok:nil];
UpdateBB3();
const BookmarkNode* newParent = bookmark_bb_3_->GetParent();
ASSERT_EQ(newParent, oldParent);
int childIndex = newParent->IndexOfChild(bookmark_bb_3_);
ASSERT_EQ(3, childIndex);
}
TEST_F(BookmarkEditorControllerTreeTest, ChangeBookmarkURLInPlace) {
const BookmarkNode* oldParent = bookmark_bb_3_->GetParent();
[controller_ setDisplayURL:@"https://bb-3.com"];
[controller_ ok:nil];
UpdateBB3();
const BookmarkNode* newParent = bookmark_bb_3_->GetParent();
ASSERT_EQ(newParent, oldParent);
int childIndex = newParent->IndexOfChild(bookmark_bb_3_);
ASSERT_EQ(3, childIndex);
}
TEST_F(BookmarkEditorControllerTreeTest, ChangeBookmarkGroup) {
[controller_ selectTestNodeInBrowser:group_c_];
[controller_ ok:nil];
UpdateBB3();
const BookmarkNode* parent = bookmark_bb_3_->GetParent();
ASSERT_EQ(parent, group_c_);
int childIndex = parent->IndexOfChild(bookmark_bb_3_);
ASSERT_EQ(4, childIndex);
}
TEST_F(BookmarkEditorControllerTreeTest, ChangeNameAndBookmarkGroup) {
[controller_ setDisplayName:@"NEW NAME"];
[controller_ selectTestNodeInBrowser:group_c_];
[controller_ ok:nil];
UpdateBB3();
const BookmarkNode* parent = bookmark_bb_3_->GetParent();
ASSERT_EQ(parent, group_c_);
int childIndex = parent->IndexOfChild(bookmark_bb_3_);
ASSERT_EQ(4, childIndex);
EXPECT_EQ(bookmark_bb_3_->GetTitle(), ASCIIToUTF16("NEW NAME"));
}
TEST_F(BookmarkEditorControllerTreeTest, AddFolderWithGroupSelected) {
// Folders are NOT added unless the OK button is pressed.
[controller_ newFolder:nil];
[controller_ cancel:nil];
EXPECT_EQ(5, group_bb_->GetChildCount());
}
class BookmarkEditorControllerTreeNoNodeTest :
public BookmarkEditorControllerTreeTest {
public:
virtual BookmarkEditorController* CreateController() {
return [[BookmarkEditorController alloc]
initWithParentWindow:test_window()
profile:browser_helper_.profile()
parent:group_bb_
node:nil
configuration:BookmarkEditor::SHOW_TREE];
}
};
TEST_F(BookmarkEditorControllerTreeNoNodeTest, NewBookmarkNoNode) {
[controller_ setDisplayName:@"NEW BOOKMARK"];
[controller_ setDisplayURL:@"http://NEWURL.com"];
[controller_ ok:nil];
const BookmarkNode* new_node = group_bb_->GetChild(5);
ASSERT_EQ(0, new_node->GetChildCount());
EXPECT_EQ(new_node->GetTitle(), ASCIIToUTF16("NEW BOOKMARK"));
EXPECT_EQ(new_node->GetURL(), GURL("http://NEWURL.com"));
}
|