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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 js_code += "var plugin = document.getElementById('plugin');" | 154 js_code += "var plugin = document.getElementById('plugin');" |
155 "plugin.removeEventListener('message'," | 155 "plugin.removeEventListener('message'," |
156 " plugin.wait_for_messages_handler);" | 156 " plugin.wait_for_messages_handler);" |
157 "delete plugin.wait_for_messages_handler;"; | 157 "delete plugin.wait_for_messages_handler;"; |
158 instance_->EvalScript(js_code); | 158 instance_->EvalScript(js_code); |
159 } | 159 } |
160 | 160 |
161 bool TestPostMessage::Init() { | 161 bool TestPostMessage::Init() { |
162 bool success = CheckTestingInterface(); | 162 bool success = CheckTestingInterface(); |
163 | 163 |
164 core_interface_ = static_cast<const PPB_Core*>( | |
165 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | |
166 file_system_interface_ = static_cast<const PPB_FileSystem*>( | |
167 pp::Module::Get()->GetBrowserInterface(PPB_FILESYSTEM_INTERFACE)); | |
168 var_interface_ = static_cast<const PPB_Var*>( | |
169 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | |
170 var_resource_interface_ = static_cast<const PPB_VarResource_Dev*>( | |
171 pp::Module::Get()->GetBrowserInterface(PPB_VAR_RESOURCE_DEV_INTERFACE)); | |
172 if (!core_interface_ || !file_system_interface_ || !var_interface_ || | |
173 !var_resource_interface_) | |
174 return false; | |
175 | |
164 // Set up a special listener that only responds to a FINISHED_WAITING string. | 176 // Set up a special listener that only responds to a FINISHED_WAITING string. |
165 // This is for use by WaitForMessages. | 177 // This is for use by WaitForMessages. |
166 std::string js_code; | 178 std::string js_code; |
167 // Note the following code is dependent on some features of test_case.html. | 179 // Note the following code is dependent on some features of test_case.html. |
168 // E.g., it is assumed that the DOM element where the plugin is embedded has | 180 // E.g., it is assumed that the DOM element where the plugin is embedded has |
169 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows | 181 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows |
170 // us to ignore the messages that are intended for use by the testing | 182 // us to ignore the messages that are intended for use by the testing |
171 // framework itself. | 183 // framework itself. |
172 js_code += "var plugin = document.getElementById('plugin');" | 184 js_code += "var plugin = document.getElementById('plugin');" |
173 "var wait_for_messages_handler = function(message_event) {" | 185 "var wait_for_messages_handler = function(message_event) {" |
(...skipping 20 matching lines...) Expand all Loading... | |
194 } | 206 } |
195 | 207 |
196 void TestPostMessage::RunTests(const std::string& filter) { | 208 void TestPostMessage::RunTests(const std::string& filter) { |
197 // Note: SendInInit must be first, because it expects to receive a message | 209 // Note: SendInInit must be first, because it expects to receive a message |
198 // that was sent in Init above. | 210 // that was sent in Init above. |
199 RUN_TEST(SendInInit, filter); | 211 RUN_TEST(SendInInit, filter); |
200 RUN_TEST(SendingData, filter); | 212 RUN_TEST(SendingData, filter); |
201 RUN_TEST(SendingArrayBuffer, filter); | 213 RUN_TEST(SendingArrayBuffer, filter); |
202 RUN_TEST(SendingArray, filter); | 214 RUN_TEST(SendingArray, filter); |
203 RUN_TEST(SendingDictionary, filter); | 215 RUN_TEST(SendingDictionary, filter); |
216 RUN_TEST(SendingResource, filter); | |
204 RUN_TEST(SendingComplexVar, filter); | 217 RUN_TEST(SendingComplexVar, filter); |
205 RUN_TEST(MessageEvent, filter); | 218 RUN_TEST(MessageEvent, filter); |
206 RUN_TEST(NoHandler, filter); | 219 RUN_TEST(NoHandler, filter); |
207 RUN_TEST(ExtraParam, filter); | 220 RUN_TEST(ExtraParam, filter); |
208 if (testing_interface_->IsOutOfProcess()) | 221 if (testing_interface_->IsOutOfProcess()) |
209 RUN_TEST(NonMainThread, filter); | 222 RUN_TEST(NonMainThread, filter); |
210 } | 223 } |
211 | 224 |
212 void TestPostMessage::HandleMessage(const pp::Var& message_data) { | 225 void TestPostMessage::HandleMessage(const pp::Var& message_data) { |
213 if (message_data.is_string() && | 226 if (message_data.is_string() && |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
246 bool TestPostMessage::PostMessageFromJavaScript(const std::string& func) { | 259 bool TestPostMessage::PostMessageFromJavaScript(const std::string& func) { |
247 std::string js_code; | 260 std::string js_code; |
248 js_code += "var plugin = document.getElementById('plugin');" | 261 js_code += "var plugin = document.getElementById('plugin');" |
249 "plugin.postMessage("; | 262 "plugin.postMessage("; |
250 js_code += func + "()"; | 263 js_code += func + "()"; |
251 js_code += " );"; | 264 js_code += " );"; |
252 instance_->EvalScript(js_code); | 265 instance_->EvalScript(js_code); |
253 return true; | 266 return true; |
254 } | 267 } |
255 | 268 |
269 bool TestPostMessage::PostMessageFromJavaScriptAsync(const std::string& func) { | |
270 // After the |func| calls callback, post both the given |message|, as well as | |
271 // the special message FINISHED_WAITING_MESSAGE. This ensures that | |
272 // WaitForMessagesAsync correctly waits until the callback is called. | |
273 std::string js_code; | |
274 js_code += "var plugin = document.getElementById('plugin');" | |
275 "var callback = function(message) {" | |
276 " plugin.postMessage(message);" | |
277 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');" | |
278 "};"; | |
279 js_code += "(" + func + ")(callback);"; | |
280 instance_->EvalScript(js_code); | |
281 return true; | |
282 } | |
283 | |
256 bool TestPostMessage::ClearListeners() { | 284 bool TestPostMessage::ClearListeners() { |
257 std::string js_code; | 285 std::string js_code; |
258 js_code += "var plugin = document.getElementById('plugin');" | 286 js_code += "var plugin = document.getElementById('plugin');" |
259 "while (plugin.eventListeners.length) {" | 287 "while (plugin.eventListeners.length) {" |
260 " plugin.removeEventListener('message'," | 288 " plugin.removeEventListener('message'," |
261 " plugin.eventListeners.pop());" | 289 " plugin.eventListeners.pop());" |
262 "}"; | 290 "}"; |
263 instance_->EvalScript(js_code); | 291 instance_->EvalScript(js_code); |
264 return true; | 292 return true; |
265 } | 293 } |
266 | 294 |
267 int TestPostMessage::WaitForMessages() { | 295 int TestPostMessage::WaitForMessages() { |
268 size_t message_size_before = message_data_.size(); | 296 size_t message_size_before = message_data_.size(); |
269 // We first post a FINISHED_WAITING_MESSAGE. This should be guaranteed to | 297 // We first post a FINISHED_WAITING_MESSAGE. This should be guaranteed to |
270 // come back _after_ any other incoming messages that were already pending. | 298 // come back _after_ any other incoming messages that were already pending. |
271 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE)); | 299 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE)); |
272 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 300 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
273 // Now that the FINISHED_WAITING_MESSAGE has been echoed back to us, we know | 301 // Now that the FINISHED_WAITING_MESSAGE has been echoed back to us, we know |
274 // that all pending messages have been slurped up. Return the number we | 302 // that all pending messages have been slurped up. Return the number we |
275 // received (which may be zero). | 303 // received (which may be zero). |
276 return message_data_.size() - message_size_before; | 304 return message_data_.size() - message_size_before; |
277 } | 305 } |
278 | 306 |
307 int TestPostMessage::WaitForMessagesAsync() { | |
308 size_t message_size_before = message_data_.size(); | |
309 // Unlike WaitForMessages, we do not post FINISHED_WAITING_MESSAGE. This is | |
310 // because PostMessageFromJavaScriptAsync will post it for us, when the | |
311 // asynchronous operation completes. | |
312 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
313 // Now that the FINISHED_WAITING_MESSAGE has been echoed back to us, we know | |
314 // that all pending messages have been slurped up. Return the number we | |
315 // received (which may be zero). | |
316 return message_data_.size() - message_size_before; | |
317 } | |
318 | |
279 std::string TestPostMessage::CheckMessageProperties( | 319 std::string TestPostMessage::CheckMessageProperties( |
280 const pp::Var& test_data, | 320 const pp::Var& test_data, |
281 const std::vector<std::string>& properties_to_check) { | 321 const std::vector<std::string>& properties_to_check) { |
282 typedef std::vector<std::string>::const_iterator Iterator; | 322 typedef std::vector<std::string>::const_iterator Iterator; |
283 for (Iterator iter = properties_to_check.begin(); | 323 for (Iterator iter = properties_to_check.begin(); |
284 iter != properties_to_check.end(); | 324 iter != properties_to_check.end(); |
285 ++iter) { | 325 ++iter) { |
286 ASSERT_TRUE(AddEchoingListener(*iter)); | 326 ASSERT_TRUE(AddEchoingListener(*iter)); |
287 message_data_.clear(); | 327 message_data_.clear(); |
288 instance_->PostMessage(test_data); | 328 instance_->PostMessage(test_data); |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
526 ASSERT_EQ(WaitForMessages(), 1); | 566 ASSERT_EQ(WaitForMessages(), 1); |
527 ASSERT_TRUE(message_data_.back().is_dictionary()); | 567 ASSERT_TRUE(message_data_.back().is_dictionary()); |
528 ASSERT_TRUE(VarsEqual(dictionary, message_data_.back())); | 568 ASSERT_TRUE(VarsEqual(dictionary, message_data_.back())); |
529 | 569 |
530 message_data_.clear(); | 570 message_data_.clear(); |
531 ASSERT_TRUE(ClearListeners()); | 571 ASSERT_TRUE(ClearListeners()); |
532 | 572 |
533 PASS(); | 573 PASS(); |
534 } | 574 } |
535 | 575 |
576 std::string TestPostMessage::TestSendingResource() { | |
577 // Clean up after previous tests. This also swallows the message sent by Init | |
578 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' | |
579 // should start with these. | |
580 WaitForMessages(); | |
581 message_data_.clear(); | |
582 ASSERT_TRUE(ClearListeners()); | |
583 | |
584 // Test sending a DOMFileSystem from JavaScript to the plugin. | |
585 ASSERT_TRUE(AddEchoingListener("message_event.data")); | |
586 PostMessageFromJavaScriptAsync( | |
587 "function(callback) { " | |
588 " window.webkitRequestFileSystem(window.TEMPORARY, 1024, callback);" | |
589 "}"); | |
590 ASSERT_EQ(message_data_.size(), 0); | |
591 ASSERT_EQ(WaitForMessagesAsync(), 1); | |
592 // TODO(mgiuca): Use the C++ API instead of the C API, when it is available. | |
593 PP_Var var = message_data_.back().Detach(); | |
594 PP_Resource result = var_resource_interface_->VarToResource(var); | |
595 ASSERT_TRUE(file_system_interface_->IsFileSystem(result)); | |
raymes
2013/10/22 05:59:21
Can we test some properties of the file system mat
Matt Giuca
2013/10/23 08:05:54
Done. (I just want you to know how hard this was!)
| |
596 core_interface_->ReleaseResource(result); | |
597 var_interface_->Release(var); | |
598 | |
599 WaitForMessages(); | |
600 message_data_.clear(); | |
601 ASSERT_TRUE(ClearListeners()); | |
602 | |
603 // TODO(mgiuca): Test roundtrip from plugin to JS and back, when the plugin to | |
604 // JS support is available. | |
605 | |
606 PASS(); | |
607 } | |
608 | |
536 std::string TestPostMessage::TestSendingComplexVar() { | 609 std::string TestPostMessage::TestSendingComplexVar() { |
537 // Clean up after previous tests. This also swallows the message sent by Init | 610 // Clean up after previous tests. This also swallows the message sent by Init |
538 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' | 611 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
539 // should start with these. | 612 // should start with these. |
540 WaitForMessages(); | 613 WaitForMessages(); |
541 message_data_.clear(); | 614 message_data_.clear(); |
542 ASSERT_TRUE(ClearListeners()); | 615 ASSERT_TRUE(ClearListeners()); |
543 | 616 |
544 pp::Var string(kTestString); | 617 pp::Var string(kTestString); |
545 pp::VarDictionary dictionary; | 618 pp::VarDictionary dictionary; |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
756 ASSERT_TRUE(received_value <= kThreadsToRun); | 829 ASSERT_TRUE(received_value <= kThreadsToRun); |
757 ++received_counts[received_value]; | 830 ++received_counts[received_value]; |
758 } | 831 } |
759 ASSERT_EQ(received_counts, expected_counts); | 832 ASSERT_EQ(received_counts, expected_counts); |
760 | 833 |
761 message_data_.clear(); | 834 message_data_.clear(); |
762 ASSERT_TRUE(ClearListeners()); | 835 ASSERT_TRUE(ClearListeners()); |
763 | 836 |
764 PASS(); | 837 PASS(); |
765 } | 838 } |
OLD | NEW |