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) == PP_FALSE) |
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; |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
431 PASS(); | 433 PASS(); |
432 } | 434 } |
433 | 435 |
434 std::string TestPostMessage::TestSendingArray() { | 436 std::string TestPostMessage::TestSendingArray() { |
435 // Clean up after previous tests. This also swallows the message sent by Init | 437 // Clean up after previous tests. This also swallows the message sent by Init |
436 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' | 438 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
437 // should start with these. | 439 // should start with these. |
438 WaitForMessages(); | 440 WaitForMessages(); |
439 ASSERT_TRUE(ClearListeners()); | 441 ASSERT_TRUE(ClearListeners()); |
440 | 442 |
441 pp::VarArray_Dev array; | 443 pp::VarArray array; |
442 array.Set(0, pp::Var(kTestBool)); | 444 array.Set(0, pp::Var(kTestBool)); |
443 array.Set(1, pp::Var(kTestString)); | 445 array.Set(1, pp::Var(kTestString)); |
444 // Purposely leave index 2 empty. | 446 // Purposely leave index 2 empty. |
445 array.Set(3, pp::Var(kTestInt)); | 447 array.Set(3, pp::Var(kTestInt)); |
446 array.Set(4, pp::Var(kTestDouble)); | 448 array.Set(4, pp::Var(kTestDouble)); |
447 | 449 |
448 std::stringstream ss; | 450 std::stringstream ss; |
449 ss << array.GetLength(); | 451 ss << array.GetLength(); |
450 std::string length_as_string(ss.str()); | 452 std::string length_as_string(ss.str()); |
451 | 453 |
(...skipping 22 matching lines...) Expand all Loading... |
474 PASS(); | 476 PASS(); |
475 } | 477 } |
476 | 478 |
477 std::string TestPostMessage::TestSendingDictionary() { | 479 std::string TestPostMessage::TestSendingDictionary() { |
478 // Clean up after previous tests. This also swallows the message sent by Init | 480 // Clean up after previous tests. This also swallows the message sent by Init |
479 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' | 481 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
480 // should start with these. | 482 // should start with these. |
481 WaitForMessages(); | 483 WaitForMessages(); |
482 ASSERT_TRUE(ClearListeners()); | 484 ASSERT_TRUE(ClearListeners()); |
483 | 485 |
484 pp::VarDictionary_Dev dictionary; | 486 pp::VarDictionary dictionary; |
485 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); | 487 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); |
486 dictionary.Set(pp::Var("bar"), pp::Var(kTestString)); | 488 dictionary.Set(pp::Var("bar"), pp::Var(kTestString)); |
487 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); | 489 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); |
488 dictionary.Set(pp::Var("def"), pp::Var()); | 490 dictionary.Set(pp::Var("def"), pp::Var()); |
489 | 491 |
490 std::stringstream ss; | 492 std::stringstream ss; |
491 ss << dictionary.GetKeys().GetLength(); | 493 ss << dictionary.GetKeys().GetLength(); |
492 std::string length_as_string(ss.str()); | 494 std::string length_as_string(ss.str()); |
493 | 495 |
494 // Have the listener test some properties of the Dictionary. | 496 // Have the listener test some properties of the Dictionary. |
(...skipping 24 matching lines...) Expand all Loading... |
519 } | 521 } |
520 | 522 |
521 std::string TestPostMessage::TestSendingComplexVar() { | 523 std::string TestPostMessage::TestSendingComplexVar() { |
522 // Clean up after previous tests. This also swallows the message sent by Init | 524 // Clean up after previous tests. This also swallows the message sent by Init |
523 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' | 525 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
524 // should start with these. | 526 // should start with these. |
525 WaitForMessages(); | 527 WaitForMessages(); |
526 ASSERT_TRUE(ClearListeners()); | 528 ASSERT_TRUE(ClearListeners()); |
527 | 529 |
528 pp::Var string(kTestString); | 530 pp::Var string(kTestString); |
529 pp::VarDictionary_Dev dictionary; | 531 pp::VarDictionary dictionary; |
530 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); | 532 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); |
531 dictionary.Set(pp::Var("bar"), string); | 533 dictionary.Set(pp::Var("bar"), string); |
532 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); | 534 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); |
533 dictionary.Set(pp::Var("def"), pp::Var()); | 535 dictionary.Set(pp::Var("def"), pp::Var()); |
534 dictionary.Set(pp::Var("dictionary"), dictionary); // Self-reference. | 536 dictionary.Set(pp::Var("dictionary"), dictionary); // Self-reference. |
535 | 537 |
536 // Reference to array. | 538 // Reference to array. |
537 pp::VarArray_Dev array; | 539 pp::VarArray array; |
538 array.Set(0, pp::Var(kTestBool)); | 540 array.Set(0, pp::Var(kTestBool)); |
539 array.Set(1, string); | 541 array.Set(1, string); |
540 // Purposely leave index 2 empty (which will place an undefined var there). | 542 // Purposely leave index 2 empty (which will place an undefined var there). |
541 array.Set(3, pp::Var(kTestInt)); | 543 array.Set(3, pp::Var(kTestInt)); |
542 array.Set(4, pp::Var(kTestDouble)); | 544 array.Set(4, pp::Var(kTestDouble)); |
543 | 545 |
544 dictionary.Set(pp::Var("array-ref1"), array); | 546 dictionary.Set(pp::Var("array-ref1"), array); |
545 dictionary.Set(pp::Var("array-ref2"), array); | 547 dictionary.Set(pp::Var("array-ref2"), array); |
546 | 548 |
547 // Set up a (dictionary -> array -> dictionary) cycle. | 549 // Set up a (dictionary -> array -> dictionary) cycle. |
548 pp::VarArray_Dev array2; | 550 pp::VarArray array2; |
549 array2.Set(0, dictionary); | 551 array2.Set(0, dictionary); |
550 dictionary.Set(pp::Var("array2"), array2); | 552 dictionary.Set(pp::Var("array2"), array2); |
551 | 553 |
552 // Set up the JavaScript message event listener to echo the data part of the | 554 // Set up the JavaScript message event listener to echo the data part of the |
553 // message event back to us. | 555 // message event back to us. |
554 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 556 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
555 message_data_.clear(); | 557 message_data_.clear(); |
556 instance_->PostMessage(dictionary); | 558 instance_->PostMessage(dictionary); |
557 // PostMessage is asynchronous, so we should not receive a response yet. | 559 // PostMessage is asynchronous, so we should not receive a response yet. |
558 ASSERT_EQ(message_data_.size(), 0); | 560 ASSERT_EQ(message_data_.size(), 0); |
559 ASSERT_EQ(WaitForMessages(), 1); | 561 ASSERT_EQ(WaitForMessages(), 1); |
560 ASSERT_TRUE(message_data_.back().is_dictionary()); | 562 ASSERT_TRUE(message_data_.back().is_dictionary()); |
561 pp::VarDictionary_Dev result(message_data_.back()); | 563 pp::VarDictionary result(message_data_.back()); |
562 ASSERT_TRUE(VarsEqual(dictionary, message_data_.back())); | 564 ASSERT_TRUE(VarsEqual(dictionary, message_data_.back())); |
563 | 565 |
564 // Break the cycles. | 566 // Break the cycles. |
565 dictionary.Delete(pp::Var("dictionary")); | 567 dictionary.Delete(pp::Var("dictionary")); |
566 dictionary.Delete(pp::Var("array2")); | 568 dictionary.Delete(pp::Var("array2")); |
567 result.Delete(pp::Var("dictionary")); | 569 result.Delete(pp::Var("dictionary")); |
568 result.Delete(pp::Var("array2")); | 570 result.Delete(pp::Var("array2")); |
569 | 571 |
570 message_data_.clear(); | 572 message_data_.clear(); |
571 ASSERT_TRUE(ClearListeners()); | 573 ASSERT_TRUE(ClearListeners()); |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 ASSERT_TRUE(received_value <= kThreadsToRun); | 725 ASSERT_TRUE(received_value <= kThreadsToRun); |
724 ++received_counts[received_value]; | 726 ++received_counts[received_value]; |
725 } | 727 } |
726 ASSERT_EQ(received_counts, expected_counts); | 728 ASSERT_EQ(received_counts, expected_counts); |
727 | 729 |
728 message_data_.clear(); | 730 message_data_.clear(); |
729 ASSERT_TRUE(ClearListeners()); | 731 ASSERT_TRUE(ClearListeners()); |
730 | 732 |
731 PASS(); | 733 PASS(); |
732 } | 734 } |
OLD | NEW |