| 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" |
| 11 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" | 11 #include "ppapi/cpp/dev/var_array_buffer_dev.h" |
| 12 #include "ppapi/cpp/instance.h" | 12 #include "ppapi/cpp/instance.h" |
| 13 #include "ppapi/cpp/var.h" | 13 #include "ppapi/cpp/var.h" |
| 14 #include "ppapi/tests/pp_thread.h" | 14 #include "ppapi/tests/pp_thread.h" |
| 15 #include "ppapi/tests/test_utils.h" | 15 #include "ppapi/tests/test_utils.h" |
| 16 #include "ppapi/tests/testing_instance.h" | 16 #include "ppapi/tests/testing_instance.h" |
| 17 | 17 |
| 18 // Windows defines 'PostMessage', so we have to undef it. | 18 // Windows defines 'PostMessage', so we have to undef it. |
| 19 #ifdef PostMessage | 19 #ifdef PostMessage |
| 20 #undef PostMessage | 20 #undef PostMessage |
| 21 #endif | 21 #endif |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 104 instance_->PostMessage(pp::Var(kTestString)); | 104 instance_->PostMessage(pp::Var(kTestString)); |
| 105 | 105 |
| 106 return success; | 106 return success; |
| 107 } | 107 } |
| 108 | 108 |
| 109 void TestPostMessage::RunTests(const std::string& filter) { | 109 void TestPostMessage::RunTests(const std::string& filter) { |
| 110 // Note: SendInInit must be first, because it expects to receive a message | 110 // Note: SendInInit must be first, because it expects to receive a message |
| 111 // that was sent in Init above. | 111 // that was sent in Init above. |
| 112 RUN_TEST(SendInInit, filter); | 112 RUN_TEST(SendInInit, filter); |
| 113 RUN_TEST(SendingData, filter); | 113 RUN_TEST(SendingData, filter); |
| 114 RUN_TEST(SendingArrayBuffer, filter); |
| 114 RUN_TEST(MessageEvent, filter); | 115 RUN_TEST(MessageEvent, filter); |
| 115 RUN_TEST(NoHandler, filter); | 116 RUN_TEST(NoHandler, filter); |
| 116 RUN_TEST(ExtraParam, filter); | 117 RUN_TEST(ExtraParam, filter); |
| 117 if (testing_interface_->IsOutOfProcess()) | 118 if (testing_interface_->IsOutOfProcess()) |
| 118 RUN_TEST(NonMainThread, filter); | 119 RUN_TEST(NonMainThread, filter); |
| 119 } | 120 } |
| 120 | 121 |
| 121 void TestPostMessage::HandleMessage(const pp::Var& message_data) { | 122 void TestPostMessage::HandleMessage(const pp::Var& message_data) { |
| 122 if (message_data.is_string() && | 123 if (message_data.is_string() && |
| 123 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) | 124 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 instance_->PostMessage(pp::Var(pp::Var::Null())); | 238 instance_->PostMessage(pp::Var(pp::Var::Null())); |
| 238 ASSERT_EQ(message_data_.size(), 0); | 239 ASSERT_EQ(message_data_.size(), 0); |
| 239 ASSERT_EQ(WaitForMessages(), 1); | 240 ASSERT_EQ(WaitForMessages(), 1); |
| 240 ASSERT_TRUE(message_data_.back().is_null()); | 241 ASSERT_TRUE(message_data_.back().is_null()); |
| 241 | 242 |
| 242 ASSERT_TRUE(ClearListeners()); | 243 ASSERT_TRUE(ClearListeners()); |
| 243 | 244 |
| 244 PASS(); | 245 PASS(); |
| 245 } | 246 } |
| 246 | 247 |
| 248 std::string TestPostMessage::TestSendingArrayBuffer() { |
| 249 // Clean up after previous tests. This also swallows the message sent by Init |
| 250 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
| 251 // should start with these. |
| 252 WaitForMessages(); |
| 253 ASSERT_TRUE(ClearListeners()); |
| 254 |
| 255 // Create a 100-byte array buffer with test_data[i] == i. |
| 256 pp::VarArrayBuffer_Dev test_data(100u); |
| 257 ASSERT_NE(NULL, test_data.Map()); |
| 258 ASSERT_EQ(100u, test_data.ByteLength()); |
| 259 unsigned char* buff = static_cast<unsigned char*>(test_data.Map()); |
| 260 for (size_t i = 0; i < test_data.ByteLength(); ++i) |
| 261 buff[i] = i; |
| 262 |
| 263 // Have the listener test some properties of the ArrayBuffer. |
| 264 const char* const properties_to_check[] = { |
| 265 "message_event.data.constructor.name === 'ArrayBuffer'", |
| 266 "message_event.data.byteLength === 100", |
| 267 "(new DataView(message_event.data)).getInt8(0) == 0", |
| 268 "(new DataView(message_event.data)).getInt8(99) == 99", |
| 269 NULL}; |
| 270 for (size_t i = 0; properties_to_check[i]; ++i) { |
| 271 ASSERT_TRUE(AddEchoingListener(properties_to_check[i])); |
| 272 message_data_.clear(); |
| 273 instance_->PostMessage(test_data); |
| 274 ASSERT_EQ(message_data_.size(), 0); |
| 275 ASSERT_EQ(WaitForMessages(), 1); |
| 276 ASSERT_TRUE(message_data_.back().is_bool()); |
| 277 if (!message_data_.back().AsBool()) |
| 278 return std::string("Failed: ") + properties_to_check[i]; |
| 279 ASSERT_TRUE(message_data_.back().AsBool()); |
| 280 ASSERT_TRUE(ClearListeners()); |
| 281 } |
| 282 |
| 283 // Set up the JavaScript message event listener to echo the data part of the |
| 284 // message event back to us. |
| 285 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
| 286 message_data_.clear(); |
| 287 instance_->PostMessage(test_data); |
| 288 // PostMessage is asynchronous, so we should not receive a response yet. |
| 289 ASSERT_EQ(message_data_.size(), 0); |
| 290 ASSERT_EQ(WaitForMessages(), 1); |
| 291 ASSERT_TRUE(message_data_.back().is_array_buffer()); |
| 292 pp::VarArrayBuffer_Dev received(message_data_.back()); |
| 293 message_data_.clear(); |
| 294 ASSERT_EQ(test_data.ByteLength(), received.ByteLength()); |
| 295 unsigned char* received_buff = static_cast<unsigned char*>(received.Map()); |
| 296 // The buffer should be copied, so this should be a distinct buffer. When |
| 297 // 'transferrables' are implemented for PPAPI, we'll also want to test that |
| 298 // we get the _same_ buffer back when it's transferred. |
| 299 ASSERT_NE(buff, received_buff); |
| 300 for (size_t i = 0; i < test_data.ByteLength(); ++i) |
| 301 ASSERT_EQ(buff[i], received_buff[i]); |
| 302 |
| 303 ASSERT_TRUE(ClearListeners()); |
| 304 |
| 305 PASS(); |
| 306 } |
| 307 |
| 247 std::string TestPostMessage::TestMessageEvent() { | 308 std::string TestPostMessage::TestMessageEvent() { |
| 248 // Set up the JavaScript message event listener to pass us some values from | 309 // Set up the JavaScript message event listener to pass us some values from |
| 249 // the MessageEvent and make sure they match our expectations. | 310 // the MessageEvent and make sure they match our expectations. |
| 250 | 311 |
| 251 WaitForMessages(); | 312 WaitForMessages(); |
| 252 ASSERT_TRUE(ClearListeners()); | 313 ASSERT_TRUE(ClearListeners()); |
| 253 // Have the listener pass back the type of message_event and make sure it's | 314 // Have the listener pass back the class name of message_event and make sure |
| 254 // "object". | 315 // it's "MessageEvent". |
| 255 ASSERT_TRUE(AddEchoingListener("typeof(message_event)")); | 316 ASSERT_TRUE(AddEchoingListener("message_event.constructor.name")); |
| 256 message_data_.clear(); | 317 message_data_.clear(); |
| 257 instance_->PostMessage(pp::Var(kTestInt)); | 318 instance_->PostMessage(pp::Var(kTestInt)); |
| 258 ASSERT_EQ(message_data_.size(), 0); | 319 ASSERT_EQ(message_data_.size(), 0); |
| 259 ASSERT_EQ(WaitForMessages(), 1); | 320 ASSERT_EQ(WaitForMessages(), 1); |
| 260 ASSERT_TRUE(message_data_.back().is_string()); | 321 ASSERT_TRUE(message_data_.back().is_string()); |
| 261 ASSERT_EQ(message_data_.back().AsString(), "object"); | 322 ASSERT_EQ(message_data_.back().AsString(), "MessageEvent"); |
| 262 ASSERT_TRUE(ClearListeners()); | 323 ASSERT_TRUE(ClearListeners()); |
| 263 | 324 |
| 264 // Make sure all the non-data properties have the expected values. | 325 // Make sure all the non-data properties have the expected values. |
| 265 bool success = AddEchoingListener("((message_event.origin == '')" | 326 bool success = AddEchoingListener("((message_event.origin === '')" |
| 266 " && (message_event.lastEventId == '')" | 327 " && (message_event.lastEventId === '')" |
| 267 " && (message_event.source == null)" | 328 " && (message_event.source === null)" |
| 268 " && (message_event.ports.length == 0)" | 329 " && (message_event.ports.length === 0)" |
| 269 " && (message_event.bubbles == false)" | 330 " && (message_event.bubbles === false)" |
| 270 " && (message_event.cancelable == false)" | 331 " && (message_event.cancelable === false)" |
| 271 ")"); | 332 ")"); |
| 272 ASSERT_TRUE(success); | 333 ASSERT_TRUE(success); |
| 273 message_data_.clear(); | 334 message_data_.clear(); |
| 274 instance_->PostMessage(pp::Var(kTestInt)); | 335 instance_->PostMessage(pp::Var(kTestInt)); |
| 275 ASSERT_EQ(message_data_.size(), 0); | 336 ASSERT_EQ(message_data_.size(), 0); |
| 276 ASSERT_EQ(WaitForMessages(), 1); | 337 ASSERT_EQ(WaitForMessages(), 1); |
| 277 ASSERT_TRUE(message_data_.back().is_bool()); | 338 ASSERT_TRUE(message_data_.back().is_bool()); |
| 278 ASSERT_TRUE(message_data_.back().AsBool()); | 339 ASSERT_TRUE(message_data_.back().AsBool()); |
| 279 ASSERT_TRUE(ClearListeners()); | 340 ASSERT_TRUE(ClearListeners()); |
| 280 | 341 |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 } | 448 } |
| 388 ASSERT_TRUE(received_value >= 0); | 449 ASSERT_TRUE(received_value >= 0); |
| 389 ASSERT_TRUE(received_value <= kThreadsToRun); | 450 ASSERT_TRUE(received_value <= kThreadsToRun); |
| 390 ++received_counts[received_value]; | 451 ++received_counts[received_value]; |
| 391 } | 452 } |
| 392 ASSERT_EQ(received_counts, expected_counts); | 453 ASSERT_EQ(received_counts, expected_counts); |
| 393 | 454 |
| 394 PASS(); | 455 PASS(); |
| 395 } | 456 } |
| 396 | 457 |
| OLD | NEW |