| 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 "ppapi/c/dev/ppb_testing_dev.h" | 7 #include "ppapi/c/dev/ppb_testing_dev.h" |
| 8 #include "ppapi/c/pp_var.h" | 8 #include "ppapi/c/pp_var.h" |
| 9 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" | 9 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" |
| 10 #include "ppapi/cpp/instance.h" | 10 #include "ppapi/cpp/instance.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 } | 44 } |
| 45 | 45 |
| 46 void TestPostMessage::RunTest() { | 46 void TestPostMessage::RunTest() { |
| 47 RUN_TEST(SendingData); | 47 RUN_TEST(SendingData); |
| 48 RUN_TEST(MessageEvent); | 48 RUN_TEST(MessageEvent); |
| 49 RUN_TEST(NoHandler); | 49 RUN_TEST(NoHandler); |
| 50 } | 50 } |
| 51 | 51 |
| 52 void TestPostMessage::HandleMessage(const pp::Var& message_data) { | 52 void TestPostMessage::HandleMessage(const pp::Var& message_data) { |
| 53 message_data_.push_back(message_data); | 53 message_data_.push_back(message_data); |
| 54 testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
| 54 } | 55 } |
| 55 | 56 |
| 56 bool TestPostMessage::MakeOnMessageEcho(const std::string& expression) { | 57 bool TestPostMessage::MakeOnMessageEcho(const std::string& expression) { |
| 57 std::string js_code( | 58 std::string js_code( |
| 58 "document.getElementById('plugin').onmessage = function(message_event) {" | 59 "document.getElementById('plugin').onmessage = function(message_event) {" |
| 59 " document.getElementById('plugin').postMessage("); | 60 " document.getElementById('plugin').postMessage("); |
| 60 js_code += expression; | 61 js_code += expression; |
| 61 js_code += ");}"; | 62 js_code += ");}"; |
| 62 pp::Var exception; | 63 pp::Var exception; |
| 63 // TODO(dmichael): Move ExecuteScript to the testing interface. | 64 // TODO(dmichael): Move ExecuteScript to the testing interface. |
| 64 instance_->ExecuteScript(js_code, &exception); | 65 instance_->ExecuteScript(js_code, &exception); |
| 65 return(exception.is_undefined()); | 66 return(exception.is_undefined()); |
| 66 } | 67 } |
| 67 | 68 |
| 68 std::string TestPostMessage::TestSendingData() { | 69 std::string TestPostMessage::TestSendingData() { |
| 69 // Set up the JavaScript onmessage handler to echo the data part of the | 70 // Set up the JavaScript onmessage handler to echo the data part of the |
| 70 // message event back to us. | 71 // message event back to us. |
| 71 ASSERT_TRUE(MakeOnMessageEcho("message_event.data")); | 72 ASSERT_TRUE(MakeOnMessageEcho("message_event.data")); |
| 72 | 73 |
| 73 // Test sending a message to JavaScript for each supported type. The JS sends | 74 // 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 // the data back to us, and we check that they match. |
| 75 message_data_.clear(); | 76 message_data_.clear(); |
| 76 instance_->PostMessage(pp::Var(kTestString)); | 77 instance_->PostMessage(pp::Var(kTestString)); |
| 77 // Note that the trusted in-process version is completely synchronous, so we | 78 // PostMessage is asynchronous, so we should not receive a response yet. |
| 78 // do not need to use 'RunMessageLoop' to wait. | 79 ASSERT_EQ(message_data_.size(), 0); |
| 80 |
| 81 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 79 ASSERT_EQ(message_data_.size(), 1); | 82 ASSERT_EQ(message_data_.size(), 1); |
| 80 ASSERT_TRUE(message_data_.back().is_string()); | 83 ASSERT_TRUE(message_data_.back().is_string()); |
| 81 ASSERT_EQ(message_data_.back().AsString(), kTestString); | 84 ASSERT_EQ(message_data_.back().AsString(), kTestString); |
| 82 | 85 |
| 83 message_data_.clear(); | 86 message_data_.clear(); |
| 84 instance_->PostMessage(pp::Var(kTestBool)); | 87 instance_->PostMessage(pp::Var(kTestBool)); |
| 88 ASSERT_EQ(message_data_.size(), 0); |
| 89 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 85 ASSERT_EQ(message_data_.size(), 1); | 90 ASSERT_EQ(message_data_.size(), 1); |
| 86 ASSERT_TRUE(message_data_.back().is_bool()); | 91 ASSERT_TRUE(message_data_.back().is_bool()); |
| 87 ASSERT_EQ(message_data_.back().AsBool(), kTestBool); | 92 ASSERT_EQ(message_data_.back().AsBool(), kTestBool); |
| 88 | 93 |
| 89 message_data_.clear(); | 94 message_data_.clear(); |
| 90 instance_->PostMessage(pp::Var(kTestInt)); | 95 instance_->PostMessage(pp::Var(kTestInt)); |
| 96 ASSERT_EQ(message_data_.size(), 0); |
| 97 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 91 ASSERT_EQ(message_data_.size(), 1); | 98 ASSERT_EQ(message_data_.size(), 1); |
| 92 ASSERT_TRUE(message_data_.back().is_number()); | 99 ASSERT_TRUE(message_data_.back().is_number()); |
| 93 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), | 100 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), |
| 94 static_cast<double>(kTestInt)); | 101 static_cast<double>(kTestInt)); |
| 95 | 102 |
| 96 message_data_.clear(); | 103 message_data_.clear(); |
| 97 instance_->PostMessage(pp::Var(kTestDouble)); | 104 instance_->PostMessage(pp::Var(kTestDouble)); |
| 105 ASSERT_EQ(message_data_.size(), 0); |
| 106 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 98 ASSERT_EQ(message_data_.size(), 1); | 107 ASSERT_EQ(message_data_.size(), 1); |
| 99 ASSERT_TRUE(message_data_.back().is_number()); | 108 ASSERT_TRUE(message_data_.back().is_number()); |
| 100 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), kTestDouble); | 109 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), kTestDouble); |
| 101 | 110 |
| 102 message_data_.clear(); | 111 message_data_.clear(); |
| 103 instance_->PostMessage(pp::Var()); | 112 instance_->PostMessage(pp::Var()); |
| 113 ASSERT_EQ(message_data_.size(), 0); |
| 114 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 104 ASSERT_EQ(message_data_.size(), 1); | 115 ASSERT_EQ(message_data_.size(), 1); |
| 105 ASSERT_TRUE(message_data_.back().is_undefined()); | 116 ASSERT_TRUE(message_data_.back().is_undefined()); |
| 106 | 117 |
| 107 message_data_.clear(); | 118 message_data_.clear(); |
| 108 instance_->PostMessage(pp::Var(pp::Var::Null())); | 119 instance_->PostMessage(pp::Var(pp::Var::Null())); |
| 120 ASSERT_EQ(message_data_.size(), 0); |
| 121 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 109 ASSERT_EQ(message_data_.size(), 1); | 122 ASSERT_EQ(message_data_.size(), 1); |
| 110 ASSERT_TRUE(message_data_.back().is_null()); | 123 ASSERT_TRUE(message_data_.back().is_null()); |
| 111 | 124 |
| 112 PASS(); | 125 PASS(); |
| 113 } | 126 } |
| 114 | 127 |
| 115 std::string TestPostMessage::TestMessageEvent() { | 128 std::string TestPostMessage::TestMessageEvent() { |
| 116 // Set up the JavaScript onmessage handler to pass us some values from the | 129 // Set up the JavaScript onmessage handler to pass us some values from the |
| 117 // MessageEvent and make sure they match our expectations. | 130 // MessageEvent and make sure they match our expectations. |
| 118 | 131 |
| 119 // Have onmessage pass back the type of message_event and make sure it's | 132 // Have onmessage pass back the type of message_event and make sure it's |
| 120 // "object". | 133 // "object". |
| 121 ASSERT_TRUE(MakeOnMessageEcho("typeof(message_event)")); | 134 ASSERT_TRUE(MakeOnMessageEcho("typeof(message_event)")); |
| 122 message_data_.clear(); | 135 message_data_.clear(); |
| 123 instance_->PostMessage(pp::Var(kTestInt)); | 136 instance_->PostMessage(pp::Var(kTestInt)); |
| 137 ASSERT_EQ(message_data_.size(), 0); |
| 138 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 124 ASSERT_EQ(message_data_.size(), 1); | 139 ASSERT_EQ(message_data_.size(), 1); |
| 125 ASSERT_TRUE(message_data_.back().is_string()); | 140 ASSERT_TRUE(message_data_.back().is_string()); |
| 126 ASSERT_EQ(message_data_.back().AsString(), "object"); | 141 ASSERT_EQ(message_data_.back().AsString(), "object"); |
| 127 | 142 |
| 128 // Make sure all the non-data properties have the expected values. | 143 // Make sure all the non-data properties have the expected values. |
| 129 bool success = MakeOnMessageEcho("((message_event.origin == '')" | 144 bool success = MakeOnMessageEcho("((message_event.origin == '')" |
| 130 " && (message_event.lastEventId == '')" | 145 " && (message_event.lastEventId == '')" |
| 131 " && (message_event.source == null)" | 146 " && (message_event.source == null)" |
| 132 " && (message_event.ports == null)" | 147 " && (message_event.ports == null)" |
| 133 " && (message_event.bubbles == false)" | 148 " && (message_event.bubbles == false)" |
| 134 " && (message_event.cancelable == false)" | 149 " && (message_event.cancelable == false)" |
| 135 ")"); | 150 ")"); |
| 136 ASSERT_TRUE(success); | 151 ASSERT_TRUE(success); |
| 137 message_data_.clear(); | 152 message_data_.clear(); |
| 138 instance_->PostMessage(pp::Var(kTestInt)); | 153 instance_->PostMessage(pp::Var(kTestInt)); |
| 139 // Note that the trusted in-process version is completely synchronous, so we | 154 ASSERT_EQ(message_data_.size(), 0); |
| 140 // do not need to use 'RunMessageLoop' to wait. | 155 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 141 ASSERT_EQ(message_data_.size(), 1); | 156 ASSERT_EQ(message_data_.size(), 1); |
| 142 ASSERT_TRUE(message_data_.back().is_bool()); | 157 ASSERT_TRUE(message_data_.back().is_bool()); |
| 143 ASSERT_TRUE(message_data_.back().AsBool()); | 158 ASSERT_TRUE(message_data_.back().AsBool()); |
| 144 | 159 |
| 145 PASS(); | 160 PASS(); |
| 146 } | 161 } |
| 147 | 162 |
| 148 std::string TestPostMessage::TestNoHandler() { | 163 std::string TestPostMessage::TestNoHandler() { |
| 149 // Delete the onmessage handler (if it exists) | 164 // Delete the onmessage handler (if it exists) |
| 150 std::string js_code( | 165 std::string js_code( |
| 151 "if (document.getElementById('plugin').onmessage) {" | 166 "if (document.getElementById('plugin').onmessage) {" |
| 152 " delete document.getElementById('plugin').onmessage;" | 167 " delete document.getElementById('plugin').onmessage;" |
| 153 "}"); | 168 "}"); |
| 154 pp::Var exception; | 169 pp::Var exception; |
| 155 instance_->ExecuteScript(js_code, &exception); | 170 instance_->ExecuteScript(js_code, &exception); |
| 156 ASSERT_TRUE(exception.is_undefined()); | 171 ASSERT_TRUE(exception.is_undefined()); |
| 157 | 172 |
| 158 // Now send a message and make sure we don't get anything back (and that we | 173 // Now send a message and make sure we don't get anything back (and that we |
| 159 // don't crash). | 174 // don't crash). |
| 160 message_data_.clear(); | 175 message_data_.clear(); |
| 161 instance_->PostMessage(pp::Var()); | 176 instance_->PostMessage(pp::Var()); |
| 177 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 162 ASSERT_EQ(message_data_.size(), 0); | 178 ASSERT_EQ(message_data_.size(), 0); |
| 163 | 179 |
| 164 PASS(); | 180 PASS(); |
| 165 } | 181 } |
| 166 | 182 |
| OLD | NEW |