blob: 931ba28eba83ee41f258022bd9c714240cda79f0 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2013 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
Henrik Kjellander15583c12016-02-10 10:53:12 +010011#include "webrtc/api/proxy.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
kwibergd1fe2812016-04-27 06:47:29 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <string>
15
nisse25ed5802016-04-11 06:01:27 -070016#include "testing/gmock/include/gmock/gmock.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000017#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/refcount.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000019#include "webrtc/base/thread.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020
21using ::testing::_;
22using ::testing::DoAll;
23using ::testing::Exactly;
24using ::testing::InvokeWithoutArgs;
25using ::testing::Return;
26
27namespace webrtc {
28
29// Interface used for testing here.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030class FakeInterface : public rtc::RefCountInterface {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031 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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043// Implementation of the test interface.
44class Fake : public FakeInterface {
45 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000046 static rtc::scoped_refptr<Fake> Create() {
47 return new rtc::RefCountedObject<Fake>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048 }
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
nisse72c8d2b2016-04-15 03:49:07 -070064// Proxies for the test interface.
65BEGIN_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)
72END_PROXY()
73
74// Preprocessor hack to get a proxy class a name different than FakeProxy.
75#define FakeProxy FakeSignalingProxy
76BEGIN_SIGNALING_PROXY_MAP(Fake)
77 PROXY_METHOD0(void, VoidMethod0)
78 PROXY_METHOD0(std::string, Method0)
79 PROXY_CONSTMETHOD0(std::string, ConstMethod0)
80 PROXY_METHOD1(std::string, Method1, std::string)
81 PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
82 PROXY_METHOD2(std::string, Method2, std::string, std::string)
83END_SIGNALING_PROXY()
84#undef FakeProxy
85
86class SignalingProxyTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087 public:
nisse72c8d2b2016-04-15 03:49:07 -070088 // Checks that the functions are called on the right thread.
89 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090
91 protected:
nisse72c8d2b2016-04-15 03:49:07 -070092 void SetUp() override {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000093 signaling_thread_.reset(new rtc::Thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094 ASSERT_TRUE(signaling_thread_->Start());
95 fake_ = Fake::Create();
nisse72c8d2b2016-04-15 03:49:07 -070096 fake_signaling_proxy_ =
97 FakeSignalingProxy::Create(signaling_thread_.get(), fake_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000098 }
99
100 protected:
kwibergd1fe2812016-04-27 06:47:29 -0700101 std::unique_ptr<rtc::Thread> signaling_thread_;
nisse72c8d2b2016-04-15 03:49:07 -0700102 rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000103 rtc::scoped_refptr<Fake> fake_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104};
105
nisse72c8d2b2016-04-15 03:49:07 -0700106TEST_F(SignalingProxyTest, VoidMethod0) {
107 EXPECT_CALL(*fake_, VoidMethod0())
108 .Times(Exactly(1))
109 .WillOnce(
110 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread));
111 fake_signaling_proxy_->VoidMethod0();
112}
113
114TEST_F(SignalingProxyTest, Method0) {
115 EXPECT_CALL(*fake_, Method0())
116 .Times(Exactly(1))
117 .WillOnce(DoAll(
118 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
119 Return("Method0")));
120 EXPECT_EQ("Method0", fake_signaling_proxy_->Method0());
121}
122
123TEST_F(SignalingProxyTest, ConstMethod0) {
124 EXPECT_CALL(*fake_, ConstMethod0())
125 .Times(Exactly(1))
126 .WillOnce(DoAll(
127 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
128 Return("ConstMethod0")));
129 EXPECT_EQ("ConstMethod0", fake_signaling_proxy_->ConstMethod0());
130}
131
132TEST_F(SignalingProxyTest, Method1) {
133 const std::string arg1 = "arg1";
134 EXPECT_CALL(*fake_, Method1(arg1))
135 .Times(Exactly(1))
136 .WillOnce(DoAll(
137 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
138 Return("Method1")));
139 EXPECT_EQ("Method1", fake_signaling_proxy_->Method1(arg1));
140}
141
142TEST_F(SignalingProxyTest, ConstMethod1) {
143 const std::string arg1 = "arg1";
144 EXPECT_CALL(*fake_, ConstMethod1(arg1))
145 .Times(Exactly(1))
146 .WillOnce(DoAll(
147 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
148 Return("ConstMethod1")));
149 EXPECT_EQ("ConstMethod1", fake_signaling_proxy_->ConstMethod1(arg1));
150}
151
152TEST_F(SignalingProxyTest, Method2) {
153 const std::string arg1 = "arg1";
154 const std::string arg2 = "arg2";
155 EXPECT_CALL(*fake_, Method2(arg1, arg2))
156 .Times(Exactly(1))
157 .WillOnce(DoAll(
158 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread),
159 Return("Method2")));
160 EXPECT_EQ("Method2", fake_signaling_proxy_->Method2(arg1, arg2));
161}
162
163class ProxyTest : public SignalingProxyTest {
164 public:
165 // Checks that the functions are called on the right thread.
166 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); }
167
168 protected:
169 void SetUp() override {
170 SignalingProxyTest::SetUp();
171 worker_thread_.reset(new rtc::Thread());
172 ASSERT_TRUE(worker_thread_->Start());
173 fake_proxy_ = FakeProxy::Create(signaling_thread_.get(),
174 worker_thread_.get(), fake_.get());
175 }
176
177 protected:
kwibergd1fe2812016-04-27 06:47:29 -0700178 std::unique_ptr<rtc::Thread> worker_thread_;
nisse72c8d2b2016-04-15 03:49:07 -0700179 rtc::scoped_refptr<FakeInterface> fake_proxy_;
180};
181
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182TEST_F(ProxyTest, VoidMethod0) {
183 EXPECT_CALL(*fake_, VoidMethod0())
nisse72c8d2b2016-04-15 03:49:07 -0700184 .Times(Exactly(1))
185 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 fake_proxy_->VoidMethod0();
187}
188
189TEST_F(ProxyTest, Method0) {
190 EXPECT_CALL(*fake_, Method0())
nisse72c8d2b2016-04-15 03:49:07 -0700191 .Times(Exactly(1))
192 .WillOnce(
193 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
194 Return("Method0")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 EXPECT_EQ("Method0",
196 fake_proxy_->Method0());
197}
198
199TEST_F(ProxyTest, ConstMethod0) {
200 EXPECT_CALL(*fake_, ConstMethod0())
nisse72c8d2b2016-04-15 03:49:07 -0700201 .Times(Exactly(1))
202 .WillOnce(
203 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
204 Return("ConstMethod0")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000205 EXPECT_EQ("ConstMethod0",
206 fake_proxy_->ConstMethod0());
207}
208
nisse72c8d2b2016-04-15 03:49:07 -0700209TEST_F(ProxyTest, WorkerMethod1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000210 const std::string arg1 = "arg1";
211 EXPECT_CALL(*fake_, Method1(arg1))
nisse72c8d2b2016-04-15 03:49:07 -0700212 .Times(Exactly(1))
213 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 Return("Method1")));
215 EXPECT_EQ("Method1", fake_proxy_->Method1(arg1));
216}
217
218TEST_F(ProxyTest, ConstMethod1) {
219 const std::string arg1 = "arg1";
220 EXPECT_CALL(*fake_, ConstMethod1(arg1))
nisse72c8d2b2016-04-15 03:49:07 -0700221 .Times(Exactly(1))
222 .WillOnce(
223 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
224 Return("ConstMethod1")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
226}
227
nisse72c8d2b2016-04-15 03:49:07 -0700228TEST_F(ProxyTest, WorkerMethod2) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 const std::string arg1 = "arg1";
230 const std::string arg2 = "arg2";
231 EXPECT_CALL(*fake_, Method2(arg1, arg2))
nisse72c8d2b2016-04-15 03:49:07 -0700232 .Times(Exactly(1))
233 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 Return("Method2")));
235 EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
236}
237
238} // namespace webrtc