| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <map> | 8 #include <map> |
| 9 #include <sstream> | 9 #include <sstream> |
| 10 | 10 |
| 11 #include "ppapi/c/dev/ppb_testing_dev.h" | 11 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 12 #include "ppapi/c/pp_var.h" | 12 #include "ppapi/c/pp_var.h" |
| 13 #include "ppapi/cpp/instance.h" | 13 #include "ppapi/cpp/instance.h" |
| 14 #include "ppapi/cpp/var.h" | 14 #include "ppapi/cpp/var.h" |
| 15 #include "ppapi/cpp/var_array.h" |
| 15 #include "ppapi/cpp/var_array_buffer.h" | 16 #include "ppapi/cpp/var_array_buffer.h" |
| 17 #include "ppapi/cpp/var_dictionary.h" |
| 16 #include "ppapi/tests/pp_thread.h" | 18 #include "ppapi/tests/pp_thread.h" |
| 17 #include "ppapi/tests/test_utils.h" | 19 #include "ppapi/tests/test_utils.h" |
| 18 #include "ppapi/tests/testing_instance.h" | 20 #include "ppapi/tests/testing_instance.h" |
| 19 | 21 |
| 20 // Windows defines 'PostMessage', so we have to undef it. | 22 // Windows defines 'PostMessage', so we have to undef it. |
| 21 #ifdef PostMessage | 23 #ifdef PostMessage |
| 22 #undef PostMessage | 24 #undef PostMessage |
| 23 #endif | 25 #endif |
| 24 | 26 |
| 25 REGISTER_TEST_CASE(PostMessage); | 27 REGISTER_TEST_CASE(PostMessage); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 if (it->second == actual.pp_var().value.as_id) | 77 if (it->second == actual.pp_var().value.as_id) |
| 76 return true; | 78 return true; |
| 77 return false; | 79 return false; |
| 78 } | 80 } |
| 79 (*visited_ids)[expected.pp_var().value.as_id] = actual.pp_var().value.as_id; | 81 (*visited_ids)[expected.pp_var().value.as_id] = actual.pp_var().value.as_id; |
| 80 } | 82 } |
| 81 | 83 |
| 82 if (expected.is_number()) { | 84 if (expected.is_number()) { |
| 83 return fabs(expected.AsDouble() - actual.AsDouble()) < 1.0e-4; | 85 return fabs(expected.AsDouble() - actual.AsDouble()) < 1.0e-4; |
| 84 } else if (expected.is_array()) { | 86 } else if (expected.is_array()) { |
| 85 pp::VarArray_Dev expected_array(expected); | 87 pp::VarArray expected_array(expected); |
| 86 pp::VarArray_Dev actual_array(actual); | 88 pp::VarArray actual_array(actual); |
| 87 if (expected_array.GetLength() != actual_array.GetLength()) | 89 if (expected_array.GetLength() != actual_array.GetLength()) |
| 88 return false; | 90 return false; |
| 89 for (uint32_t i = 0; i < expected_array.GetLength(); ++i) { | 91 for (uint32_t i = 0; i < expected_array.GetLength(); ++i) { |
| 90 if (!VarsEqual(expected_array.Get(i), actual_array.Get(i), visited_ids)) | 92 if (!VarsEqual(expected_array.Get(i), actual_array.Get(i), visited_ids)) |
| 91 return false; | 93 return false; |
| 92 } | 94 } |
| 93 return true; | 95 return true; |
| 94 } else if (expected.is_dictionary()) { | 96 } else if (expected.is_dictionary()) { |
| 95 pp::VarDictionary_Dev expected_dict(expected); | 97 pp::VarDictionary expected_dict(expected); |
| 96 pp::VarDictionary_Dev actual_dict(actual); | 98 pp::VarDictionary actual_dict(actual); |
| 97 if (expected_dict.GetKeys().GetLength() != | 99 if (expected_dict.GetKeys().GetLength() != |
| 98 actual_dict.GetKeys().GetLength()) { | 100 actual_dict.GetKeys().GetLength()) { |
| 99 return false; | 101 return false; |
| 100 } | 102 } |
| 101 for (uint32_t i = 0; i < expected_dict.GetKeys().GetLength(); ++i) { | 103 for (uint32_t i = 0; i < expected_dict.GetKeys().GetLength(); ++i) { |
| 102 pp::Var key = expected_dict.GetKeys().Get(i); | 104 pp::Var key = expected_dict.GetKeys().Get(i); |
| 103 if (actual_dict.HasKey(key) == PP_FALSE) | 105 if (!actual_dict.HasKey(key)) |
| 104 return false; | 106 return false; |
| 105 if (!VarsEqual(expected_dict.Get(key), actual_dict.Get(key), visited_ids)) | 107 if (!VarsEqual(expected_dict.Get(key), actual_dict.Get(key), visited_ids)) |
| 106 return false; | 108 return false; |
| 107 } | 109 } |
| 108 return true; | 110 return true; |
| 109 } else { | 111 } else { |
| 110 return expected == actual; | 112 return expected == actual; |
| 111 } | 113 } |
| 112 } | 114 } |
| 113 | 115 |
| (...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 441 PASS(); | 443 PASS(); |
| 442 } | 444 } |
| 443 | 445 |
| 444 std::string TestPostMessage::TestSendingArray() { | 446 std::string TestPostMessage::TestSendingArray() { |
| 445 // Clean up after previous tests. This also swallows the message sent by Init | 447 // Clean up after previous tests. This also swallows the message sent by Init |
| 446 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' | 448 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
| 447 // should start with these. | 449 // should start with these. |
| 448 WaitForMessages(); | 450 WaitForMessages(); |
| 449 ASSERT_TRUE(ClearListeners()); | 451 ASSERT_TRUE(ClearListeners()); |
| 450 | 452 |
| 451 pp::VarArray_Dev array; | 453 pp::VarArray array; |
| 452 array.Set(0, pp::Var(kTestBool)); | 454 array.Set(0, pp::Var(kTestBool)); |
| 453 array.Set(1, pp::Var(kTestString)); | 455 array.Set(1, pp::Var(kTestString)); |
| 454 // Purposely leave index 2 empty. | 456 // Purposely leave index 2 empty. |
| 455 array.Set(3, pp::Var(kTestInt)); | 457 array.Set(3, pp::Var(kTestInt)); |
| 456 array.Set(4, pp::Var(kTestDouble)); | 458 array.Set(4, pp::Var(kTestDouble)); |
| 457 | 459 |
| 458 std::stringstream ss; | 460 std::stringstream ss; |
| 459 ss << array.GetLength(); | 461 ss << array.GetLength(); |
| 460 std::string length_as_string(ss.str()); | 462 std::string length_as_string(ss.str()); |
| 461 | 463 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 484 PASS(); | 486 PASS(); |
| 485 } | 487 } |
| 486 | 488 |
| 487 std::string TestPostMessage::TestSendingDictionary() { | 489 std::string TestPostMessage::TestSendingDictionary() { |
| 488 // Clean up after previous tests. This also swallows the message sent by Init | 490 // Clean up after previous tests. This also swallows the message sent by Init |
| 489 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' | 491 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
| 490 // should start with these. | 492 // should start with these. |
| 491 WaitForMessages(); | 493 WaitForMessages(); |
| 492 ASSERT_TRUE(ClearListeners()); | 494 ASSERT_TRUE(ClearListeners()); |
| 493 | 495 |
| 494 pp::VarDictionary_Dev dictionary; | 496 pp::VarDictionary dictionary; |
| 495 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); | 497 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); |
| 496 dictionary.Set(pp::Var("bar"), pp::Var(kTestString)); | 498 dictionary.Set(pp::Var("bar"), pp::Var(kTestString)); |
| 497 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); | 499 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); |
| 498 dictionary.Set(pp::Var("def"), pp::Var()); | 500 dictionary.Set(pp::Var("def"), pp::Var()); |
| 499 | 501 |
| 500 std::stringstream ss; | 502 std::stringstream ss; |
| 501 ss << dictionary.GetKeys().GetLength(); | 503 ss << dictionary.GetKeys().GetLength(); |
| 502 std::string length_as_string(ss.str()); | 504 std::string length_as_string(ss.str()); |
| 503 | 505 |
| 504 // Have the listener test some properties of the Dictionary. | 506 // Have the listener test some properties of the Dictionary. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 530 | 532 |
| 531 std::string TestPostMessage::TestSendingComplexVar() { | 533 std::string TestPostMessage::TestSendingComplexVar() { |
| 532 // Clean up after previous tests. This also swallows the message sent by Init | 534 // Clean up after previous tests. This also swallows the message sent by Init |
| 533 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' | 535 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
| 534 // should start with these. | 536 // should start with these. |
| 535 WaitForMessages(); | 537 WaitForMessages(); |
| 536 message_data_.clear(); | 538 message_data_.clear(); |
| 537 ASSERT_TRUE(ClearListeners()); | 539 ASSERT_TRUE(ClearListeners()); |
| 538 | 540 |
| 539 pp::Var string(kTestString); | 541 pp::Var string(kTestString); |
| 540 pp::VarDictionary_Dev dictionary; | 542 pp::VarDictionary dictionary; |
| 541 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); | 543 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); |
| 542 dictionary.Set(pp::Var("bar"), string); | 544 dictionary.Set(pp::Var("bar"), string); |
| 543 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); | 545 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); |
| 544 dictionary.Set(pp::Var("def"), pp::Var()); | 546 dictionary.Set(pp::Var("def"), pp::Var()); |
| 545 | 547 |
| 546 // Reference to array. | 548 // Reference to array. |
| 547 pp::VarArray_Dev array; | 549 pp::VarArray array; |
| 548 array.Set(0, pp::Var(kTestBool)); | 550 array.Set(0, pp::Var(kTestBool)); |
| 549 array.Set(1, string); | 551 array.Set(1, string); |
| 550 // Purposely leave index 2 empty (which will place an undefined var there). | 552 // Purposely leave index 2 empty (which will place an undefined var there). |
| 551 array.Set(3, pp::Var(kTestInt)); | 553 array.Set(3, pp::Var(kTestInt)); |
| 552 array.Set(4, pp::Var(kTestDouble)); | 554 array.Set(4, pp::Var(kTestDouble)); |
| 553 | 555 |
| 554 dictionary.Set(pp::Var("array-ref1"), array); | 556 dictionary.Set(pp::Var("array-ref1"), array); |
| 555 dictionary.Set(pp::Var("array-ref2"), array); | 557 dictionary.Set(pp::Var("array-ref2"), array); |
| 556 | 558 |
| 557 // Set up the JavaScript message event listener to echo the data part of the | 559 // Set up the JavaScript message event listener to echo the data part of the |
| 558 // message event back to us. | 560 // message event back to us. |
| 559 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 561 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
| 560 instance_->PostMessage(dictionary); | 562 instance_->PostMessage(dictionary); |
| 561 // PostMessage is asynchronous, so we should not receive a response yet. | 563 // PostMessage is asynchronous, so we should not receive a response yet. |
| 562 ASSERT_EQ(message_data_.size(), 0); | 564 ASSERT_EQ(message_data_.size(), 0); |
| 563 ASSERT_EQ(WaitForMessages(), 1); | 565 ASSERT_EQ(WaitForMessages(), 1); |
| 564 ASSERT_TRUE(message_data_.back().is_dictionary()); | 566 ASSERT_TRUE(message_data_.back().is_dictionary()); |
| 565 pp::VarDictionary_Dev result(message_data_.back()); | 567 pp::VarDictionary result(message_data_.back()); |
| 566 ASSERT_TRUE(VarsEqual(dictionary, message_data_.back())); | 568 ASSERT_TRUE(VarsEqual(dictionary, message_data_.back())); |
| 567 | 569 |
| 568 WaitForMessages(); | 570 WaitForMessages(); |
| 569 message_data_.clear(); | 571 message_data_.clear(); |
| 570 ASSERT_TRUE(ClearListeners()); | 572 ASSERT_TRUE(ClearListeners()); |
| 571 | 573 |
| 572 // Set up a (dictionary -> array -> dictionary) cycle. Cycles shouldn't be | 574 // Set up a (dictionary -> array -> dictionary) cycle. Cycles shouldn't be |
| 573 // transmitted. | 575 // transmitted. |
| 574 pp::VarArray_Dev array2; | 576 pp::VarArray array2; |
| 575 array2.Set(0, dictionary); | 577 array2.Set(0, dictionary); |
| 576 dictionary.Set(pp::Var("array2"), array2); | 578 dictionary.Set(pp::Var("array2"), array2); |
| 577 | 579 |
| 578 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 580 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
| 579 instance_->PostMessage(dictionary); | 581 instance_->PostMessage(dictionary); |
| 580 // PostMessage is asynchronous, so we should not receive a response yet. | 582 // PostMessage is asynchronous, so we should not receive a response yet. |
| 581 ASSERT_EQ(message_data_.size(), 0); | 583 ASSERT_EQ(message_data_.size(), 0); |
| 582 ASSERT_EQ(WaitForMessages(), 0); | 584 ASSERT_EQ(WaitForMessages(), 0); |
| 583 | 585 |
| 584 // Break the cycles. | 586 // Break the cycles. |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 751 ASSERT_TRUE(received_value <= kThreadsToRun); | 753 ASSERT_TRUE(received_value <= kThreadsToRun); |
| 752 ++received_counts[received_value]; | 754 ++received_counts[received_value]; |
| 753 } | 755 } |
| 754 ASSERT_EQ(received_counts, expected_counts); | 756 ASSERT_EQ(received_counts, expected_counts); |
| 755 | 757 |
| 756 message_data_.clear(); | 758 message_data_.clear(); |
| 757 ASSERT_TRUE(ClearListeners()); | 759 ASSERT_TRUE(ClearListeners()); |
| 758 | 760 |
| 759 PASS(); | 761 PASS(); |
| 760 } | 762 } |
| OLD | NEW |