| OLD | NEW |
| (Empty) |
| 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 #include "build/build_config.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/pickle.h" | |
| 13 #include "base/run_loop.h" | |
| 14 #include "base/strings/string_util.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 #include "testing/platform_test.h" | |
| 18 #include "third_party/skia/include/core/SkBitmap.h" | |
| 19 #include "third_party/skia/include/core/SkColor.h" | |
| 20 #include "third_party/skia/include/core/SkScalar.h" | |
| 21 #include "third_party/skia/include/core/SkUnPreMultiply.h" | |
| 22 #include "ui/base/clipboard/clipboard.h" | |
| 23 #include "ui/base/clipboard/scoped_clipboard_writer.h" | |
| 24 #include "ui/gfx/size.h" | |
| 25 | |
| 26 #if defined(OS_WIN) | |
| 27 #include "ui/base/clipboard/clipboard_util_win.h" | |
| 28 #endif | |
| 29 | |
| 30 #if defined(OS_ANDROID) | |
| 31 #include "base/android/jni_android.h" | |
| 32 #include "base/android/jni_string.h" | |
| 33 #endif | |
| 34 | |
| 35 #if defined(USE_AURA) | |
| 36 #include "ui/events/platform/platform_event_source.h" | |
| 37 #endif | |
| 38 | |
| 39 using base::ASCIIToUTF16; | |
| 40 using base::UTF8ToUTF16; | |
| 41 using base::UTF16ToUTF8; | |
| 42 | |
| 43 namespace ui { | |
| 44 | |
| 45 class ClipboardTest : public PlatformTest { | |
| 46 public: | |
| 47 #if defined(USE_AURA) | |
| 48 ClipboardTest() : event_source_(ui::PlatformEventSource::CreateDefault()) {} | |
| 49 #else | |
| 50 ClipboardTest() {} | |
| 51 #endif | |
| 52 | |
| 53 virtual ~ClipboardTest() { | |
| 54 ui::Clipboard::DestroyClipboardForCurrentThread(); | |
| 55 } | |
| 56 | |
| 57 static void WriteObjectsToClipboard(ui::Clipboard* clipboard, | |
| 58 const Clipboard::ObjectMap& objects) { | |
| 59 clipboard->WriteObjects(ui::CLIPBOARD_TYPE_COPY_PASTE, objects); | |
| 60 } | |
| 61 | |
| 62 protected: | |
| 63 Clipboard& clipboard() { return *ui::Clipboard::GetForCurrentThread(); } | |
| 64 | |
| 65 void WriteObjectsToClipboard(const Clipboard::ObjectMap& objects) { | |
| 66 WriteObjectsToClipboard(&clipboard(), objects); | |
| 67 } | |
| 68 | |
| 69 private: | |
| 70 base::MessageLoopForUI message_loop_; | |
| 71 #if defined(USE_AURA) | |
| 72 scoped_ptr<PlatformEventSource> event_source_; | |
| 73 #endif | |
| 74 }; | |
| 75 | |
| 76 namespace { | |
| 77 | |
| 78 bool MarkupMatches(const base::string16& expected_markup, | |
| 79 const base::string16& actual_markup) { | |
| 80 return actual_markup.find(expected_markup) != base::string16::npos; | |
| 81 } | |
| 82 | |
| 83 } // namespace | |
| 84 | |
| 85 TEST_F(ClipboardTest, ClearTest) { | |
| 86 { | |
| 87 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 88 clipboard_writer.WriteText(ASCIIToUTF16("clear me")); | |
| 89 } | |
| 90 | |
| 91 clipboard().Clear(CLIPBOARD_TYPE_COPY_PASTE); | |
| 92 | |
| 93 EXPECT_FALSE(clipboard().IsFormatAvailable( | |
| 94 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 95 EXPECT_FALSE(clipboard().IsFormatAvailable( | |
| 96 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 97 } | |
| 98 | |
| 99 TEST_F(ClipboardTest, TextTest) { | |
| 100 base::string16 text(ASCIIToUTF16("This is a base::string16!#$")), text_result; | |
| 101 std::string ascii_text; | |
| 102 | |
| 103 { | |
| 104 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 105 clipboard_writer.WriteText(text); | |
| 106 } | |
| 107 | |
| 108 EXPECT_TRUE(clipboard().IsFormatAvailable( | |
| 109 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 110 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetPlainTextFormatType(), | |
| 111 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 112 clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); | |
| 113 | |
| 114 EXPECT_EQ(text, text_result); | |
| 115 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text); | |
| 116 EXPECT_EQ(UTF16ToUTF8(text), ascii_text); | |
| 117 } | |
| 118 | |
| 119 TEST_F(ClipboardTest, HTMLTest) { | |
| 120 base::string16 markup(ASCIIToUTF16("<string>Hi!</string>")), markup_result; | |
| 121 base::string16 plain(ASCIIToUTF16("Hi!")), plain_result; | |
| 122 std::string url("http://www.example.com/"), url_result; | |
| 123 | |
| 124 { | |
| 125 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 126 clipboard_writer.WriteText(plain); | |
| 127 clipboard_writer.WriteHTML(markup, url); | |
| 128 } | |
| 129 | |
| 130 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), | |
| 131 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 132 uint32 ignored; | |
| 133 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result, | |
| 134 &ignored, &ignored); | |
| 135 EXPECT_PRED2(MarkupMatches, markup, markup_result); | |
| 136 #if defined(OS_WIN) | |
| 137 // TODO(playmobil): It's not clear that non windows clipboards need to support | |
| 138 // this. | |
| 139 EXPECT_EQ(url, url_result); | |
| 140 #endif // defined(OS_WIN) | |
| 141 } | |
| 142 | |
| 143 TEST_F(ClipboardTest, RTFTest) { | |
| 144 std::string rtf = | |
| 145 "{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\n" | |
| 146 "This is some {\\b bold} text.\\par\n" | |
| 147 "}"; | |
| 148 | |
| 149 { | |
| 150 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 151 clipboard_writer.WriteRTF(rtf); | |
| 152 } | |
| 153 | |
| 154 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetRtfFormatType(), | |
| 155 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 156 std::string result; | |
| 157 clipboard().ReadRTF(CLIPBOARD_TYPE_COPY_PASTE, &result); | |
| 158 EXPECT_EQ(rtf, result); | |
| 159 } | |
| 160 | |
| 161 // TODO(dnicoara) Enable test once Ozone implements clipboard support: | |
| 162 // crbug.com/361707 | |
| 163 #if defined(OS_LINUX) && !defined(OS_CHROMEOS) && !defined(USE_OZONE) | |
| 164 TEST_F(ClipboardTest, MultipleBufferTest) { | |
| 165 base::string16 text(ASCIIToUTF16("Standard")), text_result; | |
| 166 base::string16 markup(ASCIIToUTF16("<string>Selection</string>")); | |
| 167 std::string url("http://www.example.com/"), url_result; | |
| 168 | |
| 169 { | |
| 170 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 171 clipboard_writer.WriteText(text); | |
| 172 } | |
| 173 | |
| 174 { | |
| 175 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_SELECTION); | |
| 176 clipboard_writer.WriteHTML(markup, url); | |
| 177 } | |
| 178 | |
| 179 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetPlainTextFormatType(), | |
| 180 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 181 EXPECT_FALSE(clipboard().IsFormatAvailable( | |
| 182 Clipboard::GetPlainTextFormatType(), | |
| 183 CLIPBOARD_TYPE_SELECTION)); | |
| 184 | |
| 185 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), | |
| 186 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 187 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), | |
| 188 CLIPBOARD_TYPE_SELECTION)); | |
| 189 | |
| 190 clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); | |
| 191 EXPECT_EQ(text, text_result); | |
| 192 | |
| 193 uint32 ignored; | |
| 194 base::string16 markup_result; | |
| 195 clipboard().ReadHTML(CLIPBOARD_TYPE_SELECTION, | |
| 196 &markup_result, | |
| 197 &url_result, | |
| 198 &ignored, | |
| 199 &ignored); | |
| 200 EXPECT_PRED2(MarkupMatches, markup, markup_result); | |
| 201 } | |
| 202 #endif | |
| 203 | |
| 204 TEST_F(ClipboardTest, TrickyHTMLTest) { | |
| 205 base::string16 markup(ASCIIToUTF16("<em>Bye!<!--EndFragment --></em>")), | |
| 206 markup_result; | |
| 207 std::string url, url_result; | |
| 208 base::string16 plain(ASCIIToUTF16("Bye!")), plain_result; | |
| 209 | |
| 210 { | |
| 211 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 212 clipboard_writer.WriteText(plain); | |
| 213 clipboard_writer.WriteHTML(markup, url); | |
| 214 } | |
| 215 | |
| 216 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), | |
| 217 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 218 uint32 ignored; | |
| 219 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result, | |
| 220 &ignored, &ignored); | |
| 221 EXPECT_PRED2(MarkupMatches, markup, markup_result); | |
| 222 #if defined(OS_WIN) | |
| 223 // TODO(playmobil): It's not clear that non windows clipboards need to support | |
| 224 // this. | |
| 225 EXPECT_EQ(url, url_result); | |
| 226 #endif // defined(OS_WIN) | |
| 227 } | |
| 228 | |
| 229 #if defined(OS_WIN) | |
| 230 TEST_F(ClipboardTest, UniodeHTMLTest) { | |
| 231 base::string16 markup(UTF8ToUTF16("<div>A \xc3\xb8 \xe6\xb0\xb4</div>")), | |
| 232 markup_result; | |
| 233 std::string url, url_result; | |
| 234 | |
| 235 { | |
| 236 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 237 clipboard_writer.WriteHTML(markup, url); | |
| 238 } | |
| 239 | |
| 240 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), | |
| 241 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 242 uint32 fragment_start; | |
| 243 uint32 fragment_end; | |
| 244 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result, | |
| 245 &fragment_start, &fragment_end); | |
| 246 EXPECT_PRED2(MarkupMatches, markup, markup_result); | |
| 247 EXPECT_EQ(url, url_result); | |
| 248 // Make sure that fragment indices were adjusted when converting. | |
| 249 EXPECT_EQ(36, fragment_start); | |
| 250 EXPECT_EQ(52, fragment_end); | |
| 251 } | |
| 252 #endif // defined(OS_WIN) | |
| 253 | |
| 254 // TODO(estade): Port the following test (decide what target we use for urls) | |
| 255 #if !defined(OS_POSIX) || defined(OS_MACOSX) | |
| 256 TEST_F(ClipboardTest, BookmarkTest) { | |
| 257 base::string16 title(ASCIIToUTF16("The Example Company")), title_result; | |
| 258 std::string url("http://www.example.com/"), url_result; | |
| 259 | |
| 260 { | |
| 261 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 262 clipboard_writer.WriteBookmark(title, url); | |
| 263 } | |
| 264 | |
| 265 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetUrlWFormatType(), | |
| 266 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 267 clipboard().ReadBookmark(&title_result, &url_result); | |
| 268 EXPECT_EQ(title, title_result); | |
| 269 EXPECT_EQ(url, url_result); | |
| 270 } | |
| 271 #endif // defined(OS_WIN) | |
| 272 | |
| 273 TEST_F(ClipboardTest, MultiFormatTest) { | |
| 274 base::string16 text(ASCIIToUTF16("Hi!")), text_result; | |
| 275 base::string16 markup(ASCIIToUTF16("<strong>Hi!</string>")), markup_result; | |
| 276 std::string url("http://www.example.com/"), url_result; | |
| 277 std::string ascii_text; | |
| 278 | |
| 279 { | |
| 280 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 281 clipboard_writer.WriteHTML(markup, url); | |
| 282 clipboard_writer.WriteText(text); | |
| 283 } | |
| 284 | |
| 285 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), | |
| 286 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 287 EXPECT_TRUE(clipboard().IsFormatAvailable( | |
| 288 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 289 EXPECT_TRUE(clipboard().IsFormatAvailable( | |
| 290 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 291 uint32 ignored; | |
| 292 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, &url_result, | |
| 293 &ignored, &ignored); | |
| 294 EXPECT_PRED2(MarkupMatches, markup, markup_result); | |
| 295 #if defined(OS_WIN) | |
| 296 // TODO(playmobil): It's not clear that non windows clipboards need to support | |
| 297 // this. | |
| 298 EXPECT_EQ(url, url_result); | |
| 299 #endif // defined(OS_WIN) | |
| 300 clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); | |
| 301 EXPECT_EQ(text, text_result); | |
| 302 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text); | |
| 303 EXPECT_EQ(UTF16ToUTF8(text), ascii_text); | |
| 304 } | |
| 305 | |
| 306 TEST_F(ClipboardTest, URLTest) { | |
| 307 base::string16 url(ASCIIToUTF16("http://www.google.com/")); | |
| 308 | |
| 309 { | |
| 310 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 311 clipboard_writer.WriteURL(url); | |
| 312 } | |
| 313 | |
| 314 EXPECT_TRUE(clipboard().IsFormatAvailable( | |
| 315 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 316 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetPlainTextFormatType(), | |
| 317 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 318 base::string16 text_result; | |
| 319 clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); | |
| 320 | |
| 321 EXPECT_EQ(text_result, url); | |
| 322 | |
| 323 std::string ascii_text; | |
| 324 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text); | |
| 325 EXPECT_EQ(UTF16ToUTF8(url), ascii_text); | |
| 326 | |
| 327 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | |
| 328 ascii_text.clear(); | |
| 329 clipboard().ReadAsciiText(CLIPBOARD_TYPE_SELECTION, &ascii_text); | |
| 330 EXPECT_EQ(UTF16ToUTF8(url), ascii_text); | |
| 331 #endif | |
| 332 } | |
| 333 | |
| 334 // TODO(dcheng): The tests for copying to the clipboard also test IPC | |
| 335 // interaction... consider moving them to a different layer so we can | |
| 336 // consolidate the validation logic. | |
| 337 // Note that |bitmap_data| is not premultiplied! | |
| 338 static void TestBitmapWrite(Clipboard* clipboard, | |
| 339 const uint32* bitmap_data, | |
| 340 size_t bitmap_data_size, | |
| 341 const gfx::Size& size) { | |
| 342 // Create shared memory region. | |
| 343 base::SharedMemory shared_buf; | |
| 344 ASSERT_TRUE(shared_buf.CreateAndMapAnonymous(bitmap_data_size)); | |
| 345 memcpy(shared_buf.memory(), bitmap_data, bitmap_data_size); | |
| 346 // CBF_SMBITMAP expects premultiplied bitmap data so do that now. | |
| 347 uint32* pixel_buffer = static_cast<uint32*>(shared_buf.memory()); | |
| 348 for (int j = 0; j < size.height(); ++j) { | |
| 349 for (int i = 0; i < size.width(); ++i) { | |
| 350 uint32& pixel = pixel_buffer[i + j * size.width()]; | |
| 351 pixel = SkPreMultiplyColor(pixel); | |
| 352 } | |
| 353 } | |
| 354 base::SharedMemoryHandle handle_to_share; | |
| 355 base::ProcessHandle current_process = base::kNullProcessHandle; | |
| 356 #if defined(OS_WIN) | |
| 357 current_process = GetCurrentProcess(); | |
| 358 #endif | |
| 359 shared_buf.ShareToProcess(current_process, &handle_to_share); | |
| 360 ASSERT_TRUE(shared_buf.Unmap()); | |
| 361 | |
| 362 // Setup data for clipboard(). | |
| 363 Clipboard::ObjectMapParam placeholder_param; | |
| 364 Clipboard::ObjectMapParam size_param; | |
| 365 const char* size_data = reinterpret_cast<const char*>(&size); | |
| 366 for (size_t i = 0; i < sizeof(size); ++i) | |
| 367 size_param.push_back(size_data[i]); | |
| 368 | |
| 369 Clipboard::ObjectMapParams params; | |
| 370 params.push_back(placeholder_param); | |
| 371 params.push_back(size_param); | |
| 372 | |
| 373 Clipboard::ObjectMap objects; | |
| 374 objects[Clipboard::CBF_SMBITMAP] = params; | |
| 375 ASSERT_TRUE(Clipboard::ReplaceSharedMemHandle( | |
| 376 &objects, handle_to_share, current_process)); | |
| 377 | |
| 378 ClipboardTest::WriteObjectsToClipboard(clipboard, objects); | |
| 379 | |
| 380 EXPECT_TRUE(clipboard->IsFormatAvailable(Clipboard::GetBitmapFormatType(), | |
| 381 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 382 const SkBitmap& image = clipboard->ReadImage(CLIPBOARD_TYPE_COPY_PASTE); | |
| 383 EXPECT_EQ(size, gfx::Size(image.width(), image.height())); | |
| 384 SkAutoLockPixels image_lock(image); | |
| 385 for (int j = 0; j < image.height(); ++j) { | |
| 386 const uint32* row_address = image.getAddr32(0, j); | |
| 387 for (int i = 0; i < image.width(); ++i) { | |
| 388 int offset = i + j * image.width(); | |
| 389 uint32 pixel = SkPreMultiplyColor(bitmap_data[offset]); | |
| 390 EXPECT_EQ(pixel, row_address[i]) | |
| 391 << "i = " << i << ", j = " << j; | |
| 392 } | |
| 393 } | |
| 394 } | |
| 395 | |
| 396 TEST_F(ClipboardTest, SharedBitmapTest) { | |
| 397 const uint32 fake_bitmap_1[] = { | |
| 398 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89, | |
| 399 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568, | |
| 400 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1, | |
| 401 }; | |
| 402 { | |
| 403 SCOPED_TRACE("first bitmap"); | |
| 404 TestBitmapWrite( | |
| 405 &clipboard(), fake_bitmap_1, sizeof(fake_bitmap_1), gfx::Size(4, 3)); | |
| 406 } | |
| 407 | |
| 408 const uint32 fake_bitmap_2[] = { | |
| 409 0x46155189, 0xF6A55C8D, | |
| 410 0x79845674, 0xFA57BD89, | |
| 411 0x78FD46AE, 0x87C64F5A, | |
| 412 0x36EDC5AF, 0x4378F568, | |
| 413 0x91E9F63A, 0xC31EA14F, | |
| 414 0x69AB32DF, 0x643A3FD1, | |
| 415 0xA6DF041D, 0x83046278, | |
| 416 }; | |
| 417 { | |
| 418 SCOPED_TRACE("second bitmap"); | |
| 419 TestBitmapWrite( | |
| 420 &clipboard(), fake_bitmap_2, sizeof(fake_bitmap_2), gfx::Size(2, 7)); | |
| 421 } | |
| 422 } | |
| 423 | |
| 424 namespace { | |
| 425 // A size class that just happens to have the same layout as gfx::Size! | |
| 426 struct UnsafeSize { | |
| 427 int width; | |
| 428 int height; | |
| 429 }; | |
| 430 COMPILE_ASSERT(sizeof(UnsafeSize) == sizeof(gfx::Size), | |
| 431 UnsafeSize_must_be_same_size_as_gfx_Size); | |
| 432 } // namespace | |
| 433 | |
| 434 TEST_F(ClipboardTest, SharedBitmapWithTwoNegativeSizes) { | |
| 435 Clipboard::ObjectMapParam placeholder_param; | |
| 436 void* crash_me = reinterpret_cast<void*>(57); | |
| 437 placeholder_param.resize(sizeof(crash_me)); | |
| 438 memcpy(&placeholder_param.front(), &crash_me, sizeof(crash_me)); | |
| 439 | |
| 440 Clipboard::ObjectMapParam size_param; | |
| 441 UnsafeSize size = {-100, -100}; | |
| 442 size_param.resize(sizeof(size)); | |
| 443 memcpy(&size_param.front(), &size, sizeof(size)); | |
| 444 | |
| 445 Clipboard::ObjectMapParams params; | |
| 446 params.push_back(placeholder_param); | |
| 447 params.push_back(size_param); | |
| 448 | |
| 449 Clipboard::ObjectMap objects; | |
| 450 objects[Clipboard::CBF_SMBITMAP] = params; | |
| 451 | |
| 452 WriteObjectsToClipboard(objects); | |
| 453 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetBitmapFormatType(), | |
| 454 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 455 } | |
| 456 | |
| 457 TEST_F(ClipboardTest, SharedBitmapWithOneNegativeSize) { | |
| 458 Clipboard::ObjectMapParam placeholder_param; | |
| 459 void* crash_me = reinterpret_cast<void*>(57); | |
| 460 placeholder_param.resize(sizeof(crash_me)); | |
| 461 memcpy(&placeholder_param.front(), &crash_me, sizeof(crash_me)); | |
| 462 | |
| 463 Clipboard::ObjectMapParam size_param; | |
| 464 UnsafeSize size = {-100, 100}; | |
| 465 size_param.resize(sizeof(size)); | |
| 466 memcpy(&size_param.front(), &size, sizeof(size)); | |
| 467 | |
| 468 Clipboard::ObjectMapParams params; | |
| 469 params.push_back(placeholder_param); | |
| 470 params.push_back(size_param); | |
| 471 | |
| 472 Clipboard::ObjectMap objects; | |
| 473 objects[Clipboard::CBF_SMBITMAP] = params; | |
| 474 | |
| 475 WriteObjectsToClipboard(objects); | |
| 476 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetBitmapFormatType(), | |
| 477 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 478 } | |
| 479 | |
| 480 TEST_F(ClipboardTest, BitmapWithSuperSize) { | |
| 481 Clipboard::ObjectMapParam placeholder_param; | |
| 482 void* crash_me = reinterpret_cast<void*>(57); | |
| 483 placeholder_param.resize(sizeof(crash_me)); | |
| 484 memcpy(&placeholder_param.front(), &crash_me, sizeof(crash_me)); | |
| 485 | |
| 486 Clipboard::ObjectMapParam size_param; | |
| 487 // Width just big enough that bytes per row won't fit in a 32-bit | |
| 488 // representation. | |
| 489 gfx::Size size(0x20000000, 1); | |
| 490 size_param.resize(sizeof(size)); | |
| 491 memcpy(&size_param.front(), &size, sizeof(size)); | |
| 492 | |
| 493 Clipboard::ObjectMapParams params; | |
| 494 params.push_back(placeholder_param); | |
| 495 params.push_back(size_param); | |
| 496 | |
| 497 Clipboard::ObjectMap objects; | |
| 498 objects[Clipboard::CBF_SMBITMAP] = params; | |
| 499 | |
| 500 WriteObjectsToClipboard(objects); | |
| 501 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetBitmapFormatType(), | |
| 502 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 503 } | |
| 504 | |
| 505 TEST_F(ClipboardTest, BitmapWithSuperSize2) { | |
| 506 Clipboard::ObjectMapParam placeholder_param; | |
| 507 void* crash_me = reinterpret_cast<void*>(57); | |
| 508 placeholder_param.resize(sizeof(crash_me)); | |
| 509 memcpy(&placeholder_param.front(), &crash_me, sizeof(crash_me)); | |
| 510 | |
| 511 Clipboard::ObjectMapParam size_param; | |
| 512 // Width and height large enough that SkBitmap::getSize() will be truncated. | |
| 513 gfx::Size size(0x0fffffff, 0x0fffffff); | |
| 514 size_param.resize(sizeof(size)); | |
| 515 memcpy(&size_param.front(), &size, sizeof(size)); | |
| 516 | |
| 517 Clipboard::ObjectMapParams params; | |
| 518 params.push_back(placeholder_param); | |
| 519 params.push_back(size_param); | |
| 520 | |
| 521 Clipboard::ObjectMap objects; | |
| 522 objects[Clipboard::CBF_SMBITMAP] = params; | |
| 523 | |
| 524 WriteObjectsToClipboard(objects); | |
| 525 EXPECT_FALSE(clipboard().IsFormatAvailable(Clipboard::GetBitmapFormatType(), | |
| 526 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 527 } | |
| 528 | |
| 529 TEST_F(ClipboardTest, DataTest) { | |
| 530 const ui::Clipboard::FormatType kFormat = | |
| 531 ui::Clipboard::GetFormatType("chromium/x-test-format"); | |
| 532 std::string payload("test string"); | |
| 533 Pickle write_pickle; | |
| 534 write_pickle.WriteString(payload); | |
| 535 | |
| 536 { | |
| 537 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 538 clipboard_writer.WritePickledData(write_pickle, kFormat); | |
| 539 } | |
| 540 | |
| 541 ASSERT_TRUE(clipboard().IsFormatAvailable( | |
| 542 kFormat, CLIPBOARD_TYPE_COPY_PASTE)); | |
| 543 std::string output; | |
| 544 clipboard().ReadData(kFormat, &output); | |
| 545 ASSERT_FALSE(output.empty()); | |
| 546 | |
| 547 Pickle read_pickle(output.data(), output.size()); | |
| 548 PickleIterator iter(read_pickle); | |
| 549 std::string unpickled_string; | |
| 550 ASSERT_TRUE(read_pickle.ReadString(&iter, &unpickled_string)); | |
| 551 EXPECT_EQ(payload, unpickled_string); | |
| 552 } | |
| 553 | |
| 554 TEST_F(ClipboardTest, MultipleDataTest) { | |
| 555 const ui::Clipboard::FormatType kFormat1 = | |
| 556 ui::Clipboard::GetFormatType("chromium/x-test-format1"); | |
| 557 std::string payload1("test string1"); | |
| 558 Pickle write_pickle1; | |
| 559 write_pickle1.WriteString(payload1); | |
| 560 | |
| 561 const ui::Clipboard::FormatType kFormat2 = | |
| 562 ui::Clipboard::GetFormatType("chromium/x-test-format2"); | |
| 563 std::string payload2("test string2"); | |
| 564 Pickle write_pickle2; | |
| 565 write_pickle2.WriteString(payload2); | |
| 566 | |
| 567 { | |
| 568 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 569 clipboard_writer.WritePickledData(write_pickle1, kFormat1); | |
| 570 // overwrite the previous pickle for fun | |
| 571 clipboard_writer.WritePickledData(write_pickle2, kFormat2); | |
| 572 } | |
| 573 | |
| 574 ASSERT_TRUE(clipboard().IsFormatAvailable( | |
| 575 kFormat2, CLIPBOARD_TYPE_COPY_PASTE)); | |
| 576 | |
| 577 // Check string 2. | |
| 578 std::string output2; | |
| 579 clipboard().ReadData(kFormat2, &output2); | |
| 580 ASSERT_FALSE(output2.empty()); | |
| 581 | |
| 582 Pickle read_pickle2(output2.data(), output2.size()); | |
| 583 PickleIterator iter2(read_pickle2); | |
| 584 std::string unpickled_string2; | |
| 585 ASSERT_TRUE(read_pickle2.ReadString(&iter2, &unpickled_string2)); | |
| 586 EXPECT_EQ(payload2, unpickled_string2); | |
| 587 | |
| 588 { | |
| 589 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 590 clipboard_writer.WritePickledData(write_pickle2, kFormat2); | |
| 591 // overwrite the previous pickle for fun | |
| 592 clipboard_writer.WritePickledData(write_pickle1, kFormat1); | |
| 593 } | |
| 594 | |
| 595 ASSERT_TRUE(clipboard().IsFormatAvailable( | |
| 596 kFormat1, CLIPBOARD_TYPE_COPY_PASTE)); | |
| 597 | |
| 598 // Check string 1. | |
| 599 std::string output1; | |
| 600 clipboard().ReadData(kFormat1, &output1); | |
| 601 ASSERT_FALSE(output1.empty()); | |
| 602 | |
| 603 Pickle read_pickle1(output1.data(), output1.size()); | |
| 604 PickleIterator iter1(read_pickle1); | |
| 605 std::string unpickled_string1; | |
| 606 ASSERT_TRUE(read_pickle1.ReadString(&iter1, &unpickled_string1)); | |
| 607 EXPECT_EQ(payload1, unpickled_string1); | |
| 608 } | |
| 609 | |
| 610 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | |
| 611 TEST_F(ClipboardTest, HyperlinkTest) { | |
| 612 const std::string kTitle("The <Example> Company's \"home page\""); | |
| 613 const std::string kUrl("http://www.example.com?x=3<=3#\"'<>"); | |
| 614 const std::string kExpectedHtml( | |
| 615 "<a href=\"http://www.example.com?x=3&lt=3#"'<>\">" | |
| 616 "The <Example> Company's "home page"</a>"); | |
| 617 | |
| 618 std::string url_result; | |
| 619 base::string16 html_result; | |
| 620 { | |
| 621 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 622 clipboard_writer.WriteHyperlink(ASCIIToUTF16(kTitle), kUrl); | |
| 623 } | |
| 624 | |
| 625 EXPECT_TRUE(clipboard().IsFormatAvailable(Clipboard::GetHtmlFormatType(), | |
| 626 CLIPBOARD_TYPE_COPY_PASTE)); | |
| 627 uint32 ignored; | |
| 628 clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &html_result, &url_result, | |
| 629 &ignored, &ignored); | |
| 630 EXPECT_PRED2(MarkupMatches, ASCIIToUTF16(kExpectedHtml), html_result); | |
| 631 } | |
| 632 #endif | |
| 633 | |
| 634 #if defined(OS_WIN) // Windows only tests. | |
| 635 TEST_F(ClipboardTest, WebSmartPasteTest) { | |
| 636 { | |
| 637 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 638 clipboard_writer.WriteWebSmartPaste(); | |
| 639 } | |
| 640 | |
| 641 EXPECT_TRUE(clipboard().IsFormatAvailable( | |
| 642 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 643 } | |
| 644 | |
| 645 void HtmlTestHelper(const std::string& cf_html, | |
| 646 const std::string& expected_html) { | |
| 647 std::string html; | |
| 648 ClipboardUtil::CFHtmlToHtml(cf_html, &html, NULL); | |
| 649 EXPECT_EQ(html, expected_html); | |
| 650 } | |
| 651 | |
| 652 TEST_F(ClipboardTest, HtmlTest) { | |
| 653 // Test converting from CF_HTML format data with <!--StartFragment--> and | |
| 654 // <!--EndFragment--> comments, like from MS Word. | |
| 655 HtmlTestHelper("Version:1.0\r\n" | |
| 656 "StartHTML:0000000105\r\n" | |
| 657 "EndHTML:0000000199\r\n" | |
| 658 "StartFragment:0000000123\r\n" | |
| 659 "EndFragment:0000000161\r\n" | |
| 660 "\r\n" | |
| 661 "<html>\r\n" | |
| 662 "<body>\r\n" | |
| 663 "<!--StartFragment-->\r\n" | |
| 664 "\r\n" | |
| 665 "<p>Foo</p>\r\n" | |
| 666 "\r\n" | |
| 667 "<!--EndFragment-->\r\n" | |
| 668 "</body>\r\n" | |
| 669 "</html>\r\n\r\n", | |
| 670 "<p>Foo</p>"); | |
| 671 | |
| 672 // Test converting from CF_HTML format data without <!--StartFragment--> and | |
| 673 // <!--EndFragment--> comments, like from OpenOffice Writer. | |
| 674 HtmlTestHelper("Version:1.0\r\n" | |
| 675 "StartHTML:0000000105\r\n" | |
| 676 "EndHTML:0000000151\r\n" | |
| 677 "StartFragment:0000000121\r\n" | |
| 678 "EndFragment:0000000131\r\n" | |
| 679 "<html>\r\n" | |
| 680 "<body>\r\n" | |
| 681 "<p>Foo</p>\r\n" | |
| 682 "</body>\r\n" | |
| 683 "</html>\r\n\r\n", | |
| 684 "<p>Foo</p>"); | |
| 685 } | |
| 686 #endif // defined(OS_WIN) | |
| 687 | |
| 688 // Test writing all formats we have simultaneously. | |
| 689 TEST_F(ClipboardTest, WriteEverything) { | |
| 690 { | |
| 691 ScopedClipboardWriter writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 692 writer.WriteText(UTF8ToUTF16("foo")); | |
| 693 writer.WriteURL(UTF8ToUTF16("foo")); | |
| 694 writer.WriteHTML(UTF8ToUTF16("foo"), "bar"); | |
| 695 writer.WriteBookmark(UTF8ToUTF16("foo"), "bar"); | |
| 696 writer.WriteHyperlink(ASCIIToUTF16("foo"), "bar"); | |
| 697 writer.WriteWebSmartPaste(); | |
| 698 // Left out: WriteFile, WriteFiles, WriteBitmapFromPixels, WritePickledData. | |
| 699 } | |
| 700 | |
| 701 // Passes if we don't crash. | |
| 702 } | |
| 703 | |
| 704 // TODO(dcheng): Fix this test for Android. It's rather involved, since the | |
| 705 // clipboard change listener is posted to the Java message loop, and spinning | |
| 706 // that loop from C++ to trigger the callback in the test requires a non-trivial | |
| 707 // amount of additional work. | |
| 708 #if !defined(OS_ANDROID) | |
| 709 // Simple test that the sequence number appears to change when the clipboard is | |
| 710 // written to. | |
| 711 // TODO(dcheng): Add a version to test CLIPBOARD_TYPE_SELECTION. | |
| 712 TEST_F(ClipboardTest, GetSequenceNumber) { | |
| 713 const uint64 first_sequence_number = | |
| 714 clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE); | |
| 715 | |
| 716 { | |
| 717 ScopedClipboardWriter writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 718 writer.WriteText(UTF8ToUTF16("World")); | |
| 719 } | |
| 720 | |
| 721 // On some platforms, the sequence number is updated by a UI callback so pump | |
| 722 // the message loop to make sure we get the notification. | |
| 723 base::RunLoop().RunUntilIdle(); | |
| 724 | |
| 725 const uint64 second_sequence_number = | |
| 726 clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE); | |
| 727 | |
| 728 EXPECT_NE(first_sequence_number, second_sequence_number); | |
| 729 } | |
| 730 #endif | |
| 731 | |
| 732 #if defined(OS_ANDROID) | |
| 733 | |
| 734 // Test that if another application writes some text to the pasteboard the | |
| 735 // clipboard properly invalidates other types. | |
| 736 TEST_F(ClipboardTest, InternalClipboardInvalidation) { | |
| 737 // Write a Webkit smart paste tag to our clipboard. | |
| 738 { | |
| 739 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 740 clipboard_writer.WriteWebSmartPaste(); | |
| 741 } | |
| 742 EXPECT_TRUE(clipboard().IsFormatAvailable( | |
| 743 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 744 | |
| 745 // | |
| 746 // Simulate that another application copied something in the Clipboard | |
| 747 // | |
| 748 std::string new_value("Some text copied by some other app"); | |
| 749 using base::android::ConvertUTF8ToJavaString; | |
| 750 using base::android::MethodID; | |
| 751 using base::android::ScopedJavaLocalRef; | |
| 752 | |
| 753 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 754 ASSERT_TRUE(env); | |
| 755 | |
| 756 jobject context = base::android::GetApplicationContext(); | |
| 757 ASSERT_TRUE(context); | |
| 758 | |
| 759 ScopedJavaLocalRef<jclass> context_class = | |
| 760 base::android::GetClass(env, "android/content/Context"); | |
| 761 | |
| 762 jmethodID get_system_service = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
| 763 env, context_class.obj(), "getSystemService", | |
| 764 "(Ljava/lang/String;)Ljava/lang/Object;"); | |
| 765 | |
| 766 // Retrieve the system service. | |
| 767 ScopedJavaLocalRef<jstring> service_name = ConvertUTF8ToJavaString( | |
| 768 env, "clipboard"); | |
| 769 ScopedJavaLocalRef<jobject> clipboard_manager( | |
| 770 env, env->CallObjectMethod( | |
| 771 context, get_system_service, service_name.obj())); | |
| 772 ASSERT_TRUE(clipboard_manager.obj() && !base::android::ClearException(env)); | |
| 773 | |
| 774 ScopedJavaLocalRef<jclass> clipboard_class = | |
| 775 base::android::GetClass(env, "android/text/ClipboardManager"); | |
| 776 jmethodID set_text = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
| 777 env, clipboard_class.obj(), "setText", "(Ljava/lang/CharSequence;)V"); | |
| 778 ScopedJavaLocalRef<jstring> new_value_string = ConvertUTF8ToJavaString( | |
| 779 env, new_value.c_str()); | |
| 780 | |
| 781 // Will need to call toString as CharSequence is not always a String. | |
| 782 env->CallVoidMethod(clipboard_manager.obj(), | |
| 783 set_text, | |
| 784 new_value_string.obj()); | |
| 785 | |
| 786 // The WebKit smart paste tag should now be gone. | |
| 787 EXPECT_FALSE(clipboard().IsFormatAvailable( | |
| 788 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 789 | |
| 790 // Make sure some text is available | |
| 791 EXPECT_TRUE(clipboard().IsFormatAvailable( | |
| 792 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 793 | |
| 794 // Make sure the text is what we inserted while simulating the other app | |
| 795 std::string contents; | |
| 796 clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &contents); | |
| 797 EXPECT_EQ(contents, new_value); | |
| 798 } | |
| 799 #endif | |
| 800 } // namespace ui | |
| OLD | NEW |