| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "mojo/public/bindings/lib/remote_ptr.h" | |
| 6 #include "mojo/public/tests/simple_bindings_support.h" | |
| 7 #include "mojo/public/tests/test_support.h" | |
| 8 #include "mojom/sample_factory.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 | |
| 11 namespace mojo { | |
| 12 namespace test { | |
| 13 namespace { | |
| 14 | |
| 15 const char kText1[] = "hello"; | |
| 16 const char kText2[] = "world"; | |
| 17 | |
| 18 class SampleFactoryImpl : public sample::Factory { | |
| 19 public: | |
| 20 explicit SampleFactoryImpl(ScopedMessagePipeHandle pipe) | |
| 21 : client_(pipe.Pass(), this) { | |
| 22 } | |
| 23 | |
| 24 virtual void DoStuff(const sample::Request& request, | |
| 25 ScopedMessagePipeHandle pipe) MOJO_OVERRIDE { | |
| 26 std::string text1; | |
| 27 if (pipe.is_valid()) | |
| 28 EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1)); | |
| 29 | |
| 30 std::string text2; | |
| 31 if (request.pipe().is_valid()) { | |
| 32 EXPECT_TRUE(ReadTextMessage(request.pipe().get(), &text2)); | |
| 33 | |
| 34 // Ensure that simply accessing request.pipe() does not close it. | |
| 35 EXPECT_TRUE(request.pipe().is_valid()); | |
| 36 } | |
| 37 | |
| 38 ScopedMessagePipeHandle pipe0; | |
| 39 if (!text2.empty()) { | |
| 40 CreateMessagePipe(&pipe0, &pipe1_); | |
| 41 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2)); | |
| 42 } | |
| 43 | |
| 44 AllocationScope scope; | |
| 45 sample::Response::Builder response; | |
| 46 response.set_x(2); | |
| 47 response.set_pipe(pipe0.Pass()); | |
| 48 client_->DidStuff(response.Finish(), text1); | |
| 49 } | |
| 50 | |
| 51 private: | |
| 52 RemotePtr<sample::FactoryClient> client_; | |
| 53 ScopedMessagePipeHandle pipe1_; | |
| 54 }; | |
| 55 | |
| 56 class SampleFactoryClientImpl : public sample::FactoryClient { | |
| 57 public: | |
| 58 explicit SampleFactoryClientImpl(ScopedMessagePipeHandle pipe) | |
| 59 : factory_(pipe.Pass(), this), | |
| 60 got_response_(false) { | |
| 61 } | |
| 62 | |
| 63 void Start() { | |
| 64 expected_text_reply_ = kText1; | |
| 65 | |
| 66 ScopedMessagePipeHandle pipe0; | |
| 67 CreateMessagePipe(&pipe0, &pipe1_); | |
| 68 | |
| 69 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), kText1)); | |
| 70 | |
| 71 ScopedMessagePipeHandle pipe2; | |
| 72 CreateMessagePipe(&pipe2, &pipe3_); | |
| 73 | |
| 74 EXPECT_TRUE(WriteTextMessage(pipe3_.get(), kText2)); | |
| 75 | |
| 76 AllocationScope scope; | |
| 77 sample::Request::Builder request; | |
| 78 request.set_x(1); | |
| 79 request.set_pipe(pipe2.Pass()); | |
| 80 factory_->DoStuff(request.Finish(), pipe0.Pass()); | |
| 81 } | |
| 82 | |
| 83 void StartNoPipes() { | |
| 84 expected_text_reply_.clear(); | |
| 85 | |
| 86 AllocationScope scope; | |
| 87 sample::Request::Builder request; | |
| 88 request.set_x(1); | |
| 89 factory_->DoStuff(request.Finish(), ScopedMessagePipeHandle().Pass()); | |
| 90 } | |
| 91 | |
| 92 bool got_response() const { | |
| 93 return got_response_; | |
| 94 } | |
| 95 | |
| 96 virtual void DidStuff(const sample::Response& response, | |
| 97 const String& text_reply) MOJO_OVERRIDE { | |
| 98 EXPECT_EQ(expected_text_reply_, text_reply.To<std::string>()); | |
| 99 | |
| 100 if (response.pipe().is_valid()) { | |
| 101 std::string text2; | |
| 102 EXPECT_TRUE(ReadTextMessage(response.pipe().get(), &text2)); | |
| 103 | |
| 104 // Ensure that simply accessing response.pipe() does not close it. | |
| 105 EXPECT_TRUE(response.pipe().is_valid()); | |
| 106 | |
| 107 EXPECT_EQ(std::string(kText2), text2); | |
| 108 | |
| 109 // Do some more tests of handle passing: | |
| 110 ScopedMessagePipeHandle p = response.pipe().Pass(); | |
| 111 EXPECT_TRUE(p.is_valid()); | |
| 112 EXPECT_FALSE(response.pipe().is_valid()); | |
| 113 } | |
| 114 | |
| 115 got_response_ = true; | |
| 116 } | |
| 117 | |
| 118 private: | |
| 119 RemotePtr<sample::Factory> factory_; | |
| 120 ScopedMessagePipeHandle pipe1_; | |
| 121 ScopedMessagePipeHandle pipe3_; | |
| 122 std::string expected_text_reply_; | |
| 123 bool got_response_; | |
| 124 }; | |
| 125 | |
| 126 } // namespace | |
| 127 | |
| 128 class BindingsHandlePassingTest : public testing::Test { | |
| 129 public: | |
| 130 void PumpMessages() { | |
| 131 bindings_support_.Process(); | |
| 132 } | |
| 133 | |
| 134 private: | |
| 135 SimpleBindingsSupport bindings_support_; | |
| 136 }; | |
| 137 | |
| 138 TEST_F(BindingsHandlePassingTest, Basic) { | |
| 139 ScopedMessagePipeHandle pipe0; | |
| 140 ScopedMessagePipeHandle pipe1; | |
| 141 CreateMessagePipe(&pipe0, &pipe1); | |
| 142 | |
| 143 SampleFactoryImpl factory(pipe0.Pass()); | |
| 144 SampleFactoryClientImpl factory_client(pipe1.Pass()); | |
| 145 | |
| 146 factory_client.Start(); | |
| 147 | |
| 148 EXPECT_FALSE(factory_client.got_response()); | |
| 149 | |
| 150 PumpMessages(); | |
| 151 | |
| 152 EXPECT_TRUE(factory_client.got_response()); | |
| 153 } | |
| 154 | |
| 155 TEST_F(BindingsHandlePassingTest, PassInvalid) { | |
| 156 ScopedMessagePipeHandle pipe0; | |
| 157 ScopedMessagePipeHandle pipe1; | |
| 158 CreateMessagePipe(&pipe0, &pipe1); | |
| 159 | |
| 160 SampleFactoryImpl factory(pipe0.Pass()); | |
| 161 SampleFactoryClientImpl factory_client(pipe1.Pass()); | |
| 162 | |
| 163 factory_client.StartNoPipes(); | |
| 164 | |
| 165 EXPECT_FALSE(factory_client.got_response()); | |
| 166 | |
| 167 PumpMessages(); | |
| 168 | |
| 169 EXPECT_TRUE(factory_client.got_response()); | |
| 170 } | |
| 171 | |
| 172 } // namespace test | |
| 173 } // namespace mojo | |
| OLD | NEW |