Chromium Code Reviews| 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 "chrome/browser/bookmarks/bookmark_utils.h" | 5 #include "chrome/browser/bookmarks/bookmark_utils.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "chrome/browser/bookmarks/base_bookmark_model_observer.h" | |
| 11 #include "chrome/browser/bookmarks/bookmark_model.h" | 12 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 12 #include "chrome/browser/bookmarks/bookmark_node_data.h" | 13 #include "chrome/browser/bookmarks/bookmark_node_data.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "ui/base/clipboard/clipboard.h" | 15 #include "ui/base/clipboard/clipboard.h" |
| 15 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 16 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 16 | 17 |
| 17 using base::ASCIIToUTF16; | 18 using base::ASCIIToUTF16; |
| 18 using std::string; | 19 using std::string; |
| 19 | 20 |
| 20 namespace bookmark_utils { | 21 namespace bookmark_utils { |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 class BookmarkUtilsTest : public ::testing::Test { | 24 class BookmarkUtilsTest : public testing::Test, |
| 25 public BaseBookmarkModelObserver { | |
| 24 public: | 26 public: |
| 27 BookmarkUtilsTest() { | |
| 28 ClearCounts(); | |
| 29 } | |
| 30 | |
| 25 virtual void TearDown() OVERRIDE { | 31 virtual void TearDown() OVERRIDE { |
| 26 ui::Clipboard::DestroyClipboardForCurrentThread(); | 32 ui::Clipboard::DestroyClipboardForCurrentThread(); |
| 27 } | 33 } |
| 28 | 34 |
| 35 void ClearCounts() { | |
| 36 grouped_changes_beginning_count_ = grouped_changes_ended_count_ = 0; | |
| 37 } | |
| 38 | |
| 39 void AssertExpectedGroupChanges(int expected_beginning_count, | |
|
sky
2014/02/21 20:54:38
Don't use assert in the name since it doesn't use
| |
| 40 int expected_ended_count) { | |
| 41 EXPECT_EQ(grouped_changes_beginning_count_, expected_beginning_count); | |
| 42 EXPECT_EQ(grouped_changes_ended_count_, expected_ended_count); | |
| 43 } | |
| 44 | |
| 45 // BookmarkModelObserver: | |
| 46 virtual void BookmarkModelChanged() OVERRIDE {} | |
| 47 | |
| 48 virtual void GroupedBookmarkChangesBeginning(BookmarkModel* model) OVERRIDE { | |
| 49 ++grouped_changes_beginning_count_; | |
| 50 } | |
| 51 | |
| 52 virtual void GroupedBookmarkChangesEnded(BookmarkModel* model) OVERRIDE { | |
| 53 ++grouped_changes_ended_count_; | |
| 54 } | |
| 55 | |
| 29 private: | 56 private: |
| 57 int grouped_changes_beginning_count_; | |
| 58 int grouped_changes_ended_count_; | |
| 59 | |
| 30 // Clipboard requires a message loop. | 60 // Clipboard requires a message loop. |
| 31 base::MessageLoopForUI loop; | 61 base::MessageLoopForUI loop; |
| 32 }; | 62 }; |
| 33 | 63 |
| 34 TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesWordPhraseQuery) { | 64 TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesWordPhraseQuery) { |
| 35 BookmarkModel model(NULL); | 65 BookmarkModel model(NULL); |
| 36 const BookmarkNode* node1 = model.AddURL(model.other_node(), | 66 const BookmarkNode* node1 = model.AddURL(model.other_node(), |
| 37 0, | 67 0, |
| 38 ASCIIToUTF16("foo bar"), | 68 ASCIIToUTF16("foo bar"), |
| 39 GURL("http://www.google.com")); | 69 GURL("http://www.google.com")); |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 ui::ScopedClipboardWriter clipboard_writer( | 265 ui::ScopedClipboardWriter clipboard_writer( |
| 236 ui::Clipboard::GetForCurrentThread(), | 266 ui::Clipboard::GetForCurrentThread(), |
| 237 ui::CLIPBOARD_TYPE_COPY_PASTE); | 267 ui::CLIPBOARD_TYPE_COPY_PASTE); |
| 238 clipboard_writer.WriteText(ASCIIToUTF16("foo")); | 268 clipboard_writer.WriteText(ASCIIToUTF16("foo")); |
| 239 } | 269 } |
| 240 | 270 |
| 241 // Now we shouldn't be able to paste from the clipboard. | 271 // Now we shouldn't be able to paste from the clipboard. |
| 242 EXPECT_FALSE(CanPasteFromClipboard(model.bookmark_bar_node())); | 272 EXPECT_FALSE(CanPasteFromClipboard(model.bookmark_bar_node())); |
| 243 } | 273 } |
| 244 | 274 |
| 275 TEST_F(BookmarkUtilsTest, CutToClipboard) { | |
| 276 BookmarkModel model(NULL); | |
| 277 model.AddObserver(this); | |
| 278 | |
| 279 base::string16 title(ASCIIToUTF16("foo")); | |
| 280 GURL url("http://foo.com"); | |
| 281 const BookmarkNode* n1 = model.AddURL(model.other_node(), 0, title, url); | |
| 282 const BookmarkNode* n2 = model.AddURL(model.other_node(), 1, title, url); | |
| 283 ClearCounts(); | |
| 284 | |
| 285 // Cut the nodes to the clipboard. | |
| 286 std::vector<const BookmarkNode*> nodes; | |
| 287 nodes.push_back(n1); | |
| 288 nodes.push_back(n2); | |
| 289 CopyToClipboard(&model, nodes, true); | |
| 290 | |
| 291 // Make sure the nodes were removed. | |
| 292 EXPECT_EQ(0, model.other_node()->child_count()); | |
| 293 | |
| 294 // Make sure observers were notified the set of changes should be grouped. | |
| 295 AssertExpectedGroupChanges(1, 1); | |
| 296 | |
| 297 // And make sure we can paste from the clipboard. | |
| 298 EXPECT_TRUE(CanPasteFromClipboard(model.bookmark_bar_node())); | |
| 299 } | |
| 300 | |
| 245 TEST_F(BookmarkUtilsTest, GetParentForNewNodes) { | 301 TEST_F(BookmarkUtilsTest, GetParentForNewNodes) { |
| 246 BookmarkModel model(NULL); | 302 BookmarkModel model(NULL); |
| 247 // This tests the case where selection contains one item and that item is a | 303 // This tests the case where selection contains one item and that item is a |
| 248 // folder. | 304 // folder. |
| 249 std::vector<const BookmarkNode*> nodes; | 305 std::vector<const BookmarkNode*> nodes; |
| 250 nodes.push_back(model.bookmark_bar_node()); | 306 nodes.push_back(model.bookmark_bar_node()); |
| 251 int index = -1; | 307 int index = -1; |
| 252 const BookmarkNode* real_parent = GetParentForNewNodes( | 308 const BookmarkNode* real_parent = GetParentForNewNodes( |
| 253 model.bookmark_bar_node(), nodes, &index); | 309 model.bookmark_bar_node(), nodes, &index); |
| 254 EXPECT_EQ(real_parent, model.bookmark_bar_node()); | 310 EXPECT_EQ(real_parent, model.bookmark_bar_node()); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 308 EXPECT_EQ(2u, clone->GetMetaInfoMap()->size()); | 364 EXPECT_EQ(2u, clone->GetMetaInfoMap()->size()); |
| 309 std::string value; | 365 std::string value; |
| 310 EXPECT_TRUE(clone->GetMetaInfo("somekey", &value)); | 366 EXPECT_TRUE(clone->GetMetaInfo("somekey", &value)); |
| 311 EXPECT_EQ("somevalue", value); | 367 EXPECT_EQ("somevalue", value); |
| 312 EXPECT_TRUE(clone->GetMetaInfo("someotherkey", &value)); | 368 EXPECT_TRUE(clone->GetMetaInfo("someotherkey", &value)); |
| 313 EXPECT_EQ("someothervalue", value); | 369 EXPECT_EQ("someothervalue", value); |
| 314 } | 370 } |
| 315 | 371 |
| 316 } // namespace | 372 } // namespace |
| 317 } // namespace bookmark_utils | 373 } // namespace bookmark_utils |
| OLD | NEW |