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("; | |
| 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 instance_->PostMessage(pp::Var()); | |
| 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 | |
| 586 // Break the cycles. | |
| 587 dictionary.Delete(pp::Var("array2")); | |
| 588 | |
| 589 WaitForMessages(); | |
| 590 message_data_.clear(); | |
| 591 ASSERT_TRUE(ClearListeners()); | |
| 592 | |
| 593 // Test sending a cycle from JavaScript to the plugin. | |
| 594 ASSERT_TRUE(AddEchoingListener("message_event.data")); | |
| 595 PostMessageFromJavaScript("function() { var x = []; x[0] = x; return x; }"); | |
| 596 instance_->PostMessage(pp::Var()); | |
| 597 ASSERT_EQ(message_data_.size(), 0); | |
| 598 ASSERT_EQ(WaitForMessages(), 1); | |
|
dmichael (off chromium)
2013/06/05 17:05:47
Shouldn't we expect 0 here now?
raymes
2013/06/05 22:48:26
Yeah - I thought that WaitForMessages() wouldn't w
dmichael (off chromium)
2013/06/06 16:32:30
It sends its own special message (that's echoed ba
| |
| 599 ASSERT_TRUE(message_data_.back().is_undefined()); | |
| 600 | |
| 601 WaitForMessages(); | |
| 602 message_data_.clear(); | |
| 603 ASSERT_TRUE(ClearListeners()); | |
| 604 | |
| 573 PASS(); | 605 PASS(); |
| 574 } | 606 } |
| 575 | 607 |
| 576 std::string TestPostMessage::TestMessageEvent() { | 608 std::string TestPostMessage::TestMessageEvent() { |
| 577 // Set up the JavaScript message event listener to pass us some values from | 609 // Set up the JavaScript message event listener to pass us some values from |
| 578 // the MessageEvent and make sure they match our expectations. | 610 // the MessageEvent and make sure they match our expectations. |
| 579 | 611 |
| 580 WaitForMessages(); | 612 WaitForMessages(); |
| 581 ASSERT_TRUE(ClearListeners()); | 613 ASSERT_TRUE(ClearListeners()); |
| 582 // Have the listener pass back the class name of message_event and make sure | 614 // 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); | 755 ASSERT_TRUE(received_value <= kThreadsToRun); |
| 724 ++received_counts[received_value]; | 756 ++received_counts[received_value]; |
| 725 } | 757 } |
| 726 ASSERT_EQ(received_counts, expected_counts); | 758 ASSERT_EQ(received_counts, expected_counts); |
| 727 | 759 |
| 728 message_data_.clear(); | 760 message_data_.clear(); |
| 729 ASSERT_TRUE(ClearListeners()); | 761 ASSERT_TRUE(ClearListeners()); |
| 730 | 762 |
| 731 PASS(); | 763 PASS(); |
| 732 } | 764 } |
| OLD | NEW |