| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 | 8 |
| 9 #include "ppapi/c/dev/ppb_testing_dev.h" | 9 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 10 #include "ppapi/c/pp_var.h" | 10 #include "ppapi/c/pp_var.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 | 53 |
| 54 } // namespace | 54 } // namespace |
| 55 | 55 |
| 56 TestPostMessage::TestPostMessage(TestingInstance* instance) | 56 TestPostMessage::TestPostMessage(TestingInstance* instance) |
| 57 : TestCase(instance) { | 57 : TestCase(instance) { |
| 58 } | 58 } |
| 59 | 59 |
| 60 TestPostMessage::~TestPostMessage() { | 60 TestPostMessage::~TestPostMessage() { |
| 61 // Remove the special listener that only responds to a FINISHED_WAITING | 61 // Remove the special listener that only responds to a FINISHED_WAITING |
| 62 // string. See Init for where it gets added. | 62 // string. See Init for where it gets added. |
| 63 std::string js_code; | 63 std::string js_code("TESTING_MESSAGE:ExecuteScript:"); |
| 64 js_code += "var plugin = document.getElementById('plugin');" | 64 js_code += "var plugin = document.getElementById('plugin');" |
| 65 "plugin.removeEventListener('message'," | 65 "plugin.removeEventListener('message'," |
| 66 " plugin.wait_for_messages_handler);" | 66 " plugin.wait_for_messages_handler);" |
| 67 "delete plugin.wait_for_messages_handler;"; | 67 "delete plugin.wait_for_messages_handler;"; |
| 68 pp::Var exception; | 68 instance_->PostMessage(pp::Var(js_code)); |
| 69 instance_->ExecuteScript(js_code, &exception); | |
| 70 } | 69 } |
| 71 | 70 |
| 72 bool TestPostMessage::Init() { | 71 bool TestPostMessage::Init() { |
| 73 bool success = InitTestingInterface(); | 72 bool success = InitTestingInterface(); |
| 74 | 73 |
| 75 // Set up a special listener that only responds to a FINISHED_WAITING string. | 74 // Set up a special listener that only responds to a FINISHED_WAITING string. |
| 76 // This is for use by WaitForMessages. | 75 // This is for use by WaitForMessages. |
| 77 std::string js_code; | 76 std::string js_code("TESTING_MESSAGE:ExecuteScript:"); |
| 78 // Note the following code is dependent on some features of test_case.html. | 77 // Note the following code is dependent on some features of test_case.html. |
| 79 // E.g., it is assumed that the DOM element where the plugin is embedded has | 78 // E.g., it is assumed that the DOM element where the plugin is embedded has |
| 80 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows | 79 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows |
| 81 // us to ignore the messages that are intended for use by the testing | 80 // us to ignore the messages that are intended for use by the testing |
| 82 // framework itself. | 81 // framework itself. |
| 83 js_code += "var plugin = document.getElementById('plugin');" | 82 js_code += "var plugin = document.getElementById('plugin');" |
| 84 "var wait_for_messages_handler = function(message_event) {" | 83 "var wait_for_messages_handler = function(message_event) {" |
| 85 " if (!IsTestingMessage(message_event.data) &&" | 84 " if (!IsTestingMessage(message_event.data) &&" |
| 86 " message_event.data === '" FINISHED_WAITING_MESSAGE "') {" | 85 " message_event.data === '" FINISHED_WAITING_MESSAGE "') {" |
| 87 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');" | 86 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');" |
| 88 " }" | 87 " }" |
| 89 "};" | 88 "};" |
| 90 "plugin.addEventListener('message', wait_for_messages_handler);" | 89 "plugin.addEventListener('message', wait_for_messages_handler);" |
| 91 // Stash it on the plugin so we can remove it in the destructor. | 90 // Stash it on the plugin so we can remove it in the destructor. |
| 92 "plugin.wait_for_messages_handler = wait_for_messages_handler;"; | 91 "plugin.wait_for_messages_handler = wait_for_messages_handler;"; |
| 93 pp::Var exception; | 92 instance_->PostMessage(pp::Var(js_code)); |
| 94 instance_->ExecuteScript(js_code, &exception); | |
| 95 success = success && exception.is_undefined(); | |
| 96 | 93 |
| 97 // Set up the JavaScript message event listener to echo the data part of the | 94 // Set up the JavaScript message event listener to echo the data part of the |
| 98 // message event back to us. | 95 // message event back to us. |
| 99 success = success && AddEchoingListener("message_event.data"); | 96 success = success && AddEchoingListener("message_event.data"); |
| 100 message_data_.clear(); | 97 message_data_.clear(); |
| 101 // Send a message that the first test will expect to receive. This is to | 98 // Send a message that the first test will expect to receive. This is to |
| 102 // verify that we can send messages when the 'Instance::Init' function is on | 99 // verify that we can send messages when the 'Instance::Init' function is on |
| 103 // the stack. | 100 // the stack. |
| 104 instance_->PostMessage(pp::Var(kTestString)); | 101 instance_->PostMessage(pp::Var(kTestString)); |
| 105 | 102 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 121 | 118 |
| 122 void TestPostMessage::HandleMessage(const pp::Var& message_data) { | 119 void TestPostMessage::HandleMessage(const pp::Var& message_data) { |
| 123 if (message_data.is_string() && | 120 if (message_data.is_string() && |
| 124 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) | 121 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) |
| 125 testing_interface_->QuitMessageLoop(instance_->pp_instance()); | 122 testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
| 126 else | 123 else |
| 127 message_data_.push_back(message_data); | 124 message_data_.push_back(message_data); |
| 128 } | 125 } |
| 129 | 126 |
| 130 bool TestPostMessage::AddEchoingListener(const std::string& expression) { | 127 bool TestPostMessage::AddEchoingListener(const std::string& expression) { |
| 131 std::string js_code; | 128 std::string js_code("TESTING_MESSAGE:ExecuteScript:"); |
| 132 // Note the following code is dependent on some features of test_case.html. | 129 // Note the following code is dependent on some features of test_case.html. |
| 133 // E.g., it is assumed that the DOM element where the plugin is embedded has | 130 // E.g., it is assumed that the DOM element where the plugin is embedded has |
| 134 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows | 131 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows |
| 135 // us to ignore the messages that are intended for use by the testing | 132 // us to ignore the messages that are intended for use by the testing |
| 136 // framework itself. | 133 // framework itself. |
| 137 js_code += "var plugin = document.getElementById('plugin');" | 134 js_code += "var plugin = document.getElementById('plugin');" |
| 138 "var message_handler = function(message_event) {" | 135 "var message_handler = function(message_event) {" |
| 139 " if (!IsTestingMessage(message_event.data) &&" | 136 " if (!IsTestingMessage(message_event.data) &&" |
| 140 " !(message_event.data === '" FINISHED_WAITING_MESSAGE "')) {" | 137 " !(message_event.data === '" FINISHED_WAITING_MESSAGE "')) {" |
| 141 " plugin.postMessage("; | 138 " plugin.postMessage("; |
| 142 js_code += expression; | 139 js_code += expression; |
| 143 js_code += " );" | 140 js_code += " );" |
| 144 " }" | 141 " }" |
| 145 "};" | 142 "};" |
| 146 "plugin.addEventListener('message', message_handler);" | 143 "plugin.addEventListener('message', message_handler);" |
| 147 // Maintain an array of all event listeners, attached to the | 144 // Maintain an array of all event listeners, attached to the |
| 148 // plugin. This is so that we can easily remove them later (see | 145 // plugin. This is so that we can easily remove them later (see |
| 149 // ClearListeners()). | 146 // ClearListeners()). |
| 150 "if (!plugin.eventListeners) plugin.eventListeners = [];" | 147 "if (!plugin.eventListeners) plugin.eventListeners = [];" |
| 151 "plugin.eventListeners.push(message_handler);"; | 148 "plugin.eventListeners.push(message_handler);"; |
| 152 pp::Var exception; | 149 instance_->PostMessage(pp::Var(js_code)); |
| 153 instance_->ExecuteScript(js_code, &exception); | 150 return true; |
| 154 return exception.is_undefined(); | |
| 155 } | 151 } |
| 156 | 152 |
| 157 bool TestPostMessage::ClearListeners() { | 153 bool TestPostMessage::ClearListeners() { |
| 158 std::string js_code( | 154 std::string js_code("TESTING_MESSAGE:ExecuteScript:"); |
| 159 "var plugin = document.getElementById('plugin');" | 155 js_code += "var plugin = document.getElementById('plugin');" |
| 160 "while (plugin.eventListeners.length) {" | 156 "while (plugin.eventListeners.length) {" |
| 161 " plugin.removeEventListener('message', plugin.eventListeners.pop());" | 157 " plugin.removeEventListener('message'," |
| 162 "}"); | 158 " plugin.eventListeners.pop());" |
| 163 pp::Var exception; | 159 "}"; |
| 164 instance_->ExecuteScript(js_code, &exception); | 160 instance_->PostMessage(pp::Var(js_code)); |
| 165 return(exception.is_undefined()); | 161 return true; |
| 166 } | 162 } |
| 167 | 163 |
| 168 int TestPostMessage::WaitForMessages() { | 164 int TestPostMessage::WaitForMessages() { |
| 169 size_t message_size_before = message_data_.size(); | 165 size_t message_size_before = message_data_.size(); |
| 170 // We first post a FINISHED_WAITING_MESSAGE. This should be guaranteed to | 166 // We first post a FINISHED_WAITING_MESSAGE. This should be guaranteed to |
| 171 // come back _after_ any other incoming messages that were already pending. | 167 // come back _after_ any other incoming messages that were already pending. |
| 172 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE)); | 168 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE)); |
| 173 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 169 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 174 // Now that the FINISHED_WAITING_MESSAGE has been echoed back to us, we know | 170 // Now that the FINISHED_WAITING_MESSAGE has been echoed back to us, we know |
| 175 // that all pending messages have been slurped up. Return the number we | 171 // that all pending messages have been slurped up. Return the number we |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 } | 444 } |
| 449 ASSERT_TRUE(received_value >= 0); | 445 ASSERT_TRUE(received_value >= 0); |
| 450 ASSERT_TRUE(received_value <= kThreadsToRun); | 446 ASSERT_TRUE(received_value <= kThreadsToRun); |
| 451 ++received_counts[received_value]; | 447 ++received_counts[received_value]; |
| 452 } | 448 } |
| 453 ASSERT_EQ(received_counts, expected_counts); | 449 ASSERT_EQ(received_counts, expected_counts); |
| 454 | 450 |
| 455 PASS(); | 451 PASS(); |
| 456 } | 452 } |
| 457 | 453 |
| OLD | NEW |