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

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

Issue 2318793002: Mojo C++ bindings: support disconnect with a reason. (Closed)
Patch Set: . Created 4 years, 3 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 #include <utility> 6 #include <utility>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 11 matching lines...) Expand all
22 namespace { 22 namespace {
23 23
24 typedef base::Callback<void(double)> CalcCallback; 24 typedef base::Callback<void(double)> CalcCallback;
25 25
26 class MathCalculatorImpl : public math::Calculator { 26 class MathCalculatorImpl : public math::Calculator {
27 public: 27 public:
28 explicit MathCalculatorImpl(InterfaceRequest<math::Calculator> request) 28 explicit MathCalculatorImpl(InterfaceRequest<math::Calculator> request)
29 : total_(0.0), binding_(this, std::move(request)) {} 29 : total_(0.0), binding_(this, std::move(request)) {}
30 ~MathCalculatorImpl() override {} 30 ~MathCalculatorImpl() override {}
31 31
32 void CloseMessagePipe() { binding_.Close(); }
33
34 void WaitForIncomingMethodCall() { binding_.WaitForIncomingMethodCall(); }
35
36 void Clear(const CalcCallback& callback) override { 32 void Clear(const CalcCallback& callback) override {
37 total_ = 0.0; 33 total_ = 0.0;
38 callback.Run(total_); 34 callback.Run(total_);
39 } 35 }
40 36
41 void Add(double value, const CalcCallback& callback) override { 37 void Add(double value, const CalcCallback& callback) override {
42 total_ += value; 38 total_ += value;
43 callback.Run(total_); 39 callback.Run(total_);
44 } 40 }
45 41
46 void Multiply(double value, const CalcCallback& callback) override { 42 void Multiply(double value, const CalcCallback& callback) override {
47 total_ *= value; 43 total_ *= value;
48 callback.Run(total_); 44 callback.Run(total_);
49 } 45 }
50 46
47 Binding<math::Calculator>* binding() { return &binding_; }
48
51 private: 49 private:
52 double total_; 50 double total_;
53 Binding<math::Calculator> binding_; 51 Binding<math::Calculator> binding_;
54 }; 52 };
55 53
56 class MathCalculatorUI { 54 class MathCalculatorUI {
57 public: 55 public:
58 explicit MathCalculatorUI(math::CalculatorPtr calculator) 56 explicit MathCalculatorUI(math::CalculatorPtr calculator)
59 : calculator_(std::move(calculator)), 57 : calculator_(std::move(calculator)),
60 output_(0.0) {} 58 output_(0.0) {}
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 MathCalculatorImpl calc_impl(GetProxy(&calc)); 243 MathCalculatorImpl calc_impl(GetProxy(&calc));
246 244
247 // Suppose this is instantiated in a process that has pipe1_. 245 // Suppose this is instantiated in a process that has pipe1_.
248 MathCalculatorUI calculator_ui(std::move(calc)); 246 MathCalculatorUI calculator_ui(std::move(calc));
249 247
250 EXPECT_EQ(0.0, calculator_ui.GetOutput()); 248 EXPECT_EQ(0.0, calculator_ui.GetOutput());
251 249
252 base::RunLoop run_loop; 250 base::RunLoop run_loop;
253 calculator_ui.Add(2.0, run_loop.QuitClosure()); 251 calculator_ui.Add(2.0, run_loop.QuitClosure());
254 EXPECT_EQ(0.0, calculator_ui.GetOutput()); 252 EXPECT_EQ(0.0, calculator_ui.GetOutput());
255 calc_impl.WaitForIncomingMethodCall(); 253 calc_impl.binding()->WaitForIncomingMethodCall();
256 run_loop.Run(); 254 run_loop.Run();
257 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 255 EXPECT_EQ(2.0, calculator_ui.GetOutput());
258 256
259 base::RunLoop run_loop2; 257 base::RunLoop run_loop2;
260 calculator_ui.Multiply(5.0, run_loop2.QuitClosure()); 258 calculator_ui.Multiply(5.0, run_loop2.QuitClosure());
261 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 259 EXPECT_EQ(2.0, calculator_ui.GetOutput());
262 calc_impl.WaitForIncomingMethodCall(); 260 calc_impl.binding()->WaitForIncomingMethodCall();
263 run_loop2.Run(); 261 run_loop2.Run();
264 EXPECT_EQ(10.0, calculator_ui.GetOutput()); 262 EXPECT_EQ(10.0, calculator_ui.GetOutput());
265 } 263 }
266 264
267 TEST_F(InterfacePtrTest, Movable) { 265 TEST_F(InterfacePtrTest, Movable) {
268 math::CalculatorPtr a; 266 math::CalculatorPtr a;
269 math::CalculatorPtr b; 267 math::CalculatorPtr b;
270 MathCalculatorImpl calc_impl(GetProxy(&b)); 268 MathCalculatorImpl calc_impl(GetProxy(&b));
271 269
272 EXPECT_TRUE(!a); 270 EXPECT_TRUE(!a);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 base::RunLoop run_loop; 319 base::RunLoop run_loop;
322 calculator_ui.Add(2.0, run_loop.QuitClosure()); 320 calculator_ui.Add(2.0, run_loop.QuitClosure());
323 run_loop.Run(); 321 run_loop.Run();
324 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 322 EXPECT_EQ(2.0, calculator_ui.GetOutput());
325 EXPECT_FALSE(calculator_ui.encountered_error()); 323 EXPECT_FALSE(calculator_ui.encountered_error());
326 324
327 calculator_ui.Multiply(5.0, base::Closure()); 325 calculator_ui.Multiply(5.0, base::Closure());
328 EXPECT_FALSE(calculator_ui.encountered_error()); 326 EXPECT_FALSE(calculator_ui.encountered_error());
329 327
330 // Close the server. 328 // Close the server.
331 calc_impl.CloseMessagePipe(); 329 calc_impl.binding()->Close();
332 330
333 // The state change isn't picked up locally yet. 331 // The state change isn't picked up locally yet.
334 base::RunLoop run_loop2; 332 base::RunLoop run_loop2;
335 calculator_ui.set_connection_error_handler(run_loop2.QuitClosure()); 333 calculator_ui.set_connection_error_handler(run_loop2.QuitClosure());
336 EXPECT_FALSE(calculator_ui.encountered_error()); 334 EXPECT_FALSE(calculator_ui.encountered_error());
337 335
338 run_loop2.Run(); 336 run_loop2.Run();
339 337
340 // OK, now we see the error. 338 // OK, now we see the error.
341 EXPECT_TRUE(calculator_ui.encountered_error()); 339 EXPECT_TRUE(calculator_ui.encountered_error());
(...skipping 14 matching lines...) Expand all
356 base::RunLoop run_loop2; 354 base::RunLoop run_loop2;
357 calculator_ui.Add(2.0, run_loop2.QuitClosure()); 355 calculator_ui.Add(2.0, run_loop2.QuitClosure());
358 run_loop2.Run(); 356 run_loop2.Run();
359 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 357 EXPECT_EQ(2.0, calculator_ui.GetOutput());
360 EXPECT_FALSE(calculator_ui.encountered_error()); 358 EXPECT_FALSE(calculator_ui.encountered_error());
361 359
362 calculator_ui.Multiply(5.0, base::Closure()); 360 calculator_ui.Multiply(5.0, base::Closure());
363 EXPECT_FALSE(calculator_ui.encountered_error()); 361 EXPECT_FALSE(calculator_ui.encountered_error());
364 362
365 // Close the server. 363 // Close the server.
366 calc_impl.CloseMessagePipe(); 364 calc_impl.binding()->Close();
367 365
368 // The state change isn't picked up locally yet. 366 // The state change isn't picked up locally yet.
369 EXPECT_FALSE(calculator_ui.encountered_error()); 367 EXPECT_FALSE(calculator_ui.encountered_error());
370 368
371 run_loop.Run(); 369 run_loop.Run();
372 370
373 // OK, now we see the error. 371 // OK, now we see the error.
374 EXPECT_TRUE(calculator_ui.encountered_error()); 372 EXPECT_TRUE(calculator_ui.encountered_error());
375 373
376 // We should have also been able to observe the error through the error 374 // We should have also been able to observe the error through the error
(...skipping 370 matching lines...) Expand 10 before | Expand all | Expand 10 after
747 TEST_F(InterfacePtrTest, FlushForTestingWithClosedPeer) { 745 TEST_F(InterfacePtrTest, FlushForTestingWithClosedPeer) {
748 math::CalculatorPtr calc; 746 math::CalculatorPtr calc;
749 GetProxy(&calc); 747 GetProxy(&calc);
750 bool called = false; 748 bool called = false;
751 calc.set_connection_error_handler(base::Bind(&SetBool, &called)); 749 calc.set_connection_error_handler(base::Bind(&SetBool, &called));
752 calc.FlushForTesting(); 750 calc.FlushForTesting();
753 EXPECT_TRUE(called); 751 EXPECT_TRUE(called);
754 calc.FlushForTesting(); 752 calc.FlushForTesting();
755 } 753 }
756 754
755 TEST_F(InterfacePtrTest, ConnectionErrorWithReason) {
756 math::CalculatorPtr calc;
757 MathCalculatorImpl calc_impl(GetProxy(&calc));
758
759 base::RunLoop run_loop;
760 calc.set_connection_error_with_reason_handler(base::Bind(
761 [](const base::Closure& quit_closure, uint32_t custom_reason,
762 const std::string& description) {
763 EXPECT_EQ(42u, custom_reason);
764 EXPECT_EQ("hey", description);
765 quit_closure.Run();
766 },
767 run_loop.QuitClosure()));
768
769 calc_impl.binding()->CloseWithReason(42u, "hey");
770
771 run_loop.Run();
772 }
773
757 } // namespace 774 } // namespace
758 } // namespace test 775 } // namespace test
759 } // namespace mojo 776 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/tests/binding_unittest.cc ('k') | mojo/public/interfaces/bindings/interface_control_messages.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698