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_utils.h" | 5 #include "ppapi/tests/test_utils.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #if defined(_MSC_VER) | 9 #if defined(_MSC_VER) |
10 #include <windows.h> | 10 #include <windows.h> |
11 #else | 11 #else |
12 #include <unistd.h> | 12 #include <unistd.h> |
13 #endif | 13 #endif |
14 | 14 |
15 #include "ppapi/c/pp_errors.h" | 15 #include "ppapi/c/pp_errors.h" |
16 #include "ppapi/cpp/dev/message_loop_dev.h" | |
16 #include "ppapi/cpp/module.h" | 17 #include "ppapi/cpp/module.h" |
17 #include "ppapi/cpp/var.h" | 18 #include "ppapi/cpp/var.h" |
18 | 19 |
19 const int kActionTimeoutMs = 10000; | 20 const int kActionTimeoutMs = 10000; |
20 | 21 |
21 const PPB_Testing_Dev* GetTestingInterface() { | 22 const PPB_Testing_Dev* GetTestingInterface() { |
22 static const PPB_Testing_Dev* g_testing_interface = | 23 static const PPB_Testing_Dev* g_testing_interface = |
23 static_cast<const PPB_Testing_Dev*>( | 24 static_cast<const PPB_Testing_Dev*>( |
24 pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE)); | 25 pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE)); |
25 return g_testing_interface; | 26 return g_testing_interface; |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 instance_(instance), | 132 instance_(instance), |
132 delegate_(NULL) { | 133 delegate_(NULL) { |
133 } | 134 } |
134 | 135 |
135 int32_t TestCompletionCallback::WaitForResult() { | 136 int32_t TestCompletionCallback::WaitForResult() { |
136 PP_DCHECK(!wait_for_result_called_); | 137 PP_DCHECK(!wait_for_result_called_); |
137 wait_for_result_called_ = true; | 138 wait_for_result_called_ = true; |
138 errors_.clear(); | 139 errors_.clear(); |
139 if (!have_result_) { | 140 if (!have_result_) { |
140 post_quit_task_ = true; | 141 post_quit_task_ = true; |
141 GetTestingInterface()->RunMessageLoop(instance_); | 142 RunMessageLoop(); |
142 } | 143 } |
143 return result_; | 144 return result_; |
144 } | 145 } |
145 | 146 |
146 void TestCompletionCallback::WaitForResult(int32_t result) { | 147 void TestCompletionCallback::WaitForResult(int32_t result) { |
147 PP_DCHECK(!wait_for_result_called_); | 148 PP_DCHECK(!wait_for_result_called_); |
148 wait_for_result_called_ = true; | 149 wait_for_result_called_ = true; |
149 errors_.clear(); | 150 errors_.clear(); |
150 if (result == PP_OK_COMPLETIONPENDING) { | 151 if (result == PP_OK_COMPLETIONPENDING) { |
151 if (!have_result_) { | 152 if (!have_result_) { |
152 post_quit_task_ = true; | 153 post_quit_task_ = true; |
153 GetTestingInterface()->RunMessageLoop(instance_); | 154 RunMessageLoop(); |
154 } | 155 } |
155 if (callback_type_ == PP_BLOCKING) { | 156 if (callback_type_ == PP_BLOCKING) { |
156 errors_.assign( | 157 errors_.assign( |
157 ReportError("TestCompletionCallback: Call did not run synchronously " | 158 ReportError("TestCompletionCallback: Call did not run synchronously " |
158 "when passed a blocking completion callback!", | 159 "when passed a blocking completion callback!", |
159 result_)); | 160 result_)); |
160 return; | 161 return; |
161 } | 162 } |
162 } else { | 163 } else { |
163 result_ = result; | 164 result_ = result; |
(...skipping 29 matching lines...) Expand all Loading... | |
193 } | 194 } |
194 } | 195 } |
195 | 196 |
196 pp::CompletionCallback TestCompletionCallback::GetCallback() { | 197 pp::CompletionCallback TestCompletionCallback::GetCallback() { |
197 Reset(); | 198 Reset(); |
198 int32_t flags = 0; | 199 int32_t flags = 0; |
199 if (callback_type_ == PP_BLOCKING) | 200 if (callback_type_ == PP_BLOCKING) |
200 return pp::CompletionCallback(); | 201 return pp::CompletionCallback(); |
201 else if (callback_type_ == PP_OPTIONAL) | 202 else if (callback_type_ == PP_OPTIONAL) |
202 flags = PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; | 203 flags = PP_COMPLETIONCALLBACK_FLAG_OPTIONAL; |
204 target_loop_ = pp::MessageLoop_Dev::GetCurrent(); | |
203 return pp::CompletionCallback(&TestCompletionCallback::Handler, | 205 return pp::CompletionCallback(&TestCompletionCallback::Handler, |
204 const_cast<TestCompletionCallback*>(this), | 206 const_cast<TestCompletionCallback*>(this), |
205 flags); | 207 flags); |
206 } | 208 } |
207 | 209 |
208 void TestCompletionCallback::Reset() { | 210 void TestCompletionCallback::Reset() { |
209 wait_for_result_called_ = false; | 211 wait_for_result_called_ = false; |
210 result_ = PP_OK_COMPLETIONPENDING; | 212 result_ = PP_OK_COMPLETIONPENDING; |
211 have_result_ = false; | 213 have_result_ = false; |
212 post_quit_task_ = false; | 214 post_quit_task_ = false; |
213 run_count_ = 0; // TODO(dmichael): Remove when all tests are updated. | 215 run_count_ = 0; // TODO(dmichael): Remove when all tests are updated. |
214 delegate_ = NULL; | 216 delegate_ = NULL; |
215 errors_.clear(); | 217 errors_.clear(); |
216 } | 218 } |
217 | 219 |
218 // static | 220 // static |
219 void TestCompletionCallback::Handler(void* user_data, int32_t result) { | 221 void TestCompletionCallback::Handler(void* user_data, int32_t result) { |
220 TestCompletionCallback* callback = | 222 TestCompletionCallback* callback = |
221 static_cast<TestCompletionCallback*>(user_data); | 223 static_cast<TestCompletionCallback*>(user_data); |
222 // If this check fails, it means that the callback was invoked twice or that | 224 // If this check fails, it means that the callback was invoked twice or that |
223 // the PPAPI call completed synchronously, but also ran the callback. | 225 // the PPAPI call completed synchronously, but also ran the callback. |
224 PP_DCHECK(!callback->have_result_); | 226 PP_DCHECK(!callback->have_result_); |
225 callback->result_ = result; | 227 callback->result_ = result; |
226 callback->have_result_ = true; | 228 callback->have_result_ = true; |
227 callback->run_count_++; // TODO(dmichael): Remove when all tests are updated. | 229 callback->run_count_++; // TODO(dmichael): Remove when all tests are updated. |
228 if (callback->delegate_) | 230 if (callback->delegate_) |
229 callback->delegate_->OnCallback(user_data, result); | 231 callback->delegate_->OnCallback(user_data, result); |
230 if (callback->post_quit_task_) { | 232 if (callback->post_quit_task_) { |
231 callback->post_quit_task_ = false; | 233 callback->post_quit_task_ = false; |
232 GetTestingInterface()->QuitMessageLoop(callback->instance_); | 234 callback->QuitMessageLoop(); |
235 } | |
236 if (!(callback->target_loop_ == pp::MessageLoop_Dev::GetCurrent())) { | |
brettw
2012/11/06 23:16:28
Use != instead?
dmichael (off chromium)
2012/11/06 23:39:26
I was being lazy, since pp::Resource didn't have o
| |
237 // Note, in-process, loop_ and GetCurrent() will both be NULL, so should | |
238 // still be equal. | |
239 callback->errors_.assign( | |
240 ReportError("TestCompletionCallback: Callback ran on the wrong message " | |
241 "loop!", | |
242 result)); | |
233 } | 243 } |
234 } | 244 } |
235 | 245 |
246 void TestCompletionCallback::RunMessageLoop() { | |
247 pp::MessageLoop_Dev loop(pp::MessageLoop_Dev::GetCurrent()); | |
248 // If we don't have a message loop, we're probably running in process, where | |
249 // PPB_MessageLoop is not supported. Just use the Testing message loop. | |
250 if (loop.is_null() || loop == pp::MessageLoop_Dev::GetForMainThread()) | |
251 GetTestingInterface()->RunMessageLoop(instance_); | |
252 else | |
253 loop.Run(); | |
254 } | |
255 | |
256 void TestCompletionCallback::QuitMessageLoop() { | |
257 pp::MessageLoop_Dev loop(pp::MessageLoop_Dev::GetCurrent()); | |
258 // If we don't have a message loop, we're probably running in process, where | |
259 // PPB_MessageLoop is not supported. Just use the Testing message loop. | |
260 if (loop.is_null() || loop == pp::MessageLoop_Dev::GetForMainThread()) { | |
261 GetTestingInterface()->QuitMessageLoop(instance_); | |
262 } else { | |
263 const bool should_quit = false; | |
264 loop.PostQuit(should_quit); | |
265 } | |
266 } | |
OLD | NEW |