media/mtransport/test/TestSyncRunnable.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/mtransport/test/TestSyncRunnable.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,77 @@
     1.4 +/* -*- Mode: C++; tab-width: 12; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsThreadUtils.h"
    1.10 +#include "mozilla/SyncRunnable.h"
    1.11 +
    1.12 +#include "gtest/gtest.h"
    1.13 +
    1.14 +using namespace mozilla;
    1.15 +
    1.16 +nsIThread *gThread = nullptr;
    1.17 +
    1.18 +class TestRunnable : public nsRunnable {
    1.19 +public:
    1.20 +  TestRunnable() : ran_(false) {}
    1.21 +
    1.22 +  NS_IMETHOD Run()
    1.23 +  {
    1.24 +    ran_ = true;
    1.25 +
    1.26 +    return NS_OK;
    1.27 +  }
    1.28 +
    1.29 +  bool ran() const { return ran_; }
    1.30 +
    1.31 +private:
    1.32 +  bool ran_;
    1.33 +};
    1.34 +
    1.35 +class TestSyncRunnable : public ::testing::Test {
    1.36 +public:
    1.37 +  static void SetUpTestCase()
    1.38 +  {
    1.39 +    nsresult rv = NS_NewNamedThread("thread", &gThread);
    1.40 +    ASSERT_TRUE(NS_SUCCEEDED(rv));
    1.41 +  }
    1.42 +
    1.43 +  static void TearDownTestCase()
    1.44 +  {
    1.45 +    if (gThread)
    1.46 +      gThread->Shutdown();
    1.47 +  }
    1.48 +};
    1.49 +
    1.50 +TEST_F(TestSyncRunnable, TestDispatch)
    1.51 +{
    1.52 +  nsRefPtr<TestRunnable> r(new TestRunnable());
    1.53 +  nsRefPtr<SyncRunnable> s(new SyncRunnable(r));
    1.54 +  s->DispatchToThread(gThread);
    1.55 +
    1.56 +  ASSERT_TRUE(r->ran());
    1.57 +}
    1.58 +
    1.59 +TEST_F(TestSyncRunnable, TestDispatchStatic)
    1.60 +{
    1.61 +  nsRefPtr<TestRunnable> r(new TestRunnable());
    1.62 +  SyncRunnable::DispatchToThread(gThread, r);
    1.63 +  ASSERT_TRUE(r->ran());
    1.64 +}
    1.65 +
    1.66 +
    1.67 +#include "mtransport_test_utils.h"
    1.68 +MtransportTestUtils *test_utils;
    1.69 +
    1.70 +int main(int argc, char **argv)
    1.71 +{
    1.72 +  test_utils = new MtransportTestUtils();
    1.73 +  // Start the tests
    1.74 +  ::testing::InitGoogleTest(&argc, argv);
    1.75 +
    1.76 +  int rv = RUN_ALL_TESTS();
    1.77 +
    1.78 +  delete test_utils;
    1.79 +  return rv;
    1.80 +}

mercurial