test_message_loop.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright (c) 2012 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. #include "ppapi/tests/test_message_loop.h"
  5. #include "ppapi/c/pp_macros.h"
  6. #include "ppapi/cpp/core.h"
  7. #include "ppapi/cpp/logging.h"
  8. #include "ppapi/cpp/message_loop.h"
  9. #include "ppapi/cpp/module.h"
  10. #include "ppapi/tests/testing_instance.h"
  11. #include "ppapi/utility/threading/simple_thread.h"
  12. REGISTER_TEST_CASE(MessageLoop);
  13. TestMessageLoop::TestMessageLoop(TestingInstance* instance)
  14. : TestCase(instance),
  15. param_(kInvalid),
  16. callback_factory_(this),
  17. main_loop_task_ran_(instance->pp_instance()) {
  18. }
  19. TestMessageLoop::~TestMessageLoop() {
  20. }
  21. void TestMessageLoop::RunTests(const std::string& filter) {
  22. RUN_TEST(Basics, filter);
  23. RUN_TEST(Post, filter);
  24. }
  25. std::string TestMessageLoop::TestBasics() {
  26. // The main thread message loop should be valid, and equal to the "current"
  27. // one.
  28. ASSERT_NE(0, pp::MessageLoop::GetForMainThread().pp_resource());
  29. ASSERT_EQ(pp::MessageLoop::GetForMainThread().pp_resource(),
  30. pp::MessageLoop::GetCurrent().pp_resource());
  31. // We shouldn't be able to attach a new loop to the main thread.
  32. pp::MessageLoop loop(instance_);
  33. ASSERT_EQ(PP_ERROR_INPROGRESS, loop.AttachToCurrentThread());
  34. // Nested loops aren't allowed.
  35. ASSERT_EQ(PP_ERROR_INPROGRESS,
  36. pp::MessageLoop::GetForMainThread().Run());
  37. // We can't run on a loop that isn't attached to a thread.
  38. ASSERT_EQ(PP_ERROR_WRONG_THREAD, loop.Run());
  39. PASS();
  40. }
  41. std::string TestMessageLoop::TestPost() {
  42. // Make sure we can post a task from the main thread back to the main thread.
  43. pp::MessageLoop::GetCurrent().PostWork(callback_factory_.NewCallback(
  44. &TestMessageLoop::SetParamAndQuitTask, kMainToMain));
  45. main_loop_task_ran_.Wait();
  46. ASSERT_EQ(param_, kMainToMain);
  47. main_loop_task_ran_.Reset();
  48. pp::SimpleThread thread(instance_);
  49. // Post a task before the thread is started, to make sure it is run.
  50. // TODO(dmichael): CompletionCallbackFactory is not 100% thread safe for
  51. // posting tasks to a thread other than where the factory was created. It
  52. // should be OK for this test, since we know that the
  53. // CompletionCallbackFactory and its target object outlive all callbacks. But
  54. // developers are likely to misuse CompletionCallbackFactory. Maybe we should
  55. // make it safe to use a callback on another thread?
  56. thread.message_loop().PostWork(callback_factory_.NewCallback(
  57. &TestMessageLoop::EchoParamToMainTask, kBeforeStart));
  58. ASSERT_TRUE(thread.Start());
  59. main_loop_task_ran_.Wait();
  60. ASSERT_EQ(param_, kBeforeStart);
  61. main_loop_task_ran_.Reset();
  62. // Now post another one after start. This is the more normal case.
  63. // Nested loops aren't allowed.
  64. ASSERT_EQ(PP_ERROR_INPROGRESS,
  65. pp::MessageLoop::GetForMainThread().Run());
  66. thread.message_loop().PostWork(callback_factory_.NewCallback(
  67. &TestMessageLoop::EchoParamToMainTask, kAfterStart));
  68. main_loop_task_ran_.Wait();
  69. ASSERT_EQ(param_, kAfterStart);
  70. main_loop_task_ran_.Reset();
  71. // Quit and join the thread.
  72. ASSERT_TRUE(thread.Join());
  73. PASS();
  74. }
  75. void TestMessageLoop::SetParamAndQuitTask(int32_t result, TestParam param) {
  76. PP_DCHECK(result == PP_OK);
  77. param_ = param;
  78. main_loop_task_ran_.Signal();
  79. }
  80. void TestMessageLoop::EchoParamToMainTask(int32_t result, TestParam param) {
  81. PP_DCHECK(result == PP_OK);
  82. pp::MessageLoop::GetForMainThread().PostWork(
  83. callback_factory_.NewCallback(
  84. &TestMessageLoop::SetParamAndQuitTask, param));
  85. }