OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include "webrtc/api/proxy.h" | |
12 | |
13 #include <memory> | |
14 #include <string> | |
15 | |
16 #include "webrtc/base/gunit.h" | |
17 #include "webrtc/base/refcount.h" | |
18 #include "webrtc/base/thread.h" | |
19 #include "webrtc/test/gmock.h" | |
20 | |
21 using ::testing::_; | |
22 using ::testing::DoAll; | |
23 using ::testing::Exactly; | |
24 using ::testing::InvokeWithoutArgs; | |
25 using ::testing::Return; | |
26 | |
27 namespace webrtc { | |
28 | |
29 // Interface used for testing here. | |
30 class FakeInterface : public rtc::RefCountInterface { | |
31 public: | |
32 virtual void VoidMethod0() = 0; | |
33 virtual std::string Method0() = 0; | |
34 virtual std::string ConstMethod0() const = 0; | |
35 virtual std::string Method1(std::string s) = 0; | |
36 virtual std::string ConstMethod1(std::string s) const = 0; | |
37 virtual std::string Method2(std::string s1, std::string s2) = 0; | |
38 | |
39 protected: | |
40 ~FakeInterface() {} | |
41 }; | |
42 | |
43 // Implementation of the test interface. | |
44 class Fake : public FakeInterface { | |
45 public: | |
46 static rtc::scoped_refptr<Fake> Create() { | |
47 return new rtc::RefCountedObject<Fake>(); | |
48 } | |
49 | |
50 MOCK_METHOD0(VoidMethod0, void()); | |
51 MOCK_METHOD0(Method0, std::string()); | |
52 MOCK_CONST_METHOD0(ConstMethod0, std::string()); | |
53 | |
54 MOCK_METHOD1(Method1, std::string(std::string)); | |
55 MOCK_CONST_METHOD1(ConstMethod1, std::string(std::string)); | |
56 | |
57 MOCK_METHOD2(Method2, std::string(std::string, std::string)); | |
58 | |
59 protected: | |
60 Fake() {} | |
61 ~Fake() {} | |
62 }; | |
63 | |
64 // Proxies for the test interface. | |
65 BEGIN_PROXY_MAP(Fake) | |
66 PROXY_METHOD0(void, VoidMethod0) | |
67 PROXY_METHOD0(std::string, Method0) | |
68 PROXY_CONSTMETHOD0(std::string, ConstMethod0) | |
69 PROXY_WORKER_METHOD1(std::string, Method1, std::string) | |
70 PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string) | |
71 PROXY_WORKER_METHOD2(std::string, Method2, std::string, std::string) | |
72 END_PROXY() | |
73 | |
74 // Preprocessor hack to get a proxy class a name different than FakeProxy. | |
75 #define FakeProxy FakeSignalingProxy | |
76 #define FakeProxyWithInternal FakeSignalingProxyWithInternal | |
77 BEGIN_SIGNALING_PROXY_MAP(Fake) | |
78 PROXY_METHOD0(void, VoidMethod0) | |
79 PROXY_METHOD0(std::string, Method0) | |
80 PROXY_CONSTMETHOD0(std::string, ConstMethod0) | |
81 PROXY_METHOD1(std::string, Method1, std::string) | |
82 PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string) | |
83 PROXY_METHOD2(std::string, Method2, std::string, std::string) | |
84 END_SIGNALING_PROXY() | |
85 #undef FakeProxy | |
86 | |
87 class SignalingProxyTest : public testing::Test { | |
88 public: | |
89 // Checks that the functions are called on the right thread. | |
90 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); } | |
91 | |
92 protected: | |
93 void SetUp() override { | |
94 signaling_thread_.reset(new rtc::Thread()); | |
95 ASSERT_TRUE(signaling_thread_->Start()); | |
96 fake_ = Fake::Create(); | |
97 fake_signaling_proxy_ = | |
98 FakeSignalingProxy::Create(signaling_thread_.get(), fake_.get()); | |
99 } | |
100 | |
101 protected: | |
102 std::unique_ptr<rtc::Thread> signaling_thread_; | |
103 rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_; | |
104 rtc::scoped_refptr<Fake> fake_; | |
105 }; | |
106 | |
107 TEST_F(SignalingProxyTest, VoidMethod0) { | |
108 EXPECT_CALL(*fake_, VoidMethod0()) | |
109 .Times(Exactly(1)) | |
110 .WillOnce( | |
111 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread)); | |
112 fake_signaling_proxy_->VoidMethod0(); | |
113 } | |
114 | |
115 TEST_F(SignalingProxyTest, Method0) { | |
116 EXPECT_CALL(*fake_, Method0()) | |
117 .Times(Exactly(1)) | |
118 .WillOnce(DoAll( | |
119 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread), | |
120 Return("Method0"))); | |
121 EXPECT_EQ("Method0", fake_signaling_proxy_->Method0()); | |
122 } | |
123 | |
124 TEST_F(SignalingProxyTest, ConstMethod0) { | |
125 EXPECT_CALL(*fake_, ConstMethod0()) | |
126 .Times(Exactly(1)) | |
127 .WillOnce(DoAll( | |
128 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread), | |
129 Return("ConstMethod0"))); | |
130 EXPECT_EQ("ConstMethod0", fake_signaling_proxy_->ConstMethod0()); | |
131 } | |
132 | |
133 TEST_F(SignalingProxyTest, Method1) { | |
134 const std::string arg1 = "arg1"; | |
135 EXPECT_CALL(*fake_, Method1(arg1)) | |
136 .Times(Exactly(1)) | |
137 .WillOnce(DoAll( | |
138 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread), | |
139 Return("Method1"))); | |
140 EXPECT_EQ("Method1", fake_signaling_proxy_->Method1(arg1)); | |
141 } | |
142 | |
143 TEST_F(SignalingProxyTest, ConstMethod1) { | |
144 const std::string arg1 = "arg1"; | |
145 EXPECT_CALL(*fake_, ConstMethod1(arg1)) | |
146 .Times(Exactly(1)) | |
147 .WillOnce(DoAll( | |
148 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread), | |
149 Return("ConstMethod1"))); | |
150 EXPECT_EQ("ConstMethod1", fake_signaling_proxy_->ConstMethod1(arg1)); | |
151 } | |
152 | |
153 TEST_F(SignalingProxyTest, Method2) { | |
154 const std::string arg1 = "arg1"; | |
155 const std::string arg2 = "arg2"; | |
156 EXPECT_CALL(*fake_, Method2(arg1, arg2)) | |
157 .Times(Exactly(1)) | |
158 .WillOnce(DoAll( | |
159 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread), | |
160 Return("Method2"))); | |
161 EXPECT_EQ("Method2", fake_signaling_proxy_->Method2(arg1, arg2)); | |
162 } | |
163 | |
164 class ProxyTest : public SignalingProxyTest { | |
165 public: | |
166 // Checks that the functions are called on the right thread. | |
167 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); } | |
168 | |
169 protected: | |
170 void SetUp() override { | |
171 SignalingProxyTest::SetUp(); | |
172 worker_thread_.reset(new rtc::Thread()); | |
173 ASSERT_TRUE(worker_thread_->Start()); | |
174 fake_proxy_ = FakeProxy::Create(signaling_thread_.get(), | |
175 worker_thread_.get(), fake_.get()); | |
176 } | |
177 | |
178 protected: | |
179 std::unique_ptr<rtc::Thread> worker_thread_; | |
180 rtc::scoped_refptr<FakeInterface> fake_proxy_; | |
181 }; | |
182 | |
183 TEST_F(ProxyTest, VoidMethod0) { | |
184 EXPECT_CALL(*fake_, VoidMethod0()) | |
185 .Times(Exactly(1)) | |
186 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread)); | |
187 fake_proxy_->VoidMethod0(); | |
188 } | |
189 | |
190 TEST_F(ProxyTest, Method0) { | |
191 EXPECT_CALL(*fake_, Method0()) | |
192 .Times(Exactly(1)) | |
193 .WillOnce( | |
194 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread), | |
195 Return("Method0"))); | |
196 EXPECT_EQ("Method0", | |
197 fake_proxy_->Method0()); | |
198 } | |
199 | |
200 TEST_F(ProxyTest, ConstMethod0) { | |
201 EXPECT_CALL(*fake_, ConstMethod0()) | |
202 .Times(Exactly(1)) | |
203 .WillOnce( | |
204 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread), | |
205 Return("ConstMethod0"))); | |
206 EXPECT_EQ("ConstMethod0", | |
207 fake_proxy_->ConstMethod0()); | |
208 } | |
209 | |
210 TEST_F(ProxyTest, WorkerMethod1) { | |
211 const std::string arg1 = "arg1"; | |
212 EXPECT_CALL(*fake_, Method1(arg1)) | |
213 .Times(Exactly(1)) | |
214 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread), | |
215 Return("Method1"))); | |
216 EXPECT_EQ("Method1", fake_proxy_->Method1(arg1)); | |
217 } | |
218 | |
219 TEST_F(ProxyTest, ConstMethod1) { | |
220 const std::string arg1 = "arg1"; | |
221 EXPECT_CALL(*fake_, ConstMethod1(arg1)) | |
222 .Times(Exactly(1)) | |
223 .WillOnce( | |
224 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread), | |
225 Return("ConstMethod1"))); | |
226 EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1)); | |
227 } | |
228 | |
229 TEST_F(ProxyTest, WorkerMethod2) { | |
230 const std::string arg1 = "arg1"; | |
231 const std::string arg2 = "arg2"; | |
232 EXPECT_CALL(*fake_, Method2(arg1, arg2)) | |
233 .Times(Exactly(1)) | |
234 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread), | |
235 Return("Method2"))); | |
236 EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2)); | |
237 } | |
238 | |
239 } // namespace webrtc | |
OLD | NEW |