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

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

Issue 864123002: Declient calculator test mojom. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Remove extraneous typedef Created 5 years, 11 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 "mojo/public/cpp/bindings/binding.h" 5 #include "mojo/public/cpp/bindings/binding.h"
6 #include "mojo/public/cpp/bindings/error_handler.h" 6 #include "mojo/public/cpp/bindings/error_handler.h"
7 #include "mojo/public/cpp/bindings/strong_binding.h" 7 #include "mojo/public/cpp/bindings/strong_binding.h"
8 #include "mojo/public/cpp/environment/environment.h" 8 #include "mojo/public/cpp/environment/environment.h"
9 #include "mojo/public/cpp/utility/run_loop.h" 9 #include "mojo/public/cpp/utility/run_loop.h"
10 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" 10 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h"
11 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" 11 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 13
14 namespace mojo { 14 namespace mojo {
15 namespace test { 15 namespace test {
16 namespace { 16 namespace {
17 17
18 class ErrorObserver : public ErrorHandler { 18 class ErrorObserver : public ErrorHandler {
19 public: 19 public:
20 ErrorObserver() : encountered_error_(false) {} 20 ErrorObserver() : encountered_error_(false) {}
21 21
22 bool encountered_error() const { return encountered_error_; } 22 bool encountered_error() const { return encountered_error_; }
23 23
24 void OnConnectionError() override { encountered_error_ = true; } 24 void OnConnectionError() override { encountered_error_ = true; }
25 25
26 private: 26 private:
27 bool encountered_error_; 27 bool encountered_error_;
28 }; 28 };
29 29
30 typedef mojo::Callback<void(double)> CalcCallback;
31
30 class MathCalculatorImpl : public InterfaceImpl<math::Calculator> { 32 class MathCalculatorImpl : public InterfaceImpl<math::Calculator> {
31 public: 33 public:
32 ~MathCalculatorImpl() override {} 34 ~MathCalculatorImpl() override {}
33 35
34 MathCalculatorImpl() : total_(0.0) {} 36 MathCalculatorImpl() : total_(0.0) {}
35 37
36 void Clear() override { client()->Output(total_); } 38 void Clear(const CalcCallback& callback) override {
37 39 total_ = 0.0;
38 void Add(double value) override { 40 callback.Run(total_);
39 total_ += value;
40 client()->Output(total_);
41 } 41 }
42 42
43 void Multiply(double value) override { 43 void Add(double value, const CalcCallback& callback) override {
44 total_ += value;
45 callback.Run(total_);
46 }
47
48 void Multiply(double value, const CalcCallback& callback) override {
44 total_ *= value; 49 total_ *= value;
45 client()->Output(total_); 50 callback.Run(total_);
46 } 51 }
47 52
48 private: 53 private:
49 double total_; 54 double total_;
50 }; 55 };
51 56
52 class MathCalculatorUIImpl : public math::CalculatorUI { 57 class MathCalculatorUI {
53 public: 58 public:
54 explicit MathCalculatorUIImpl(math::CalculatorPtr calculator) 59 explicit MathCalculatorUI(math::CalculatorPtr calculator)
55 : calculator_(calculator.Pass()), output_(0.0) { 60 : calculator_(calculator.Pass()),
56 calculator_.set_client(this); 61 output_(0.0),
57 } 62 callback_(MakeRunnable(&MathCalculatorUI::Output, this)) {}
58 63
59 bool WaitForIncomingMethodCall() { 64 bool WaitForIncomingMethodCall() {
60 return calculator_.WaitForIncomingMethodCall(); 65 return calculator_.WaitForIncomingMethodCall();
61 } 66 }
62 67
63 bool encountered_error() const { return calculator_.encountered_error(); } 68 bool encountered_error() const { return calculator_.encountered_error(); }
64 69
65 void Add(double value) { calculator_->Add(value); } 70 void Add(double value) { calculator_->Add(value, callback_); }
66 71
67 void Subtract(double value) { calculator_->Add(-value); } 72 void Subtract(double value) { calculator_->Add(-value, callback_); }
68 73
69 void Multiply(double value) { calculator_->Multiply(value); } 74 void Multiply(double value) { calculator_->Multiply(value, callback_); }
70 75
71 void Divide(double value) { calculator_->Multiply(1.0 / value); } 76 void Divide(double value) { calculator_->Multiply(1.0 / value, callback_); }
72 77
73 double GetOutput() const { return output_; } 78 double GetOutput() const { return output_; }
74 79
75 private: 80 private:
76 // math::CalculatorUI implementation: 81 void Output(double output) { output_ = output; }
77 void Output(double value) override { output_ = value; }
78 82
79 math::CalculatorPtr calculator_; 83 math::CalculatorPtr calculator_;
80 double output_; 84 double output_;
85 Callback<void(double)> callback_;
81 }; 86 };
82 87
83 class SelfDestructingMathCalculatorUIImpl : public math::CalculatorUI { 88 class SelfDestructingMathCalculatorUI {
84 public: 89 public:
85 explicit SelfDestructingMathCalculatorUIImpl(math::CalculatorPtr calculator) 90 explicit SelfDestructingMathCalculatorUI(math::CalculatorPtr calculator)
86 : calculator_(calculator.Pass()), nesting_level_(0) { 91 : calculator_(calculator.Pass()), nesting_level_(0) {
87 ++num_instances_; 92 ++num_instances_;
88 calculator_.set_client(this);
89 } 93 }
90 94
91 void BeginTest(bool nested) { 95 void BeginTest(bool nested) {
92 nesting_level_ = nested ? 2 : 1; 96 nesting_level_ = nested ? 2 : 1;
93 calculator_->Add(1.0); 97 calculator_->Add(
98 1.0, MakeRunnable(&SelfDestructingMathCalculatorUI::Output, this));
94 } 99 }
95 100
96 static int num_instances() { return num_instances_; } 101 static int num_instances() { return num_instances_; }
97 102
98 private: 103 void Output(double value) {
99 ~SelfDestructingMathCalculatorUIImpl() override { --num_instances_; }
100
101 void Output(double value) override {
102 if (--nesting_level_ > 0) { 104 if (--nesting_level_ > 0) {
103 // Add some more and wait for re-entrant call to Output! 105 // Add some more and wait for re-entrant call to Output!
104 calculator_->Add(1.0); 106 calculator_->Add(
107 1.0, MakeRunnable(&SelfDestructingMathCalculatorUI::Output, this));
105 RunLoop::current()->RunUntilIdle(); 108 RunLoop::current()->RunUntilIdle();
106 } else { 109 } else {
107 delete this; 110 delete this;
108 } 111 }
109 } 112 }
110 113
114 private:
115 ~SelfDestructingMathCalculatorUI() { --num_instances_; }
116
111 math::CalculatorPtr calculator_; 117 math::CalculatorPtr calculator_;
112 int nesting_level_; 118 int nesting_level_;
113 static int num_instances_; 119 static int num_instances_;
114 }; 120 };
115 121
116 // static 122 // static
117 int SelfDestructingMathCalculatorUIImpl::num_instances_ = 0; 123 int SelfDestructingMathCalculatorUI::num_instances_ = 0;
118 124
119 class ReentrantServiceImpl : public InterfaceImpl<sample::Service> { 125 class ReentrantServiceImpl : public InterfaceImpl<sample::Service> {
120 public: 126 public:
121 ~ReentrantServiceImpl() override {} 127 ~ReentrantServiceImpl() override {}
122 128
123 ReentrantServiceImpl() : call_depth_(0), max_call_depth_(0) {} 129 ReentrantServiceImpl() : call_depth_(0), max_call_depth_(0) {}
124 130
125 int max_call_depth() { return max_call_depth_; } 131 int max_call_depth() { return max_call_depth_; }
126 132
127 void Frobinate(sample::FooPtr foo, 133 void Frobinate(sample::FooPtr foo,
(...skipping 22 matching lines...) Expand all
150 private: 156 private:
151 Environment env_; 157 Environment env_;
152 RunLoop loop_; 158 RunLoop loop_;
153 }; 159 };
154 160
155 TEST_F(InterfacePtrTest, EndToEnd) { 161 TEST_F(InterfacePtrTest, EndToEnd) {
156 math::CalculatorPtr calc; 162 math::CalculatorPtr calc;
157 BindToProxy(new MathCalculatorImpl(), &calc); 163 BindToProxy(new MathCalculatorImpl(), &calc);
158 164
159 // Suppose this is instantiated in a process that has pipe1_. 165 // Suppose this is instantiated in a process that has pipe1_.
160 MathCalculatorUIImpl calculator_ui(calc.Pass()); 166 MathCalculatorUI calculator_ui(calc.Pass());
161 167
162 calculator_ui.Add(2.0); 168 calculator_ui.Add(2.0);
163 calculator_ui.Multiply(5.0); 169 calculator_ui.Multiply(5.0);
164 170
165 PumpMessages(); 171 PumpMessages();
166 172
167 EXPECT_EQ(10.0, calculator_ui.GetOutput()); 173 EXPECT_EQ(10.0, calculator_ui.GetOutput());
168 } 174 }
169 175
170 TEST_F(InterfacePtrTest, EndToEnd_Synchronous) { 176 TEST_F(InterfacePtrTest, EndToEnd_Synchronous) {
171 math::CalculatorPtr calc; 177 math::CalculatorPtr calc;
172 MathCalculatorImpl* impl = BindToProxy(new MathCalculatorImpl(), &calc); 178 MathCalculatorImpl* impl = BindToProxy(new MathCalculatorImpl(), &calc);
173 179
174 // Suppose this is instantiated in a process that has pipe1_. 180 // Suppose this is instantiated in a process that has pipe1_.
175 MathCalculatorUIImpl calculator_ui(calc.Pass()); 181 MathCalculatorUI calculator_ui(calc.Pass());
176 182
177 EXPECT_EQ(0.0, calculator_ui.GetOutput()); 183 EXPECT_EQ(0.0, calculator_ui.GetOutput());
178 184
179 calculator_ui.Add(2.0); 185 calculator_ui.Add(2.0);
180 EXPECT_EQ(0.0, calculator_ui.GetOutput()); 186 EXPECT_EQ(0.0, calculator_ui.GetOutput());
181 impl->WaitForIncomingMethodCall(); 187 impl->WaitForIncomingMethodCall();
182 calculator_ui.WaitForIncomingMethodCall(); 188 calculator_ui.WaitForIncomingMethodCall();
183 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 189 EXPECT_EQ(2.0, calculator_ui.GetOutput());
184 190
185 calculator_ui.Multiply(5.0); 191 calculator_ui.Multiply(5.0);
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 EXPECT_FALSE(a.internal_state()->router_for_testing()); 229 EXPECT_FALSE(a.internal_state()->router_for_testing());
224 230
225 // Test that handle was closed. 231 // Test that handle was closed.
226 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle)); 232 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, CloseRaw(handle));
227 } 233 }
228 234
229 TEST_F(InterfacePtrTest, EncounteredError) { 235 TEST_F(InterfacePtrTest, EncounteredError) {
230 math::CalculatorPtr proxy; 236 math::CalculatorPtr proxy;
231 MathCalculatorImpl* server = BindToProxy(new MathCalculatorImpl(), &proxy); 237 MathCalculatorImpl* server = BindToProxy(new MathCalculatorImpl(), &proxy);
232 238
233 MathCalculatorUIImpl calculator_ui(proxy.Pass()); 239 MathCalculatorUI calculator_ui(proxy.Pass());
234 240
235 calculator_ui.Add(2.0); 241 calculator_ui.Add(2.0);
236 PumpMessages(); 242 PumpMessages();
237 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 243 EXPECT_EQ(2.0, calculator_ui.GetOutput());
238 EXPECT_FALSE(calculator_ui.encountered_error()); 244 EXPECT_FALSE(calculator_ui.encountered_error());
239 245
240 calculator_ui.Multiply(5.0); 246 calculator_ui.Multiply(5.0);
241 EXPECT_FALSE(calculator_ui.encountered_error()); 247 EXPECT_FALSE(calculator_ui.encountered_error());
242 248
243 // Close the server. 249 // Close the server.
244 server->internal_router()->CloseMessagePipe(); 250 server->internal_router()->CloseMessagePipe();
245 251
246 // The state change isn't picked up locally yet. 252 // The state change isn't picked up locally yet.
247 EXPECT_FALSE(calculator_ui.encountered_error()); 253 EXPECT_FALSE(calculator_ui.encountered_error());
248 254
249 PumpMessages(); 255 PumpMessages();
250 256
251 // OK, now we see the error. 257 // OK, now we see the error.
252 EXPECT_TRUE(calculator_ui.encountered_error()); 258 EXPECT_TRUE(calculator_ui.encountered_error());
253 } 259 }
254 260
255 TEST_F(InterfacePtrTest, EncounteredErrorCallback) { 261 TEST_F(InterfacePtrTest, EncounteredErrorCallback) {
256 math::CalculatorPtr proxy; 262 math::CalculatorPtr proxy;
257 MathCalculatorImpl* server = BindToProxy(new MathCalculatorImpl(), &proxy); 263 MathCalculatorImpl* server = BindToProxy(new MathCalculatorImpl(), &proxy);
258 264
259 ErrorObserver error_observer; 265 ErrorObserver error_observer;
260 proxy.set_error_handler(&error_observer); 266 proxy.set_error_handler(&error_observer);
261 267
262 MathCalculatorUIImpl calculator_ui(proxy.Pass()); 268 MathCalculatorUI calculator_ui(proxy.Pass());
263 269
264 calculator_ui.Add(2.0); 270 calculator_ui.Add(2.0);
265 PumpMessages(); 271 PumpMessages();
266 EXPECT_EQ(2.0, calculator_ui.GetOutput()); 272 EXPECT_EQ(2.0, calculator_ui.GetOutput());
267 EXPECT_FALSE(calculator_ui.encountered_error()); 273 EXPECT_FALSE(calculator_ui.encountered_error());
268 274
269 calculator_ui.Multiply(5.0); 275 calculator_ui.Multiply(5.0);
270 EXPECT_FALSE(calculator_ui.encountered_error()); 276 EXPECT_FALSE(calculator_ui.encountered_error());
271 277
272 // Close the server. 278 // Close the server.
(...skipping 17 matching lines...) Expand all
290 // does not have an explicit Client attribute. 296 // does not have an explicit Client attribute.
291 sample::PortPtr port; 297 sample::PortPtr port;
292 MessagePipe pipe; 298 MessagePipe pipe;
293 port.Bind(pipe.handle0.Pass()); 299 port.Bind(pipe.handle0.Pass());
294 } 300 }
295 301
296 TEST_F(InterfacePtrTest, DestroyInterfacePtrOnClientMethod) { 302 TEST_F(InterfacePtrTest, DestroyInterfacePtrOnClientMethod) {
297 math::CalculatorPtr proxy; 303 math::CalculatorPtr proxy;
298 BindToProxy(new MathCalculatorImpl(), &proxy); 304 BindToProxy(new MathCalculatorImpl(), &proxy);
299 305
300 EXPECT_EQ(0, SelfDestructingMathCalculatorUIImpl::num_instances()); 306 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances());
301 307
302 SelfDestructingMathCalculatorUIImpl* impl = 308 SelfDestructingMathCalculatorUI* impl =
303 new SelfDestructingMathCalculatorUIImpl(proxy.Pass()); 309 new SelfDestructingMathCalculatorUI(proxy.Pass());
304 impl->BeginTest(false); 310 impl->BeginTest(false);
305 311
306 PumpMessages(); 312 PumpMessages();
307 313
308 EXPECT_EQ(0, SelfDestructingMathCalculatorUIImpl::num_instances()); 314 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances());
309 } 315 }
310 316
311 TEST_F(InterfacePtrTest, NestedDestroyInterfacePtrOnClientMethod) { 317 TEST_F(InterfacePtrTest, NestedDestroyInterfacePtrOnClientMethod) {
312 math::CalculatorPtr proxy; 318 math::CalculatorPtr proxy;
313 BindToProxy(new MathCalculatorImpl(), &proxy); 319 BindToProxy(new MathCalculatorImpl(), &proxy);
314 320
315 EXPECT_EQ(0, SelfDestructingMathCalculatorUIImpl::num_instances()); 321 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances());
316 322
317 SelfDestructingMathCalculatorUIImpl* impl = 323 SelfDestructingMathCalculatorUI* impl =
318 new SelfDestructingMathCalculatorUIImpl(proxy.Pass()); 324 new SelfDestructingMathCalculatorUI(proxy.Pass());
319 impl->BeginTest(true); 325 impl->BeginTest(true);
320 326
321 PumpMessages(); 327 PumpMessages();
322 328
323 EXPECT_EQ(0, SelfDestructingMathCalculatorUIImpl::num_instances()); 329 EXPECT_EQ(0, SelfDestructingMathCalculatorUI::num_instances());
324 } 330 }
325 331
326 TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) { 332 TEST_F(InterfacePtrTest, ReentrantWaitForIncomingMethodCall) {
327 sample::ServicePtr proxy; 333 sample::ServicePtr proxy;
328 ReentrantServiceImpl* impl = BindToProxy(new ReentrantServiceImpl(), &proxy); 334 ReentrantServiceImpl* impl = BindToProxy(new ReentrantServiceImpl(), &proxy);
329 335
330 proxy->Frobinate(nullptr, sample::Service::BAZ_OPTIONS_REGULAR, nullptr); 336 proxy->Frobinate(nullptr, sample::Service::BAZ_OPTIONS_REGULAR, nullptr);
331 proxy->Frobinate(nullptr, sample::Service::BAZ_OPTIONS_REGULAR, nullptr); 337 proxy->Frobinate(nullptr, sample::Service::BAZ_OPTIONS_REGULAR, nullptr);
332 338
333 PumpMessages(); 339 PumpMessages();
334 340
335 EXPECT_EQ(2, impl->max_call_depth()); 341 EXPECT_EQ(2, impl->max_call_depth());
336 } 342 }
337 343
338 class StrongMathCalculatorImpl : public math::Calculator, public ErrorHandler { 344 class StrongMathCalculatorImpl : public math::Calculator, public ErrorHandler {
339 public: 345 public:
340 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, 346 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle,
341 bool* error_received, 347 bool* error_received,
342 bool* destroyed) 348 bool* destroyed)
343 : error_received_(error_received), 349 : error_received_(error_received),
344 destroyed_(destroyed), 350 destroyed_(destroyed),
345 binding_(this, handle.Pass()) { 351 binding_(this, handle.Pass()) {
346 binding_.set_error_handler(this); 352 binding_.set_error_handler(this);
347 } 353 }
348 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } 354 ~StrongMathCalculatorImpl() override { *destroyed_ = true; }
349 355
350 // math::Calculator implementation. 356 // math::Calculator implementation.
351 void Clear() override { binding_.client()->Output(total_); } 357 void Clear(const CalcCallback& callback) override { callback.Run(total_); }
352 358
353 void Add(double value) override { 359 void Add(double value, const CalcCallback& callback) override {
354 total_ += value; 360 total_ += value;
355 binding_.client()->Output(total_); 361 callback.Run(total_);
356 } 362 }
357 363
358 void Multiply(double value) override { 364 void Multiply(double value, const CalcCallback& callback) override {
359 total_ *= value; 365 total_ *= value;
360 binding_.client()->Output(total_); 366 callback.Run(total_);
361 } 367 }
362 368
363 // ErrorHandler implementation. 369 // ErrorHandler implementation.
364 void OnConnectionError() override { *error_received_ = true; } 370 void OnConnectionError() override { *error_received_ = true; }
365 371
366 private: 372 private:
367 double total_ = 0.0; 373 double total_ = 0.0;
368 bool* error_received_; 374 bool* error_received_;
369 bool* destroyed_; 375 bool* destroyed_;
370 376
371 StrongBinding<math::Calculator> binding_; 377 StrongBinding<math::Calculator> binding_;
372 }; 378 };
373 379
374 TEST(StrongConnectorTest, Math) { 380 TEST(StrongConnectorTest, Math) {
375 Environment env; 381 Environment env;
376 RunLoop loop; 382 RunLoop loop;
377 383
378 bool error_received = false; 384 bool error_received = false;
379 bool destroyed = false; 385 bool destroyed = false;
380 MessagePipe pipe; 386 MessagePipe pipe;
381 new StrongMathCalculatorImpl(pipe.handle0.Pass(), &error_received, 387 new StrongMathCalculatorImpl(pipe.handle0.Pass(), &error_received,
382 &destroyed); 388 &destroyed);
383 389
384 math::CalculatorPtr calc; 390 math::CalculatorPtr calc;
385 calc.Bind(pipe.handle1.Pass()); 391 calc.Bind(pipe.handle1.Pass());
386 392
387 { 393 {
388 // Suppose this is instantiated in a process that has the other end of the 394 // Suppose this is instantiated in a process that has the other end of the
389 // message pipe. 395 // message pipe.
390 MathCalculatorUIImpl calculator_ui(calc.Pass()); 396 MathCalculatorUI calculator_ui(calc.Pass());
391 397
392 calculator_ui.Add(2.0); 398 calculator_ui.Add(2.0);
393 calculator_ui.Multiply(5.0); 399 calculator_ui.Multiply(5.0);
394 400
395 loop.RunUntilIdle(); 401 loop.RunUntilIdle();
396 402
397 EXPECT_EQ(10.0, calculator_ui.GetOutput()); 403 EXPECT_EQ(10.0, calculator_ui.GetOutput());
398 EXPECT_FALSE(error_received); 404 EXPECT_FALSE(error_received);
399 EXPECT_FALSE(destroyed); 405 EXPECT_FALSE(destroyed);
400 } 406 }
(...skipping 11 matching lines...) Expand all
412 WeakMathCalculatorImpl(ScopedMessagePipeHandle handle, 418 WeakMathCalculatorImpl(ScopedMessagePipeHandle handle,
413 bool* error_received, 419 bool* error_received,
414 bool* destroyed) 420 bool* destroyed)
415 : error_received_(error_received), 421 : error_received_(error_received),
416 destroyed_(destroyed), 422 destroyed_(destroyed),
417 binding_(this, handle.Pass()) { 423 binding_(this, handle.Pass()) {
418 binding_.set_error_handler(this); 424 binding_.set_error_handler(this);
419 } 425 }
420 ~WeakMathCalculatorImpl() override { *destroyed_ = true; } 426 ~WeakMathCalculatorImpl() override { *destroyed_ = true; }
421 427
422 void Clear() override { binding_.client()->Output(total_); } 428 void Clear(const CalcCallback& callback) override { callback.Run(total_); }
423 429
424 void Add(double value) override { 430 void Add(double value, const CalcCallback& callback) override {
425 total_ += value; 431 total_ += value;
426 binding_.client()->Output(total_); 432 callback.Run(total_);
427 } 433 }
428 434
429 void Multiply(double value) override { 435 void Multiply(double value, const CalcCallback& callback) override {
430 total_ *= value; 436 total_ *= value;
431 binding_.client()->Output(total_); 437 callback.Run(total_);
432 } 438 }
433 439
434 // ErrorHandler implementation. 440 // ErrorHandler implementation.
435 void OnConnectionError() override { *error_received_ = true; } 441 void OnConnectionError() override { *error_received_ = true; }
436 442
437 private: 443 private:
438 double total_ = 0.0; 444 double total_ = 0.0;
439 bool* error_received_; 445 bool* error_received_;
440 bool* destroyed_; 446 bool* destroyed_;
441 447
442 Binding<math::Calculator> binding_; 448 Binding<math::Calculator> binding_;
443 }; 449 };
444 450
445 TEST(WeakConnectorTest, Math) { 451 TEST(WeakConnectorTest, Math) {
446 Environment env; 452 Environment env;
447 RunLoop loop; 453 RunLoop loop;
448 454
449 bool error_received = false; 455 bool error_received = false;
450 bool destroyed = false; 456 bool destroyed = false;
451 MessagePipe pipe; 457 MessagePipe pipe;
452 WeakMathCalculatorImpl impl(pipe.handle0.Pass(), &error_received, &destroyed); 458 WeakMathCalculatorImpl impl(pipe.handle0.Pass(), &error_received, &destroyed);
453 459
454 math::CalculatorPtr calc; 460 math::CalculatorPtr calc;
455 calc.Bind(pipe.handle1.Pass()); 461 calc.Bind(pipe.handle1.Pass());
456 462
457 { 463 {
458 // Suppose this is instantiated in a process that has the other end of the 464 // Suppose this is instantiated in a process that has the other end of the
459 // message pipe. 465 // message pipe.
460 MathCalculatorUIImpl calculator_ui(calc.Pass()); 466 MathCalculatorUI calculator_ui(calc.Pass());
461 467
462 calculator_ui.Add(2.0); 468 calculator_ui.Add(2.0);
463 calculator_ui.Multiply(5.0); 469 calculator_ui.Multiply(5.0);
464 470
465 loop.RunUntilIdle(); 471 loop.RunUntilIdle();
466 472
467 EXPECT_EQ(10.0, calculator_ui.GetOutput()); 473 EXPECT_EQ(10.0, calculator_ui.GetOutput());
468 EXPECT_FALSE(error_received); 474 EXPECT_FALSE(error_received);
469 EXPECT_FALSE(destroyed); 475 EXPECT_FALSE(destroyed);
470 // Destroying calculator_ui should close the pipe and generate an error on 476 // Destroying calculator_ui should close the pipe and generate an error on
471 // the other 477 // the other
472 // end which will destroy the instance since it is strongly bound. 478 // end which will destroy the instance since it is strongly bound.
473 } 479 }
474 480
475 loop.RunUntilIdle(); 481 loop.RunUntilIdle();
476 EXPECT_TRUE(error_received); 482 EXPECT_TRUE(error_received);
477 EXPECT_FALSE(destroyed); 483 EXPECT_FALSE(destroyed);
478 } 484 }
479 485
480 } // namespace 486 } // namespace
481 } // namespace test 487 } // namespace test
482 } // namespace mojo 488 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698