Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(106)

Side by Side Diff: app/os_exchange_data_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698