OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_message_handler.h" | 5 #include "ppapi/tests/test_message_handler.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <map> | 9 #include <map> |
10 #include <sstream> | 10 #include <sstream> |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 void* user_data, | 177 void* user_data, |
178 const PP_Var* message_data, | 178 const PP_Var* message_data, |
179 PP_Var* result) {} | 179 PP_Var* result) {} |
180 void FakeDestroy(PP_Instance instance, void* user_data) {} | 180 void FakeDestroy(PP_Instance instance, void* user_data) {} |
181 | 181 |
182 } // namespace | 182 } // namespace |
183 | 183 |
184 TestMessageHandler::TestMessageHandler(TestingInstance* instance) | 184 TestMessageHandler::TestMessageHandler(TestingInstance* instance) |
185 : TestCase(instance), | 185 : TestCase(instance), |
186 ppb_messaging_if_(NULL), | 186 ppb_messaging_if_(NULL), |
187 handler_thread_(instance) { | 187 handler_thread_(instance), |
| 188 message_received_(instance->pp_instance()) { |
188 } | 189 } |
189 | 190 |
190 TestMessageHandler::~TestMessageHandler() { | 191 TestMessageHandler::~TestMessageHandler() { |
191 handler_thread_.Join(); | 192 handler_thread_.Join(); |
192 } | 193 } |
193 | 194 |
194 bool TestMessageHandler::Init() { | 195 bool TestMessageHandler::Init() { |
195 ppb_messaging_if_ = static_cast<const PPB_Messaging_1_2*>( | 196 ppb_messaging_if_ = static_cast<const PPB_Messaging_1_2*>( |
196 pp::Module::Get()->GetBrowserInterface(PPB_MESSAGING_INTERFACE_1_2)); | 197 pp::Module::Get()->GetBrowserInterface(PPB_MESSAGING_INTERFACE_1_2)); |
197 return ppb_messaging_if_ && | 198 return ppb_messaging_if_ && |
198 CheckTestingInterface() && | 199 CheckTestingInterface() && |
199 handler_thread_.Start(); | 200 handler_thread_.Start(); |
200 } | 201 } |
201 | 202 |
202 void TestMessageHandler::RunTests(const std::string& filter) { | 203 void TestMessageHandler::RunTests(const std::string& filter) { |
203 RUN_TEST(RegisterErrorConditions, filter); | 204 RUN_TEST(RegisterErrorConditions, filter); |
204 RUN_TEST(PostMessageAndAwaitResponse, filter); | 205 RUN_TEST(PostMessageAndAwaitResponse, filter); |
| 206 RUN_TEST(Exceptions, filter); |
205 } | 207 } |
206 | 208 |
207 void TestMessageHandler::HandleMessage(const pp::Var& message_data) { | 209 void TestMessageHandler::HandleMessage(const pp::Var& message_data) { |
208 // All messages should go to the background thread message handler. | 210 if (instance()->current_test_name() == "Exceptions") { |
209 assert(false); | 211 // For TestPostMessageAndAwaitResponse(), all messages should go to the |
| 212 // background thread message handler. |
| 213 assert(false); |
| 214 } else { |
| 215 if (message_data.is_string()) { |
| 216 last_message_ = message_data.AsString(); |
| 217 } else { |
| 218 last_message_ = "message_data was not a string!"; |
| 219 } |
| 220 message_received_.Signal(); |
| 221 } |
210 } | 222 } |
211 | 223 |
212 std::string TestMessageHandler::TestRegisterErrorConditions() { | 224 std::string TestMessageHandler::TestRegisterErrorConditions() { |
213 { | 225 { |
214 // Test registering with the main thread as the message loop. | 226 // Test registering with the main thread as the message loop. |
215 PPP_MessageHandler_0_2 fake_ppp_message_handler = { | 227 PPP_MessageHandler_0_2 fake_ppp_message_handler = { |
216 &FakeHandleMessage, &FakeHandleBlockingMessage, &FakeDestroy | 228 &FakeHandleMessage, &FakeHandleBlockingMessage, &FakeDestroy |
217 }; | 229 }; |
218 pp::MessageLoop main_loop = pp::MessageLoop::GetForMainThread(); | 230 pp::MessageLoop main_loop = pp::MessageLoop::GetForMainThread(); |
219 int32_t result = ppb_messaging_if_->RegisterMessageHandler( | 231 int32_t result = ppb_messaging_if_->RegisterMessageHandler( |
(...skipping 17 matching lines...) Expand all Loading... |
237 handler_thread_.message_loop().pp_resource()); | 249 handler_thread_.message_loop().pp_resource()); |
238 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 250 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
239 } | 251 } |
240 } | 252 } |
241 PASS(); | 253 PASS(); |
242 } | 254 } |
243 | 255 |
244 std::string TestMessageHandler::TestPostMessageAndAwaitResponse() { | 256 std::string TestMessageHandler::TestPostMessageAndAwaitResponse() { |
245 EchoingMessageHandler handler(instance(), | 257 EchoingMessageHandler handler(instance(), |
246 handler_thread_.message_loop()); | 258 handler_thread_.message_loop()); |
| 259 // Test doing a sync call before the handler is registered. |
247 handler.Register(); | 260 handler.Register(); |
248 std::string js_code("var plugin = document.getElementById('plugin');\n"); | 261 std::string js_code("var plugin = document.getElementById('plugin');\n"); |
249 js_code += "var result = undefined;\n"; | 262 js_code += "var result = undefined;\n"; |
250 const char* const values_to_test[] = { | 263 const char* const values_to_test[] = { |
251 "5", | 264 "5", |
252 "undefined", | 265 "undefined", |
253 "1.5", | 266 "1.5", |
254 "'hello'", | 267 "'hello'", |
255 "{'key': 'value', 'array_key': [1, 2, 3, 4, 5]}", | 268 "{'key': 'value', 'array_key': [1, 2, 3, 4, 5]}", |
256 NULL | 269 NULL |
(...skipping 11 matching lines...) Expand all Loading... |
268 } | 281 } |
269 instance_->EvalScript(js_code); | 282 instance_->EvalScript(js_code); |
270 instance_->EvalScript("plugin.postMessage('FINISHED_TEST');\n"); | 283 instance_->EvalScript("plugin.postMessage('FINISHED_TEST');\n"); |
271 handler.WaitForTestFinishedMessage(); | 284 handler.WaitForTestFinishedMessage(); |
272 handler.Unregister(); | 285 handler.Unregister(); |
273 ASSERT_SUBTEST_SUCCESS(handler.WaitForDestroy()); | 286 ASSERT_SUBTEST_SUCCESS(handler.WaitForDestroy()); |
274 | 287 |
275 PASS(); | 288 PASS(); |
276 } | 289 } |
277 | 290 |
| 291 std::string TestMessageHandler::TestExceptions() { |
| 292 EchoingMessageHandler handler(instance(), |
| 293 handler_thread_.message_loop()); |
| 294 { |
| 295 // First, try sending a blocking message when there is no handler |
| 296 // registered. It should throw an exception. |
| 297 std::string js_code( |
| 298 "var plugin = document.getElementById('plugin');\n" |
| 299 "var caught_exception = false;\n" |
| 300 "try {\n" |
| 301 " plugin.postMessageAndAwaitResponse('Hello!');\n" |
| 302 "} catch (err) {\n" |
| 303 " caught_exception = true;\n" |
| 304 "}\n" |
| 305 "plugin.postMessage(caught_exception ? 'SUCCESS' : 'FAIL');\n"); |
| 306 instance_->EvalScript(js_code); |
| 307 message_received_.Wait(); |
| 308 ASSERT_EQ("SUCCESS", last_message_); |
| 309 } |
| 310 handler.Register(); |
| 311 { |
| 312 // Now that a handler is registered, try requesting and sending a |
| 313 // FileSystem. It should throw an exception. The file system is opened |
| 314 // asynchronously. What *should* happen is that it opens successfully, then |
| 315 // we try to send it via postMessageAndAwaitResponse, which fails with an |
| 316 // exception. The test could fail either because the filesystem doesn't |
| 317 // open or because postMessageAndAwaitResponse doesn't throw an exception. |
| 318 std::string js_code( |
| 319 "var plugin = document.getElementById('plugin');\n" |
| 320 "function gotFileSystem(fs) {\n" |
| 321 " var caught_exception = false;\n" |
| 322 " try {\n" |
| 323 " plugin.postMessageAndAwaitResponse(fs);\n" |
| 324 " } catch (err) {\n" |
| 325 " caught_exception = true;\n" |
| 326 " }\n" |
| 327 " plugin.postMessage(caught_exception ? 'SUCCESS' : 'FAIL');\n" |
| 328 "}\n" |
| 329 "function fileSystemError() {\n" |
| 330 " plugin.postMessage('Failed to open filesystem');\n" |
| 331 "}\n" |
| 332 "window.webkitRequestFileSystem(\n" |
| 333 " window.Temporary, 1024, gotFileSystem, fileSystemError)\n"); |
| 334 instance_->EvalScript(js_code); |
| 335 message_received_.Wait(); |
| 336 ASSERT_EQ("SUCCESS", last_message_); |
| 337 } |
| 338 handler.Unregister(); |
| 339 ASSERT_SUBTEST_SUCCESS(handler.WaitForDestroy()); |
| 340 |
| 341 PASS(); |
| 342 } |
| 343 |
OLD | NEW |