Home | History | Annotate | Download | only in webrtc
      1 /*
      2  * libjingle
      3  * Copyright 2013 Google Inc.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions are met:
      7  *
      8  *  1. Redistributions of source code must retain the above copyright notice,
      9  *     this list of conditions and the following disclaimer.
     10  *  2. Redistributions in binary form must reproduce the above copyright notice,
     11  *     this list of conditions and the following disclaimer in the documentation
     12  *     and/or other materials provided with the distribution.
     13  *  3. The name of the author may not be used to endorse or promote products
     14  *     derived from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
     19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  */
     27 
     28 #include "talk/app/webrtc/proxy.h"
     29 
     30 #include <string>
     31 
     32 #include "testing/base/public/gmock.h"
     33 #include "webrtc/base/gunit.h"
     34 #include "webrtc/base/refcount.h"
     35 #include "webrtc/base/scoped_ptr.h"
     36 #include "webrtc/base/thread.h"
     37 
     38 using ::testing::_;
     39 using ::testing::DoAll;
     40 using ::testing::Exactly;
     41 using ::testing::InvokeWithoutArgs;
     42 using ::testing::Return;
     43 
     44 namespace webrtc {
     45 
     46 // Interface used for testing here.
     47 class FakeInterface : public rtc::RefCountInterface {
     48  public:
     49   virtual void VoidMethod0() = 0;
     50   virtual std::string Method0() = 0;
     51   virtual std::string ConstMethod0() const = 0;
     52   virtual std::string Method1(std::string s) = 0;
     53   virtual std::string ConstMethod1(std::string s) const = 0;
     54   virtual std::string Method2(std::string s1, std::string s2) = 0;
     55 
     56  protected:
     57   ~FakeInterface() {}
     58 };
     59 
     60 // Proxy for the test interface.
     61 BEGIN_PROXY_MAP(Fake)
     62   PROXY_METHOD0(void, VoidMethod0)
     63   PROXY_METHOD0(std::string, Method0)
     64   PROXY_CONSTMETHOD0(std::string, ConstMethod0)
     65   PROXY_METHOD1(std::string, Method1, std::string)
     66   PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
     67   PROXY_METHOD2(std::string, Method2, std::string, std::string)
     68 END_PROXY()
     69 
     70 // Implementation of the test interface.
     71 class Fake : public FakeInterface {
     72  public:
     73   static rtc::scoped_refptr<Fake> Create() {
     74     return new rtc::RefCountedObject<Fake>();
     75   }
     76 
     77   MOCK_METHOD0(VoidMethod0, void());
     78   MOCK_METHOD0(Method0, std::string());
     79   MOCK_CONST_METHOD0(ConstMethod0, std::string());
     80 
     81   MOCK_METHOD1(Method1, std::string(std::string));
     82   MOCK_CONST_METHOD1(ConstMethod1, std::string(std::string));
     83 
     84   MOCK_METHOD2(Method2, std::string(std::string, std::string));
     85 
     86  protected:
     87   Fake() {}
     88   ~Fake() {}
     89 };
     90 
     91 class ProxyTest: public testing::Test {
     92  public:
     93   // Checks that the functions is called on the |signaling_thread_|.
     94   void CheckThread() {
     95     EXPECT_EQ(rtc::Thread::Current(), signaling_thread_.get());
     96   }
     97 
     98  protected:
     99   virtual void SetUp() {
    100     signaling_thread_.reset(new rtc::Thread());
    101     ASSERT_TRUE(signaling_thread_->Start());
    102     fake_ = Fake::Create();
    103     fake_proxy_ = FakeProxy::Create(signaling_thread_.get(), fake_.get());
    104   }
    105 
    106  protected:
    107   rtc::scoped_ptr<rtc::Thread> signaling_thread_;
    108   rtc::scoped_refptr<FakeInterface> fake_proxy_;
    109   rtc::scoped_refptr<Fake> fake_;
    110 };
    111 
    112 TEST_F(ProxyTest, VoidMethod0) {
    113   EXPECT_CALL(*fake_, VoidMethod0())
    114             .Times(Exactly(1))
    115             .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckThread));
    116   fake_proxy_->VoidMethod0();
    117 }
    118 
    119 TEST_F(ProxyTest, Method0) {
    120   EXPECT_CALL(*fake_, Method0())
    121             .Times(Exactly(1))
    122             .WillOnce(
    123                 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
    124                       Return("Method0")));
    125   EXPECT_EQ("Method0",
    126             fake_proxy_->Method0());
    127 }
    128 
    129 TEST_F(ProxyTest, ConstMethod0) {
    130   EXPECT_CALL(*fake_, ConstMethod0())
    131             .Times(Exactly(1))
    132             .WillOnce(
    133                 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
    134                       Return("ConstMethod0")));
    135   EXPECT_EQ("ConstMethod0",
    136             fake_proxy_->ConstMethod0());
    137 }
    138 
    139 TEST_F(ProxyTest, Method1) {
    140   const std::string arg1 = "arg1";
    141   EXPECT_CALL(*fake_, Method1(arg1))
    142             .Times(Exactly(1))
    143             .WillOnce(
    144                 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
    145                       Return("Method1")));
    146   EXPECT_EQ("Method1", fake_proxy_->Method1(arg1));
    147 }
    148 
    149 TEST_F(ProxyTest, ConstMethod1) {
    150   const std::string arg1 = "arg1";
    151   EXPECT_CALL(*fake_, ConstMethod1(arg1))
    152             .Times(Exactly(1))
    153             .WillOnce(
    154                 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
    155                       Return("ConstMethod1")));
    156   EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
    157 }
    158 
    159 TEST_F(ProxyTest, Method2) {
    160   const std::string arg1 = "arg1";
    161   const std::string arg2 = "arg2";
    162   EXPECT_CALL(*fake_, Method2(arg1, arg2))
    163             .Times(Exactly(1))
    164             .WillOnce(
    165                 DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
    166                       Return("Method2")));
    167   EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
    168 }
    169 
    170 }  // namespace webrtc
    171