|
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 nsRunnable { |
|
16 public: |
|
17 TestRunnable() : ran_(false) {} |
|
18 |
|
19 NS_IMETHOD Run() |
|
20 { |
|
21 ran_ = true; |
|
22 |
|
23 return NS_OK; |
|
24 } |
|
25 |
|
26 bool ran() const { return ran_; } |
|
27 |
|
28 private: |
|
29 bool ran_; |
|
30 }; |
|
31 |
|
32 class TestSyncRunnable : public ::testing::Test { |
|
33 public: |
|
34 static void SetUpTestCase() |
|
35 { |
|
36 nsresult rv = NS_NewNamedThread("thread", &gThread); |
|
37 ASSERT_TRUE(NS_SUCCEEDED(rv)); |
|
38 } |
|
39 |
|
40 static void TearDownTestCase() |
|
41 { |
|
42 if (gThread) |
|
43 gThread->Shutdown(); |
|
44 } |
|
45 }; |
|
46 |
|
47 TEST_F(TestSyncRunnable, TestDispatch) |
|
48 { |
|
49 nsRefPtr<TestRunnable> r(new TestRunnable()); |
|
50 nsRefPtr<SyncRunnable> s(new SyncRunnable(r)); |
|
51 s->DispatchToThread(gThread); |
|
52 |
|
53 ASSERT_TRUE(r->ran()); |
|
54 } |
|
55 |
|
56 TEST_F(TestSyncRunnable, TestDispatchStatic) |
|
57 { |
|
58 nsRefPtr<TestRunnable> r(new TestRunnable()); |
|
59 SyncRunnable::DispatchToThread(gThread, r); |
|
60 ASSERT_TRUE(r->ran()); |
|
61 } |
|
62 |
|
63 |
|
64 #include "mtransport_test_utils.h" |
|
65 MtransportTestUtils *test_utils; |
|
66 |
|
67 int main(int argc, char **argv) |
|
68 { |
|
69 test_utils = new MtransportTestUtils(); |
|
70 // Start the tests |
|
71 ::testing::InitGoogleTest(&argc, argv); |
|
72 |
|
73 int rv = RUN_ALL_TESTS(); |
|
74 |
|
75 delete test_utils; |
|
76 return rv; |
|
77 } |