Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / ui / base / clipboard / clipboard_test_template.h
blob9d519213888de22f50ff226541931b849b235c45
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/gtest/include/gtest/gtest.h"
27 #include "testing/platform_test.h"
28 #include "third_party/skia/include/core/SkBitmap.h"
29 #include "third_party/skia/include/core/SkColor.h"
30 #include "third_party/skia/include/core/SkScalar.h"
31 #include "third_party/skia/include/core/SkUnPreMultiply.h"
32 #include "ui/base/clipboard/clipboard.h"
33 #include "ui/base/clipboard/scoped_clipboard_writer.h"
34 #include "ui/base/test/test_clipboard.h"
35 #include "ui/gfx/geometry/size.h"
37 #if defined(OS_WIN)
38 #include "ui/base/clipboard/clipboard_util_win.h"
39 #endif
41 #if defined(USE_AURA)
42 #include "ui/events/platform/platform_event_source.h"
43 #endif
45 using base::ASCIIToUTF16;
46 using base::UTF8ToUTF16;
47 using base::UTF16ToUTF8;
49 namespace ui {
51 template <typename ClipboardTraits>
52 class ClipboardTest : public PlatformTest {
53 public:
54 #if defined(USE_AURA)
55 ClipboardTest()
56 : event_source_(PlatformEventSource::CreateDefault()),
57 clipboard_(ClipboardTraits::Create()) {}
58 #else
59 ClipboardTest() : clipboard_(ClipboardTraits::Create()) {}
60 #endif
62 ~ClipboardTest() override { ClipboardTraits::Destroy(clipboard_); }
64 protected:
65 Clipboard& clipboard() { return *clipboard_; }
67 private:
68 base::MessageLoopForUI message_loop_;
69 #if defined(USE_AURA)
70 scoped_ptr<PlatformEventSource> event_source_;
71 #endif
72 // ui::Clipboard has a protected destructor, so scoped_ptr doesn't work here.
73 Clipboard* const clipboard_;
76 // Hack for tests that need to call static methods of ClipboardTest.
77 struct NullClipboardTraits {
78 static Clipboard* Create() { return nullptr; }
79 static void Destroy(Clipboard*) {}
82 TYPED_TEST_CASE(ClipboardTest, TypesToTest);
84 TYPED_TEST(ClipboardTest, ClearTest) {
86 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
87 clipboard_writer.WriteText(ASCIIToUTF16("clear me"));
90 this->clipboard().Clear(CLIPBOARD_TYPE_COPY_PASTE);
92 EXPECT_FALSE(this->clipboard().IsFormatAvailable(
93 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
94 EXPECT_FALSE(this->clipboard().IsFormatAvailable(
95 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
98 TYPED_TEST(ClipboardTest, TextTest) {
99 base::string16 text(ASCIIToUTF16("This is a base::string16!#$")), text_result;
100 std::string ascii_text;
103 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
104 clipboard_writer.WriteText(text);
107 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
108 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
109 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
110 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
111 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
113 EXPECT_EQ(text, text_result);
114 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text);
115 EXPECT_EQ(UTF16ToUTF8(text), ascii_text);
118 TYPED_TEST(ClipboardTest, HTMLTest) {
119 base::string16 markup(ASCIIToUTF16("<string>Hi!</string>")), markup_result;
120 base::string16 plain(ASCIIToUTF16("Hi!")), plain_result;
121 std::string url("http://www.example.com/"), url_result;
124 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
125 clipboard_writer.WriteText(plain);
126 clipboard_writer.WriteHTML(markup, url);
129 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
130 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
131 uint32 fragment_start;
132 uint32 fragment_end;
133 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result,
134 &url_result, &fragment_start, &fragment_end);
135 EXPECT_LE(markup.size(), fragment_end - fragment_start);
136 EXPECT_EQ(markup,
137 markup_result.substr(fragment_end - markup.size(), markup.size()));
138 #if defined(OS_WIN)
139 // TODO(playmobil): It's not clear that non windows clipboards need to support
140 // this.
141 EXPECT_EQ(url, url_result);
142 #endif // defined(OS_WIN)
145 TYPED_TEST(ClipboardTest, RTFTest) {
146 std::string rtf =
147 "{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\n"
148 "This is some {\\b bold} text.\\par\n"
149 "}";
152 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
153 clipboard_writer.WriteRTF(rtf);
156 EXPECT_TRUE(this->clipboard().IsFormatAvailable(Clipboard::GetRtfFormatType(),
157 CLIPBOARD_TYPE_COPY_PASTE));
158 std::string result;
159 this->clipboard().ReadRTF(CLIPBOARD_TYPE_COPY_PASTE, &result);
160 EXPECT_EQ(rtf, result);
163 // TODO(dnicoara) Enable test once Ozone implements clipboard support:
164 // crbug.com/361707
165 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && !defined(USE_OZONE)
166 TYPED_TEST(ClipboardTest, MultipleBufferTest) {
167 base::string16 text(ASCIIToUTF16("Standard")), text_result;
168 base::string16 markup(ASCIIToUTF16("<string>Selection</string>"));
169 std::string url("http://www.example.com/"), url_result;
172 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
173 clipboard_writer.WriteText(text);
177 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_SELECTION);
178 clipboard_writer.WriteHTML(markup, url);
181 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
182 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
183 EXPECT_FALSE(this->clipboard().IsFormatAvailable(
184 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_SELECTION));
186 EXPECT_FALSE(this->clipboard().IsFormatAvailable(
187 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
188 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
189 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_SELECTION));
191 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
192 EXPECT_EQ(text, text_result);
194 base::string16 markup_result;
195 uint32 fragment_start;
196 uint32 fragment_end;
197 this->clipboard().ReadHTML(CLIPBOARD_TYPE_SELECTION, &markup_result,
198 &url_result, &fragment_start, &fragment_end);
199 EXPECT_LE(markup.size(), fragment_end - fragment_start);
200 EXPECT_EQ(markup,
201 markup_result.substr(fragment_end - markup.size(), markup.size()));
203 #endif
205 TYPED_TEST(ClipboardTest, TrickyHTMLTest) {
206 base::string16 markup(ASCIIToUTF16("<em>Bye!<!--EndFragment --></em>")),
207 markup_result;
208 std::string url, url_result;
209 base::string16 plain(ASCIIToUTF16("Bye!")), plain_result;
212 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
213 clipboard_writer.WriteText(plain);
214 clipboard_writer.WriteHTML(markup, url);
217 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
218 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
219 uint32 fragment_start;
220 uint32 fragment_end;
221 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result,
222 &url_result, &fragment_start, &fragment_end);
223 EXPECT_LE(markup.size(), fragment_end - fragment_start);
224 EXPECT_EQ(markup,
225 markup_result.substr(fragment_end - markup.size(), markup.size()));
226 #if defined(OS_WIN)
227 // TODO(playmobil): It's not clear that non windows clipboards need to support
228 // this.
229 EXPECT_EQ(url, url_result);
230 #endif // defined(OS_WIN)
233 // Some platforms store HTML as UTF-8 internally. Make sure fragment indices are
234 // adjusted appropriately when converting back to UTF-16.
235 TYPED_TEST(ClipboardTest, UnicodeHTMLTest) {
236 base::string16 markup(UTF8ToUTF16("<div>A \xc3\xb8 \xe6\xb0\xb4</div>")),
237 markup_result;
238 std::string url, url_result;
241 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
242 clipboard_writer.WriteHTML(markup, url);
245 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
246 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
247 uint32 fragment_start;
248 uint32 fragment_end;
249 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result,
250 &url_result, &fragment_start, &fragment_end);
251 EXPECT_LE(markup.size(), fragment_end - fragment_start);
252 EXPECT_EQ(markup,
253 markup_result.substr(fragment_end - markup.size(), markup.size()));
254 #if defined(OS_WIN)
255 EXPECT_EQ(url, url_result);
256 #endif
259 // TODO(estade): Port the following test (decide what target we use for urls)
260 #if !defined(OS_POSIX) || defined(OS_MACOSX)
261 TYPED_TEST(ClipboardTest, BookmarkTest) {
262 base::string16 title(ASCIIToUTF16("The Example Company")), title_result;
263 std::string url("http://www.example.com/"), url_result;
266 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
267 clipboard_writer.WriteBookmark(title, url);
270 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
271 Clipboard::GetUrlWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
272 this->clipboard().ReadBookmark(&title_result, &url_result);
273 EXPECT_EQ(title, title_result);
274 EXPECT_EQ(url, url_result);
276 #endif // !defined(OS_POSIX) || defined(OS_MACOSX)
278 TYPED_TEST(ClipboardTest, MultiFormatTest) {
279 base::string16 text(ASCIIToUTF16("Hi!")), text_result;
280 base::string16 markup(ASCIIToUTF16("<strong>Hi!</string>")), markup_result;
281 std::string url("http://www.example.com/"), url_result;
282 std::string ascii_text;
285 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
286 clipboard_writer.WriteHTML(markup, url);
287 clipboard_writer.WriteText(text);
290 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
291 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
292 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
293 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
294 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
295 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
296 uint32 fragment_start;
297 uint32 fragment_end;
298 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result,
299 &url_result, &fragment_start, &fragment_end);
300 EXPECT_LE(markup.size(), fragment_end - fragment_start);
301 EXPECT_EQ(markup,
302 markup_result.substr(fragment_end - markup.size(), markup.size()));
303 #if defined(OS_WIN)
304 // TODO(playmobil): It's not clear that non windows clipboards need to support
305 // this.
306 EXPECT_EQ(url, url_result);
307 #endif // defined(OS_WIN)
308 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
309 EXPECT_EQ(text, text_result);
310 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text);
311 EXPECT_EQ(UTF16ToUTF8(text), ascii_text);
314 TYPED_TEST(ClipboardTest, URLTest) {
315 base::string16 url(ASCIIToUTF16("http://www.google.com/"));
318 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
319 clipboard_writer.WriteURL(url);
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 base::string16 text_result;
327 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result);
329 EXPECT_EQ(text_result, url);
331 std::string ascii_text;
332 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text);
333 EXPECT_EQ(UTF16ToUTF8(url), ascii_text);
335 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && \
336 !defined(OS_CHROMEOS)
337 ascii_text.clear();
338 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_SELECTION, &ascii_text);
339 EXPECT_EQ(UTF16ToUTF8(url), ascii_text);
340 #endif
343 static void TestBitmapWrite(Clipboard* clipboard,
344 const gfx::Size& size,
345 const uint32* bitmap_data) {
347 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
348 SkBitmap bitmap;
349 ASSERT_TRUE(bitmap.setInfo(
350 SkImageInfo::MakeN32Premul(size.width(), size.height())));
351 bitmap.setPixels(
352 const_cast<void*>(reinterpret_cast<const void*>(bitmap_data)));
353 scw.WriteImage(bitmap);
356 EXPECT_TRUE(clipboard->IsFormatAvailable(Clipboard::GetBitmapFormatType(),
357 CLIPBOARD_TYPE_COPY_PASTE));
358 const SkBitmap& image = clipboard->ReadImage(CLIPBOARD_TYPE_COPY_PASTE);
359 EXPECT_EQ(size, gfx::Size(image.width(), image.height()));
360 SkAutoLockPixels image_lock(image);
361 for (int j = 0; j < image.height(); ++j) {
362 const uint32* row_address = image.getAddr32(0, j);
363 for (int i = 0; i < image.width(); ++i) {
364 int offset = i + j * image.width();
365 EXPECT_EQ(bitmap_data[offset], row_address[i]) << "i = " << i
366 << ", j = " << j;
371 TYPED_TEST(ClipboardTest, SharedBitmapTest) {
372 const uint32 fake_bitmap_1[] = {
373 0x46061626, 0xf69f5988, 0x793f2937, 0xfa55b986,
374 0x78772152, 0x87692a30, 0x36322a25, 0x4320401b,
375 0x91848c21, 0xc3177b3c, 0x6946155c, 0x64171952,
378 SCOPED_TRACE("first bitmap");
379 TestBitmapWrite(&this->clipboard(), gfx::Size(4, 3), fake_bitmap_1);
382 const uint32 fake_bitmap_2[] = {
383 0x46061626, 0xf69f5988,
384 0x793f2937, 0xfa55b986,
385 0x78772152, 0x87692a30,
386 0x36322a25, 0x4320401b,
387 0x91848c21, 0xc3177b3c,
388 0x6946155c, 0x64171952,
389 0xa6910313, 0x8302323e,
392 SCOPED_TRACE("second bitmap");
393 TestBitmapWrite(&this->clipboard(), gfx::Size(2, 7), fake_bitmap_2);
397 TYPED_TEST(ClipboardTest, DataTest) {
398 const ui::Clipboard::FormatType kFormat =
399 ui::Clipboard::GetFormatType("chromium/x-test-format");
400 std::string payload("test string");
401 Pickle write_pickle;
402 write_pickle.WriteString(payload);
405 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
406 clipboard_writer.WritePickledData(write_pickle, kFormat);
409 ASSERT_TRUE(
410 this->clipboard().IsFormatAvailable(kFormat, CLIPBOARD_TYPE_COPY_PASTE));
411 std::string output;
412 this->clipboard().ReadData(kFormat, &output);
413 ASSERT_FALSE(output.empty());
415 Pickle read_pickle(output.data(), output.size());
416 PickleIterator iter(read_pickle);
417 std::string unpickled_string;
418 ASSERT_TRUE(iter.ReadString(&unpickled_string));
419 EXPECT_EQ(payload, unpickled_string);
422 TYPED_TEST(ClipboardTest, MultipleDataTest) {
423 const ui::Clipboard::FormatType kFormat1 =
424 ui::Clipboard::GetFormatType("chromium/x-test-format1");
425 std::string payload1("test string1");
426 Pickle write_pickle1;
427 write_pickle1.WriteString(payload1);
429 const ui::Clipboard::FormatType kFormat2 =
430 ui::Clipboard::GetFormatType("chromium/x-test-format2");
431 std::string payload2("test string2");
432 Pickle write_pickle2;
433 write_pickle2.WriteString(payload2);
436 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
437 clipboard_writer.WritePickledData(write_pickle1, kFormat1);
438 // overwrite the previous pickle for fun
439 clipboard_writer.WritePickledData(write_pickle2, kFormat2);
442 ASSERT_TRUE(
443 this->clipboard().IsFormatAvailable(kFormat2, CLIPBOARD_TYPE_COPY_PASTE));
445 // Check string 2.
446 std::string output2;
447 this->clipboard().ReadData(kFormat2, &output2);
448 ASSERT_FALSE(output2.empty());
450 Pickle read_pickle2(output2.data(), output2.size());
451 PickleIterator iter2(read_pickle2);
452 std::string unpickled_string2;
453 ASSERT_TRUE(iter2.ReadString(&unpickled_string2));
454 EXPECT_EQ(payload2, unpickled_string2);
457 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
458 clipboard_writer.WritePickledData(write_pickle2, kFormat2);
459 // overwrite the previous pickle for fun
460 clipboard_writer.WritePickledData(write_pickle1, kFormat1);
463 ASSERT_TRUE(
464 this->clipboard().IsFormatAvailable(kFormat1, CLIPBOARD_TYPE_COPY_PASTE));
466 // Check string 1.
467 std::string output1;
468 this->clipboard().ReadData(kFormat1, &output1);
469 ASSERT_FALSE(output1.empty());
471 Pickle read_pickle1(output1.data(), output1.size());
472 PickleIterator iter1(read_pickle1);
473 std::string unpickled_string1;
474 ASSERT_TRUE(iter1.ReadString(&unpickled_string1));
475 EXPECT_EQ(payload1, unpickled_string1);
478 #if !defined(OS_MACOSX) && !defined(OS_ANDROID)
479 TYPED_TEST(ClipboardTest, HyperlinkTest) {
480 const std::string kTitle("The <Example> Company's \"home page\"");
481 const std::string kUrl("http://www.example.com?x=3&lt=3#\"'<>");
482 const base::string16 kExpectedHtml(UTF8ToUTF16(
483 "<a href=\"http://www.example.com?x=3&amp;lt=3#&quot;&#39;&lt;&gt;\">"
484 "The &lt;Example&gt; Company&#39;s &quot;home page&quot;</a>"));
486 std::string url_result;
487 base::string16 html_result;
489 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
490 clipboard_writer.WriteHyperlink(ASCIIToUTF16(kTitle), kUrl);
493 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
494 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
495 uint32 fragment_start;
496 uint32 fragment_end;
497 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &html_result,
498 &url_result, &fragment_start, &fragment_end);
499 EXPECT_EQ(kExpectedHtml,
500 html_result.substr(fragment_end - kExpectedHtml.size(),
501 kExpectedHtml.size()));
503 #endif
505 TYPED_TEST(ClipboardTest, WebSmartPasteTest) {
507 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE);
508 clipboard_writer.WriteWebSmartPaste();
511 EXPECT_TRUE(this->clipboard().IsFormatAvailable(
512 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE));
515 #if defined(OS_WIN) // Windows only tests.
516 void HtmlTestHelper(const std::string& cf_html,
517 const std::string& expected_html) {
518 std::string html;
519 ClipboardUtil::CFHtmlToHtml(cf_html, &html, NULL);
520 EXPECT_EQ(html, expected_html);
523 TYPED_TEST(ClipboardTest, HtmlTest) {
524 // Test converting from CF_HTML format data with <!--StartFragment--> and
525 // <!--EndFragment--> comments, like from MS Word.
526 HtmlTestHelper(
527 "Version:1.0\r\n"
528 "StartHTML:0000000105\r\n"
529 "EndHTML:0000000199\r\n"
530 "StartFragment:0000000123\r\n"
531 "EndFragment:0000000161\r\n"
532 "\r\n"
533 "<html>\r\n"
534 "<body>\r\n"
535 "<!--StartFragment-->\r\n"
536 "\r\n"
537 "<p>Foo</p>\r\n"
538 "\r\n"
539 "<!--EndFragment-->\r\n"
540 "</body>\r\n"
541 "</html>\r\n\r\n",
542 "<p>Foo</p>");
544 // Test converting from CF_HTML format data without <!--StartFragment--> and
545 // <!--EndFragment--> comments, like from OpenOffice Writer.
546 HtmlTestHelper(
547 "Version:1.0\r\n"
548 "StartHTML:0000000105\r\n"
549 "EndHTML:0000000151\r\n"
550 "StartFragment:0000000121\r\n"
551 "EndFragment:0000000131\r\n"
552 "<html>\r\n"
553 "<body>\r\n"
554 "<p>Foo</p>\r\n"
555 "</body>\r\n"
556 "</html>\r\n\r\n",
557 "<p>Foo</p>");
559 #endif // defined(OS_WIN)
561 // Test writing all formats we have simultaneously.
562 TYPED_TEST(ClipboardTest, WriteEverything) {
564 ScopedClipboardWriter writer(CLIPBOARD_TYPE_COPY_PASTE);
565 writer.WriteText(UTF8ToUTF16("foo"));
566 writer.WriteURL(UTF8ToUTF16("foo"));
567 writer.WriteHTML(UTF8ToUTF16("foo"), "bar");
568 writer.WriteBookmark(UTF8ToUTF16("foo"), "bar");
569 writer.WriteHyperlink(ASCIIToUTF16("foo"), "bar");
570 writer.WriteWebSmartPaste();
571 // Left out: WriteFile, WriteFiles, WriteBitmapFromPixels, WritePickledData.
574 // Passes if we don't crash.
577 // TODO(dcheng): Fix this test for Android. It's rather involved, since the
578 // clipboard change listener is posted to the Java message loop, and spinning
579 // that loop from C++ to trigger the callback in the test requires a non-trivial
580 // amount of additional work.
581 #if !defined(OS_ANDROID)
582 // Simple test that the sequence number appears to change when the clipboard is
583 // written to.
584 // TODO(dcheng): Add a version to test CLIPBOARD_TYPE_SELECTION.
585 TYPED_TEST(ClipboardTest, GetSequenceNumber) {
586 const uint64 first_sequence_number =
587 this->clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE);
590 ScopedClipboardWriter writer(CLIPBOARD_TYPE_COPY_PASTE);
591 writer.WriteText(UTF8ToUTF16("World"));
594 // On some platforms, the sequence number is updated by a UI callback so pump
595 // the message loop to make sure we get the notification.
596 base::RunLoop().RunUntilIdle();
598 const uint64 second_sequence_number =
599 this->clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE);
601 EXPECT_NE(first_sequence_number, second_sequence_number);
603 #endif
605 // Test that writing empty parameters doesn't try to dereference an empty data
606 // vector. Not crashing = passing.
607 TYPED_TEST(ClipboardTest, WriteTextEmptyParams) {
608 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
609 scw.WriteText(base::string16());
612 TYPED_TEST(ClipboardTest, WriteURLEmptyParams) {
613 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
614 scw.WriteURL(base::string16());
617 TYPED_TEST(ClipboardTest, WriteHTMLEmptyParams) {
618 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
619 scw.WriteHTML(base::string16(), std::string());
622 TYPED_TEST(ClipboardTest, WriteRTFEmptyParams) {
623 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
624 scw.WriteRTF(std::string());
627 TYPED_TEST(ClipboardTest, WriteBookmarkEmptyParams) {
628 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
629 scw.WriteBookmark(base::string16(), std::string());
632 TYPED_TEST(ClipboardTest, WriteHyperlinkEmptyParams) {
633 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
634 scw.WriteHyperlink(base::string16(), std::string());
637 TYPED_TEST(ClipboardTest, WritePickledData) {
638 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
639 scw.WritePickledData(Pickle(), Clipboard::GetPlainTextFormatType());
642 TYPED_TEST(ClipboardTest, WriteImageEmptyParams) {
643 ScopedClipboardWriter scw(CLIPBOARD_TYPE_COPY_PASTE);
644 scw.WriteImage(SkBitmap());
647 } // namespace ui