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 "nsThreadUtils.h" 7 #include "mozilla/SyncRunnable.h" 8 9 #include "gtest/gtest.h" 10 11 using namespace mozilla; 12 13 nsIThread *gThread = nullptr; 14 15 class TestRunnable : public Runnable { 16 public: TestRunnable()17 TestRunnable() : Runnable("TestRunnable"), ran_(false) {} 18 Run()19 NS_IMETHOD Run() override 20 { 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 { 36 nsresult rv = NS_NewNamedThread("thread", &gThread); 37 ASSERT_TRUE(NS_SUCCEEDED(rv)); 38 } 39 TearDownTestCase()40 static void TearDownTestCase() 41 { 42 if (gThread) 43 gThread->Shutdown(); 44 } 45 }; 46 TEST_F(TestSyncRunnable,TestDispatch)47TEST_F(TestSyncRunnable, TestDispatch) 48 { 49 RefPtr<TestRunnable> r(new TestRunnable()); 50 RefPtr<SyncRunnable> s(new SyncRunnable(r)); 51 s->DispatchToThread(gThread); 52 53 ASSERT_TRUE(r->ran()); 54 } 55 TEST_F(TestSyncRunnable,TestDispatchStatic)56TEST_F(TestSyncRunnable, TestDispatchStatic) 57 { 58 RefPtr<TestRunnable> r(new TestRunnable()); 59 SyncRunnable::DispatchToThread(gThread, r); 60 ASSERT_TRUE(r->ran()); 61 } 62