Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_post_message.h" | 5 #include "ppapi/tests/test_post_message.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "ppapi/c/dev/ppb_testing_dev.h" | 9 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 10 #include "ppapi/c/pp_var.h" | 10 #include "ppapi/c/pp_var.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 void InvokePostMessageThreadFunc(void* user_data) { | 44 void InvokePostMessageThreadFunc(void* user_data) { |
| 45 InvokePostMessageThreadArg* arg = | 45 InvokePostMessageThreadArg* arg = |
| 46 static_cast<InvokePostMessageThreadArg*>(user_data); | 46 static_cast<InvokePostMessageThreadArg*>(user_data); |
| 47 for (int32_t i = 0; i < kMessagesToSendPerThread; ++i) | 47 for (int32_t i = 0; i < kMessagesToSendPerThread; ++i) |
| 48 arg->instance->PostMessage(arg->value_to_send); | 48 arg->instance->PostMessage(arg->value_to_send); |
| 49 delete arg; | 49 delete arg; |
| 50 } | 50 } |
| 51 | 51 |
| 52 #define FINISHED_WAITING_MESSAGE "TEST_POST_MESSAGE_FINISHED_WAITING" | |
| 53 | |
| 52 } // namespace | 54 } // namespace |
| 53 | 55 |
| 56 TestPostMessage::TestPostMessage(TestingInstance* instance) | |
| 57 : TestCase(instance), | |
| 58 finished_waiting_(false) { | |
| 59 } | |
| 60 | |
| 61 TestPostMessage::~TestPostMessage() { | |
| 62 // Remove the special listener that only responds to a FINISHED_WAITING | |
| 63 // string. See Init for where it gets added. | |
| 64 std::string js_code; | |
| 65 js_code += "var plugin = document.getElementById('plugin');" | |
| 66 "plugin.removeEventListener('message'," | |
| 67 " plugin.wait_for_messages_handler);" | |
| 68 "delete plugin.wait_for_messages_handler;"; | |
| 69 pp::Var exception; | |
| 70 instance_->ExecuteScript(js_code, &exception); | |
| 71 } | |
| 72 | |
| 54 bool TestPostMessage::Init() { | 73 bool TestPostMessage::Init() { |
| 55 return InitTestingInterface(); | 74 bool success = InitTestingInterface(); |
| 75 | |
| 76 // Set up a special listener that only responds to a FINISHED_WAITING string. | |
| 77 // This is for use by WaitForMessages. | |
| 78 std::string js_code; | |
| 79 // Note the following code is dependent on some features of test_case.html. | |
| 80 // E.g., it is assumed that the DOM element where the plugin is embedded has | |
| 81 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows | |
| 82 // us to ignore the messages that are intended for use by the testing | |
| 83 // framework itself. | |
| 84 js_code += "var plugin = document.getElementById('plugin');" | |
| 85 "var wait_for_messages_handler = function(message_event) {" | |
| 86 " if (!IsTestingMessage(message_event.data) &&" | |
| 87 " message_event.data === '" FINISHED_WAITING_MESSAGE "') {" | |
| 88 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');" | |
| 89 " }" | |
| 90 "};" | |
| 91 "plugin.addEventListener('message', wait_for_messages_handler);" | |
| 92 // Stash it on the plugin so we can remove it in the destructor. | |
| 93 "plugin.wait_for_messages_handler = wait_for_messages_handler;"; | |
| 94 pp::Var exception; | |
| 95 instance_->ExecuteScript(js_code, &exception); | |
| 96 success = success && exception.is_undefined(); | |
| 97 | |
| 98 // Set up the JavaScript message event listener to echo the data part of the | |
| 99 // message event back to us. | |
| 100 success = success && AddEchoingListener("message_event.data"); | |
| 101 message_data_.clear(); | |
| 102 // Send a message that the first test will expect to receive. This is to | |
| 103 // verify that we can send messages when the 'Instance::Init' function is on | |
| 104 // the stack. | |
| 105 instance_->PostMessage(pp::Var(kTestString)); | |
| 106 | |
| 107 return success; | |
| 56 } | 108 } |
| 57 | 109 |
| 58 void TestPostMessage::RunTest() { | 110 void TestPostMessage::RunTest() { |
| 111 // Note: SendInInit must be first, because it expects to receive a message | |
| 112 // that was sent in Init above. | |
| 113 RUN_TEST(SendInInit); | |
| 59 RUN_TEST(SendingData); | 114 RUN_TEST(SendingData); |
| 60 RUN_TEST(MessageEvent); | 115 RUN_TEST(MessageEvent); |
| 61 RUN_TEST(NoHandler); | 116 RUN_TEST(NoHandler); |
| 62 RUN_TEST(ExtraParam); | 117 RUN_TEST(ExtraParam); |
| 63 if (testing_interface_->IsOutOfProcess()) | 118 if (testing_interface_->IsOutOfProcess()) |
| 64 RUN_TEST(NonMainThread); | 119 RUN_TEST(NonMainThread); |
| 65 } | 120 } |
| 66 | 121 |
| 67 void TestPostMessage::HandleMessage(const pp::Var& message_data) { | 122 void TestPostMessage::HandleMessage(const pp::Var& message_data) { |
| 68 message_data_.push_back(message_data); | 123 if (message_data.is_string() && |
| 124 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) | |
| 125 finished_waiting_ = true; | |
| 126 else | |
| 127 message_data_.push_back(message_data); | |
| 69 testing_interface_->QuitMessageLoop(instance_->pp_instance()); | 128 testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
| 70 } | 129 } |
| 71 | 130 |
| 72 bool TestPostMessage::AddEchoingListener(const std::string& expression) { | 131 bool TestPostMessage::AddEchoingListener(const std::string& expression) { |
| 73 std::string js_code; | 132 std::string js_code; |
| 74 // Note the following code is dependent on some features of test_case.html. | 133 // Note the following code is dependent on some features of test_case.html. |
| 75 // E.g., it is assumed that the DOM element where the plugin is embedded has | 134 // E.g., it is assumed that the DOM element where the plugin is embedded has |
| 76 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows | 135 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows |
| 77 // us to ignore the messages that are intended for use by the testing | 136 // us to ignore the messages that are intended for use by the testing |
| 78 // framework itself. | 137 // framework itself. |
| 79 js_code += "var plugin = document.getElementById('plugin');" | 138 js_code += "var plugin = document.getElementById('plugin');" |
| 80 "var message_handler = function(message_event) {" | 139 "var message_handler = function(message_event) {" |
| 81 " if (!IsTestingMessage(message_event.data)) {" | 140 " if (!IsTestingMessage(message_event.data) &&" |
| 141 " !(message_event.data === '" FINISHED_WAITING_MESSAGE "')) {" | |
| 82 " plugin.postMessage("; | 142 " plugin.postMessage("; |
| 83 js_code += expression; | 143 js_code += expression; |
| 84 js_code += " );" | 144 js_code += " );" |
| 85 " }" | 145 " }" |
| 86 "};" | 146 "};" |
| 87 "plugin.addEventListener('message', message_handler);" | 147 "plugin.addEventListener('message', message_handler);" |
| 88 // Maintain an array of all event listeners, attached to the | 148 // Maintain an array of all event listeners, attached to the |
| 89 // plugin. This is so that we can easily remove them later (see | 149 // plugin. This is so that we can easily remove them later (see |
| 90 // ClearListeners()). | 150 // ClearListeners()). |
| 91 "if (!plugin.eventListeners) plugin.eventListeners = [];" | 151 "if (!plugin.eventListeners) plugin.eventListeners = [];" |
| 92 "plugin.eventListeners.push(message_handler);"; | 152 "plugin.eventListeners.push(message_handler);"; |
| 93 pp::Var exception; | 153 pp::Var exception; |
| 94 instance_->ExecuteScript(js_code, &exception); | 154 instance_->ExecuteScript(js_code, &exception); |
| 95 return exception.is_undefined(); | 155 return exception.is_undefined(); |
| 96 } | 156 } |
| 97 | 157 |
| 98 bool TestPostMessage::ClearListeners() { | 158 bool TestPostMessage::ClearListeners() { |
| 99 std::string js_code( | 159 std::string js_code( |
| 100 "var plugin = document.getElementById('plugin');" | 160 "var plugin = document.getElementById('plugin');" |
| 101 "while (plugin.eventListeners.length) {" | 161 "while (plugin.eventListeners.length) {" |
| 102 " plugin.removeEventListener('message', plugin.eventListeners.pop());" | 162 " plugin.removeEventListener('message', plugin.eventListeners.pop());" |
| 103 "}"); | 163 "}"); |
| 104 pp::Var exception; | 164 pp::Var exception; |
| 105 instance_->ExecuteScript(js_code, &exception); | 165 instance_->ExecuteScript(js_code, &exception); |
| 106 return(exception.is_undefined()); | 166 return(exception.is_undefined()); |
| 107 } | 167 } |
| 108 | 168 |
| 169 int TestPostMessage::WaitForMessages() { | |
| 170 size_t message_size_before = message_data_.size(); | |
| 171 finished_waiting_ = false; | |
| 172 // We first post a FINISHED_WAITING_MESSAGE. This should be guaranteed to | |
| 173 // come back _after_ any other incoming messages that were already pending. | |
| 174 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE)); | |
| 175 while (!finished_waiting_) | |
| 176 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
|
Paweł Hajdan Jr.
2011/09/01 18:54:43
Could you run the message loop only once please? W
| |
| 177 // Now that the FINISHED_WAITING_MESSAGE has been echoed back to us, we know | |
| 178 // that all pending messages have been slurped up. Return the number we | |
| 179 // received (which may be zero). | |
| 180 return message_data_.size() - message_size_before; | |
| 181 } | |
| 182 | |
| 183 std::string TestPostMessage::TestSendInInit() { | |
| 184 ASSERT_EQ(WaitForMessages(), 1); | |
| 185 // This test assumes Init already sent a message. | |
| 186 ASSERT_EQ(message_data_.size(), 1); | |
| 187 ASSERT_TRUE(message_data_.back().is_string()); | |
| 188 ASSERT_EQ(message_data_.back().AsString(), kTestString); | |
| 189 PASS(); | |
| 190 } | |
| 191 | |
| 109 std::string TestPostMessage::TestSendingData() { | 192 std::string TestPostMessage::TestSendingData() { |
| 110 // Set up the JavaScript message event listener to echo the data part of the | 193 // Set up the JavaScript message event listener to echo the data part of the |
| 111 // message event back to us. | 194 // message event back to us. |
| 195 ASSERT_TRUE(ClearListeners()); | |
| 112 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 196 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
| 113 | 197 |
| 114 // Test sending a message to JavaScript for each supported type. The JS sends | 198 // Test sending a message to JavaScript for each supported type. The JS sends |
| 115 // the data back to us, and we check that they match. | 199 // the data back to us, and we check that they match. |
| 116 message_data_.clear(); | 200 message_data_.clear(); |
| 117 instance_->PostMessage(pp::Var(kTestString)); | 201 instance_->PostMessage(pp::Var(kTestString)); |
| 118 // PostMessage is asynchronous, so we should not receive a response yet. | 202 // PostMessage is asynchronous, so we should not receive a response yet. |
| 119 ASSERT_EQ(message_data_.size(), 0); | 203 ASSERT_EQ(message_data_.size(), 0); |
| 120 | 204 ASSERT_EQ(WaitForMessages(), 1); |
| 121 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
| 122 ASSERT_EQ(message_data_.size(), 1); | |
| 123 ASSERT_TRUE(message_data_.back().is_string()); | 205 ASSERT_TRUE(message_data_.back().is_string()); |
| 124 ASSERT_EQ(message_data_.back().AsString(), kTestString); | 206 ASSERT_EQ(message_data_.back().AsString(), kTestString); |
| 125 | 207 |
| 126 message_data_.clear(); | 208 message_data_.clear(); |
| 127 instance_->PostMessage(pp::Var(kTestBool)); | 209 instance_->PostMessage(pp::Var(kTestBool)); |
| 128 ASSERT_EQ(message_data_.size(), 0); | 210 ASSERT_EQ(message_data_.size(), 0); |
| 129 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 211 ASSERT_EQ(WaitForMessages(), 1); |
| 130 ASSERT_EQ(message_data_.size(), 1); | |
| 131 ASSERT_TRUE(message_data_.back().is_bool()); | 212 ASSERT_TRUE(message_data_.back().is_bool()); |
| 132 ASSERT_EQ(message_data_.back().AsBool(), kTestBool); | 213 ASSERT_EQ(message_data_.back().AsBool(), kTestBool); |
| 133 | 214 |
| 134 message_data_.clear(); | 215 message_data_.clear(); |
| 135 instance_->PostMessage(pp::Var(kTestInt)); | 216 instance_->PostMessage(pp::Var(kTestInt)); |
| 136 ASSERT_EQ(message_data_.size(), 0); | 217 ASSERT_EQ(message_data_.size(), 0); |
| 137 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 218 ASSERT_EQ(WaitForMessages(), 1); |
| 138 ASSERT_EQ(message_data_.size(), 1); | |
| 139 ASSERT_TRUE(message_data_.back().is_number()); | 219 ASSERT_TRUE(message_data_.back().is_number()); |
| 140 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), | 220 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), |
| 141 static_cast<double>(kTestInt)); | 221 static_cast<double>(kTestInt)); |
| 142 | 222 |
| 143 message_data_.clear(); | 223 message_data_.clear(); |
| 144 instance_->PostMessage(pp::Var(kTestDouble)); | 224 instance_->PostMessage(pp::Var(kTestDouble)); |
| 145 ASSERT_EQ(message_data_.size(), 0); | 225 ASSERT_EQ(message_data_.size(), 0); |
| 146 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 226 ASSERT_EQ(WaitForMessages(), 1); |
| 147 ASSERT_EQ(message_data_.size(), 1); | |
| 148 ASSERT_TRUE(message_data_.back().is_number()); | 227 ASSERT_TRUE(message_data_.back().is_number()); |
| 149 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), kTestDouble); | 228 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), kTestDouble); |
| 150 | 229 |
| 151 message_data_.clear(); | 230 message_data_.clear(); |
| 152 instance_->PostMessage(pp::Var()); | 231 instance_->PostMessage(pp::Var()); |
| 153 ASSERT_EQ(message_data_.size(), 0); | 232 ASSERT_EQ(message_data_.size(), 0); |
| 154 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 233 ASSERT_EQ(WaitForMessages(), 1); |
| 155 ASSERT_EQ(message_data_.size(), 1); | |
| 156 ASSERT_TRUE(message_data_.back().is_undefined()); | 234 ASSERT_TRUE(message_data_.back().is_undefined()); |
| 157 | 235 |
| 158 message_data_.clear(); | 236 message_data_.clear(); |
| 159 instance_->PostMessage(pp::Var(pp::Var::Null())); | 237 instance_->PostMessage(pp::Var(pp::Var::Null())); |
| 160 ASSERT_EQ(message_data_.size(), 0); | 238 ASSERT_EQ(message_data_.size(), 0); |
| 161 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 239 ASSERT_EQ(WaitForMessages(), 1); |
| 162 ASSERT_EQ(message_data_.size(), 1); | |
| 163 ASSERT_TRUE(message_data_.back().is_null()); | 240 ASSERT_TRUE(message_data_.back().is_null()); |
| 164 | 241 |
| 165 ASSERT_TRUE(ClearListeners()); | 242 ASSERT_TRUE(ClearListeners()); |
| 166 | 243 |
| 167 PASS(); | 244 PASS(); |
| 168 } | 245 } |
| 169 | 246 |
| 170 std::string TestPostMessage::TestMessageEvent() { | 247 std::string TestPostMessage::TestMessageEvent() { |
| 171 // Set up the JavaScript message event listener to pass us some values from | 248 // Set up the JavaScript message event listener to pass us some values from |
| 172 // the MessageEvent and make sure they match our expectations. | 249 // the MessageEvent and make sure they match our expectations. |
| 173 | 250 |
| 174 // Have the listener pass back the type of message_event and make sure it's | 251 // Have the listener pass back the type of message_event and make sure it's |
| 175 // "object". | 252 // "object". |
| 253 ASSERT_TRUE(ClearListeners()); | |
| 176 ASSERT_TRUE(AddEchoingListener("typeof(message_event)")); | 254 ASSERT_TRUE(AddEchoingListener("typeof(message_event)")); |
| 177 message_data_.clear(); | 255 message_data_.clear(); |
| 178 instance_->PostMessage(pp::Var(kTestInt)); | 256 instance_->PostMessage(pp::Var(kTestInt)); |
| 179 ASSERT_EQ(message_data_.size(), 0); | 257 ASSERT_EQ(message_data_.size(), 0); |
| 180 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 258 ASSERT_EQ(WaitForMessages(), 1); |
| 181 ASSERT_EQ(message_data_.size(), 1); | |
| 182 ASSERT_TRUE(message_data_.back().is_string()); | 259 ASSERT_TRUE(message_data_.back().is_string()); |
| 183 ASSERT_EQ(message_data_.back().AsString(), "object"); | 260 ASSERT_EQ(message_data_.back().AsString(), "object"); |
| 184 ASSERT_TRUE(ClearListeners()); | 261 ASSERT_TRUE(ClearListeners()); |
| 185 | 262 |
| 186 // Make sure all the non-data properties have the expected values. | 263 // Make sure all the non-data properties have the expected values. |
| 187 bool success = AddEchoingListener("((message_event.origin == '')" | 264 bool success = AddEchoingListener("((message_event.origin == '')" |
| 188 " && (message_event.lastEventId == '')" | 265 " && (message_event.lastEventId == '')" |
| 189 " && (message_event.source == null)" | 266 " && (message_event.source == null)" |
| 190 " && (message_event.ports.length == 0)" | 267 " && (message_event.ports.length == 0)" |
| 191 " && (message_event.bubbles == false)" | 268 " && (message_event.bubbles == false)" |
| 192 " && (message_event.cancelable == false)" | 269 " && (message_event.cancelable == false)" |
| 193 ")"); | 270 ")"); |
| 194 ASSERT_TRUE(success); | 271 ASSERT_TRUE(success); |
| 195 message_data_.clear(); | 272 message_data_.clear(); |
| 196 instance_->PostMessage(pp::Var(kTestInt)); | 273 instance_->PostMessage(pp::Var(kTestInt)); |
| 197 ASSERT_EQ(message_data_.size(), 0); | 274 ASSERT_EQ(message_data_.size(), 0); |
| 198 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 275 ASSERT_EQ(WaitForMessages(), 1); |
| 199 ASSERT_EQ(message_data_.size(), 1); | |
| 200 ASSERT_TRUE(message_data_.back().is_bool()); | 276 ASSERT_TRUE(message_data_.back().is_bool()); |
| 201 ASSERT_TRUE(message_data_.back().AsBool()); | 277 ASSERT_TRUE(message_data_.back().AsBool()); |
| 202 ASSERT_TRUE(ClearListeners()); | 278 ASSERT_TRUE(ClearListeners()); |
| 203 | 279 |
| 204 // Add some event handlers to make sure they receive messages. | 280 // Add some event handlers to make sure they receive messages. |
| 205 ASSERT_TRUE(AddEchoingListener("1")); | 281 ASSERT_TRUE(AddEchoingListener("1")); |
| 206 ASSERT_TRUE(AddEchoingListener("2")); | 282 ASSERT_TRUE(AddEchoingListener("2")); |
| 207 ASSERT_TRUE(AddEchoingListener("3")); | 283 ASSERT_TRUE(AddEchoingListener("3")); |
| 208 | 284 |
| 209 message_data_.clear(); | 285 message_data_.clear(); |
| 210 instance_->PostMessage(pp::Var(kTestInt)); | 286 instance_->PostMessage(pp::Var(kTestInt)); |
| 211 // Make sure we don't get a response in a re-entrant fashion. | 287 // Make sure we don't get a response in a re-entrant fashion. |
| 212 ASSERT_EQ(message_data_.size(), 0); | 288 ASSERT_EQ(message_data_.size(), 0); |
| 213 // We should get 3 messages. | 289 // We should get 3 messages. |
| 214 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 290 ASSERT_EQ(WaitForMessages(), 3); |
| 215 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
| 216 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
| 217 ASSERT_EQ(message_data_.size(), 3); | |
| 218 // Copy to a vector of doubles and sort; w3c does not specify the order for | 291 // Copy to a vector of doubles and sort; w3c does not specify the order for |
| 219 // event listeners. (Copying is easier than writing an operator< for pp::Var.) | 292 // event listeners. (Copying is easier than writing an operator< for pp::Var.) |
| 220 // | 293 // |
| 221 // See http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html. | 294 // See http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html. |
| 222 VarVector::iterator iter(message_data_.begin()), the_end(message_data_.end()); | 295 VarVector::iterator iter(message_data_.begin()), the_end(message_data_.end()); |
| 223 std::vector<double> double_vec; | 296 std::vector<double> double_vec; |
| 224 for (; iter != the_end; ++iter) { | 297 for (; iter != the_end; ++iter) { |
| 225 ASSERT_TRUE(iter->is_number()); | 298 ASSERT_TRUE(iter->is_number()); |
| 226 double_vec.push_back(iter->AsDouble()); | 299 double_vec.push_back(iter->AsDouble()); |
| 227 } | 300 } |
| 228 std::sort(double_vec.begin(), double_vec.end()); | 301 std::sort(double_vec.begin(), double_vec.end()); |
| 229 ASSERT_DOUBLE_EQ(double_vec[0], 1.0); | 302 ASSERT_DOUBLE_EQ(double_vec[0], 1.0); |
| 230 ASSERT_DOUBLE_EQ(double_vec[1], 2.0); | 303 ASSERT_DOUBLE_EQ(double_vec[1], 2.0); |
| 231 ASSERT_DOUBLE_EQ(double_vec[2], 3.0); | 304 ASSERT_DOUBLE_EQ(double_vec[2], 3.0); |
| 232 | 305 |
| 233 ASSERT_TRUE(ClearListeners()); | 306 ASSERT_TRUE(ClearListeners()); |
| 234 | 307 |
| 235 PASS(); | 308 PASS(); |
| 236 } | 309 } |
| 237 | 310 |
| 238 std::string TestPostMessage::TestNoHandler() { | 311 std::string TestPostMessage::TestNoHandler() { |
| 239 // Delete any lingering event listeners. | 312 // Delete any lingering event listeners. |
| 240 ASSERT_TRUE(ClearListeners()); | 313 ASSERT_TRUE(ClearListeners()); |
| 241 | 314 |
| 242 // Now send a message. We shouldn't get a response. | 315 // Now send a message. We shouldn't get a response. |
| 243 message_data_.clear(); | 316 message_data_.clear(); |
| 244 instance_->PostMessage(pp::Var()); | 317 instance_->PostMessage(pp::Var()); |
| 245 // Note that at this point, if we call RunMessageLoop, we should hang, because | 318 ASSERT_EQ(WaitForMessages(), 0); |
| 246 // there should be no call to our HandleMessage function to quit the loop. | |
| 247 // Therefore, we will do CallOnMainThread to yield control. That event should | |
| 248 // fire, but we should see no messages when we return. | |
| 249 TestCompletionCallback callback(instance_->pp_instance()); | |
| 250 pp::Module::Get()->core()->CallOnMainThread(0, callback); | |
| 251 callback.WaitForResult(); | |
| 252 ASSERT_TRUE(message_data_.empty()); | 319 ASSERT_TRUE(message_data_.empty()); |
| 253 | 320 |
| 254 PASS(); | 321 PASS(); |
| 255 } | 322 } |
| 256 | 323 |
| 257 std::string TestPostMessage::TestExtraParam() { | 324 std::string TestPostMessage::TestExtraParam() { |
| 258 // Delete any lingering event listeners. | 325 // Delete any lingering event listeners. |
| 259 ASSERT_TRUE(ClearListeners()); | 326 ASSERT_TRUE(ClearListeners()); |
| 260 // Add a listener that will respond with 1 and an empty array (where the | 327 // Add a listener that will respond with 1 and an empty array (where the |
| 261 // message port array would appear if it was Worker postMessage). | 328 // message port array would appear if it was Worker postMessage). |
| 262 ASSERT_TRUE(AddEchoingListener("1, []")); | 329 ASSERT_TRUE(AddEchoingListener("1, []")); |
| 263 | 330 |
| 264 // Now send a message. We shouldn't get a response. | 331 // Now send a message. We shouldn't get a response. |
| 265 message_data_.clear(); | 332 message_data_.clear(); |
| 266 instance_->PostMessage(pp::Var()); | 333 instance_->PostMessage(pp::Var()); |
| 267 // Note that at this point, if we call RunMessageLoop, we should hang, because | 334 ASSERT_EQ(WaitForMessages(), 0); |
| 268 // there should be no call to our HandleMessage function to quit the loop. | |
| 269 // Therefore, we will do CallOnMainThread to yield control. That event should | |
| 270 // fire, but we should see no messages when we return. | |
| 271 TestCompletionCallback callback(instance_->pp_instance()); | |
| 272 pp::Module::Get()->core()->CallOnMainThread(0, callback); | |
| 273 callback.WaitForResult(); | |
| 274 ASSERT_TRUE(message_data_.empty()); | 335 ASSERT_TRUE(message_data_.empty()); |
| 275 | 336 |
| 276 PASS(); | 337 PASS(); |
| 277 } | 338 } |
| 278 | 339 |
| 279 std::string TestPostMessage::TestNonMainThread() { | 340 std::string TestPostMessage::TestNonMainThread() { |
| 280 ASSERT_TRUE(ClearListeners()); | 341 ASSERT_TRUE(ClearListeners()); |
| 281 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 342 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
| 282 message_data_.clear(); | 343 message_data_.clear(); |
| 283 | 344 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 305 | 366 |
| 306 // Make sure we got all values that we expected. Note that because it's legal | 367 // Make sure we got all values that we expected. Note that because it's legal |
| 307 // for the JavaScript engine to treat our integers as floating points, we | 368 // for the JavaScript engine to treat our integers as floating points, we |
| 308 // can't just use std::find or equality comparison. So we instead, we convert | 369 // can't just use std::find or equality comparison. So we instead, we convert |
| 309 // each incoming value to an integer, and count them in received_counts. | 370 // each incoming value to an integer, and count them in received_counts. |
| 310 int32_t expected_num = (kThreadsToRun + 1) * kMessagesToSendPerThread; | 371 int32_t expected_num = (kThreadsToRun + 1) * kMessagesToSendPerThread; |
| 311 // Count how many we receive per-index. | 372 // Count how many we receive per-index. |
| 312 std::vector<int32_t> expected_counts(kThreadsToRun + 1, | 373 std::vector<int32_t> expected_counts(kThreadsToRun + 1, |
| 313 kMessagesToSendPerThread); | 374 kMessagesToSendPerThread); |
| 314 std::vector<int32_t> received_counts(kThreadsToRun + 1, 0); | 375 std::vector<int32_t> received_counts(kThreadsToRun + 1, 0); |
| 376 ASSERT_EQ(WaitForMessages(), expected_num); | |
| 315 for (int32_t i = 0; i < expected_num; ++i) { | 377 for (int32_t i = 0; i < expected_num; ++i) { |
| 316 // Run the message loop to get the next expected message. | 378 const pp::Var& latest_var(message_data_[i]); |
| 317 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
| 318 // Make sure we got another message in. | |
| 319 ASSERT_EQ(message_data_.size(), 1); | |
| 320 pp::Var latest_var(message_data_.back()); | |
| 321 message_data_.clear(); | |
| 322 | |
| 323 ASSERT_TRUE(latest_var.is_int() || latest_var.is_double()); | 379 ASSERT_TRUE(latest_var.is_int() || latest_var.is_double()); |
| 324 int32_t received_value = -1; | 380 int32_t received_value = -1; |
| 325 if (latest_var.is_int()) { | 381 if (latest_var.is_int()) { |
| 326 received_value = latest_var.AsInt(); | 382 received_value = latest_var.AsInt(); |
| 327 } else if (latest_var.is_double()) { | 383 } else if (latest_var.is_double()) { |
| 328 received_value = static_cast<int32_t>(latest_var.AsDouble() + 0.5); | 384 received_value = static_cast<int32_t>(latest_var.AsDouble() + 0.5); |
| 329 } | 385 } |
| 330 ASSERT_TRUE(received_value >= 0); | 386 ASSERT_TRUE(received_value >= 0); |
| 331 ASSERT_TRUE(received_value <= kThreadsToRun); | 387 ASSERT_TRUE(received_value <= kThreadsToRun); |
| 332 ++received_counts[received_value]; | 388 ++received_counts[received_value]; |
| 333 } | 389 } |
| 334 ASSERT_EQ(received_counts, expected_counts); | 390 ASSERT_EQ(received_counts, expected_counts); |
| 335 | 391 |
| 336 PASS(); | 392 PASS(); |
| 337 } | 393 } |
| 338 | 394 |
| OLD | NEW |