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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
// 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_utils.h"
#include <stdio.h>
#include <stdlib.h>
#if defined(_MSC_VER)
#include <windows.h>
#else
#include <unistd.h>
#endif
#include "ppapi/c/pp_errors.h"
#include "ppapi/cpp/module.h"
#include "ppapi/cpp/var.h"
const int kActionTimeoutMs = 10000;
const PPB_Testing_Dev* GetTestingInterface() {
static const PPB_Testing_Dev* g_testing_interface =
static_cast<const PPB_Testing_Dev*>(
pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE));
return g_testing_interface;
}
std::string ReportError(const char* method, int32_t error) {
char error_as_string[12];
sprintf(error_as_string, "%d", static_cast<int>(error));
std::string result = method + std::string(" failed with error: ") +
error_as_string;
return result;
}
void PlatformSleep(int duration_ms) {
#if defined(_MSC_VER)
::Sleep(duration_ms);
#else
usleep(duration_ms * 1000);
#endif
}
bool GetLocalHostPort(PP_Instance instance, std::string* host, uint16_t* port) {
if (!host || !port)
return false;
const PPB_Testing_Dev* testing = GetTestingInterface();
if (!testing)
return false;
PP_URLComponents_Dev components;
pp::Var pp_url(pp::PASS_REF,
testing->GetDocumentURL(instance, &components));
if (!pp_url.is_string())
return false;
std::string url = pp_url.AsString();
if (components.host.len < 0)
return false;
host->assign(url.substr(components.host.begin, components.host.len));
if (components.port.len <= 0)
return false;
int i = atoi(url.substr(components.port.begin, components.port.len).c_str());
if (i < 0 || i > 65535)
return false;
*port = static_cast<uint16_t>(i);
return true;
}
void NestedEvent::Wait() {
// Don't allow nesting more than once; it doesn't work with the code as-is,
// and probably is a bad idea most of the time anyway.
PP_DCHECK(!waiting_);
if (signalled_)
return;
waiting_ = true;
while (!signalled_)
GetTestingInterface()->RunMessageLoop(instance_);
waiting_ = false;
}
void NestedEvent::Signal() {
signalled_ = true;
if (waiting_)
GetTestingInterface()->QuitMessageLoop(instance_);
}
void NestedEvent::Reset() {
// It doesn't make sense to reset when we're still waiting.
PP_DCHECK(!waiting_);
// We must have already been Signalled().
PP_DCHECK(signalled_);
signalled_ = false;
}
TestCompletionCallback::TestCompletionCallback(PP_Instance instance)
: wait_for_result_called_(false),
have_result_(false),
result_(PP_OK_COMPLETIONPENDING),
// TODO(dmichael): The default should probably be PP_REQUIRED, but this is
// what the tests currently expect.
callback_type_(PP_OPTIONAL),
post_quit_task_(false),
run_count_(0), // TODO(dmichael): Remove when all tests are updated.
instance_(instance),
delegate_(NULL) {
}
TestCompletionCallback::TestCompletionCallback(PP_Instance instance,
bool force_async)
: wait_for_result_called_(false),
have_result_(false),
result_(PP_OK_COMPLETIONPENDING),
callback_type_(force_async ? PP_REQUIRED : PP_OPTIONAL),
post_quit_task_(false),
instance_(instance),
delegate_(NULL) {
}
TestCompletionCallback::TestCompletionCallback(PP_Instance instance,
CallbackType callback_type)
: wait_for_result_called_(false),
have_result_(false),
result_(PP_OK_COMPLETIONPENDING),
callback_type_(callback_type),
post_quit_task_(false),
instance_(instance),
delegate_(NULL) {
}
int32_t TestCompletionCallback::WaitForResult() {
PP_DCHECK(!wait_for_result_called_);
wait_for_result_called_ = true;
errors_.clear();
if (!have_result_) {
post_quit_task_ = true;
RunMessageLoop();
}
return result_;
}
void TestCompletionCallback::WaitForResult(int32_t result) {
PP_DCHECK(!wait_for_result_called_);
wait_for_result_called_ = true;
errors_.clear();
if (result == PP_OK_COMPLETIONPENDING) {
if (!have_result_) {
post_quit_task_ = true;
RunMessageLoop();
}
if (callback_type_ == PP_BLOCKING) {
errors_.assign(
ReportError("TestCompletionCallback: Call did not run synchronously "
"when passed a blocking completion callback!",
result_));
return;
}
} else {
result_ = result;
have_result_ = true;
if (callback_type_ == PP_REQUIRED) {
errors_.assign(
ReportError("TestCompletionCallback: Call ran synchronously when "
"passed a required completion callback!",
result_));
return;
}
}
PP_DCHECK(have_result_ == true);
}
void TestCompletionCallback::WaitForAbortResult(int32_t result) {
WaitForResult(result);
int32_t final_result = result_;
if (result == PP_OK_COMPLETIONPENDING) {
if (final_result != PP_ERROR_ABORTED) {
errors_.assign(
ReportError("TestCompletionCallback: Expected PP_ERROR_ABORTED or "
"PP_OK. Ran asynchronously.",
final_result));
return;
}
} else if (result < PP_OK) {
errors_.assign(
ReportError("TestCompletionCallback: Expected PP_ERROR_ABORTED or "
"non-error response. Ran synchronously.",
result));
return;
}
}
pp::CompletionCallback TestCompletionCallback::GetCallback() {
Reset();
int32_t flags = 0;
if (callback_type_ == PP_BLOCKING)
return pp::CompletionCallback();
else if (callback_type_ == PP_OPTIONAL)
flags = PP_COMPLETIONCALLBACK_FLAG_OPTIONAL;
target_loop_ = pp::MessageLoop::GetCurrent();
return pp::CompletionCallback(&TestCompletionCallback::Handler,
const_cast<TestCompletionCallback*>(this),
flags);
}
void TestCompletionCallback::Reset() {
wait_for_result_called_ = false;
result_ = PP_OK_COMPLETIONPENDING;
have_result_ = false;
post_quit_task_ = false;
run_count_ = 0; // TODO(dmichael): Remove when all tests are updated.
delegate_ = NULL;
errors_.clear();
}
// static
void TestCompletionCallback::Handler(void* user_data, int32_t result) {
TestCompletionCallback* callback =
static_cast<TestCompletionCallback*>(user_data);
// If this check fails, it means that the callback was invoked twice or that
// the PPAPI call completed synchronously, but also ran the callback.
PP_DCHECK(!callback->have_result_);
callback->result_ = result;
callback->have_result_ = true;
callback->run_count_++; // TODO(dmichael): Remove when all tests are updated.
if (callback->delegate_)
callback->delegate_->OnCallback(user_data, result);
if (callback->post_quit_task_) {
callback->post_quit_task_ = false;
callback->QuitMessageLoop();
}
if (callback->target_loop_ != pp::MessageLoop::GetCurrent()) {
// Note, in-process, loop_ and GetCurrent() will both be NULL, so should
// still be equal.
callback->errors_.assign(
ReportError("TestCompletionCallback: Callback ran on the wrong message "
"loop!",
result));
}
}
void TestCompletionCallback::RunMessageLoop() {
pp::MessageLoop loop(pp::MessageLoop::GetCurrent());
// If we don't have a message loop, we're probably running in process, where
// PPB_MessageLoop is not supported. Just use the Testing message loop.
if (loop.is_null() || loop == pp::MessageLoop::GetForMainThread())
GetTestingInterface()->RunMessageLoop(instance_);
else
loop.Run();
}
void TestCompletionCallback::QuitMessageLoop() {
pp::MessageLoop loop(pp::MessageLoop::GetCurrent());
// If we don't have a message loop, we're probably running in process, where
// PPB_MessageLoop is not supported. Just use the Testing message loop.
if (loop.is_null() || loop == pp::MessageLoop::GetForMainThread()) {
GetTestingInterface()->QuitMessageLoop(instance_);
} else {
const bool should_quit = false;
loop.PostQuit(should_quit);
}
}
|