• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "base/memory/ref_counted.h"
6 #include "base/memory/scoped_handle.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/win/scoped_hglobal.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "ui/base/clipboard/clipboard.h"
12 #include "ui/base/dragdrop/os_exchange_data.h"
13 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
14 #include "url/gurl.h"
15 
16 namespace ui {
17 
18 // Test getting using the IDataObject COM API
TEST(OSExchangeDataWinTest,StringDataAccessViaCOM)19 TEST(OSExchangeDataWinTest, StringDataAccessViaCOM) {
20   OSExchangeData data;
21   std::wstring input = L"O hai googlz.";
22   data.SetString(input);
23   base::win::ScopedComPtr<IDataObject> com_data(
24       OSExchangeDataProviderWin::GetIDataObject(data));
25 
26   FORMATETC format_etc =
27       { CF_UNICODETEXT, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
28   EXPECT_EQ(S_OK, com_data->QueryGetData(&format_etc));
29 
30   STGMEDIUM medium;
31   EXPECT_EQ(S_OK, com_data->GetData(&format_etc, &medium));
32   std::wstring output =
33       base::win::ScopedHGlobal<wchar_t>(medium.hGlobal).get();
34   EXPECT_EQ(input, output);
35   ReleaseStgMedium(&medium);
36 }
37 
38 // Test setting using the IDataObject COM API
TEST(OSExchangeDataWinTest,StringDataWritingViaCOM)39 TEST(OSExchangeDataWinTest, StringDataWritingViaCOM) {
40   OSExchangeData data;
41   std::wstring input = L"http://www.google.com/";
42 
43   base::win::ScopedComPtr<IDataObject> com_data(
44       OSExchangeDataProviderWin::GetIDataObject(data));
45 
46   // Store data in the object using the COM SetData API.
47   CLIPFORMAT cfstr_ineturl = RegisterClipboardFormat(CFSTR_INETURL);
48   FORMATETC format_etc =
49       { cfstr_ineturl, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
50   STGMEDIUM medium;
51   medium.tymed = TYMED_HGLOBAL;
52   HGLOBAL glob = GlobalAlloc(GPTR, sizeof(wchar_t) * (input.size() + 1));
53   size_t stringsz = input.size();
54   SIZE_T sz = GlobalSize(glob);
55   base::win::ScopedHGlobal<wchar_t> global_lock(glob);
56   wchar_t* buffer_handle = global_lock.get();
57   wcscpy_s(buffer_handle, input.size() + 1, input.c_str());
58   medium.hGlobal = glob;
59   medium.pUnkForRelease = NULL;
60   EXPECT_EQ(S_OK, com_data->SetData(&format_etc, &medium, TRUE));
61 
62   // Construct a new object with the old object so that we can use our access
63   // APIs.
64   OSExchangeData data2(data.provider().Clone());
65   EXPECT_TRUE(data2.HasURL());
66   GURL url_from_data;
67   std::wstring title;
68   EXPECT_TRUE(data2.GetURLAndTitle(&url_from_data, &title));
69   GURL reference_url(input);
70   EXPECT_EQ(reference_url.spec(), url_from_data.spec());
71 }
72 
73 // Verifies SetData invoked twice with the same data clobbers existing data.
TEST(OSExchangeDataWinTest,RemoveData)74 TEST(OSExchangeDataWinTest, RemoveData) {
75   OSExchangeData data;
76   std::wstring input = L"http://www.google.com/";
77   std::wstring input2 = L"http://www.google2.com/";
78 
79   base::win::ScopedComPtr<IDataObject> com_data(
80       OSExchangeDataProviderWin::GetIDataObject(data));
81 
82   // Store data in the object using the COM SetData API.
83   CLIPFORMAT cfstr_ineturl = RegisterClipboardFormat(CFSTR_INETURL);
84   FORMATETC format_etc =
85       { cfstr_ineturl, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
86   STGMEDIUM medium;
87   medium.tymed = TYMED_HGLOBAL;
88   {
89     HGLOBAL glob = GlobalAlloc(GPTR, sizeof(wchar_t) * (input.size() + 1));
90     size_t stringsz = input.size();
91     SIZE_T sz = GlobalSize(glob);
92     base::win::ScopedHGlobal<wchar_t> global_lock(glob);
93     wchar_t* buffer_handle = global_lock.get();
94     wcscpy_s(buffer_handle, input.size() + 1, input.c_str());
95     medium.hGlobal = glob;
96     medium.pUnkForRelease = NULL;
97     EXPECT_EQ(S_OK, com_data->SetData(&format_etc, &medium, TRUE));
98   }
99   // This should clobber the existing data.
100   {
101     HGLOBAL glob = GlobalAlloc(GPTR, sizeof(wchar_t) * (input2.size() + 1));
102     size_t stringsz = input2.size();
103     SIZE_T sz = GlobalSize(glob);
104     base::win::ScopedHGlobal<wchar_t> global_lock(glob);
105     wchar_t* buffer_handle = global_lock.get();
106     wcscpy_s(buffer_handle, input2.size() + 1, input2.c_str());
107     medium.hGlobal = glob;
108     medium.pUnkForRelease = NULL;
109     EXPECT_EQ(S_OK, com_data->SetData(&format_etc, &medium, TRUE));
110   }
111   EXPECT_EQ(1u, static_cast<DataObjectImpl*>(com_data.get())->size());
112 
113   // Construct a new object with the old object so that we can use our access
114   // APIs.
115   OSExchangeData data2(data.provider().Clone());
116   EXPECT_TRUE(data2.HasURL());
117   GURL url_from_data;
118   std::wstring title;
119   EXPECT_TRUE(data2.GetURLAndTitle(&url_from_data, &title));
120   EXPECT_EQ(GURL(input2).spec(), url_from_data.spec());
121 }
122 
TEST(OSExchangeDataWinTest,URLDataAccessViaCOM)123 TEST(OSExchangeDataWinTest, URLDataAccessViaCOM) {
124   OSExchangeData data;
125   GURL url("http://www.google.com/");
126   data.SetURL(url, L"");
127   base::win::ScopedComPtr<IDataObject> com_data(
128       OSExchangeDataProviderWin::GetIDataObject(data));
129 
130   CLIPFORMAT cfstr_ineturl = RegisterClipboardFormat(CFSTR_INETURL);
131   FORMATETC format_etc =
132       { cfstr_ineturl, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
133   EXPECT_EQ(S_OK, com_data->QueryGetData(&format_etc));
134 
135   STGMEDIUM medium;
136   EXPECT_EQ(S_OK, com_data->GetData(&format_etc, &medium));
137   std::wstring output =
138       base::win::ScopedHGlobal<wchar_t>(medium.hGlobal).get();
139   EXPECT_EQ(url.spec(), WideToUTF8(output));
140   ReleaseStgMedium(&medium);
141 }
142 
TEST(OSExchangeDataWinTest,MultipleFormatsViaCOM)143 TEST(OSExchangeDataWinTest, MultipleFormatsViaCOM) {
144   OSExchangeData data;
145   std::string url_spec = "http://www.google.com/";
146   GURL url(url_spec);
147   std::wstring text = L"O hai googlz.";
148   data.SetURL(url, L"Google");
149   data.SetString(text);
150 
151   base::win::ScopedComPtr<IDataObject> com_data(
152       OSExchangeDataProviderWin::GetIDataObject(data));
153 
154   CLIPFORMAT cfstr_ineturl = RegisterClipboardFormat(CFSTR_INETURL);
155   FORMATETC url_format_etc =
156       { cfstr_ineturl, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
157   EXPECT_EQ(S_OK, com_data->QueryGetData(&url_format_etc));
158   FORMATETC text_format_etc =
159       { CF_UNICODETEXT, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
160   EXPECT_EQ(S_OK, com_data->QueryGetData(&text_format_etc));
161 
162   STGMEDIUM medium;
163   EXPECT_EQ(S_OK, com_data->GetData(&url_format_etc, &medium));
164   std::wstring output_url =
165       base::win::ScopedHGlobal<wchar_t>(medium.hGlobal).get();
166   EXPECT_EQ(url.spec(), WideToUTF8(output_url));
167   ReleaseStgMedium(&medium);
168 
169   // The text is supposed to be the raw text of the URL, _NOT_ the value of
170   // |text|! This is because the URL is added first and thus takes precedence!
171   EXPECT_EQ(S_OK, com_data->GetData(&text_format_etc, &medium));
172   std::wstring output_text =
173       base::win::ScopedHGlobal<wchar_t>(medium.hGlobal).get();
174   EXPECT_EQ(url_spec, WideToUTF8(output_text));
175   ReleaseStgMedium(&medium);
176 }
177 
TEST(OSExchangeDataWinTest,EnumerationViaCOM)178 TEST(OSExchangeDataWinTest, EnumerationViaCOM) {
179   OSExchangeData data;
180   data.SetURL(GURL("http://www.google.com/"), L"");
181   data.SetString(L"O hai googlz.");
182 
183   CLIPFORMAT cfstr_file_group_descriptor =
184       RegisterClipboardFormat(CFSTR_FILEDESCRIPTOR);
185   CLIPFORMAT text_x_moz_url = RegisterClipboardFormat(L"text/x-moz-url");
186 
187   base::win::ScopedComPtr<IDataObject> com_data(
188       OSExchangeDataProviderWin::GetIDataObject(data));
189   base::win::ScopedComPtr<IEnumFORMATETC> enumerator;
190   EXPECT_EQ(S_OK, com_data.get()->EnumFormatEtc(DATADIR_GET,
191                                                 enumerator.Receive()));
192 
193   // Test that we can get one item.
194   {
195     // Explictly don't reset the first time, to verify the creation state is
196     // OK.
197     ULONG retrieved = 0;
198     FORMATETC elements_array[1];
199     EXPECT_EQ(S_OK, enumerator->Next(1,
200         reinterpret_cast<FORMATETC*>(&elements_array), &retrieved));
201     EXPECT_EQ(1, retrieved);
202     EXPECT_EQ(text_x_moz_url, elements_array[0].cfFormat);
203   }
204 
205   // Test that we can get one item with a NULL retrieved value.
206   {
207     EXPECT_EQ(S_OK, enumerator->Reset());
208     FORMATETC elements_array[1];
209     EXPECT_EQ(S_OK, enumerator->Next(1,
210         reinterpret_cast<FORMATETC*>(&elements_array), NULL));
211     EXPECT_EQ(text_x_moz_url, elements_array[0].cfFormat);
212   }
213 
214   // Test that we can get two items.
215   {
216     EXPECT_EQ(S_OK, enumerator->Reset());
217     ULONG retrieved = 0;
218     FORMATETC elements_array[2];
219     EXPECT_EQ(S_OK, enumerator->Next(2,
220         reinterpret_cast<FORMATETC*>(&elements_array), &retrieved));
221     EXPECT_EQ(2, retrieved);
222     EXPECT_EQ(text_x_moz_url, elements_array[0].cfFormat);
223     EXPECT_EQ(cfstr_file_group_descriptor, elements_array[1].cfFormat);
224   }
225 
226   // Test that we can skip the first item.
227   {
228     EXPECT_EQ(S_OK, enumerator->Reset());
229     EXPECT_EQ(S_OK, enumerator->Skip(1));
230     ULONG retrieved = 0;
231     FORMATETC elements_array[1];
232     EXPECT_EQ(S_OK, enumerator->Next(1,
233         reinterpret_cast<FORMATETC*>(&elements_array), &retrieved));
234     EXPECT_EQ(1, retrieved);
235     EXPECT_EQ(cfstr_file_group_descriptor, elements_array[0].cfFormat);
236   }
237 
238   // Test that we can skip the first item, and create a clone that matches in
239   // this state, and modify the original without affecting the clone.
240   {
241     EXPECT_EQ(S_OK, enumerator->Reset());
242     EXPECT_EQ(S_OK, enumerator->Skip(1));
243     base::win::ScopedComPtr<IEnumFORMATETC> cloned_enumerator;
244     EXPECT_EQ(S_OK, enumerator.get()->Clone(cloned_enumerator.Receive()));
245     EXPECT_EQ(S_OK, enumerator.get()->Reset());
246 
247     {
248       ULONG retrieved = 0;
249       FORMATETC elements_array[1];
250       EXPECT_EQ(S_OK, cloned_enumerator->Next(1,
251           reinterpret_cast<FORMATETC*>(&elements_array), &retrieved));
252       EXPECT_EQ(1, retrieved);
253       EXPECT_EQ(cfstr_file_group_descriptor, elements_array[0].cfFormat);
254     }
255 
256     {
257       ULONG retrieved = 0;
258       FORMATETC elements_array[1];
259       EXPECT_EQ(S_OK, enumerator->Next(1,
260           reinterpret_cast<FORMATETC*>(&elements_array), &retrieved));
261       EXPECT_EQ(1, retrieved);
262       EXPECT_EQ(text_x_moz_url, elements_array[0].cfFormat);
263     }
264   }
265 }
266 
TEST(OSExchangeDataWinTest,TestURLExchangeFormatsViaCOM)267 TEST(OSExchangeDataWinTest, TestURLExchangeFormatsViaCOM) {
268   OSExchangeData data;
269   std::string url_spec = "http://www.google.com/";
270   GURL url(url_spec);
271   std::wstring url_title = L"www.google.com";
272   data.SetURL(url, url_title);
273 
274   // File contents access via COM
275   base::win::ScopedComPtr<IDataObject> com_data(
276       OSExchangeDataProviderWin::GetIDataObject(data));
277   {
278     CLIPFORMAT cfstr_file_contents =
279         RegisterClipboardFormat(CFSTR_FILECONTENTS);
280     FORMATETC format_etc =
281         { cfstr_file_contents, NULL, DVASPECT_CONTENT, 0, TYMED_HGLOBAL };
282     EXPECT_EQ(S_OK, com_data->QueryGetData(&format_etc));
283 
284     STGMEDIUM medium;
285     EXPECT_EQ(S_OK, com_data->GetData(&format_etc, &medium));
286     base::win::ScopedHGlobal<char> glob(medium.hGlobal);
287     std::string output(glob.get(), glob.Size());
288     std::string file_contents = "[InternetShortcut]\r\nURL=";
289     file_contents += url_spec;
290     file_contents += "\r\n";
291     EXPECT_EQ(file_contents, output);
292     ReleaseStgMedium(&medium);
293   }
294 }
295 
TEST(OSExchangeDataWinTest,FileContents)296 TEST(OSExchangeDataWinTest, FileContents) {
297   OSExchangeData data;
298   std::string file_contents("data\0with\0nulls", 15);
299   data.SetFileContents(base::FilePath(L"filename.txt"), file_contents);
300 
301   OSExchangeData copy(data.provider().Clone());
302   base::FilePath filename;
303   std::string read_contents;
304   EXPECT_TRUE(copy.GetFileContents(&filename, &read_contents));
305   EXPECT_EQ(L"filename.txt", filename.value());
306   EXPECT_EQ(file_contents, read_contents);
307 }
308 
TEST(OSExchangeDataWinTest,CFHtml)309 TEST(OSExchangeDataWinTest, CFHtml) {
310   OSExchangeData data;
311   GURL url("http://www.google.com/");
312   std::wstring html(
313       L"<HTML>\n<BODY>\n"
314       L"<b>bold.</b> <i><b>This is bold italic.</b></i>\n"
315       L"</BODY>\n</HTML>");
316   data.SetHtml(html, url);
317 
318   // Check the CF_HTML too.
319   std::string expected_cf_html(
320       "Version:0.9\r\nStartHTML:0000000139\r\nEndHTML:0000000288\r\n"
321       "StartFragment:0000000175\r\nEndFragment:0000000252\r\n"
322       "SourceURL:http://www.google.com/\r\n<html>\r\n<body>\r\n"
323       "<!--StartFragment-->");
324   expected_cf_html += WideToUTF8(html);
325   expected_cf_html.append("<!--EndFragment-->\r\n</body>\r\n</html>");
326 
327   FORMATETC format = Clipboard::GetHtmlFormatType().ToFormatEtc();
328   STGMEDIUM medium;
329   IDataObject* data_object = OSExchangeDataProviderWin::GetIDataObject(data);
330   EXPECT_EQ(S_OK, data_object->GetData(&format, &medium));
331   base::win::ScopedHGlobal<char> glob(medium.hGlobal);
332   std::string output(glob.get(), glob.Size());
333   EXPECT_EQ(expected_cf_html, output);
334   ReleaseStgMedium(&medium);
335 }
336 
TEST(OSExchangeDataWinTest,SetURLWithMaxPath)337 TEST(OSExchangeDataWinTest, SetURLWithMaxPath) {
338   OSExchangeData data;
339   std::wstring long_title(L'a', MAX_PATH + 1);
340   data.SetURL(GURL("http://google.com"), long_title);
341 }
342 
TEST(OSExchangeDataWinTest,ProvideURLForPlainTextURL)343 TEST(OSExchangeDataWinTest, ProvideURLForPlainTextURL) {
344   OSExchangeData data;
345   data.SetString(L"http://google.com");
346 
347   OSExchangeData data2(data.provider().Clone());
348   ASSERT_TRUE(data2.HasURL());
349   GURL read_url;
350   std::wstring title;
351   EXPECT_TRUE(data2.GetURLAndTitle(&read_url, &title));
352   EXPECT_EQ(GURL("http://google.com"), read_url);
353 }
354 
355 }  // namespace ui
356