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 <string.h> | 7 #include <string.h> |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <map> | 9 #include <map> |
10 #include <sstream> | 10 #include <sstream> |
11 | 11 |
12 #include "ppapi/c/dev/ppb_testing_dev.h" | 12 #include "ppapi/c/dev/ppb_testing_dev.h" |
13 #include "ppapi/c/pp_var.h" | 13 #include "ppapi/c/pp_var.h" |
14 #include "ppapi/c/ppb_file_io.h" | 14 #include "ppapi/c/ppb_file_io.h" |
| 15 #include "ppapi/cpp/dev/var_resource_dev.h" |
15 #include "ppapi/cpp/file_io.h" | 16 #include "ppapi/cpp/file_io.h" |
16 #include "ppapi/cpp/file_ref.h" | 17 #include "ppapi/cpp/file_ref.h" |
17 #include "ppapi/cpp/file_system.h" | 18 #include "ppapi/cpp/file_system.h" |
18 #include "ppapi/cpp/instance.h" | 19 #include "ppapi/cpp/instance.h" |
19 #include "ppapi/cpp/var.h" | 20 #include "ppapi/cpp/var.h" |
20 #include "ppapi/cpp/var_array.h" | 21 #include "ppapi/cpp/var_array.h" |
21 #include "ppapi/cpp/var_array_buffer.h" | 22 #include "ppapi/cpp/var_array_buffer.h" |
22 #include "ppapi/cpp/var_dictionary.h" | 23 #include "ppapi/cpp/var_dictionary.h" |
23 #include "ppapi/tests/pp_thread.h" | 24 #include "ppapi/tests/pp_thread.h" |
24 #include "ppapi/tests/test_utils.h" | 25 #include "ppapi/tests/test_utils.h" |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 js_code += "var plugin = document.getElementById('plugin');" | 161 js_code += "var plugin = document.getElementById('plugin');" |
161 "plugin.removeEventListener('message'," | 162 "plugin.removeEventListener('message'," |
162 " plugin.wait_for_messages_handler);" | 163 " plugin.wait_for_messages_handler);" |
163 "delete plugin.wait_for_messages_handler;"; | 164 "delete plugin.wait_for_messages_handler;"; |
164 instance_->EvalScript(js_code); | 165 instance_->EvalScript(js_code); |
165 } | 166 } |
166 | 167 |
167 bool TestPostMessage::Init() { | 168 bool TestPostMessage::Init() { |
168 bool success = CheckTestingInterface(); | 169 bool success = CheckTestingInterface(); |
169 | 170 |
170 core_interface_ = static_cast<const PPB_Core*>( | |
171 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE)); | |
172 file_system_interface_ = static_cast<const PPB_FileSystem*>( | |
173 pp::Module::Get()->GetBrowserInterface(PPB_FILESYSTEM_INTERFACE)); | |
174 var_interface_ = static_cast<const PPB_Var*>( | |
175 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE)); | |
176 var_resource_interface_ = static_cast<const PPB_VarResource_Dev*>( | |
177 pp::Module::Get()->GetBrowserInterface(PPB_VAR_RESOURCE_DEV_INTERFACE)); | |
178 if (!core_interface_ || !file_system_interface_ || !var_interface_ || | |
179 !var_resource_interface_) | |
180 return false; | |
181 | |
182 // Set up a special listener that only responds to a FINISHED_WAITING string. | 171 // Set up a special listener that only responds to a FINISHED_WAITING string. |
183 // This is for use by WaitForMessages. | 172 // This is for use by WaitForMessages. |
184 std::string js_code; | 173 std::string js_code; |
185 // Note the following code is dependent on some features of test_case.html. | 174 // Note the following code is dependent on some features of test_case.html. |
186 // E.g., it is assumed that the DOM element where the plugin is embedded has | 175 // E.g., it is assumed that the DOM element where the plugin is embedded has |
187 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows | 176 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows |
188 // us to ignore the messages that are intended for use by the testing | 177 // us to ignore the messages that are intended for use by the testing |
189 // framework itself. | 178 // framework itself. |
190 js_code += "var plugin = document.getElementById('plugin');" | 179 js_code += "var plugin = document.getElementById('plugin');" |
191 "var wait_for_messages_handler = function(message_event) {" | 180 "var wait_for_messages_handler = function(message_event) {" |
(...skipping 410 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 " writer.onwriteend = function() { callback(fileSystem); };" | 591 " writer.onwriteend = function() { callback(fileSystem); };" |
603 " var blob = new Blob(['"; | 592 " var blob = new Blob(['"; |
604 js_code += kTestString; | 593 js_code += kTestString; |
605 js_code += "'], {'type': 'text/plain'});" | 594 js_code += "'], {'type': 'text/plain'});" |
606 " writer.write(blob);" | 595 " writer.write(blob);" |
607 " });" | 596 " });" |
608 " }, function() { callback(null); });" | 597 " }, function() { callback(null); });" |
609 " }, function() { callback(null); });" | 598 " }, function() { callback(null); });" |
610 "}"; | 599 "}"; |
611 ASSERT_EQ(PostAsyncMessageFromJavaScriptAndWait(js_code), 1); | 600 ASSERT_EQ(PostAsyncMessageFromJavaScriptAndWait(js_code), 1); |
612 // TODO(mgiuca): Use the C++ API instead of the C API, when it is available. | 601 pp::Var var = message_data_.back(); |
613 PP_Var var = message_data_.back().Detach(); | 602 ASSERT_TRUE(var.is_resource()); |
614 PP_Resource result = var_resource_interface_->VarToResource(var); | 603 pp::VarResource_Dev var_resource(var); |
615 ASSERT_TRUE(file_system_interface_->IsFileSystem(result)); | 604 pp::Resource result = var_resource.AsResource(); |
| 605 ASSERT_TRUE(pp::FileSystem::IsFileSystem(result)); |
616 { | 606 { |
617 pp::FileSystem file_system(pp::PASS_REF, result); | 607 pp::FileSystem file_system(result); |
618 std::string file_path("/"); | 608 std::string file_path("/"); |
619 file_path += kTestFilename; | 609 file_path += kTestFilename; |
620 pp::FileRef file_ref(file_system, file_path.c_str()); | 610 pp::FileRef file_ref(file_system, file_path.c_str()); |
621 ASSERT_NE(0, file_ref.pp_resource()); | 611 ASSERT_NE(0, file_ref.pp_resource()); |
622 | 612 |
623 // Read the file and test that its contents match. | 613 // Read the file and test that its contents match. |
624 pp::FileIO file_io(instance_); | 614 pp::FileIO file_io(instance_); |
625 ASSERT_NE(0, file_io.pp_resource()); | 615 ASSERT_NE(0, file_io.pp_resource()); |
626 TestCompletionCallback callback(instance_->pp_instance(), | 616 TestCompletionCallback callback(instance_->pp_instance(), |
627 callback_type()); | 617 callback_type()); |
628 callback.WaitForResult( | 618 callback.WaitForResult( |
629 file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback.GetCallback())); | 619 file_io.Open(file_ref, PP_FILEOPENFLAG_READ, callback.GetCallback())); |
630 CHECK_CALLBACK_BEHAVIOR(callback); | 620 CHECK_CALLBACK_BEHAVIOR(callback); |
631 ASSERT_EQ(PP_OK, callback.result()); | 621 ASSERT_EQ(PP_OK, callback.result()); |
632 | 622 |
633 int length = strlen(kTestString); | 623 int length = strlen(kTestString); |
634 std::vector<char> buffer_vector(length); | 624 std::vector<char> buffer_vector(length); |
635 char* buffer = &buffer_vector[0]; // Note: Not null-terminated! | 625 char* buffer = &buffer_vector[0]; // Note: Not null-terminated! |
636 callback.WaitForResult( | 626 callback.WaitForResult( |
637 file_io.Read(0, buffer, length, callback.GetCallback())); | 627 file_io.Read(0, buffer, length, callback.GetCallback())); |
638 CHECK_CALLBACK_BEHAVIOR(callback); | 628 CHECK_CALLBACK_BEHAVIOR(callback); |
639 ASSERT_EQ(length, callback.result()); | 629 ASSERT_EQ(length, callback.result()); |
640 ASSERT_EQ(0, memcmp(buffer, kTestString, length)); | 630 ASSERT_EQ(0, memcmp(buffer, kTestString, length)); |
641 } | 631 } |
642 var_interface_->Release(var); | |
643 | 632 |
644 WaitForMessages(); | 633 WaitForMessages(); |
645 message_data_.clear(); | 634 message_data_.clear(); |
646 ASSERT_TRUE(ClearListeners()); | 635 ASSERT_TRUE(ClearListeners()); |
647 | 636 |
648 // TODO(mgiuca): Test roundtrip from plugin to JS and back, when the plugin to | 637 // TODO(mgiuca): Test roundtrip from plugin to JS and back, when the plugin to |
649 // JS support is available. | 638 // JS support is available. |
650 | 639 |
651 PASS(); | 640 PASS(); |
652 } | 641 } |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
874 ASSERT_TRUE(received_value <= kThreadsToRun); | 863 ASSERT_TRUE(received_value <= kThreadsToRun); |
875 ++received_counts[received_value]; | 864 ++received_counts[received_value]; |
876 } | 865 } |
877 ASSERT_EQ(received_counts, expected_counts); | 866 ASSERT_EQ(received_counts, expected_counts); |
878 | 867 |
879 message_data_.clear(); | 868 message_data_.clear(); |
880 ASSERT_TRUE(ClearListeners()); | 869 ASSERT_TRUE(ClearListeners()); |
881 | 870 |
882 PASS(); | 871 PASS(); |
883 } | 872 } |
OLD | NEW |