Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / bookmarks / bookmark_node_data_unittest.cc
blobcce44dc6a045741333286ecd60490ee209274eee
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.h"
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
12 #include "chrome/browser/bookmarks/bookmark_node_data.h"
13 #include "chrome/browser/bookmarks/bookmark_test_helpers.h"
14 #include "chrome/test/base/testing_profile.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 "url/gurl.h"
20 using base::ASCIIToUTF16;
22 class BookmarkNodeDataTest : public testing::Test {
23 public:
24 BookmarkNodeDataTest() : model_(NULL) {}
26 virtual void SetUp() OVERRIDE {
27 profile_.reset(new TestingProfile);
28 profile_->SetID(L"id");
29 profile_->CreateBookmarkModel(false);
30 model_ = BookmarkModelFactory::GetForProfile(profile_.get());
31 test::WaitForBookmarkModelToLoad(model_);
34 virtual void TearDown() OVERRIDE {
35 profile_.reset();
38 Profile* profile() { return profile_.get(); }
40 BookmarkModel* model() { return model_; }
42 private:
43 content::TestBrowserThreadBundle thread_bundle_;
44 scoped_ptr<TestingProfile> profile_;
45 BookmarkModel* model_;
47 DISALLOW_COPY_AND_ASSIGN(BookmarkNodeDataTest);
50 namespace {
52 ui::OSExchangeData::Provider* CloneProvider(const ui::OSExchangeData& data) {
53 return data.provider().Clone();
56 } // namespace
58 // Makes sure BookmarkNodeData is initially invalid.
59 TEST_F(BookmarkNodeDataTest, InitialState) {
60 BookmarkNodeData data;
61 EXPECT_FALSE(data.is_valid());
64 // Makes sure reading bogus data leaves the BookmarkNodeData invalid.
65 TEST_F(BookmarkNodeDataTest, BogusRead) {
66 ui::OSExchangeData data;
67 BookmarkNodeData drag_data;
68 EXPECT_FALSE(drag_data.Read(ui::OSExchangeData(CloneProvider(data))));
69 EXPECT_FALSE(drag_data.is_valid());
72 // Writes a URL to the clipboard and make sure BookmarkNodeData can correctly
73 // read it.
74 TEST_F(BookmarkNodeDataTest, JustURL) {
75 const GURL url("http://google.com");
76 const base::string16 title(ASCIIToUTF16("google.com"));
78 ui::OSExchangeData data;
79 data.SetURL(url, title);
81 BookmarkNodeData drag_data;
82 EXPECT_TRUE(drag_data.Read(ui::OSExchangeData(CloneProvider(data))));
83 EXPECT_TRUE(drag_data.is_valid());
84 ASSERT_EQ(1u, drag_data.elements.size());
85 EXPECT_TRUE(drag_data.elements[0].is_url);
86 EXPECT_EQ(url, drag_data.elements[0].url);
87 EXPECT_EQ(title, drag_data.elements[0].title);
88 EXPECT_TRUE(drag_data.elements[0].date_added.is_null());
89 EXPECT_TRUE(drag_data.elements[0].date_folder_modified.is_null());
90 EXPECT_EQ(0u, drag_data.elements[0].children.size());
93 TEST_F(BookmarkNodeDataTest, URL) {
94 // Write a single node representing a URL to the clipboard.
95 const BookmarkNode* root = model()->bookmark_bar_node();
96 GURL url(GURL("http://foo.com"));
97 const base::string16 title(ASCIIToUTF16("foo.com"));
98 const BookmarkNode* node = model()->AddURL(root, 0, title, url);
99 BookmarkNodeData drag_data(node);
100 EXPECT_TRUE(drag_data.is_valid());
101 ASSERT_EQ(1u, drag_data.elements.size());
102 EXPECT_TRUE(drag_data.elements[0].is_url);
103 EXPECT_EQ(url, drag_data.elements[0].url);
104 EXPECT_EQ(title, drag_data.elements[0].title);
105 EXPECT_EQ(node->date_added(), drag_data.elements[0].date_added);
106 EXPECT_EQ(node->date_folder_modified(),
107 drag_data.elements[0].date_folder_modified);
108 ui::OSExchangeData data;
109 drag_data.Write(profile(), &data);
111 // Now read the data back in.
112 ui::OSExchangeData data2(CloneProvider(data));
113 BookmarkNodeData read_data;
114 EXPECT_TRUE(read_data.Read(data2));
115 EXPECT_TRUE(read_data.is_valid());
116 ASSERT_EQ(1u, read_data.elements.size());
117 EXPECT_TRUE(read_data.elements[0].is_url);
118 EXPECT_EQ(url, read_data.elements[0].url);
119 EXPECT_EQ(title, read_data.elements[0].title);
120 EXPECT_TRUE(read_data.elements[0].date_added.is_null());
121 EXPECT_TRUE(read_data.elements[0].date_folder_modified.is_null());
122 EXPECT_TRUE(read_data.GetFirstNode(profile()) == node);
124 // Make sure asking for the node with a different profile returns NULL.
125 TestingProfile profile2;
126 EXPECT_TRUE(read_data.GetFirstNode(&profile2) == NULL);
128 // Writing should also put the URL and title on the clipboard.
129 GURL read_url;
130 base::string16 read_title;
131 EXPECT_TRUE(data2.GetURLAndTitle(
132 ui::OSExchangeData::CONVERT_FILENAMES, &read_url, &read_title));
133 EXPECT_EQ(url, read_url);
134 EXPECT_EQ(title, read_title);
137 // Tests writing a folder to the clipboard.
138 TEST_F(BookmarkNodeDataTest, Folder) {
139 const BookmarkNode* root = model()->bookmark_bar_node();
140 const BookmarkNode* g1 = model()->AddFolder(root, 0, ASCIIToUTF16("g1"));
141 model()->AddFolder(g1, 0, ASCIIToUTF16("g11"));
142 const BookmarkNode* g12 = model()->AddFolder(g1, 0, ASCIIToUTF16("g12"));
144 BookmarkNodeData drag_data(g12);
145 EXPECT_TRUE(drag_data.is_valid());
146 ASSERT_EQ(1u, drag_data.elements.size());
147 EXPECT_EQ(g12->GetTitle(), drag_data.elements[0].title);
148 EXPECT_FALSE(drag_data.elements[0].is_url);
149 EXPECT_EQ(g12->date_added(), drag_data.elements[0].date_added);
150 EXPECT_EQ(g12->date_folder_modified(),
151 drag_data.elements[0].date_folder_modified);
153 ui::OSExchangeData data;
154 drag_data.Write(profile(), &data);
156 // Now read the data back in.
157 ui::OSExchangeData data2(CloneProvider(data));
158 BookmarkNodeData read_data;
159 EXPECT_TRUE(read_data.Read(data2));
160 EXPECT_TRUE(read_data.is_valid());
161 ASSERT_EQ(1u, read_data.elements.size());
162 EXPECT_EQ(g12->GetTitle(), read_data.elements[0].title);
163 EXPECT_FALSE(read_data.elements[0].is_url);
164 EXPECT_TRUE(read_data.elements[0].date_added.is_null());
165 EXPECT_TRUE(read_data.elements[0].date_folder_modified.is_null());
167 // We should get back the same node when asking for the same profile.
168 const BookmarkNode* r_g12 = read_data.GetFirstNode(profile());
169 EXPECT_TRUE(g12 == r_g12);
171 // A different profile should return NULL for the node.
172 TestingProfile profile2;
173 EXPECT_TRUE(read_data.GetFirstNode(&profile2) == NULL);
176 // Tests reading/writing a folder with children.
177 TEST_F(BookmarkNodeDataTest, FolderWithChild) {
178 const BookmarkNode* root = model()->bookmark_bar_node();
179 const BookmarkNode* folder = model()->AddFolder(root, 0, ASCIIToUTF16("g1"));
181 GURL url(GURL("http://foo.com"));
182 const base::string16 title(ASCIIToUTF16("blah2"));
184 model()->AddURL(folder, 0, title, url);
186 BookmarkNodeData drag_data(folder);
188 ui::OSExchangeData data;
189 drag_data.Write(profile(), &data);
191 // Now read the data back in.
192 ui::OSExchangeData data2(CloneProvider(data));
193 BookmarkNodeData read_data;
194 EXPECT_TRUE(read_data.Read(data2));
195 ASSERT_EQ(1u, read_data.elements.size());
196 ASSERT_EQ(1u, read_data.elements[0].children.size());
197 const BookmarkNodeData::Element& read_child =
198 read_data.elements[0].children[0];
200 EXPECT_TRUE(read_child.is_url);
201 EXPECT_EQ(title, read_child.title);
202 EXPECT_EQ(url, read_child.url);
203 EXPECT_TRUE(read_data.elements[0].date_added.is_null());
204 EXPECT_TRUE(read_data.elements[0].date_folder_modified.is_null());
205 EXPECT_TRUE(read_child.is_url);
207 // And make sure we get the node back.
208 const BookmarkNode* r_folder = read_data.GetFirstNode(profile());
209 EXPECT_TRUE(folder == r_folder);
212 // Tests reading/writing of multiple nodes.
213 TEST_F(BookmarkNodeDataTest, MultipleNodes) {
214 const BookmarkNode* root = model()->bookmark_bar_node();
215 const BookmarkNode* folder = model()->AddFolder(root, 0, ASCIIToUTF16("g1"));
217 GURL url(GURL("http://foo.com"));
218 const base::string16 title(ASCIIToUTF16("blah2"));
220 const BookmarkNode* url_node = model()->AddURL(folder, 0, title, url);
222 // Write the nodes to the clipboard.
223 std::vector<const BookmarkNode*> nodes;
224 nodes.push_back(folder);
225 nodes.push_back(url_node);
226 BookmarkNodeData drag_data(nodes);
227 ui::OSExchangeData data;
228 drag_data.Write(profile(), &data);
230 // Read the data back in.
231 ui::OSExchangeData data2(CloneProvider(data));
232 BookmarkNodeData read_data;
233 EXPECT_TRUE(read_data.Read(data2));
234 EXPECT_TRUE(read_data.is_valid());
235 ASSERT_EQ(2u, read_data.elements.size());
236 ASSERT_EQ(1u, read_data.elements[0].children.size());
237 EXPECT_TRUE(read_data.elements[0].date_added.is_null());
238 EXPECT_TRUE(read_data.elements[0].date_folder_modified.is_null());
240 const BookmarkNodeData::Element& read_folder = read_data.elements[0];
241 EXPECT_FALSE(read_folder.is_url);
242 EXPECT_EQ(ASCIIToUTF16("g1"), read_folder.title);
243 EXPECT_EQ(1u, read_folder.children.size());
245 const BookmarkNodeData::Element& read_url = read_data.elements[1];
246 EXPECT_TRUE(read_url.is_url);
247 EXPECT_EQ(title, read_url.title);
248 EXPECT_EQ(0u, read_url.children.size());
250 // And make sure we get the node back.
251 std::vector<const BookmarkNode*> read_nodes = read_data.GetNodes(profile());
252 ASSERT_EQ(2u, read_nodes.size());
253 EXPECT_TRUE(read_nodes[0] == folder);
254 EXPECT_TRUE(read_nodes[1] == url_node);
256 // Asking for the first node should return NULL with more than one element
257 // present.
258 EXPECT_TRUE(read_data.GetFirstNode(profile()) == NULL);
261 // Tests reading/writing of meta info.
262 TEST_F(BookmarkNodeDataTest, MetaInfo) {
263 // Create a node containing meta info.
264 const BookmarkNode* node = model()->AddURL(model()->other_node(),
266 ASCIIToUTF16("foo bar"),
267 GURL("http://www.google.com"));
268 model()->SetNodeMetaInfo(node, "somekey", "somevalue");
269 model()->SetNodeMetaInfo(node, "someotherkey", "someothervalue");
271 BookmarkNodeData node_data(node);
272 ui::OSExchangeData data;
273 node_data.Write(profile(), &data);
275 // Read the data back in.
276 ui::OSExchangeData data2(CloneProvider(data));
277 BookmarkNodeData read_data;
278 EXPECT_TRUE(read_data.Read(data2));
279 EXPECT_TRUE(read_data.is_valid());
280 ASSERT_EQ(1u, read_data.elements.size());
282 // Verify that the read data contains the same meta info.
283 BookmarkNode::MetaInfoMap meta_info_map = read_data.elements[0].meta_info_map;
284 EXPECT_EQ(2u, meta_info_map.size());
285 EXPECT_EQ("somevalue", meta_info_map["somekey"]);
286 EXPECT_EQ("someothervalue", meta_info_map["someotherkey"]);