This source file includes following definitions.
- main_loop_task_ran_
- RunTests
- TestBasics
- TestPost
- SetParamAndQuitTask
- EchoParamToMainTask
#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() {
ASSERT_NE(0, pp::MessageLoop::GetForMainThread().pp_resource());
ASSERT_EQ(pp::MessageLoop::GetForMainThread().pp_resource(),
pp::MessageLoop::GetCurrent().pp_resource());
pp::MessageLoop loop(instance_);
ASSERT_EQ(PP_ERROR_INPROGRESS, loop.AttachToCurrentThread());
ASSERT_EQ(PP_ERROR_INPROGRESS,
pp::MessageLoop::GetForMainThread().Run());
ASSERT_EQ(PP_ERROR_WRONG_THREAD, loop.Run());
PASS();
}
std::string TestMessageLoop::TestPost() {
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_);
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();
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();
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));
}