blob: 2578e9b4b10d1d83c8f668f0634716ad43798514 [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
13#include <string>
14
nisse25ed5802016-04-11 06:01:27 -070015#include "testing/gmock/include/gmock/gmock.h"
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"
18#include "webrtc/base/scoped_ptr.h"
19#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
43// Proxy for the test interface.
44BEGIN_PROXY_MAP(Fake)
45 PROXY_METHOD0(void, VoidMethod0)
46 PROXY_METHOD0(std::string, Method0)
47 PROXY_CONSTMETHOD0(std::string, ConstMethod0)
48 PROXY_METHOD1(std::string, Method1, std::string)
49 PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
50 PROXY_METHOD2(std::string, Method2, std::string, std::string)
51END_PROXY()
52
53// Implementation of the test interface.
54class Fake : public FakeInterface {
55 public:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000056 static rtc::scoped_refptr<Fake> Create() {
57 return new rtc::RefCountedObject<Fake>();
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058 }
59
60 MOCK_METHOD0(VoidMethod0, void());
61 MOCK_METHOD0(Method0, std::string());
62 MOCK_CONST_METHOD0(ConstMethod0, std::string());
63
64 MOCK_METHOD1(Method1, std::string(std::string));
65 MOCK_CONST_METHOD1(ConstMethod1, std::string(std::string));
66
67 MOCK_METHOD2(Method2, std::string(std::string, std::string));
68
69 protected:
70 Fake() {}
71 ~Fake() {}
72};
73
74class ProxyTest: public testing::Test {
75 public:
76 // Checks that the functions is called on the |signaling_thread_|.
77 void CheckThread() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000078 EXPECT_EQ(rtc::Thread::Current(), signaling_thread_.get());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 }
80
81 protected:
82 virtual void SetUp() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000083 signaling_thread_.reset(new rtc::Thread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084 ASSERT_TRUE(signaling_thread_->Start());
85 fake_ = Fake::Create();
86 fake_proxy_ = FakeProxy::Create(signaling_thread_.get(), fake_.get());
87 }
88
89 protected:
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000090 rtc::scoped_ptr<rtc::Thread> signaling_thread_;
91 rtc::scoped_refptr<FakeInterface> fake_proxy_;
92 rtc::scoped_refptr<Fake> fake_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093};
94
95TEST_F(ProxyTest, VoidMethod0) {
96 EXPECT_CALL(*fake_, VoidMethod0())
97 .Times(Exactly(1))
98 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckThread));
99 fake_proxy_->VoidMethod0();
100}
101
102TEST_F(ProxyTest, Method0) {
103 EXPECT_CALL(*fake_, Method0())
104 .Times(Exactly(1))
105 .WillOnce(
106 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
107 Return("Method0")));
108 EXPECT_EQ("Method0",
109 fake_proxy_->Method0());
110}
111
112TEST_F(ProxyTest, ConstMethod0) {
113 EXPECT_CALL(*fake_, ConstMethod0())
114 .Times(Exactly(1))
115 .WillOnce(
116 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
117 Return("ConstMethod0")));
118 EXPECT_EQ("ConstMethod0",
119 fake_proxy_->ConstMethod0());
120}
121
122TEST_F(ProxyTest, Method1) {
123 const std::string arg1 = "arg1";
124 EXPECT_CALL(*fake_, Method1(arg1))
125 .Times(Exactly(1))
126 .WillOnce(
127 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
128 Return("Method1")));
129 EXPECT_EQ("Method1", fake_proxy_->Method1(arg1));
130}
131
132TEST_F(ProxyTest, ConstMethod1) {
133 const std::string arg1 = "arg1";
134 EXPECT_CALL(*fake_, ConstMethod1(arg1))
135 .Times(Exactly(1))
136 .WillOnce(
137 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
138 Return("ConstMethod1")));
139 EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
140}
141
142TEST_F(ProxyTest, Method2) {
143 const std::string arg1 = "arg1";
144 const std::string arg2 = "arg2";
145 EXPECT_CALL(*fake_, Method2(arg1, arg2))
146 .Times(Exactly(1))
147 .WillOnce(
148 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
149 Return("Method2")));
150 EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
151}
152
153} // namespace webrtc