OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 19 matching lines...) Expand all Loading... |
30 class FakeInterface : public rtc::RefCountInterface { | 30 class FakeInterface : public rtc::RefCountInterface { |
31 public: | 31 public: |
32 virtual void VoidMethod0() = 0; | 32 virtual void VoidMethod0() = 0; |
33 virtual std::string Method0() = 0; | 33 virtual std::string Method0() = 0; |
34 virtual std::string ConstMethod0() const = 0; | 34 virtual std::string ConstMethod0() const = 0; |
35 virtual std::string Method1(std::string s) = 0; | 35 virtual std::string Method1(std::string s) = 0; |
36 virtual std::string ConstMethod1(std::string s) const = 0; | 36 virtual std::string ConstMethod1(std::string s) const = 0; |
37 virtual std::string Method2(std::string s1, std::string s2) = 0; | 37 virtual std::string Method2(std::string s1, std::string s2) = 0; |
38 | 38 |
39 protected: | 39 protected: |
40 ~FakeInterface() {} | 40 virtual ~FakeInterface() {} |
41 }; | 41 }; |
42 | 42 |
43 // Implementation of the test interface. | 43 // Implementation of the test interface. |
44 class Fake : public FakeInterface { | 44 class Fake : public FakeInterface { |
45 public: | 45 public: |
46 static rtc::scoped_refptr<Fake> Create() { | 46 static rtc::scoped_refptr<Fake> Create() { |
47 return new rtc::RefCountedObject<Fake>(); | 47 return new rtc::RefCountedObject<Fake>(); |
48 } | 48 } |
| 49 // Used to verify destructor is called on the correct thread. |
| 50 MOCK_METHOD0(Destroy, void()); |
49 | 51 |
50 MOCK_METHOD0(VoidMethod0, void()); | 52 MOCK_METHOD0(VoidMethod0, void()); |
51 MOCK_METHOD0(Method0, std::string()); | 53 MOCK_METHOD0(Method0, std::string()); |
52 MOCK_CONST_METHOD0(ConstMethod0, std::string()); | 54 MOCK_CONST_METHOD0(ConstMethod0, std::string()); |
53 | 55 |
54 MOCK_METHOD1(Method1, std::string(std::string)); | 56 MOCK_METHOD1(Method1, std::string(std::string)); |
55 MOCK_CONST_METHOD1(ConstMethod1, std::string(std::string)); | 57 MOCK_CONST_METHOD1(ConstMethod1, std::string(std::string)); |
56 | 58 |
57 MOCK_METHOD2(Method2, std::string(std::string, std::string)); | 59 MOCK_METHOD2(Method2, std::string(std::string, std::string)); |
58 | 60 |
59 protected: | 61 protected: |
60 Fake() {} | 62 Fake() {} |
61 ~Fake() {} | 63 ~Fake() { Destroy(); } |
62 }; | 64 }; |
63 | 65 |
64 // Proxies for the test interface. | 66 // Proxies for the test interface. |
65 BEGIN_PROXY_MAP(Fake) | 67 BEGIN_PROXY_MAP(Fake) |
| 68 PROXY_WORKER_THREAD_DESTRUCTOR() |
66 PROXY_METHOD0(void, VoidMethod0) | 69 PROXY_METHOD0(void, VoidMethod0) |
67 PROXY_METHOD0(std::string, Method0) | 70 PROXY_METHOD0(std::string, Method0) |
68 PROXY_CONSTMETHOD0(std::string, ConstMethod0) | 71 PROXY_CONSTMETHOD0(std::string, ConstMethod0) |
69 PROXY_WORKER_METHOD1(std::string, Method1, std::string) | 72 PROXY_WORKER_METHOD1(std::string, Method1, std::string) |
70 PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string) | 73 PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string) |
71 PROXY_WORKER_METHOD2(std::string, Method2, std::string, std::string) | 74 PROXY_WORKER_METHOD2(std::string, Method2, std::string, std::string) |
72 END_PROXY() | 75 END_PROXY_MAP() |
73 | 76 |
74 // Preprocessor hack to get a proxy class a name different than FakeProxy. | 77 // Preprocessor hack to get a proxy class a name different than FakeProxy. |
75 #define FakeProxy FakeSignalingProxy | 78 #define FakeProxy FakeSignalingProxy |
76 #define FakeProxyWithInternal FakeSignalingProxyWithInternal | 79 #define FakeProxyWithInternal FakeSignalingProxyWithInternal |
77 BEGIN_SIGNALING_PROXY_MAP(Fake) | 80 BEGIN_SIGNALING_PROXY_MAP(Fake) |
| 81 PROXY_SIGNALING_THREAD_DESTRUCTOR() |
78 PROXY_METHOD0(void, VoidMethod0) | 82 PROXY_METHOD0(void, VoidMethod0) |
79 PROXY_METHOD0(std::string, Method0) | 83 PROXY_METHOD0(std::string, Method0) |
80 PROXY_CONSTMETHOD0(std::string, ConstMethod0) | 84 PROXY_CONSTMETHOD0(std::string, ConstMethod0) |
81 PROXY_METHOD1(std::string, Method1, std::string) | 85 PROXY_METHOD1(std::string, Method1, std::string) |
82 PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string) | 86 PROXY_CONSTMETHOD1(std::string, ConstMethod1, std::string) |
83 PROXY_METHOD2(std::string, Method2, std::string, std::string) | 87 PROXY_METHOD2(std::string, Method2, std::string, std::string) |
84 END_SIGNALING_PROXY() | 88 END_PROXY_MAP() |
85 #undef FakeProxy | 89 #undef FakeProxy |
86 | 90 |
87 class SignalingProxyTest : public testing::Test { | 91 class SignalingProxyTest : public testing::Test { |
88 public: | 92 public: |
89 // Checks that the functions are called on the right thread. | 93 // Checks that the functions are called on the right thread. |
90 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); } | 94 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); } |
91 | 95 |
92 protected: | 96 protected: |
93 void SetUp() override { | 97 void SetUp() override { |
94 signaling_thread_.reset(new rtc::Thread()); | 98 signaling_thread_.reset(new rtc::Thread()); |
95 ASSERT_TRUE(signaling_thread_->Start()); | 99 ASSERT_TRUE(signaling_thread_->Start()); |
96 fake_ = Fake::Create(); | 100 fake_ = Fake::Create(); |
97 fake_signaling_proxy_ = | 101 fake_signaling_proxy_ = |
98 FakeSignalingProxy::Create(signaling_thread_.get(), fake_.get()); | 102 FakeSignalingProxy::Create(signaling_thread_.get(), fake_.get()); |
99 } | 103 } |
100 | 104 |
101 protected: | 105 protected: |
102 std::unique_ptr<rtc::Thread> signaling_thread_; | 106 std::unique_ptr<rtc::Thread> signaling_thread_; |
103 rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_; | 107 rtc::scoped_refptr<FakeInterface> fake_signaling_proxy_; |
104 rtc::scoped_refptr<Fake> fake_; | 108 rtc::scoped_refptr<Fake> fake_; |
105 }; | 109 }; |
106 | 110 |
| 111 TEST_F(SignalingProxyTest, SignalingThreadDestructor) { |
| 112 EXPECT_CALL(*fake_, Destroy()) |
| 113 .Times(Exactly(1)) |
| 114 .WillOnce( |
| 115 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread)); |
| 116 fake_ = nullptr; |
| 117 fake_signaling_proxy_ = nullptr; |
| 118 } |
| 119 |
107 TEST_F(SignalingProxyTest, VoidMethod0) { | 120 TEST_F(SignalingProxyTest, VoidMethod0) { |
108 EXPECT_CALL(*fake_, VoidMethod0()) | 121 EXPECT_CALL(*fake_, VoidMethod0()) |
109 .Times(Exactly(1)) | 122 .Times(Exactly(1)) |
110 .WillOnce( | 123 .WillOnce( |
111 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread)); | 124 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread)); |
112 fake_signaling_proxy_->VoidMethod0(); | 125 fake_signaling_proxy_->VoidMethod0(); |
113 } | 126 } |
114 | 127 |
115 TEST_F(SignalingProxyTest, Method0) { | 128 TEST_F(SignalingProxyTest, Method0) { |
116 EXPECT_CALL(*fake_, Method0()) | 129 EXPECT_CALL(*fake_, Method0()) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 const std::string arg1 = "arg1"; | 167 const std::string arg1 = "arg1"; |
155 const std::string arg2 = "arg2"; | 168 const std::string arg2 = "arg2"; |
156 EXPECT_CALL(*fake_, Method2(arg1, arg2)) | 169 EXPECT_CALL(*fake_, Method2(arg1, arg2)) |
157 .Times(Exactly(1)) | 170 .Times(Exactly(1)) |
158 .WillOnce(DoAll( | 171 .WillOnce(DoAll( |
159 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread), | 172 InvokeWithoutArgs(this, &SignalingProxyTest::CheckSignalingThread), |
160 Return("Method2"))); | 173 Return("Method2"))); |
161 EXPECT_EQ("Method2", fake_signaling_proxy_->Method2(arg1, arg2)); | 174 EXPECT_EQ("Method2", fake_signaling_proxy_->Method2(arg1, arg2)); |
162 } | 175 } |
163 | 176 |
164 class ProxyTest : public SignalingProxyTest { | 177 class ProxyTest : public testing::Test { |
165 public: | 178 public: |
166 // Checks that the functions are called on the right thread. | 179 // Checks that the functions are called on the right thread. |
| 180 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_->IsCurrent()); } |
167 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); } | 181 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_->IsCurrent()); } |
168 | 182 |
169 protected: | 183 protected: |
170 void SetUp() override { | 184 void SetUp() override { |
171 SignalingProxyTest::SetUp(); | 185 signaling_thread_.reset(new rtc::Thread()); |
172 worker_thread_.reset(new rtc::Thread()); | 186 worker_thread_.reset(new rtc::Thread()); |
| 187 ASSERT_TRUE(signaling_thread_->Start()); |
173 ASSERT_TRUE(worker_thread_->Start()); | 188 ASSERT_TRUE(worker_thread_->Start()); |
| 189 fake_ = Fake::Create(); |
174 fake_proxy_ = FakeProxy::Create(signaling_thread_.get(), | 190 fake_proxy_ = FakeProxy::Create(signaling_thread_.get(), |
175 worker_thread_.get(), fake_.get()); | 191 worker_thread_.get(), fake_.get()); |
176 } | 192 } |
177 | 193 |
178 protected: | 194 protected: |
| 195 std::unique_ptr<rtc::Thread> signaling_thread_; |
179 std::unique_ptr<rtc::Thread> worker_thread_; | 196 std::unique_ptr<rtc::Thread> worker_thread_; |
180 rtc::scoped_refptr<FakeInterface> fake_proxy_; | 197 rtc::scoped_refptr<FakeInterface> fake_proxy_; |
| 198 rtc::scoped_refptr<Fake> fake_; |
181 }; | 199 }; |
182 | 200 |
| 201 TEST_F(ProxyTest, WorkerThreadDestructor) { |
| 202 EXPECT_CALL(*fake_, Destroy()) |
| 203 .Times(Exactly(1)) |
| 204 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread)); |
| 205 fake_ = nullptr; |
| 206 fake_proxy_ = nullptr; |
| 207 } |
| 208 |
183 TEST_F(ProxyTest, VoidMethod0) { | 209 TEST_F(ProxyTest, VoidMethod0) { |
184 EXPECT_CALL(*fake_, VoidMethod0()) | 210 EXPECT_CALL(*fake_, VoidMethod0()) |
185 .Times(Exactly(1)) | 211 .Times(Exactly(1)) |
186 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread)); | 212 .WillOnce(InvokeWithoutArgs(this, &ProxyTest::CheckSignalingThread)); |
187 fake_proxy_->VoidMethod0(); | 213 fake_proxy_->VoidMethod0(); |
188 } | 214 } |
189 | 215 |
190 TEST_F(ProxyTest, Method0) { | 216 TEST_F(ProxyTest, Method0) { |
191 EXPECT_CALL(*fake_, Method0()) | 217 EXPECT_CALL(*fake_, Method0()) |
192 .Times(Exactly(1)) | 218 .Times(Exactly(1)) |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 TEST_F(ProxyTest, WorkerMethod2) { | 255 TEST_F(ProxyTest, WorkerMethod2) { |
230 const std::string arg1 = "arg1"; | 256 const std::string arg1 = "arg1"; |
231 const std::string arg2 = "arg2"; | 257 const std::string arg2 = "arg2"; |
232 EXPECT_CALL(*fake_, Method2(arg1, arg2)) | 258 EXPECT_CALL(*fake_, Method2(arg1, arg2)) |
233 .Times(Exactly(1)) | 259 .Times(Exactly(1)) |
234 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread), | 260 .WillOnce(DoAll(InvokeWithoutArgs(this, &ProxyTest::CheckWorkerThread), |
235 Return("Method2"))); | 261 Return("Method2"))); |
236 EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2)); | 262 EXPECT_EQ("Method2", fake_proxy_->Method2(arg1, arg2)); |
237 } | 263 } |
238 | 264 |
| 265 // Interface for testing OWNED_PROXY_MAP. |
| 266 class FooInterface { |
| 267 public: |
| 268 virtual ~FooInterface() {} |
| 269 virtual void Bar() = 0; |
| 270 }; |
| 271 |
| 272 class Foo : public FooInterface { |
| 273 public: |
| 274 Foo() {} |
| 275 MOCK_METHOD0(Bar, void()); |
| 276 }; |
| 277 |
| 278 BEGIN_OWNED_PROXY_MAP(Foo) |
| 279 PROXY_SIGNALING_THREAD_DESTRUCTOR() |
| 280 PROXY_METHOD0(void, Bar) |
| 281 END_PROXY_MAP() |
| 282 |
| 283 class OwnedProxyTest : public testing::Test { |
| 284 public: |
| 285 OwnedProxyTest() |
| 286 : foo_(new Foo()), |
| 287 foo_proxy_( |
| 288 FooProxy::Create(&signaling_thread_, &worker_thread_, foo_)) { |
| 289 signaling_thread_.Start(); |
| 290 worker_thread_.Start(); |
| 291 } |
| 292 |
| 293 void CheckSignalingThread() { EXPECT_TRUE(signaling_thread_.IsCurrent()); } |
| 294 void CheckWorkerThread() { EXPECT_TRUE(worker_thread_.IsCurrent()); } |
| 295 |
| 296 protected: |
| 297 rtc::Thread signaling_thread_; |
| 298 rtc::Thread worker_thread_; |
| 299 Foo* foo_; // Owned by foo_proxy_, not this class. |
| 300 std::unique_ptr<FooInterface> foo_proxy_; |
| 301 }; |
| 302 |
| 303 // Just tests that a method can be invoked using an "owned proxy" (as opposed |
| 304 // to normal ref-counted version). |
| 305 TEST_F(OwnedProxyTest, BasicTest) { |
| 306 EXPECT_CALL(*foo_, Bar()) |
| 307 .Times(Exactly(1)) |
| 308 .WillOnce(InvokeWithoutArgs(this, &OwnedProxyTest::CheckSignalingThread)); |
| 309 foo_proxy_->Bar(); |
| 310 } |
| 311 |
239 } // namespace webrtc | 312 } // namespace webrtc |
OLD | NEW |