Chromium Code Reviews| 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("; | |
|
dmichael (off chromium)
2013/06/04 16:59:10
nit: inner code should line up with "var", not "pl
raymes
2013/06/04 19:36:06
Done.
| |
| 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 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 524 // should start with these. | 534 // should start with these. |
| 525 WaitForMessages(); | 535 WaitForMessages(); |
| 526 ASSERT_TRUE(ClearListeners()); | 536 ASSERT_TRUE(ClearListeners()); |
| 527 | 537 |
| 528 pp::Var string(kTestString); | 538 pp::Var string(kTestString); |
| 529 pp::VarDictionary_Dev dictionary; | 539 pp::VarDictionary_Dev dictionary; |
| 530 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); | 540 dictionary.Set(pp::Var("foo"), pp::Var(kTestBool)); |
| 531 dictionary.Set(pp::Var("bar"), string); | 541 dictionary.Set(pp::Var("bar"), string); |
| 532 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); | 542 dictionary.Set(pp::Var("abc"), pp::Var(kTestInt)); |
| 533 dictionary.Set(pp::Var("def"), pp::Var()); | 543 dictionary.Set(pp::Var("def"), pp::Var()); |
| 534 dictionary.Set(pp::Var("dictionary"), dictionary); // Self-reference. | |
| 535 | 544 |
| 536 // Reference to array. | 545 // Reference to array. |
| 537 pp::VarArray_Dev array; | 546 pp::VarArray_Dev array; |
| 538 array.Set(0, pp::Var(kTestBool)); | 547 array.Set(0, pp::Var(kTestBool)); |
| 539 array.Set(1, string); | 548 array.Set(1, string); |
| 540 // Purposely leave index 2 empty (which will place an undefined var there). | 549 // Purposely leave index 2 empty (which will place an undefined var there). |
| 541 array.Set(3, pp::Var(kTestInt)); | 550 array.Set(3, pp::Var(kTestInt)); |
| 542 array.Set(4, pp::Var(kTestDouble)); | 551 array.Set(4, pp::Var(kTestDouble)); |
| 543 | 552 |
| 544 dictionary.Set(pp::Var("array-ref1"), array); | 553 dictionary.Set(pp::Var("array-ref1"), array); |
| 545 dictionary.Set(pp::Var("array-ref2"), array); | 554 dictionary.Set(pp::Var("array-ref2"), array); |
| 546 | 555 |
| 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 | 556 // Set up the JavaScript message event listener to echo the data part of the |
| 553 // message event back to us. | 557 // message event back to us. |
| 554 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 558 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
| 555 message_data_.clear(); | 559 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 |
| 568 message_data_.clear(); | |
| 569 WaitForMessages(); | |
| 570 ASSERT_TRUE(ClearListeners()); | |
| 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 message_data_.clear(); | |
| 580 instance_->PostMessage(dictionary); | |
| 581 // PostMessage is asynchronous, so we should not receive a response yet. | |
| 582 ASSERT_EQ(message_data_.size(), 0); | |
| 583 ASSERT_EQ(WaitForMessages(), 1); | |
| 584 ASSERT_TRUE(message_data_.back().is_undefined()); | |
| 585 | |
| 564 // Break the cycles. | 586 // Break the cycles. |
| 565 dictionary.Delete(pp::Var("dictionary")); | |
| 566 dictionary.Delete(pp::Var("array2")); | 587 dictionary.Delete(pp::Var("array2")); |
| 567 result.Delete(pp::Var("dictionary")); | |
| 568 result.Delete(pp::Var("array2")); | |
| 569 | 588 |
| 570 message_data_.clear(); | 589 message_data_.clear(); |
| 571 ASSERT_TRUE(ClearListeners()); | 590 ASSERT_TRUE(ClearListeners()); |
| 572 | 591 |
| 592 // Test sending a cycle from JavaScript to the plugin. | |
| 593 PostMessageFromJavaScript("function() { var x = []; x[0] = x; return x; }"); | |
| 594 ASSERT_EQ(message_data_.size(), 0); | |
| 595 ASSERT_EQ(WaitForMessages(), 1); | |
| 596 ASSERT_TRUE(message_data_.back().is_undefined()); | |
| 597 | |
| 598 message_data_.clear(); | |
| 599 WaitForMessages(); | |
| 600 ASSERT_TRUE(ClearListeners()); | |
| 601 | |
| 573 PASS(); | 602 PASS(); |
| 574 } | 603 } |
| 575 | 604 |
| 576 std::string TestPostMessage::TestMessageEvent() { | 605 std::string TestPostMessage::TestMessageEvent() { |
| 577 // Set up the JavaScript message event listener to pass us some values from | 606 // Set up the JavaScript message event listener to pass us some values from |
| 578 // the MessageEvent and make sure they match our expectations. | 607 // the MessageEvent and make sure they match our expectations. |
| 579 | 608 |
| 580 WaitForMessages(); | 609 WaitForMessages(); |
| 581 ASSERT_TRUE(ClearListeners()); | 610 ASSERT_TRUE(ClearListeners()); |
| 582 // Have the listener pass back the class name of message_event and make sure | 611 // 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); | 752 ASSERT_TRUE(received_value <= kThreadsToRun); |
| 724 ++received_counts[received_value]; | 753 ++received_counts[received_value]; |
| 725 } | 754 } |
| 726 ASSERT_EQ(received_counts, expected_counts); | 755 ASSERT_EQ(received_counts, expected_counts); |
| 727 | 756 |
| 728 message_data_.clear(); | 757 message_data_.clear(); |
| 729 ASSERT_TRUE(ClearListeners()); | 758 ASSERT_TRUE(ClearListeners()); |
| 730 | 759 |
| 731 PASS(); | 760 PASS(); |
| 732 } | 761 } |
| OLD | NEW |