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 10 matching lines...) Expand all Loading... |
21 #endif | 21 #endif |
22 | 22 |
23 REGISTER_TEST_CASE(PostMessage); | 23 REGISTER_TEST_CASE(PostMessage); |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const char kTestString[] = "Hello world!"; | 27 const char kTestString[] = "Hello world!"; |
28 const bool kTestBool = true; | 28 const bool kTestBool = true; |
29 const int32_t kTestInt = 42; | 29 const int32_t kTestInt = 42; |
30 const double kTestDouble = 42.0; | 30 const double kTestDouble = 42.0; |
31 const int32_t kThreadsToRun = 10; | 31 const int32_t kThreadsToRun = 4; |
32 const int32_t kMessagesToSendPerThread = 50; | 32 const int32_t kMessagesToSendPerThread = 10; |
33 | 33 |
34 // The struct that invoke_post_message_thread_func expects for its argument. | 34 // The struct that invoke_post_message_thread_func expects for its argument. |
35 // It includes the instance on which to invoke PostMessage, and the value to | 35 // It includes the instance on which to invoke PostMessage, and the value to |
36 // pass to PostMessage. | 36 // pass to PostMessage. |
37 struct InvokePostMessageThreadArg { | 37 struct InvokePostMessageThreadArg { |
38 InvokePostMessageThreadArg(pp::Instance* i, const pp::Var& v) | 38 InvokePostMessageThreadArg(pp::Instance* i, const pp::Var& v) |
39 : instance(i), value_to_send(v) {} | 39 : instance(i), value_to_send(v) {} |
40 pp::Instance* instance; | 40 pp::Instance* instance; |
41 pp::Var value_to_send; | 41 pp::Var value_to_send; |
42 }; | 42 }; |
43 | 43 |
44 void InvokePostMessageThreadFunc(void* user_data) { | 44 void InvokePostMessageThreadFunc(void* user_data) { |
45 InvokePostMessageThreadArg* arg = | 45 InvokePostMessageThreadArg* arg = |
46 static_cast<InvokePostMessageThreadArg*>(user_data); | 46 static_cast<InvokePostMessageThreadArg*>(user_data); |
47 for (int32_t i = 0; i < kMessagesToSendPerThread; ++i) | 47 for (int32_t i = 0; i < kMessagesToSendPerThread; ++i) |
48 arg->instance->PostMessage(arg->value_to_send); | 48 arg->instance->PostMessage(arg->value_to_send); |
49 delete arg; | 49 delete arg; |
50 } | 50 } |
51 | 51 |
| 52 #define FINISHED_WAITING_MESSAGE "TEST_POST_MESSAGE_FINISHED_WAITING" |
| 53 |
52 } // namespace | 54 } // namespace |
53 | 55 |
| 56 TestPostMessage::TestPostMessage(TestingInstance* instance) |
| 57 : TestCase(instance) { |
| 58 } |
| 59 |
| 60 TestPostMessage::~TestPostMessage() { |
| 61 // Remove the special listener that only responds to a FINISHED_WAITING |
| 62 // string. See Init for where it gets added. |
| 63 std::string js_code; |
| 64 js_code += "var plugin = document.getElementById('plugin');" |
| 65 "plugin.removeEventListener('message'," |
| 66 " plugin.wait_for_messages_handler);" |
| 67 "delete plugin.wait_for_messages_handler;"; |
| 68 pp::Var exception; |
| 69 instance_->ExecuteScript(js_code, &exception); |
| 70 } |
| 71 |
54 bool TestPostMessage::Init() { | 72 bool TestPostMessage::Init() { |
55 return InitTestingInterface(); | 73 bool success = InitTestingInterface(); |
| 74 |
| 75 // Set up a special listener that only responds to a FINISHED_WAITING string. |
| 76 // This is for use by WaitForMessages. |
| 77 std::string js_code; |
| 78 // 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 |
| 80 // 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 |
| 82 // framework itself. |
| 83 js_code += "var plugin = document.getElementById('plugin');" |
| 84 "var wait_for_messages_handler = function(message_event) {" |
| 85 " if (!IsTestingMessage(message_event.data) &&" |
| 86 " message_event.data === '" FINISHED_WAITING_MESSAGE "') {" |
| 87 " plugin.postMessage('" FINISHED_WAITING_MESSAGE "');" |
| 88 " }" |
| 89 "};" |
| 90 "plugin.addEventListener('message', wait_for_messages_handler);" |
| 91 // Stash it on the plugin so we can remove it in the destructor. |
| 92 "plugin.wait_for_messages_handler = wait_for_messages_handler;"; |
| 93 pp::Var exception; |
| 94 instance_->ExecuteScript(js_code, &exception); |
| 95 success = success && exception.is_undefined(); |
| 96 |
| 97 // Set up the JavaScript message event listener to echo the data part of the |
| 98 // message event back to us. |
| 99 success = success && AddEchoingListener("message_event.data"); |
| 100 message_data_.clear(); |
| 101 // 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 |
| 103 // the stack. |
| 104 instance_->PostMessage(pp::Var(kTestString)); |
| 105 |
| 106 return success; |
56 } | 107 } |
57 | 108 |
58 void TestPostMessage::RunTest() { | 109 void TestPostMessage::RunTest() { |
| 110 // Note: SendInInit must be first, because it expects to receive a message |
| 111 // that was sent in Init above. |
| 112 RUN_TEST(SendInInit); |
59 RUN_TEST(SendingData); | 113 RUN_TEST(SendingData); |
60 RUN_TEST(MessageEvent); | 114 RUN_TEST(MessageEvent); |
61 RUN_TEST(NoHandler); | 115 RUN_TEST(NoHandler); |
62 RUN_TEST(ExtraParam); | 116 RUN_TEST(ExtraParam); |
63 if (testing_interface_->IsOutOfProcess()) | 117 if (testing_interface_->IsOutOfProcess()) |
64 RUN_TEST(NonMainThread); | 118 RUN_TEST(NonMainThread); |
65 } | 119 } |
66 | 120 |
67 void TestPostMessage::HandleMessage(const pp::Var& message_data) { | 121 void TestPostMessage::HandleMessage(const pp::Var& message_data) { |
68 message_data_.push_back(message_data); | 122 if (message_data.is_string() && |
69 testing_interface_->QuitMessageLoop(instance_->pp_instance()); | 123 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) |
| 124 testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
| 125 else |
| 126 message_data_.push_back(message_data); |
70 } | 127 } |
71 | 128 |
72 bool TestPostMessage::AddEchoingListener(const std::string& expression) { | 129 bool TestPostMessage::AddEchoingListener(const std::string& expression) { |
73 std::string js_code; | 130 std::string js_code; |
74 // Note the following code is dependent on some features of test_case.html. | 131 // Note the following code is dependent on some features of test_case.html. |
75 // E.g., it is assumed that the DOM element where the plugin is embedded has | 132 // E.g., it is assumed that the DOM element where the plugin is embedded has |
76 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows | 133 // an id of 'plugin', and there is a function 'IsTestingMessage' that allows |
77 // us to ignore the messages that are intended for use by the testing | 134 // us to ignore the messages that are intended for use by the testing |
78 // framework itself. | 135 // framework itself. |
79 js_code += "var plugin = document.getElementById('plugin');" | 136 js_code += "var plugin = document.getElementById('plugin');" |
80 "var message_handler = function(message_event) {" | 137 "var message_handler = function(message_event) {" |
81 " if (!IsTestingMessage(message_event.data)) {" | 138 " if (!IsTestingMessage(message_event.data) &&" |
| 139 " !(message_event.data === '" FINISHED_WAITING_MESSAGE "')) {" |
82 " plugin.postMessage("; | 140 " plugin.postMessage("; |
83 js_code += expression; | 141 js_code += expression; |
84 js_code += " );" | 142 js_code += " );" |
85 " }" | 143 " }" |
86 "};" | 144 "};" |
87 "plugin.addEventListener('message', message_handler);" | 145 "plugin.addEventListener('message', message_handler);" |
88 // Maintain an array of all event listeners, attached to the | 146 // Maintain an array of all event listeners, attached to the |
89 // plugin. This is so that we can easily remove them later (see | 147 // plugin. This is so that we can easily remove them later (see |
90 // ClearListeners()). | 148 // ClearListeners()). |
91 "if (!plugin.eventListeners) plugin.eventListeners = [];" | 149 "if (!plugin.eventListeners) plugin.eventListeners = [];" |
92 "plugin.eventListeners.push(message_handler);"; | 150 "plugin.eventListeners.push(message_handler);"; |
93 pp::Var exception; | 151 pp::Var exception; |
94 instance_->ExecuteScript(js_code, &exception); | 152 instance_->ExecuteScript(js_code, &exception); |
95 return exception.is_undefined(); | 153 return exception.is_undefined(); |
96 } | 154 } |
97 | 155 |
98 bool TestPostMessage::ClearListeners() { | 156 bool TestPostMessage::ClearListeners() { |
99 std::string js_code( | 157 std::string js_code( |
100 "var plugin = document.getElementById('plugin');" | 158 "var plugin = document.getElementById('plugin');" |
101 "while (plugin.eventListeners.length) {" | 159 "while (plugin.eventListeners.length) {" |
102 " plugin.removeEventListener('message', plugin.eventListeners.pop());" | 160 " plugin.removeEventListener('message', plugin.eventListeners.pop());" |
103 "}"); | 161 "}"); |
104 pp::Var exception; | 162 pp::Var exception; |
105 instance_->ExecuteScript(js_code, &exception); | 163 instance_->ExecuteScript(js_code, &exception); |
106 return(exception.is_undefined()); | 164 return(exception.is_undefined()); |
107 } | 165 } |
108 | 166 |
| 167 int TestPostMessage::WaitForMessages() { |
| 168 size_t message_size_before = message_data_.size(); |
| 169 // We first post a FINISHED_WAITING_MESSAGE. This should be guaranteed to |
| 170 // come back _after_ any other incoming messages that were already pending. |
| 171 instance_->PostMessage(pp::Var(FINISHED_WAITING_MESSAGE)); |
| 172 testing_interface_->RunMessageLoop(instance_->pp_instance()); |
| 173 // Now that the FINISHED_WAITING_MESSAGE has been echoed back to us, we know |
| 174 // that all pending messages have been slurped up. Return the number we |
| 175 // received (which may be zero). |
| 176 return message_data_.size() - message_size_before; |
| 177 } |
| 178 |
| 179 std::string TestPostMessage::TestSendInInit() { |
| 180 ASSERT_EQ(WaitForMessages(), 1); |
| 181 // This test assumes Init already sent a message. |
| 182 ASSERT_EQ(message_data_.size(), 1); |
| 183 ASSERT_TRUE(message_data_.back().is_string()); |
| 184 ASSERT_EQ(message_data_.back().AsString(), kTestString); |
| 185 PASS(); |
| 186 } |
| 187 |
109 std::string TestPostMessage::TestSendingData() { | 188 std::string TestPostMessage::TestSendingData() { |
110 // Set up the JavaScript message event listener to echo the data part of the | 189 // Set up the JavaScript message event listener to echo the data part of the |
111 // message event back to us. | 190 // message event back to us. |
| 191 ASSERT_TRUE(ClearListeners()); |
112 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 192 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
113 | 193 |
114 // Test sending a message to JavaScript for each supported type. The JS sends | 194 // Test sending a message to JavaScript for each supported type. The JS sends |
115 // the data back to us, and we check that they match. | 195 // the data back to us, and we check that they match. |
116 message_data_.clear(); | 196 message_data_.clear(); |
117 instance_->PostMessage(pp::Var(kTestString)); | 197 instance_->PostMessage(pp::Var(kTestString)); |
118 // PostMessage is asynchronous, so we should not receive a response yet. | 198 // PostMessage is asynchronous, so we should not receive a response yet. |
119 ASSERT_EQ(message_data_.size(), 0); | 199 ASSERT_EQ(message_data_.size(), 0); |
120 | 200 ASSERT_EQ(WaitForMessages(), 1); |
121 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
122 ASSERT_EQ(message_data_.size(), 1); | |
123 ASSERT_TRUE(message_data_.back().is_string()); | 201 ASSERT_TRUE(message_data_.back().is_string()); |
124 ASSERT_EQ(message_data_.back().AsString(), kTestString); | 202 ASSERT_EQ(message_data_.back().AsString(), kTestString); |
125 | 203 |
126 message_data_.clear(); | 204 message_data_.clear(); |
127 instance_->PostMessage(pp::Var(kTestBool)); | 205 instance_->PostMessage(pp::Var(kTestBool)); |
128 ASSERT_EQ(message_data_.size(), 0); | 206 ASSERT_EQ(message_data_.size(), 0); |
129 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 207 ASSERT_EQ(WaitForMessages(), 1); |
130 ASSERT_EQ(message_data_.size(), 1); | |
131 ASSERT_TRUE(message_data_.back().is_bool()); | 208 ASSERT_TRUE(message_data_.back().is_bool()); |
132 ASSERT_EQ(message_data_.back().AsBool(), kTestBool); | 209 ASSERT_EQ(message_data_.back().AsBool(), kTestBool); |
133 | 210 |
134 message_data_.clear(); | 211 message_data_.clear(); |
135 instance_->PostMessage(pp::Var(kTestInt)); | 212 instance_->PostMessage(pp::Var(kTestInt)); |
136 ASSERT_EQ(message_data_.size(), 0); | 213 ASSERT_EQ(message_data_.size(), 0); |
137 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 214 ASSERT_EQ(WaitForMessages(), 1); |
138 ASSERT_EQ(message_data_.size(), 1); | |
139 ASSERT_TRUE(message_data_.back().is_number()); | 215 ASSERT_TRUE(message_data_.back().is_number()); |
140 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), | 216 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), |
141 static_cast<double>(kTestInt)); | 217 static_cast<double>(kTestInt)); |
142 | 218 |
143 message_data_.clear(); | 219 message_data_.clear(); |
144 instance_->PostMessage(pp::Var(kTestDouble)); | 220 instance_->PostMessage(pp::Var(kTestDouble)); |
145 ASSERT_EQ(message_data_.size(), 0); | 221 ASSERT_EQ(message_data_.size(), 0); |
146 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 222 ASSERT_EQ(WaitForMessages(), 1); |
147 ASSERT_EQ(message_data_.size(), 1); | |
148 ASSERT_TRUE(message_data_.back().is_number()); | 223 ASSERT_TRUE(message_data_.back().is_number()); |
149 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), kTestDouble); | 224 ASSERT_DOUBLE_EQ(message_data_.back().AsDouble(), kTestDouble); |
150 | 225 |
151 message_data_.clear(); | 226 message_data_.clear(); |
152 instance_->PostMessage(pp::Var()); | 227 instance_->PostMessage(pp::Var()); |
153 ASSERT_EQ(message_data_.size(), 0); | 228 ASSERT_EQ(message_data_.size(), 0); |
154 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 229 ASSERT_EQ(WaitForMessages(), 1); |
155 ASSERT_EQ(message_data_.size(), 1); | |
156 ASSERT_TRUE(message_data_.back().is_undefined()); | 230 ASSERT_TRUE(message_data_.back().is_undefined()); |
157 | 231 |
158 message_data_.clear(); | 232 message_data_.clear(); |
159 instance_->PostMessage(pp::Var(pp::Var::Null())); | 233 instance_->PostMessage(pp::Var(pp::Var::Null())); |
160 ASSERT_EQ(message_data_.size(), 0); | 234 ASSERT_EQ(message_data_.size(), 0); |
161 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 235 ASSERT_EQ(WaitForMessages(), 1); |
162 ASSERT_EQ(message_data_.size(), 1); | |
163 ASSERT_TRUE(message_data_.back().is_null()); | 236 ASSERT_TRUE(message_data_.back().is_null()); |
164 | 237 |
165 ASSERT_TRUE(ClearListeners()); | 238 ASSERT_TRUE(ClearListeners()); |
166 | 239 |
167 PASS(); | 240 PASS(); |
168 } | 241 } |
169 | 242 |
170 std::string TestPostMessage::TestMessageEvent() { | 243 std::string TestPostMessage::TestMessageEvent() { |
171 // Set up the JavaScript message event listener to pass us some values from | 244 // Set up the JavaScript message event listener to pass us some values from |
172 // the MessageEvent and make sure they match our expectations. | 245 // the MessageEvent and make sure they match our expectations. |
173 | 246 |
174 // Have the listener pass back the type of message_event and make sure it's | 247 // Have the listener pass back the type of message_event and make sure it's |
175 // "object". | 248 // "object". |
| 249 ASSERT_TRUE(ClearListeners()); |
176 ASSERT_TRUE(AddEchoingListener("typeof(message_event)")); | 250 ASSERT_TRUE(AddEchoingListener("typeof(message_event)")); |
177 message_data_.clear(); | 251 message_data_.clear(); |
178 instance_->PostMessage(pp::Var(kTestInt)); | 252 instance_->PostMessage(pp::Var(kTestInt)); |
179 ASSERT_EQ(message_data_.size(), 0); | 253 ASSERT_EQ(message_data_.size(), 0); |
180 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 254 ASSERT_EQ(WaitForMessages(), 1); |
181 ASSERT_EQ(message_data_.size(), 1); | |
182 ASSERT_TRUE(message_data_.back().is_string()); | 255 ASSERT_TRUE(message_data_.back().is_string()); |
183 ASSERT_EQ(message_data_.back().AsString(), "object"); | 256 ASSERT_EQ(message_data_.back().AsString(), "object"); |
184 ASSERT_TRUE(ClearListeners()); | 257 ASSERT_TRUE(ClearListeners()); |
185 | 258 |
186 // Make sure all the non-data properties have the expected values. | 259 // Make sure all the non-data properties have the expected values. |
187 bool success = AddEchoingListener("((message_event.origin == '')" | 260 bool success = AddEchoingListener("((message_event.origin == '')" |
188 " && (message_event.lastEventId == '')" | 261 " && (message_event.lastEventId == '')" |
189 " && (message_event.source == null)" | 262 " && (message_event.source == null)" |
190 " && (message_event.ports.length == 0)" | 263 " && (message_event.ports.length == 0)" |
191 " && (message_event.bubbles == false)" | 264 " && (message_event.bubbles == false)" |
192 " && (message_event.cancelable == false)" | 265 " && (message_event.cancelable == false)" |
193 ")"); | 266 ")"); |
194 ASSERT_TRUE(success); | 267 ASSERT_TRUE(success); |
195 message_data_.clear(); | 268 message_data_.clear(); |
196 instance_->PostMessage(pp::Var(kTestInt)); | 269 instance_->PostMessage(pp::Var(kTestInt)); |
197 ASSERT_EQ(message_data_.size(), 0); | 270 ASSERT_EQ(message_data_.size(), 0); |
198 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 271 ASSERT_EQ(WaitForMessages(), 1); |
199 ASSERT_EQ(message_data_.size(), 1); | |
200 ASSERT_TRUE(message_data_.back().is_bool()); | 272 ASSERT_TRUE(message_data_.back().is_bool()); |
201 ASSERT_TRUE(message_data_.back().AsBool()); | 273 ASSERT_TRUE(message_data_.back().AsBool()); |
202 ASSERT_TRUE(ClearListeners()); | 274 ASSERT_TRUE(ClearListeners()); |
203 | 275 |
204 // Add some event handlers to make sure they receive messages. | 276 // Add some event handlers to make sure they receive messages. |
205 ASSERT_TRUE(AddEchoingListener("1")); | 277 ASSERT_TRUE(AddEchoingListener("1")); |
206 ASSERT_TRUE(AddEchoingListener("2")); | 278 ASSERT_TRUE(AddEchoingListener("2")); |
207 ASSERT_TRUE(AddEchoingListener("3")); | 279 ASSERT_TRUE(AddEchoingListener("3")); |
208 | 280 |
209 message_data_.clear(); | 281 message_data_.clear(); |
210 instance_->PostMessage(pp::Var(kTestInt)); | 282 instance_->PostMessage(pp::Var(kTestInt)); |
211 // Make sure we don't get a response in a re-entrant fashion. | 283 // Make sure we don't get a response in a re-entrant fashion. |
212 ASSERT_EQ(message_data_.size(), 0); | 284 ASSERT_EQ(message_data_.size(), 0); |
213 // We should get 3 messages. | 285 // We should get 3 messages. |
214 testing_interface_->RunMessageLoop(instance_->pp_instance()); | 286 ASSERT_EQ(WaitForMessages(), 3); |
215 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
216 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
217 ASSERT_EQ(message_data_.size(), 3); | |
218 // Copy to a vector of doubles and sort; w3c does not specify the order for | 287 // Copy to a vector of doubles and sort; w3c does not specify the order for |
219 // event listeners. (Copying is easier than writing an operator< for pp::Var.) | 288 // event listeners. (Copying is easier than writing an operator< for pp::Var.) |
220 // | 289 // |
221 // See http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html. | 290 // See http://www.w3.org/TR/2000/REC-DOM-Level-2-Events-20001113/events.html. |
222 VarVector::iterator iter(message_data_.begin()), the_end(message_data_.end()); | 291 VarVector::iterator iter(message_data_.begin()), the_end(message_data_.end()); |
223 std::vector<double> double_vec; | 292 std::vector<double> double_vec; |
224 for (; iter != the_end; ++iter) { | 293 for (; iter != the_end; ++iter) { |
225 ASSERT_TRUE(iter->is_number()); | 294 ASSERT_TRUE(iter->is_number()); |
226 double_vec.push_back(iter->AsDouble()); | 295 double_vec.push_back(iter->AsDouble()); |
227 } | 296 } |
228 std::sort(double_vec.begin(), double_vec.end()); | 297 std::sort(double_vec.begin(), double_vec.end()); |
229 ASSERT_DOUBLE_EQ(double_vec[0], 1.0); | 298 ASSERT_DOUBLE_EQ(double_vec[0], 1.0); |
230 ASSERT_DOUBLE_EQ(double_vec[1], 2.0); | 299 ASSERT_DOUBLE_EQ(double_vec[1], 2.0); |
231 ASSERT_DOUBLE_EQ(double_vec[2], 3.0); | 300 ASSERT_DOUBLE_EQ(double_vec[2], 3.0); |
232 | 301 |
233 ASSERT_TRUE(ClearListeners()); | 302 ASSERT_TRUE(ClearListeners()); |
234 | 303 |
235 PASS(); | 304 PASS(); |
236 } | 305 } |
237 | 306 |
238 std::string TestPostMessage::TestNoHandler() { | 307 std::string TestPostMessage::TestNoHandler() { |
239 // Delete any lingering event listeners. | 308 // Delete any lingering event listeners. |
240 ASSERT_TRUE(ClearListeners()); | 309 ASSERT_TRUE(ClearListeners()); |
241 | 310 |
242 // Now send a message. We shouldn't get a response. | 311 // Now send a message. We shouldn't get a response. |
243 message_data_.clear(); | 312 message_data_.clear(); |
244 instance_->PostMessage(pp::Var()); | 313 instance_->PostMessage(pp::Var()); |
245 // Note that at this point, if we call RunMessageLoop, we should hang, because | 314 ASSERT_EQ(WaitForMessages(), 0); |
246 // there should be no call to our HandleMessage function to quit the loop. | |
247 // Therefore, we will do CallOnMainThread to yield control. That event should | |
248 // fire, but we should see no messages when we return. | |
249 TestCompletionCallback callback(instance_->pp_instance()); | |
250 pp::Module::Get()->core()->CallOnMainThread(0, callback); | |
251 callback.WaitForResult(); | |
252 ASSERT_TRUE(message_data_.empty()); | 315 ASSERT_TRUE(message_data_.empty()); |
253 | 316 |
254 PASS(); | 317 PASS(); |
255 } | 318 } |
256 | 319 |
257 std::string TestPostMessage::TestExtraParam() { | 320 std::string TestPostMessage::TestExtraParam() { |
258 // Delete any lingering event listeners. | 321 // Delete any lingering event listeners. |
259 ASSERT_TRUE(ClearListeners()); | 322 ASSERT_TRUE(ClearListeners()); |
260 // Add a listener that will respond with 1 and an empty array (where the | 323 // Add a listener that will respond with 1 and an empty array (where the |
261 // message port array would appear if it was Worker postMessage). | 324 // message port array would appear if it was Worker postMessage). |
262 ASSERT_TRUE(AddEchoingListener("1, []")); | 325 ASSERT_TRUE(AddEchoingListener("1, []")); |
263 | 326 |
264 // Now send a message. We shouldn't get a response. | 327 // Now send a message. We shouldn't get a response. |
265 message_data_.clear(); | 328 message_data_.clear(); |
266 instance_->PostMessage(pp::Var()); | 329 instance_->PostMessage(pp::Var()); |
267 // Note that at this point, if we call RunMessageLoop, we should hang, because | 330 ASSERT_EQ(WaitForMessages(), 0); |
268 // there should be no call to our HandleMessage function to quit the loop. | |
269 // Therefore, we will do CallOnMainThread to yield control. That event should | |
270 // fire, but we should see no messages when we return. | |
271 TestCompletionCallback callback(instance_->pp_instance()); | |
272 pp::Module::Get()->core()->CallOnMainThread(0, callback); | |
273 callback.WaitForResult(); | |
274 ASSERT_TRUE(message_data_.empty()); | 331 ASSERT_TRUE(message_data_.empty()); |
275 | 332 |
276 PASS(); | 333 PASS(); |
277 } | 334 } |
278 | 335 |
279 std::string TestPostMessage::TestNonMainThread() { | 336 std::string TestPostMessage::TestNonMainThread() { |
280 ASSERT_TRUE(ClearListeners()); | 337 ASSERT_TRUE(ClearListeners()); |
281 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 338 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
282 message_data_.clear(); | 339 message_data_.clear(); |
283 | 340 |
(...skipping 21 matching lines...) Expand all Loading... |
305 | 362 |
306 // Make sure we got all values that we expected. Note that because it's legal | 363 // Make sure we got all values that we expected. Note that because it's legal |
307 // for the JavaScript engine to treat our integers as floating points, we | 364 // for the JavaScript engine to treat our integers as floating points, we |
308 // can't just use std::find or equality comparison. So we instead, we convert | 365 // can't just use std::find or equality comparison. So we instead, we convert |
309 // each incoming value to an integer, and count them in received_counts. | 366 // each incoming value to an integer, and count them in received_counts. |
310 int32_t expected_num = (kThreadsToRun + 1) * kMessagesToSendPerThread; | 367 int32_t expected_num = (kThreadsToRun + 1) * kMessagesToSendPerThread; |
311 // Count how many we receive per-index. | 368 // Count how many we receive per-index. |
312 std::vector<int32_t> expected_counts(kThreadsToRun + 1, | 369 std::vector<int32_t> expected_counts(kThreadsToRun + 1, |
313 kMessagesToSendPerThread); | 370 kMessagesToSendPerThread); |
314 std::vector<int32_t> received_counts(kThreadsToRun + 1, 0); | 371 std::vector<int32_t> received_counts(kThreadsToRun + 1, 0); |
| 372 ASSERT_EQ(WaitForMessages(), expected_num); |
315 for (int32_t i = 0; i < expected_num; ++i) { | 373 for (int32_t i = 0; i < expected_num; ++i) { |
316 // Run the message loop to get the next expected message. | 374 const pp::Var& latest_var(message_data_[i]); |
317 testing_interface_->RunMessageLoop(instance_->pp_instance()); | |
318 // Make sure we got another message in. | |
319 ASSERT_EQ(message_data_.size(), 1); | |
320 pp::Var latest_var(message_data_.back()); | |
321 message_data_.clear(); | |
322 | |
323 ASSERT_TRUE(latest_var.is_int() || latest_var.is_double()); | 375 ASSERT_TRUE(latest_var.is_int() || latest_var.is_double()); |
324 int32_t received_value = -1; | 376 int32_t received_value = -1; |
325 if (latest_var.is_int()) { | 377 if (latest_var.is_int()) { |
326 received_value = latest_var.AsInt(); | 378 received_value = latest_var.AsInt(); |
327 } else if (latest_var.is_double()) { | 379 } else if (latest_var.is_double()) { |
328 received_value = static_cast<int32_t>(latest_var.AsDouble() + 0.5); | 380 received_value = static_cast<int32_t>(latest_var.AsDouble() + 0.5); |
329 } | 381 } |
330 ASSERT_TRUE(received_value >= 0); | 382 ASSERT_TRUE(received_value >= 0); |
331 ASSERT_TRUE(received_value <= kThreadsToRun); | 383 ASSERT_TRUE(received_value <= kThreadsToRun); |
332 ++received_counts[received_value]; | 384 ++received_counts[received_value]; |
333 } | 385 } |
334 ASSERT_EQ(received_counts, expected_counts); | 386 ASSERT_EQ(received_counts, expected_counts); |
335 | 387 |
336 PASS(); | 388 PASS(); |
337 } | 389 } |
338 | 390 |
OLD | NEW |