| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 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. |
| 4 | 14 |
| 5 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 6 | 16 |
| 7 #include <string> | 17 #include <string> |
| 8 | 18 |
| 9 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 10 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/message_loop/message_loop.h" | 21 #include "base/message_loop/message_loop.h" |
| 12 #include "base/pickle.h" | 22 #include "base/pickle.h" |
| 13 #include "base/run_loop.h" | 23 #include "base/run_loop.h" |
| 14 #include "base/strings/string_util.h" | 24 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "testing/platform_test.h" | 27 #include "testing/platform_test.h" |
| 18 #include "third_party/skia/include/core/SkBitmap.h" | 28 #include "third_party/skia/include/core/SkBitmap.h" |
| 19 #include "third_party/skia/include/core/SkColor.h" | 29 #include "third_party/skia/include/core/SkColor.h" |
| 20 #include "third_party/skia/include/core/SkScalar.h" | 30 #include "third_party/skia/include/core/SkScalar.h" |
| 21 #include "third_party/skia/include/core/SkUnPreMultiply.h" | 31 #include "third_party/skia/include/core/SkUnPreMultiply.h" |
| 22 #include "ui/base/clipboard/clipboard.h" | 32 #include "ui/base/clipboard/clipboard.h" |
| 23 #include "ui/base/clipboard/scoped_clipboard_writer.h" | 33 #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 34 #include "ui/base/test/test_clipboard.h" |
| 24 #include "ui/gfx/size.h" | 35 #include "ui/gfx/size.h" |
| 25 | 36 |
| 26 #if defined(OS_WIN) | 37 #if defined(OS_WIN) |
| 27 #include "ui/base/clipboard/clipboard_util_win.h" | 38 #include "ui/base/clipboard/clipboard_util_win.h" |
| 28 #endif | 39 #endif |
| 29 | 40 |
| 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) | 41 #if defined(USE_AURA) |
| 36 #include "ui/events/platform/platform_event_source.h" | 42 #include "ui/events/platform/platform_event_source.h" |
| 37 #endif | 43 #endif |
| 38 | 44 |
| 39 using base::ASCIIToUTF16; | 45 using base::ASCIIToUTF16; |
| 40 using base::UTF8ToUTF16; | 46 using base::UTF8ToUTF16; |
| 41 using base::UTF16ToUTF8; | 47 using base::UTF16ToUTF8; |
| 42 | 48 |
| 43 namespace ui { | 49 namespace ui { |
| 44 | 50 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 69 | 75 |
| 70 private: | 76 private: |
| 71 base::MessageLoopForUI message_loop_; | 77 base::MessageLoopForUI message_loop_; |
| 72 #if defined(USE_AURA) | 78 #if defined(USE_AURA) |
| 73 scoped_ptr<PlatformEventSource> event_source_; | 79 scoped_ptr<PlatformEventSource> event_source_; |
| 74 #endif | 80 #endif |
| 75 // ui::Clipboard has a protected destructor, so scoped_ptr doesn't work here. | 81 // ui::Clipboard has a protected destructor, so scoped_ptr doesn't work here. |
| 76 Clipboard* const clipboard_; | 82 Clipboard* const clipboard_; |
| 77 }; | 83 }; |
| 78 | 84 |
| 79 namespace { | 85 // Hack for tests that need to call static methods of ClipboardTest. |
| 80 | 86 struct NullClipboardTraits { |
| 81 bool MarkupMatches(const base::string16& expected_markup, | 87 static Clipboard* Create() { return nullptr; } |
| 82 const base::string16& actual_markup) { | 88 static void Destroy(Clipboard*) {} |
| 83 return actual_markup.find(expected_markup) != base::string16::npos; | |
| 84 } | |
| 85 | |
| 86 } // namespace | |
| 87 | |
| 88 struct PlatformClipboardTraits { | |
| 89 static Clipboard* Create() { | |
| 90 return Clipboard::GetForCurrentThread(); | |
| 91 } | |
| 92 | |
| 93 static void Destroy(Clipboard* clipboard) { | |
| 94 ASSERT_EQ(Clipboard::GetForCurrentThread(), clipboard); | |
| 95 Clipboard::DestroyClipboardForCurrentThread(); | |
| 96 } | |
| 97 }; | 89 }; |
| 98 | 90 |
| 99 TYPED_TEST_CASE(ClipboardTest, PlatformClipboardTraits); | 91 TYPED_TEST_CASE(ClipboardTest, TypesToTest); |
| 100 | 92 |
| 101 TYPED_TEST(ClipboardTest, ClearTest) { | 93 TYPED_TEST(ClipboardTest, ClearTest) { |
| 102 { | 94 { |
| 103 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | 95 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); |
| 104 clipboard_writer.WriteText(ASCIIToUTF16("clear me")); | 96 clipboard_writer.WriteText(ASCIIToUTF16("clear me")); |
| 105 } | 97 } |
| 106 | 98 |
| 107 this->clipboard().Clear(CLIPBOARD_TYPE_COPY_PASTE); | 99 this->clipboard().Clear(CLIPBOARD_TYPE_COPY_PASTE); |
| 108 | 100 |
| 109 EXPECT_FALSE(this->clipboard().IsFormatAvailable( | 101 EXPECT_FALSE(this->clipboard().IsFormatAvailable( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 138 std::string url("http://www.example.com/"), url_result; | 130 std::string url("http://www.example.com/"), url_result; |
| 139 | 131 |
| 140 { | 132 { |
| 141 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | 133 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); |
| 142 clipboard_writer.WriteText(plain); | 134 clipboard_writer.WriteText(plain); |
| 143 clipboard_writer.WriteHTML(markup, url); | 135 clipboard_writer.WriteHTML(markup, url); |
| 144 } | 136 } |
| 145 | 137 |
| 146 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | 138 EXPECT_TRUE(this->clipboard().IsFormatAvailable( |
| 147 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | 139 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); |
| 148 uint32 ignored; | 140 uint32 fragment_start; |
| 141 uint32 fragment_end; |
| 149 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, | 142 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, |
| 150 &url_result, &ignored, &ignored); | 143 &url_result, &fragment_start, &fragment_end); |
| 151 EXPECT_PRED2(MarkupMatches, markup, markup_result); | 144 EXPECT_LE(markup.size(), fragment_end - fragment_start); |
| 145 EXPECT_EQ(markup, |
| 146 markup_result.substr(fragment_end - markup.size(), markup.size())); |
| 152 #if defined(OS_WIN) | 147 #if defined(OS_WIN) |
| 153 // TODO(playmobil): It's not clear that non windows clipboards need to support | 148 // TODO(playmobil): It's not clear that non windows clipboards need to support |
| 154 // this. | 149 // this. |
| 155 EXPECT_EQ(url, url_result); | 150 EXPECT_EQ(url, url_result); |
| 156 #endif // defined(OS_WIN) | 151 #endif // defined(OS_WIN) |
| 157 } | 152 } |
| 158 | 153 |
| 159 TYPED_TEST(ClipboardTest, RTFTest) { | 154 TYPED_TEST(ClipboardTest, RTFTest) { |
| 160 std::string rtf = | 155 std::string rtf = |
| 161 "{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\n" | 156 "{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\n" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_SELECTION)); | 193 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_SELECTION)); |
| 199 | 194 |
| 200 EXPECT_FALSE(this->clipboard().IsFormatAvailable( | 195 EXPECT_FALSE(this->clipboard().IsFormatAvailable( |
| 201 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | 196 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); |
| 202 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | 197 EXPECT_TRUE(this->clipboard().IsFormatAvailable( |
| 203 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_SELECTION)); | 198 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_SELECTION)); |
| 204 | 199 |
| 205 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); | 200 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); |
| 206 EXPECT_EQ(text, text_result); | 201 EXPECT_EQ(text, text_result); |
| 207 | 202 |
| 208 uint32 ignored; | |
| 209 base::string16 markup_result; | 203 base::string16 markup_result; |
| 204 uint32 fragment_start; |
| 205 uint32 fragment_end; |
| 210 this->clipboard().ReadHTML(CLIPBOARD_TYPE_SELECTION, &markup_result, | 206 this->clipboard().ReadHTML(CLIPBOARD_TYPE_SELECTION, &markup_result, |
| 211 &url_result, &ignored, &ignored); | 207 &url_result, &fragment_start, &fragment_end); |
| 212 EXPECT_PRED2(MarkupMatches, markup, markup_result); | 208 EXPECT_LE(markup.size(), fragment_end - fragment_start); |
| 209 EXPECT_EQ(markup, |
| 210 markup_result.substr(fragment_end - markup.size(), markup.size())); |
| 213 } | 211 } |
| 214 #endif | 212 #endif |
| 215 | 213 |
| 216 TYPED_TEST(ClipboardTest, TrickyHTMLTest) { | 214 TYPED_TEST(ClipboardTest, TrickyHTMLTest) { |
| 217 base::string16 markup(ASCIIToUTF16("<em>Bye!<!--EndFragment --></em>")), | 215 base::string16 markup(ASCIIToUTF16("<em>Bye!<!--EndFragment --></em>")), |
| 218 markup_result; | 216 markup_result; |
| 219 std::string url, url_result; | 217 std::string url, url_result; |
| 220 base::string16 plain(ASCIIToUTF16("Bye!")), plain_result; | 218 base::string16 plain(ASCIIToUTF16("Bye!")), plain_result; |
| 221 | 219 |
| 222 { | 220 { |
| 223 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | 221 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); |
| 224 clipboard_writer.WriteText(plain); | 222 clipboard_writer.WriteText(plain); |
| 225 clipboard_writer.WriteHTML(markup, url); | 223 clipboard_writer.WriteHTML(markup, url); |
| 226 } | 224 } |
| 227 | 225 |
| 228 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | 226 EXPECT_TRUE(this->clipboard().IsFormatAvailable( |
| 229 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | 227 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); |
| 230 uint32 ignored; | 228 uint32 fragment_start; |
| 229 uint32 fragment_end; |
| 231 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, | 230 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, |
| 232 &url_result, &ignored, &ignored); | 231 &url_result, &fragment_start, &fragment_end); |
| 233 EXPECT_PRED2(MarkupMatches, markup, markup_result); | 232 EXPECT_LE(markup.size(), fragment_end - fragment_start); |
| 233 EXPECT_EQ(markup, |
| 234 markup_result.substr(fragment_end - markup.size(), markup.size())); |
| 234 #if defined(OS_WIN) | 235 #if defined(OS_WIN) |
| 235 // TODO(playmobil): It's not clear that non windows clipboards need to support | 236 // TODO(playmobil): It's not clear that non windows clipboards need to support |
| 236 // this. | 237 // this. |
| 237 EXPECT_EQ(url, url_result); | 238 EXPECT_EQ(url, url_result); |
| 238 #endif // defined(OS_WIN) | 239 #endif // defined(OS_WIN) |
| 239 } | 240 } |
| 240 | 241 |
| 241 #if defined(OS_WIN) | 242 // Some platforms store HTML as UTF-8 internally. Make sure fragment indices are |
| 242 TYPED_TEST(ClipboardTest, UniodeHTMLTest) { | 243 // adjusted appropriately when converting back to UTF-16. |
| 244 TYPED_TEST(ClipboardTest, UnicodeHTMLTest) { |
| 243 base::string16 markup(UTF8ToUTF16("<div>A \xc3\xb8 \xe6\xb0\xb4</div>")), | 245 base::string16 markup(UTF8ToUTF16("<div>A \xc3\xb8 \xe6\xb0\xb4</div>")), |
| 244 markup_result; | 246 markup_result; |
| 245 std::string url, url_result; | 247 std::string url, url_result; |
| 246 | 248 |
| 247 { | 249 { |
| 248 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | 250 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); |
| 249 clipboard_writer.WriteHTML(markup, url); | 251 clipboard_writer.WriteHTML(markup, url); |
| 250 } | 252 } |
| 251 | 253 |
| 252 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | 254 EXPECT_TRUE(this->clipboard().IsFormatAvailable( |
| 253 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | 255 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); |
| 254 uint32 fragment_start; | 256 uint32 fragment_start; |
| 255 uint32 fragment_end; | 257 uint32 fragment_end; |
| 256 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, | 258 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, |
| 257 &url_result, &fragment_start, &fragment_end); | 259 &url_result, &fragment_start, &fragment_end); |
| 258 EXPECT_PRED2(MarkupMatches, markup, markup_result); | 260 EXPECT_LE(markup.size(), fragment_end - fragment_start); |
| 261 EXPECT_EQ(markup, |
| 262 markup_result.substr(fragment_end - markup.size(), markup.size())); |
| 263 #if defined(OS_WIN) |
| 259 EXPECT_EQ(url, url_result); | 264 EXPECT_EQ(url, url_result); |
| 260 // Make sure that fragment indices were adjusted when converting. | 265 #endif |
| 261 EXPECT_EQ(36, fragment_start); | |
| 262 EXPECT_EQ(52, fragment_end); | |
| 263 } | 266 } |
| 264 #endif // defined(OS_WIN) | |
| 265 | 267 |
| 266 // TODO(estade): Port the following test (decide what target we use for urls) | 268 // TODO(estade): Port the following test (decide what target we use for urls) |
| 267 #if !defined(OS_POSIX) || defined(OS_MACOSX) | 269 #if !defined(OS_POSIX) || defined(OS_MACOSX) |
| 268 TYPED_TEST(ClipboardTest, BookmarkTest) { | 270 TYPED_TEST(ClipboardTest, BookmarkTest) { |
| 269 base::string16 title(ASCIIToUTF16("The Example Company")), title_result; | 271 base::string16 title(ASCIIToUTF16("The Example Company")), title_result; |
| 270 std::string url("http://www.example.com/"), url_result; | 272 std::string url("http://www.example.com/"), url_result; |
| 271 | 273 |
| 272 { | 274 { |
| 273 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | 275 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); |
| 274 clipboard_writer.WriteBookmark(title, url); | 276 clipboard_writer.WriteBookmark(title, url); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 293 clipboard_writer.WriteHTML(markup, url); | 295 clipboard_writer.WriteHTML(markup, url); |
| 294 clipboard_writer.WriteText(text); | 296 clipboard_writer.WriteText(text); |
| 295 } | 297 } |
| 296 | 298 |
| 297 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | 299 EXPECT_TRUE(this->clipboard().IsFormatAvailable( |
| 298 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | 300 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); |
| 299 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | 301 EXPECT_TRUE(this->clipboard().IsFormatAvailable( |
| 300 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | 302 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); |
| 301 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | 303 EXPECT_TRUE(this->clipboard().IsFormatAvailable( |
| 302 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | 304 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); |
| 303 uint32 ignored; | 305 uint32 fragment_start; |
| 306 uint32 fragment_end; |
| 304 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, | 307 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &markup_result, |
| 305 &url_result, &ignored, &ignored); | 308 &url_result, &fragment_start, &fragment_end); |
| 306 EXPECT_PRED2(MarkupMatches, markup, markup_result); | 309 EXPECT_LE(markup.size(), fragment_end - fragment_start); |
| 310 EXPECT_EQ(markup, |
| 311 markup_result.substr(fragment_end - markup.size(), markup.size())); |
| 307 #if defined(OS_WIN) | 312 #if defined(OS_WIN) |
| 308 // TODO(playmobil): It's not clear that non windows clipboards need to support | 313 // TODO(playmobil): It's not clear that non windows clipboards need to support |
| 309 // this. | 314 // this. |
| 310 EXPECT_EQ(url, url_result); | 315 EXPECT_EQ(url, url_result); |
| 311 #endif // defined(OS_WIN) | 316 #endif // defined(OS_WIN) |
| 312 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); | 317 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); |
| 313 EXPECT_EQ(text, text_result); | 318 EXPECT_EQ(text, text_result); |
| 314 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text); | 319 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text); |
| 315 EXPECT_EQ(UTF16ToUTF8(text), ascii_text); | 320 EXPECT_EQ(UTF16ToUTF8(text), ascii_text); |
| 316 } | 321 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 329 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | 334 Clipboard::GetPlainTextFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); |
| 330 base::string16 text_result; | 335 base::string16 text_result; |
| 331 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); | 336 this->clipboard().ReadText(CLIPBOARD_TYPE_COPY_PASTE, &text_result); |
| 332 | 337 |
| 333 EXPECT_EQ(text_result, url); | 338 EXPECT_EQ(text_result, url); |
| 334 | 339 |
| 335 std::string ascii_text; | 340 std::string ascii_text; |
| 336 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text); | 341 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &ascii_text); |
| 337 EXPECT_EQ(UTF16ToUTF8(url), ascii_text); | 342 EXPECT_EQ(UTF16ToUTF8(url), ascii_text); |
| 338 | 343 |
| 339 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) | 344 #if defined(OS_POSIX) && !defined(OS_MACOSX) && !defined(OS_ANDROID) && \ |
| 345 !defined(OS_CHROMEOS) |
| 340 ascii_text.clear(); | 346 ascii_text.clear(); |
| 341 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_SELECTION, &ascii_text); | 347 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_SELECTION, &ascii_text); |
| 342 EXPECT_EQ(UTF16ToUTF8(url), ascii_text); | 348 EXPECT_EQ(UTF16ToUTF8(url), ascii_text); |
| 343 #endif | 349 #endif |
| 344 } | 350 } |
| 345 | 351 |
| 346 // TODO(dcheng): The tests for copying to the clipboard also test IPC | 352 // TODO(dcheng): The tests for copying to the clipboard also test IPC |
| 347 // interaction... consider moving them to a different layer so we can | 353 // interaction... consider moving them to a different layer so we can |
| 348 // consolidate the validation logic. | 354 // consolidate the validation logic. |
| 349 // Note that |bitmap_data| is not premultiplied! | 355 // Note that |bitmap_data| is not premultiplied! |
| (...skipping 27 matching lines...) Expand all Loading... |
| 377 const char* size_data = reinterpret_cast<const char*>(&size); | 383 const char* size_data = reinterpret_cast<const char*>(&size); |
| 378 for (size_t i = 0; i < sizeof(size); ++i) | 384 for (size_t i = 0; i < sizeof(size); ++i) |
| 379 size_param.push_back(size_data[i]); | 385 size_param.push_back(size_data[i]); |
| 380 | 386 |
| 381 Clipboard::ObjectMapParams params; | 387 Clipboard::ObjectMapParams params; |
| 382 params.push_back(placeholder_param); | 388 params.push_back(placeholder_param); |
| 383 params.push_back(size_param); | 389 params.push_back(size_param); |
| 384 | 390 |
| 385 Clipboard::ObjectMap objects; | 391 Clipboard::ObjectMap objects; |
| 386 objects[Clipboard::CBF_SMBITMAP] = params; | 392 objects[Clipboard::CBF_SMBITMAP] = params; |
| 387 ASSERT_TRUE(Clipboard::ReplaceSharedMemHandle( | 393 ASSERT_TRUE(Clipboard::ReplaceSharedMemHandle(&objects, handle_to_share, |
| 388 &objects, handle_to_share, current_process)); | 394 current_process)); |
| 389 | 395 |
| 390 // This is pretty ugly, but the template type parameter is irrelevant... and | 396 // This is pretty ugly, but the template type parameter is irrelevant... and |
| 391 // this test will be going away anyway. | 397 // this test will be going away anyway. |
| 392 ClipboardTest<PlatformClipboardTraits>::WriteObjectsToClipboard(clipboard, | 398 ClipboardTest<NullClipboardTraits>::WriteObjectsToClipboard(clipboard, |
| 393 objects); | 399 objects); |
| 394 | 400 |
| 395 EXPECT_TRUE(clipboard->IsFormatAvailable(Clipboard::GetBitmapFormatType(), | 401 EXPECT_TRUE(clipboard->IsFormatAvailable(Clipboard::GetBitmapFormatType(), |
| 396 CLIPBOARD_TYPE_COPY_PASTE)); | 402 CLIPBOARD_TYPE_COPY_PASTE)); |
| 397 const SkBitmap& image = clipboard->ReadImage(CLIPBOARD_TYPE_COPY_PASTE); | 403 const SkBitmap& image = clipboard->ReadImage(CLIPBOARD_TYPE_COPY_PASTE); |
| 398 EXPECT_EQ(size, gfx::Size(image.width(), image.height())); | 404 EXPECT_EQ(size, gfx::Size(image.width(), image.height())); |
| 399 SkAutoLockPixels image_lock(image); | 405 SkAutoLockPixels image_lock(image); |
| 400 for (int j = 0; j < image.height(); ++j) { | 406 for (int j = 0; j < image.height(); ++j) { |
| 401 const uint32* row_address = image.getAddr32(0, j); | 407 const uint32* row_address = image.getAddr32(0, j); |
| 402 for (int i = 0; i < image.width(); ++i) { | 408 for (int i = 0; i < image.width(); ++i) { |
| 403 int offset = i + j * image.width(); | 409 int offset = i + j * image.width(); |
| 404 uint32 pixel = SkPreMultiplyColor(bitmap_data[offset]); | 410 uint32 pixel = SkPreMultiplyColor(bitmap_data[offset]); |
| 405 EXPECT_EQ(pixel, row_address[i]) | 411 EXPECT_EQ(pixel, row_address[i]) << "i = " << i << ", j = " << j; |
| 406 << "i = " << i << ", j = " << j; | |
| 407 } | 412 } |
| 408 } | 413 } |
| 409 } | 414 } |
| 410 | 415 |
| 411 TYPED_TEST(ClipboardTest, SharedBitmapTest) { | 416 TYPED_TEST(ClipboardTest, SharedBitmapTest) { |
| 412 const uint32 fake_bitmap_1[] = { | 417 const uint32 fake_bitmap_1[] = { |
| 413 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89, | 418 0x46155189, 0xF6A55C8D, 0x79845674, 0xFA57BD89, |
| 414 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568, | 419 0x78FD46AE, 0x87C64F5A, 0x36EDC5AF, 0x4378F568, |
| 415 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1, | 420 0x91E9F63A, 0xC31EA14F, 0x69AB32DF, 0x643A3FD1, |
| 416 }; | 421 }; |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 619 PickleIterator iter1(read_pickle1); | 624 PickleIterator iter1(read_pickle1); |
| 620 std::string unpickled_string1; | 625 std::string unpickled_string1; |
| 621 ASSERT_TRUE(read_pickle1.ReadString(&iter1, &unpickled_string1)); | 626 ASSERT_TRUE(read_pickle1.ReadString(&iter1, &unpickled_string1)); |
| 622 EXPECT_EQ(payload1, unpickled_string1); | 627 EXPECT_EQ(payload1, unpickled_string1); |
| 623 } | 628 } |
| 624 | 629 |
| 625 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) | 630 #if !defined(OS_MACOSX) && !defined(OS_ANDROID) |
| 626 TYPED_TEST(ClipboardTest, HyperlinkTest) { | 631 TYPED_TEST(ClipboardTest, HyperlinkTest) { |
| 627 const std::string kTitle("The <Example> Company's \"home page\""); | 632 const std::string kTitle("The <Example> Company's \"home page\""); |
| 628 const std::string kUrl("http://www.example.com?x=3<=3#\"'<>"); | 633 const std::string kUrl("http://www.example.com?x=3<=3#\"'<>"); |
| 629 const std::string kExpectedHtml( | 634 const base::string16 kExpectedHtml(UTF8ToUTF16( |
| 630 "<a href=\"http://www.example.com?x=3&lt=3#"'<>\">" | 635 "<a href=\"http://www.example.com?x=3&lt=3#"'<>\">" |
| 631 "The <Example> Company's "home page"</a>"); | 636 "The <Example> Company's "home page"</a>")); |
| 632 | 637 |
| 633 std::string url_result; | 638 std::string url_result; |
| 634 base::string16 html_result; | 639 base::string16 html_result; |
| 635 { | 640 { |
| 636 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | 641 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); |
| 637 clipboard_writer.WriteHyperlink(ASCIIToUTF16(kTitle), kUrl); | 642 clipboard_writer.WriteHyperlink(ASCIIToUTF16(kTitle), kUrl); |
| 638 } | 643 } |
| 639 | 644 |
| 640 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | 645 EXPECT_TRUE(this->clipboard().IsFormatAvailable( |
| 641 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | 646 Clipboard::GetHtmlFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); |
| 642 uint32 ignored; | 647 uint32 fragment_start; |
| 648 uint32 fragment_end; |
| 643 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &html_result, | 649 this->clipboard().ReadHTML(CLIPBOARD_TYPE_COPY_PASTE, &html_result, |
| 644 &url_result, &ignored, &ignored); | 650 &url_result, &fragment_start, &fragment_end); |
| 645 EXPECT_PRED2(MarkupMatches, ASCIIToUTF16(kExpectedHtml), html_result); | 651 EXPECT_EQ(kExpectedHtml, |
| 652 html_result.substr(fragment_end - kExpectedHtml.size(), |
| 653 kExpectedHtml.size())); |
| 646 } | 654 } |
| 647 #endif | 655 #endif |
| 648 | 656 |
| 649 TYPED_TEST(ClipboardTest, WebSmartPasteTest) { | 657 TYPED_TEST(ClipboardTest, WebSmartPasteTest) { |
| 650 { | 658 { |
| 651 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | 659 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); |
| 652 clipboard_writer.WriteWebSmartPaste(); | 660 clipboard_writer.WriteWebSmartPaste(); |
| 653 } | 661 } |
| 654 | 662 |
| 655 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | 663 EXPECT_TRUE(this->clipboard().IsFormatAvailable( |
| 656 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | 664 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); |
| 657 } | 665 } |
| 658 | 666 |
| 659 #if defined(OS_WIN) // Windows only tests. | 667 #if defined(OS_WIN) // Windows only tests. |
| 660 void HtmlTestHelper(const std::string& cf_html, | 668 void HtmlTestHelper(const std::string& cf_html, |
| 661 const std::string& expected_html) { | 669 const std::string& expected_html) { |
| 662 std::string html; | 670 std::string html; |
| 663 ClipboardUtil::CFHtmlToHtml(cf_html, &html, NULL); | 671 ClipboardUtil::CFHtmlToHtml(cf_html, &html, NULL); |
| 664 EXPECT_EQ(html, expected_html); | 672 EXPECT_EQ(html, expected_html); |
| 665 } | 673 } |
| 666 | 674 |
| 667 TYPED_TEST(ClipboardTest, HtmlTest) { | 675 TYPED_TEST(ClipboardTest, HtmlTest) { |
| 668 // Test converting from CF_HTML format data with <!--StartFragment--> and | 676 // Test converting from CF_HTML format data with <!--StartFragment--> and |
| 669 // <!--EndFragment--> comments, like from MS Word. | 677 // <!--EndFragment--> comments, like from MS Word. |
| 670 HtmlTestHelper("Version:1.0\r\n" | 678 HtmlTestHelper( |
| 671 "StartHTML:0000000105\r\n" | 679 "Version:1.0\r\n" |
| 672 "EndHTML:0000000199\r\n" | 680 "StartHTML:0000000105\r\n" |
| 673 "StartFragment:0000000123\r\n" | 681 "EndHTML:0000000199\r\n" |
| 674 "EndFragment:0000000161\r\n" | 682 "StartFragment:0000000123\r\n" |
| 675 "\r\n" | 683 "EndFragment:0000000161\r\n" |
| 676 "<html>\r\n" | 684 "\r\n" |
| 677 "<body>\r\n" | 685 "<html>\r\n" |
| 678 "<!--StartFragment-->\r\n" | 686 "<body>\r\n" |
| 679 "\r\n" | 687 "<!--StartFragment-->\r\n" |
| 680 "<p>Foo</p>\r\n" | 688 "\r\n" |
| 681 "\r\n" | 689 "<p>Foo</p>\r\n" |
| 682 "<!--EndFragment-->\r\n" | 690 "\r\n" |
| 683 "</body>\r\n" | 691 "<!--EndFragment-->\r\n" |
| 684 "</html>\r\n\r\n", | 692 "</body>\r\n" |
| 685 "<p>Foo</p>"); | 693 "</html>\r\n\r\n", |
| 694 "<p>Foo</p>"); |
| 686 | 695 |
| 687 // Test converting from CF_HTML format data without <!--StartFragment--> and | 696 // Test converting from CF_HTML format data without <!--StartFragment--> and |
| 688 // <!--EndFragment--> comments, like from OpenOffice Writer. | 697 // <!--EndFragment--> comments, like from OpenOffice Writer. |
| 689 HtmlTestHelper("Version:1.0\r\n" | 698 HtmlTestHelper( |
| 690 "StartHTML:0000000105\r\n" | 699 "Version:1.0\r\n" |
| 691 "EndHTML:0000000151\r\n" | 700 "StartHTML:0000000105\r\n" |
| 692 "StartFragment:0000000121\r\n" | 701 "EndHTML:0000000151\r\n" |
| 693 "EndFragment:0000000131\r\n" | 702 "StartFragment:0000000121\r\n" |
| 694 "<html>\r\n" | 703 "EndFragment:0000000131\r\n" |
| 695 "<body>\r\n" | 704 "<html>\r\n" |
| 696 "<p>Foo</p>\r\n" | 705 "<body>\r\n" |
| 697 "</body>\r\n" | 706 "<p>Foo</p>\r\n" |
| 698 "</html>\r\n\r\n", | 707 "</body>\r\n" |
| 699 "<p>Foo</p>"); | 708 "</html>\r\n\r\n", |
| 709 "<p>Foo</p>"); |
| 700 } | 710 } |
| 701 #endif // defined(OS_WIN) | 711 #endif // defined(OS_WIN) |
| 702 | 712 |
| 703 // Test writing all formats we have simultaneously. | 713 // Test writing all formats we have simultaneously. |
| 704 TYPED_TEST(ClipboardTest, WriteEverything) { | 714 TYPED_TEST(ClipboardTest, WriteEverything) { |
| 705 { | 715 { |
| 706 ScopedClipboardWriter writer(CLIPBOARD_TYPE_COPY_PASTE); | 716 ScopedClipboardWriter writer(CLIPBOARD_TYPE_COPY_PASTE); |
| 707 writer.WriteText(UTF8ToUTF16("foo")); | 717 writer.WriteText(UTF8ToUTF16("foo")); |
| 708 writer.WriteURL(UTF8ToUTF16("foo")); | 718 writer.WriteURL(UTF8ToUTF16("foo")); |
| 709 writer.WriteHTML(UTF8ToUTF16("foo"), "bar"); | 719 writer.WriteHTML(UTF8ToUTF16("foo"), "bar"); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 737 // the message loop to make sure we get the notification. | 747 // the message loop to make sure we get the notification. |
| 738 base::RunLoop().RunUntilIdle(); | 748 base::RunLoop().RunUntilIdle(); |
| 739 | 749 |
| 740 const uint64 second_sequence_number = | 750 const uint64 second_sequence_number = |
| 741 this->clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE); | 751 this->clipboard().GetSequenceNumber(CLIPBOARD_TYPE_COPY_PASTE); |
| 742 | 752 |
| 743 EXPECT_NE(first_sequence_number, second_sequence_number); | 753 EXPECT_NE(first_sequence_number, second_sequence_number); |
| 744 } | 754 } |
| 745 #endif | 755 #endif |
| 746 | 756 |
| 747 #if defined(OS_ANDROID) | |
| 748 | |
| 749 // Test that if another application writes some text to the pasteboard the | |
| 750 // clipboard properly invalidates other types. | |
| 751 TYPED_TEST(ClipboardTest, InternalClipboardInvalidation) { | |
| 752 // Write a Webkit smart paste tag to our clipboard. | |
| 753 { | |
| 754 ScopedClipboardWriter clipboard_writer(CLIPBOARD_TYPE_COPY_PASTE); | |
| 755 clipboard_writer.WriteWebSmartPaste(); | |
| 756 } | |
| 757 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | |
| 758 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 759 | |
| 760 // | |
| 761 // Simulate that another application copied something in the Clipboard | |
| 762 // | |
| 763 std::string new_value("Some text copied by some other app"); | |
| 764 using base::android::ConvertUTF8ToJavaString; | |
| 765 using base::android::MethodID; | |
| 766 using base::android::ScopedJavaLocalRef; | |
| 767 | |
| 768 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 769 ASSERT_TRUE(env); | |
| 770 | |
| 771 jobject context = base::android::GetApplicationContext(); | |
| 772 ASSERT_TRUE(context); | |
| 773 | |
| 774 ScopedJavaLocalRef<jclass> context_class = | |
| 775 base::android::GetClass(env, "android/content/Context"); | |
| 776 | |
| 777 jmethodID get_system_service = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
| 778 env, context_class.obj(), "getSystemService", | |
| 779 "(Ljava/lang/String;)Ljava/lang/Object;"); | |
| 780 | |
| 781 // Retrieve the system service. | |
| 782 ScopedJavaLocalRef<jstring> service_name = ConvertUTF8ToJavaString( | |
| 783 env, "clipboard"); | |
| 784 ScopedJavaLocalRef<jobject> clipboard_manager( | |
| 785 env, env->CallObjectMethod( | |
| 786 context, get_system_service, service_name.obj())); | |
| 787 ASSERT_TRUE(clipboard_manager.obj() && !base::android::ClearException(env)); | |
| 788 | |
| 789 ScopedJavaLocalRef<jclass> clipboard_class = | |
| 790 base::android::GetClass(env, "android/text/ClipboardManager"); | |
| 791 jmethodID set_text = MethodID::Get<MethodID::TYPE_INSTANCE>( | |
| 792 env, clipboard_class.obj(), "setText", "(Ljava/lang/CharSequence;)V"); | |
| 793 ScopedJavaLocalRef<jstring> new_value_string = ConvertUTF8ToJavaString( | |
| 794 env, new_value.c_str()); | |
| 795 | |
| 796 // Will need to call toString as CharSequence is not always a String. | |
| 797 env->CallVoidMethod(clipboard_manager.obj(), | |
| 798 set_text, | |
| 799 new_value_string.obj()); | |
| 800 | |
| 801 // The WebKit smart paste tag should now be gone. | |
| 802 EXPECT_FALSE(this->clipboard().IsFormatAvailable( | |
| 803 Clipboard::GetWebKitSmartPasteFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 804 | |
| 805 // Make sure some text is available | |
| 806 EXPECT_TRUE(this->clipboard().IsFormatAvailable( | |
| 807 Clipboard::GetPlainTextWFormatType(), CLIPBOARD_TYPE_COPY_PASTE)); | |
| 808 | |
| 809 // Make sure the text is what we inserted while simulating the other app | |
| 810 std::string contents; | |
| 811 this->clipboard().ReadAsciiText(CLIPBOARD_TYPE_COPY_PASTE, &contents); | |
| 812 EXPECT_EQ(contents, new_value); | |
| 813 } | |
| 814 #endif | |
| 815 | |
| 816 } // namespace ui | 757 } // namespace ui |
| OLD | NEW |