| 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 #include <sstream> |
| 8 | 9 |
| 9 #include "ppapi/c/dev/ppb_testing_dev.h" | 10 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 10 #include "ppapi/c/pp_var.h" | 11 #include "ppapi/c/pp_var.h" |
| 11 #include "ppapi/cpp/dev/var_array_buffer_dev.h" | 12 #include "ppapi/cpp/dev/var_array_buffer_dev.h" |
| 12 #include "ppapi/cpp/instance.h" | 13 #include "ppapi/cpp/instance.h" |
| 13 #include "ppapi/cpp/var.h" | 14 #include "ppapi/cpp/var.h" |
| 14 #include "ppapi/tests/pp_thread.h" | 15 #include "ppapi/tests/pp_thread.h" |
| 15 #include "ppapi/tests/test_utils.h" | 16 #include "ppapi/tests/test_utils.h" |
| 16 #include "ppapi/tests/testing_instance.h" | 17 #include "ppapi/tests/testing_instance.h" |
| 17 | 18 |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 PASS(); | 244 PASS(); |
| 244 } | 245 } |
| 245 | 246 |
| 246 std::string TestPostMessage::TestSendingArrayBuffer() { | 247 std::string TestPostMessage::TestSendingArrayBuffer() { |
| 247 // Clean up after previous tests. This also swallows the message sent by Init | 248 // Clean up after previous tests. This also swallows the message sent by Init |
| 248 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' | 249 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
| 249 // should start with these. | 250 // should start with these. |
| 250 WaitForMessages(); | 251 WaitForMessages(); |
| 251 ASSERT_TRUE(ClearListeners()); | 252 ASSERT_TRUE(ClearListeners()); |
| 252 | 253 |
| 253 // Create a 100-byte array buffer with test_data[i] == i. | 254 // TODO(dmichael): Add testing of longer array buffers when crbug.com/106266 |
| 254 pp::VarArrayBuffer_Dev test_data(100u); | 255 // is fixed. |
| 255 ASSERT_NE(NULL, test_data.Map()); | 256 uint32_t sizes[] = { 0, 100, 1000 }; |
| 256 ASSERT_EQ(100u, test_data.ByteLength()); | 257 for (size_t i = 0; i < sizeof(sizes)/sizeof(sizes[i]); ++i) { |
| 257 unsigned char* buff = static_cast<unsigned char*>(test_data.Map()); | 258 std::ostringstream size_stream; |
| 258 for (size_t i = 0; i < test_data.ByteLength(); ++i) | 259 size_stream << sizes[i]; |
| 259 buff[i] = i; | 260 const std::string kSizeAsString(size_stream.str()); |
| 260 | 261 |
| 261 // Have the listener test some properties of the ArrayBuffer. | 262 // Create an appropriately sized array buffer with test_data[i] == i. |
| 262 const char* const properties_to_check[] = { | 263 pp::VarArrayBuffer_Dev test_data(sizes[i]); |
| 263 "message_event.data.constructor.name === 'ArrayBuffer'", | 264 if (sizes[i] > 0) |
| 264 "message_event.data.byteLength === 100", | 265 ASSERT_NE(NULL, test_data.Map()); |
| 265 "(new DataView(message_event.data)).getInt8(0) == 0", | 266 ASSERT_EQ(sizes[i], test_data.ByteLength()); |
| 266 "(new DataView(message_event.data)).getInt8(99) == 99", | 267 unsigned char* buff = static_cast<unsigned char*>(test_data.Map()); |
| 267 NULL}; | 268 const uint32_t kByteLength = test_data.ByteLength(); |
| 268 for (size_t i = 0; properties_to_check[i]; ++i) { | 269 for (size_t j = 0; j < kByteLength; ++j) |
| 269 ASSERT_TRUE(AddEchoingListener(properties_to_check[i])); | 270 buff[j] = static_cast<uint8_t>(j % 256u); |
| 271 |
| 272 // Have the listener test some properties of the ArrayBuffer. |
| 273 std::vector<std::string> properties_to_check; |
| 274 properties_to_check.push_back( |
| 275 "message_event.data.constructor.name === 'ArrayBuffer'"); |
| 276 properties_to_check.push_back( |
| 277 std::string("message_event.data.byteLength === ") + kSizeAsString); |
| 278 if (sizes[i] > 0) { |
| 279 properties_to_check.push_back( |
| 280 "(new DataView(message_event.data)).getUint8(0) == 0"); |
| 281 // Checks that the last element has the right value: (byteLength-1)%256. |
| 282 std::string received_byte("(new DataView(message_event.data)).getUint8(" |
| 283 " message_event.data.byteLength-1)"); |
| 284 std::string expected_byte("(message_event.data.byteLength-1)%256"); |
| 285 properties_to_check.push_back(received_byte + " == " + expected_byte); |
| 286 } |
| 287 for (std::vector<std::string>::iterator iter = properties_to_check.begin(); |
| 288 iter != properties_to_check.end(); |
| 289 ++iter) { |
| 290 ASSERT_TRUE(AddEchoingListener(*iter)); |
| 291 message_data_.clear(); |
| 292 instance_->PostMessage(test_data); |
| 293 ASSERT_EQ(message_data_.size(), 0); |
| 294 ASSERT_EQ(WaitForMessages(), 1); |
| 295 ASSERT_TRUE(message_data_.back().is_bool()); |
| 296 if (!message_data_.back().AsBool()) |
| 297 return std::string("Failed: ") + *iter + ", size: " + kSizeAsString; |
| 298 ASSERT_TRUE(message_data_.back().AsBool()); |
| 299 ASSERT_TRUE(ClearListeners()); |
| 300 } |
| 301 |
| 302 // Set up the JavaScript message event listener to echo the data part of the |
| 303 // message event back to us. |
| 304 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
| 270 message_data_.clear(); | 305 message_data_.clear(); |
| 271 instance_->PostMessage(test_data); | 306 instance_->PostMessage(test_data); |
| 307 // PostMessage is asynchronous, so we should not receive a response yet. |
| 272 ASSERT_EQ(message_data_.size(), 0); | 308 ASSERT_EQ(message_data_.size(), 0); |
| 273 ASSERT_EQ(WaitForMessages(), 1); | 309 ASSERT_EQ(WaitForMessages(), 1); |
| 274 ASSERT_TRUE(message_data_.back().is_bool()); | 310 ASSERT_TRUE(message_data_.back().is_array_buffer()); |
| 275 if (!message_data_.back().AsBool()) | 311 pp::VarArrayBuffer_Dev received(message_data_.back()); |
| 276 return std::string("Failed: ") + properties_to_check[i]; | 312 message_data_.clear(); |
| 277 ASSERT_TRUE(message_data_.back().AsBool()); | 313 ASSERT_EQ(test_data.ByteLength(), received.ByteLength()); |
| 314 unsigned char* received_buff = static_cast<unsigned char*>(received.Map()); |
| 315 // The buffer should be copied, so this should be a distinct buffer. When |
| 316 // 'transferrables' are implemented for PPAPI, we'll also want to test that |
| 317 // we get the _same_ buffer back when it's transferred. |
| 318 if (sizes[i] > 0) |
| 319 ASSERT_NE(buff, received_buff); |
| 320 for (size_t i = 0; i < test_data.ByteLength(); ++i) |
| 321 ASSERT_EQ(buff[i], received_buff[i]); |
| 322 |
| 323 message_data_.clear(); |
| 278 ASSERT_TRUE(ClearListeners()); | 324 ASSERT_TRUE(ClearListeners()); |
| 279 } | 325 } |
| 280 | 326 |
| 281 // Set up the JavaScript message event listener to echo the data part of the | |
| 282 // message event back to us. | |
| 283 ASSERT_TRUE(AddEchoingListener("message_event.data")); | |
| 284 message_data_.clear(); | |
| 285 instance_->PostMessage(test_data); | |
| 286 // PostMessage is asynchronous, so we should not receive a response yet. | |
| 287 ASSERT_EQ(message_data_.size(), 0); | |
| 288 ASSERT_EQ(WaitForMessages(), 1); | |
| 289 ASSERT_TRUE(message_data_.back().is_array_buffer()); | |
| 290 pp::VarArrayBuffer_Dev received(message_data_.back()); | |
| 291 message_data_.clear(); | |
| 292 ASSERT_EQ(test_data.ByteLength(), received.ByteLength()); | |
| 293 unsigned char* received_buff = static_cast<unsigned char*>(received.Map()); | |
| 294 // The buffer should be copied, so this should be a distinct buffer. When | |
| 295 // 'transferrables' are implemented for PPAPI, we'll also want to test that | |
| 296 // we get the _same_ buffer back when it's transferred. | |
| 297 ASSERT_NE(buff, received_buff); | |
| 298 for (size_t i = 0; i < test_data.ByteLength(); ++i) | |
| 299 ASSERT_EQ(buff[i], received_buff[i]); | |
| 300 | |
| 301 message_data_.clear(); | |
| 302 ASSERT_TRUE(ClearListeners()); | |
| 303 | |
| 304 PASS(); | 327 PASS(); |
| 305 } | 328 } |
| 306 | 329 |
| 307 std::string TestPostMessage::TestMessageEvent() { | 330 std::string TestPostMessage::TestMessageEvent() { |
| 308 // Set up the JavaScript message event listener to pass us some values from | 331 // Set up the JavaScript message event listener to pass us some values from |
| 309 // the MessageEvent and make sure they match our expectations. | 332 // the MessageEvent and make sure they match our expectations. |
| 310 | 333 |
| 311 WaitForMessages(); | 334 WaitForMessages(); |
| 312 ASSERT_TRUE(ClearListeners()); | 335 ASSERT_TRUE(ClearListeners()); |
| 313 // Have the listener pass back the class name of message_event and make sure | 336 // Have the listener pass back the class name of message_event and make sure |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 455 ++received_counts[received_value]; | 478 ++received_counts[received_value]; |
| 456 } | 479 } |
| 457 ASSERT_EQ(received_counts, expected_counts); | 480 ASSERT_EQ(received_counts, expected_counts); |
| 458 | 481 |
| 459 message_data_.clear(); | 482 message_data_.clear(); |
| 460 ASSERT_TRUE(ClearListeners()); | 483 ASSERT_TRUE(ClearListeners()); |
| 461 | 484 |
| 462 PASS(); | 485 PASS(); |
| 463 } | 486 } |
| 464 | 487 |
| OLD | NEW |