Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(130)

Side by Side Diff: mojo/public/cpp/bindings/tests/associated_interface_unittest.cc

Issue 2062333002: mojo::Callback -> base::Callback (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 #include <algorithm> 7 #include <algorithm>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
60 public: 60 public:
61 explicit IntegerSenderConnectionImpl( 61 explicit IntegerSenderConnectionImpl(
62 InterfaceRequest<IntegerSenderConnection> request) 62 InterfaceRequest<IntegerSenderConnection> request)
63 : binding_(this, std::move(request)) {} 63 : binding_(this, std::move(request)) {}
64 64
65 ~IntegerSenderConnectionImpl() override {} 65 ~IntegerSenderConnectionImpl() override {}
66 66
67 void GetSender(AssociatedInterfaceRequest<IntegerSender> sender) override { 67 void GetSender(AssociatedInterfaceRequest<IntegerSender> sender) override {
68 IntegerSenderImpl* sender_impl = new IntegerSenderImpl(std::move(sender)); 68 IntegerSenderImpl* sender_impl = new IntegerSenderImpl(std::move(sender));
69 sender_impl->set_connection_error_handler( 69 sender_impl->set_connection_error_handler(
70 [sender_impl]() { delete sender_impl; }); 70 base::Bind(&DeleteSender, sender_impl));
71 } 71 }
72 72
73 void AsyncGetSender(const AsyncGetSenderCallback& callback) override { 73 void AsyncGetSender(const AsyncGetSenderCallback& callback) override {
74 AssociatedInterfaceRequest<IntegerSender> request; 74 AssociatedInterfaceRequest<IntegerSender> request;
75 IntegerSenderAssociatedPtrInfo ptr_info; 75 IntegerSenderAssociatedPtrInfo ptr_info;
76 binding_.associated_group()->CreateAssociatedInterface( 76 binding_.associated_group()->CreateAssociatedInterface(
77 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request); 77 AssociatedGroup::WILL_PASS_PTR, &ptr_info, &request);
78 GetSender(std::move(request)); 78 GetSender(std::move(request));
79 callback.Run(std::move(ptr_info)); 79 callback.Run(std::move(ptr_info));
80 } 80 }
81 81
82 Binding<IntegerSenderConnection>* binding() { return &binding_; } 82 Binding<IntegerSenderConnection>* binding() { return &binding_; }
83 83
84 private: 84 private:
85 static void DeleteSender(IntegerSenderImpl* sender) { delete sender; }
86
85 Binding<IntegerSenderConnection> binding_; 87 Binding<IntegerSenderConnection> binding_;
86 }; 88 };
87 89
88 class AssociatedInterfaceTest : public testing::Test { 90 class AssociatedInterfaceTest : public testing::Test {
89 public: 91 public:
90 AssociatedInterfaceTest() {} 92 AssociatedInterfaceTest() {}
91 ~AssociatedInterfaceTest() override { loop_.RunUntilIdle(); } 93 ~AssociatedInterfaceTest() override { loop_.RunUntilIdle(); }
92 94
93 void PumpMessages() { loop_.RunUntilIdle(); } 95 void PumpMessages() { loop_.RunUntilIdle(); }
94 96
(...skipping 27 matching lines...) Expand all
122 FROM_HERE, 124 FROM_HERE,
123 base::Bind(&AssociatedInterfaceTest::QuitRunLoop, 125 base::Bind(&AssociatedInterfaceTest::QuitRunLoop,
124 base::Unretained(this), base::Unretained(run_loop))); 126 base::Unretained(this), base::Unretained(run_loop)));
125 } 127 }
126 } 128 }
127 129
128 private: 130 private:
129 base::MessageLoop loop_; 131 base::MessageLoop loop_;
130 }; 132 };
131 133
134 void DoSetFlagAndRunClosure(bool* flag, const base::Closure& closure) {
135 *flag = true;
136 closure.Run();
137 }
138
139 void DoExpectValueSetFlagAndRunClosure(int32_t expected_value,
140 bool* flag,
141 const base::Closure& closure,
142 int32_t value) {
143 EXPECT_EQ(expected_value, value);
144 DoSetFlagAndRunClosure(flag, closure);
145 }
146
147 base::Closure SetFlagAndRunClosure(bool* flag, const base::Closure& closure) {
148 return base::Bind(&DoSetFlagAndRunClosure, flag, closure);
149 }
150
151 base::Callback<void(int32_t)> ExpectValueSetFlagAndRunClosure(
152 int32_t expected_value,
153 bool* flag,
154 const base::Closure& closure) {
155 return base::Bind(
156 &DoExpectValueSetFlagAndRunClosure, expected_value, flag, closure);
157 }
158
132 TEST_F(AssociatedInterfaceTest, InterfacesAtBothEnds) { 159 TEST_F(AssociatedInterfaceTest, InterfacesAtBothEnds) {
133 // Bind to the same pipe two associated interfaces, whose implementation lives 160 // Bind to the same pipe two associated interfaces, whose implementation lives
134 // at different ends. Test that the two don't interfere with each other. 161 // at different ends. Test that the two don't interfere with each other.
135 162
136 MessagePipe pipe; 163 MessagePipe pipe;
137 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter( 164 scoped_refptr<MultiplexRouter> router0(new MultiplexRouter(
138 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get())); 165 true, std::move(pipe.handle0), base::ThreadTaskRunnerHandle::Get()));
139 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter( 166 scoped_refptr<MultiplexRouter> router1(new MultiplexRouter(
140 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get())); 167 false, std::move(pipe.handle1), base::ThreadTaskRunnerHandle::Get()));
141 168
(...skipping 11 matching lines...) Expand all
153 router0->CreateAssociatedGroup()->CreateAssociatedInterface( 180 router0->CreateAssociatedGroup()->CreateAssociatedInterface(
154 AssociatedGroup::WILL_PASS_REQUEST, &ptr_info, &request); 181 AssociatedGroup::WILL_PASS_REQUEST, &ptr_info, &request);
155 request = EmulatePassingAssociatedRequest(std::move(request), router1); 182 request = EmulatePassingAssociatedRequest(std::move(request), router1);
156 183
157 IntegerSenderImpl impl1(std::move(request)); 184 IntegerSenderImpl impl1(std::move(request));
158 AssociatedInterfacePtr<IntegerSender> ptr1; 185 AssociatedInterfacePtr<IntegerSender> ptr1;
159 ptr1.Bind(std::move(ptr_info)); 186 ptr1.Bind(std::move(ptr_info));
160 187
161 base::RunLoop run_loop, run_loop2; 188 base::RunLoop run_loop, run_loop2;
162 bool ptr0_callback_run = false; 189 bool ptr0_callback_run = false;
163 ptr0->Echo(123, [&ptr0_callback_run, &run_loop](int32_t value) { 190 ptr0->Echo(123, ExpectValueSetFlagAndRunClosure(123, &ptr0_callback_run,
164 EXPECT_EQ(123, value); 191 run_loop.QuitClosure()));
165 ptr0_callback_run = true;
166 run_loop.Quit();
167 });
168 192
169 bool ptr1_callback_run = false; 193 bool ptr1_callback_run = false;
170 ptr1->Echo(456, [&ptr1_callback_run, &run_loop2](int32_t value) { 194 ptr1->Echo(456, ExpectValueSetFlagAndRunClosure(456, &ptr1_callback_run,
171 EXPECT_EQ(456, value); 195 run_loop2.QuitClosure()));
172 ptr1_callback_run = true;
173 run_loop2.Quit();
174 });
175 196
176 run_loop.Run(); 197 run_loop.Run();
177 run_loop2.Run(); 198 run_loop2.Run();
178 EXPECT_TRUE(ptr0_callback_run); 199 EXPECT_TRUE(ptr0_callback_run);
179 EXPECT_TRUE(ptr1_callback_run); 200 EXPECT_TRUE(ptr1_callback_run);
180 201
181 bool ptr0_error_callback_run = false; 202 bool ptr0_error_callback_run = false;
182 base::RunLoop run_loop3; 203 base::RunLoop run_loop3;
183 ptr0.set_connection_error_handler([&ptr0_error_callback_run, &run_loop3]() { 204 ptr0.set_connection_error_handler(
184 ptr0_error_callback_run = true; 205 SetFlagAndRunClosure(&ptr0_error_callback_run, run_loop3.QuitClosure()));
185 run_loop3.Quit();
186 });
187 206
188 impl0.binding()->Close(); 207 impl0.binding()->Close();
189 run_loop3.Run(); 208 run_loop3.Run();
190 EXPECT_TRUE(ptr0_error_callback_run); 209 EXPECT_TRUE(ptr0_error_callback_run);
191 210
192 bool impl1_error_callback_run = false; 211 bool impl1_error_callback_run = false;
193 base::RunLoop run_loop4; 212 base::RunLoop run_loop4;
194 impl1.binding()->set_connection_error_handler( 213 impl1.binding()->set_connection_error_handler(
195 [&impl1_error_callback_run, &run_loop4]() { 214 SetFlagAndRunClosure(&impl1_error_callback_run, run_loop4.QuitClosure()));
196 impl1_error_callback_run = true;
197 run_loop4.Quit();
198 });
199 215
200 ptr1.reset(); 216 ptr1.reset();
201 run_loop4.Run(); 217 run_loop4.Run();
202 EXPECT_TRUE(impl1_error_callback_run); 218 EXPECT_TRUE(impl1_error_callback_run);
203 } 219 }
204 220
205 class TestSender { 221 class TestSender {
206 public: 222 public:
207 TestSender() 223 TestSender()
208 : sender_thread_("TestSender"), 224 : sender_thread_("TestSender"),
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
497 513
498 EXPECT_EQ(static_cast<size_t>(kMaxValue / 2), receivers[0].values().size()); 514 EXPECT_EQ(static_cast<size_t>(kMaxValue / 2), receivers[0].values().size());
499 EXPECT_EQ(static_cast<size_t>(kMaxValue / 2), receivers[1].values().size()); 515 EXPECT_EQ(static_cast<size_t>(kMaxValue / 2), receivers[1].values().size());
500 516
501 for (size_t i = 0; i < 2; ++i) { 517 for (size_t i = 0; i < 2; ++i) {
502 for (size_t j = 1; j < receivers[i].values().size(); ++j) 518 for (size_t j = 1; j < receivers[i].values().size(); ++j)
503 EXPECT_LT(receivers[i].values()[j - 1], receivers[i].values()[j]); 519 EXPECT_LT(receivers[i].values()[j - 1], receivers[i].values()[j]);
504 } 520 }
505 } 521 }
506 522
523 void CaptureInt32(int32_t* storage,
524 const base::Closure& closure,
525 int32_t value) {
526 *storage = value;
527 closure.Run();
528 }
529
530 void CaptureSenderPtrInfo(IntegerSenderAssociatedPtr* storage,
531 const base::Closure& closure,
532 IntegerSenderAssociatedPtrInfo info) {
533 storage->Bind(std::move(info));
534 closure.Run();
535 }
536
507 TEST_F(AssociatedInterfaceTest, PassAssociatedInterfaces) { 537 TEST_F(AssociatedInterfaceTest, PassAssociatedInterfaces) {
508 IntegerSenderConnectionPtr connection_ptr; 538 IntegerSenderConnectionPtr connection_ptr;
509 IntegerSenderConnectionImpl connection(GetProxy(&connection_ptr)); 539 IntegerSenderConnectionImpl connection(GetProxy(&connection_ptr));
510 540
511 IntegerSenderAssociatedPtr sender0; 541 IntegerSenderAssociatedPtr sender0;
512 connection_ptr->GetSender( 542 connection_ptr->GetSender(
513 GetProxy(&sender0, connection_ptr.associated_group())); 543 GetProxy(&sender0, connection_ptr.associated_group()));
514 544
515 int32_t echoed_value = 0; 545 int32_t echoed_value = 0;
516 base::RunLoop run_loop; 546 base::RunLoop run_loop;
517 sender0->Echo(123, [&echoed_value, &run_loop](int32_t value) { 547 sender0->Echo(123, base::Bind(&CaptureInt32, &echoed_value,
518 echoed_value = value; 548 run_loop.QuitClosure()));
519 run_loop.Quit();
520 });
521 run_loop.Run(); 549 run_loop.Run();
522 EXPECT_EQ(123, echoed_value); 550 EXPECT_EQ(123, echoed_value);
523 551
524 IntegerSenderAssociatedPtr sender1; 552 IntegerSenderAssociatedPtr sender1;
525 base::RunLoop run_loop2; 553 base::RunLoop run_loop2;
526 connection_ptr->AsyncGetSender( 554 connection_ptr->AsyncGetSender(
527 [&sender1, &run_loop2](IntegerSenderAssociatedPtrInfo ptr_info) { 555 base::Bind(&CaptureSenderPtrInfo, &sender1, run_loop2.QuitClosure()));
528 sender1.Bind(std::move(ptr_info));
529 run_loop2.Quit();
530 });
531 run_loop2.Run(); 556 run_loop2.Run();
532 EXPECT_TRUE(sender1); 557 EXPECT_TRUE(sender1);
533 558
534 base::RunLoop run_loop3; 559 base::RunLoop run_loop3;
535 sender1->Echo(456, [&echoed_value, &run_loop3](int32_t value) { 560 sender1->Echo(456, base::Bind(&CaptureInt32, &echoed_value,
536 echoed_value = value; 561 run_loop3.QuitClosure()));
537 run_loop3.Quit();
538 });
539 run_loop3.Run(); 562 run_loop3.Run();
540 EXPECT_EQ(456, echoed_value); 563 EXPECT_EQ(456, echoed_value);
541 } 564 }
542 565
543 TEST_F(AssociatedInterfaceTest, BindingWaitAndPauseWhenNoAssociatedInterfaces) { 566 TEST_F(AssociatedInterfaceTest, BindingWaitAndPauseWhenNoAssociatedInterfaces) {
544 IntegerSenderConnectionPtr connection_ptr; 567 IntegerSenderConnectionPtr connection_ptr;
545 IntegerSenderConnectionImpl connection(GetProxy(&connection_ptr)); 568 IntegerSenderConnectionImpl connection(GetProxy(&connection_ptr));
546 569
547 IntegerSenderAssociatedPtr sender0; 570 IntegerSenderAssociatedPtr sender0;
548 connection_ptr->GetSender( 571 connection_ptr->GetSender(
(...skipping 11 matching lines...) Expand all
560 583
561 // The previous wait has dispatched the GetSender request message, therefore 584 // The previous wait has dispatched the GetSender request message, therefore
562 // an associated interface has been set up on the pipe. It is not allowed to 585 // an associated interface has been set up on the pipe. It is not allowed to
563 // wait or pause. 586 // wait or pause.
564 EXPECT_TRUE(connection.binding()->HasAssociatedInterfaces()); 587 EXPECT_TRUE(connection.binding()->HasAssociatedInterfaces());
565 } 588 }
566 589
567 } // namespace 590 } // namespace
568 } // namespace test 591 } // namespace test
569 } // namespace mojo 592 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/BUILD.gn ('k') | mojo/public/cpp/bindings/tests/bind_task_runner_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698