Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1110)

Side by Side Diff: ppapi/tests/test_flash_clipboard.cc

Issue 11225021: Move flash clipboard to the new proxy and add custom format support (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "ppapi/cpp/instance.h" 10 #include "ppapi/cpp/instance.h"
(...skipping 13 matching lines...) Expand all
24 const int kMaxIntervals = kActionTimeoutMs / kIntervalMs; 24 const int kMaxIntervals = kActionTimeoutMs / kIntervalMs;
25 25
26 TestFlashClipboard::TestFlashClipboard(TestingInstance* instance) 26 TestFlashClipboard::TestFlashClipboard(TestingInstance* instance)
27 : TestCase(instance) { 27 : TestCase(instance) {
28 } 28 }
29 29
30 void TestFlashClipboard::RunTests(const std::string& filter) { 30 void TestFlashClipboard::RunTests(const std::string& filter) {
31 RUN_TEST(ReadWritePlainText, filter); 31 RUN_TEST(ReadWritePlainText, filter);
32 RUN_TEST(ReadWriteHTML, filter); 32 RUN_TEST(ReadWriteHTML, filter);
33 RUN_TEST(ReadWriteRTF, filter); 33 RUN_TEST(ReadWriteRTF, filter);
34 RUN_TEST(ReadWriteCustomData, filter);
34 RUN_TEST(ReadWriteMultipleFormats, filter); 35 RUN_TEST(ReadWriteMultipleFormats, filter);
35 RUN_TEST(Clear, filter); 36 RUN_TEST(Clear, filter);
36 RUN_TEST(InvalidFormat, filter); 37 RUN_TEST(InvalidFormat, filter);
38 RUN_TEST(RegisterCustomFormat, filter);
37 } 39 }
38 40
39 bool TestFlashClipboard::ReadStringVar( 41 bool TestFlashClipboard::ReadStringVar(uint32_t format, std::string* result) {
40 PP_Flash_Clipboard_Format format,
41 std::string* result) {
42 pp::Var text; 42 pp::Var text;
43 bool success = pp::flash::Clipboard::ReadData( 43 bool success = pp::flash::Clipboard::ReadData(
44 instance_, 44 instance_,
45 PP_FLASH_CLIPBOARD_TYPE_STANDARD, 45 PP_FLASH_CLIPBOARD_TYPE_STANDARD,
46 format, 46 format,
47 &text); 47 &text);
48 if (success && text.is_string()) { 48 if (success && text.is_string()) {
49 *result = text.AsString(); 49 *result = text.AsString();
50 return true; 50 return true;
51 } 51 }
52 return false; 52 return false;
53 } 53 }
54 54
55 bool TestFlashClipboard::WriteStringVar(PP_Flash_Clipboard_Format format, 55 bool TestFlashClipboard::WriteStringVar(uint32_t format,
56 const std::string& text) { 56 const std::string& text) {
57 std::vector<PP_Flash_Clipboard_Format> formats_vector(1, format); 57 std::vector<uint32_t> formats_vector(1, format);
58 std::vector<pp::Var> data_vector(1, pp::Var(text)); 58 std::vector<pp::Var> data_vector(1, pp::Var(text));
59 bool success = pp::flash::Clipboard::WriteData( 59 bool success = pp::flash::Clipboard::WriteData(
60 instance_, 60 instance_,
61 PP_FLASH_CLIPBOARD_TYPE_STANDARD, 61 PP_FLASH_CLIPBOARD_TYPE_STANDARD,
62 formats_vector, 62 formats_vector,
63 data_vector); 63 data_vector);
64 return success; 64 return success;
65 } 65 }
66 66
67 bool TestFlashClipboard::IsFormatAvailableMatches( 67 bool TestFlashClipboard::IsFormatAvailableMatches(uint32_t format,
68 PP_Flash_Clipboard_Format format, 68 bool expected) {
69 bool expected) {
70 for (int i = 0; i < kMaxIntervals; ++i) { 69 for (int i = 0; i < kMaxIntervals; ++i) {
71 bool is_available = pp::flash::Clipboard::IsFormatAvailable( 70 bool is_available = pp::flash::Clipboard::IsFormatAvailable(
72 instance_, 71 instance_,
73 PP_FLASH_CLIPBOARD_TYPE_STANDARD, 72 PP_FLASH_CLIPBOARD_TYPE_STANDARD,
74 format); 73 format);
75 if (is_available == expected) 74 if (is_available == expected)
76 return true; 75 return true;
77 76
78 PlatformSleep(kIntervalMs); 77 PlatformSleep(kIntervalMs);
79 } 78 }
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 128
130 PASS(); 129 PASS();
131 } 130 }
132 131
133 std::string TestFlashClipboard::TestReadWriteRTF() { 132 std::string TestFlashClipboard::TestReadWriteRTF() {
134 std::string rtf_string = 133 std::string rtf_string =
135 "{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\n" 134 "{\\rtf1\\ansi{\\fonttbl\\f0\\fswiss Helvetica;}\\f0\\pard\n"
136 "This is some {\\b bold} text.\\par\n" 135 "This is some {\\b bold} text.\\par\n"
137 "}"; 136 "}";
138 pp::VarArrayBuffer array_buffer(rtf_string.size()); 137 pp::VarArrayBuffer array_buffer(rtf_string.size());
139 char *bytes = static_cast<char*>(array_buffer.Map()); 138 char* bytes = static_cast<char*>(array_buffer.Map());
140 std::copy(rtf_string.data(), rtf_string.data() + rtf_string.size(), bytes); 139 std::copy(rtf_string.data(), rtf_string.data() + rtf_string.size(), bytes);
141 std::vector<PP_Flash_Clipboard_Format> formats_vector(1, 140 std::vector<uint32_t> formats_vector(1, PP_FLASH_CLIPBOARD_FORMAT_RTF);
142 PP_FLASH_CLIPBOARD_FORMAT_RTF);
143 std::vector<pp::Var> data_vector(1, array_buffer); 141 std::vector<pp::Var> data_vector(1, array_buffer);
144 ASSERT_TRUE(pp::flash::Clipboard::WriteData( 142 ASSERT_TRUE(pp::flash::Clipboard::WriteData(
145 instance_, 143 instance_,
146 PP_FLASH_CLIPBOARD_TYPE_STANDARD, 144 PP_FLASH_CLIPBOARD_TYPE_STANDARD,
147 formats_vector, 145 formats_vector,
148 data_vector)); 146 data_vector));
149 147
150 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_RTF, true)); 148 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_RTF, true));
151 149
152 pp::Var rtf_result; 150 pp::Var rtf_result;
153 ASSERT_TRUE(pp::flash::Clipboard::ReadData( 151 ASSERT_TRUE(pp::flash::Clipboard::ReadData(
154 instance_, 152 instance_,
155 PP_FLASH_CLIPBOARD_TYPE_STANDARD, 153 PP_FLASH_CLIPBOARD_TYPE_STANDARD,
156 PP_FLASH_CLIPBOARD_FORMAT_RTF, 154 PP_FLASH_CLIPBOARD_FORMAT_RTF,
157 &rtf_result)); 155 &rtf_result));
158 ASSERT_TRUE(rtf_result.is_array_buffer()); 156 ASSERT_TRUE(rtf_result.is_array_buffer());
159 pp::VarArrayBuffer array_buffer_result(rtf_result); 157 pp::VarArrayBuffer array_buffer_result(rtf_result);
160 ASSERT_TRUE(array_buffer_result.ByteLength() == array_buffer.ByteLength()); 158 ASSERT_TRUE(array_buffer_result.ByteLength() == array_buffer.ByteLength());
161 char *bytes_result = static_cast<char*>(array_buffer_result.Map()); 159 char* bytes_result = static_cast<char*>(array_buffer_result.Map());
162 ASSERT_TRUE(std::equal(bytes, bytes + array_buffer.ByteLength(), 160 ASSERT_TRUE(std::equal(bytes, bytes + array_buffer.ByteLength(),
163 bytes_result)); 161 bytes_result));
164 162
163 PASS();
164 }
165
166 std::string TestFlashClipboard::TestReadWriteCustomData() {
167 std::string custom_data = "custom_data";
168 pp::VarArrayBuffer array_buffer(custom_data.size());
169 char* bytes = static_cast<char*>(array_buffer.Map());
170 std::copy(custom_data.begin(), custom_data.end(), bytes);
171 uint32_t format_id =
172 pp::flash::Clipboard::RegisterCustomFormat(instance_, "my-format");
173 ASSERT_NE(format_id, PP_FLASH_CLIPBOARD_FORMAT_INVALID);
174
175 std::vector<uint32_t> formats_vector(1, format_id);
176 std::vector<pp::Var> data_vector(1, array_buffer);
177 ASSERT_TRUE(pp::flash::Clipboard::WriteData(
178 instance_,
179 PP_FLASH_CLIPBOARD_TYPE_STANDARD,
180 formats_vector,
181 data_vector));
182
183 ASSERT_TRUE(IsFormatAvailableMatches(format_id, true));
184
185 pp::Var custom_data_result;
186 ASSERT_TRUE(pp::flash::Clipboard::ReadData(
187 instance_,
yzshen1 2012/10/29 18:57:33 wrong indent.
raymes 2012/10/29 20:04:48 Done.
188 PP_FLASH_CLIPBOARD_TYPE_STANDARD,
189 format_id,
190 &custom_data_result));
191 ASSERT_TRUE(custom_data_result.is_array_buffer());
192 pp::VarArrayBuffer array_buffer_result(custom_data_result);
193 ASSERT_EQ(array_buffer_result.ByteLength(), array_buffer.ByteLength());
194 char* bytes_result = static_cast<char*>(array_buffer_result.Map());
195 ASSERT_TRUE(std::equal(bytes, bytes + array_buffer.ByteLength(),
196 bytes_result));
197
165 PASS(); 198 PASS();
166 } 199 }
167 200
168 std::string TestFlashClipboard::TestReadWriteMultipleFormats() { 201 std::string TestFlashClipboard::TestReadWriteMultipleFormats() {
169 std::vector<PP_Flash_Clipboard_Format> formats; 202 std::vector<uint32_t> formats;
170 std::vector<pp::Var> data; 203 std::vector<pp::Var> data;
171 formats.push_back(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT); 204 formats.push_back(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT);
172 data.push_back(pp::Var("plain text")); 205 data.push_back(pp::Var("plain text"));
173 formats.push_back(PP_FLASH_CLIPBOARD_FORMAT_HTML); 206 formats.push_back(PP_FLASH_CLIPBOARD_FORMAT_HTML);
174 data.push_back(pp::Var("html")); 207 data.push_back(pp::Var("html"));
175 bool success = pp::flash::Clipboard::WriteData( 208 bool success = pp::flash::Clipboard::WriteData(
176 instance_, 209 instance_,
177 PP_FLASH_CLIPBOARD_TYPE_STANDARD, 210 PP_FLASH_CLIPBOARD_TYPE_STANDARD,
178 formats, 211 formats,
179 data); 212 data);
180 ASSERT_TRUE(success); 213 ASSERT_TRUE(success);
181 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, 214 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT,
182 true)); 215 true));
183 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_HTML, true)); 216 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_HTML, true));
184 ASSERT_TRUE(ReadPlainTextMatches(data[0].AsString())); 217 ASSERT_TRUE(ReadPlainTextMatches(data[0].AsString()));
185 ASSERT_TRUE(ReadHTMLMatches(data[1].AsString())); 218 ASSERT_TRUE(ReadHTMLMatches(data[1].AsString()));
186 219
187 PASS(); 220 PASS();
188 } 221 }
189 222
190 std::string TestFlashClipboard::TestClear() { 223 std::string TestFlashClipboard::TestClear() {
191 std::string input = "Hello world plain text!"; 224 std::string input = "Hello world plain text!";
192 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, input)); 225 ASSERT_TRUE(WriteStringVar(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, input));
193 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, 226 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT,
194 true)); 227 true));
195 bool success = pp::flash::Clipboard::WriteData( 228 bool success = pp::flash::Clipboard::WriteData(
196 instance_, 229 instance_,
197 PP_FLASH_CLIPBOARD_TYPE_STANDARD, 230 PP_FLASH_CLIPBOARD_TYPE_STANDARD,
198 std::vector<PP_Flash_Clipboard_Format>(), 231 std::vector<uint32_t>(),
199 std::vector<pp::Var>()); 232 std::vector<pp::Var>());
200 ASSERT_TRUE(success); 233 ASSERT_TRUE(success);
201 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT, 234 ASSERT_TRUE(IsFormatAvailableMatches(PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT,
202 false)); 235 false));
203 236
204 PASS(); 237 PASS();
205 } 238 }
206 239
207 std::string TestFlashClipboard::TestInvalidFormat() { 240 std::string TestFlashClipboard::TestInvalidFormat() {
208 PP_Flash_Clipboard_Format invalid_format = 241 uint32_t invalid_format = 999;
209 static_cast<PP_Flash_Clipboard_Format>(-1);
210 ASSERT_FALSE(WriteStringVar(invalid_format, "text")); 242 ASSERT_FALSE(WriteStringVar(invalid_format, "text"));
211 ASSERT_TRUE(IsFormatAvailableMatches(invalid_format, false)); 243 ASSERT_TRUE(IsFormatAvailableMatches(invalid_format, false));
212 std::string unused; 244 std::string unused;
213 ASSERT_FALSE(ReadStringVar(invalid_format, &unused)); 245 ASSERT_FALSE(ReadStringVar(invalid_format, &unused));
214 246
215 PASS(); 247 PASS();
216 } 248 }
249
250 std::string TestFlashClipboard::TestRegisterCustomFormat() {
251 // Test an empty name is rejected.
252 uint32_t format_id =
253 pp::flash::Clipboard::RegisterCustomFormat(instance_, "");
yzshen1 2012/10/29 18:57:33 wrong indent.
raymes 2012/10/29 20:04:48 Done.
254 ASSERT_EQ(format_id, PP_FLASH_CLIPBOARD_FORMAT_INVALID);
255
256 // Test a valid format name.
257 format_id = pp::flash::Clipboard::RegisterCustomFormat(instance_, "a-b");
258 ASSERT_NE(format_id, PP_FLASH_CLIPBOARD_FORMAT_INVALID);
259 // Make sure the format doesn't collide with predefined formats.
260 ASSERT_NE(format_id, PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT);
261 ASSERT_NE(format_id, PP_FLASH_CLIPBOARD_FORMAT_HTML);
262 ASSERT_NE(format_id, PP_FLASH_CLIPBOARD_FORMAT_RTF);
263
264 // Check that if the same name is registered, the same id comes out.
265 uint32_t format_id2 =
266 pp::flash::Clipboard::RegisterCustomFormat(instance_, "a-b");
267 ASSERT_EQ(format_id, format_id2);
268
269 // Check that the second format registered has a different id.
270 uint32_t format_id3 =
271 pp::flash::Clipboard::RegisterCustomFormat(instance_, "a-b-c");
272 ASSERT_NE(format_id, format_id3);
273
274 PASS();
275 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698