OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ppapi/tests/test_post_message.h" |
| 6 |
| 7 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 8 #include "ppapi/c/pp_var.h" |
| 9 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" |
| 10 #include "ppapi/cpp/instance.h" |
| 11 #include "ppapi/cpp/var.h" |
| 12 #include "ppapi/tests/testing_instance.h" |
| 13 |
| 14 REGISTER_TEST_CASE(PostMessage); |
| 15 |
| 16 namespace { |
| 17 |
| 18 const PPB_Testing_Dev* GetTestingInterface() { |
| 19 static const PPB_Testing_Dev* g_testing_interface = |
| 20 reinterpret_cast<PPB_Testing_Dev const*>( |
| 21 pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE)); |
| 22 return g_testing_interface; |
| 23 } |
| 24 |
| 25 const char kTestString[] = "Hello world!"; |
| 26 const bool kTestBool = true; |
| 27 const int32_t kTestInt = 42; |
| 28 const double kTestDouble = 42.0; |
| 29 const int32_t kThreadsToRun = 10; |
| 30 |
| 31 } // namespace |
| 32 |
| 33 bool TestPostMessage::Init() { |
| 34 testing_interface_ = reinterpret_cast<const PPB_Testing_Dev*>( |
| 35 pp::Module::Get()->GetBrowserInterface(PPB_TESTING_DEV_INTERFACE)); |
| 36 if (!testing_interface_) { |
| 37 // Give a more helpful error message for the testing interface being gone |
| 38 // since that needs special enabling in Chrome. |
| 39 instance_->AppendError("This test needs the testing interface, which is " |
| 40 "not currently available. In Chrome, use --enable-pepper-testing when " |
| 41 "launching."); |
| 42 } |
| 43 return (testing_interface_ != NULL); |
| 44 } |
| 45 |
| 46 void TestPostMessage::RunTest() { |
| 47 RUN_TEST(SendingData); |
| 48 RUN_TEST(MessageEvent); |
| 49 RUN_TEST(NoHandler); |
| 50 } |
| 51 |
| 52 void TestPostMessage::HandleMessage(const pp::Var& message_data) { |
| 53 message_data_.push_back(message_data); |
| 54 } |
| 55 |
| 56 bool TestPostMessage::MakeOnMessageEcho(const std::string& expression) { |
| 57 std::string js_code( |
| 58 "document.getElementById('plugin').onmessage = function(message_event) {" |
| 59 " document.getElementById('plugin').postMessage("); |
| 60 js_code += expression; |
| 61 js_code += ");}"; |
| 62 pp::Var exception; |
| 63 // TODO(dmichael): Move ExecuteScript to the testing interface. |
| 64 instance_->ExecuteScript(js_code, &exception); |
| 65 return(exception.is_undefined()); |
| 66 } |
| 67 |
| 68 std::string TestPostMessage::TestSendingData() { |
| 69 // Set up the JavaScript onmessage handler to echo the data part of the |
| 70 // message event back to us. |
| 71 ASSERT_TRUE(MakeOnMessageEcho("message_event.data")); |
| 72 |
| 73 // Test sending a message to JavaScript for each supported type. The JS sends |
| 74 // the data back to us, and we check that they match. |
| 75 message_data_.clear(); |
| 76 instance_->PostMessage(pp::Var(kTestString)); |
| 77 // Note that the trusted in-process version is completely synchronous, so we |
| 78 // do not need to use 'RunMessageLoop' to wait. |
| 79 ASSERT_EQ(message_data_.size(), 1); |
| 80 ASSERT_TRUE(message_data_.back().is_string()); |
| 81 ASSERT_EQ(message_data_.back().AsString(), kTestString); |
| 82 |
| 83 message_data_.clear(); |
| 84 instance_->PostMessage(pp::Var(kTestBool)); |
| 85 ASSERT_EQ(message_data_.size(), 1); |
| 86 ASSERT_TRUE(message_data_.back().is_bool()); |
| 87 ASSERT_EQ(message_data_.back().AsBool(), kTestBool); |
| 88 |
| 89 message_data_.clear(); |
| 90 instance_->PostMessage(pp::Var(kTestInt)); |
| 91 ASSERT_EQ(message_data_.size(), 1); |
| 92 ASSERT_TRUE(message_data_.back().is_number()); |
| 93 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), |
| 94 static_cast<double>(kTestInt)); |
| 95 |
| 96 message_data_.clear(); |
| 97 instance_->PostMessage(pp::Var(kTestDouble)); |
| 98 ASSERT_EQ(message_data_.size(), 1); |
| 99 ASSERT_TRUE(message_data_.back().is_number()); |
| 100 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), kTestDouble); |
| 101 |
| 102 message_data_.clear(); |
| 103 instance_->PostMessage(pp::Var()); |
| 104 ASSERT_EQ(message_data_.size(), 1); |
| 105 ASSERT_TRUE(message_data_.back().is_undefined()); |
| 106 |
| 107 message_data_.clear(); |
| 108 instance_->PostMessage(pp::Var(pp::Var::Null())); |
| 109 ASSERT_EQ(message_data_.size(), 1); |
| 110 ASSERT_TRUE(message_data_.back().is_null()); |
| 111 |
| 112 PASS(); |
| 113 } |
| 114 |
| 115 std::string TestPostMessage::TestMessageEvent() { |
| 116 // Set up the JavaScript onmessage handler to pass us some values from the |
| 117 // MessageEvent and make sure they match our expectations. |
| 118 |
| 119 // Have onmessage pass back the type of message_event and make sure it's |
| 120 // "object". |
| 121 ASSERT_TRUE(MakeOnMessageEcho("typeof(message_event)")); |
| 122 message_data_.clear(); |
| 123 instance_->PostMessage(pp::Var(kTestInt)); |
| 124 ASSERT_EQ(message_data_.size(), 1); |
| 125 ASSERT_TRUE(message_data_.back().is_string()); |
| 126 ASSERT_EQ(message_data_.back().AsString(), "object"); |
| 127 |
| 128 // Make sure all the non-data properties have the expected values. |
| 129 bool success = MakeOnMessageEcho("((message_event.origin == '')" |
| 130 " && (message_event.lastEventId == '')" |
| 131 " && (message_event.source == null)" |
| 132 " && (message_event.ports == null)" |
| 133 " && (message_event.bubbles == false)" |
| 134 " && (message_event.cancelable == false)" |
| 135 ")"); |
| 136 ASSERT_TRUE(success); |
| 137 message_data_.clear(); |
| 138 instance_->PostMessage(pp::Var(kTestInt)); |
| 139 // Note that the trusted in-process version is completely synchronous, so we |
| 140 // do not need to use 'RunMessageLoop' to wait. |
| 141 ASSERT_EQ(message_data_.size(), 1); |
| 142 ASSERT_TRUE(message_data_.back().is_bool()); |
| 143 ASSERT_TRUE(message_data_.back().AsBool()); |
| 144 |
| 145 PASS(); |
| 146 } |
| 147 |
| 148 std::string TestPostMessage::TestNoHandler() { |
| 149 // Delete the onmessage handler (if it exists) |
| 150 std::string js_code( |
| 151 "if (document.getElementById('plugin').onmessage) {" |
| 152 " delete document.getElementById('plugin').onmessage;" |
| 153 "}"); |
| 154 pp::Var exception; |
| 155 instance_->ExecuteScript(js_code, &exception); |
| 156 ASSERT_TRUE(exception.is_undefined()); |
| 157 |
| 158 // Now send a message and make sure we don't get anything back (and that we |
| 159 // don't crash). |
| 160 message_data_.clear(); |
| 161 instance_->PostMessage(pp::Var()); |
| 162 ASSERT_EQ(message_data_.size(), 0); |
| 163 |
| 164 PASS(); |
| 165 } |
| 166 |
OLD | NEW |