| 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 #include "mojo/public/cpp/bindings/binding.h" | 5 #include "mojo/public/cpp/bindings/binding.h" |
| 6 #include "mojo/public/cpp/environment/environment.h" | 6 #include "mojo/public/cpp/environment/environment.h" |
| 7 #include "mojo/public/cpp/utility/run_loop.h" | 7 #include "mojo/public/cpp/utility/run_loop.h" |
| 8 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" | 8 #include "mojo/public/interfaces/bindings/tests/sample_interfaces.mojom.h" |
| 9 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" | 9 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 ~IntegerAccessorImpl() override {} | 34 ~IntegerAccessorImpl() override {} |
| 35 | 35 |
| 36 private: | 36 private: |
| 37 // sample::IntegerAccessor implementation. | 37 // sample::IntegerAccessor implementation. |
| 38 void GetInteger(const GetIntegerCallback& callback) override { | 38 void GetInteger(const GetIntegerCallback& callback) override { |
| 39 callback.Run(1, sample::ENUM_VALUE); | 39 callback.Run(1, sample::ENUM_VALUE); |
| 40 } | 40 } |
| 41 void SetInteger(int64_t data, sample::Enum type) override {} | 41 void SetInteger(int64_t data, sample::Enum type) override {} |
| 42 }; | 42 }; |
| 43 | 43 |
| 44 class RecordingErrorHandler : public ErrorHandler { | |
| 45 public: | |
| 46 RecordingErrorHandler() : error_(false) {} | |
| 47 ~RecordingErrorHandler() override {} | |
| 48 | |
| 49 bool encountered_error() const { return error_; } | |
| 50 | |
| 51 private: | |
| 52 // ErrorHandler implementation. | |
| 53 void OnConnectionError() override { error_ = true; } | |
| 54 | |
| 55 bool error_; | |
| 56 }; | |
| 57 | |
| 58 class BindingTest : public testing::Test { | 44 class BindingTest : public testing::Test { |
| 59 public: | 45 public: |
| 60 BindingTest() {} | 46 BindingTest() {} |
| 61 ~BindingTest() override {} | 47 ~BindingTest() override {} |
| 62 | 48 |
| 63 protected: | 49 protected: |
| 64 RecordingErrorHandler handler_; | |
| 65 Environment env_; | 50 Environment env_; |
| 66 RunLoop loop_; | 51 RunLoop loop_; |
| 67 }; | 52 }; |
| 68 | 53 |
| 69 // Tests that destroying a mojo::Binding closes the bound message pipe handle. | 54 // Tests that destroying a mojo::Binding closes the bound message pipe handle. |
| 70 TEST_F(BindingTest, DestroyClosesMessagePipe) { | 55 TEST_F(BindingTest, DestroyClosesMessagePipe) { |
| 56 bool encountered_error = false; |
| 71 ServiceImpl impl; | 57 ServiceImpl impl; |
| 72 sample::ServicePtr ptr; | 58 sample::ServicePtr ptr; |
| 73 auto request = GetProxy(&ptr); | 59 auto request = GetProxy(&ptr); |
| 74 ptr.set_error_handler(&handler_); | 60 ptr.set_connection_error_handler( |
| 61 [&encountered_error]() { encountered_error = true; }); |
| 75 bool called = false; | 62 bool called = false; |
| 76 auto called_cb = [&called](int32_t result) { called = true; }; | 63 auto called_cb = [&called](int32_t result) { called = true; }; |
| 77 { | 64 { |
| 78 Binding<sample::Service> binding(&impl, request.Pass()); | 65 Binding<sample::Service> binding(&impl, request.Pass()); |
| 79 ptr->Frobinate(nullptr, sample::Service::BAZ_OPTIONS_REGULAR, nullptr, | 66 ptr->Frobinate(nullptr, sample::Service::BAZ_OPTIONS_REGULAR, nullptr, |
| 80 called_cb); | 67 called_cb); |
| 81 loop_.RunUntilIdle(); | 68 loop_.RunUntilIdle(); |
| 82 EXPECT_TRUE(called); | 69 EXPECT_TRUE(called); |
| 83 EXPECT_FALSE(handler_.encountered_error()); | 70 EXPECT_FALSE(encountered_error); |
| 84 } | 71 } |
| 85 // Now that the Binding is out of scope we should detect an error on the other | 72 // Now that the Binding is out of scope we should detect an error on the other |
| 86 // end of the pipe. | 73 // end of the pipe. |
| 87 loop_.RunUntilIdle(); | 74 loop_.RunUntilIdle(); |
| 88 EXPECT_TRUE(handler_.encountered_error()); | 75 EXPECT_TRUE(encountered_error); |
| 89 | 76 |
| 90 // And calls should fail. | 77 // And calls should fail. |
| 91 called = false; | 78 called = false; |
| 92 ptr->Frobinate(nullptr, sample::Service::BAZ_OPTIONS_REGULAR, nullptr, | 79 ptr->Frobinate(nullptr, sample::Service::BAZ_OPTIONS_REGULAR, nullptr, |
| 93 called_cb); | 80 called_cb); |
| 94 loop_.RunUntilIdle(); | 81 loop_.RunUntilIdle(); |
| 95 EXPECT_FALSE(called); | 82 EXPECT_FALSE(called); |
| 96 } | 83 } |
| 97 | 84 |
| 98 // Tests that explicitly calling Unbind followed by rebinding works. | 85 // Tests that explicitly calling Unbind followed by rebinding works. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 129 | 116 |
| 130 TEST_F(BindingTest, SetInterfacePtrVersion) { | 117 TEST_F(BindingTest, SetInterfacePtrVersion) { |
| 131 IntegerAccessorImpl impl; | 118 IntegerAccessorImpl impl; |
| 132 sample::IntegerAccessorPtr ptr; | 119 sample::IntegerAccessorPtr ptr; |
| 133 Binding<sample::IntegerAccessor> binding(&impl, &ptr); | 120 Binding<sample::IntegerAccessor> binding(&impl, &ptr); |
| 134 EXPECT_EQ(3u, ptr.version()); | 121 EXPECT_EQ(3u, ptr.version()); |
| 135 } | 122 } |
| 136 | 123 |
| 137 } // namespace | 124 } // namespace |
| 138 } // mojo | 125 } // mojo |
| OLD | NEW |