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 // Note: This file tests both binding.h (mojo::Binding) and strong_binding.h | 5 // Note: This file tests both binding.h (mojo::Binding) and strong_binding.h |
6 // (mojo::StrongBinding). | 6 // (mojo::StrongBinding). |
7 | 7 |
8 #include "mojo/public/cpp/bindings/binding.h" | 8 #include "mojo/public/cpp/bindings/binding.h" |
9 | 9 |
10 #include <stdint.h> | 10 #include <stdint.h> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "mojo/public/cpp/bindings/strong_binding.h" | 16 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 17 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.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 "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" | 19 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 namespace mojo { | 22 namespace mojo { |
22 namespace { | 23 namespace { |
23 | 24 |
24 class BindingTestBase : public testing::Test { | 25 class BindingTestBase : public testing::Test { |
25 public: | 26 public: |
26 BindingTestBase() {} | 27 BindingTestBase() {} |
(...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 base::RunLoop().RunUntilIdle(); | 304 base::RunLoop().RunUntilIdle(); |
304 // The connection error handle should not be called as the binding is paused. | 305 // The connection error handle should not be called as the binding is paused. |
305 EXPECT_FALSE(called); | 306 EXPECT_FALSE(called); |
306 | 307 |
307 // Resume the binding, which should trigger the error handler. | 308 // Resume the binding, which should trigger the error handler. |
308 binding.ResumeIncomingMethodCallProcessing(); | 309 binding.ResumeIncomingMethodCallProcessing(); |
309 run_loop.Run(); | 310 run_loop.Run(); |
310 EXPECT_TRUE(called); | 311 EXPECT_TRUE(called); |
311 } | 312 } |
312 | 313 |
| 314 class PingServiceImpl : public test::PingService { |
| 315 public: |
| 316 explicit PingServiceImpl(test::PingServiceRequest request) |
| 317 : binding_(this, std::move(request)) {} |
| 318 ~PingServiceImpl() override {} |
| 319 |
| 320 // test::PingService: |
| 321 void Ping(const PingCallback& callback) override { |
| 322 if (!ping_handler_.is_null()) |
| 323 ping_handler_.Run(); |
| 324 callback.Run(); |
| 325 } |
| 326 |
| 327 mojo::Binding<test::PingService>& binding() { return binding_; } |
| 328 |
| 329 void set_ping_handler(const base::Closure& handler) { |
| 330 ping_handler_ = handler; |
| 331 } |
| 332 |
| 333 private: |
| 334 mojo::Binding<test::PingService> binding_; |
| 335 base::Closure ping_handler_; |
| 336 |
| 337 DISALLOW_COPY_AND_ASSIGN(PingServiceImpl); |
| 338 }; |
| 339 |
| 340 class CallbackFilter : public MessageReceiver { |
| 341 public: |
| 342 explicit CallbackFilter(const base::Closure& callback) |
| 343 : callback_(callback) {} |
| 344 ~CallbackFilter() override {} |
| 345 |
| 346 static std::unique_ptr<CallbackFilter> Wrap(const base::Closure& callback) { |
| 347 return base::MakeUnique<CallbackFilter>(callback); |
| 348 } |
| 349 |
| 350 // MessageReceiver: |
| 351 bool Accept(Message* message) override { |
| 352 callback_.Run(); |
| 353 return true; |
| 354 } |
| 355 |
| 356 private: |
| 357 const base::Closure callback_; |
| 358 }; |
| 359 |
| 360 // Verifies that message filters are notified in the order they were added and |
| 361 // are always notified before a message is dispatched. |
| 362 TEST_F(BindingTest, MessageFilter) { |
| 363 test::PingServicePtr ptr; |
| 364 PingServiceImpl impl(GetProxy(&ptr)); |
| 365 |
| 366 int status = 0; |
| 367 auto handler_helper = [] (int* status, int expected_status, int new_status) { |
| 368 EXPECT_EQ(expected_status, *status); |
| 369 *status = new_status; |
| 370 }; |
| 371 auto create_handler = [&] (int expected_status, int new_status) { |
| 372 return base::Bind(handler_helper, &status, expected_status, new_status); |
| 373 }; |
| 374 |
| 375 impl.binding().AddFilter(CallbackFilter::Wrap(create_handler(0, 1))); |
| 376 impl.binding().AddFilter(CallbackFilter::Wrap(create_handler(1, 2))); |
| 377 impl.set_ping_handler(create_handler(2, 3)); |
| 378 |
| 379 for (int i = 0; i < 10; ++i) { |
| 380 status = 0; |
| 381 base::RunLoop loop; |
| 382 ptr->Ping(loop.QuitClosure()); |
| 383 loop.Run(); |
| 384 EXPECT_EQ(3, status); |
| 385 } |
| 386 } |
| 387 |
313 // StrongBindingTest ----------------------------------------------------------- | 388 // StrongBindingTest ----------------------------------------------------------- |
314 | 389 |
315 using StrongBindingTest = BindingTestBase; | 390 using StrongBindingTest = BindingTestBase; |
316 | 391 |
317 // Tests that destroying a mojo::StrongBinding closes the bound message pipe | 392 // Tests that destroying a mojo::StrongBinding closes the bound message pipe |
318 // handle but does *not* destroy the implementation object. | 393 // handle but does *not* destroy the implementation object. |
319 TEST_F(StrongBindingTest, DestroyClosesMessagePipe) { | 394 TEST_F(StrongBindingTest, DestroyClosesMessagePipe) { |
320 base::RunLoop run_loop; | 395 base::RunLoop run_loop; |
321 bool encountered_error = false; | 396 bool encountered_error = false; |
322 bool was_deleted = false; | 397 bool was_deleted = false; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 was_deleted = false; // It shouldn't be double-deleted! | 480 was_deleted = false; // It shouldn't be double-deleted! |
406 run_loop.Run(); | 481 run_loop.Run(); |
407 EXPECT_TRUE(ptr_error_handler_called); | 482 EXPECT_TRUE(ptr_error_handler_called); |
408 EXPECT_FALSE(was_deleted); | 483 EXPECT_FALSE(was_deleted); |
409 | 484 |
410 EXPECT_FALSE(binding_error_handler_called); | 485 EXPECT_FALSE(binding_error_handler_called); |
411 } | 486 } |
412 | 487 |
413 } // namespace | 488 } // namespace |
414 } // mojo | 489 } // mojo |
OLD | NEW |