OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <stdint.h> | 5 #include <stdint.h> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
11 #include "build/build_config.h" | 12 #include "build/build_config.h" |
12 #include "mojo/public/cpp/bindings/binding.h" | 13 #include "mojo/public/cpp/bindings/binding.h" |
13 #include "mojo/public/cpp/bindings/interface_ptr.h" | 14 #include "mojo/public/cpp/bindings/interface_ptr.h" |
14 #include "mojo/public/cpp/bindings/string.h" | 15 #include "mojo/public/cpp/bindings/string.h" |
15 #include "mojo/public/cpp/system/message_pipe.h" | 16 #include "mojo/public/cpp/system/message_pipe.h" |
16 #include "mojo/public/cpp/test_support/test_support.h" | 17 #include "mojo/public/cpp/test_support/test_support.h" |
17 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | 18 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
19 | 20 |
20 /////////////////////////////////////////////////////////////////////////////// | 21 /////////////////////////////////////////////////////////////////////////////// |
21 // | 22 // |
22 // The tests in this file are designed to test the interaction between a | 23 // The tests in this file are designed to test the interaction between a |
23 // Callback and its associated Binding. If a Callback is deleted before | 24 // Callback and its associated Binding. If a Callback is deleted before |
24 // being used we DCHECK fail--unless the associated Binding has already | 25 // being used we DCHECK fail--unless the associated Binding has already |
25 // been closed or deleted. This contract must be explained to the Mojo | 26 // been closed or deleted. This contract must be explained to the Mojo |
26 // application developer. For example it is the developer's responsibility to | 27 // application developer. For example it is the developer's responsibility to |
27 // ensure that the Binding is destroyed before an unused Callback is destroyed. | 28 // ensure that the Binding is destroyed before an unused Callback is destroyed. |
28 // | 29 // |
29 /////////////////////////////////////////////////////////////////////////////// | 30 /////////////////////////////////////////////////////////////////////////////// |
30 | 31 |
31 namespace mojo { | 32 namespace mojo { |
32 namespace test { | 33 namespace test { |
33 namespace { | 34 namespace { |
34 | 35 |
35 // A Runnable object that saves the last value it sees via the | 36 void SaveValue(int32_t* storage, const base::Closure& closure, int32_t value) { |
36 // provided int32_t*. Used on the client side. | 37 *storage = value; |
37 class ValueSaver { | 38 if (!closure.is_null()) |
38 public: | 39 closure.Run(); |
39 ValueSaver(int32_t* last_value_seen, const base::Closure& closure) | 40 } |
40 : last_value_seen_(last_value_seen), closure_(closure) {} | |
41 void Run(int32_t x) const { | |
42 *last_value_seen_ = x; | |
43 if (!closure_.is_null()) { | |
44 closure_.Run(); | |
45 closure_.Reset(); | |
46 } | |
47 } | |
48 | 41 |
49 private: | 42 base::Callback<void(int32_t)> BindValueSaver(int32_t* last_value_seen, |
50 int32_t* const last_value_seen_; | 43 const base::Closure& closure) { |
51 mutable base::Closure closure_; | 44 return base::Bind(&SaveValue, last_value_seen, closure); |
52 }; | 45 } |
53 | 46 |
54 // An implementation of sample::Provider used on the server side. | 47 // An implementation of sample::Provider used on the server side. |
55 // It only implements one of the methods: EchoInt(). | 48 // It only implements one of the methods: EchoInt(). |
56 // All it does is save the values and Callbacks it sees. | 49 // All it does is save the values and Callbacks it sees. |
57 class InterfaceImpl : public sample::Provider { | 50 class InterfaceImpl : public sample::Provider { |
58 public: | 51 public: |
59 InterfaceImpl() | 52 InterfaceImpl() |
60 : last_server_value_seen_(0), | 53 : last_server_value_seen_(0), |
61 callback_saved_(new Callback<void(int32_t)>()) {} | 54 callback_saved_(new Callback<void(int32_t)>()) {} |
62 | 55 |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
152 | 145 |
153 // Initialize the test values. | 146 // Initialize the test values. |
154 server_impl.resetLastServerValueSeen(); | 147 server_impl.resetLastServerValueSeen(); |
155 last_client_callback_value_seen_ = 0; | 148 last_client_callback_value_seen_ = 0; |
156 | 149 |
157 // Invoke the Echo method. | 150 // Invoke the Echo method. |
158 base::RunLoop run_loop, run_loop2; | 151 base::RunLoop run_loop, run_loop2; |
159 server_impl.set_closure(run_loop.QuitClosure()); | 152 server_impl.set_closure(run_loop.QuitClosure()); |
160 interface_ptr_->EchoInt( | 153 interface_ptr_->EchoInt( |
161 7, | 154 7, |
162 ValueSaver(&last_client_callback_value_seen_, run_loop2.QuitClosure())); | 155 BindValueSaver(&last_client_callback_value_seen_, |
| 156 run_loop2.QuitClosure())); |
163 run_loop.Run(); | 157 run_loop.Run(); |
164 | 158 |
165 // Check that server saw the correct value, but the client has not yet. | 159 // Check that server saw the correct value, but the client has not yet. |
166 EXPECT_EQ(7, server_impl.last_server_value_seen()); | 160 EXPECT_EQ(7, server_impl.last_server_value_seen()); |
167 EXPECT_EQ(0, last_client_callback_value_seen_); | 161 EXPECT_EQ(0, last_client_callback_value_seen_); |
168 | 162 |
169 // Now run the Callback. | 163 // Now run the Callback. |
170 server_impl.RunCallback(); | 164 server_impl.RunCallback(); |
171 run_loop2.Run(); | 165 run_loop2.Run(); |
172 | 166 |
173 // Check that the client has now seen the correct value. | 167 // Check that the client has now seen the correct value. |
174 EXPECT_EQ(7, last_client_callback_value_seen_); | 168 EXPECT_EQ(7, last_client_callback_value_seen_); |
175 | 169 |
176 // Initialize the test values again. | 170 // Initialize the test values again. |
177 server_impl.resetLastServerValueSeen(); | 171 server_impl.resetLastServerValueSeen(); |
178 last_client_callback_value_seen_ = 0; | 172 last_client_callback_value_seen_ = 0; |
179 | 173 |
180 // Invoke the Echo method again. | 174 // Invoke the Echo method again. |
181 base::RunLoop run_loop3, run_loop4; | 175 base::RunLoop run_loop3, run_loop4; |
182 server_impl.set_closure(run_loop3.QuitClosure()); | 176 server_impl.set_closure(run_loop3.QuitClosure()); |
183 interface_ptr_->EchoInt( | 177 interface_ptr_->EchoInt( |
184 13, | 178 13, |
185 ValueSaver(&last_client_callback_value_seen_, run_loop4.QuitClosure())); | 179 BindValueSaver(&last_client_callback_value_seen_, |
| 180 run_loop4.QuitClosure())); |
186 run_loop3.Run(); | 181 run_loop3.Run(); |
187 | 182 |
188 // Check that server saw the correct value, but the client has not yet. | 183 // Check that server saw the correct value, but the client has not yet. |
189 EXPECT_EQ(13, server_impl.last_server_value_seen()); | 184 EXPECT_EQ(13, server_impl.last_server_value_seen()); |
190 EXPECT_EQ(0, last_client_callback_value_seen_); | 185 EXPECT_EQ(0, last_client_callback_value_seen_); |
191 | 186 |
192 // Now run the Callback again. | 187 // Now run the Callback again. |
193 server_impl.RunCallback(); | 188 server_impl.RunCallback(); |
194 run_loop4.Run(); | 189 run_loop4.Run(); |
195 | 190 |
(...skipping 14 matching lines...) Expand all Loading... |
210 | 205 |
211 // Initialize the test values. | 206 // Initialize the test values. |
212 server_impl.resetLastServerValueSeen(); | 207 server_impl.resetLastServerValueSeen(); |
213 last_client_callback_value_seen_ = 0; | 208 last_client_callback_value_seen_ = 0; |
214 | 209 |
215 // Invoke the Echo method. | 210 // Invoke the Echo method. |
216 base::RunLoop run_loop2; | 211 base::RunLoop run_loop2; |
217 server_impl.set_closure(run_loop2.QuitClosure()); | 212 server_impl.set_closure(run_loop2.QuitClosure()); |
218 interface_ptr_->EchoInt( | 213 interface_ptr_->EchoInt( |
219 7, | 214 7, |
220 ValueSaver(&last_client_callback_value_seen_, base::Closure())); | 215 BindValueSaver(&last_client_callback_value_seen_, base::Closure())); |
221 run_loop2.Run(); | 216 run_loop2.Run(); |
222 } | 217 } |
223 // The binding has now been destroyed and the pipe is closed. | 218 // The binding has now been destroyed and the pipe is closed. |
224 | 219 |
225 // Check that server saw the correct value, but the client has not yet. | 220 // Check that server saw the correct value, but the client has not yet. |
226 EXPECT_EQ(7, server_impl.last_server_value_seen()); | 221 EXPECT_EQ(7, server_impl.last_server_value_seen()); |
227 EXPECT_EQ(0, last_client_callback_value_seen_); | 222 EXPECT_EQ(0, last_client_callback_value_seen_); |
228 | 223 |
229 // Now try to run the Callback. This should do nothing since the pipe | 224 // Now try to run the Callback. This should do nothing since the pipe |
230 // is closed. | 225 // is closed. |
231 EXPECT_TRUE(server_impl.RunCallback()); | 226 EXPECT_TRUE(server_impl.RunCallback()); |
232 PumpMessages(); | 227 PumpMessages(); |
233 | 228 |
234 // Check that the client has still not seen the correct value. | 229 // Check that the client has still not seen the correct value. |
235 EXPECT_EQ(0, last_client_callback_value_seen_); | 230 EXPECT_EQ(0, last_client_callback_value_seen_); |
236 | 231 |
237 // Attempt to invoke the method again and confirm that an error was | 232 // Attempt to invoke the method again and confirm that an error was |
238 // encountered. | 233 // encountered. |
239 interface_ptr_->EchoInt( | 234 interface_ptr_->EchoInt( |
240 13, | 235 13, |
241 ValueSaver(&last_client_callback_value_seen_, base::Closure())); | 236 BindValueSaver(&last_client_callback_value_seen_, base::Closure())); |
242 run_loop.Run(); | 237 run_loop.Run(); |
243 EXPECT_TRUE(interface_ptr_.encountered_error()); | 238 EXPECT_TRUE(interface_ptr_.encountered_error()); |
244 } | 239 } |
245 | 240 |
246 // Tests that deleting a Callback without running it after the corresponding | 241 // Tests that deleting a Callback without running it after the corresponding |
247 // binding has already been deleted does not result in a crash. | 242 // binding has already been deleted does not result in a crash. |
248 TEST_F(BindingCallbackTest, DeleteBindingThenDeleteCallback) { | 243 TEST_F(BindingCallbackTest, DeleteBindingThenDeleteCallback) { |
249 // Create the ServerImpl. | 244 // Create the ServerImpl. |
250 InterfaceImpl server_impl; | 245 InterfaceImpl server_impl; |
251 { | 246 { |
252 // Create the binding in an inner scope so it can be deleted first. | 247 // Create the binding in an inner scope so it can be deleted first. |
253 Binding<sample::Provider> binding(&server_impl, GetProxy(&interface_ptr_)); | 248 Binding<sample::Provider> binding(&server_impl, GetProxy(&interface_ptr_)); |
254 | 249 |
255 // Initialize the test values. | 250 // Initialize the test values. |
256 server_impl.resetLastServerValueSeen(); | 251 server_impl.resetLastServerValueSeen(); |
257 last_client_callback_value_seen_ = 0; | 252 last_client_callback_value_seen_ = 0; |
258 | 253 |
259 // Invoke the Echo method. | 254 // Invoke the Echo method. |
260 base::RunLoop run_loop; | 255 base::RunLoop run_loop; |
261 server_impl.set_closure(run_loop.QuitClosure()); | 256 server_impl.set_closure(run_loop.QuitClosure()); |
262 interface_ptr_->EchoInt( | 257 interface_ptr_->EchoInt( |
263 7, | 258 7, |
264 ValueSaver(&last_client_callback_value_seen_, base::Closure())); | 259 BindValueSaver(&last_client_callback_value_seen_, base::Closure())); |
265 run_loop.Run(); | 260 run_loop.Run(); |
266 } | 261 } |
267 // The binding has now been destroyed and the pipe is closed. | 262 // The binding has now been destroyed and the pipe is closed. |
268 | 263 |
269 // Check that server saw the correct value, but the client has not yet. | 264 // Check that server saw the correct value, but the client has not yet. |
270 EXPECT_EQ(7, server_impl.last_server_value_seen()); | 265 EXPECT_EQ(7, server_impl.last_server_value_seen()); |
271 EXPECT_EQ(0, last_client_callback_value_seen_); | 266 EXPECT_EQ(0, last_client_callback_value_seen_); |
272 | 267 |
273 // Delete the callback without running it. This should not | 268 // Delete the callback without running it. This should not |
274 // cause a problem because the insfrastructure can detect that the | 269 // cause a problem because the insfrastructure can detect that the |
(...skipping 10 matching lines...) Expand all Loading... |
285 | 280 |
286 // Initialize the test values. | 281 // Initialize the test values. |
287 server_impl.resetLastServerValueSeen(); | 282 server_impl.resetLastServerValueSeen(); |
288 last_client_callback_value_seen_ = 0; | 283 last_client_callback_value_seen_ = 0; |
289 | 284 |
290 // Invoke the Echo method. | 285 // Invoke the Echo method. |
291 base::RunLoop run_loop; | 286 base::RunLoop run_loop; |
292 server_impl.set_closure(run_loop.QuitClosure()); | 287 server_impl.set_closure(run_loop.QuitClosure()); |
293 interface_ptr_->EchoInt( | 288 interface_ptr_->EchoInt( |
294 7, | 289 7, |
295 ValueSaver(&last_client_callback_value_seen_, base::Closure())); | 290 BindValueSaver(&last_client_callback_value_seen_, base::Closure())); |
296 run_loop.Run(); | 291 run_loop.Run(); |
297 | 292 |
298 // Check that server saw the correct value, but the client has not yet. | 293 // Check that server saw the correct value, but the client has not yet. |
299 EXPECT_EQ(7, server_impl.last_server_value_seen()); | 294 EXPECT_EQ(7, server_impl.last_server_value_seen()); |
300 EXPECT_EQ(0, last_client_callback_value_seen_); | 295 EXPECT_EQ(0, last_client_callback_value_seen_); |
301 | 296 |
302 // Now close the Binding. | 297 // Now close the Binding. |
303 binding.Close(); | 298 binding.Close(); |
304 | 299 |
305 // Delete the callback without running it. This should not | 300 // Delete the callback without running it. This should not |
(...skipping 14 matching lines...) Expand all Loading... |
320 | 315 |
321 // Initialize the test values. | 316 // Initialize the test values. |
322 server_impl.resetLastServerValueSeen(); | 317 server_impl.resetLastServerValueSeen(); |
323 last_client_callback_value_seen_ = 0; | 318 last_client_callback_value_seen_ = 0; |
324 | 319 |
325 // Invoke the Echo method. | 320 // Invoke the Echo method. |
326 base::RunLoop run_loop; | 321 base::RunLoop run_loop; |
327 server_impl.set_closure(run_loop.QuitClosure()); | 322 server_impl.set_closure(run_loop.QuitClosure()); |
328 interface_ptr_->EchoInt( | 323 interface_ptr_->EchoInt( |
329 7, | 324 7, |
330 ValueSaver(&last_client_callback_value_seen_, base::Closure())); | 325 BindValueSaver(&last_client_callback_value_seen_, base::Closure())); |
331 run_loop.Run(); | 326 run_loop.Run(); |
332 | 327 |
333 // Check that server saw the correct value, but the client has not yet. | 328 // Check that server saw the correct value, but the client has not yet. |
334 EXPECT_EQ(7, server_impl.last_server_value_seen()); | 329 EXPECT_EQ(7, server_impl.last_server_value_seen()); |
335 EXPECT_EQ(0, last_client_callback_value_seen_); | 330 EXPECT_EQ(0, last_client_callback_value_seen_); |
336 | 331 |
337 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && !defined(OS_ANDROID) | 332 #if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && !defined(OS_ANDROID) |
338 // Delete the callback without running it. This should cause a crash in debug | 333 // Delete the callback without running it. This should cause a crash in debug |
339 // builds due to a DCHECK. | 334 // builds due to a DCHECK. |
340 std::string regex("Check failed: !is_valid"); | 335 std::string regex("Check failed: !is_valid"); |
341 #if defined(OS_WIN) | 336 #if defined(OS_WIN) |
342 // TODO(msw): Fix MOJO_DCHECK logs and EXPECT_DEATH* on Win: crbug.com/535014 | 337 // TODO(msw): Fix MOJO_DCHECK logs and EXPECT_DEATH* on Win: crbug.com/535014 |
343 regex.clear(); | 338 regex.clear(); |
344 #endif // OS_WIN | 339 #endif // OS_WIN |
345 EXPECT_DEATH_IF_SUPPORTED(server_impl.DeleteCallback(), regex.c_str()); | 340 EXPECT_DEATH_IF_SUPPORTED(server_impl.DeleteCallback(), regex.c_str()); |
346 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && | 341 #endif // (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)) && |
347 // !defined(OS_ANDROID) | 342 // !defined(OS_ANDROID) |
348 } | 343 } |
349 | 344 |
350 } // namespace | 345 } // namespace |
351 } // namespace test | 346 } // namespace test |
352 } // namespace mojo | 347 } // namespace mojo |
OLD | NEW |