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