Chromium Code Reviews| 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 testing_exceptions_(false), | |
| 189 message_received_(instance->pp_instance()) { | |
| 188 } | 190 } |
| 189 | 191 |
| 190 TestMessageHandler::~TestMessageHandler() { | 192 TestMessageHandler::~TestMessageHandler() { |
| 191 handler_thread_.Join(); | 193 handler_thread_.Join(); |
| 192 } | 194 } |
| 193 | 195 |
| 194 bool TestMessageHandler::Init() { | 196 bool TestMessageHandler::Init() { |
| 195 ppb_messaging_if_ = static_cast<const PPB_Messaging_1_2*>( | 197 ppb_messaging_if_ = static_cast<const PPB_Messaging_1_2*>( |
| 196 pp::Module::Get()->GetBrowserInterface(PPB_MESSAGING_INTERFACE_1_2)); | 198 pp::Module::Get()->GetBrowserInterface(PPB_MESSAGING_INTERFACE_1_2)); |
| 197 return ppb_messaging_if_ && | 199 return ppb_messaging_if_ && |
| 198 CheckTestingInterface() && | 200 CheckTestingInterface() && |
| 199 handler_thread_.Start(); | 201 handler_thread_.Start(); |
| 200 } | 202 } |
| 201 | 203 |
| 202 void TestMessageHandler::RunTests(const std::string& filter) { | 204 void TestMessageHandler::RunTests(const std::string& filter) { |
| 203 RUN_TEST(RegisterErrorConditions, filter); | 205 RUN_TEST(RegisterErrorConditions, filter); |
| 204 RUN_TEST(PostMessageAndAwaitResponse, filter); | 206 RUN_TEST(PostMessageAndAwaitResponse, filter); |
| 207 RUN_TEST(Exceptions, filter); | |
| 205 } | 208 } |
| 206 | 209 |
| 207 void TestMessageHandler::HandleMessage(const pp::Var& message_data) { | 210 void TestMessageHandler::HandleMessage(const pp::Var& message_data) { |
| 208 // All messages should go to the background thread message handler. | 211 if (!testing_exceptions_) { |
| 209 assert(false); | 212 // For TestPostMessageAndAwaitResponse(), all messages should go to the |
| 213 // background thread message handler. | |
| 214 assert(false); | |
| 215 } else { | |
| 216 if (message_data.is_string()) { | |
| 217 last_message_ = message_data.AsString(); | |
| 218 } else { | |
| 219 last_message_ = "message_data was not a string!"; | |
| 220 } | |
| 221 message_received_.Signal(); | |
| 222 } | |
| 210 } | 223 } |
| 211 | 224 |
| 212 std::string TestMessageHandler::TestRegisterErrorConditions() { | 225 std::string TestMessageHandler::TestRegisterErrorConditions() { |
| 213 { | 226 { |
| 214 // Test registering with the main thread as the message loop. | 227 // Test registering with the main thread as the message loop. |
| 215 PPP_MessageHandler_0_2 fake_ppp_message_handler = { | 228 PPP_MessageHandler_0_2 fake_ppp_message_handler = { |
| 216 &FakeHandleMessage, &FakeHandleBlockingMessage, &FakeDestroy | 229 &FakeHandleMessage, &FakeHandleBlockingMessage, &FakeDestroy |
| 217 }; | 230 }; |
| 218 pp::MessageLoop main_loop = pp::MessageLoop::GetForMainThread(); | 231 pp::MessageLoop main_loop = pp::MessageLoop::GetForMainThread(); |
| 219 int32_t result = ppb_messaging_if_->RegisterMessageHandler( | 232 int32_t result = ppb_messaging_if_->RegisterMessageHandler( |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 237 handler_thread_.message_loop().pp_resource()); | 250 handler_thread_.message_loop().pp_resource()); |
| 238 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); | 251 ASSERT_EQ(PP_ERROR_BADARGUMENT, result); |
| 239 } | 252 } |
| 240 } | 253 } |
| 241 PASS(); | 254 PASS(); |
| 242 } | 255 } |
| 243 | 256 |
| 244 std::string TestMessageHandler::TestPostMessageAndAwaitResponse() { | 257 std::string TestMessageHandler::TestPostMessageAndAwaitResponse() { |
| 245 EchoingMessageHandler handler(instance(), | 258 EchoingMessageHandler handler(instance(), |
| 246 handler_thread_.message_loop()); | 259 handler_thread_.message_loop()); |
| 260 // Test doing a sync call before the handler is registered. | |
| 247 handler.Register(); | 261 handler.Register(); |
| 248 std::string js_code("var plugin = document.getElementById('plugin');\n"); | 262 std::string js_code("var plugin = document.getElementById('plugin');\n"); |
| 249 js_code += "var result = undefined;\n"; | 263 js_code += "var result = undefined;\n"; |
| 250 const char* const values_to_test[] = { | 264 const char* const values_to_test[] = { |
| 251 "5", | 265 "5", |
| 252 "undefined", | 266 "undefined", |
| 253 "1.5", | 267 "1.5", |
| 254 "'hello'", | 268 "'hello'", |
| 255 "{'key': 'value', 'array_key': [1, 2, 3, 4, 5]}", | 269 "{'key': 'value', 'array_key': [1, 2, 3, 4, 5]}", |
| 256 NULL | 270 NULL |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 273 // That should be fixed before this API goes to stable. See crbug.com/384528 | 287 // That should be fixed before this API goes to stable. See crbug.com/384528 |
| 274 js_code += "plugin.postMessage('FINISHED_TEST');\n"; | 288 js_code += "plugin.postMessage('FINISHED_TEST');\n"; |
| 275 instance_->EvalScript(js_code); | 289 instance_->EvalScript(js_code); |
| 276 handler.WaitForTestFinishedMessage(); | 290 handler.WaitForTestFinishedMessage(); |
| 277 handler.Unregister(); | 291 handler.Unregister(); |
| 278 ASSERT_SUBTEST_SUCCESS(handler.WaitForDestroy()); | 292 ASSERT_SUBTEST_SUCCESS(handler.WaitForDestroy()); |
| 279 | 293 |
| 280 PASS(); | 294 PASS(); |
| 281 } | 295 } |
| 282 | 296 |
| 297 std::string TestMessageHandler::TestExceptions() { | |
| 298 testing_exceptions_ = true; | |
| 299 | |
| 300 EchoingMessageHandler handler(instance(), | |
| 301 handler_thread_.message_loop()); | |
| 302 { | |
| 303 // First, try sending a blocking message when there is no handler | |
| 304 // registered. It should throw an exception. | |
| 305 std::string js_code( | |
| 306 "var plugin = document.getElementById('plugin');\n" | |
| 307 "var caught_exception = false;\n" | |
| 308 "try {\n" | |
| 309 " plugin.postMessageAndAwaitResponse('Hello!');\n" | |
| 310 "} catch (err) {\n" | |
| 311 " caught_exception = true;\n" | |
| 312 "}\n" | |
| 313 "plugin.postMessage(caught_exception ? 'SUCCESS' : 'FAIL');\n"); | |
| 314 instance_->EvalScript(js_code); | |
| 315 message_received_.Wait(); | |
| 316 ASSERT_EQ("SUCCESS", last_message_); | |
|
teravest
2014/09/26 19:28:50
If this assertion fails, will testing_exceptions_
dmichael (off chromium)
2014/09/26 20:12:42
Good catch, thanks.
| |
| 317 } | |
| 318 handler.Register(); | |
| 319 { | |
| 320 // Now that a handler is registered, try requesting and sending a | |
| 321 // FileSystem. It should throw an exception. The file system is opened | |
| 322 // asynchronously. What *should* happen is that it opens successfully, then | |
| 323 // we try to send it via postMessageAndAwaitResponse, which fails with an | |
| 324 // exception. The test could fail either because the filesystem doesn't | |
| 325 // open or because postMessageAndAwaitResponse doesn't throw an exception. | |
| 326 std::string js_code( | |
| 327 "var plugin = document.getElementById('plugin');\n" | |
| 328 "function gotFileSystem(fs) {\n" | |
| 329 " var caught_exception = false;\n" | |
| 330 " try {\n" | |
| 331 " plugin.postMessageAndAwaitResponse(fs);\n" | |
| 332 " } catch (err) {\n" | |
| 333 " caught_exception = true;\n" | |
| 334 " }\n" | |
| 335 " plugin.postMessage(caught_exception ? 'SUCCESS' : 'FAIL');\n" | |
| 336 "}\n" | |
| 337 "function fileSystemError() {\n" | |
| 338 " plugin.postMessage('Failed to open filesystem');\n" | |
| 339 "}\n" | |
| 340 "window.webkitRequestFileSystem(\n" | |
| 341 " window.Temporary, 1024, gotFileSystem, fileSystemError)\n"); | |
| 342 instance_->EvalScript(js_code); | |
| 343 message_received_.Wait(); | |
| 344 ASSERT_EQ("SUCCESS", last_message_); | |
| 345 } | |
| 346 handler.Unregister(); | |
| 347 ASSERT_SUBTEST_SUCCESS(handler.WaitForDestroy()); | |
| 348 | |
| 349 testing_exceptions_ = false; | |
| 350 PASS(); | |
| 351 } | |
| 352 | |
| OLD | NEW |