blob: 193fb298a281ddd681ce0decb6cb057a3b1bafea [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
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000016#include "webrtc/base/gunit.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000017#include "webrtc/base/refcount.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000018#include "webrtc/base/thread.h"
kwibergac9f8762016-09-30 22:29:43 -070019#include "webrtc/test/gmock.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
deadbeefa601f5c2016-06-06 14:27:39 -070076#define FakeProxyWithInternal FakeSignalingProxyWithInternal
nisse72c8d2b2016-04-15 03:49:07 -070077BEGIN_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)
84END_SIGNALING_PROXY()
85#undef FakeProxy
86
87class SignalingProxyTest : public testing::Test {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 public:
nisse72c8d2b2016-04-15 03:49:07 -070089 // Checks that the functions are called on the right thread.
90 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091
92 protected:
nisse72c8d2b2016-04-15 03:49:07 -070093 void SetUp() override {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000094 signaling_thread_.reset(new rtc::Thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000095 ASSERT_TRUE(signaling_thread_->Start());
96 fake_ = Fake::Create();
nisse72c8d2b2016-04-15 03:49:07 -070097 fake_signaling_proxy_ =
98 FakeSignalingProxy::Create(signaling_thread_.get(), fake_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000099 }
100
101 protected:
kwibergd1fe2812016-04-27 06:47:29 -0700102 std::unique_ptr<rtc::Thread> signaling_thread_;
nisse72c8d2b2016-04-15 03:49:07 -0700103 rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000104 rtc::scoped_refptr<Fake> fake_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105};
106
nisse72c8d2b2016-04-15 03:49:07 -0700107TEST_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
115TEST_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
124TEST_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
133TEST_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
143TEST_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
153TEST_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
164class 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:
kwibergd1fe2812016-04-27 06:47:29 -0700179 std::unique_ptr<rtc::Thread> worker_thread_;
nisse72c8d2b2016-04-15 03:49:07 -0700180 rtc::scoped_refptr<FakeInterface> fake_proxy_;
181};
182
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183TEST_F(ProxyTest, VoidMethod0) {
184 EXPECT_CALL(*fake_, VoidMethod0())
nisse72c8d2b2016-04-15 03:49:07 -0700185 .Times(Exactly(1))
186 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 fake_proxy_->VoidMethod0();
188}
189
190TEST_F(ProxyTest, Method0) {
191 EXPECT_CALL(*fake_, Method0())
nisse72c8d2b2016-04-15 03:49:07 -0700192 .Times(Exactly(1))
193 .WillOnce(
194 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
195 Return("Method0")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 EXPECT_EQ("Method0",
197 fake_proxy_->Method0());
198}
199
200TEST_F(ProxyTest, ConstMethod0) {
201 EXPECT_CALL(*fake_, ConstMethod0())
nisse72c8d2b2016-04-15 03:49:07 -0700202 .Times(Exactly(1))
203 .WillOnce(
204 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
205 Return("ConstMethod0")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 EXPECT_EQ("ConstMethod0",
207 fake_proxy_->ConstMethod0());
208}
209
nisse72c8d2b2016-04-15 03:49:07 -0700210TEST_F(ProxyTest, WorkerMethod1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000211 const std::string arg1 = "arg1";
212 EXPECT_CALL(*fake_, Method1(arg1))
nisse72c8d2b2016-04-15 03:49:07 -0700213 .Times(Exactly(1))
214 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 Return("Method1")));
216 EXPECT_EQ("Method1", fake_proxy_->Method1(arg1));
217}
218
219TEST_F(ProxyTest, ConstMethod1) {
220 const std::string arg1 = "arg1";
221 EXPECT_CALL(*fake_, ConstMethod1(arg1))
nisse72c8d2b2016-04-15 03:49:07 -0700222 .Times(Exactly(1))
223 .WillOnce(
224 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread),
225 Return("ConstMethod1")));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
227}
228
nisse72c8d2b2016-04-15 03:49:07 -0700229TEST_F(ProxyTest, WorkerMethod2) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 const std::string arg1 = "arg1";
231 const std::string arg2 = "arg2";
232 EXPECT_CALL(*fake_, Method2(arg1, arg2))
nisse72c8d2b2016-04-15 03:49:07 -0700233 .Times(Exactly(1))
234 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000235 Return("Method2")));
236 EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
237}
238
239} // namespace webrtc