Chromium Code Reviews| Index: ppapi/tests/test_message_handler.cc |
| diff --git a/ppapi/tests/test_message_handler.cc b/ppapi/tests/test_message_handler.cc |
| index 7dc933dde74ff2c134ccfb3a79092a3968e2fa10..c11f014e496426fcfe15b8f52d562b719276eaff 100644 |
| --- a/ppapi/tests/test_message_handler.cc |
| +++ b/ppapi/tests/test_message_handler.cc |
| @@ -42,15 +42,16 @@ namespace { |
| // Created and destroyed on the main thread. All public methods should be called |
| // on the main thread. Most data members are only accessed on the main thread. |
| // (Though it handles messages on the background thread). |
| -class EchoingMessageHandler : public pp::MessageHandler { |
| +class MockMessageHandler : public pp::MessageHandler { |
|
teravest
2014/10/26 21:09:07
I'm not sure Mock is the right prefix, maybe TestM
dmichael (off chromium)
2014/10/27 16:30:39
I almost used that name myself, but realized that'
|
| public: |
| - explicit EchoingMessageHandler(TestingInstance* instance, |
| - const pp::MessageLoop& loop) |
| + explicit MockMessageHandler(TestingInstance* instance, |
| + const pp::MessageLoop& loop) |
| : testing_instance_(instance), |
| message_handler_loop_(loop), |
| is_registered_(false), |
| test_finished_event_(instance->pp_instance()), |
| - destroy_event_(instance->pp_instance()) { |
| + destroy_event_(instance->pp_instance()), |
| + async_message_received_(instance->pp_instance()) { |
| AssertOnMainThread(); |
| } |
| void Register() { |
| @@ -95,6 +96,13 @@ class EchoingMessageHandler : public pp::MessageHandler { |
| errors_.swap(temp_errors); |
| return temp_errors; |
| } |
| + pp::Var WaitForAsyncMessage() { |
| + async_message_received_.Wait(); |
| + pp::Var var_to_return = last_async_message_received_; |
| + last_async_message_received_ = pp::Var(); |
| + async_message_received_.Reset(); |
| + return var_to_return; |
| + } |
| private: |
| static void AssertOnMainThread() { |
| assert(pp::MessageLoop::GetForMainThread() == |
| @@ -114,8 +122,13 @@ class EchoingMessageHandler : public pp::MessageHandler { |
| AddError("HandleMessage was passed the wrong instance!"); |
| if (var.is_string() && var.AsString() == "FINISHED_TEST") |
| test_finished_event_.Signal(); |
| - else |
| - testing_instance_->PostMessage(var); |
|
dmichael (off chromium)
2014/10/24 21:06:21
This "echo" was unused
|
| + else { |
|
teravest
2014/10/26 21:09:07
Please use braces on the if clause if you're going
dmichael (off chromium)
2014/10/27 16:30:39
Oops, done, thanks.
|
| + // Any client causing a message to arrive must wait for the message |
| + // before continuing. See WaitForAsyncMessage(). |
| + assert(last_async_message_received_.is_undefined()); |
| + last_async_message_received_ = var; |
| + async_message_received_.Signal(); |
| + } |
| } |
| virtual pp::Var HandleBlockingMessage(pp::InstanceHandle instance, |
| @@ -165,9 +178,12 @@ class EchoingMessageHandler : public pp::MessageHandler { |
| NestedEvent test_finished_event_; |
| NestedEvent destroy_event_; |
| + pp::Var last_async_message_received_; |
| + NestedEvent async_message_received_; |
| + |
| // Undefined & private to disallow copy and assign. |
| - EchoingMessageHandler(const EchoingMessageHandler&); |
| - EchoingMessageHandler& operator=(const EchoingMessageHandler&); |
| + MockMessageHandler(const MockMessageHandler&); |
| + MockMessageHandler& operator=(const MockMessageHandler&); |
| }; |
| void FakeHandleMessage(PP_Instance instance, |
| @@ -183,9 +199,9 @@ void FakeDestroy(PP_Instance instance, void* user_data) {} |
| TestMessageHandler::TestMessageHandler(TestingInstance* instance) |
| : TestCase(instance), |
| + message_received_(instance->pp_instance()), |
| ppb_messaging_if_(NULL), |
| - handler_thread_(instance), |
| - message_received_(instance->pp_instance()) { |
| + handler_thread_(instance) { |
| } |
| TestMessageHandler::~TestMessageHandler() { |
| @@ -212,11 +228,10 @@ void TestMessageHandler::HandleMessage(const pp::Var& message_data) { |
| // background thread message handler. |
| assert(false); |
| } else { |
| - if (message_data.is_string()) { |
| - last_message_ = message_data.AsString(); |
| - } else { |
| - last_message_ = "message_data was not a string!"; |
| - } |
| + // Any subtest causing a message to arrive here must wait for it before |
| + // continuing. See WaitForMessage(). |
| + assert(last_message_.is_undefined()); |
| + last_message_ = message_data; |
| message_received_.Signal(); |
| } |
| } |
| @@ -254,9 +269,8 @@ std::string TestMessageHandler::TestRegisterErrorConditions() { |
| } |
| std::string TestMessageHandler::TestPostMessageAndAwaitResponse() { |
| - EchoingMessageHandler handler(instance(), |
| - handler_thread_.message_loop()); |
| - // Test doing a sync call before the handler is registered. |
| + MockMessageHandler handler(instance(), |
| + handler_thread_.message_loop()); |
| handler.Register(); |
| std::string js_code("var plugin = document.getElementById('plugin');\n"); |
| js_code += "var result = undefined;\n"; |
| @@ -289,8 +303,8 @@ std::string TestMessageHandler::TestPostMessageAndAwaitResponse() { |
| } |
| std::string TestMessageHandler::TestExceptions() { |
| - EchoingMessageHandler handler(instance(), |
| - handler_thread_.message_loop()); |
| + MockMessageHandler handler(instance(), |
| + handler_thread_.message_loop()); |
| { |
| // First, try sending a blocking message when there is no handler |
| // registered. It should throw an exception. |
| @@ -304,8 +318,12 @@ std::string TestMessageHandler::TestExceptions() { |
| "}\n" |
| "plugin.postMessage(caught_exception ? 'SUCCESS' : 'FAIL');\n"); |
| instance_->EvalScript(js_code); |
| - message_received_.Wait(); |
| - ASSERT_EQ("SUCCESS", last_message_); |
| + // Note that we want to wait for the Instance to get the SUCCESS/FAIL |
| + // message here. |message_handler| is not yet registered, so the message |
| + // goes to the instance instead. |
| + pp::Var msg = WaitForMessage(); |
| + ASSERT_TRUE(msg.is_string()); |
| + ASSERT_EQ("SUCCESS", msg.AsString()); |
| } |
| handler.Register(); |
| { |
| @@ -332,8 +350,9 @@ std::string TestMessageHandler::TestExceptions() { |
| "window.webkitRequestFileSystem(\n" |
| " window.Temporary, 1024, gotFileSystem, fileSystemError)\n"); |
| instance_->EvalScript(js_code); |
| - message_received_.Wait(); |
|
dmichael (off chromium)
2014/10/24 21:06:21
I never called message_received_.Reset(), so this
|
| - ASSERT_EQ("SUCCESS", last_message_); |
| + pp::Var msg = handler.WaitForAsyncMessage(); |
| + ASSERT_EQ(PP_VARTYPE_STRING, msg.pp_var().type); |
| + ASSERT_EQ("SUCCESS", msg.AsString()); |
| } |
| handler.Unregister(); |
| ASSERT_SUBTEST_SUCCESS(handler.WaitForDestroy()); |
| @@ -341,3 +360,11 @@ std::string TestMessageHandler::TestExceptions() { |
| PASS(); |
| } |
| +pp::Var TestMessageHandler::WaitForMessage() { |
| + message_received_.Wait(); |
| + pp::Var var_to_return = last_message_; |
| + last_message_ = pp::Var(); |
| + message_received_.Reset(); |
| + return var_to_return; |
| +} |
| + |