OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
10 #include "chrome/browser/bookmarks/bookmark_model.h" | 10 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
11 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
12 #include "chrome/browser/ui/views/bookmarks/bookmark_editor_view.h" | 13 #include "chrome/browser/ui/views/bookmarks/bookmark_editor_view.h" |
13 #include "chrome/test/base/testing_profile.h" | 14 #include "chrome/test/base/testing_profile.h" |
14 #include "content/public/test/test_browser_thread.h" | 15 #include "content/public/test/test_browser_thread.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 using base::Time; | 18 using base::Time; |
18 using base::TimeDelta; | 19 using base::TimeDelta; |
19 using content::BrowserThread; | 20 using content::BrowserThread; |
20 | 21 |
21 // Base class for bookmark editor tests. Creates a BookmarkModel and populates | 22 // Base class for bookmark editor tests. Creates a BookmarkModel and populates |
22 // it with test data. | 23 // it with test data. |
23 class BookmarkEditorViewTest : public testing::Test { | 24 class BookmarkEditorViewTest : public testing::Test { |
24 public: | 25 public: |
25 BookmarkEditorViewTest() | 26 BookmarkEditorViewTest() |
26 : ui_thread_(BrowserThread::UI, &message_loop_), | 27 : ui_thread_(BrowserThread::UI, &message_loop_), |
27 file_thread_(BrowserThread::FILE, &message_loop_), | 28 file_thread_(BrowserThread::FILE, &message_loop_), |
28 model_(NULL) { | 29 model_(NULL) { |
29 } | 30 } |
30 | 31 |
31 virtual void SetUp() { | 32 virtual void SetUp() { |
32 profile_.reset(new TestingProfile()); | 33 profile_.reset(new TestingProfile()); |
33 profile_->CreateBookmarkModel(true); | 34 profile_->CreateBookmarkModel(true); |
34 | 35 |
35 model_ = profile_->GetBookmarkModel(); | 36 model_ = BookmarkModelFactory::GetForProfile(profile_.get()); |
36 profile_->BlockUntilBookmarkModelLoaded(); | 37 profile_->BlockUntilBookmarkModelLoaded(); |
37 | 38 |
38 AddTestData(); | 39 AddTestData(); |
39 } | 40 } |
40 | 41 |
41 virtual void TearDown() { | 42 virtual void TearDown() { |
42 } | 43 } |
43 | 44 |
44 protected: | 45 protected: |
45 std::string base_path() const { return "file:///c:/tmp/"; } | 46 std::string base_path() const { return "file:///c:/tmp/"; } |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 // Changes the title and makes sure parent/visual order doesn't change. | 164 // Changes the title and makes sure parent/visual order doesn't change. |
164 TEST_F(BookmarkEditorViewTest, EditTitleKeepsPosition) { | 165 TEST_F(BookmarkEditorViewTest, EditTitleKeepsPosition) { |
165 CreateEditor(profile_.get(), NULL, | 166 CreateEditor(profile_.get(), NULL, |
166 BookmarkEditor::EditDetails::EditNode(GetNode("a")), | 167 BookmarkEditor::EditDetails::EditNode(GetNode("a")), |
167 BookmarkEditorView::SHOW_TREE); | 168 BookmarkEditorView::SHOW_TREE); |
168 SetTitleText(L"new_a"); | 169 SetTitleText(L"new_a"); |
169 | 170 |
170 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0)); | 171 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0)); |
171 | 172 |
172 const BookmarkNode* bb_node = | 173 const BookmarkNode* bb_node = |
173 profile_->GetBookmarkModel()->bookmark_bar_node(); | 174 BookmarkModelFactory::GetForProfile(profile_.get())->bookmark_bar_node(); |
174 ASSERT_EQ(ASCIIToUTF16("new_a"), bb_node->GetChild(0)->GetTitle()); | 175 ASSERT_EQ(ASCIIToUTF16("new_a"), bb_node->GetChild(0)->GetTitle()); |
175 // The URL shouldn't have changed. | 176 // The URL shouldn't have changed. |
176 ASSERT_TRUE(GURL(base_path() + "a") == bb_node->GetChild(0)->url()); | 177 ASSERT_TRUE(GURL(base_path() + "a") == bb_node->GetChild(0)->url()); |
177 } | 178 } |
178 | 179 |
179 // Changes the url and makes sure parent/visual order doesn't change. | 180 // Changes the url and makes sure parent/visual order doesn't change. |
180 TEST_F(BookmarkEditorViewTest, EditURLKeepsPosition) { | 181 TEST_F(BookmarkEditorViewTest, EditURLKeepsPosition) { |
181 Time node_time = Time::Now() + TimeDelta::FromDays(2); | 182 Time node_time = Time::Now() + TimeDelta::FromDays(2); |
182 GetMutableNode("a")->set_date_added(node_time); | 183 GetMutableNode("a")->set_date_added(node_time); |
183 CreateEditor(profile_.get(), NULL, | 184 CreateEditor(profile_.get(), NULL, |
184 BookmarkEditor::EditDetails::EditNode(GetNode("a")), | 185 BookmarkEditor::EditDetails::EditNode(GetNode("a")), |
185 BookmarkEditorView::SHOW_TREE); | 186 BookmarkEditorView::SHOW_TREE); |
186 | 187 |
187 SetURLText(UTF8ToWide(GURL(base_path() + "new_a").spec())); | 188 SetURLText(UTF8ToWide(GURL(base_path() + "new_a").spec())); |
188 | 189 |
189 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0)); | 190 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0)); |
190 | 191 |
191 const BookmarkNode* bb_node = | 192 const BookmarkNode* bb_node = |
192 profile_->GetBookmarkModel()->bookmark_bar_node(); | 193 BookmarkModelFactory::GetForProfile(profile_.get())->bookmark_bar_node(); |
193 ASSERT_EQ(ASCIIToUTF16("a"), bb_node->GetChild(0)->GetTitle()); | 194 ASSERT_EQ(ASCIIToUTF16("a"), bb_node->GetChild(0)->GetTitle()); |
194 // The URL should have changed. | 195 // The URL should have changed. |
195 ASSERT_TRUE(GURL(base_path() + "new_a") == bb_node->GetChild(0)->url()); | 196 ASSERT_TRUE(GURL(base_path() + "new_a") == bb_node->GetChild(0)->url()); |
196 ASSERT_TRUE(node_time == bb_node->GetChild(0)->date_added()); | 197 ASSERT_TRUE(node_time == bb_node->GetChild(0)->date_added()); |
197 } | 198 } |
198 | 199 |
199 // Moves 'a' to be a child of the other node. | 200 // Moves 'a' to be a child of the other node. |
200 TEST_F(BookmarkEditorViewTest, ChangeParent) { | 201 TEST_F(BookmarkEditorViewTest, ChangeParent) { |
201 CreateEditor(profile_.get(), NULL, | 202 CreateEditor(profile_.get(), NULL, |
202 BookmarkEditor::EditDetails::EditNode(GetNode("a")), | 203 BookmarkEditor::EditDetails::EditNode(GetNode("a")), |
203 BookmarkEditorView::SHOW_TREE); | 204 BookmarkEditorView::SHOW_TREE); |
204 | 205 |
205 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(1)); | 206 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(1)); |
206 | 207 |
207 const BookmarkNode* other_node = profile_->GetBookmarkModel()->other_node(); | 208 const BookmarkNode* other_node = |
| 209 BookmarkModelFactory::GetForProfile(profile_.get())->other_node(); |
208 ASSERT_EQ(ASCIIToUTF16("a"), other_node->GetChild(2)->GetTitle()); | 210 ASSERT_EQ(ASCIIToUTF16("a"), other_node->GetChild(2)->GetTitle()); |
209 ASSERT_TRUE(GURL(base_path() + "a") == other_node->GetChild(2)->url()); | 211 ASSERT_TRUE(GURL(base_path() + "a") == other_node->GetChild(2)->url()); |
210 } | 212 } |
211 | 213 |
212 // Moves 'a' to be a child of the other node and changes its url to new_a. | 214 // Moves 'a' to be a child of the other node and changes its url to new_a. |
213 TEST_F(BookmarkEditorViewTest, ChangeParentAndURL) { | 215 TEST_F(BookmarkEditorViewTest, ChangeParentAndURL) { |
214 Time node_time = Time::Now() + TimeDelta::FromDays(2); | 216 Time node_time = Time::Now() + TimeDelta::FromDays(2); |
215 GetMutableNode("a")->set_date_added(node_time); | 217 GetMutableNode("a")->set_date_added(node_time); |
216 CreateEditor(profile_.get(), NULL, | 218 CreateEditor(profile_.get(), NULL, |
217 BookmarkEditor::EditDetails::EditNode(GetNode("a")), | 219 BookmarkEditor::EditDetails::EditNode(GetNode("a")), |
218 BookmarkEditorView::SHOW_TREE); | 220 BookmarkEditorView::SHOW_TREE); |
219 | 221 |
220 SetURLText(UTF8ToWide(GURL(base_path() + "new_a").spec())); | 222 SetURLText(UTF8ToWide(GURL(base_path() + "new_a").spec())); |
221 | 223 |
222 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(1)); | 224 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(1)); |
223 | 225 |
224 const BookmarkNode* other_node = profile_->GetBookmarkModel()->other_node(); | 226 const BookmarkNode* other_node = |
| 227 BookmarkModelFactory::GetForProfile(profile_.get())->other_node(); |
225 ASSERT_EQ(ASCIIToUTF16("a"), other_node->GetChild(2)->GetTitle()); | 228 ASSERT_EQ(ASCIIToUTF16("a"), other_node->GetChild(2)->GetTitle()); |
226 ASSERT_TRUE(GURL(base_path() + "new_a") == other_node->GetChild(2)->url()); | 229 ASSERT_TRUE(GURL(base_path() + "new_a") == other_node->GetChild(2)->url()); |
227 ASSERT_TRUE(node_time == other_node->GetChild(2)->date_added()); | 230 ASSERT_TRUE(node_time == other_node->GetChild(2)->date_added()); |
228 } | 231 } |
229 | 232 |
230 // Creates a new folder and moves a node to it. | 233 // Creates a new folder and moves a node to it. |
231 TEST_F(BookmarkEditorViewTest, MoveToNewParent) { | 234 TEST_F(BookmarkEditorViewTest, MoveToNewParent) { |
232 CreateEditor(profile_.get(), NULL, | 235 CreateEditor(profile_.get(), NULL, |
233 BookmarkEditor::EditDetails::EditNode(GetNode("a")), | 236 BookmarkEditor::EditDetails::EditNode(GetNode("a")), |
234 BookmarkEditorView::SHOW_TREE); | 237 BookmarkEditorView::SHOW_TREE); |
235 | 238 |
236 // Create two nodes: "F21" as a child of "F2" and "F211" as a child of "F21". | 239 // Create two nodes: "F21" as a child of "F2" and "F211" as a child of "F21". |
237 BookmarkEditorView::EditorNode* f2 = | 240 BookmarkEditorView::EditorNode* f2 = |
238 editor_tree_model()->GetRoot()->GetChild(0)->GetChild(1); | 241 editor_tree_model()->GetRoot()->GetChild(0)->GetChild(1); |
239 BookmarkEditorView::EditorNode* f21 = AddNewFolder(f2); | 242 BookmarkEditorView::EditorNode* f21 = AddNewFolder(f2); |
240 f21->SetTitle(ASCIIToUTF16("F21")); | 243 f21->SetTitle(ASCIIToUTF16("F21")); |
241 BookmarkEditorView::EditorNode* f211 = AddNewFolder(f21); | 244 BookmarkEditorView::EditorNode* f211 = AddNewFolder(f21); |
242 f211->SetTitle(ASCIIToUTF16("F211")); | 245 f211->SetTitle(ASCIIToUTF16("F211")); |
243 | 246 |
244 // Parent the node to "F21". | 247 // Parent the node to "F21". |
245 ApplyEdits(f2); | 248 ApplyEdits(f2); |
246 | 249 |
247 const BookmarkNode* bb_node = | 250 const BookmarkNode* bb_node = |
248 profile_->GetBookmarkModel()->bookmark_bar_node(); | 251 BookmarkModelFactory::GetForProfile(profile_.get())->bookmark_bar_node(); |
249 const BookmarkNode* mf2 = bb_node->GetChild(1); | 252 const BookmarkNode* mf2 = bb_node->GetChild(1); |
250 | 253 |
251 // F2 in the model should have two children now: F21 and the node edited. | 254 // F2 in the model should have two children now: F21 and the node edited. |
252 ASSERT_EQ(2, mf2->child_count()); | 255 ASSERT_EQ(2, mf2->child_count()); |
253 // F21 should be first. | 256 // F21 should be first. |
254 ASSERT_EQ(ASCIIToUTF16("F21"), mf2->GetChild(0)->GetTitle()); | 257 ASSERT_EQ(ASCIIToUTF16("F21"), mf2->GetChild(0)->GetTitle()); |
255 // Then a. | 258 // Then a. |
256 ASSERT_EQ(ASCIIToUTF16("a"), mf2->GetChild(1)->GetTitle()); | 259 ASSERT_EQ(ASCIIToUTF16("a"), mf2->GetChild(1)->GetTitle()); |
257 | 260 |
258 // F21 should have one child, F211. | 261 // F21 should have one child, F211. |
259 const BookmarkNode* mf21 = mf2->GetChild(0); | 262 const BookmarkNode* mf21 = mf2->GetChild(0); |
260 ASSERT_EQ(1, mf21->child_count()); | 263 ASSERT_EQ(1, mf21->child_count()); |
261 ASSERT_EQ(ASCIIToUTF16("F211"), mf21->GetChild(0)->GetTitle()); | 264 ASSERT_EQ(ASCIIToUTF16("F211"), mf21->GetChild(0)->GetTitle()); |
262 } | 265 } |
263 | 266 |
264 // Brings up the editor, creating a new URL on the bookmark bar. | 267 // Brings up the editor, creating a new URL on the bookmark bar. |
265 TEST_F(BookmarkEditorViewTest, NewURL) { | 268 TEST_F(BookmarkEditorViewTest, NewURL) { |
266 CreateEditor(profile_.get(), NULL, | 269 CreateEditor(profile_.get(), NULL, |
267 BookmarkEditor::EditDetails::AddNodeInFolder( | 270 BookmarkEditor::EditDetails::AddNodeInFolder( |
268 NULL, -1, GURL(), string16()), | 271 NULL, -1, GURL(), string16()), |
269 BookmarkEditorView::SHOW_TREE); | 272 BookmarkEditorView::SHOW_TREE); |
270 | 273 |
271 SetURLText(UTF8ToWide(GURL(base_path() + "a").spec())); | 274 SetURLText(UTF8ToWide(GURL(base_path() + "a").spec())); |
272 SetTitleText(L"new_a"); | 275 SetTitleText(L"new_a"); |
273 | 276 |
274 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0)); | 277 ApplyEdits(editor_tree_model()->GetRoot()->GetChild(0)); |
275 | 278 |
276 const BookmarkNode* bb_node = | 279 const BookmarkNode* bb_node = |
277 profile_->GetBookmarkModel()->bookmark_bar_node(); | 280 BookmarkModelFactory::GetForProfile(profile_.get())->bookmark_bar_node(); |
278 ASSERT_EQ(4, bb_node->child_count()); | 281 ASSERT_EQ(4, bb_node->child_count()); |
279 | 282 |
280 const BookmarkNode* new_node = bb_node->GetChild(3); | 283 const BookmarkNode* new_node = bb_node->GetChild(3); |
281 | 284 |
282 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node->GetTitle()); | 285 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node->GetTitle()); |
283 EXPECT_TRUE(GURL(base_path() + "a") == new_node->url()); | 286 EXPECT_TRUE(GURL(base_path() + "a") == new_node->url()); |
284 } | 287 } |
285 | 288 |
286 // Brings up the editor with no tree and modifies the url. | 289 // Brings up the editor with no tree and modifies the url. |
287 TEST_F(BookmarkEditorViewTest, ChangeURLNoTree) { | 290 TEST_F(BookmarkEditorViewTest, ChangeURLNoTree) { |
288 CreateEditor(profile_.get(), NULL, | 291 CreateEditor(profile_.get(), NULL, |
289 BookmarkEditor::EditDetails::EditNode( | 292 BookmarkEditor::EditDetails::EditNode( |
290 model_->other_node()->GetChild(0)), | 293 model_->other_node()->GetChild(0)), |
291 BookmarkEditorView::NO_TREE); | 294 BookmarkEditorView::NO_TREE); |
292 | 295 |
293 SetURLText(UTF8ToWide(GURL(base_path() + "a").spec())); | 296 SetURLText(UTF8ToWide(GURL(base_path() + "a").spec())); |
294 SetTitleText(L"new_a"); | 297 SetTitleText(L"new_a"); |
295 | 298 |
296 ApplyEdits(NULL); | 299 ApplyEdits(NULL); |
297 | 300 |
298 const BookmarkNode* other_node = profile_->GetBookmarkModel()->other_node(); | 301 const BookmarkNode* other_node = |
| 302 BookmarkModelFactory::GetForProfile(profile_.get())->other_node(); |
299 ASSERT_EQ(2, other_node->child_count()); | 303 ASSERT_EQ(2, other_node->child_count()); |
300 | 304 |
301 const BookmarkNode* new_node = other_node->GetChild(0); | 305 const BookmarkNode* new_node = other_node->GetChild(0); |
302 | 306 |
303 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node->GetTitle()); | 307 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node->GetTitle()); |
304 EXPECT_TRUE(GURL(base_path() + "a") == new_node->url()); | 308 EXPECT_TRUE(GURL(base_path() + "a") == new_node->url()); |
305 } | 309 } |
306 | 310 |
307 // Brings up the editor with no tree and modifies only the title. | 311 // Brings up the editor with no tree and modifies only the title. |
308 TEST_F(BookmarkEditorViewTest, ChangeTitleNoTree) { | 312 TEST_F(BookmarkEditorViewTest, ChangeTitleNoTree) { |
309 CreateEditor(profile_.get(), NULL, | 313 CreateEditor(profile_.get(), NULL, |
310 BookmarkEditor::EditDetails::EditNode( | 314 BookmarkEditor::EditDetails::EditNode( |
311 model_->other_node()->GetChild(0)), | 315 model_->other_node()->GetChild(0)), |
312 BookmarkEditorView::NO_TREE); | 316 BookmarkEditorView::NO_TREE); |
313 | 317 |
314 SetTitleText(L"new_a"); | 318 SetTitleText(L"new_a"); |
315 | 319 |
316 ApplyEdits(NULL); | 320 ApplyEdits(NULL); |
317 | 321 |
318 const BookmarkNode* other_node = profile_->GetBookmarkModel()->other_node(); | 322 const BookmarkNode* other_node = |
| 323 BookmarkModelFactory::GetForProfile(profile_.get())->other_node(); |
319 ASSERT_EQ(2, other_node->child_count()); | 324 ASSERT_EQ(2, other_node->child_count()); |
320 | 325 |
321 const BookmarkNode* new_node = other_node->GetChild(0); | 326 const BookmarkNode* new_node = other_node->GetChild(0); |
322 | 327 |
323 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node->GetTitle()); | 328 EXPECT_EQ(ASCIIToUTF16("new_a"), new_node->GetTitle()); |
324 } | 329 } |
325 | 330 |
326 // Creates a new folder. | 331 // Creates a new folder. |
327 TEST_F(BookmarkEditorViewTest, NewFolder) { | 332 TEST_F(BookmarkEditorViewTest, NewFolder) { |
328 const BookmarkNode* bb_node = model_->bookmark_bar_node(); | 333 const BookmarkNode* bb_node = model_->bookmark_bar_node(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 EXPECT_EQ(BookmarkNode::FOLDER, new_node->type()); | 378 EXPECT_EQ(BookmarkNode::FOLDER, new_node->type()); |
374 EXPECT_EQ(ASCIIToUTF16("new_F"), new_node->GetTitle()); | 379 EXPECT_EQ(ASCIIToUTF16("new_F"), new_node->GetTitle()); |
375 // The node should have one child. | 380 // The node should have one child. |
376 ASSERT_EQ(1, new_node->child_count()); | 381 ASSERT_EQ(1, new_node->child_count()); |
377 const BookmarkNode* new_child = new_node->GetChild(0); | 382 const BookmarkNode* new_child = new_node->GetChild(0); |
378 // Make sure the child url/title match. | 383 // Make sure the child url/title match. |
379 EXPECT_EQ(BookmarkNode::URL, new_child->type()); | 384 EXPECT_EQ(BookmarkNode::URL, new_child->type()); |
380 EXPECT_EQ(details.urls[0].second, new_child->GetTitle()); | 385 EXPECT_EQ(details.urls[0].second, new_child->GetTitle()); |
381 EXPECT_EQ(details.urls[0].first, new_child->url()); | 386 EXPECT_EQ(details.urls[0].first, new_child->url()); |
382 } | 387 } |
OLD | NEW |