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