Home | History | Annotate | Download | only in tests
      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 
      5 #include "ppapi/tests/test_flash_message_loop.h"
      6 
      7 #include "ppapi/c/pp_macros.h"
      8 #include "ppapi/cpp/core.h"
      9 #include "ppapi/cpp/logging.h"
     10 #include "ppapi/cpp/module.h"
     11 #include "ppapi/cpp/private/flash_message_loop.h"
     12 #include "ppapi/tests/testing_instance.h"
     13 
     14 REGISTER_TEST_CASE(FlashMessageLoop);
     15 
     16 TestFlashMessageLoop::TestFlashMessageLoop(TestingInstance* instance)
     17     : TestCase(instance),
     18       message_loop_(NULL),
     19       PP_ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)) {
     20 }
     21 
     22 TestFlashMessageLoop::~TestFlashMessageLoop() {
     23   PP_DCHECK(!message_loop_);
     24 }
     25 
     26 void TestFlashMessageLoop::RunTests(const std::string& filter) {
     27   RUN_TEST(Basics, filter);
     28   RUN_TEST(RunWithoutQuit, filter);
     29 }
     30 
     31 std::string TestFlashMessageLoop::TestBasics() {
     32   message_loop_ = new pp::flash::MessageLoop(instance_);
     33 
     34   pp::CompletionCallback callback = callback_factory_.NewCallback(
     35       &TestFlashMessageLoop::QuitMessageLoopTask);
     36   pp::Module::Get()->core()->CallOnMainThread(0, callback);
     37   int32_t result = message_loop_->Run();
     38 
     39   ASSERT_TRUE(message_loop_);
     40   delete message_loop_;
     41   message_loop_ = NULL;
     42 
     43   ASSERT_EQ(result, PP_OK);
     44   PASS();
     45 }
     46 
     47 std::string TestFlashMessageLoop::TestRunWithoutQuit() {
     48   message_loop_ = new pp::flash::MessageLoop(instance_);
     49 
     50   pp::CompletionCallback callback = callback_factory_.NewCallback(
     51       &TestFlashMessageLoop::DestroyMessageLoopResourceTask);
     52   pp::Module::Get()->core()->CallOnMainThread(0, callback);
     53   int32_t result = message_loop_->Run();
     54 
     55   if (message_loop_) {
     56     delete message_loop_;
     57     message_loop_ = NULL;
     58     ASSERT_TRUE(false);
     59   }
     60 
     61   ASSERT_EQ(result, PP_ERROR_ABORTED);
     62   PASS();
     63 }
     64 
     65 void TestFlashMessageLoop::QuitMessageLoopTask(int32_t unused) {
     66   if (message_loop_)
     67     message_loop_->Quit();
     68   else
     69     PP_NOTREACHED();
     70 }
     71 
     72 void TestFlashMessageLoop::DestroyMessageLoopResourceTask(int32_t unused) {
     73   if (message_loop_) {
     74     delete message_loop_;
     75     message_loop_ = NULL;
     76   } else {
     77     PP_NOTREACHED();
     78   }
     79 }
     80