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/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/message_loop/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
18 #include "mojo/public/cpp/bindings/strong_binding.h" | 18 #include "mojo/public/cpp/bindings/strong_binding.h" |
19 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" | 19 #include "mojo/public/interfaces/bindings/tests/ping_service.mojom.h" |
20 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | 20 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
21 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" | 21 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 | 23 |
| 24 #include "media/base/run_on_destruction.h" |
| 25 |
24 namespace mojo { | 26 namespace mojo { |
25 namespace { | 27 namespace { |
26 | 28 |
27 class BindingTestBase : public testing::Test { | 29 class BindingTestBase : public testing::Test { |
28 public: | 30 public: |
29 BindingTestBase() {} | 31 BindingTestBase() {} |
30 ~BindingTestBase() override {} | 32 ~BindingTestBase() override {} |
31 | 33 |
32 base::MessageLoop& loop() { return loop_; } | 34 base::MessageLoop& loop() { return loop_; } |
33 | 35 |
(...skipping 14 matching lines...) Expand all Loading... |
48 | 50 |
49 private: | 51 private: |
50 // sample::Service implementation | 52 // sample::Service implementation |
51 void Frobinate(sample::FooPtr foo, | 53 void Frobinate(sample::FooPtr foo, |
52 BazOptions options, | 54 BazOptions options, |
53 sample::PortPtr port, | 55 sample::PortPtr port, |
54 const FrobinateCallback& callback) override { | 56 const FrobinateCallback& callback) override { |
55 callback.Run(1); | 57 callback.Run(1); |
56 } | 58 } |
57 void GetPort(InterfaceRequest<sample::Port> port) override {} | 59 void GetPort(InterfaceRequest<sample::Port> port) override {} |
| 60 void EatCallback(const EatCallbackCallback& callback) override { |
| 61 eat_callback_ = callback; |
| 62 } |
58 | 63 |
59 bool* const was_deleted_; | 64 bool* const was_deleted_; |
| 65 EatCallbackCallback eat_callback_; |
60 | 66 |
61 DISALLOW_COPY_AND_ASSIGN(ServiceImpl); | 67 DISALLOW_COPY_AND_ASSIGN(ServiceImpl); |
62 }; | 68 }; |
63 | 69 |
64 template <typename... Args> | 70 template <typename... Args> |
65 void DoSetFlagAndRunClosure(bool* flag, | 71 void DoSetFlagAndRunClosure(bool* flag, |
66 const base::Closure& closure, | 72 const base::Closure& closure, |
67 Args... args) { | 73 Args... args) { |
68 *flag = true; | 74 *flag = true; |
69 if (!closure.is_null()) | 75 if (!closure.is_null()) |
70 closure.Run(); | 76 closure.Run(); |
71 } | 77 } |
72 | 78 |
73 template <typename... Args> | 79 template <typename... Args> |
74 base::Callback<void(Args...)> SetFlagAndRunClosure( | 80 base::Callback<void(Args...)> SetFlagAndRunClosure( |
75 bool* flag, | 81 bool* flag, |
76 const base::Closure& callback = base::Closure()) { | 82 const base::Closure& callback = base::Closure()) { |
77 return base::Bind(&DoSetFlagAndRunClosure<Args...>, flag, callback); | 83 return base::Bind(&DoSetFlagAndRunClosure<Args...>, flag, callback); |
78 } | 84 } |
79 | 85 |
| 86 void OnResult(bool* result_storage, bool result) { |
| 87 LOG(ERROR) << __func__ << ": " << result; |
| 88 *result_storage = result; |
| 89 } |
| 90 |
80 // BindingTest ----------------------------------------------------------------- | 91 // BindingTest ----------------------------------------------------------------- |
81 | 92 |
82 using BindingTest = BindingTestBase; | 93 using BindingTest = BindingTestBase; |
83 | 94 |
84 TEST_F(BindingTest, Close) { | 95 TEST_F(BindingTest, Close) { |
85 bool called = false; | 96 bool called = false; |
86 sample::ServicePtr ptr; | 97 sample::ServicePtr ptr; |
87 auto request = MakeRequest(&ptr); | 98 auto request = MakeRequest(&ptr); |
88 base::RunLoop run_loop; | 99 base::RunLoop run_loop; |
89 ptr.set_connection_error_handler( | 100 ptr.set_connection_error_handler( |
(...skipping 10 matching lines...) Expand all Loading... |
100 // Tests that destroying a mojo::Binding closes the bound message pipe handle. | 111 // Tests that destroying a mojo::Binding closes the bound message pipe handle. |
101 TEST_F(BindingTest, DestroyClosesMessagePipe) { | 112 TEST_F(BindingTest, DestroyClosesMessagePipe) { |
102 bool encountered_error = false; | 113 bool encountered_error = false; |
103 ServiceImpl impl; | 114 ServiceImpl impl; |
104 sample::ServicePtr ptr; | 115 sample::ServicePtr ptr; |
105 auto request = MakeRequest(&ptr); | 116 auto request = MakeRequest(&ptr); |
106 base::RunLoop run_loop; | 117 base::RunLoop run_loop; |
107 ptr.set_connection_error_handler( | 118 ptr.set_connection_error_handler( |
108 SetFlagAndRunClosure(&encountered_error, run_loop.QuitClosure())); | 119 SetFlagAndRunClosure(&encountered_error, run_loop.QuitClosure())); |
109 bool called = false; | 120 bool called = false; |
| 121 bool result = false; |
110 base::RunLoop run_loop2; | 122 base::RunLoop run_loop2; |
111 { | 123 { |
112 Binding<sample::Service> binding(&impl, std::move(request)); | 124 Binding<sample::Service> binding(&impl, std::move(request)); |
113 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, | 125 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
114 SetFlagAndRunClosure<int32_t>(&called, | 126 SetFlagAndRunClosure<int32_t>(&called, |
115 run_loop2.QuitClosure())); | 127 run_loop2.QuitClosure())); |
| 128 ptr->EatCallback(media::RunOnDestruction(base::Bind(&OnResult, &result), tru
e)); |
116 run_loop2.Run(); | 129 run_loop2.Run(); |
117 EXPECT_TRUE(called); | 130 EXPECT_TRUE(called); |
118 EXPECT_FALSE(encountered_error); | 131 EXPECT_FALSE(encountered_error); |
| 132 EXPECT_FALSE(result); |
119 } | 133 } |
120 // Now that the Binding is out of scope we should detect an error on the other | 134 // Now that the Binding is out of scope we should detect an error on the other |
121 // end of the pipe. | 135 // end of the pipe. |
122 run_loop.Run(); | 136 run_loop.Run(); |
123 EXPECT_TRUE(encountered_error); | 137 EXPECT_TRUE(encountered_error); |
124 | 138 |
125 // And calls should fail. | 139 // And calls should fail. |
126 called = false; | 140 called = false; |
127 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, | 141 ptr->Frobinate(nullptr, sample::Service::BazOptions::REGULAR, nullptr, |
128 SetFlagAndRunClosure<int32_t>(&called, | 142 SetFlagAndRunClosure<int32_t>(&called, |
129 run_loop2.QuitClosure())); | 143 run_loop2.QuitClosure())); |
130 base::RunLoop().RunUntilIdle(); | 144 base::RunLoop().RunUntilIdle(); |
131 EXPECT_FALSE(called); | 145 EXPECT_FALSE(called); |
| 146 EXPECT_TRUE(result); |
132 } | 147 } |
133 | 148 |
134 // Tests that the binding's connection error handler gets called when the other | 149 // Tests that the binding's connection error handler gets called when the other |
135 // end is closed. | 150 // end is closed. |
136 TEST_F(BindingTest, ConnectionError) { | 151 TEST_F(BindingTest, ConnectionError) { |
137 bool called = false; | 152 bool called = false; |
138 { | 153 { |
139 ServiceImpl impl; | 154 ServiceImpl impl; |
140 sample::ServicePtr ptr; | 155 sample::ServicePtr ptr; |
141 Binding<sample::Service> binding(&impl, MakeRequest(&ptr)); | 156 Binding<sample::Service> binding(&impl, MakeRequest(&ptr)); |
(...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
602 }, | 617 }, |
603 run_loop.QuitClosure())); | 618 run_loop.QuitClosure())); |
604 | 619 |
605 ptr.ResetWithReason(5678u, "hello"); | 620 ptr.ResetWithReason(5678u, "hello"); |
606 | 621 |
607 run_loop.Run(); | 622 run_loop.Run(); |
608 } | 623 } |
609 | 624 |
610 } // namespace | 625 } // namespace |
611 } // mojo | 626 } // mojo |
OLD | NEW |