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 "base/basictypes.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/bookmarks/core/browser/bookmark_model.h"
13 #include "components/bookmarks/core/browser/bookmark_node_data.h"
14 #include "components/bookmarks/core/test/bookmark_test_helpers.h"
15 #include "content/public/test/test_browser_thread_bundle.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "ui/base/dragdrop/os_exchange_data.h"
18 #include "ui/events/platform/platform_event_source.h"
21 using base::ASCIIToUTF16
;
23 class BookmarkNodeDataTest
: public testing::Test
{
25 BookmarkNodeDataTest() : model_(NULL
) {}
27 virtual void SetUp() OVERRIDE
{
28 event_source_
= ui::PlatformEventSource::CreateDefault();
29 profile_
.reset(new TestingProfile
);
30 profile_
->SetID(L
"id");
31 profile_
->CreateBookmarkModel(false);
32 model_
= BookmarkModelFactory::GetForProfile(profile_
.get());
33 test::WaitForBookmarkModelToLoad(model_
);
36 virtual void TearDown() OVERRIDE
{
38 event_source_
.reset();
41 Profile
* profile() { return profile_
.get(); }
43 BookmarkModel
* model() { return model_
; }
46 content::TestBrowserThreadBundle thread_bundle_
;
47 scoped_ptr
<TestingProfile
> profile_
;
48 BookmarkModel
* model_
;
49 scoped_ptr
<ui::PlatformEventSource
> event_source_
;
51 DISALLOW_COPY_AND_ASSIGN(BookmarkNodeDataTest
);
56 ui::OSExchangeData::Provider
* CloneProvider(const ui::OSExchangeData
& data
) {
57 return data
.provider().Clone();
62 // Makes sure BookmarkNodeData is initially invalid.
63 TEST_F(BookmarkNodeDataTest
, InitialState
) {
64 BookmarkNodeData data
;
65 EXPECT_FALSE(data
.is_valid());
68 // Makes sure reading bogus data leaves the BookmarkNodeData invalid.
69 TEST_F(BookmarkNodeDataTest
, BogusRead
) {
70 ui::OSExchangeData data
;
71 BookmarkNodeData drag_data
;
72 EXPECT_FALSE(drag_data
.Read(ui::OSExchangeData(CloneProvider(data
))));
73 EXPECT_FALSE(drag_data
.is_valid());
76 // Writes a URL to the clipboard and make sure BookmarkNodeData can correctly
78 TEST_F(BookmarkNodeDataTest
, JustURL
) {
79 const GURL
url("http://google.com");
80 const base::string16
title(ASCIIToUTF16("google.com"));
82 ui::OSExchangeData data
;
83 data
.SetURL(url
, title
);
85 BookmarkNodeData drag_data
;
86 EXPECT_TRUE(drag_data
.Read(ui::OSExchangeData(CloneProvider(data
))));
87 EXPECT_TRUE(drag_data
.is_valid());
88 ASSERT_EQ(1u, drag_data
.elements
.size());
89 EXPECT_TRUE(drag_data
.elements
[0].is_url
);
90 EXPECT_EQ(url
, drag_data
.elements
[0].url
);
91 EXPECT_EQ(title
, drag_data
.elements
[0].title
);
92 EXPECT_TRUE(drag_data
.elements
[0].date_added
.is_null());
93 EXPECT_TRUE(drag_data
.elements
[0].date_folder_modified
.is_null());
94 EXPECT_EQ(0u, drag_data
.elements
[0].children
.size());
97 TEST_F(BookmarkNodeDataTest
, URL
) {
98 // Write a single node representing a URL to the clipboard.
99 const BookmarkNode
* root
= model()->bookmark_bar_node();
100 GURL
url(GURL("http://foo.com"));
101 const base::string16
title(ASCIIToUTF16("foo.com"));
102 const BookmarkNode
* node
= model()->AddURL(root
, 0, title
, url
);
103 BookmarkNodeData
drag_data(node
);
104 EXPECT_TRUE(drag_data
.is_valid());
105 ASSERT_EQ(1u, drag_data
.elements
.size());
106 EXPECT_TRUE(drag_data
.elements
[0].is_url
);
107 EXPECT_EQ(url
, drag_data
.elements
[0].url
);
108 EXPECT_EQ(title
, drag_data
.elements
[0].title
);
109 EXPECT_EQ(node
->date_added(), drag_data
.elements
[0].date_added
);
110 EXPECT_EQ(node
->date_folder_modified(),
111 drag_data
.elements
[0].date_folder_modified
);
112 ui::OSExchangeData data
;
113 drag_data
.Write(profile()->GetPath(), &data
);
115 // Now read the data back in.
116 ui::OSExchangeData
data2(CloneProvider(data
));
117 BookmarkNodeData read_data
;
118 EXPECT_TRUE(read_data
.Read(data2
));
119 EXPECT_TRUE(read_data
.is_valid());
120 ASSERT_EQ(1u, read_data
.elements
.size());
121 EXPECT_TRUE(read_data
.elements
[0].is_url
);
122 EXPECT_EQ(url
, read_data
.elements
[0].url
);
123 EXPECT_EQ(title
, read_data
.elements
[0].title
);
124 EXPECT_TRUE(read_data
.elements
[0].date_added
.is_null());
125 EXPECT_TRUE(read_data
.elements
[0].date_folder_modified
.is_null());
126 EXPECT_TRUE(read_data
.GetFirstNode(model(), profile()->GetPath()) == node
);
128 // Make sure asking for the node with a different profile returns NULL.
129 TestingProfile profile2
;
130 EXPECT_TRUE(read_data
.GetFirstNode(model(), profile2
.GetPath()) == NULL
);
132 // Writing should also put the URL and title on the clipboard.
134 base::string16 read_title
;
135 EXPECT_TRUE(data2
.GetURLAndTitle(
136 ui::OSExchangeData::CONVERT_FILENAMES
, &read_url
, &read_title
));
137 EXPECT_EQ(url
, read_url
);
138 EXPECT_EQ(title
, read_title
);
141 // Tests writing a folder to the clipboard.
142 TEST_F(BookmarkNodeDataTest
, Folder
) {
143 const BookmarkNode
* root
= model()->bookmark_bar_node();
144 const BookmarkNode
* g1
= model()->AddFolder(root
, 0, ASCIIToUTF16("g1"));
145 model()->AddFolder(g1
, 0, ASCIIToUTF16("g11"));
146 const BookmarkNode
* g12
= model()->AddFolder(g1
, 0, ASCIIToUTF16("g12"));
148 BookmarkNodeData
drag_data(g12
);
149 EXPECT_TRUE(drag_data
.is_valid());
150 ASSERT_EQ(1u, drag_data
.elements
.size());
151 EXPECT_EQ(g12
->GetTitle(), drag_data
.elements
[0].title
);
152 EXPECT_FALSE(drag_data
.elements
[0].is_url
);
153 EXPECT_EQ(g12
->date_added(), drag_data
.elements
[0].date_added
);
154 EXPECT_EQ(g12
->date_folder_modified(),
155 drag_data
.elements
[0].date_folder_modified
);
157 ui::OSExchangeData data
;
158 drag_data
.Write(profile()->GetPath(), &data
);
160 // Now read the data back in.
161 ui::OSExchangeData
data2(CloneProvider(data
));
162 BookmarkNodeData read_data
;
163 EXPECT_TRUE(read_data
.Read(data2
));
164 EXPECT_TRUE(read_data
.is_valid());
165 ASSERT_EQ(1u, read_data
.elements
.size());
166 EXPECT_EQ(g12
->GetTitle(), read_data
.elements
[0].title
);
167 EXPECT_FALSE(read_data
.elements
[0].is_url
);
168 EXPECT_TRUE(read_data
.elements
[0].date_added
.is_null());
169 EXPECT_TRUE(read_data
.elements
[0].date_folder_modified
.is_null());
171 // We should get back the same node when asking for the same profile.
172 const BookmarkNode
* r_g12
=
173 read_data
.GetFirstNode(model(), profile()->GetPath());
174 EXPECT_TRUE(g12
== r_g12
);
176 // A different profile should return NULL for the node.
177 TestingProfile profile2
;
178 EXPECT_TRUE(read_data
.GetFirstNode(model(), profile2
.GetPath()) == NULL
);
181 // Tests reading/writing a folder with children.
182 TEST_F(BookmarkNodeDataTest
, FolderWithChild
) {
183 const BookmarkNode
* root
= model()->bookmark_bar_node();
184 const BookmarkNode
* folder
= model()->AddFolder(root
, 0, ASCIIToUTF16("g1"));
186 GURL
url(GURL("http://foo.com"));
187 const base::string16
title(ASCIIToUTF16("blah2"));
189 model()->AddURL(folder
, 0, title
, url
);
191 BookmarkNodeData
drag_data(folder
);
193 ui::OSExchangeData data
;
194 drag_data
.Write(profile()->GetPath(), &data
);
196 // Now read the data back in.
197 ui::OSExchangeData
data2(CloneProvider(data
));
198 BookmarkNodeData read_data
;
199 EXPECT_TRUE(read_data
.Read(data2
));
200 ASSERT_EQ(1u, read_data
.elements
.size());
201 ASSERT_EQ(1u, read_data
.elements
[0].children
.size());
202 const BookmarkNodeData::Element
& read_child
=
203 read_data
.elements
[0].children
[0];
205 EXPECT_TRUE(read_child
.is_url
);
206 EXPECT_EQ(title
, read_child
.title
);
207 EXPECT_EQ(url
, read_child
.url
);
208 EXPECT_TRUE(read_data
.elements
[0].date_added
.is_null());
209 EXPECT_TRUE(read_data
.elements
[0].date_folder_modified
.is_null());
210 EXPECT_TRUE(read_child
.is_url
);
212 // And make sure we get the node back.
213 const BookmarkNode
* r_folder
=
214 read_data
.GetFirstNode(model(), profile()->GetPath());
215 EXPECT_TRUE(folder
== r_folder
);
218 // Tests reading/writing of multiple nodes.
219 TEST_F(BookmarkNodeDataTest
, MultipleNodes
) {
220 const BookmarkNode
* root
= model()->bookmark_bar_node();
221 const BookmarkNode
* folder
= model()->AddFolder(root
, 0, ASCIIToUTF16("g1"));
223 GURL
url(GURL("http://foo.com"));
224 const base::string16
title(ASCIIToUTF16("blah2"));
226 const BookmarkNode
* url_node
= model()->AddURL(folder
, 0, title
, url
);
228 // Write the nodes to the clipboard.
229 std::vector
<const BookmarkNode
*> nodes
;
230 nodes
.push_back(folder
);
231 nodes
.push_back(url_node
);
232 BookmarkNodeData
drag_data(nodes
);
233 ui::OSExchangeData data
;
234 drag_data
.Write(profile()->GetPath(), &data
);
236 // Read the data back in.
237 ui::OSExchangeData
data2(CloneProvider(data
));
238 BookmarkNodeData read_data
;
239 EXPECT_TRUE(read_data
.Read(data2
));
240 EXPECT_TRUE(read_data
.is_valid());
241 ASSERT_EQ(2u, read_data
.elements
.size());
242 ASSERT_EQ(1u, read_data
.elements
[0].children
.size());
243 EXPECT_TRUE(read_data
.elements
[0].date_added
.is_null());
244 EXPECT_TRUE(read_data
.elements
[0].date_folder_modified
.is_null());
246 const BookmarkNodeData::Element
& read_folder
= read_data
.elements
[0];
247 EXPECT_FALSE(read_folder
.is_url
);
248 EXPECT_EQ(ASCIIToUTF16("g1"), read_folder
.title
);
249 EXPECT_EQ(1u, read_folder
.children
.size());
251 const BookmarkNodeData::Element
& read_url
= read_data
.elements
[1];
252 EXPECT_TRUE(read_url
.is_url
);
253 EXPECT_EQ(title
, read_url
.title
);
254 EXPECT_EQ(0u, read_url
.children
.size());
256 // And make sure we get the node back.
257 std::vector
<const BookmarkNode
*> read_nodes
=
258 read_data
.GetNodes(model(), profile()->GetPath());
259 ASSERT_EQ(2u, read_nodes
.size());
260 EXPECT_TRUE(read_nodes
[0] == folder
);
261 EXPECT_TRUE(read_nodes
[1] == url_node
);
263 // Asking for the first node should return NULL with more than one element
265 EXPECT_TRUE(read_data
.GetFirstNode(model(), profile()->GetPath()) == NULL
);
268 // Tests reading/writing of meta info.
269 TEST_F(BookmarkNodeDataTest
, MetaInfo
) {
270 // Create a node containing meta info.
271 const BookmarkNode
* node
= model()->AddURL(model()->other_node(),
273 ASCIIToUTF16("foo bar"),
274 GURL("http://www.google.com"));
275 model()->SetNodeMetaInfo(node
, "somekey", "somevalue");
276 model()->SetNodeMetaInfo(node
, "someotherkey", "someothervalue");
278 BookmarkNodeData
node_data(node
);
279 ui::OSExchangeData data
;
280 node_data
.Write(profile()->GetPath(), &data
);
282 // Read the data back in.
283 ui::OSExchangeData
data2(CloneProvider(data
));
284 BookmarkNodeData read_data
;
285 EXPECT_TRUE(read_data
.Read(data2
));
286 EXPECT_TRUE(read_data
.is_valid());
287 ASSERT_EQ(1u, read_data
.elements
.size());
289 // Verify that the read data contains the same meta info.
290 BookmarkNode::MetaInfoMap meta_info_map
= read_data
.elements
[0].meta_info_map
;
291 EXPECT_EQ(2u, meta_info_map
.size());
292 EXPECT_EQ("somevalue", meta_info_map
["somekey"]);
293 EXPECT_EQ("someothervalue", meta_info_map
["someotherkey"]);