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/allocation_scope.h" | 5 #include "mojo/public/cpp/bindings/allocation_scope.h" |
6 #include "mojo/public/cpp/environment/environment.h" | 6 #include "mojo/public/cpp/environment/environment.h" |
7 #include "mojo/public/cpp/test_support/test_utils.h" | 7 #include "mojo/public/cpp/test_support/test_utils.h" |
8 #include "mojo/public/cpp/utility/run_loop.h" | 8 #include "mojo/public/cpp/utility/run_loop.h" |
9 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h" | 9 #include "mojo/public/interfaces/bindings/tests/sample_factory.mojom.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 namespace mojo { | 12 namespace mojo { |
13 namespace test { | 13 namespace test { |
14 namespace { | 14 namespace { |
15 | 15 |
16 const char kText1[] = "hello"; | 16 const char kText1[] = "hello"; |
17 const char kText2[] = "world"; | 17 const char kText2[] = "world"; |
18 | 18 |
19 class SampleFactoryImpl : public InterfaceImpl<sample::Factory> { | 19 class SampleFactoryImpl : public InterfaceImpl<sample::Factory> { |
20 public: | 20 public: |
21 virtual void OnConnectionError() MOJO_OVERRIDE { | 21 virtual void OnConnectionError() MOJO_OVERRIDE { |
22 delete this; | 22 delete this; |
23 } | 23 } |
24 | 24 |
25 virtual void SetClient(sample::FactoryClient* client) MOJO_OVERRIDE { | |
26 client_ = client; | |
27 } | |
28 | |
29 virtual void DoStuff(const sample::Request& request, | 25 virtual void DoStuff(const sample::Request& request, |
30 ScopedMessagePipeHandle pipe) MOJO_OVERRIDE { | 26 ScopedMessagePipeHandle pipe) MOJO_OVERRIDE { |
31 std::string text1; | 27 std::string text1; |
32 if (pipe.is_valid()) | 28 if (pipe.is_valid()) |
33 EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1)); | 29 EXPECT_TRUE(ReadTextMessage(pipe.get(), &text1)); |
34 | 30 |
35 std::string text2; | 31 std::string text2; |
36 if (request.pipe().is_valid()) { | 32 if (request.pipe().is_valid()) { |
37 EXPECT_TRUE(ReadTextMessage(request.pipe().get(), &text2)); | 33 EXPECT_TRUE(ReadTextMessage(request.pipe().get(), &text2)); |
38 | 34 |
39 // Ensure that simply accessing request.pipe() does not close it. | 35 // Ensure that simply accessing request.pipe() does not close it. |
40 EXPECT_TRUE(request.pipe().is_valid()); | 36 EXPECT_TRUE(request.pipe().is_valid()); |
41 } | 37 } |
42 | 38 |
43 ScopedMessagePipeHandle pipe0; | 39 ScopedMessagePipeHandle pipe0; |
44 if (!text2.empty()) { | 40 if (!text2.empty()) { |
45 CreateMessagePipe(&pipe0, &pipe1_); | 41 CreateMessagePipe(&pipe0, &pipe1_); |
46 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2)); | 42 EXPECT_TRUE(WriteTextMessage(pipe1_.get(), text2)); |
47 } | 43 } |
48 | 44 |
49 AllocationScope scope; | 45 AllocationScope scope; |
50 sample::Response::Builder response; | 46 sample::Response::Builder response; |
51 response.set_x(2); | 47 response.set_x(2); |
52 response.set_pipe(pipe0.Pass()); | 48 response.set_pipe(pipe0.Pass()); |
53 client_->DidStuff(response.Finish(), text1); | 49 client()->DidStuff(response.Finish(), text1); |
54 } | 50 } |
55 | 51 |
56 virtual void DoStuff2(ScopedDataPipeConsumerHandle pipe) MOJO_OVERRIDE { | 52 virtual void DoStuff2(ScopedDataPipeConsumerHandle pipe) MOJO_OVERRIDE { |
57 // Read the data from the pipe, writing the response (as a string) to | 53 // Read the data from the pipe, writing the response (as a string) to |
58 // DidStuff2(). | 54 // DidStuff2(). |
59 ASSERT_TRUE(pipe.is_valid()); | 55 ASSERT_TRUE(pipe.is_valid()); |
60 uint32_t data_size = 0; | 56 uint32_t data_size = 0; |
61 ASSERT_EQ(MOJO_RESULT_OK, | 57 ASSERT_EQ(MOJO_RESULT_OK, |
62 ReadDataRaw(pipe.get(), NULL, &data_size, | 58 ReadDataRaw(pipe.get(), NULL, &data_size, |
63 MOJO_READ_DATA_FLAG_QUERY)); | 59 MOJO_READ_DATA_FLAG_QUERY)); |
64 ASSERT_NE(0, static_cast<int>(data_size)); | 60 ASSERT_NE(0, static_cast<int>(data_size)); |
65 char data[64]; | 61 char data[64]; |
66 ASSERT_LT(static_cast<int>(data_size), 64); | 62 ASSERT_LT(static_cast<int>(data_size), 64); |
67 ASSERT_EQ(MOJO_RESULT_OK, | 63 ASSERT_EQ(MOJO_RESULT_OK, |
68 ReadDataRaw(pipe.get(), data, &data_size, | 64 ReadDataRaw(pipe.get(), data, &data_size, |
69 MOJO_READ_DATA_FLAG_ALL_OR_NONE)); | 65 MOJO_READ_DATA_FLAG_ALL_OR_NONE)); |
70 | 66 |
71 AllocationScope scope; | 67 AllocationScope scope; |
72 client_->DidStuff2(String(std::string(data))); | 68 client()->DidStuff2(String(std::string(data))); |
73 } | 69 } |
74 | 70 |
75 private: | 71 private: |
76 sample::FactoryClient* client_; | |
77 ScopedMessagePipeHandle pipe1_; | 72 ScopedMessagePipeHandle pipe1_; |
78 }; | 73 }; |
79 | 74 |
80 class SampleFactoryClientImpl : public sample::FactoryClient { | 75 class SampleFactoryClientImpl : public sample::FactoryClient { |
81 public: | 76 public: |
82 SampleFactoryClientImpl() : got_response_(false) { | 77 SampleFactoryClientImpl() : got_response_(false) { |
83 } | 78 } |
84 | 79 |
85 void set_expected_text_reply(const std::string& expected_text_reply) { | 80 void set_expected_text_reply(const std::string& expected_text_reply) { |
86 expected_text_reply_ = expected_text_reply; | 81 expected_text_reply_ = expected_text_reply; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 } | 260 } |
266 | 261 |
267 // We expect the pipes to have been closed. | 262 // We expect the pipes to have been closed. |
268 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle0_value)); | 263 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle0_value)); |
269 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle1_value)); | 264 EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, MojoClose(handle1_value)); |
270 } | 265 } |
271 | 266 |
272 } // namespace | 267 } // namespace |
273 } // namespace test | 268 } // namespace test |
274 } // namespace mojo | 269 } // namespace mojo |
OLD | NEW |