| 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 | 4 |
| 5 #include "ppapi/tests/test_flash_clipboard.h" | 5 #include "ppapi/tests/test_flash_clipboard.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "ppapi/cpp/instance.h" | 10 #include "ppapi/cpp/instance.h" |
| 10 #include "ppapi/cpp/module.h" | 11 #include "ppapi/cpp/module.h" |
| 11 #include "ppapi/cpp/point.h" | 12 #include "ppapi/cpp/point.h" |
| 12 #include "ppapi/cpp/private/flash_clipboard.h" | 13 #include "ppapi/cpp/private/flash_clipboard.h" |
| 14 #include "ppapi/cpp/var.h" |
| 15 #include "ppapi/cpp/var_array_buffer.h" |
| 13 #include "ppapi/tests/testing_instance.h" | 16 #include "ppapi/tests/testing_instance.h" |
| 14 | 17 |
| 15 REGISTER_TEST_CASE(FlashClipboard); | 18 REGISTER_TEST_CASE(FlashClipboard); |
| 16 | 19 |
| 17 // WriteData() sends an async request to the browser process. As a result, the | 20 // WriteData() sends an async request to the browser process. As a result, the |
| 18 // string written may not be reflected by IsFormatAvailable() or ReadPlainText() | 21 // string written may not be reflected by IsFormatAvailable() or ReadPlainText() |
| 19 // immediately. We need to wait and retry. | 22 // immediately. We need to wait and retry. |
| 20 const int kIntervalMs = 250; | 23 const int kIntervalMs = 250; |
| 21 const int kMaxIntervals = kActionTimeoutMs / kIntervalMs; | 24 const int kMaxIntervals = kActionTimeoutMs / kIntervalMs; |
| 22 | 25 |
| 23 TestFlashClipboard::TestFlashClipboard(TestingInstance* instance) | 26 TestFlashClipboard::TestFlashClipboard(TestingInstance* instance) |
| 24 : TestCase(instance) { | 27 : TestCase(instance) { |
| 25 } | 28 } |
| 26 | 29 |
| 27 void TestFlashClipboard::RunTests(const std::string& filter) { | 30 void TestFlashClipboard::RunTests(const std::string& filter) { |
| 28 RUN_TEST(ReadWritePlainText, filter); | 31 RUN_TEST(ReadWritePlainText, filter); |
| 29 RUN_TEST(ReadWriteHTML, filter); | 32 RUN_TEST(ReadWriteHTML, filter); |
| 33 RUN_TEST(ReadWriteRTF, filter); |
| 30 RUN_TEST(ReadWriteMultipleFormats, filter); | 34 RUN_TEST(ReadWriteMultipleFormats, filter); |
| 31 RUN_TEST(Clear, filter); | 35 RUN_TEST(Clear, filter); |
| 36 RUN_TEST(InvalidFormat, filter); |
| 32 } | 37 } |
| 33 | 38 |
| 34 std::string TestFlashClipboard::ReadStringVar( | 39 bool TestFlashClipboard::ReadStringVar( |
| 35 PP_Flash_Clipboard_Format format) { | 40 PP_Flash_Clipboard_Format format, |
| 41 std::string* result) { |
| 36 pp::Var text; | 42 pp::Var text; |
| 37 bool success = pp::flash::Clipboard::ReadData( | 43 bool success = pp::flash::Clipboard::ReadData( |
| 38 instance_, | 44 instance_, |
| 39 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | 45 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
| 40 format, | 46 format, |
| 41 &text); | 47 &text); |
| 42 if (success && text.is_string()) | 48 if (success && text.is_string()) { |
| 43 return text.AsString(); | 49 *result = text.AsString(); |
| 44 return std::string(); | 50 return true; |
| 51 } |
| 52 return false; |
| 45 } | 53 } |
| 46 | 54 |
| 47 bool TestFlashClipboard::WriteStringVar(PP_Flash_Clipboard_Format format, | 55 bool TestFlashClipboard::WriteStringVar(PP_Flash_Clipboard_Format format, |
| 48 const std::string& text) { | 56 const std::string& text) { |
| 49 std::vector<PP_Flash_Clipboard_Format> formats_vector(1, format); | 57 std::vector<PP_Flash_Clipboard_Format> formats_vector(1, format); |
| 50 std::vector<pp::Var> data_vector(1, pp::Var(text)); | 58 std::vector<pp::Var> data_vector(1, pp::Var(text)); |
| 51 bool success = pp::flash::Clipboard::WriteData( | 59 bool success = pp::flash::Clipboard::WriteData( |
| 52 instance_, | 60 instance_, |
| 53 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | 61 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
| 54 formats_vector, | 62 formats_vector, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 67 if (is_available == expected) | 75 if (is_available == expected) |
| 68 return true; | 76 return true; |
| 69 | 77 |
| 70 PlatformSleep(kIntervalMs); | 78 PlatformSleep(kIntervalMs); |
| 71 } | 79 } |
| 72 return false; | 80 return false; |
| 73 } | 81 } |
| 74 | 82 |
| 75 bool TestFlashClipboard::ReadPlainTextMatches(const std::string& expected) { | 83 bool TestFlashClipboard::ReadPlainTextMatches(const std::string& expected) { |
| 76 for (int i = 0; i < kMaxIntervals; ++i) { | 84 for (int i = 0; i < kMaxIntervals; ++i) { |
| 77 if (ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT) == expected) | 85 std::string result; |
| 86 bool success = ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, &result); |
| 87 if (success && result == expected) |
| 78 return true; | 88 return true; |
| 79 | 89 |
| 80 PlatformSleep(kIntervalMs); | 90 PlatformSleep(kIntervalMs); |
| 81 } | 91 } |
| 82 return false; | 92 return false; |
| 83 } | 93 } |
| 84 | 94 |
| 85 bool TestFlashClipboard::ReadHTMLMatches(const std::string& expected) { | 95 bool TestFlashClipboard::ReadHTMLMatches(const std::string& expected) { |
| 86 for (int i = 0; i < kMaxIntervals; ++i) { | 96 for (int i = 0; i < kMaxIntervals; ++i) { |
| 87 std::string result = ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML); | 97 std::string result; |
| 98 bool success = ReadStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML, &result); |
| 88 // Markup is inserted around the copied html, so just check that | 99 // Markup is inserted around the copied html, so just check that |
| 89 // the pasted string contains the copied string. | 100 // the pasted string contains the copied string. |
| 90 bool match = result.find(expected) != std::string::npos; | 101 if (success && result.find(expected) != std::string::npos) |
| 91 if (match) | |
| 92 return true; | 102 return true; |
| 93 | 103 |
| 94 PlatformSleep(kIntervalMs); | 104 PlatformSleep(kIntervalMs); |
| 95 } | 105 } |
| 96 return false; | 106 return false; |
| 97 } | 107 } |
| 98 | 108 |
| 99 std::string TestFlashClipboard::TestReadWritePlainText() { | 109 std::string TestFlashClipboard::TestReadWritePlainText() { |
| 100 std::string input = "Hello world plain text!"; | 110 std::string input = "Hello world plain text!"; |
| 101 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, input)); | 111 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, input)); |
| 102 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, | 112 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, |
| 103 true)); | 113 true)); |
| 104 ASSERT_TRUE(ReadPlainTextMatches(input)); | 114 ASSERT_TRUE(ReadPlainTextMatches(input)); |
| 105 | 115 |
| 106 PASS(); | 116 PASS(); |
| 107 } | 117 } |
| 108 | 118 |
| 109 std::string TestFlashClipboard::TestReadWriteHTML() { | 119 std::string TestFlashClipboard::TestReadWriteHTML() { |
| 110 std::string input = "Hello world html!"; | 120 std::string input = "Hello world html!"; |
| 111 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML, input)); | 121 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_HTML, input)); |
| 112 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_HTML, true)); | 122 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_HTML, true)); |
| 113 ASSERT_TRUE(ReadHTMLMatches(input)); | 123 ASSERT_TRUE(ReadHTMLMatches(input)); |
| 114 | 124 |
| 115 PASS(); | 125 PASS(); |
| 116 } | 126 } |
| 117 | 127 |
| 128 std::string TestFlashClipboard::TestReadWriteRTF() { |
| 129 std::string rtf_string = |
| 130 "{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\n" |
| 131 "This is some {\\b bold} text.\\par\n" |
| 132 "}"; |
| 133 pp::VarArrayBuffer array_buffer(rtf_string.size()); |
| 134 char *bytes = static_cast<char*>(array_buffer.Map()); |
| 135 std::copy(rtf_string.data(), rtf_string.data() + rtf_string.size(), bytes); |
| 136 std::vector<PP_Flash_Clipboard_Format> formats_vector(1, |
| 137 PP_FLASH_CLIPBOARD_FORMAT_RTF); |
| 138 std::vector<pp::Var> data_vector(1, array_buffer); |
| 139 ASSERT_TRUE(pp::flash::Clipboard::WriteData( |
| 140 instance_, |
| 141 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
| 142 formats_vector, |
| 143 data_vector)); |
| 144 |
| 145 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_RTF, true)); |
| 146 |
| 147 pp::Var rtf_result; |
| 148 ASSERT_TRUE(pp::flash::Clipboard::ReadData( |
| 149 instance_, |
| 150 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
| 151 PP_FLASH_CLIPBOARD_FORMAT_RTF, |
| 152 &rtf_result)); |
| 153 ASSERT_TRUE(rtf_result.is_array_buffer()); |
| 154 pp::VarArrayBuffer array_buffer_result(rtf_result); |
| 155 ASSERT_TRUE(array_buffer_result.ByteLength() == array_buffer.ByteLength()); |
| 156 char *bytes_result = static_cast<char*>(array_buffer_result.Map()); |
| 157 ASSERT_TRUE(std::equal(bytes, bytes + array_buffer.ByteLength(), |
| 158 bytes_result)); |
| 159 |
| 160 PASS(); |
| 161 } |
| 162 |
| 118 std::string TestFlashClipboard::TestReadWriteMultipleFormats() { | 163 std::string TestFlashClipboard::TestReadWriteMultipleFormats() { |
| 119 std::vector<PP_Flash_Clipboard_Format> formats; | 164 std::vector<PP_Flash_Clipboard_Format> formats; |
| 120 std::vector<pp::Var> data; | 165 std::vector<pp::Var> data; |
| 121 formats.push_back(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT); | 166 formats.push_back(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT); |
| 122 data.push_back(pp::Var("plain text")); | 167 data.push_back(pp::Var("plain text")); |
| 123 formats.push_back(PP_FLASH_CLIPBOARD_FORMAT_HTML); | 168 formats.push_back(PP_FLASH_CLIPBOARD_FORMAT_HTML); |
| 124 data.push_back(pp::Var("html")); | 169 data.push_back(pp::Var("html")); |
| 125 bool success = pp::flash::Clipboard::WriteData( | 170 bool success = pp::flash::Clipboard::WriteData( |
| 126 instance_, | 171 instance_, |
| 127 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | 172 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
| (...skipping 18 matching lines...) Expand all Loading... |
| 146 instance_, | 191 instance_, |
| 147 PP_FLASH_CLIPBOARD_TYPE_STANDARD, | 192 PP_FLASH_CLIPBOARD_TYPE_STANDARD, |
| 148 std::vector<PP_Flash_Clipboard_Format>(), | 193 std::vector<PP_Flash_Clipboard_Format>(), |
| 149 std::vector<pp::Var>()); | 194 std::vector<pp::Var>()); |
| 150 ASSERT_TRUE(success); | 195 ASSERT_TRUE(success); |
| 151 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, | 196 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, |
| 152 false)); | 197 false)); |
| 153 | 198 |
| 154 PASS(); | 199 PASS(); |
| 155 } | 200 } |
| 201 |
| 202 std::string TestFlashClipboard::TestInvalidFormat() { |
| 203 PP_Flash_Clipboard_Format invalid_format = |
| 204 static_cast<PP_Flash_Clipboard_Format>(-1); |
| 205 ASSERT_FALSE(WriteStringVar(invalid_format, "text")); |
| 206 ASSERT_TRUE(IsFormatAvailableMatches(invalid_format, false)); |
| 207 std::string unused; |
| 208 ASSERT_FALSE(ReadStringVar(invalid_format, &unused)); |
| 209 |
| 210 PASS(); |
| 211 } |
| OLD | NEW |