henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 2 | * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
kjellander | b24317b | 2016-02-10 07:54:43 -0800 | [diff] [blame] | 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. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Henrik Kjellander | 15583c1 | 2016-02-10 10:53:12 +0100 | [diff] [blame] | 11 | #include "webrtc/api/proxy.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 12 | |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 13 | #include <memory> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 14 | #include <string> |
| 15 | |
buildbot@webrtc.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 16 | #include "webrtc/base/gunit.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 17 | #include "webrtc/base/refcount.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 18 | #include "webrtc/base/thread.h" |
kwiberg | ac9f876 | 2016-09-30 22:29:43 -0700 | [diff] [blame] | 19 | #include "webrtc/test/gmock.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 30 | class FakeInterface : public rtc::RefCountInterface { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 43 | // Implementation of the test interface. |
| 44 | class Fake : public FakeInterface { |
| 45 | public: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 46 | static rtc::scoped_refptr<Fake> Create() { |
| 47 | return new rtc::RefCountedObject<Fake>(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 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 |
deadbeef | a601f5c | 2016-06-06 14:27:39 -0700 | [diff] [blame] | 76 | #define FakeProxyWithInternal FakeSignalingProxyWithInternal |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 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 { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 88 | public: |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 89 | // Checks that the functions are called on the right thread. |
| 90 | void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 91 | |
| 92 | protected: |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 93 | void SetUp() override { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 94 | signaling_thread_.reset(new rtc::Thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 95 | ASSERT_TRUE(signaling_thread_->Start()); |
| 96 | fake_ = Fake::Create(); |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 97 | fake_signaling_proxy_ = |
| 98 | FakeSignalingProxy::Create(signaling_thread_.get(), fake_.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | protected: |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 102 | std::unique_ptr<rtc::Thread> signaling_thread_; |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 103 | rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 104 | rtc::scoped_refptr<Fake> fake_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 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: |
kwiberg | d1fe281 | 2016-04-27 06:47:29 -0700 | [diff] [blame] | 179 | std::unique_ptr<rtc::Thread> worker_thread_; |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 180 | rtc::scoped_refptr<FakeInterface> fake_proxy_; |
| 181 | }; |
| 182 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 183 | TEST_F(ProxyTest, VoidMethod0) { |
| 184 | EXPECT_CALL(*fake_, VoidMethod0()) |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 185 | .Times(Exactly(1)) |
| 186 | .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread)); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 187 | fake_proxy_->VoidMethod0(); |
| 188 | } |
| 189 | |
| 190 | TEST_F(ProxyTest, Method0) { |
| 191 | EXPECT_CALL(*fake_, Method0()) |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 192 | .Times(Exactly(1)) |
| 193 | .WillOnce( |
| 194 | DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread), |
| 195 | Return("Method0"))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 196 | EXPECT_EQ("Method0", |
| 197 | fake_proxy_->Method0()); |
| 198 | } |
| 199 | |
| 200 | TEST_F(ProxyTest, ConstMethod0) { |
| 201 | EXPECT_CALL(*fake_, ConstMethod0()) |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 202 | .Times(Exactly(1)) |
| 203 | .WillOnce( |
| 204 | DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread), |
| 205 | Return("ConstMethod0"))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 206 | EXPECT_EQ("ConstMethod0", |
| 207 | fake_proxy_->ConstMethod0()); |
| 208 | } |
| 209 | |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 210 | TEST_F(ProxyTest, WorkerMethod1) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 211 | const std::string arg1 = "arg1"; |
| 212 | EXPECT_CALL(*fake_, Method1(arg1)) |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 213 | .Times(Exactly(1)) |
| 214 | .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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)) |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 222 | .Times(Exactly(1)) |
| 223 | .WillOnce( |
| 224 | DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread), |
| 225 | Return("ConstMethod1"))); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 226 | EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1)); |
| 227 | } |
| 228 | |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 229 | TEST_F(ProxyTest, WorkerMethod2) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 230 | const std::string arg1 = "arg1"; |
| 231 | const std::string arg2 = "arg2"; |
| 232 | EXPECT_CALL(*fake_, Method2(arg1, arg2)) |
nisse | 72c8d2b | 2016-04-15 03:49:07 -0700 | [diff] [blame] | 233 | .Times(Exactly(1)) |
| 234 | .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread), |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 235 | Return("Method2"))); |
| 236 | EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2)); |
| 237 | } |
| 238 | |
| 239 | } // namespace webrtc |