bookmarks: Simply reuse |model_| in BookmarkEditorViewTest.
[chromium-blink-merge.git] / chrome / browser / ui / views / bookmarks / bookmark_editor_view_unittest.cc
blob92e57266278843969be7e4ff03567f15a17eee6c
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"
7 #include <string>
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;
24 using base::Time;
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
31 // it with test data.
32 class BookmarkEditorViewTest : public testing::Test {
33 public:
34 BookmarkEditorViewTest()
35 : ui_thread_(BrowserThread::UI, &message_loop_),
36 file_thread_(BrowserThread::FILE, &message_loop_),
37 model_(NULL) {
40 void SetUp() override {
41 profile_.reset(new TestingProfile());
42 profile_->CreateBookmarkModel(true);
44 model_ = BookmarkModelFactory::GetForProfile(profile_.get());
45 bookmarks::test::WaitForBookmarkModelToLoad(model_);
47 AddTestData();
50 void TearDown() override {}
52 protected:
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,
72 configuration));
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);
84 void ApplyEdits() {
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);
97 void NewFolder() {
98 return editor_->NewFolder();
101 bool URLTFHasParent() {
102 if (editor_->details_.type == BookmarkEditor::EditDetails::NEW_FOLDER)
103 return false;
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_;
120 private:
121 // Creates the following structure:
122 // bookmark bar node
123 // a
124 // F1
125 // f1a
126 // F11
127 // f11a
128 // F2
129 // other node
130 // oa
131 // OF1
132 // of1a
133 void AddTestData() {
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());
165 } else {
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 = model_->bookmark_bar_node();
195 ASSERT_EQ(ASCIIToUTF16("new_a"), bb_node->GetChild(0)->GetTitle());
196 // The URL shouldn't have changed.
197 ASSERT_TRUE(GURL(base_path() + "a") == bb_node->GetChild(0)->url());
200 // Changes the url and makes sure parent/visual order doesn't change.
201 TEST_F(BookmarkEditorViewTest, EditURLKeepsPosition) {
202 Time node_time = Time::Now() + TimeDelta::FromDays(2);
203 GetMutableNode("a")->set_date_added(node_time);
204 CreateEditor(profile_.get(), NULL,
205 BookmarkEditor::EditDetails::EditNode(GetNode("a")),
206 BookmarkEditorView::SHOW_TREE);
208 SetURLText(UTF8ToUTF16(GURL(base_path() + "new_a").spec()));
210 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0));
212 const BookmarkNode* bb_node = model_->bookmark_bar_node();
213 ASSERT_EQ(ASCIIToUTF16("a"), bb_node->GetChild(0)->GetTitle());
214 // The URL should have changed.
215 ASSERT_TRUE(GURL(base_path() + "new_a") == bb_node->GetChild(0)->url());
216 ASSERT_TRUE(node_time == bb_node->GetChild(0)->date_added());
219 // Moves 'a' to be a child of the other node.
220 TEST_F(BookmarkEditorViewTest, ChangeParent) {
221 CreateEditor(profile_.get(), NULL,
222 BookmarkEditor::EditDetails::EditNode(GetNode("a")),
223 BookmarkEditorView::SHOW_TREE);
225 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(1));
227 const BookmarkNode* other_node = model_->other_node();
228 ASSERT_EQ(ASCIIToUTF16("a"), other_node->GetChild(2)->GetTitle());
229 ASSERT_TRUE(GURL(base_path() + "a") == other_node->GetChild(2)->url());
232 // Moves 'a' to be a child of the other node and changes its url to new_a.
233 TEST_F(BookmarkEditorViewTest, ChangeParentAndURL) {
234 Time node_time = Time::Now() + TimeDelta::FromDays(2);
235 GetMutableNode("a")->set_date_added(node_time);
236 CreateEditor(profile_.get(), NULL,
237 BookmarkEditor::EditDetails::EditNode(GetNode("a")),
238 BookmarkEditorView::SHOW_TREE);
240 SetURLText(UTF8ToUTF16(GURL(base_path() + "new_a").spec()));
242 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(1));
244 const BookmarkNode* other_node = model_->other_node();
245 ASSERT_EQ(ASCIIToUTF16("a"), other_node->GetChild(2)->GetTitle());
246 ASSERT_TRUE(GURL(base_path() + "new_a") == other_node->GetChild(2)->url());
247 ASSERT_TRUE(node_time == other_node->GetChild(2)->date_added());
250 // Creates a new folder and moves a node to it.
251 TEST_F(BookmarkEditorViewTest, MoveToNewParent) {
252 CreateEditor(profile_.get(), NULL,
253 BookmarkEditor::EditDetails::EditNode(GetNode("a")),
254 BookmarkEditorView::SHOW_TREE);
256 // Create two nodes: "F21" as a child of "F2" and "F211" as a child of "F21".
257 BookmarkEditorView::EditorNode* f2 =
258 editor_tree_model()->GetRoot()->GetChild(0)->GetChild(1);
259 BookmarkEditorView::EditorNode* f21 = AddNewFolder(f2);
260 f21->SetTitle(ASCIIToUTF16("F21"));
261 BookmarkEditorView::EditorNode* f211 = AddNewFolder(f21);
262 f211->SetTitle(ASCIIToUTF16("F211"));
264 // Parent the node to "F21".
265 ApplyEdits(f2);
267 const BookmarkNode* bb_node = model_->bookmark_bar_node();
268 const BookmarkNode* mf2 = bb_node->GetChild(1);
270 // F2 in the model should have two children now: F21 and the node edited.
271 ASSERT_EQ(2, mf2->child_count());
272 // F21 should be first.
273 ASSERT_EQ(ASCIIToUTF16("F21"), mf2->GetChild(0)->GetTitle());
274 // Then a.
275 ASSERT_EQ(ASCIIToUTF16("a"), mf2->GetChild(1)->GetTitle());
277 // F21 should have one child, F211.
278 const BookmarkNode* mf21 = mf2->GetChild(0);
279 ASSERT_EQ(1, mf21->child_count());
280 ASSERT_EQ(ASCIIToUTF16("F211"), mf21->GetChild(0)->GetTitle());
283 // Brings up the editor, creating a new URL on the bookmark bar.
284 TEST_F(BookmarkEditorViewTest, NewURL) {
285 const BookmarkNode* bb_node = model_->bookmark_bar_node();
287 CreateEditor(profile_.get(), bb_node,
288 BookmarkEditor::EditDetails::AddNodeInFolder(
289 bb_node, 1, GURL(), base::string16()),
290 BookmarkEditorView::SHOW_TREE);
292 SetURLText(UTF8ToUTF16(GURL(base_path() + "a").spec()));
293 SetTitleText(ASCIIToUTF16("new_a"));
295 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0));
297 ASSERT_EQ(4, bb_node->child_count());
299 const BookmarkNode* new_node = bb_node->GetChild(1);
301 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node->GetTitle());
302 EXPECT_TRUE(GURL(base_path() + "a") == new_node->url());
305 // Brings up the editor with no tree and modifies the url.
306 TEST_F(BookmarkEditorViewTest, ChangeURLNoTree) {
307 CreateEditor(profile_.get(), NULL,
308 BookmarkEditor::EditDetails::EditNode(
309 model_->other_node()->GetChild(0)),
310 BookmarkEditorView::NO_TREE);
312 SetURLText(UTF8ToUTF16(GURL(base_path() + "a").spec()));
313 SetTitleText(ASCIIToUTF16("new_a"));
315 ApplyEdits(NULL);
317 const BookmarkNode* other_node = model_->other_node();
318 ASSERT_EQ(2, other_node->child_count());
320 const BookmarkNode* new_node = other_node->GetChild(0);
322 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node->GetTitle());
323 EXPECT_TRUE(GURL(base_path() + "a") == new_node->url());
326 // Brings up the editor with no tree and modifies only the title.
327 TEST_F(BookmarkEditorViewTest, ChangeTitleNoTree) {
328 CreateEditor(profile_.get(), NULL,
329 BookmarkEditor::EditDetails::EditNode(
330 model_->other_node()->GetChild(0)),
331 BookmarkEditorView::NO_TREE);
333 SetTitleText(ASCIIToUTF16("new_a"));
335 ApplyEdits(NULL);
337 const BookmarkNode* other_node = model_->other_node();
338 ASSERT_EQ(2, other_node->child_count());
340 const BookmarkNode* new_node = other_node->GetChild(0);
342 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node->GetTitle());
345 // Creates a new folder.
346 TEST_F(BookmarkEditorViewTest, NewFolder) {
347 const BookmarkNode* bb_node = model_->bookmark_bar_node();
348 BookmarkEditor::EditDetails details =
349 BookmarkEditor::EditDetails::AddFolder(bb_node, 1);
350 details.urls.push_back(std::make_pair(GURL(base_path() + "x"),
351 ASCIIToUTF16("z")));
352 CreateEditor(profile_.get(), bb_node, details, BookmarkEditorView::SHOW_TREE);
354 // The url field shouldn't be visible.
355 EXPECT_FALSE(URLTFHasParent());
356 SetTitleText(ASCIIToUTF16("new_F"));
358 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0));
360 // Make sure the folder was created.
361 ASSERT_EQ(4, bb_node->child_count());
362 const BookmarkNode* new_node = bb_node->GetChild(1);
363 EXPECT_EQ(BookmarkNode::FOLDER, new_node->type());
364 EXPECT_EQ(ASCIIToUTF16("new_F"), new_node->GetTitle());
365 // The node should have one child.
366 ASSERT_EQ(1, new_node->child_count());
367 const BookmarkNode* new_child = new_node->GetChild(0);
368 // Make sure the child url/title match.
369 EXPECT_EQ(BookmarkNode::URL, new_child->type());
370 EXPECT_EQ(details.urls[0].second, new_child->GetTitle());
371 EXPECT_EQ(details.urls[0].first, new_child->url());
374 // Creates a new folder and selects a different folder for the folder to appear
375 // in then the editor is initially created showing.
376 TEST_F(BookmarkEditorViewTest, MoveFolder) {
377 BookmarkEditor::EditDetails details = BookmarkEditor::EditDetails::AddFolder(
378 model_->bookmark_bar_node(), -1);
379 details.urls.push_back(std::make_pair(GURL(base_path() + "x"),
380 ASCIIToUTF16("z")));
381 CreateEditor(profile_.get(), model_->bookmark_bar_node(),
382 details, BookmarkEditorView::SHOW_TREE);
384 SetTitleText(ASCIIToUTF16("new_F"));
386 // Create the folder in the 'other' folder.
387 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(1));
389 // Make sure the folder we edited is still there.
390 ASSERT_EQ(3, model_->other_node()->child_count());
391 const BookmarkNode* new_node = model_->other_node()->GetChild(2);
392 EXPECT_EQ(BookmarkNode::FOLDER, new_node->type());
393 EXPECT_EQ(ASCIIToUTF16("new_F"), new_node->GetTitle());
394 // The node should have one child.
395 ASSERT_EQ(1, new_node->child_count());
396 const BookmarkNode* new_child = new_node->GetChild(0);
397 // Make sure the child url/title match.
398 EXPECT_EQ(BookmarkNode::URL, new_child->type());
399 EXPECT_EQ(details.urls[0].second, new_child->GetTitle());
400 EXPECT_EQ(details.urls[0].first, new_child->url());
403 // Verifies the title of a new folder is updated correctly if ApplyEdits() is
404 // is invoked while focus is still on the text field.
405 TEST_F(BookmarkEditorViewTest, NewFolderTitleUpdatedOnCommit) {
406 const BookmarkNode* parent = model_->bookmark_bar_node()->GetChild(2);
408 CreateEditor(profile_.get(), parent,
409 BookmarkEditor::EditDetails::AddNodeInFolder(
410 parent, 1, GURL(), base::string16()),
411 BookmarkEditorView::SHOW_TREE);
412 ExpandAndSelect();
414 SetURLText(UTF8ToUTF16(GURL(base_path() + "a").spec()));
415 SetTitleText(ASCIIToUTF16("new_a"));
417 NewFolder();
418 ASSERT_TRUE(tree_view()->editor() != NULL);
419 tree_view()->editor()->SetText(ASCIIToUTF16("modified"));
420 ApplyEdits();
422 // Verify the new folder was added and title set appropriately.
423 ASSERT_EQ(1, parent->child_count());
424 const BookmarkNode* new_folder = parent->GetChild(0);
425 ASSERT_TRUE(new_folder->is_folder());
426 EXPECT_EQ("modified", base::UTF16ToASCII(new_folder->GetTitle()));