Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / base / clipboard / clipboard_test_template.h
blob2b298da68d9412093e4a46b2edac610db8f4a603
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 // Note: This header doesn't use REGISTER_TYPED_TEST_CASE_P like most
6 // type-parameterized gtests. There are lot of test cases in here that are only
7 // enabled on certain platforms. However, preprocessor directives in macro
8 // arguments result in undefined behavior (and don't work on MSVC). Instead,
9 // 'parameterized' tests should typedef TypesToTest (which is used to
10 // instantiate the tests using the TYPED_TEST_CASE macro) and then #include this
11 // header.
12 // TODO(dcheng): This is really horrible. In general, all tests should run on
13 // all platforms, to avoid this mess.
15 #include "build/build_config.h"
17 #include <string>
19 #include "base/basictypes.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/message_loop/message_loop.h"
22 #include "base/pickle.h"
23 #include "base/run_loop.h"
24 #include "base/strings/string_util.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "testing/gmock/include/gmock/gmock-matchers.h"
27 #include "testing/gtest/include/gtest/gtest.h"
28 #include "testing/platform_test.h"
29 #include "third_party/skia/include/core/SkBitmap.h"
30 #include "third_party/skia/include/core/SkColor.h"
31 #include "third_party/skia/include/core/SkScalar.h"
32 #include "third_party/skia/include/core/SkUnPreMultiply.h"
33 #include "ui/base/clipboard/clipboard.h"
34 #include "ui/base/clipboard/scoped_clipboard_writer.h"
35 #include "ui/base/test/test_clipboard.h"
36 #include "ui/gfx/geometry/size.h"
38 #if defined(OS_WIN)
39 #include "ui/base/clipboard/clipboard_util_win.h"
40 #endif
42 #if defined(USE_AURA)
43 #include "ui/events/platform/platform_event_source.h"
44 #endif
46 using base::ASCIIToUTF16;
47 using base::UTF8ToUTF16;
48 using base::UTF16ToUTF8;
50 using testing::Contains;
52 namespace ui {
54 template <typename ClipboardTraits>
55 class ClipboardTest : public PlatformTest {
56 public:
57 #if defined(USE_AURA)
58 ClipboardTest()
59 : event_source_(PlatformEventSource::CreateDefault()),
60 clipboard_(ClipboardTraits::Create()) {}
61 #else
62 ClipboardTest() : clipboard_(ClipboardTraits::Create()) {}
63 #endif
65 ~ClipboardTest() override { ClipboardTraits::Destroy(clipboard_); }
67 protected:
68 Clipboard& clipboard() { return *clipboard_; }
70 std::vector<base::string16> GetAvailableTypes(ClipboardType type) {
71 bool contains_filenames;
72 std::vector<base::string16> types;
73 clipboard().ReadAvailableTypes(type, &types, &contains_filenames);
74 return types;
77 private:
78 base::MessageLoopForUI message_loop_;
79 #if defined(USE_AURA)
80 scoped_ptr<PlatformEventSource> event_source_;
81 #endif
82 // ui::Clipboard has a protected destructor, so scoped_ptr doesn't work here.
83 Clipboard* const clipboard_;
86 // Hack for tests that need to call static methods of ClipboardTest.
87 struct NullClipboardTraits {
88 static Clipboard* Create() { return nullptr; }
89 static void Destroy(Clipboard*) {}
92 TYPED_TEST_CASE(ClipboardTest, TypesToTest);
94 TYPED_TEST(ClipboardTest, ClearTest) {
96 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
97 clipboard_writer.WriteText(ASCIIToUTF16("clear me"));
100 this->clipboard().Clear(CLIPBOARD_TYPE_COPY_PASTE);
102 EXPECT_TRUE(this->GetAvailableTypes(CLIPBOARD_TYPE_COPY_PASTE).empty());
103 EXPECT_FALSE(this->clipboard().IsFormatAvailable(
104 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
105 EXPECT_FALSE(this->clipboard().IsFormatAvailable(
106 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
109 TYPED_TEST(ClipboardTest, TextTest) {
110 base::string16 text(ASCIIToUTF16("This is a base::string16!#$")), text_result;
111 std::string ascii_text;
114 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
115 clipboard_writer.WriteText(text);
118 EXPECT_THAT(this->GetAvailableTypes(CLIPBOARD_TYPE_COPY_PASTE),
119 Contains(ASCIIToUTF16(Clipboard::kMimeTypeText)));
120 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
121 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
122 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
123 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
124 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
126 EXPECT_EQ(text, text_result);
127 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text);
128 EXPECT_EQ(UTF16ToUTF8(text), ascii_text);
131 TYPED_TEST(ClipboardTest, HTMLTest) {
132 base::string16 markup(ASCIIToUTF16("<string>Hi!</string>")), markup_result;
133 base::string16 plain(ASCIIToUTF16("Hi!")), plain_result;
134 std::string url("http://www.example.com/"), url_result;
137 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
138 clipboard_writer.WriteText(plain);
139 clipboard_writer.WriteHTML(markup, url);
142 EXPECT_THAT(this->GetAvailableTypes(CLIPBOARD_TYPE_COPY_PASTE),
143 Contains(ASCIIToUTF16(Clipboard::kMimeTypeHTML)));
144 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
145 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
146 uint32 fragment_start;
147 uint32 fragment_end;
148 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result,
149 &url_result, &fragment_start, &fragment_end);
150 EXPECT_LE(markup.size(), fragment_end - fragment_start);
151 EXPECT_EQ(markup,
152 markup_result.substr(fragment_end - markup.size(), markup.size()));
153 #if defined(OS_WIN)
154 // TODO(playmobil): It's not clear that non windows clipboards need to support
155 // this.
156 EXPECT_EQ(url, url_result);
157 #endif // defined(OS_WIN)
160 TYPED_TEST(ClipboardTest, RTFTest) {
161 std::string rtf =
162 "{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\n"
163 "This is some {\\b bold} text.\\par\n"
164 "}";
167 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
168 clipboard_writer.WriteRTF(rtf);
171 EXPECT_THAT(this->GetAvailableTypes(CLIPBOARD_TYPE_COPY_PASTE),
172 Contains(ASCIIToUTF16(Clipboard::kMimeTypeRTF)));
173 EXPECT_TRUE(this->clipboard().IsFormatAvailable(Clipboard::GetRtfFormatType(),
174 CLIPBOARD_TYPE_COPY_PASTE));
175 std::string result;
176 this->clipboard().ReadRTF(CLIPBOARD_TYPE_COPY_PASTE, &result);
177 EXPECT_EQ(rtf, result);
180 // TODO(dnicoara) Enable test once Ozone implements clipboard support:
181 // crbug.com/361707
182 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && !defined(USE_OZONE)
183 TYPED_TEST(ClipboardTest, MultipleBufferTest) {
184 base::string16 text(ASCIIToUTF16("Standard")), text_result;
185 base::string16 markup(ASCIIToUTF16("<string>Selection</string>"));
186 std::string url("http://www.example.com/"), url_result;
189 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
190 clipboard_writer.WriteText(text);
194 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_SELECTION);
195 clipboard_writer.WriteHTML(markup, url);
198 EXPECT_THAT(this->GetAvailableTypes(CLIPBOARD_TYPE_COPY_PASTE),
199 Contains(ASCIIToUTF16(Clipboard::kMimeTypeText)));
200 EXPECT_THAT(this->GetAvailableTypes(CLIPBOARD_TYPE_SELECTION),
201 Contains(ASCIIToUTF16(Clipboard::kMimeTypeHTML)));
203 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
204 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
205 EXPECT_FALSE(this->clipboard().IsFormatAvailable(
206 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_SELECTION));
208 EXPECT_FALSE(this->clipboard().IsFormatAvailable(
209 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
210 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
211 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_SELECTION));
213 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
214 EXPECT_EQ(text, text_result);
216 base::string16 markup_result;
217 uint32 fragment_start;
218 uint32 fragment_end;
219 this->clipboard().ReadHTML(CLIPBOARD_TYPE_SELECTION, &markup_result,
220 &url_result, &fragment_start, &fragment_end);
221 EXPECT_LE(markup.size(), fragment_end - fragment_start);
222 EXPECT_EQ(markup,
223 markup_result.substr(fragment_end - markup.size(), markup.size()));
225 #endif
227 TYPED_TEST(ClipboardTest, TrickyHTMLTest) {
228 base::string16 markup(ASCIIToUTF16("<em>Bye!<!--EndFragment --></em>")),
229 markup_result;
230 std::string url, url_result;
231 base::string16 plain(ASCIIToUTF16("Bye!")), plain_result;
234 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
235 clipboard_writer.WriteText(plain);
236 clipboard_writer.WriteHTML(markup, url);
239 EXPECT_THAT(this->GetAvailableTypes(CLIPBOARD_TYPE_COPY_PASTE),
240 Contains(ASCIIToUTF16(Clipboard::kMimeTypeHTML)));
241 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
242 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
243 uint32 fragment_start;
244 uint32 fragment_end;
245 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result,
246 &url_result, &fragment_start, &fragment_end);
247 EXPECT_LE(markup.size(), fragment_end - fragment_start);
248 EXPECT_EQ(markup,
249 markup_result.substr(fragment_end - markup.size(), markup.size()));
250 #if defined(OS_WIN)
251 // TODO(playmobil): It's not clear that non windows clipboards need to support
252 // this.
253 EXPECT_EQ(url, url_result);
254 #endif // defined(OS_WIN)
257 // Some platforms store HTML as UTF-8 internally. Make sure fragment indices are
258 // adjusted appropriately when converting back to UTF-16.
259 TYPED_TEST(ClipboardTest, UnicodeHTMLTest) {
260 base::string16 markup(UTF8ToUTF16("<div>A \xc3\xb8 \xe6\xb0\xb4</div>")),
261 markup_result;
262 std::string url, url_result;
265 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
266 clipboard_writer.WriteHTML(markup, url);
269 EXPECT_THAT(this->GetAvailableTypes(CLIPBOARD_TYPE_COPY_PASTE),
270 Contains(ASCIIToUTF16(Clipboard::kMimeTypeHTML)));
271 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
272 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
273 uint32 fragment_start;
274 uint32 fragment_end;
275 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result,
276 &url_result, &fragment_start, &fragment_end);
277 EXPECT_LE(markup.size(), fragment_end - fragment_start);
278 EXPECT_EQ(markup,
279 markup_result.substr(fragment_end - markup.size(), markup.size()));
280 #if defined(OS_WIN)
281 EXPECT_EQ(url, url_result);
282 #endif
285 // TODO(estade): Port the following test (decide what target we use for urls)
286 #if !defined(OS_POSIX) || defined(OS_MACOSX)
287 TYPED_TEST(ClipboardTest, BookmarkTest) {
288 base::string16 title(ASCIIToUTF16("The Example Company")), title_result;
289 std::string url("http://www.example.com/"), url_result;
292 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
293 clipboard_writer.WriteBookmark(title, url);
296 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
297 Clipboard::GetUrlWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
298 this->clipboard().ReadBookmark(&title_result, &url_result);
299 EXPECT_EQ(title, title_result);
300 EXPECT_EQ(url, url_result);
302 #endif // !defined(OS_POSIX) || defined(OS_MACOSX)
304 TYPED_TEST(ClipboardTest, MultiFormatTest) {
305 base::string16 text(ASCIIToUTF16("Hi!")), text_result;
306 base::string16 markup(ASCIIToUTF16("<strong>Hi!</string>")), markup_result;
307 std::string url("http://www.example.com/"), url_result;
308 std::string ascii_text;
311 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
312 clipboard_writer.WriteHTML(markup, url);
313 clipboard_writer.WriteText(text);
316 EXPECT_THAT(this->GetAvailableTypes(CLIPBOARD_TYPE_COPY_PASTE),
317 Contains(ASCIIToUTF16(Clipboard::kMimeTypeHTML)));
318 EXPECT_THAT(this->GetAvailableTypes(CLIPBOARD_TYPE_COPY_PASTE),
319 Contains(ASCIIToUTF16(Clipboard::kMimeTypeText)));
320 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
321 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
322 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
323 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
324 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
325 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
326 uint32 fragment_start;
327 uint32 fragment_end;
328 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result,
329 &url_result, &fragment_start, &fragment_end);
330 EXPECT_LE(markup.size(), fragment_end - fragment_start);
331 EXPECT_EQ(markup,
332 markup_result.substr(fragment_end - markup.size(), markup.size()));
333 #if defined(OS_WIN)
334 // TODO(playmobil): It's not clear that non windows clipboards need to support
335 // this.
336 EXPECT_EQ(url, url_result);
337 #endif // defined(OS_WIN)
338 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
339 EXPECT_EQ(text, text_result);
340 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text);
341 EXPECT_EQ(UTF16ToUTF8(text), ascii_text);
344 TYPED_TEST(ClipboardTest, URLTest) {
345 base::string16 url(ASCIIToUTF16("http://www.google.com/"));
348 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
349 clipboard_writer.WriteURL(url);
352 EXPECT_THAT(this->GetAvailableTypes(CLIPBOARD_TYPE_COPY_PASTE),
353 Contains(ASCIIToUTF16(Clipboard::kMimeTypeText)));
354 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
355 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
356 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
357 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
358 base::string16 text_result;
359 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
361 EXPECT_EQ(text_result, url);
363 std::string ascii_text;
364 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text);
365 EXPECT_EQ(UTF16ToUTF8(url), ascii_text);
367 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && \
368 !defined(OS_CHROMEOS)
369 ascii_text.clear();
370 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_SELECTION, &ascii_text);
371 EXPECT_EQ(UTF16ToUTF8(url), ascii_text);
372 #endif
375 static void TestBitmapWrite(Clipboard* clipboard,
376 const gfx::Size& size,
377 const uint32* bitmap_data) {
379 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
380 SkBitmap bitmap;
381 ASSERT_TRUE(bitmap.setInfo(
382 SkImageInfo::MakeN32Premul(size.width(), size.height())));
383 bitmap.setPixels(
384 const_cast<void*>(reinterpret_cast<const void*>(bitmap_data)));
385 scw.WriteImage(bitmap);
388 EXPECT_TRUE(clipboard->IsFormatAvailable(Clipboard::GetBitmapFormatType(),
389 CLIPBOARD_TYPE_COPY_PASTE));
390 const SkBitmap& image = clipboard->ReadImage(CLIPBOARD_TYPE_COPY_PASTE);
391 EXPECT_EQ(size, gfx::Size(image.width(), image.height()));
392 SkAutoLockPixels image_lock(image);
393 for (int j = 0; j < image.height(); ++j) {
394 const uint32* row_address = image.getAddr32(0, j);
395 for (int i = 0; i < image.width(); ++i) {
396 int offset = i + j * image.width();
397 EXPECT_EQ(bitmap_data[offset], row_address[i]) << "i = " << i
398 << ", j = " << j;
403 TYPED_TEST(ClipboardTest, SharedBitmapTest) {
404 const uint32 fake_bitmap_1[] = {
405 0x46061626, 0xf69f5988, 0x793f2937, 0xfa55b986,
406 0x78772152, 0x87692a30, 0x36322a25, 0x4320401b,
407 0x91848c21, 0xc3177b3c, 0x6946155c, 0x64171952,
410 SCOPED_TRACE("first bitmap");
411 TestBitmapWrite(&this->clipboard(), gfx::Size(4, 3), fake_bitmap_1);
414 const uint32 fake_bitmap_2[] = {
415 0x46061626, 0xf69f5988,
416 0x793f2937, 0xfa55b986,
417 0x78772152, 0x87692a30,
418 0x36322a25, 0x4320401b,
419 0x91848c21, 0xc3177b3c,
420 0x6946155c, 0x64171952,
421 0xa6910313, 0x8302323e,
424 SCOPED_TRACE("second bitmap");
425 TestBitmapWrite(&this->clipboard(), gfx::Size(2, 7), fake_bitmap_2);
429 TYPED_TEST(ClipboardTest, DataTest) {
430 const ui::Clipboard::FormatType kFormat =
431 ui::Clipboard::GetFormatType("chromium/x-test-format");
432 std::string payload("test string");
433 base::Pickle write_pickle;
434 write_pickle.WriteString(payload);
437 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
438 clipboard_writer.WritePickledData(write_pickle, kFormat);
441 ASSERT_TRUE(
442 this->clipboard().IsFormatAvailable(kFormat, CLIPBOARD_TYPE_COPY_PASTE));
443 std::string output;
444 this->clipboard().ReadData(kFormat, &output);
445 ASSERT_FALSE(output.empty());
447 base::Pickle read_pickle(output.data(), output.size());
448 base::PickleIterator iter(read_pickle);
449 std::string unpickled_string;
450 ASSERT_TRUE(iter.ReadString(&unpickled_string));
451 EXPECT_EQ(payload, unpickled_string);
454 TYPED_TEST(ClipboardTest, MultipleDataTest) {
455 const ui::Clipboard::FormatType kFormat1 =
456 ui::Clipboard::GetFormatType("chromium/x-test-format1");
457 std::string payload1("test string1");
458 base::Pickle write_pickle1;
459 write_pickle1.WriteString(payload1);
461 const ui::Clipboard::FormatType kFormat2 =
462 ui::Clipboard::GetFormatType("chromium/x-test-format2");
463 std::string payload2("test string2");
464 base::Pickle write_pickle2;
465 write_pickle2.WriteString(payload2);
468 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
469 clipboard_writer.WritePickledData(write_pickle1, kFormat1);
470 // overwrite the previous pickle for fun
471 clipboard_writer.WritePickledData(write_pickle2, kFormat2);
474 ASSERT_TRUE(
475 this->clipboard().IsFormatAvailable(kFormat2, CLIPBOARD_TYPE_COPY_PASTE));
477 // Check string 2.
478 std::string output2;
479 this->clipboard().ReadData(kFormat2, &output2);
480 ASSERT_FALSE(output2.empty());
482 base::Pickle read_pickle2(output2.data(), output2.size());
483 base::PickleIterator iter2(read_pickle2);
484 std::string unpickled_string2;
485 ASSERT_TRUE(iter2.ReadString(&unpickled_string2));
486 EXPECT_EQ(payload2, unpickled_string2);
489 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
490 clipboard_writer.WritePickledData(write_pickle2, kFormat2);
491 // overwrite the previous pickle for fun
492 clipboard_writer.WritePickledData(write_pickle1, kFormat1);
495 ASSERT_TRUE(
496 this->clipboard().IsFormatAvailable(kFormat1, CLIPBOARD_TYPE_COPY_PASTE));
498 // Check string 1.
499 std::string output1;
500 this->clipboard().ReadData(kFormat1, &output1);
501 ASSERT_FALSE(output1.empty());
503 base::Pickle read_pickle1(output1.data(), output1.size());
504 base::PickleIterator iter1(read_pickle1);
505 std::string unpickled_string1;
506 ASSERT_TRUE(iter1.ReadString(&unpickled_string1));
507 EXPECT_EQ(payload1, unpickled_string1);
510 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
511 TYPED_TEST(ClipboardTest, HyperlinkTest) {
512 const std::string kTitle("The <Example> Company's \"home page\"");
513 const std::string kUrl("http://www.example.com?x=3&lt=3#\"'<>");
514 const base::string16 kExpectedHtml(UTF8ToUTF16(
515 "<a href=\"http://www.example.com?x=3&amp;lt=3#&quot;&#39;&lt;&gt;\">"
516 "The &lt;Example&gt; Company&#39;s &quot;home page&quot;</a>"));
518 std::string url_result;
519 base::string16 html_result;
521 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
522 clipboard_writer.WriteHyperlink(ASCIIToUTF16(kTitle), kUrl);
525 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
526 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
527 uint32 fragment_start;
528 uint32 fragment_end;
529 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &html_result,
530 &url_result, &fragment_start, &fragment_end);
531 EXPECT_EQ(kExpectedHtml,
532 html_result.substr(fragment_end - kExpectedHtml.size(),
533 kExpectedHtml.size()));
535 #endif
537 TYPED_TEST(ClipboardTest, WebSmartPasteTest) {
539 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
540 clipboard_writer.WriteWebSmartPaste();
543 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
544 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
547 #if defined(OS_WIN) // Windows only tests.
548 void HtmlTestHelper(const std::string& cf_html,
549 const std::string& expected_html) {
550 std::string html;
551 ClipboardUtil::CFHtmlToHtml(cf_html, &html, NULL);
552 EXPECT_EQ(html, expected_html);
555 TYPED_TEST(ClipboardTest, HtmlTest) {
556 // Test converting from CF_HTML format data with <!--StartFragment--> and
557 // <!--EndFragment--> comments, like from MS Word.
558 HtmlTestHelper(
559 "Version:1.0\r\n"
560 "StartHTML:0000000105\r\n"
561 "EndHTML:0000000199\r\n"
562 "StartFragment:0000000123\r\n"
563 "EndFragment:0000000161\r\n"
564 "\r\n"
565 "<html>\r\n"
566 "<body>\r\n"
567 "<!--StartFragment-->\r\n"
568 "\r\n"
569 "<p>Foo</p>\r\n"
570 "\r\n"
571 "<!--EndFragment-->\r\n"
572 "</body>\r\n"
573 "</html>\r\n\r\n",
574 "<p>Foo</p>");
576 // Test converting from CF_HTML format data without <!--StartFragment--> and
577 // <!--EndFragment--> comments, like from OpenOffice Writer.
578 HtmlTestHelper(
579 "Version:1.0\r\n"
580 "StartHTML:0000000105\r\n"
581 "EndHTML:0000000151\r\n"
582 "StartFragment:0000000121\r\n"
583 "EndFragment:0000000131\r\n"
584 "<html>\r\n"
585 "<body>\r\n"
586 "<p>Foo</p>\r\n"
587 "</body>\r\n"
588 "</html>\r\n\r\n",
589 "<p>Foo</p>");
591 #endif // defined(OS_WIN)
593 // Test writing all formats we have simultaneously.
594 TYPED_TEST(ClipboardTest, WriteEverything) {
596 ScopedClipboardWriter writer(CLIPBOARD_TYPE_COPY_PASTE);
597 writer.WriteText(UTF8ToUTF16("foo"));
598 writer.WriteURL(UTF8ToUTF16("foo"));
599 writer.WriteHTML(UTF8ToUTF16("foo"), "bar");
600 writer.WriteBookmark(UTF8ToUTF16("foo"), "bar");
601 writer.WriteHyperlink(ASCIIToUTF16("foo"), "bar");
602 writer.WriteWebSmartPaste();
603 // Left out: WriteFile, WriteFiles, WriteBitmapFromPixels, WritePickledData.
606 // Passes if we don't crash.
609 // TODO(dcheng): Fix this test for Android. It's rather involved, since the
610 // clipboard change listener is posted to the Java message loop, and spinning
611 // that loop from C++ to trigger the callback in the test requires a non-trivial
612 // amount of additional work.
613 #if !defined(OS_ANDROID)
614 // Simple test that the sequence number appears to change when the clipboard is
615 // written to.
616 // TODO(dcheng): Add a version to test CLIPBOARD_TYPE_SELECTION.
617 TYPED_TEST(ClipboardTest, GetSequenceNumber) {
618 const uint64 first_sequence_number =
619 this->clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE);
622 ScopedClipboardWriter writer(CLIPBOARD_TYPE_COPY_PASTE);
623 writer.WriteText(UTF8ToUTF16("World"));
626 // On some platforms, the sequence number is updated by a UI callback so pump
627 // the message loop to make sure we get the notification.
628 base::RunLoop().RunUntilIdle();
630 const uint64 second_sequence_number =
631 this->clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE);
633 EXPECT_NE(first_sequence_number, second_sequence_number);
635 #endif
637 // Test that writing empty parameters doesn't try to dereference an empty data
638 // vector. Not crashing = passing.
639 TYPED_TEST(ClipboardTest, WriteTextEmptyParams) {
640 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
641 scw.WriteText(base::string16());
644 TYPED_TEST(ClipboardTest, WriteURLEmptyParams) {
645 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
646 scw.WriteURL(base::string16());
649 TYPED_TEST(ClipboardTest, WriteHTMLEmptyParams) {
650 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
651 scw.WriteHTML(base::string16(), std::string());
654 TYPED_TEST(ClipboardTest, WriteRTFEmptyParams) {
655 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
656 scw.WriteRTF(std::string());
659 TYPED_TEST(ClipboardTest, WriteBookmarkEmptyParams) {
660 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
661 scw.WriteBookmark(base::string16(), std::string());
664 TYPED_TEST(ClipboardTest, WriteHyperlinkEmptyParams) {
665 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
666 scw.WriteHyperlink(base::string16(), std::string());
669 TYPED_TEST(ClipboardTest, WritePickledData) {
670 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
671 scw.WritePickledData(base::Pickle(), Clipboard::GetPlainTextFormatType());
674 TYPED_TEST(ClipboardTest, WriteImageEmptyParams) {
675 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
676 scw.WriteImage(SkBitmap());
679 } // namespace ui