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. 4 5 #include "chrome/browser/bookmarks/bookmark_utils.h" 6 7 #include "base/message_loop/message_loop.h" 8 #include "base/strings/utf_string_conversions.h" 9 #include "chrome/browser/bookmarks/bookmark_model.h" 10 #include "testing/gtest/include/gtest/gtest.h" 11 #include "ui/base/clipboard/clipboard.h" 12 #include "ui/base/clipboard/scoped_clipboard_writer.h" 13 14 using std::string; 15 16 namespace bookmark_utils { 17 namespace { 18 19 class BookmarkUtilsTest : public ::testing::Test { 20 public: 21 virtual void TearDown() OVERRIDE { 22 ui::Clipboard::DestroyClipboardForCurrentThread(); 23 } 24 25 private: 26 // Clipboard requires a message loop. 27 base::MessageLoopForUI loop; 28 }; 29 30 TEST_F(BookmarkUtilsTest, GetBookmarksContainingText) { 31 BookmarkModel model(NULL); 32 const BookmarkNode* n1 = model.AddURL(model.other_node(), 33 0, 34 ASCIIToUTF16("foo bar"), 35 GURL("http://www.google.com")); 36 const BookmarkNode* n2 = model.AddURL(model.other_node(), 37 0, 38 ASCIIToUTF16("baz buz"), 39 GURL("http://www.cnn.com")); 40 41 model.AddFolder(model.other_node(), 0, ASCIIToUTF16("foo")); 42 43 std::vector<const BookmarkNode*> nodes; 44 GetBookmarksContainingText( 45 &model, ASCIIToUTF16("foo"), 100, string(), &nodes); 46 ASSERT_EQ(1U, nodes.size()); 47 EXPECT_TRUE(nodes[0] == n1); 48 EXPECT_TRUE(DoesBookmarkContainText(n1, ASCIIToUTF16("foo"), string())); 49 nodes.clear(); 50 51 GetBookmarksContainingText( 52 &model, ASCIIToUTF16("cnn"), 100, string(), &nodes); 53 ASSERT_EQ(1U, nodes.size()); 54 EXPECT_TRUE(nodes[0] == n2); 55 EXPECT_TRUE(DoesBookmarkContainText(n2, ASCIIToUTF16("cnn"), string())); 56 nodes.clear(); 57 58 GetBookmarksContainingText( 59 &model, ASCIIToUTF16("foo bar"), 100, string(), &nodes); 60 ASSERT_EQ(1U, nodes.size()); 61 EXPECT_TRUE(nodes[0] == n1); 62 EXPECT_TRUE(DoesBookmarkContainText(n1, ASCIIToUTF16("foo bar"), string())); 63 nodes.clear(); 64 } 65 66 TEST_F(BookmarkUtilsTest, DoesBookmarkContainText) { 67 BookmarkModel model(NULL); 68 const BookmarkNode* node = model.AddURL(model.other_node(), 69 0, 70 ASCIIToUTF16("foo bar"), 71 GURL("http://www.google.com")); 72 // Matches to the title. 73 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("ar"), string())); 74 // Matches to the URL. 75 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("www"), string())); 76 // No match. 77 EXPECT_FALSE(DoesBookmarkContainText(node, ASCIIToUTF16("cnn"), string())); 78 79 // Tests for a Japanese IDN. 80 const string16 kDecodedIdn = WideToUTF16(L"\x30B0\x30FC\x30B0\x30EB"); 81 node = model.AddURL(model.other_node(), 82 0, 83 ASCIIToUTF16("foo bar"), 84 GURL("http://xn--qcka1pmc.jp")); 85 // Unicode query doesn't match if languages have no "ja". 86 EXPECT_FALSE(DoesBookmarkContainText(node, kDecodedIdn, "en")); 87 // Unicode query matches if languages have "ja". 88 EXPECT_TRUE(DoesBookmarkContainText(node, kDecodedIdn, "ja")); 89 // Punycode query also matches as ever. 90 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("qcka1pmc"), "ja")); 91 92 // Tests with various lower/upper case characters. 93 node = model.AddURL(model.other_node(), 94 0, 95 ASCIIToUTF16("FOO bar"), 96 GURL("http://www.google.com/search?q=ABC")); 97 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("foo"), string())); 98 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("Foo"), string())); 99 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("FOO"), string())); 100 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("google abc"), 101 string())); 102 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("google ABC"), 103 string())); 104 EXPECT_TRUE(DoesBookmarkContainText( 105 node, ASCIIToUTF16("http://www.google.com/search?q=A"), string())); 106 107 // Test with accents. 108 node = model.AddURL(model.other_node(), 109 0, 110 WideToUTF16(L"fr\u00E4n\u00E7\u00F3s\u00EA"), 111 GURL("http://www.google.com/search?q=FBA")); 112 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("francose"), 113 string())); 114 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("FrAnCoSe"), 115 string())); 116 EXPECT_TRUE(DoesBookmarkContainText(node, WideToUTF16(L"fr\u00E4ncose"), 117 string())); 118 EXPECT_TRUE(DoesBookmarkContainText(node, WideToUTF16(L"fran\u00E7ose"), 119 string())); 120 EXPECT_TRUE(DoesBookmarkContainText(node, WideToUTF16(L"franc\u00F3se"), 121 string())); 122 EXPECT_TRUE(DoesBookmarkContainText(node, WideToUTF16(L"francos\u00EA"), 123 string())); 124 EXPECT_TRUE(DoesBookmarkContainText( 125 node, WideToUTF16(L"Fr\u00C4n\u00C7\u00F3S\u00EA"), string())); 126 EXPECT_TRUE(DoesBookmarkContainText( 127 node, WideToUTF16(L"fr\u00C4n\u00C7\u00D3s\u00CA"), string())); 128 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("fba"), string())); 129 EXPECT_TRUE(DoesBookmarkContainText(node, ASCIIToUTF16("FBA"), string())); 130 } 131 132 TEST_F(BookmarkUtilsTest, CopyPaste) { 133 BookmarkModel model(NULL); 134 const BookmarkNode* node = model.AddURL(model.other_node(), 135 0, 136 ASCIIToUTF16("foo bar"), 137 GURL("http://www.google.com")); 138 139 // Copy a node to the clipboard. 140 std::vector<const BookmarkNode*> nodes; 141 nodes.push_back(node); 142 CopyToClipboard(&model, nodes, false); 143 144 // And make sure we can paste a bookmark from the clipboard. 145 EXPECT_TRUE(CanPasteFromClipboard(model.bookmark_bar_node())); 146 147 // Write some text to the clipboard. 148 { 149 ui::ScopedClipboardWriter clipboard_writer( 150 ui::Clipboard::GetForCurrentThread(), 151 ui::Clipboard::BUFFER_STANDARD); 152 clipboard_writer.WriteText(ASCIIToUTF16("foo")); 153 } 154 155 // Now we shouldn't be able to paste from the clipboard. 156 EXPECT_FALSE(CanPasteFromClipboard(model.bookmark_bar_node())); 157 } 158 159 TEST_F(BookmarkUtilsTest, GetParentForNewNodes) { 160 BookmarkModel model(NULL); 161 // This tests the case where selection contains one item and that item is a 162 // folder. 163 std::vector<const BookmarkNode*> nodes; 164 nodes.push_back(model.bookmark_bar_node()); 165 int index = -1; 166 const BookmarkNode* real_parent = GetParentForNewNodes( 167 model.bookmark_bar_node(), nodes, &index); 168 EXPECT_EQ(real_parent, model.bookmark_bar_node()); 169 EXPECT_EQ(0, index); 170 171 nodes.clear(); 172 173 // This tests the case where selection contains one item and that item is an 174 // url. 175 const BookmarkNode* page1 = model.AddURL(model.bookmark_bar_node(), 0, 176 ASCIIToUTF16("Google"), 177 GURL("http://google.com")); 178 nodes.push_back(page1); 179 real_parent = GetParentForNewNodes(model.bookmark_bar_node(), nodes, &index); 180 EXPECT_EQ(real_parent, model.bookmark_bar_node()); 181 EXPECT_EQ(1, index); 182 183 // This tests the case where selection has more than one item. 184 const BookmarkNode* folder1 = model.AddFolder(model.bookmark_bar_node(), 1, 185 ASCIIToUTF16("Folder 1")); 186 nodes.push_back(folder1); 187 real_parent = GetParentForNewNodes(model.bookmark_bar_node(), nodes, &index); 188 EXPECT_EQ(real_parent, model.bookmark_bar_node()); 189 EXPECT_EQ(2, index); 190 191 // This tests the case where selection doesn't contain any items. 192 nodes.clear(); 193 real_parent = GetParentForNewNodes(model.bookmark_bar_node(), nodes, &index); 194 EXPECT_EQ(real_parent, model.bookmark_bar_node()); 195 EXPECT_EQ(2, index); 196 } 197 198 } // namespace 199 } // namespace bookmark_utils 200