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