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 | |
| 13 | #include <string> |
| 14 | |
nisse | 25ed580 | 2016-04-11 06:01:27 -0700 | [diff] [blame^] | 15 | #include "testing/gmock/include/gmock/gmock.h" |
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" |
| 18 | #include "webrtc/base/scoped_ptr.h" |
| 19 | #include "webrtc/base/thread.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 | |
| 43 | // Proxy for the test interface. |
| 44 | BEGIN_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) |
| 51 | END_PROXY() |
| 52 | |
| 53 | // Implementation of the test interface. |
| 54 | class Fake : public FakeInterface { |
| 55 | public: |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 56 | static rtc::scoped_refptr<Fake> Create() { |
| 57 | return new rtc::RefCountedObject<Fake>(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | } |
| 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 | |
| 74 | class ProxyTest: public testing::Test { |
| 75 | public: |
| 76 | // Checks that the functions is called on the |signaling_thread_|. |
| 77 | void CheckThread() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 78 | EXPECT_EQ(rtc::Thread::Current(), signaling_thread_.get()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | protected: |
| 82 | virtual void SetUp() { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 83 | signaling_thread_.reset(new rtc::Thread()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | 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.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 90 | rtc::scoped_ptr<rtc::Thread> signaling_thread_; |
| 91 | rtc::scoped_refptr<FakeInterface> fake_proxy_; |
| 92 | rtc::scoped_refptr<Fake> fake_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | TEST_F(ProxyTest, VoidMethod0) { |
| 96 | EXPECT_CALL(*fake_, VoidMethod0()) |
| 97 | .Times(Exactly(1)) |
| 98 | .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckThread)); |
| 99 | fake_proxy_->VoidMethod0(); |
| 100 | } |
| 101 | |
| 102 | TEST_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 | |
| 112 | TEST_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 | |
| 122 | TEST_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 | |
| 132 | TEST_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 | |
| 142 | TEST_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 |