OLD | NEW |
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/error_handler.h" | 5 #include "mojo/public/cpp/bindings/error_handler.h" |
| 6 #include "mojo/public/cpp/bindings/strong_connector.h" |
| 7 #include "mojo/public/cpp/bindings/weak_connector.h" |
6 #include "mojo/public/cpp/environment/environment.h" | 8 #include "mojo/public/cpp/environment/environment.h" |
7 #include "mojo/public/cpp/utility/run_loop.h" | 9 #include "mojo/public/cpp/utility/run_loop.h" |
8 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" | 10 #include "mojo/public/interfaces/bindings/tests/math_calculator.mojom.h" |
9 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" | 11 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 | 13 |
12 namespace mojo { | 14 namespace mojo { |
13 namespace test { | 15 namespace test { |
14 namespace { | 16 namespace { |
15 | 17 |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
344 sample::PortPtr()); | 346 sample::PortPtr()); |
345 proxy->Frobinate(sample::FooPtr(), | 347 proxy->Frobinate(sample::FooPtr(), |
346 sample::Service::BAZ_OPTIONS_REGULAR, | 348 sample::Service::BAZ_OPTIONS_REGULAR, |
347 sample::PortPtr()); | 349 sample::PortPtr()); |
348 | 350 |
349 PumpMessages(); | 351 PumpMessages(); |
350 | 352 |
351 EXPECT_EQ(2, impl->max_call_depth()); | 353 EXPECT_EQ(2, impl->max_call_depth()); |
352 } | 354 } |
353 | 355 |
| 356 class StrongMathCalculatorImpl : public math::Calculator, public ErrorHandler { |
| 357 public: |
| 358 StrongMathCalculatorImpl(ScopedMessagePipeHandle handle, |
| 359 bool* error_received, |
| 360 bool* destroyed) |
| 361 : error_received_(error_received), |
| 362 destroyed_(destroyed), |
| 363 connector_(this, handle.Pass()) { |
| 364 connector_.set_error_handler(this); |
| 365 } |
| 366 ~StrongMathCalculatorImpl() override { *destroyed_ = true; } |
| 367 |
| 368 // math::Calculator implementation. |
| 369 void Clear() override { connector_.client()->Output(total_); } |
| 370 |
| 371 void Add(double value) override { |
| 372 total_ += value; |
| 373 connector_.client()->Output(total_); |
| 374 } |
| 375 |
| 376 void Multiply(double value) override { |
| 377 total_ *= value; |
| 378 connector_.client()->Output(total_); |
| 379 } |
| 380 |
| 381 // ErrorHandler implementation. |
| 382 void OnConnectionError() override { *error_received_ = true; } |
| 383 |
| 384 private: |
| 385 double total_ = 0.0; |
| 386 bool* error_received_; |
| 387 bool* destroyed_; |
| 388 |
| 389 StrongConnector<math::Calculator> connector_; |
| 390 }; |
| 391 |
| 392 TEST(StrongConnectorTest, Math) { |
| 393 Environment env; |
| 394 RunLoop loop; |
| 395 |
| 396 bool error_received = false; |
| 397 bool destroyed = false; |
| 398 MessagePipe pipe; |
| 399 new StrongMathCalculatorImpl( |
| 400 pipe.handle0.Pass(), &error_received, &destroyed); |
| 401 |
| 402 math::CalculatorPtr calc; |
| 403 calc.Bind(pipe.handle1.Pass()); |
| 404 |
| 405 { |
| 406 // Suppose this is instantiated in a process that has the other end of the |
| 407 // message pipe. |
| 408 MathCalculatorUIImpl calculator_ui(calc.Pass()); |
| 409 |
| 410 calculator_ui.Add(2.0); |
| 411 calculator_ui.Multiply(5.0); |
| 412 |
| 413 loop.RunUntilIdle(); |
| 414 |
| 415 EXPECT_EQ(10.0, calculator_ui.GetOutput()); |
| 416 EXPECT_FALSE(error_received); |
| 417 EXPECT_FALSE(destroyed); |
| 418 } |
| 419 // Destroying calculator_ui should close the pipe and generate an error on the |
| 420 // other |
| 421 // end which will destroy the instance since it is strongly bound. |
| 422 |
| 423 loop.RunUntilIdle(); |
| 424 EXPECT_TRUE(error_received); |
| 425 EXPECT_TRUE(destroyed); |
| 426 } |
| 427 |
| 428 class WeakMathCalculatorImpl : public math::Calculator, public ErrorHandler { |
| 429 public: |
| 430 WeakMathCalculatorImpl(ScopedMessagePipeHandle handle, |
| 431 bool* error_received, |
| 432 bool* destroyed) |
| 433 : error_received_(error_received), |
| 434 destroyed_(destroyed), |
| 435 connector_(this, handle.Pass()) { |
| 436 connector_.set_error_handler(this); |
| 437 } |
| 438 ~WeakMathCalculatorImpl() override { *destroyed_ = true; } |
| 439 |
| 440 void Clear() override { connector_.client()->Output(total_); } |
| 441 |
| 442 void Add(double value) override { |
| 443 total_ += value; |
| 444 connector_.client()->Output(total_); |
| 445 } |
| 446 |
| 447 void Multiply(double value) override { |
| 448 total_ *= value; |
| 449 connector_.client()->Output(total_); |
| 450 } |
| 451 |
| 452 // ErrorHandler implementation. |
| 453 void OnConnectionError() override { *error_received_ = true; } |
| 454 |
| 455 private: |
| 456 double total_ = 0.0; |
| 457 bool* error_received_; |
| 458 bool* destroyed_; |
| 459 |
| 460 WeakConnector<math::Calculator> connector_; |
| 461 }; |
| 462 |
| 463 TEST(WeakConnectorTest, Math) { |
| 464 Environment env; |
| 465 RunLoop loop; |
| 466 |
| 467 bool error_received = false; |
| 468 bool destroyed = false; |
| 469 MessagePipe pipe; |
| 470 WeakMathCalculatorImpl impl(pipe.handle0.Pass(), &error_received, &destroyed); |
| 471 |
| 472 math::CalculatorPtr calc; |
| 473 calc.Bind(pipe.handle1.Pass()); |
| 474 |
| 475 { |
| 476 // Suppose this is instantiated in a process that has the other end of the |
| 477 // message pipe. |
| 478 MathCalculatorUIImpl calculator_ui(calc.Pass()); |
| 479 |
| 480 calculator_ui.Add(2.0); |
| 481 calculator_ui.Multiply(5.0); |
| 482 |
| 483 loop.RunUntilIdle(); |
| 484 |
| 485 EXPECT_EQ(10.0, calculator_ui.GetOutput()); |
| 486 EXPECT_FALSE(error_received); |
| 487 EXPECT_FALSE(destroyed); |
| 488 // Destroying calculator_ui should close the pipe and generate an error on |
| 489 // the other |
| 490 // end which will destroy the instance since it is strongly bound. |
| 491 } |
| 492 |
| 493 loop.RunUntilIdle(); |
| 494 EXPECT_TRUE(error_received); |
| 495 EXPECT_FALSE(destroyed); |
| 496 } |
| 497 |
354 } // namespace | 498 } // namespace |
355 } // namespace test | 499 } // namespace test |
356 } // namespace mojo | 500 } // namespace mojo |
OLD | NEW |