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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
49 | 49 |
50 void set_connection_error_handler(const Closure& handler) { | 50 void set_connection_error_handler(const Closure& handler) { |
51 binding_.set_connection_error_handler(handler); | 51 binding_.set_connection_error_handler(handler); |
52 } | 52 } |
53 | 53 |
54 private: | 54 private: |
55 AssociatedBinding<IntegerSender> binding_; | 55 AssociatedBinding<IntegerSender> binding_; |
56 base::Callback<void(int32_t)> notify_send_method_called_; | 56 base::Callback<void(int32_t)> notify_send_method_called_; |
57 }; | 57 }; |
58 | 58 |
| 59 class IntegerSenderConnectionImpl : public IntegerSenderConnection { |
| 60 public: |
| 61 explicit IntegerSenderConnectionImpl( |
| 62 InterfaceRequest<IntegerSenderConnection> request) |
| 63 : binding_(this, request.Pass()) {} |
| 64 |
| 65 ~IntegerSenderConnectionImpl() override {} |
| 66 |
| 67 void GetSender(AssociatedInterfaceRequest<IntegerSender> sender) override { |
| 68 IntegerSenderImpl* sender_impl = new IntegerSenderImpl(sender.Pass()); |
| 69 sender_impl->set_connection_error_handler( |
| 70 [sender_impl]() { delete sender_impl; }); |
| 71 } |
| 72 |
| 73 void AsyncGetSender(const AsyncGetSenderCallback& callback) override { |
| 74 AssociatedInterfaceRequest<IntegerSender> request; |
| 75 AssociatedInterfacePtrInfo<IntegerSender> ptr_info; |
| 76 binding_.associated_group()->CreateAssociatedInterface( |
| 77 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); |
| 78 GetSender(request.Pass()); |
| 79 callback.Run(ptr_info.Pass()); |
| 80 } |
| 81 |
| 82 private: |
| 83 Binding<IntegerSenderConnection> binding_; |
| 84 }; |
| 85 |
59 class AssociatedInterfaceTest : public testing::Test { | 86 class AssociatedInterfaceTest : public testing::Test { |
60 public: | 87 public: |
61 AssociatedInterfaceTest() : loop_(common::MessagePumpMojo::Create()) {} | 88 AssociatedInterfaceTest() : loop_(common::MessagePumpMojo::Create()) {} |
62 ~AssociatedInterfaceTest() override { loop_.RunUntilIdle(); } | 89 ~AssociatedInterfaceTest() override { loop_.RunUntilIdle(); } |
63 | 90 |
64 void PumpMessages() { loop_.RunUntilIdle(); } | 91 void PumpMessages() { loop_.RunUntilIdle(); } |
65 | 92 |
66 template <typename T> | 93 template <typename T> |
67 AssociatedInterfacePtrInfo<T> EmulatePassingAssociatedPtrInfo( | 94 AssociatedInterfacePtrInfo<T> EmulatePassingAssociatedPtrInfo( |
68 AssociatedInterfacePtrInfo<T> ptr_info, | 95 AssociatedInterfacePtrInfo<T> ptr_info, |
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 | 466 |
440 EXPECT_EQ(static_cast<size_t>(kMaxValue / 2), receivers[0].values().size()); | 467 EXPECT_EQ(static_cast<size_t>(kMaxValue / 2), receivers[0].values().size()); |
441 EXPECT_EQ(static_cast<size_t>(kMaxValue / 2), receivers[1].values().size()); | 468 EXPECT_EQ(static_cast<size_t>(kMaxValue / 2), receivers[1].values().size()); |
442 | 469 |
443 for (size_t i = 0; i < 2; ++i) { | 470 for (size_t i = 0; i < 2; ++i) { |
444 for (size_t j = 1; j < receivers[i].values().size(); ++j) | 471 for (size_t j = 1; j < receivers[i].values().size(); ++j) |
445 EXPECT_LT(receivers[i].values()[j - 1], receivers[i].values()[j]); | 472 EXPECT_LT(receivers[i].values()[j - 1], receivers[i].values()[j]); |
446 } | 473 } |
447 } | 474 } |
448 | 475 |
| 476 TEST_F(AssociatedInterfaceTest, PassAssociatedInterfaces) { |
| 477 IntegerSenderConnectionPtr connection_ptr; |
| 478 IntegerSenderConnectionImpl connection(GetProxy(&connection_ptr)); |
| 479 |
| 480 AssociatedInterfacePtr<IntegerSender> sender0; |
| 481 connection_ptr->GetSender( |
| 482 GetProxy(&sender0, connection_ptr.associated_group())); |
| 483 |
| 484 int32_t echoed_value = 0; |
| 485 sender0->Echo(123, [&echoed_value](int32_t value) { echoed_value = value; }); |
| 486 PumpMessages(); |
| 487 EXPECT_EQ(123, echoed_value); |
| 488 |
| 489 AssociatedInterfacePtr<IntegerSender> sender1; |
| 490 connection_ptr->AsyncGetSender( |
| 491 [&sender1](AssociatedInterfacePtrInfo<IntegerSender> ptr_info) { |
| 492 sender1.Bind(ptr_info.Pass()); |
| 493 }); |
| 494 PumpMessages(); |
| 495 EXPECT_TRUE(sender1); |
| 496 |
| 497 sender1->Echo(456, [&echoed_value](int32_t value) { echoed_value = value; }); |
| 498 PumpMessages(); |
| 499 EXPECT_EQ(456, echoed_value); |
| 500 } |
| 501 |
449 } // namespace | 502 } // namespace |
450 } // namespace test | 503 } // namespace test |
451 } // namespace mojo | 504 } // namespace mojo |
OLD | NEW |