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 |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
231 "plugin.addEventListener('message', message_handler);" | 231 "plugin.addEventListener('message', message_handler);" |
232 // Maintain an array of all event listeners, attached to the | 232 // Maintain an array of all event listeners, attached to the |
233 // plugin. This is so that we can easily remove them later (see | 233 // plugin. This is so that we can easily remove them later (see |
234 // ClearListeners()). | 234 // ClearListeners()). |
235 "if (!plugin.eventListeners) plugin.eventListeners = [];" | 235 "if (!plugin.eventListeners) plugin.eventListeners = [];" |
236 "plugin.eventListeners.push(message_handler);"; | 236 "plugin.eventListeners.push(message_handler);"; |
237 instance_->EvalScript(js_code); | 237 instance_->EvalScript(js_code); |
238 return true; | 238 return true; |
239 } | 239 } |
240 | 240 |
| 241 bool TestPostMessage::PostMessageFromJavaScript(const std::string& func) { |
| 242 std::string js_code; |
| 243 js_code += "var plugin = document.getElementById('plugin');" |
| 244 "plugin.postMessage("; |
| 245 js_code += func + "()"; |
| 246 js_code += " );"; |
| 247 instance_->EvalScript(js_code); |
| 248 return true; |
| 249 } |
| 250 |
241 bool TestPostMessage::ClearListeners() { | 251 bool TestPostMessage::ClearListeners() { |
242 std::string js_code; | 252 std::string js_code; |
243 js_code += "var plugin = document.getElementById('plugin');" | 253 js_code += "var plugin = document.getElementById('plugin');" |
244 "while (plugin.eventListeners.length) {" | 254 "while (plugin.eventListeners.length) {" |
245 " plugin.removeEventListener('message'," | 255 " plugin.removeEventListener('message'," |
246 " plugin.eventListeners.pop());" | 256 " plugin.eventListeners.pop());" |
247 "}"; | 257 "}"; |
248 instance_->EvalScript(js_code); | 258 instance_->EvalScript(js_code); |
249 return true; | 259 return true; |
250 } | 260 } |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
516 ASSERT_TRUE(ClearListeners()); | 526 ASSERT_TRUE(ClearListeners()); |
517 | 527 |
518 PASS(); | 528 PASS(); |
519 } | 529 } |
520 | 530 |
521 std::string TestPostMessage::TestSendingComplexVar() { | 531 std::string TestPostMessage::TestSendingComplexVar() { |
522 // Clean up after previous tests. This also swallows the message sent by Init | 532 // 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' | 533 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
524 // should start with these. | 534 // should start with these. |
525 WaitForMessages(); | 535 WaitForMessages(); |
| 536 message_data_.clear(); |
526 ASSERT_TRUE(ClearListeners()); | 537 ASSERT_TRUE(ClearListeners()); |
527 | 538 |
528 pp::Var string(kTestString); | 539 pp::Var string(kTestString); |
529 pp::VarDictionary_Dev dictionary; | 540 pp::VarDictionary_Dev dictionary; |
530 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); | 541 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); |
531 dictionary.Set(pp::Var("bar"), string); | 542 dictionary.Set(pp::Var("bar"), string); |
532 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); | 543 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); |
533 dictionary.Set(pp::Var("def"), pp::Var()); | 544 dictionary.Set(pp::Var("def"), pp::Var()); |
534 dictionary.Set(pp::Var("dictionary"), dictionary); // Self-reference. | |
535 | 545 |
536 // Reference to array. | 546 // Reference to array. |
537 pp::VarArray_Dev array; | 547 pp::VarArray_Dev array; |
538 array.Set(0, pp::Var(kTestBool)); | 548 array.Set(0, pp::Var(kTestBool)); |
539 array.Set(1, string); | 549 array.Set(1, string); |
540 // Purposely leave index 2 empty (which will place an undefined var there). | 550 // Purposely leave index 2 empty (which will place an undefined var there). |
541 array.Set(3, pp::Var(kTestInt)); | 551 array.Set(3, pp::Var(kTestInt)); |
542 array.Set(4, pp::Var(kTestDouble)); | 552 array.Set(4, pp::Var(kTestDouble)); |
543 | 553 |
544 dictionary.Set(pp::Var("array-ref1"), array); | 554 dictionary.Set(pp::Var("array-ref1"), array); |
545 dictionary.Set(pp::Var("array-ref2"), array); | 555 dictionary.Set(pp::Var("array-ref2"), array); |
546 | 556 |
547 // Set up a (dictionary -> array -> dictionary) cycle. | |
548 pp::VarArray_Dev array2; | |
549 array2.Set(0, dictionary); | |
550 dictionary.Set(pp::Var("array2"), array2); | |
551 | |
552 // Set up the JavaScript message event listener to echo the data part of the | 557 // Set up the JavaScript message event listener to echo the data part of the |
553 // message event back to us. | 558 // message event back to us. |
554 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 559 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
555 message_data_.clear(); | |
556 instance_->PostMessage(dictionary); | 560 instance_->PostMessage(dictionary); |
557 // PostMessage is asynchronous, so we should not receive a response yet. | 561 // PostMessage is asynchronous, so we should not receive a response yet. |
558 ASSERT_EQ(message_data_.size(), 0); | 562 ASSERT_EQ(message_data_.size(), 0); |
559 ASSERT_EQ(WaitForMessages(), 1); | 563 ASSERT_EQ(WaitForMessages(), 1); |
560 ASSERT_TRUE(message_data_.back().is_dictionary()); | 564 ASSERT_TRUE(message_data_.back().is_dictionary()); |
561 pp::VarDictionary_Dev result(message_data_.back()); | 565 pp::VarDictionary_Dev result(message_data_.back()); |
562 ASSERT_TRUE(VarsEqual(dictionary, message_data_.back())); | 566 ASSERT_TRUE(VarsEqual(dictionary, message_data_.back())); |
563 | 567 |
564 // Break the cycles. | 568 WaitForMessages(); |
565 dictionary.Delete(pp::Var("dictionary")); | |
566 dictionary.Delete(pp::Var("array2")); | |
567 result.Delete(pp::Var("dictionary")); | |
568 result.Delete(pp::Var("array2")); | |
569 | |
570 message_data_.clear(); | 569 message_data_.clear(); |
571 ASSERT_TRUE(ClearListeners()); | 570 ASSERT_TRUE(ClearListeners()); |
572 | 571 |
| 572 // Set up a (dictionary -> array -> dictionary) cycle. Cycles shouldn't be |
| 573 // transmitted. |
| 574 pp::VarArray_Dev array2; |
| 575 array2.Set(0, dictionary); |
| 576 dictionary.Set(pp::Var("array2"), array2); |
| 577 |
| 578 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
| 579 instance_->PostMessage(dictionary); |
| 580 // PostMessage is asynchronous, so we should not receive a response yet. |
| 581 ASSERT_EQ(message_data_.size(), 0); |
| 582 ASSERT_EQ(WaitForMessages(), 0); |
| 583 |
| 584 // Break the cycles. |
| 585 dictionary.Delete(pp::Var("array2")); |
| 586 |
| 587 WaitForMessages(); |
| 588 message_data_.clear(); |
| 589 ASSERT_TRUE(ClearListeners()); |
| 590 |
| 591 // Test sending a cycle from JavaScript to the plugin. |
| 592 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
| 593 PostMessageFromJavaScript("function() { var x = []; x[0] = x; return x; }"); |
| 594 ASSERT_EQ(message_data_.size(), 0); |
| 595 ASSERT_EQ(WaitForMessages(), 0); |
| 596 |
| 597 WaitForMessages(); |
| 598 message_data_.clear(); |
| 599 ASSERT_TRUE(ClearListeners()); |
| 600 |
573 PASS(); | 601 PASS(); |
574 } | 602 } |
575 | 603 |
576 std::string TestPostMessage::TestMessageEvent() { | 604 std::string TestPostMessage::TestMessageEvent() { |
577 // Set up the JavaScript message event listener to pass us some values from | 605 // Set up the JavaScript message event listener to pass us some values from |
578 // the MessageEvent and make sure they match our expectations. | 606 // the MessageEvent and make sure they match our expectations. |
579 | 607 |
580 WaitForMessages(); | 608 WaitForMessages(); |
581 ASSERT_TRUE(ClearListeners()); | 609 ASSERT_TRUE(ClearListeners()); |
582 // Have the listener pass back the class name of message_event and make sure | 610 // Have the listener pass back the class name of message_event and make sure |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
723 ASSERT_TRUE(received_value <= kThreadsToRun); | 751 ASSERT_TRUE(received_value <= kThreadsToRun); |
724 ++received_counts[received_value]; | 752 ++received_counts[received_value]; |
725 } | 753 } |
726 ASSERT_EQ(received_counts, expected_counts); | 754 ASSERT_EQ(received_counts, expected_counts); |
727 | 755 |
728 message_data_.clear(); | 756 message_data_.clear(); |
729 ASSERT_TRUE(ClearListeners()); | 757 ASSERT_TRUE(ClearListeners()); |
730 | 758 |
731 PASS(); | 759 PASS(); |
732 } | 760 } |
OLD | NEW |