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/memory/ref_counted.h"
6 #include "base/memory/scoped_ptr.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/win/scoped_hglobal.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "ui/base/clipboard/clipboard.h"
11 #include "ui/base/dragdrop/os_exchange_data.h"
12 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
17 // Test getting using the IDataObject COM API
18 TEST(OSExchangeDataWinTest
, StringDataAccessViaCOM
) {
20 std::wstring input
= L
"O hai googlz.";
21 data
.SetString(input
);
22 base::win::ScopedComPtr
<IDataObject
> com_data(
23 OSExchangeDataProviderWin::GetIDataObject(data
));
25 FORMATETC format_etc
=
26 { CF_UNICODETEXT
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
27 EXPECT_EQ(S_OK
, com_data
->QueryGetData(&format_etc
));
30 EXPECT_EQ(S_OK
, com_data
->GetData(&format_etc
, &medium
));
32 base::win::ScopedHGlobal
<wchar_t*>(medium
.hGlobal
).get();
33 EXPECT_EQ(input
, output
);
34 ReleaseStgMedium(&medium
);
37 // Test setting using the IDataObject COM API
38 TEST(OSExchangeDataWinTest
, StringDataWritingViaCOM
) {
40 std::wstring input
= L
"http://www.google.com/";
42 base::win::ScopedComPtr
<IDataObject
> com_data(
43 OSExchangeDataProviderWin::GetIDataObject(data
));
45 // Store data in the object using the COM SetData API.
46 CLIPFORMAT cfstr_ineturl
= RegisterClipboardFormat(CFSTR_INETURL
);
47 FORMATETC format_etc
=
48 { cfstr_ineturl
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
50 medium
.tymed
= TYMED_HGLOBAL
;
51 HGLOBAL glob
= GlobalAlloc(GPTR
, sizeof(wchar_t) * (input
.size() + 1));
52 base::win::ScopedHGlobal
<wchar_t*> global_lock(glob
);
53 wchar_t* buffer_handle
= global_lock
.get();
54 wcscpy_s(buffer_handle
, input
.size() + 1, input
.c_str());
55 medium
.hGlobal
= glob
;
56 medium
.pUnkForRelease
= NULL
;
57 EXPECT_EQ(S_OK
, com_data
->SetData(&format_etc
, &medium
, TRUE
));
59 // Construct a new object with the old object so that we can use our access
61 OSExchangeData
data2(data
.provider().Clone());
62 EXPECT_TRUE(data2
.HasURL(OSExchangeData::CONVERT_FILENAMES
));
65 EXPECT_TRUE(data2
.GetURLAndTitle(
66 OSExchangeData::CONVERT_FILENAMES
, &url_from_data
, &title
));
67 GURL
reference_url(input
);
68 EXPECT_EQ(reference_url
.spec(), url_from_data
.spec());
71 // Verifies SetData invoked twice with the same data clobbers existing data.
72 TEST(OSExchangeDataWinTest
, RemoveData
) {
74 std::wstring input
= L
"http://www.google.com/";
75 std::wstring input2
= L
"http://www.google2.com/";
77 base::win::ScopedComPtr
<IDataObject
> com_data(
78 OSExchangeDataProviderWin::GetIDataObject(data
));
80 // Store data in the object using the COM SetData API.
81 CLIPFORMAT cfstr_ineturl
= RegisterClipboardFormat(CFSTR_INETURL
);
82 FORMATETC format_etc
=
83 { cfstr_ineturl
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
85 medium
.tymed
= TYMED_HGLOBAL
;
87 HGLOBAL glob
= GlobalAlloc(GPTR
, sizeof(wchar_t) * (input
.size() + 1));
88 base::win::ScopedHGlobal
<wchar_t*> global_lock(glob
);
89 wchar_t* buffer_handle
= global_lock
.get();
90 wcscpy_s(buffer_handle
, input
.size() + 1, input
.c_str());
91 medium
.hGlobal
= glob
;
92 medium
.pUnkForRelease
= NULL
;
93 EXPECT_EQ(S_OK
, com_data
->SetData(&format_etc
, &medium
, TRUE
));
95 // This should clobber the existing data.
97 HGLOBAL glob
= GlobalAlloc(GPTR
, sizeof(wchar_t) * (input2
.size() + 1));
98 base::win::ScopedHGlobal
<wchar_t*> global_lock(glob
);
99 wchar_t* buffer_handle
= global_lock
.get();
100 wcscpy_s(buffer_handle
, input2
.size() + 1, input2
.c_str());
101 medium
.hGlobal
= glob
;
102 medium
.pUnkForRelease
= NULL
;
103 EXPECT_EQ(S_OK
, com_data
->SetData(&format_etc
, &medium
, TRUE
));
105 EXPECT_EQ(1u, static_cast<DataObjectImpl
*>(com_data
.get())->size());
107 // Construct a new object with the old object so that we can use our access
109 OSExchangeData
data2(data
.provider().Clone());
110 EXPECT_TRUE(data2
.HasURL(OSExchangeData::CONVERT_FILENAMES
));
113 EXPECT_TRUE(data2
.GetURLAndTitle(
114 OSExchangeData::CONVERT_FILENAMES
, &url_from_data
, &title
));
115 EXPECT_EQ(GURL(input2
).spec(), url_from_data
.spec());
118 TEST(OSExchangeDataWinTest
, URLDataAccessViaCOM
) {
120 GURL
url("http://www.google.com/");
121 data
.SetURL(url
, L
"");
122 base::win::ScopedComPtr
<IDataObject
> com_data(
123 OSExchangeDataProviderWin::GetIDataObject(data
));
125 CLIPFORMAT cfstr_ineturl
= RegisterClipboardFormat(CFSTR_INETURL
);
126 FORMATETC format_etc
=
127 { cfstr_ineturl
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
128 EXPECT_EQ(S_OK
, com_data
->QueryGetData(&format_etc
));
131 EXPECT_EQ(S_OK
, com_data
->GetData(&format_etc
, &medium
));
132 std::wstring output
=
133 base::win::ScopedHGlobal
<wchar_t*>(medium
.hGlobal
).get();
134 EXPECT_EQ(url
.spec(), base::WideToUTF8(output
));
135 ReleaseStgMedium(&medium
);
138 TEST(OSExchangeDataWinTest
, MultipleFormatsViaCOM
) {
140 std::string url_spec
= "http://www.google.com/";
142 std::wstring text
= L
"O hai googlz.";
143 data
.SetURL(url
, L
"Google");
144 data
.SetString(text
);
146 base::win::ScopedComPtr
<IDataObject
> com_data(
147 OSExchangeDataProviderWin::GetIDataObject(data
));
149 CLIPFORMAT cfstr_ineturl
= RegisterClipboardFormat(CFSTR_INETURL
);
150 FORMATETC url_format_etc
=
151 { cfstr_ineturl
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
152 EXPECT_EQ(S_OK
, com_data
->QueryGetData(&url_format_etc
));
153 FORMATETC text_format_etc
=
154 { CF_UNICODETEXT
, NULL
, DVASPECT_CONTENT
, -1, TYMED_HGLOBAL
};
155 EXPECT_EQ(S_OK
, com_data
->QueryGetData(&text_format_etc
));
158 EXPECT_EQ(S_OK
, com_data
->GetData(&url_format_etc
, &medium
));
159 std::wstring output_url
=
160 base::win::ScopedHGlobal
<wchar_t*>(medium
.hGlobal
).get();
161 EXPECT_EQ(url
.spec(), base::WideToUTF8(output_url
));
162 ReleaseStgMedium(&medium
);
164 // The text is supposed to be the raw text of the URL, _NOT_ the value of
165 // |text|! This is because the URL is added first and thus takes precedence!
166 EXPECT_EQ(S_OK
, com_data
->GetData(&text_format_etc
, &medium
));
167 std::wstring output_text
=
168 base::win::ScopedHGlobal
<wchar_t*>(medium
.hGlobal
).get();
169 EXPECT_EQ(url_spec
, base::WideToUTF8(output_text
));
170 ReleaseStgMedium(&medium
);
173 TEST(OSExchangeDataWinTest
, EnumerationViaCOM
) {
175 data
.SetURL(GURL("http://www.google.com/"), L
"");
176 data
.SetString(L
"O hai googlz.");
178 CLIPFORMAT cfstr_file_group_descriptor
=
179 RegisterClipboardFormat(CFSTR_FILEDESCRIPTOR
);
180 CLIPFORMAT text_x_moz_url
= RegisterClipboardFormat(L
"text/x-moz-url");
182 base::win::ScopedComPtr
<IDataObject
> com_data(
183 OSExchangeDataProviderWin::GetIDataObject(data
));
184 base::win::ScopedComPtr
<IEnumFORMATETC
> enumerator
;
185 EXPECT_EQ(S_OK
, com_data
.get()->EnumFormatEtc(DATADIR_GET
,
186 enumerator
.Receive()));
188 // Test that we can get one item.
190 // Explictly don't reset the first time, to verify the creation state is
193 FORMATETC elements_array
[1];
194 EXPECT_EQ(S_OK
, enumerator
->Next(1,
195 reinterpret_cast<FORMATETC
*>(&elements_array
), &retrieved
));
196 EXPECT_EQ(1, retrieved
);
197 EXPECT_EQ(text_x_moz_url
, elements_array
[0].cfFormat
);
200 // Test that we can get one item with a NULL retrieved value.
202 EXPECT_EQ(S_OK
, enumerator
->Reset());
203 FORMATETC elements_array
[1];
204 EXPECT_EQ(S_OK
, enumerator
->Next(1,
205 reinterpret_cast<FORMATETC
*>(&elements_array
), NULL
));
206 EXPECT_EQ(text_x_moz_url
, elements_array
[0].cfFormat
);
209 // Test that we can get two items.
211 EXPECT_EQ(S_OK
, enumerator
->Reset());
213 FORMATETC elements_array
[2];
214 EXPECT_EQ(S_OK
, enumerator
->Next(2,
215 reinterpret_cast<FORMATETC
*>(&elements_array
), &retrieved
));
216 EXPECT_EQ(2, retrieved
);
217 EXPECT_EQ(text_x_moz_url
, elements_array
[0].cfFormat
);
218 EXPECT_EQ(cfstr_file_group_descriptor
, elements_array
[1].cfFormat
);
221 // Test that we can skip the first item.
223 EXPECT_EQ(S_OK
, enumerator
->Reset());
224 EXPECT_EQ(S_OK
, enumerator
->Skip(1));
226 FORMATETC elements_array
[1];
227 EXPECT_EQ(S_OK
, enumerator
->Next(1,
228 reinterpret_cast<FORMATETC
*>(&elements_array
), &retrieved
));
229 EXPECT_EQ(1, retrieved
);
230 EXPECT_EQ(cfstr_file_group_descriptor
, elements_array
[0].cfFormat
);
233 // Test that we can skip the first item, and create a clone that matches in
234 // this state, and modify the original without affecting the clone.
236 EXPECT_EQ(S_OK
, enumerator
->Reset());
237 EXPECT_EQ(S_OK
, enumerator
->Skip(1));
238 base::win::ScopedComPtr
<IEnumFORMATETC
> cloned_enumerator
;
239 EXPECT_EQ(S_OK
, enumerator
.get()->Clone(cloned_enumerator
.Receive()));
240 EXPECT_EQ(S_OK
, enumerator
.get()->Reset());
244 FORMATETC elements_array
[1];
245 EXPECT_EQ(S_OK
, cloned_enumerator
->Next(1,
246 reinterpret_cast<FORMATETC
*>(&elements_array
), &retrieved
));
247 EXPECT_EQ(1, retrieved
);
248 EXPECT_EQ(cfstr_file_group_descriptor
, elements_array
[0].cfFormat
);
253 FORMATETC elements_array
[1];
254 EXPECT_EQ(S_OK
, enumerator
->Next(1,
255 reinterpret_cast<FORMATETC
*>(&elements_array
), &retrieved
));
256 EXPECT_EQ(1, retrieved
);
257 EXPECT_EQ(text_x_moz_url
, elements_array
[0].cfFormat
);
262 TEST(OSExchangeDataWinTest
, TestURLExchangeFormatsViaCOM
) {
264 std::string url_spec
= "http://www.google.com/";
266 std::wstring url_title
= L
"www.google.com";
267 data
.SetURL(url
, url_title
);
269 // File contents access via COM
270 base::win::ScopedComPtr
<IDataObject
> com_data(
271 OSExchangeDataProviderWin::GetIDataObject(data
));
273 CLIPFORMAT cfstr_file_contents
=
274 RegisterClipboardFormat(CFSTR_FILECONTENTS
);
275 FORMATETC format_etc
=
276 { cfstr_file_contents
, NULL
, DVASPECT_CONTENT
, 0, TYMED_HGLOBAL
};
277 EXPECT_EQ(S_OK
, com_data
->QueryGetData(&format_etc
));
280 EXPECT_EQ(S_OK
, com_data
->GetData(&format_etc
, &medium
));
281 base::win::ScopedHGlobal
<char*> glob(medium
.hGlobal
);
282 std::string
output(glob
.get(), glob
.Size());
283 std::string file_contents
= "[InternetShortcut]\r\nURL=";
284 file_contents
+= url_spec
;
285 file_contents
+= "\r\n";
286 EXPECT_EQ(file_contents
, output
);
287 ReleaseStgMedium(&medium
);
291 TEST(OSExchangeDataWinTest
, FileContents
) {
293 std::string
file_contents("data\0with\0nulls", 15);
294 data
.SetFileContents(base::FilePath(L
"filename.txt"), file_contents
);
296 OSExchangeData
copy(data
.provider().Clone());
297 base::FilePath filename
;
298 std::string read_contents
;
299 EXPECT_TRUE(copy
.GetFileContents(&filename
, &read_contents
));
300 EXPECT_EQ(L
"filename.txt", filename
.value());
301 EXPECT_EQ(file_contents
, read_contents
);
304 TEST(OSExchangeDataWinTest
, CFHtml
) {
306 GURL
url("http://www.google.com/");
309 L
"<b>bold.</b> <i><b>This is bold italic.</b></i>\n"
310 L
"</BODY>\n</HTML>");
311 data
.SetHtml(html
, url
);
313 // Check the CF_HTML too.
314 std::string
expected_cf_html(
315 "Version:0.9\r\nStartHTML:0000000139\r\nEndHTML:0000000288\r\n"
316 "StartFragment:0000000175\r\nEndFragment:0000000252\r\n"
317 "SourceURL:http://www.google.com/\r\n<html>\r\n<body>\r\n"
318 "<!--StartFragment-->");
319 expected_cf_html
+= base::WideToUTF8(html
);
320 expected_cf_html
.append("<!--EndFragment-->\r\n</body>\r\n</html>");
322 FORMATETC format
= Clipboard::GetHtmlFormatType().ToFormatEtc();
324 IDataObject
* data_object
= OSExchangeDataProviderWin::GetIDataObject(data
);
325 EXPECT_EQ(S_OK
, data_object
->GetData(&format
, &medium
));
326 base::win::ScopedHGlobal
<char*> glob(medium
.hGlobal
);
327 std::string
output(glob
.get(), glob
.Size());
328 EXPECT_EQ(expected_cf_html
, output
);
329 ReleaseStgMedium(&medium
);
332 TEST(OSExchangeDataWinTest
, SetURLWithMaxPath
) {
334 std::wstring
long_title(L
'a', MAX_PATH
+ 1);
335 data
.SetURL(GURL("http://google.com"), long_title
);
338 TEST(OSExchangeDataWinTest
, ProvideURLForPlainTextURL
) {
340 data
.SetString(L
"http://google.com");
342 OSExchangeData
data2(data
.provider().Clone());
343 ASSERT_TRUE(data2
.HasURL(OSExchangeData::CONVERT_FILENAMES
));
346 EXPECT_TRUE(data2
.GetURLAndTitle(
347 OSExchangeData::CONVERT_FILENAMES
, &read_url
, &title
));
348 EXPECT_EQ(GURL("http://google.com"), read_url
);