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 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 success = success && AddEchoingListener("message_event.data"); | 99 success = success && AddEchoingListener("message_event.data"); |
100 message_data_.clear(); | 100 message_data_.clear(); |
101 // Send a message that the first test will expect to receive. This is to | 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 | 102 // verify that we can send messages when the 'Instance::Init' function is on |
103 // the stack. | 103 // the stack. |
104 instance_->PostMessage(pp::Var(kTestString)); | 104 instance_->PostMessage(pp::Var(kTestString)); |
105 | 105 |
106 return success; | 106 return success; |
107 } | 107 } |
108 | 108 |
109 void TestPostMessage::RunTest() { | 109 void TestPostMessage::RunTests(const std::string& filter) { |
110 // Note: SendInInit must be first, because it expects to receive a message | 110 // Note: SendInInit must be first, because it expects to receive a message |
111 // that was sent in Init above. | 111 // that was sent in Init above. |
112 RUN_TEST(SendInInit); | 112 RUN_TEST(SendInInit, filter); |
113 RUN_TEST(SendingData); | 113 RUN_TEST(SendingData, filter); |
114 RUN_TEST(MessageEvent); | 114 RUN_TEST(MessageEvent, filter); |
115 RUN_TEST(NoHandler); | 115 RUN_TEST(NoHandler, filter); |
116 RUN_TEST(ExtraParam); | 116 RUN_TEST(ExtraParam, filter); |
117 if (testing_interface_->IsOutOfProcess()) | 117 if (testing_interface_->IsOutOfProcess()) |
118 RUN_TEST(NonMainThread); | 118 RUN_TEST(NonMainThread, filter); |
119 } | 119 } |
120 | 120 |
121 void TestPostMessage::HandleMessage(const pp::Var& message_data) { | 121 void TestPostMessage::HandleMessage(const pp::Var& message_data) { |
122 if (message_data.is_string() && | 122 if (message_data.is_string() && |
123 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) | 123 (message_data.AsString() == FINISHED_WAITING_MESSAGE)) |
124 testing_interface_->QuitMessageLoop(instance_->pp_instance()); | 124 testing_interface_->QuitMessageLoop(instance_->pp_instance()); |
125 else | 125 else |
126 message_data_.push_back(message_data); | 126 message_data_.push_back(message_data); |
127 } | 127 } |
128 | 128 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 std::string TestPostMessage::TestSendInInit() { | 179 std::string TestPostMessage::TestSendInInit() { |
180 ASSERT_EQ(WaitForMessages(), 1); | 180 ASSERT_EQ(WaitForMessages(), 1); |
181 // This test assumes Init already sent a message. | 181 // This test assumes Init already sent a message. |
182 ASSERT_EQ(message_data_.size(), 1); | 182 ASSERT_EQ(message_data_.size(), 1); |
183 ASSERT_TRUE(message_data_.back().is_string()); | 183 ASSERT_TRUE(message_data_.back().is_string()); |
184 ASSERT_EQ(message_data_.back().AsString(), kTestString); | 184 ASSERT_EQ(message_data_.back().AsString(), kTestString); |
185 PASS(); | 185 PASS(); |
186 } | 186 } |
187 | 187 |
188 std::string TestPostMessage::TestSendingData() { | 188 std::string TestPostMessage::TestSendingData() { |
| 189 // Clean up after previous tests. This also swallows the message sent by Init |
| 190 // if we didn't run the 'SendInInit' test. All tests other than 'SendInInit' |
| 191 // should start with these. |
| 192 WaitForMessages(); |
| 193 ASSERT_TRUE(ClearListeners()); |
189 // Set up the JavaScript message event listener to echo the data part of the | 194 // Set up the JavaScript message event listener to echo the data part of the |
190 // message event back to us. | 195 // message event back to us. |
191 ASSERT_TRUE(ClearListeners()); | |
192 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 196 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
193 | 197 |
194 // Test sending a message to JavaScript for each supported type. The JS sends | 198 // Test sending a message to JavaScript for each supported type. The JS sends |
195 // the data back to us, and we check that they match. | 199 // the data back to us, and we check that they match. |
196 message_data_.clear(); | 200 message_data_.clear(); |
197 instance_->PostMessage(pp::Var(kTestString)); | 201 instance_->PostMessage(pp::Var(kTestString)); |
198 // PostMessage is asynchronous, so we should not receive a response yet. | 202 // PostMessage is asynchronous, so we should not receive a response yet. |
199 ASSERT_EQ(message_data_.size(), 0); | 203 ASSERT_EQ(message_data_.size(), 0); |
200 ASSERT_EQ(WaitForMessages(), 1); | 204 ASSERT_EQ(WaitForMessages(), 1); |
201 ASSERT_TRUE(message_data_.back().is_string()); | 205 ASSERT_TRUE(message_data_.back().is_string()); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 | 241 |
238 ASSERT_TRUE(ClearListeners()); | 242 ASSERT_TRUE(ClearListeners()); |
239 | 243 |
240 PASS(); | 244 PASS(); |
241 } | 245 } |
242 | 246 |
243 std::string TestPostMessage::TestMessageEvent() { | 247 std::string TestPostMessage::TestMessageEvent() { |
244 // Set up the JavaScript message event listener to pass us some values from | 248 // Set up the JavaScript message event listener to pass us some values from |
245 // the MessageEvent and make sure they match our expectations. | 249 // the MessageEvent and make sure they match our expectations. |
246 | 250 |
| 251 WaitForMessages(); |
| 252 ASSERT_TRUE(ClearListeners()); |
247 // Have the listener pass back the type of message_event and make sure it's | 253 // Have the listener pass back the type of message_event and make sure it's |
248 // "object". | 254 // "object". |
249 ASSERT_TRUE(ClearListeners()); | |
250 ASSERT_TRUE(AddEchoingListener("typeof(message_event)")); | 255 ASSERT_TRUE(AddEchoingListener("typeof(message_event)")); |
251 message_data_.clear(); | 256 message_data_.clear(); |
252 instance_->PostMessage(pp::Var(kTestInt)); | 257 instance_->PostMessage(pp::Var(kTestInt)); |
253 ASSERT_EQ(message_data_.size(), 0); | 258 ASSERT_EQ(message_data_.size(), 0); |
254 ASSERT_EQ(WaitForMessages(), 1); | 259 ASSERT_EQ(WaitForMessages(), 1); |
255 ASSERT_TRUE(message_data_.back().is_string()); | 260 ASSERT_TRUE(message_data_.back().is_string()); |
256 ASSERT_EQ(message_data_.back().AsString(), "object"); | 261 ASSERT_EQ(message_data_.back().AsString(), "object"); |
257 ASSERT_TRUE(ClearListeners()); | 262 ASSERT_TRUE(ClearListeners()); |
258 | 263 |
259 // Make sure all the non-data properties have the expected values. | 264 // Make sure all the non-data properties have the expected values. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 std::vector<double> double_vec; | 297 std::vector<double> double_vec; |
293 for (; iter != the_end; ++iter) { | 298 for (; iter != the_end; ++iter) { |
294 ASSERT_TRUE(iter->is_number()); | 299 ASSERT_TRUE(iter->is_number()); |
295 double_vec.push_back(iter->AsDouble()); | 300 double_vec.push_back(iter->AsDouble()); |
296 } | 301 } |
297 std::sort(double_vec.begin(), double_vec.end()); | 302 std::sort(double_vec.begin(), double_vec.end()); |
298 ASSERT_DOUBLE_EQ(double_vec[0], 1.0); | 303 ASSERT_DOUBLE_EQ(double_vec[0], 1.0); |
299 ASSERT_DOUBLE_EQ(double_vec[1], 2.0); | 304 ASSERT_DOUBLE_EQ(double_vec[1], 2.0); |
300 ASSERT_DOUBLE_EQ(double_vec[2], 3.0); | 305 ASSERT_DOUBLE_EQ(double_vec[2], 3.0); |
301 | 306 |
302 ASSERT_TRUE(ClearListeners()); | |
303 | |
304 PASS(); | 307 PASS(); |
305 } | 308 } |
306 | 309 |
307 std::string TestPostMessage::TestNoHandler() { | 310 std::string TestPostMessage::TestNoHandler() { |
308 // Delete any lingering event listeners. | 311 // Delete any lingering messages and event listeners. |
| 312 WaitForMessages(); |
309 ASSERT_TRUE(ClearListeners()); | 313 ASSERT_TRUE(ClearListeners()); |
310 | 314 |
311 // Now send a message. We shouldn't get a response. | 315 // Now send a message. We shouldn't get a response. |
312 message_data_.clear(); | 316 message_data_.clear(); |
313 instance_->PostMessage(pp::Var()); | 317 instance_->PostMessage(pp::Var()); |
314 ASSERT_EQ(WaitForMessages(), 0); | 318 ASSERT_EQ(WaitForMessages(), 0); |
315 ASSERT_TRUE(message_data_.empty()); | 319 ASSERT_TRUE(message_data_.empty()); |
316 | 320 |
317 PASS(); | 321 PASS(); |
318 } | 322 } |
319 | 323 |
320 std::string TestPostMessage::TestExtraParam() { | 324 std::string TestPostMessage::TestExtraParam() { |
321 // Delete any lingering event listeners. | 325 // Delete any lingering messages and event listeners. |
| 326 WaitForMessages(); |
322 ASSERT_TRUE(ClearListeners()); | 327 ASSERT_TRUE(ClearListeners()); |
323 // Add a listener that will respond with 1 and an empty array (where the | 328 // Add a listener that will respond with 1 and an empty array (where the |
324 // message port array would appear if it was Worker postMessage). | 329 // message port array would appear if it was Worker postMessage). |
325 ASSERT_TRUE(AddEchoingListener("1, []")); | 330 ASSERT_TRUE(AddEchoingListener("1, []")); |
326 | 331 |
327 // Now send a message. We shouldn't get a response. | 332 // Now send a message. We shouldn't get a response. |
328 message_data_.clear(); | 333 message_data_.clear(); |
329 instance_->PostMessage(pp::Var()); | 334 instance_->PostMessage(pp::Var()); |
330 ASSERT_EQ(WaitForMessages(), 0); | 335 ASSERT_EQ(WaitForMessages(), 0); |
331 ASSERT_TRUE(message_data_.empty()); | 336 ASSERT_TRUE(message_data_.empty()); |
332 | 337 |
333 PASS(); | 338 PASS(); |
334 } | 339 } |
335 | 340 |
336 std::string TestPostMessage::TestNonMainThread() { | 341 std::string TestPostMessage::TestNonMainThread() { |
| 342 WaitForMessages(); |
337 ASSERT_TRUE(ClearListeners()); | 343 ASSERT_TRUE(ClearListeners()); |
338 ASSERT_TRUE(AddEchoingListener("message_event.data")); | 344 ASSERT_TRUE(AddEchoingListener("message_event.data")); |
339 message_data_.clear(); | 345 message_data_.clear(); |
340 | 346 |
341 // Set up a thread for each integer from 0 to (kThreadsToRun - 1). Make each | 347 // Set up a thread for each integer from 0 to (kThreadsToRun - 1). Make each |
342 // thread send the number that matches its index kMessagesToSendPerThread | 348 // thread send the number that matches its index kMessagesToSendPerThread |
343 // times. For good measure, call postMessage from the main thread | 349 // times. For good measure, call postMessage from the main thread |
344 // kMessagesToSendPerThread times. At the end, we make sure we got all the | 350 // kMessagesToSendPerThread times. At the end, we make sure we got all the |
345 // values we expected. | 351 // values we expected. |
346 PP_ThreadType threads[kThreadsToRun]; | 352 PP_ThreadType threads[kThreadsToRun]; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 } | 387 } |
382 ASSERT_TRUE(received_value >= 0); | 388 ASSERT_TRUE(received_value >= 0); |
383 ASSERT_TRUE(received_value <= kThreadsToRun); | 389 ASSERT_TRUE(received_value <= kThreadsToRun); |
384 ++received_counts[received_value]; | 390 ++received_counts[received_value]; |
385 } | 391 } |
386 ASSERT_EQ(received_counts, expected_counts); | 392 ASSERT_EQ(received_counts, expected_counts); |
387 | 393 |
388 PASS(); | 394 PASS(); |
389 } | 395 } |
390 | 396 |
OLD | NEW |