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