media/mtransport/test/runnable_utils_unittest.cpp

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:4ee3dd255d15
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 // Original author: ekr@rtfm.com
8 #include <iostream>
9
10 #include "prio.h"
11
12 #include "nsCOMPtr.h"
13 #include "nsNetCID.h"
14 #include "nsXPCOM.h"
15 #include "nsXPCOMGlue.h"
16
17 #include "mozilla/RefPtr.h"
18 #include "nsIComponentManager.h"
19 #include "nsIComponentRegistrar.h"
20 #include "nsIIOService.h"
21 #include "nsIServiceManager.h"
22 #include "nsISocketTransportService.h"
23
24 #include "nsASocketHandler.h"
25 #include "nsServiceManagerUtils.h"
26 #include "nsThreadUtils.h"
27
28 #include "runnable_utils.h"
29 #include "mtransport_test_utils.h"
30
31 #define GTEST_HAS_RTTI 0
32 #include "gtest/gtest.h"
33 #include "gtest_utils.h"
34
35 using namespace mozilla;
36 MtransportTestUtils *test_utils;
37
38 namespace {
39
40 class Destructor {
41 public:
42 Destructor(bool* destroyed) : destroyed_(destroyed) {}
43 ~Destructor() {
44 std::cerr << "Destructor called" << std::endl;
45 *destroyed_ = true;
46 }
47
48 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(Destructor)
49
50 private:
51 bool *destroyed_;
52 };
53
54 class TargetClass {
55 public:
56 TargetClass(int *ran) : ran_(ran) {}
57
58 void m1(int x) {
59 std::cerr << __FUNCTION__ << " " << x << std::endl;
60 *ran_ = 1;
61 }
62
63 void m2(int x, int y) {
64 std::cerr << __FUNCTION__ << " " << x << " " << y << std::endl;
65 *ran_ = 2;
66 }
67
68 void m1set(bool *z) {
69 std::cerr << __FUNCTION__ << std::endl;
70 *z = true;
71 }
72 int return_int(int x) {
73 std::cerr << __FUNCTION__ << std::endl;
74 return x;
75 }
76 void destructor_target(Destructor*) {
77 }
78
79 void destructor_target_ref(RefPtr<Destructor> destructor) {
80 }
81
82 int *ran_;
83 };
84
85
86 class RunnableArgsTest : public ::testing::Test {
87 public:
88 RunnableArgsTest() : ran_(0), cl_(&ran_){}
89
90 void Test1Arg() {
91 nsRunnable * r = WrapRunnable(&cl_, &TargetClass::m1, 1);
92 r->Run();
93 ASSERT_EQ(1, ran_);
94 }
95
96 void Test2Args() {
97 nsRunnable* r = WrapRunnable(&cl_, &TargetClass::m2, 1, 2);
98 r->Run();
99 ASSERT_EQ(2, ran_);
100 }
101
102 private:
103 int ran_;
104 TargetClass cl_;
105 };
106
107 class DispatchTest : public ::testing::Test {
108 public:
109 DispatchTest() : ran_(0), cl_(&ran_) {}
110
111 void SetUp() {
112 nsresult rv;
113 target_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
114 ASSERT_TRUE(NS_SUCCEEDED(rv));
115 }
116
117 void Test1Arg() {
118 nsRunnable* r = WrapRunnable(&cl_, &TargetClass::m1, 1);
119 target_->Dispatch(r, NS_DISPATCH_SYNC);
120 ASSERT_EQ(1, ran_);
121 }
122
123 void Test2Args() {
124 nsRunnable* r = WrapRunnable(&cl_, &TargetClass::m2, 1, 2);
125 target_->Dispatch(r, NS_DISPATCH_SYNC);
126 ASSERT_EQ(2, ran_);
127 }
128
129 void Test1Set() {
130 bool x = false;
131 target_->Dispatch(WrapRunnable(&cl_, &TargetClass::m1set, &x),
132 NS_DISPATCH_SYNC);
133 ASSERT_TRUE(x);
134 }
135
136 void TestRet() {
137 int z;
138 int x = 10;
139
140 target_->Dispatch(WrapRunnableRet(&cl_, &TargetClass::return_int, x, &z),
141 NS_DISPATCH_SYNC);
142 ASSERT_EQ(10, z);
143 }
144
145 protected:
146 int ran_;
147 TargetClass cl_;
148 nsCOMPtr<nsIEventTarget> target_;
149 };
150
151
152 TEST_F(RunnableArgsTest, OneArgument) {
153 Test1Arg();
154 }
155
156 TEST_F(RunnableArgsTest, TwoArguments) {
157 Test2Args();
158 }
159
160 TEST_F(DispatchTest, OneArgument) {
161 Test1Arg();
162 }
163
164 TEST_F(DispatchTest, TwoArguments) {
165 Test2Args();
166 }
167
168 TEST_F(DispatchTest, Test1Set) {
169 Test1Set();
170 }
171
172 TEST_F(DispatchTest, TestRet) {
173 TestRet();
174 }
175
176 void SetNonMethod(TargetClass *cl, int x) {
177 cl->m1(x);
178 }
179
180 int SetNonMethodRet(TargetClass *cl, int x) {
181 cl->m1(x);
182
183 return x;
184 }
185
186 TEST_F(DispatchTest, TestNonMethod) {
187 test_utils->sts_target()->Dispatch(
188 WrapRunnableNM(SetNonMethod, &cl_, 10), NS_DISPATCH_SYNC);
189
190 ASSERT_EQ(1, ran_);
191 }
192
193 TEST_F(DispatchTest, TestNonMethodRet) {
194 int z;
195
196 test_utils->sts_target()->Dispatch(
197 WrapRunnableNMRet(SetNonMethodRet, &cl_, 10, &z), NS_DISPATCH_SYNC);
198
199 ASSERT_EQ(1, ran_);
200 ASSERT_EQ(10, z);
201 }
202
203 TEST_F(DispatchTest, TestDestructor) {
204 bool destroyed = false;
205 RefPtr<Destructor> destructor = new Destructor(&destroyed);
206 target_->Dispatch(WrapRunnable(&cl_, &TargetClass::destructor_target,
207 destructor),
208 NS_DISPATCH_SYNC);
209 ASSERT_FALSE(destroyed);
210 destructor = nullptr;
211 ASSERT_TRUE(destroyed);
212 }
213
214 TEST_F(DispatchTest, TestDestructorRef) {
215 bool destroyed = false;
216 RefPtr<Destructor> destructor = new Destructor(&destroyed);
217 target_->Dispatch(WrapRunnable(&cl_, &TargetClass::destructor_target_ref,
218 destructor),
219 NS_DISPATCH_SYNC);
220 ASSERT_FALSE(destroyed);
221 destructor = nullptr;
222 ASSERT_TRUE(destroyed);
223 }
224
225
226 } // end of namespace
227
228
229 int main(int argc, char **argv) {
230 test_utils = new MtransportTestUtils();
231
232 // Start the tests
233 ::testing::InitGoogleTest(&argc, argv);
234
235 int rv = RUN_ALL_TESTS();
236 delete test_utils;
237 return rv;
238 }
239

mercurial