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 6 #include "nsIThread.h" 7 #include "nsThreadUtils.h" 8 #include "mozilla/SyncRunnable.h" 9 10 #include "gtest/gtest.h" 11 12 using namespace mozilla; 13 14 nsIThread* gThread = nullptr; 15 16 class TestRunnable : public Runnable { 17 public: TestRunnable()18 TestRunnable() : Runnable("TestRunnable"), ran_(false) {} 19 Run()20 NS_IMETHOD Run() override { 21 ran_ = true; 22 23 return NS_OK; 24 } 25 ran() const26 bool ran() const { return ran_; } 27 28 private: 29 bool ran_; 30 }; 31 32 class TestSyncRunnable : public ::testing::Test { 33 public: SetUpTestCase()34 static void SetUpTestCase() { 35 nsresult rv = NS_NewNamedThread("thread", &gThread); 36 ASSERT_TRUE(NS_SUCCEEDED(rv)); 37 } 38 TearDownTestCase()39 static void TearDownTestCase() { 40 if (gThread) gThread->Shutdown(); 41 } 42 }; 43 TEST_F(TestSyncRunnable,TestDispatch)44TEST_F(TestSyncRunnable, TestDispatch) { 45 RefPtr<TestRunnable> r(new TestRunnable()); 46 RefPtr<SyncRunnable> s(new SyncRunnable(r)); 47 s->DispatchToThread(gThread); 48 49 ASSERT_TRUE(r->ran()); 50 } 51 TEST_F(TestSyncRunnable,TestDispatchStatic)52TEST_F(TestSyncRunnable, TestDispatchStatic) { 53 RefPtr<TestRunnable> r(new TestRunnable()); 54 SyncRunnable::DispatchToThread(gThread, r); 55 ASSERT_TRUE(r->ran()); 56 } 57