1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/views/bookmarks/bookmark_editor_view.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/test/base/testing_profile.h"
15 #include "components/bookmarks/browser/bookmark_model.h"
16 #include "components/bookmarks/test/bookmark_test_helpers.h"
17 #include "content/public/test/test_browser_thread.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/views/controls/textfield/textfield.h"
20 #include "ui/views/controls/tree/tree_view.h"
22 using base::ASCIIToUTF16
;
23 using base::UTF8ToUTF16
;
25 using base::TimeDelta
;
26 using bookmarks::BookmarkModel
;
27 using bookmarks::BookmarkNode
;
28 using content::BrowserThread
;
30 // Base class for bookmark editor tests. Creates a BookmarkModel and populates
32 class BookmarkEditorViewTest
: public testing::Test
{
34 BookmarkEditorViewTest()
35 : ui_thread_(BrowserThread::UI
, &message_loop_
),
36 file_thread_(BrowserThread::FILE, &message_loop_
),
40 void SetUp() override
{
41 profile_
.reset(new TestingProfile());
42 profile_
->CreateBookmarkModel(true);
44 model_
= BookmarkModelFactory::GetForProfile(profile_
.get());
45 bookmarks::test::WaitForBookmarkModelToLoad(model_
);
50 void TearDown() override
{}
53 std::string
base_path() const { return "file:///c:/tmp/"; }
55 const BookmarkNode
* GetNode(const std::string
& name
) {
56 return model_
->GetMostRecentlyAddedUserNodeForURL(GURL(base_path() + name
));
59 BookmarkNode
* GetMutableNode(const std::string
& name
) {
60 return const_cast<BookmarkNode
*>(GetNode(name
));
63 BookmarkEditorView::EditorTreeModel
* editor_tree_model() {
64 return editor_
->tree_model_
.get();
67 void CreateEditor(Profile
* profile
,
68 const BookmarkNode
* parent
,
69 const BookmarkEditor::EditDetails
& details
,
70 BookmarkEditor::Configuration configuration
) {
71 editor_
.reset(new BookmarkEditorView(profile
, parent
, details
,
75 void SetTitleText(const base::string16
& title
) {
76 editor_
->title_tf_
->SetText(title
);
79 void SetURLText(const base::string16
& text
) {
80 if (editor_
->details_
.type
!= BookmarkEditor::EditDetails::NEW_FOLDER
)
81 editor_
->url_tf_
->SetText(text
);
85 editor_
->ApplyEdits();
88 void ApplyEdits(BookmarkEditorView::EditorNode
* node
) {
89 editor_
->ApplyEdits(node
);
92 BookmarkEditorView::EditorNode
* AddNewFolder(
93 BookmarkEditorView::EditorNode
* parent
) {
94 return editor_
->AddNewFolder(parent
);
98 return editor_
->NewFolder();
101 bool URLTFHasParent() {
102 if (editor_
->details_
.type
== BookmarkEditor::EditDetails::NEW_FOLDER
)
104 return editor_
->url_tf_
->parent();
107 void ExpandAndSelect() {
108 editor_
->ExpandAndSelect();
111 views::TreeView
* tree_view() { return editor_
->tree_view_
; }
113 base::MessageLoopForUI message_loop_
;
114 content::TestBrowserThread ui_thread_
;
115 content::TestBrowserThread file_thread_
;
117 BookmarkModel
* model_
;
118 scoped_ptr
<TestingProfile
> profile_
;
121 // Creates the following structure:
134 const BookmarkNode
* bb_node
= model_
->bookmark_bar_node();
135 std::string test_base
= base_path();
136 model_
->AddURL(bb_node
, 0, ASCIIToUTF16("a"), GURL(test_base
+ "a"));
137 const BookmarkNode
* f1
= model_
->AddFolder(bb_node
, 1, ASCIIToUTF16("F1"));
138 model_
->AddURL(f1
, 0, ASCIIToUTF16("f1a"), GURL(test_base
+ "f1a"));
139 const BookmarkNode
* f11
= model_
->AddFolder(f1
, 1, ASCIIToUTF16("F11"));
140 model_
->AddURL(f11
, 0, ASCIIToUTF16("f11a"), GURL(test_base
+ "f11a"));
141 model_
->AddFolder(bb_node
, 2, ASCIIToUTF16("F2"));
143 // Children of the other node.
144 model_
->AddURL(model_
->other_node(), 0, ASCIIToUTF16("oa"),
145 GURL(test_base
+ "oa"));
146 const BookmarkNode
* of1
=
147 model_
->AddFolder(model_
->other_node(), 1, ASCIIToUTF16("OF1"));
148 model_
->AddURL(of1
, 0, ASCIIToUTF16("of1a"), GURL(test_base
+ "of1a"));
151 scoped_ptr
<BookmarkEditorView
> editor_
;
154 // Makes sure the tree model matches that of the bookmark bar model.
155 TEST_F(BookmarkEditorViewTest
, ModelsMatch
) {
156 CreateEditor(profile_
.get(), NULL
,
157 BookmarkEditor::EditDetails::AddNodeInFolder(
158 NULL
, -1, GURL(), base::string16()),
159 BookmarkEditorView::SHOW_TREE
);
160 BookmarkEditorView::EditorNode
* editor_root
= editor_tree_model()->GetRoot();
161 // The root should have two or three children: bookmark bar, other bookmarks
162 // and conditionally mobile bookmarks.
163 if (model_
->mobile_node()->IsVisible()) {
164 ASSERT_EQ(3, editor_root
->child_count());
166 ASSERT_EQ(2, editor_root
->child_count());
169 BookmarkEditorView::EditorNode
* bb_node
= editor_root
->GetChild(0);
170 // The root should have 2 nodes: folder F1 and F2.
171 ASSERT_EQ(2, bb_node
->child_count());
172 ASSERT_EQ(ASCIIToUTF16("F1"), bb_node
->GetChild(0)->GetTitle());
173 ASSERT_EQ(ASCIIToUTF16("F2"), bb_node
->GetChild(1)->GetTitle());
175 // F1 should have one child, F11
176 ASSERT_EQ(1, bb_node
->GetChild(0)->child_count());
177 ASSERT_EQ(ASCIIToUTF16("F11"), bb_node
->GetChild(0)->GetChild(0)->GetTitle());
179 BookmarkEditorView::EditorNode
* other_node
= editor_root
->GetChild(1);
180 // Other node should have one child (OF1).
181 ASSERT_EQ(1, other_node
->child_count());
182 ASSERT_EQ(ASCIIToUTF16("OF1"), other_node
->GetChild(0)->GetTitle());
185 // Changes the title and makes sure parent/visual order doesn't change.
186 TEST_F(BookmarkEditorViewTest
, EditTitleKeepsPosition
) {
187 CreateEditor(profile_
.get(), NULL
,
188 BookmarkEditor::EditDetails::EditNode(GetNode("a")),
189 BookmarkEditorView::SHOW_TREE
);
190 SetTitleText(ASCIIToUTF16("new_a"));
192 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0));
194 const BookmarkNode
* bb_node
=
195 BookmarkModelFactory::GetForProfile(profile_
.get())->bookmark_bar_node();
196 ASSERT_EQ(ASCIIToUTF16("new_a"), bb_node
->GetChild(0)->GetTitle());
197 // The URL shouldn't have changed.
198 ASSERT_TRUE(GURL(base_path() + "a") == bb_node
->GetChild(0)->url());
201 // Changes the url and makes sure parent/visual order doesn't change.
202 TEST_F(BookmarkEditorViewTest
, EditURLKeepsPosition
) {
203 Time node_time
= Time::Now() + TimeDelta::FromDays(2);
204 GetMutableNode("a")->set_date_added(node_time
);
205 CreateEditor(profile_
.get(), NULL
,
206 BookmarkEditor::EditDetails::EditNode(GetNode("a")),
207 BookmarkEditorView::SHOW_TREE
);
209 SetURLText(UTF8ToUTF16(GURL(base_path() + "new_a").spec()));
211 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0));
213 const BookmarkNode
* bb_node
=
214 BookmarkModelFactory::GetForProfile(profile_
.get())->bookmark_bar_node();
215 ASSERT_EQ(ASCIIToUTF16("a"), bb_node
->GetChild(0)->GetTitle());
216 // The URL should have changed.
217 ASSERT_TRUE(GURL(base_path() + "new_a") == bb_node
->GetChild(0)->url());
218 ASSERT_TRUE(node_time
== bb_node
->GetChild(0)->date_added());
221 // Moves 'a' to be a child of the other node.
222 TEST_F(BookmarkEditorViewTest
, ChangeParent
) {
223 CreateEditor(profile_
.get(), NULL
,
224 BookmarkEditor::EditDetails::EditNode(GetNode("a")),
225 BookmarkEditorView::SHOW_TREE
);
227 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(1));
229 const BookmarkNode
* other_node
=
230 BookmarkModelFactory::GetForProfile(profile_
.get())->other_node();
231 ASSERT_EQ(ASCIIToUTF16("a"), other_node
->GetChild(2)->GetTitle());
232 ASSERT_TRUE(GURL(base_path() + "a") == other_node
->GetChild(2)->url());
235 // Moves 'a' to be a child of the other node and changes its url to new_a.
236 TEST_F(BookmarkEditorViewTest
, ChangeParentAndURL
) {
237 Time node_time
= Time::Now() + TimeDelta::FromDays(2);
238 GetMutableNode("a")->set_date_added(node_time
);
239 CreateEditor(profile_
.get(), NULL
,
240 BookmarkEditor::EditDetails::EditNode(GetNode("a")),
241 BookmarkEditorView::SHOW_TREE
);
243 SetURLText(UTF8ToUTF16(GURL(base_path() + "new_a").spec()));
245 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(1));
247 const BookmarkNode
* other_node
=
248 BookmarkModelFactory::GetForProfile(profile_
.get())->other_node();
249 ASSERT_EQ(ASCIIToUTF16("a"), other_node
->GetChild(2)->GetTitle());
250 ASSERT_TRUE(GURL(base_path() + "new_a") == other_node
->GetChild(2)->url());
251 ASSERT_TRUE(node_time
== other_node
->GetChild(2)->date_added());
254 // Creates a new folder and moves a node to it.
255 TEST_F(BookmarkEditorViewTest
, MoveToNewParent
) {
256 CreateEditor(profile_
.get(), NULL
,
257 BookmarkEditor::EditDetails::EditNode(GetNode("a")),
258 BookmarkEditorView::SHOW_TREE
);
260 // Create two nodes: "F21" as a child of "F2" and "F211" as a child of "F21".
261 BookmarkEditorView::EditorNode
* f2
=
262 editor_tree_model()->GetRoot()->GetChild(0)->GetChild(1);
263 BookmarkEditorView::EditorNode
* f21
= AddNewFolder(f2
);
264 f21
->SetTitle(ASCIIToUTF16("F21"));
265 BookmarkEditorView::EditorNode
* f211
= AddNewFolder(f21
);
266 f211
->SetTitle(ASCIIToUTF16("F211"));
268 // Parent the node to "F21".
271 const BookmarkNode
* bb_node
=
272 BookmarkModelFactory::GetForProfile(profile_
.get())->bookmark_bar_node();
273 const BookmarkNode
* mf2
= bb_node
->GetChild(1);
275 // F2 in the model should have two children now: F21 and the node edited.
276 ASSERT_EQ(2, mf2
->child_count());
277 // F21 should be first.
278 ASSERT_EQ(ASCIIToUTF16("F21"), mf2
->GetChild(0)->GetTitle());
280 ASSERT_EQ(ASCIIToUTF16("a"), mf2
->GetChild(1)->GetTitle());
282 // F21 should have one child, F211.
283 const BookmarkNode
* mf21
= mf2
->GetChild(0);
284 ASSERT_EQ(1, mf21
->child_count());
285 ASSERT_EQ(ASCIIToUTF16("F211"), mf21
->GetChild(0)->GetTitle());
288 // Brings up the editor, creating a new URL on the bookmark bar.
289 TEST_F(BookmarkEditorViewTest
, NewURL
) {
290 const BookmarkNode
* bb_node
=
291 BookmarkModelFactory::GetForProfile(profile_
.get())->bookmark_bar_node();
293 CreateEditor(profile_
.get(), bb_node
,
294 BookmarkEditor::EditDetails::AddNodeInFolder(
295 bb_node
, 1, GURL(), base::string16()),
296 BookmarkEditorView::SHOW_TREE
);
298 SetURLText(UTF8ToUTF16(GURL(base_path() + "a").spec()));
299 SetTitleText(ASCIIToUTF16("new_a"));
301 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0));
303 ASSERT_EQ(4, bb_node
->child_count());
305 const BookmarkNode
* new_node
= bb_node
->GetChild(1);
307 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node
->GetTitle());
308 EXPECT_TRUE(GURL(base_path() + "a") == new_node
->url());
311 // Brings up the editor with no tree and modifies the url.
312 TEST_F(BookmarkEditorViewTest
, ChangeURLNoTree
) {
313 CreateEditor(profile_
.get(), NULL
,
314 BookmarkEditor::EditDetails::EditNode(
315 model_
->other_node()->GetChild(0)),
316 BookmarkEditorView::NO_TREE
);
318 SetURLText(UTF8ToUTF16(GURL(base_path() + "a").spec()));
319 SetTitleText(ASCIIToUTF16("new_a"));
323 const BookmarkNode
* other_node
=
324 BookmarkModelFactory::GetForProfile(profile_
.get())->other_node();
325 ASSERT_EQ(2, other_node
->child_count());
327 const BookmarkNode
* new_node
= other_node
->GetChild(0);
329 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node
->GetTitle());
330 EXPECT_TRUE(GURL(base_path() + "a") == new_node
->url());
333 // Brings up the editor with no tree and modifies only the title.
334 TEST_F(BookmarkEditorViewTest
, ChangeTitleNoTree
) {
335 CreateEditor(profile_
.get(), NULL
,
336 BookmarkEditor::EditDetails::EditNode(
337 model_
->other_node()->GetChild(0)),
338 BookmarkEditorView::NO_TREE
);
340 SetTitleText(ASCIIToUTF16("new_a"));
344 const BookmarkNode
* other_node
=
345 BookmarkModelFactory::GetForProfile(profile_
.get())->other_node();
346 ASSERT_EQ(2, other_node
->child_count());
348 const BookmarkNode
* new_node
= other_node
->GetChild(0);
350 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node
->GetTitle());
353 // Creates a new folder.
354 TEST_F(BookmarkEditorViewTest
, NewFolder
) {
355 const BookmarkNode
* bb_node
= model_
->bookmark_bar_node();
356 BookmarkEditor::EditDetails details
=
357 BookmarkEditor::EditDetails::AddFolder(bb_node
, 1);
358 details
.urls
.push_back(std::make_pair(GURL(base_path() + "x"),
360 CreateEditor(profile_
.get(), bb_node
, details
, BookmarkEditorView::SHOW_TREE
);
362 // The url field shouldn't be visible.
363 EXPECT_FALSE(URLTFHasParent());
364 SetTitleText(ASCIIToUTF16("new_F"));
366 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0));
368 // Make sure the folder was created.
369 ASSERT_EQ(4, bb_node
->child_count());
370 const BookmarkNode
* new_node
= bb_node
->GetChild(1);
371 EXPECT_EQ(BookmarkNode::FOLDER
, new_node
->type());
372 EXPECT_EQ(ASCIIToUTF16("new_F"), new_node
->GetTitle());
373 // The node should have one child.
374 ASSERT_EQ(1, new_node
->child_count());
375 const BookmarkNode
* new_child
= new_node
->GetChild(0);
376 // Make sure the child url/title match.
377 EXPECT_EQ(BookmarkNode::URL
, new_child
->type());
378 EXPECT_EQ(details
.urls
[0].second
, new_child
->GetTitle());
379 EXPECT_EQ(details
.urls
[0].first
, new_child
->url());
382 // Creates a new folder and selects a different folder for the folder to appear
383 // in then the editor is initially created showing.
384 TEST_F(BookmarkEditorViewTest
, MoveFolder
) {
385 BookmarkEditor::EditDetails details
= BookmarkEditor::EditDetails::AddFolder(
386 model_
->bookmark_bar_node(), -1);
387 details
.urls
.push_back(std::make_pair(GURL(base_path() + "x"),
389 CreateEditor(profile_
.get(), model_
->bookmark_bar_node(),
390 details
, BookmarkEditorView::SHOW_TREE
);
392 SetTitleText(ASCIIToUTF16("new_F"));
394 // Create the folder in the 'other' folder.
395 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(1));
397 // Make sure the folder we edited is still there.
398 ASSERT_EQ(3, model_
->other_node()->child_count());
399 const BookmarkNode
* new_node
= model_
->other_node()->GetChild(2);
400 EXPECT_EQ(BookmarkNode::FOLDER
, new_node
->type());
401 EXPECT_EQ(ASCIIToUTF16("new_F"), new_node
->GetTitle());
402 // The node should have one child.
403 ASSERT_EQ(1, new_node
->child_count());
404 const BookmarkNode
* new_child
= new_node
->GetChild(0);
405 // Make sure the child url/title match.
406 EXPECT_EQ(BookmarkNode::URL
, new_child
->type());
407 EXPECT_EQ(details
.urls
[0].second
, new_child
->GetTitle());
408 EXPECT_EQ(details
.urls
[0].first
, new_child
->url());
411 // Verifies the title of a new folder is updated correctly if ApplyEdits() is
412 // is invoked while focus is still on the text field.
413 TEST_F(BookmarkEditorViewTest
, NewFolderTitleUpdatedOnCommit
) {
414 const BookmarkNode
* parent
=
415 BookmarkModelFactory::GetForProfile(profile_
.get())->
416 bookmark_bar_node() ->GetChild(2);
418 CreateEditor(profile_
.get(), parent
,
419 BookmarkEditor::EditDetails::AddNodeInFolder(
420 parent
, 1, GURL(), base::string16()),
421 BookmarkEditorView::SHOW_TREE
);
424 SetURLText(UTF8ToUTF16(GURL(base_path() + "a").spec()));
425 SetTitleText(ASCIIToUTF16("new_a"));
428 ASSERT_TRUE(tree_view()->editor() != NULL
);
429 tree_view()->editor()->SetText(ASCIIToUTF16("modified"));
432 // Verify the new folder was added and title set appropriately.
433 ASSERT_EQ(1, parent
->child_count());
434 const BookmarkNode
* new_folder
= parent
->GetChild(0);
435 ASSERT_TRUE(new_folder
->is_folder());
436 EXPECT_EQ("modified", base::UTF16ToASCII(new_folder
->GetTitle()));