| 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/environment/environment.h" | 5 #include "mojo/public/cpp/environment/environment.h" |
| 6 #include "mojo/public/cpp/test_support/test_utils.h" | 6 #include "mojo/public/cpp/test_support/test_utils.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_factory.mojom.h" | 8 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 void Run(const String& a) const { | 22 void Run(const String& a) const { |
| 23 *buf_ = a.To<std::string>(); | 23 *buf_ = a.To<std::string>(); |
| 24 } | 24 } |
| 25 private: | 25 private: |
| 26 std::string* buf_; | 26 std::string* buf_; |
| 27 }; | 27 }; |
| 28 | 28 |
| 29 class ImportedInterfaceImpl | 29 class ImportedInterfaceImpl |
| 30 : public InterfaceImpl<imported::ImportedInterface> { | 30 : public InterfaceImpl<imported::ImportedInterface> { |
| 31 public: | 31 public: |
| 32 virtual void DoSomething() MOJO_OVERRIDE { | 32 virtual void DoSomething() override { do_something_count_++; } |
| 33 do_something_count_++; | |
| 34 } | |
| 35 | 33 |
| 36 static int do_something_count() { return do_something_count_; } | 34 static int do_something_count() { return do_something_count_; } |
| 37 | 35 |
| 38 private: | 36 private: |
| 39 static int do_something_count_; | 37 static int do_something_count_; |
| 40 }; | 38 }; |
| 41 int ImportedInterfaceImpl::do_something_count_ = 0; | 39 int ImportedInterfaceImpl::do_something_count_ = 0; |
| 42 | 40 |
| 43 class SampleNamedObjectImpl : public InterfaceImpl<sample::NamedObject> { | 41 class SampleNamedObjectImpl : public InterfaceImpl<sample::NamedObject> { |
| 44 public: | 42 public: |
| 45 virtual void SetName(const mojo::String& name) MOJO_OVERRIDE { | 43 virtual void SetName(const mojo::String& name) override { name_ = name; } |
| 46 name_ = name; | |
| 47 } | |
| 48 | 44 |
| 49 virtual void GetName(const mojo::Callback<void(mojo::String)>& callback) | 45 virtual void GetName( |
| 50 MOJO_OVERRIDE { | 46 const mojo::Callback<void(mojo::String)>& callback) override { |
| 51 callback.Run(name_); | 47 callback.Run(name_); |
| 52 } | 48 } |
| 53 | 49 |
| 54 private: | 50 private: |
| 55 std::string name_; | 51 std::string name_; |
| 56 }; | 52 }; |
| 57 | 53 |
| 58 class SampleFactoryImpl : public InterfaceImpl<sample::Factory> { | 54 class SampleFactoryImpl : public InterfaceImpl<sample::Factory> { |
| 59 public: | 55 public: |
| 60 virtual void DoStuff(sample::RequestPtr request, | 56 virtual void DoStuff(sample::RequestPtr request, |
| 61 ScopedMessagePipeHandle pipe) MOJO_OVERRIDE { | 57 ScopedMessagePipeHandle pipe) override { |
| 62 std::string text1; | 58 std::string text1; |
| 63 if (pipe.is_valid()) | 59 if (pipe.is_valid()) |
| 64 EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1)); | 60 EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1)); |
| 65 | 61 |
| 66 std::string text2; | 62 std::string text2; |
| 67 if (request->pipe.is_valid()) { | 63 if (request->pipe.is_valid()) { |
| 68 EXPECT_TRUE(ReadTextMessage(request->pipe.get(), &text2)); | 64 EXPECT_TRUE(ReadTextMessage(request->pipe.get(), &text2)); |
| 69 | 65 |
| 70 // Ensure that simply accessing request->pipe does not close it. | 66 // Ensure that simply accessing request->pipe does not close it. |
| 71 EXPECT_TRUE(request->pipe.is_valid()); | 67 EXPECT_TRUE(request->pipe.is_valid()); |
| 72 } | 68 } |
| 73 | 69 |
| 74 ScopedMessagePipeHandle pipe0; | 70 ScopedMessagePipeHandle pipe0; |
| 75 if (!text2.empty()) { | 71 if (!text2.empty()) { |
| 76 CreateMessagePipe(NULL, &pipe0, &pipe1_); | 72 CreateMessagePipe(NULL, &pipe0, &pipe1_); |
| 77 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2)); | 73 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2)); |
| 78 } | 74 } |
| 79 | 75 |
| 80 sample::ResponsePtr response(sample::Response::New()); | 76 sample::ResponsePtr response(sample::Response::New()); |
| 81 response->x = 2; | 77 response->x = 2; |
| 82 response->pipe = pipe0.Pass(); | 78 response->pipe = pipe0.Pass(); |
| 83 client()->DidStuff(response.Pass(), text1); | 79 client()->DidStuff(response.Pass(), text1); |
| 84 | 80 |
| 85 if (request->obj) | 81 if (request->obj) |
| 86 request->obj->DoSomething(); | 82 request->obj->DoSomething(); |
| 87 } | 83 } |
| 88 | 84 |
| 89 virtual void DoStuff2(ScopedDataPipeConsumerHandle pipe) MOJO_OVERRIDE { | 85 virtual void DoStuff2(ScopedDataPipeConsumerHandle pipe) override { |
| 90 // Read the data from the pipe, writing the response (as a string) to | 86 // Read the data from the pipe, writing the response (as a string) to |
| 91 // DidStuff2(). | 87 // DidStuff2(). |
| 92 ASSERT_TRUE(pipe.is_valid()); | 88 ASSERT_TRUE(pipe.is_valid()); |
| 93 uint32_t data_size = 0; | 89 uint32_t data_size = 0; |
| 94 ASSERT_EQ(MOJO_RESULT_OK, | 90 ASSERT_EQ(MOJO_RESULT_OK, |
| 95 ReadDataRaw(pipe.get(), NULL, &data_size, | 91 ReadDataRaw(pipe.get(), NULL, &data_size, |
| 96 MOJO_READ_DATA_FLAG_QUERY)); | 92 MOJO_READ_DATA_FLAG_QUERY)); |
| 97 ASSERT_NE(0, static_cast<int>(data_size)); | 93 ASSERT_NE(0, static_cast<int>(data_size)); |
| 98 char data[64]; | 94 char data[64]; |
| 99 ASSERT_LT(static_cast<int>(data_size), 64); | 95 ASSERT_LT(static_cast<int>(data_size), 64); |
| 100 ASSERT_EQ(MOJO_RESULT_OK, | 96 ASSERT_EQ(MOJO_RESULT_OK, |
| 101 ReadDataRaw(pipe.get(), data, &data_size, | 97 ReadDataRaw(pipe.get(), data, &data_size, |
| 102 MOJO_READ_DATA_FLAG_ALL_OR_NONE)); | 98 MOJO_READ_DATA_FLAG_ALL_OR_NONE)); |
| 103 | 99 |
| 104 client()->DidStuff2(data); | 100 client()->DidStuff2(data); |
| 105 } | 101 } |
| 106 | 102 |
| 107 virtual void CreateNamedObject( | 103 virtual void CreateNamedObject( |
| 108 InterfaceRequest<sample::NamedObject> object_request) MOJO_OVERRIDE { | 104 InterfaceRequest<sample::NamedObject> object_request) override { |
| 109 EXPECT_TRUE(object_request.is_pending()); | 105 EXPECT_TRUE(object_request.is_pending()); |
| 110 BindToRequest(new SampleNamedObjectImpl(), &object_request); | 106 BindToRequest(new SampleNamedObjectImpl(), &object_request); |
| 111 } | 107 } |
| 112 | 108 |
| 113 // These aren't called or implemented, but exist here to test that the | 109 // These aren't called or implemented, but exist here to test that the |
| 114 // methods are generated with the correct argument types for imported | 110 // methods are generated with the correct argument types for imported |
| 115 // interfaces. | 111 // interfaces. |
| 116 virtual void RequestImportedInterface( | 112 virtual void RequestImportedInterface( |
| 117 InterfaceRequest<imported::ImportedInterface> imported, | 113 InterfaceRequest<imported::ImportedInterface> imported, |
| 118 const mojo::Callback<void(InterfaceRequest<imported::ImportedInterface>)>& | 114 const mojo::Callback<void(InterfaceRequest<imported::ImportedInterface>)>& |
| 119 callback) MOJO_OVERRIDE {} | 115 callback) override {} |
| 120 virtual void TakeImportedInterface( | 116 virtual void TakeImportedInterface( |
| 121 imported::ImportedInterfacePtr imported, | 117 imported::ImportedInterfacePtr imported, |
| 122 const mojo::Callback<void(imported::ImportedInterfacePtr)>& callback) | 118 const mojo::Callback<void(imported::ImportedInterfacePtr)>& callback) |
| 123 MOJO_OVERRIDE {} | 119 override {} |
| 124 | 120 |
| 125 private: | 121 private: |
| 126 ScopedMessagePipeHandle pipe1_; | 122 ScopedMessagePipeHandle pipe1_; |
| 127 }; | 123 }; |
| 128 | 124 |
| 129 class SampleFactoryClientImpl : public sample::FactoryClient { | 125 class SampleFactoryClientImpl : public sample::FactoryClient { |
| 130 public: | 126 public: |
| 131 SampleFactoryClientImpl() : got_response_(false) { | 127 SampleFactoryClientImpl() : got_response_(false) { |
| 132 } | 128 } |
| 133 | 129 |
| 134 void set_expected_text_reply(const std::string& expected_text_reply) { | 130 void set_expected_text_reply(const std::string& expected_text_reply) { |
| 135 expected_text_reply_ = expected_text_reply; | 131 expected_text_reply_ = expected_text_reply; |
| 136 } | 132 } |
| 137 | 133 |
| 138 bool got_response() const { | 134 bool got_response() const { |
| 139 return got_response_; | 135 return got_response_; |
| 140 } | 136 } |
| 141 | 137 |
| 142 virtual void DidStuff(sample::ResponsePtr response, | 138 virtual void DidStuff(sample::ResponsePtr response, |
| 143 const String& text_reply) MOJO_OVERRIDE { | 139 const String& text_reply) override { |
| 144 EXPECT_EQ(expected_text_reply_, text_reply); | 140 EXPECT_EQ(expected_text_reply_, text_reply); |
| 145 | 141 |
| 146 if (response->pipe.is_valid()) { | 142 if (response->pipe.is_valid()) { |
| 147 std::string text2; | 143 std::string text2; |
| 148 EXPECT_TRUE(ReadTextMessage(response->pipe.get(), &text2)); | 144 EXPECT_TRUE(ReadTextMessage(response->pipe.get(), &text2)); |
| 149 | 145 |
| 150 // Ensure that simply accessing response.pipe does not close it. | 146 // Ensure that simply accessing response.pipe does not close it. |
| 151 EXPECT_TRUE(response->pipe.is_valid()); | 147 EXPECT_TRUE(response->pipe.is_valid()); |
| 152 | 148 |
| 153 EXPECT_EQ(std::string(kText2), text2); | 149 EXPECT_EQ(std::string(kText2), text2); |
| 154 | 150 |
| 155 // Do some more tests of handle passing: | 151 // Do some more tests of handle passing: |
| 156 ScopedMessagePipeHandle p = response->pipe.Pass(); | 152 ScopedMessagePipeHandle p = response->pipe.Pass(); |
| 157 EXPECT_TRUE(p.is_valid()); | 153 EXPECT_TRUE(p.is_valid()); |
| 158 EXPECT_FALSE(response->pipe.is_valid()); | 154 EXPECT_FALSE(response->pipe.is_valid()); |
| 159 } | 155 } |
| 160 | 156 |
| 161 got_response_ = true; | 157 got_response_ = true; |
| 162 } | 158 } |
| 163 | 159 |
| 164 virtual void DidStuff2(const String& text_reply) MOJO_OVERRIDE { | 160 virtual void DidStuff2(const String& text_reply) override { |
| 165 got_response_ = true; | 161 got_response_ = true; |
| 166 EXPECT_EQ(expected_text_reply_, text_reply); | 162 EXPECT_EQ(expected_text_reply_, text_reply); |
| 167 } | 163 } |
| 168 | 164 |
| 169 private: | 165 private: |
| 170 ScopedMessagePipeHandle pipe1_; | 166 ScopedMessagePipeHandle pipe1_; |
| 171 ScopedMessagePipeHandle pipe3_; | 167 ScopedMessagePipeHandle pipe3_; |
| 172 std::string expected_text_reply_; | 168 std::string expected_text_reply_; |
| 173 bool got_response_; | 169 bool got_response_; |
| 174 }; | 170 }; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 345 | 341 |
| 346 PumpMessages(); // Yield for results. | 342 PumpMessages(); // Yield for results. |
| 347 | 343 |
| 348 EXPECT_EQ(std::string("object1"), name1); | 344 EXPECT_EQ(std::string("object1"), name1); |
| 349 EXPECT_EQ(std::string("object2"), name2); | 345 EXPECT_EQ(std::string("object2"), name2); |
| 350 } | 346 } |
| 351 | 347 |
| 352 } // namespace | 348 } // namespace |
| 353 } // namespace test | 349 } // namespace test |
| 354 } // namespace mojo | 350 } // namespace mojo |
| OLD | NEW |