You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
5.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "talk/app/webrtc/proxy.h"
  28. #include <string>
  29. #include "talk/base/refcount.h"
  30. #include "talk/base/scoped_ptr.h"
  31. #include "talk/base/thread.h"
  32. #include "talk/base/gunit.h"
  33. #include "testing/base/public/gmock.h"
  34. using ::testing::_;
  35. using ::testing::DoAll;
  36. using ::testing::Exactly;
  37. using ::testing::InvokeWithoutArgs;
  38. using ::testing::Return;
  39. namespace webrtc {
  40. // Interface used for testing here.
  41. class FakeInterface : public talk_base::RefCountInterface {
  42. public:
  43. virtual void VoidMethod0() = 0;
  44. virtual std::string Method0() = 0;
  45. virtual std::string ConstMethod0() const = 0;
  46. virtual std::string Method1(std::string s) = 0;
  47. virtual std::string ConstMethod1(std::string s) const = 0;
  48. virtual std::string Method2(std::string s1, std::string s2) = 0;
  49. protected:
  50. ~FakeInterface() {}
  51. };
  52. // Proxy for the test interface.
  53. BEGIN_PROXY_MAP(Fake)
  54. PROXY_METHOD0(void, VoidMethod0)
  55. PROXY_METHOD0(std::string, Method0)
  56. PROXY_CONSTMETHOD0(std::string, ConstMethod0)
  57. PROXY_METHOD1(std::string, Method1, std::string)
  58. PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string)
  59. PROXY_METHOD2(std::string, Method2, std::string, std::string)
  60. END_PROXY()
  61. // Implementation of the test interface.
  62. class Fake : public FakeInterface {
  63. public:
  64. static talk_base::scoped_refptr<Fake> Create() {
  65. return new talk_base::RefCountedObject<Fake>();
  66. }
  67. MOCK_METHOD0(VoidMethod0, void());
  68. MOCK_METHOD0(Method0, std::string());
  69. MOCK_CONST_METHOD0(ConstMethod0, std::string());
  70. MOCK_METHOD1(Method1, std::string(std::string));
  71. MOCK_CONST_METHOD1(ConstMethod1, std::string(std::string));
  72. MOCK_METHOD2(Method2, std::string(std::string, std::string));
  73. protected:
  74. Fake() {}
  75. ~Fake() {}
  76. };
  77. class ProxyTest: public testing::Test {
  78. public:
  79. // Checks that the functions is called on the |signaling_thread_|.
  80. void CheckThread() {
  81. EXPECT_EQ(talk_base::Thread::Current(), signaling_thread_.get());
  82. }
  83. protected:
  84. virtual void SetUp() {
  85. signaling_thread_.reset(new talk_base::Thread());
  86. ASSERT_TRUE(signaling_thread_->Start());
  87. fake_ = Fake::Create();
  88. fake_proxy_ = FakeProxy::Create(signaling_thread_.get(), fake_.get());
  89. }
  90. protected:
  91. talk_base::scoped_ptr<talk_base::Thread> signaling_thread_;
  92. talk_base::scoped_refptr<FakeInterface> fake_proxy_;
  93. talk_base::scoped_refptr<Fake> fake_;
  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. TEST_F(ProxyTest, Method0) {
  102. EXPECT_CALL(*fake_, Method0())
  103. .Times(Exactly(1))
  104. .WillOnce(
  105. DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
  106. Return("Method0")));
  107. EXPECT_EQ("Method0",
  108. fake_proxy_->Method0());
  109. }
  110. TEST_F(ProxyTest, ConstMethod0) {
  111. EXPECT_CALL(*fake_, ConstMethod0())
  112. .Times(Exactly(1))
  113. .WillOnce(
  114. DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
  115. Return("ConstMethod0")));
  116. EXPECT_EQ("ConstMethod0",
  117. fake_proxy_->ConstMethod0());
  118. }
  119. TEST_F(ProxyTest, Method1) {
  120. const std::string arg1 = "arg1";
  121. EXPECT_CALL(*fake_, Method1(arg1))
  122. .Times(Exactly(1))
  123. .WillOnce(
  124. DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
  125. Return("Method1")));
  126. EXPECT_EQ("Method1", fake_proxy_->Method1(arg1));
  127. }
  128. TEST_F(ProxyTest, ConstMethod1) {
  129. const std::string arg1 = "arg1";
  130. EXPECT_CALL(*fake_, ConstMethod1(arg1))
  131. .Times(Exactly(1))
  132. .WillOnce(
  133. DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
  134. Return("ConstMethod1")));
  135. EXPECT_EQ("ConstMethod1", fake_proxy_->ConstMethod1(arg1));
  136. }
  137. TEST_F(ProxyTest, Method2) {
  138. const std::string arg1 = "arg1";
  139. const std::string arg2 = "arg2";
  140. EXPECT_CALL(*fake_, Method2(arg1, arg2))
  141. .Times(Exactly(1))
  142. .WillOnce(
  143. DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckThread),
  144. Return("Method2")));
  145. EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2));
  146. }
  147. } // namespace webrtc