summaryrefslogtreecommitdiffstats
path: root/ppapi/tests/test_message_loop.cc
blob: 475d9b267818c6fa3540f14d000934cfb0a7352f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ppapi/tests/test_message_loop.h"

#include "ppapi/c/pp_macros.h"
#include "ppapi/cpp/core.h"
#include "ppapi/cpp/logging.h"
#include "ppapi/cpp/message_loop.h"
#include "ppapi/cpp/module.h"
#include "ppapi/tests/testing_instance.h"
#include "ppapi/utility/threading/simple_thread.h"

REGISTER_TEST_CASE(MessageLoop);

TestMessageLoop::TestMessageLoop(TestingInstance* instance)
    : TestCase(instance),
      param_(kInvalid),
      PP_ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)),
      main_loop_task_ran_(instance->pp_instance()) {
}

TestMessageLoop::~TestMessageLoop() {
}

void TestMessageLoop::RunTests(const std::string& filter) {
  RUN_TEST(Basics, filter);
  RUN_TEST(Post, filter);
}

std::string TestMessageLoop::TestBasics() {
  // The main thread message loop should be valid, and equal to the "current"
  // one.
  ASSERT_NE(0, pp::MessageLoop::GetForMainThread().pp_resource());
  ASSERT_EQ(pp::MessageLoop::GetForMainThread().pp_resource(),
            pp::MessageLoop::GetCurrent().pp_resource());

  // We shouldn't be able to attach a new loop to the main thread.
  pp::MessageLoop loop(instance_);
  ASSERT_EQ(PP_ERROR_INPROGRESS, loop.AttachToCurrentThread());

  // Nested loops aren't allowed.
  ASSERT_EQ(PP_ERROR_INPROGRESS,
            pp::MessageLoop::GetForMainThread().Run());

  // We can't run on a loop that isn't attached to a thread.
  ASSERT_EQ(PP_ERROR_WRONG_THREAD, loop.Run());

  PASS();
}

std::string TestMessageLoop::TestPost() {
  // Make sure we can post a task from the main thread back to the main thread.
  pp::MessageLoop::GetCurrent().PostWork(callback_factory_.NewCallback(
      &TestMessageLoop::SetParamAndQuitTask, kMainToMain));
  main_loop_task_ran_.Wait();
  ASSERT_EQ(param_, kMainToMain);
  main_loop_task_ran_.Reset();

  pp::SimpleThread thread(instance_);
  // Post a task before the thread is started, to make sure it is run.
  // TODO(dmichael): CompletionCallbackFactory is not 100% thread safe for
  // posting tasks to a thread other than where the factory was created. It
  // should be OK for this test, since we know that the
  // CompletionCallbackFactory and its target object outlive all callbacks. But
  // developers are likely to misuse CompletionCallbackFactory. Maybe we should
  // make it safe to use a callback on another thread?
  thread.message_loop().PostWork(callback_factory_.NewCallback(
      &TestMessageLoop::EchoParamToMainTask, kBeforeStart));
  ASSERT_TRUE(thread.Start());
  main_loop_task_ran_.Wait();
  ASSERT_EQ(param_, kBeforeStart);
  main_loop_task_ran_.Reset();

  // Now post another one after start. This is the more normal case.

  // Nested loops aren't allowed.
  ASSERT_EQ(PP_ERROR_INPROGRESS,
            pp::MessageLoop::GetForMainThread().Run());
  thread.message_loop().PostWork(callback_factory_.NewCallback(
      &TestMessageLoop::EchoParamToMainTask, kAfterStart));
  main_loop_task_ran_.Wait();
  ASSERT_EQ(param_, kAfterStart);
  main_loop_task_ran_.Reset();

  // Quit and join the thread.
  ASSERT_TRUE(thread.Join());

  PASS();
}

void TestMessageLoop::SetParamAndQuitTask(int32_t result, TestParam param) {
  PP_DCHECK(result == PP_OK);
  param_ = param;
  main_loop_task_ran_.Signal();
}

void TestMessageLoop::EchoParamToMainTask(int32_t result, TestParam param) {
  PP_DCHECK(result == PP_OK);
  pp::MessageLoop::GetForMainThread().PostWork(
      callback_factory_.NewCallback(
          &TestMessageLoop::SetParamAndQuitTask, param));
}