TestSyncRunnable.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* -*- Mode: C++; tab-width: 12; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsThreadUtils.h"
  6. #include "mozilla/SyncRunnable.h"
  7. #include "gtest/gtest.h"
  8. using namespace mozilla;
  9. nsIThread *gThread = nullptr;
  10. class TestRunnable : public Runnable {
  11. public:
  12. TestRunnable() : ran_(false) {}
  13. NS_IMETHOD Run() override
  14. {
  15. ran_ = true;
  16. return NS_OK;
  17. }
  18. bool ran() const { return ran_; }
  19. private:
  20. bool ran_;
  21. };
  22. class TestSyncRunnable : public ::testing::Test {
  23. public:
  24. static void SetUpTestCase()
  25. {
  26. nsresult rv = NS_NewNamedThread("thread", &gThread);
  27. ASSERT_TRUE(NS_SUCCEEDED(rv));
  28. }
  29. static void TearDownTestCase()
  30. {
  31. if (gThread)
  32. gThread->Shutdown();
  33. }
  34. };
  35. TEST_F(TestSyncRunnable, TestDispatch)
  36. {
  37. RefPtr<TestRunnable> r(new TestRunnable());
  38. RefPtr<SyncRunnable> s(new SyncRunnable(r));
  39. s->DispatchToThread(gThread);
  40. ASSERT_TRUE(r->ran());
  41. }
  42. TEST_F(TestSyncRunnable, TestDispatchStatic)
  43. {
  44. RefPtr<TestRunnable> r(new TestRunnable());
  45. SyncRunnable::DispatchToThread(gThread, r);
  46. ASSERT_TRUE(r->ran());
  47. }