NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / browser / bookmarks / bookmark_utils_unittest.cc
bloba8c4132d9845154c082205f1f127fa58cc1aa265
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/bookmarks/bookmark_utils.h"
7 #include <vector>
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/bookmarks/bookmark_model.h"
12 #include "chrome/browser/bookmarks/bookmark_node_data.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "ui/base/clipboard/clipboard.h"
15 #include "ui/base/clipboard/scoped_clipboard_writer.h"
17 using base::ASCIIToUTF16;
18 using std::string;
20 namespace bookmark_utils {
21 namespace {
23 class BookmarkUtilsTest : public ::testing::Test {
24 public:
25 virtual void TearDown() OVERRIDE {
26 ui::Clipboard::DestroyClipboardForCurrentThread();
29 private:
30 // Clipboard requires a message loop.
31 base::MessageLoopForUI loop;
34 TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesWordPhraseQuery) {
35 BookmarkModel model(NULL);
36 const BookmarkNode* node1 = model.AddURL(model.other_node(),
38 ASCIIToUTF16("foo bar"),
39 GURL("http://www.google.com"));
40 const BookmarkNode* node2 = model.AddURL(model.other_node(),
42 ASCIIToUTF16("baz buz"),
43 GURL("http://www.cnn.com"));
44 const BookmarkNode* folder1 = model.AddFolder(model.other_node(),
46 ASCIIToUTF16("foo"));
47 std::vector<const BookmarkNode*> nodes;
48 QueryFields query;
49 query.word_phrase_query.reset(new base::string16);
50 // No nodes are returned for empty string.
51 *query.word_phrase_query = ASCIIToUTF16("");
52 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
53 EXPECT_TRUE(nodes.empty());
54 nodes.clear();
56 // No nodes are returned for space-only string.
57 *query.word_phrase_query = ASCIIToUTF16(" ");
58 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
59 EXPECT_TRUE(nodes.empty());
60 nodes.clear();
62 // Node "foo bar" and folder "foo" are returned in search results.
63 *query.word_phrase_query = ASCIIToUTF16("foo");
64 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
65 ASSERT_EQ(2U, nodes.size());
66 EXPECT_TRUE(nodes[0] == folder1);
67 EXPECT_TRUE(nodes[1] == node1);
68 nodes.clear();
70 // Ensure url matches return in search results.
71 *query.word_phrase_query = ASCIIToUTF16("cnn");
72 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
73 ASSERT_EQ(1U, nodes.size());
74 EXPECT_TRUE(nodes[0] == node2);
75 nodes.clear();
77 // Ensure folder "foo" is not returned in more specific search.
78 *query.word_phrase_query = ASCIIToUTF16("foo bar");
79 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
80 ASSERT_EQ(1U, nodes.size());
81 EXPECT_TRUE(nodes[0] == node1);
82 nodes.clear();
84 // Bookmark Bar and Other Bookmarks are not returned in search results.
85 *query.word_phrase_query = ASCIIToUTF16("Bookmark");
86 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
87 ASSERT_EQ(0U, nodes.size());
88 nodes.clear();
91 // Check exact matching against a URL query.
92 TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesUrl) {
93 BookmarkModel model(NULL);
94 const BookmarkNode* node1 = model.AddURL(model.other_node(),
96 ASCIIToUTF16("Google"),
97 GURL("https://www.google.com/"));
98 model.AddURL(model.other_node(),
100 ASCIIToUTF16("Google Calendar"),
101 GURL("https://www.google.com/calendar"));
103 model.AddFolder(model.other_node(),
105 ASCIIToUTF16("Folder"));
107 std::vector<const BookmarkNode*> nodes;
108 QueryFields query;
109 query.url.reset(new base::string16);
110 *query.url = ASCIIToUTF16("https://www.google.com/");
111 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
112 ASSERT_EQ(1U, nodes.size());
113 EXPECT_TRUE(nodes[0] == node1);
114 nodes.clear();
116 *query.url = ASCIIToUTF16("calendar");
117 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
118 ASSERT_EQ(0U, nodes.size());
119 nodes.clear();
121 // Empty URL should not match folders.
122 *query.url = ASCIIToUTF16("");
123 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
124 ASSERT_EQ(0U, nodes.size());
125 nodes.clear();
128 // Check exact matching against a title query.
129 TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesTitle) {
130 BookmarkModel model(NULL);
131 const BookmarkNode* node1 = model.AddURL(model.other_node(),
133 ASCIIToUTF16("Google"),
134 GURL("https://www.google.com/"));
135 model.AddURL(model.other_node(),
137 ASCIIToUTF16("Google Calendar"),
138 GURL("https://www.google.com/calendar"));
140 const BookmarkNode* folder1 = model.AddFolder(model.other_node(),
142 ASCIIToUTF16("Folder"));
144 std::vector<const BookmarkNode*> nodes;
145 QueryFields query;
146 query.title.reset(new base::string16);
147 *query.title = ASCIIToUTF16("Google");
148 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
149 ASSERT_EQ(1U, nodes.size());
150 EXPECT_TRUE(nodes[0] == node1);
151 nodes.clear();
153 *query.title = ASCIIToUTF16("Calendar");
154 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
155 ASSERT_EQ(0U, nodes.size());
156 nodes.clear();
158 // Title should match folders.
159 *query.title = ASCIIToUTF16("Folder");
160 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
161 ASSERT_EQ(1U, nodes.size());
162 EXPECT_TRUE(nodes[0] == folder1);
163 nodes.clear();
166 // Check matching against a query with multiple predicates.
167 TEST_F(BookmarkUtilsTest, GetBookmarksMatchingPropertiesConjunction) {
168 BookmarkModel model(NULL);
169 const BookmarkNode* node1 = model.AddURL(model.other_node(),
171 ASCIIToUTF16("Google"),
172 GURL("https://www.google.com/"));
173 model.AddURL(model.other_node(),
175 ASCIIToUTF16("Google Calendar"),
176 GURL("https://www.google.com/calendar"));
178 model.AddFolder(model.other_node(),
180 ASCIIToUTF16("Folder"));
182 std::vector<const BookmarkNode*> nodes;
183 QueryFields query;
185 // Test all fields matching.
186 query.word_phrase_query.reset(new base::string16(ASCIIToUTF16("www")));
187 query.url.reset(new base::string16(ASCIIToUTF16("https://www.google.com/")));
188 query.title.reset(new base::string16(ASCIIToUTF16("Google")));
189 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
190 ASSERT_EQ(1U, nodes.size());
191 EXPECT_TRUE(nodes[0] == node1);
192 nodes.clear();
194 scoped_ptr<base::string16>* fields[] = {
195 &query.word_phrase_query, &query.url, &query.title };
197 // Test two fields matching.
198 for (size_t i = 0; i < arraysize(fields); i++) {
199 scoped_ptr<base::string16> original_value(fields[i]->release());
200 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
201 ASSERT_EQ(1U, nodes.size());
202 EXPECT_TRUE(nodes[0] == node1);
203 nodes.clear();
204 fields[i]->reset(original_value.release());
207 // Test two fields matching with one non-matching field.
208 for (size_t i = 0; i < arraysize(fields); i++) {
209 scoped_ptr<base::string16> original_value(fields[i]->release());
210 fields[i]->reset(new base::string16(ASCIIToUTF16("fjdkslafjkldsa")));
211 GetBookmarksMatchingProperties(&model, query, 100, string(), &nodes);
212 ASSERT_EQ(0U, nodes.size());
213 nodes.clear();
214 fields[i]->reset(original_value.release());
218 TEST_F(BookmarkUtilsTest, CopyPaste) {
219 BookmarkModel model(NULL);
220 const BookmarkNode* node = model.AddURL(model.other_node(),
222 ASCIIToUTF16("foo bar"),
223 GURL("http://www.google.com"));
225 // Copy a node to the clipboard.
226 std::vector<const BookmarkNode*> nodes;
227 nodes.push_back(node);
228 CopyToClipboard(&model, nodes, false);
230 // And make sure we can paste a bookmark from the clipboard.
231 EXPECT_TRUE(CanPasteFromClipboard(model.bookmark_bar_node()));
233 // Write some text to the clipboard.
235 ui::ScopedClipboardWriter clipboard_writer(
236 ui::Clipboard::GetForCurrentThread(),
237 ui::CLIPBOARD_TYPE_COPY_PASTE);
238 clipboard_writer.WriteText(ASCIIToUTF16("foo"));
241 // Now we shouldn't be able to paste from the clipboard.
242 EXPECT_FALSE(CanPasteFromClipboard(model.bookmark_bar_node()));
245 TEST_F(BookmarkUtilsTest, GetParentForNewNodes) {
246 BookmarkModel model(NULL);
247 // This tests the case where selection contains one item and that item is a
248 // folder.
249 std::vector<const BookmarkNode*> nodes;
250 nodes.push_back(model.bookmark_bar_node());
251 int index = -1;
252 const BookmarkNode* real_parent = GetParentForNewNodes(
253 model.bookmark_bar_node(), nodes, &index);
254 EXPECT_EQ(real_parent, model.bookmark_bar_node());
255 EXPECT_EQ(0, index);
257 nodes.clear();
259 // This tests the case where selection contains one item and that item is an
260 // url.
261 const BookmarkNode* page1 = model.AddURL(model.bookmark_bar_node(), 0,
262 ASCIIToUTF16("Google"),
263 GURL("http://google.com"));
264 nodes.push_back(page1);
265 real_parent = GetParentForNewNodes(model.bookmark_bar_node(), nodes, &index);
266 EXPECT_EQ(real_parent, model.bookmark_bar_node());
267 EXPECT_EQ(1, index);
269 // This tests the case where selection has more than one item.
270 const BookmarkNode* folder1 = model.AddFolder(model.bookmark_bar_node(), 1,
271 ASCIIToUTF16("Folder 1"));
272 nodes.push_back(folder1);
273 real_parent = GetParentForNewNodes(model.bookmark_bar_node(), nodes, &index);
274 EXPECT_EQ(real_parent, model.bookmark_bar_node());
275 EXPECT_EQ(2, index);
277 // This tests the case where selection doesn't contain any items.
278 nodes.clear();
279 real_parent = GetParentForNewNodes(model.bookmark_bar_node(), nodes, &index);
280 EXPECT_EQ(real_parent, model.bookmark_bar_node());
281 EXPECT_EQ(2, index);
284 // Verifies that meta info is copied when nodes are cloned.
285 TEST_F(BookmarkUtilsTest, CloneMetaInfo) {
286 BookmarkModel model(NULL);
287 // Add a node containing meta info.
288 const BookmarkNode* node = model.AddURL(model.other_node(),
290 ASCIIToUTF16("foo bar"),
291 GURL("http://www.google.com"));
292 model.SetNodeMetaInfo(node, "somekey", "somevalue");
293 model.SetNodeMetaInfo(node, "someotherkey", "someothervalue");
295 // Clone node to a different folder.
296 const BookmarkNode* folder = model.AddFolder(model.bookmark_bar_node(), 0,
297 ASCIIToUTF16("Folder"));
298 std::vector<BookmarkNodeData::Element> elements;
299 BookmarkNodeData::Element node_data(node);
300 elements.push_back(node_data);
301 EXPECT_EQ(0, folder->child_count());
302 CloneBookmarkNode(&model, elements, folder, 0, false);
303 ASSERT_EQ(1, folder->child_count());
305 // Verify that the cloned node contains the same meta info.
306 const BookmarkNode* clone = folder->GetChild(0);
307 ASSERT_TRUE(clone->GetMetaInfoMap());
308 EXPECT_EQ(2u, clone->GetMetaInfoMap()->size());
309 std::string value;
310 EXPECT_TRUE(clone->GetMetaInfo("somekey", &value));
311 EXPECT_EQ("somevalue", value);
312 EXPECT_TRUE(clone->GetMetaInfo("someotherkey", &value));
313 EXPECT_EQ("someothervalue", value);
316 } // namespace
317 } // namespace bookmark_utils