• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 "base/memory/scoped_ptr.h"
6 #include "base/message_loop.h"
7 #include "base/string16.h"
8 #include "base/utf_string_conversions.h"
9 #include "chrome/browser/bookmarks/bookmark_node_data.h"
10 #include "chrome/browser/bookmarks/bookmark_model.h"
11 #include "chrome/test/testing_profile.h"
12 #include "content/browser/browser_thread.h"
13 #include "googleurl/src/gurl.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/base/dragdrop/os_exchange_data.h"
16 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
17 
18 class BookmarkNodeDataTest : public testing::Test {
19  public:
BookmarkNodeDataTest()20   BookmarkNodeDataTest()
21       : ui_thread_(BrowserThread::UI, &loop_),
22         file_thread_(BrowserThread::FILE, &loop_) { }
23 
24  private:
25   MessageLoop loop_;
26   BrowserThread ui_thread_;
27   BrowserThread file_thread_;
28 };
29 
30 namespace {
31 
CloneProvider(const ui::OSExchangeData & data)32 ui::OSExchangeData::Provider* CloneProvider(const ui::OSExchangeData& data) {
33   return new ui::OSExchangeDataProviderWin(
34       ui::OSExchangeDataProviderWin::GetIDataObject(data));
35 }
36 
37 }  // namespace
38 
39 // Makes sure BookmarkNodeData is initially invalid.
TEST_F(BookmarkNodeDataTest,InitialState)40 TEST_F(BookmarkNodeDataTest, InitialState) {
41   BookmarkNodeData data;
42   EXPECT_FALSE(data.is_valid());
43 }
44 
45 // Makes sure reading bogus data leaves the BookmarkNodeData invalid.
TEST_F(BookmarkNodeDataTest,BogusRead)46 TEST_F(BookmarkNodeDataTest, BogusRead) {
47   ui::OSExchangeData data;
48   BookmarkNodeData drag_data;
49   EXPECT_FALSE(drag_data.Read(ui::OSExchangeData(CloneProvider(data))));
50   EXPECT_FALSE(drag_data.is_valid());
51 }
52 
53 // Writes a URL to the clipboard and make sure BookmarkNodeData can correctly
54 // read it.
TEST_F(BookmarkNodeDataTest,JustURL)55 TEST_F(BookmarkNodeDataTest, JustURL) {
56   const GURL url("http://google.com");
57   const std::wstring title(L"title");
58 
59   ui::OSExchangeData data;
60   data.SetURL(url, title);
61 
62   BookmarkNodeData drag_data;
63   EXPECT_TRUE(drag_data.Read(ui::OSExchangeData(CloneProvider(data))));
64   EXPECT_TRUE(drag_data.is_valid());
65   ASSERT_EQ(1, drag_data.elements.size());
66   EXPECT_TRUE(drag_data.elements[0].is_url);
67   EXPECT_EQ(url, drag_data.elements[0].url);
68   EXPECT_EQ(title, drag_data.elements[0].title);
69   EXPECT_EQ(0, drag_data.elements[0].children.size());
70 }
71 
TEST_F(BookmarkNodeDataTest,URL)72 TEST_F(BookmarkNodeDataTest, URL) {
73   // Write a single node representing a URL to the clipboard.
74   TestingProfile profile;
75   profile.CreateBookmarkModel(false);
76   profile.BlockUntilBookmarkModelLoaded();
77   profile.SetID(L"id");
78   BookmarkModel* model = profile.GetBookmarkModel();
79   const BookmarkNode* root = model->GetBookmarkBarNode();
80   GURL url(GURL("http://foo.com"));
81   const string16 title(ASCIIToUTF16("blah"));
82   const BookmarkNode* node = model->AddURL(root, 0, title, url);
83   BookmarkNodeData drag_data(node);
84   EXPECT_TRUE(drag_data.is_valid());
85   ASSERT_EQ(1, drag_data.elements.size());
86   EXPECT_TRUE(drag_data.elements[0].is_url);
87   EXPECT_EQ(url, drag_data.elements[0].url);
88   EXPECT_EQ(title, WideToUTF16Hack(drag_data.elements[0].title));
89   ui::OSExchangeData data;
90   drag_data.Write(&profile, &data);
91 
92   // Now read the data back in.
93   ui::OSExchangeData data2(CloneProvider(data));
94   BookmarkNodeData read_data;
95   EXPECT_TRUE(read_data.Read(data2));
96   EXPECT_TRUE(read_data.is_valid());
97   ASSERT_EQ(1, read_data.elements.size());
98   EXPECT_TRUE(read_data.elements[0].is_url);
99   EXPECT_EQ(url, read_data.elements[0].url);
100   EXPECT_EQ(title, read_data.elements[0].title);
101   EXPECT_TRUE(read_data.GetFirstNode(&profile) == node);
102 
103   // Make sure asking for the node with a different profile returns NULL.
104   TestingProfile profile2;
105   EXPECT_TRUE(read_data.GetFirstNode(&profile2) == NULL);
106 
107   // Writing should also put the URL and title on the clipboard.
108   GURL read_url;
109   std::wstring read_title;
110   EXPECT_TRUE(data2.GetURLAndTitle(&read_url, &read_title));
111   EXPECT_EQ(url, read_url);
112   EXPECT_EQ(title, read_title);
113 }
114 
115 // Tests writing a folder to the clipboard.
TEST_F(BookmarkNodeDataTest,Folder)116 TEST_F(BookmarkNodeDataTest, Folder) {
117   TestingProfile profile;
118   profile.CreateBookmarkModel(false);
119   profile.BlockUntilBookmarkModelLoaded();
120   profile.SetID(L"id");
121   BookmarkModel* model = profile.GetBookmarkModel();
122   const BookmarkNode* root = model->GetBookmarkBarNode();
123   const BookmarkNode* g1 = model->AddFolder(root, 0, ASCIIToUTF16("g1"));
124   const BookmarkNode* g11 = model->AddFolder(g1, 0, ASCIIToUTF16("g11"));
125   const BookmarkNode* g12 = model->AddFolder(g1, 0, ASCIIToUTF16("g12"));
126 
127   BookmarkNodeData drag_data(g12);
128   EXPECT_TRUE(drag_data.is_valid());
129   ASSERT_EQ(1, drag_data.elements.size());
130   EXPECT_EQ(g12->GetTitle(), WideToUTF16Hack(drag_data.elements[0].title));
131   EXPECT_FALSE(drag_data.elements[0].is_url);
132 
133   ui::OSExchangeData data;
134   drag_data.Write(&profile, &data);
135 
136   // Now read the data back in.
137   ui::OSExchangeData data2(CloneProvider(data));
138   BookmarkNodeData read_data;
139   EXPECT_TRUE(read_data.Read(data2));
140   EXPECT_TRUE(read_data.is_valid());
141   ASSERT_EQ(1, read_data.elements.size());
142   EXPECT_EQ(g12->GetTitle(), WideToUTF16Hack(read_data.elements[0].title));
143   EXPECT_FALSE(read_data.elements[0].is_url);
144 
145   // We should get back the same node when asking for the same profile.
146   const BookmarkNode* r_g12 = read_data.GetFirstNode(&profile);
147   EXPECT_TRUE(g12 == r_g12);
148 
149   // A different profile should return NULL for the node.
150   TestingProfile profile2;
151   EXPECT_TRUE(read_data.GetFirstNode(&profile2) == NULL);
152 }
153 
154 // Tests reading/writing a folder with children.
TEST_F(BookmarkNodeDataTest,FolderWithChild)155 TEST_F(BookmarkNodeDataTest, FolderWithChild) {
156   TestingProfile profile;
157   profile.SetID(L"id");
158   profile.CreateBookmarkModel(false);
159   profile.BlockUntilBookmarkModelLoaded();
160   BookmarkModel* model = profile.GetBookmarkModel();
161   const BookmarkNode* root = model->GetBookmarkBarNode();
162   const BookmarkNode* folder = model->AddFolder(root, 0, ASCIIToUTF16("g1"));
163 
164   GURL url(GURL("http://foo.com"));
165   const string16 title(ASCIIToUTF16("blah2"));
166 
167   model->AddURL(folder, 0, title, url);
168 
169   BookmarkNodeData drag_data(folder);
170 
171   ui::OSExchangeData data;
172   drag_data.Write(&profile, &data);
173 
174   // Now read the data back in.
175   ui::OSExchangeData data2(CloneProvider(data));
176   BookmarkNodeData read_data;
177   EXPECT_TRUE(read_data.Read(data2));
178   ASSERT_EQ(1, read_data.elements.size());
179   ASSERT_EQ(1, read_data.elements[0].children.size());
180   const BookmarkNodeData::Element& read_child =
181       read_data.elements[0].children[0];
182 
183   EXPECT_TRUE(read_child.is_url);
184   EXPECT_EQ(title, WideToUTF16Hack(read_child.title));
185   EXPECT_EQ(url, read_child.url);
186   EXPECT_TRUE(read_child.is_url);
187 
188   // And make sure we get the node back.
189   const BookmarkNode* r_folder = read_data.GetFirstNode(&profile);
190   EXPECT_TRUE(folder == r_folder);
191 }
192 
193 // Tests reading/writing of multiple nodes.
TEST_F(BookmarkNodeDataTest,MultipleNodes)194 TEST_F(BookmarkNodeDataTest, MultipleNodes) {
195   TestingProfile profile;
196   profile.SetID(L"id");
197   profile.CreateBookmarkModel(false);
198   profile.BlockUntilBookmarkModelLoaded();
199   BookmarkModel* model = profile.GetBookmarkModel();
200   const BookmarkNode* root = model->GetBookmarkBarNode();
201   const BookmarkNode* folder = model->AddFolder(root, 0, ASCIIToUTF16("g1"));
202 
203   GURL url(GURL("http://foo.com"));
204   const string16 title(ASCIIToUTF16("blah2"));
205 
206   const BookmarkNode* url_node = model->AddURL(folder, 0, title, url);
207 
208   // Write the nodes to the clipboard.
209   std::vector<const BookmarkNode*> nodes;
210   nodes.push_back(folder);
211   nodes.push_back(url_node);
212   BookmarkNodeData drag_data(nodes);
213   ui::OSExchangeData data;
214   drag_data.Write(&profile, &data);
215 
216   // Read the data back in.
217   ui::OSExchangeData data2(CloneProvider(data));
218   BookmarkNodeData read_data;
219   EXPECT_TRUE(read_data.Read(data2));
220   EXPECT_TRUE(read_data.is_valid());
221   ASSERT_EQ(2, read_data.elements.size());
222   ASSERT_EQ(1, read_data.elements[0].children.size());
223 
224   const BookmarkNodeData::Element& read_folder = read_data.elements[0];
225   EXPECT_FALSE(read_folder.is_url);
226   EXPECT_EQ(L"g1", read_folder.title);
227   EXPECT_EQ(1, read_folder.children.size());
228 
229   const BookmarkNodeData::Element& read_url = read_data.elements[1];
230   EXPECT_TRUE(read_url.is_url);
231   EXPECT_EQ(title, WideToUTF16Hack(read_url.title));
232   EXPECT_EQ(0, read_url.children.size());
233 
234   // And make sure we get the node back.
235   std::vector<const BookmarkNode*> read_nodes = read_data.GetNodes(&profile);
236   ASSERT_EQ(2, read_nodes.size());
237   EXPECT_TRUE(read_nodes[0] == folder);
238   EXPECT_TRUE(read_nodes[1] == url_node);
239 
240   // Asking for the first node should return NULL with more than one element
241   // present.
242   EXPECT_TRUE(read_data.GetFirstNode(&profile) == NULL);
243 }
244