Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: ppapi/tests/test_post_message.cc

Issue 7648033: Add means for running some tests only o-o-p, add messaging o-o-p test. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix up windows threading Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "ppapi/cpp/dev/scriptable_object_deprecated.h" 11 #include "ppapi/cpp/dev/scriptable_object_deprecated.h"
12 #include "ppapi/cpp/instance.h" 12 #include "ppapi/cpp/instance.h"
13 #include "ppapi/cpp/var.h" 13 #include "ppapi/cpp/var.h"
14 #include "ppapi/tests/pp_thread.h"
14 #include "ppapi/tests/test_utils.h" 15 #include "ppapi/tests/test_utils.h"
15 #include "ppapi/tests/testing_instance.h" 16 #include "ppapi/tests/testing_instance.h"
16 17
17 // Windows defines 'PostMessage', so we have to undef it. 18 // Windows defines 'PostMessage', so we have to undef it.
18 #ifdef PostMessage 19 #ifdef PostMessage
19 #undef PostMessage 20 #undef PostMessage
20 #endif 21 #endif
21 22
22 REGISTER_TEST_CASE(PostMessage); 23 REGISTER_TEST_CASE(PostMessage);
23 24
24 namespace { 25 namespace {
25 26
26 const char kTestString[] = "Hello world!"; 27 const char kTestString[] = "Hello world!";
27 const bool kTestBool = true; 28 const bool kTestBool = true;
28 const int32_t kTestInt = 42; 29 const int32_t kTestInt = 42;
29 const double kTestDouble = 42.0; 30 const double kTestDouble = 42.0;
30 const int32_t kThreadsToRun = 10; 31 const int32_t kThreadsToRun = 10;
31 32
33 #if defined(PPAPI_HAS_POSIX_THREADS)
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
36 // pass to PostMessage.
37 struct InvokePostMessageThreadArg {
38 InvokePostMessageThreadArg(pp::Instance* i, const pp::Var& v)
39 : instance(i), value_to_send(v) {}
40 pp::Instance* instance;
41 pp::Var value_to_send;
42 };
43
44 void InvokePostMessageThreadFunc(void* user_data) {
45 InvokePostMessageThreadArg* arg(
46 static_cast<InvokePostMessageThreadArg*>(user_data));
47 arg->instance->PostMessage(arg->value_to_send);
48 delete arg;
49 }
50 #endif
51
32 } // namespace 52 } // namespace
33 53
34 bool TestPostMessage::Init() { 54 bool TestPostMessage::Init() {
35 return InitTestingInterface(); 55 return InitTestingInterface();
36 } 56 }
37 57
38 void TestPostMessage::RunTest() { 58 void TestPostMessage::RunTest() {
39 RUN_TEST(SendingData); 59 RUN_TEST(SendingData);
40 RUN_TEST(MessageEvent); 60 RUN_TEST(MessageEvent);
41 RUN_TEST(NoHandler); 61 RUN_TEST(NoHandler);
42 RUN_TEST(ExtraParam); 62 RUN_TEST(ExtraParam);
63 if (testing_interface_->IsOutOfProcess())
64 RUN_TEST(NonMainThread);
43 } 65 }
44 66
45 void TestPostMessage::HandleMessage(const pp::Var& message_data) { 67 void TestPostMessage::HandleMessage(const pp::Var& message_data) {
46 message_data_.push_back(message_data); 68 message_data_.push_back(message_data);
47 testing_interface_->QuitMessageLoop(instance_->pp_instance()); 69 testing_interface_->QuitMessageLoop(instance_->pp_instance());
48 } 70 }
49 71
50 bool TestPostMessage::AddEchoingListener(const std::string& expression) { 72 bool TestPostMessage::AddEchoingListener(const std::string& expression) {
51 std::string js_code; 73 std::string js_code;
52 // Note the following code is dependent on some features of test_case.html. 74 // Note the following code is dependent on some features of test_case.html.
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 // Therefore, we will do CallOnMainThread to yield control. That event should 269 // Therefore, we will do CallOnMainThread to yield control. That event should
248 // fire, but we should see no messages when we return. 270 // fire, but we should see no messages when we return.
249 TestCompletionCallback callback(instance_->pp_instance()); 271 TestCompletionCallback callback(instance_->pp_instance());
250 pp::Module::Get()->core()->CallOnMainThread(0, callback); 272 pp::Module::Get()->core()->CallOnMainThread(0, callback);
251 callback.WaitForResult(); 273 callback.WaitForResult();
252 ASSERT_TRUE(message_data_.empty()); 274 ASSERT_TRUE(message_data_.empty());
253 275
254 PASS(); 276 PASS();
255 } 277 }
256 278
279 std::string TestPostMessage::TestNonMainThread() {
280 // Set up the JavaScript onmessage handler to echo the data part of the
281 // message event back to us.
282 // Delete any lingering event listeners.
283 ASSERT_TRUE(ClearListeners());
284 // Add a listener that will respond with 1 and an empty array (where the
285 // message port array would appear if it was Worker postMessage).
286 ASSERT_TRUE(AddEchoingListener("message_event.data"));
287
288 message_data_.clear();
289
290 #if defined(PPAPI_HAS_POSIX_THREADS)
291 // Set up a thread for each integer from 0 to (kThreadsToRun - 1). Make each
292 // thread send the number that matches its index. For good measure, call
293 // postMessage from the main thread with numbers kThreadsToRun to
294 // (kThreadsToRun * 2 - 1). At the end, we make sure we got all the numbers
295 // we expected.
296 PP_ThreadType threads[kThreadsToRun];
297 for (int32_t i = 0; i < kThreadsToRun; ++i) {
298 // Set up a thread to send a value of i
299 void* arg = new InvokePostMessageThreadArg(instance_, pp::Var(i));
300 PP_CreateThread(&threads[i], &InvokePostMessageThreadFunc, arg);
301
302 // Invoke PostMessage right now to send a value of (kThreadsToRun + i).
303 instance_->PostMessage(pp::Var(kThreadsToRun + i));
304 }
305
306 // Now join all threads.
307 for (int32_t i = 0; i < kThreadsToRun; ++i) {
308 PP_JoinThread(threads[i]);
309 }
310
311 // PostMessage is asynchronous, so we should not receive a response yet.
312 ASSERT_EQ(message_data_.size(), 0);
313
314 // Make sure we got all values that we expected. Note that because it's legal
315 // for the JavaScript engine to treat our integers as floating points, we
316 // can't just use std::find or equality comparison. So we first put the
317 // integer version of all the received Vars in to received_ints, and build up
318 // expected_ints. After the loop, we can sort received_int (they may come out
319 // of order) and compare to the expected_ints.
320 int32_t expected_num = 2 * kThreadsToRun;
321 std::vector<int32_t> expected_ints;
322 std::vector<int32_t> received_ints;
323 for (int32_t i = 0; i < expected_num; ++i) {
324 expected_ints.push_back(i);
325
326 // Run the message loop to get the next expected message.
327 testing_interface_->RunMessageLoop(instance_->pp_instance());
328 // Make sure we got another message in.
329 ASSERT_EQ(message_data_.size(), static_cast<size_t>(i+1));
330
331 if (message_data_[i].is_int()) {
332 received_ints.push_back(message_data_[i].AsInt());
333 } else if (message_data_[i].is_double()) {
334 received_ints.push_back(
335 static_cast<int32_t>(message_data_[i].AsDouble() + 0.5));
336 }
337 }
338 // Check the size again, in case we received any non-numeric types that we
339 // didn't add to our int32_t vector.
340 ASSERT_EQ(received_ints.size(), expected_ints.size());
341 // Now sort received_ints; we have no guarantee as to the order in which we
342 // received them.
343 std::sort(received_ints.begin(), received_ints.end());
344 ASSERT_EQ(received_ints, expected_ints);
345 #endif
346 PASS();
347 }
348
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698