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