OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <stdint.h> | 5 #include <stdint.h> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
10 #include "mojo/public/cpp/bindings/binding.h" | 10 #include "mojo/public/cpp/bindings/binding.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 callback.Run(a); | 47 callback.Run(a); |
48 } | 48 } |
49 | 49 |
50 void EchoInt(int32_t a, const EchoIntCallback& callback) override { | 50 void EchoInt(int32_t a, const EchoIntCallback& callback) override { |
51 callback.Run(a); | 51 callback.Run(a); |
52 } | 52 } |
53 | 53 |
54 Binding<sample::Provider> binding_; | 54 Binding<sample::Provider> binding_; |
55 }; | 55 }; |
56 | 56 |
57 class StringRecorder { | 57 void RecordString(std::string* storage, |
58 public: | 58 const base::Closure& closure, |
59 StringRecorder(std::string* buf, const base::Closure& closure) | 59 String str) { |
60 : buf_(buf), closure_(closure) {} | 60 *storage = str; |
61 void Run(const String& a) const { | 61 closure.Run(); |
62 *buf_ = a; | 62 } |
63 closure_.Run(); | |
64 } | |
65 void Run(const String& a, const String& b) const { | |
66 *buf_ = a.get() + b.get(); | |
67 closure_.Run(); | |
68 } | |
69 | 63 |
70 private: | 64 void RecordStrings(std::string* storage, |
71 std::string* buf_; | 65 const base::Closure& closure, |
72 base::Closure closure_; | 66 String a, |
73 }; | 67 String b) { |
| 68 *storage = a.get() + b.get(); |
| 69 closure.Run(); |
| 70 } |
74 | 71 |
75 class EnumRecorder { | 72 void WriteToMessagePipe(const char* text, |
76 public: | 73 const base::Closure& closure, |
77 explicit EnumRecorder(sample::Enum* value, const base::Closure& closure) | 74 ScopedMessagePipeHandle handle) { |
78 : value_(value), closure_(closure) {} | 75 WriteTextMessage(handle.get(), text); |
79 void Run(sample::Enum a) const { | 76 closure.Run(); |
80 *value_ = a; | 77 } |
81 closure_.Run(); | |
82 } | |
83 | 78 |
84 private: | 79 void RecordEnum(sample::Enum* storage, |
85 sample::Enum* value_; | 80 const base::Closure& closure, |
86 base::Closure closure_; | 81 sample::Enum value) { |
87 }; | 82 *storage = value; |
88 | 83 closure.Run(); |
89 class MessagePipeWriter { | 84 } |
90 public: | |
91 MessagePipeWriter(const char* text, const base::Closure& closure) | |
92 : text_(text), closure_(closure) {} | |
93 void Run(ScopedMessagePipeHandle handle) const { | |
94 WriteTextMessage(handle.get(), text_); | |
95 closure_.Run(); | |
96 } | |
97 | |
98 private: | |
99 std::string text_; | |
100 base::Closure closure_; | |
101 }; | |
102 | 85 |
103 class RequestResponseTest : public testing::Test { | 86 class RequestResponseTest : public testing::Test { |
104 public: | 87 public: |
105 RequestResponseTest() {} | 88 RequestResponseTest() {} |
106 ~RequestResponseTest() override { loop_.RunUntilIdle(); } | 89 ~RequestResponseTest() override { loop_.RunUntilIdle(); } |
107 | 90 |
108 void PumpMessages() { loop_.RunUntilIdle(); } | 91 void PumpMessages() { loop_.RunUntilIdle(); } |
109 | 92 |
110 private: | 93 private: |
111 base::MessageLoop loop_; | 94 base::MessageLoop loop_; |
112 }; | 95 }; |
113 | 96 |
114 TEST_F(RequestResponseTest, EchoString) { | 97 TEST_F(RequestResponseTest, EchoString) { |
115 sample::ProviderPtr provider; | 98 sample::ProviderPtr provider; |
116 ProviderImpl provider_impl(GetProxy(&provider)); | 99 ProviderImpl provider_impl(GetProxy(&provider)); |
117 | 100 |
118 std::string buf; | 101 std::string buf; |
119 base::RunLoop run_loop; | 102 base::RunLoop run_loop; |
120 provider->EchoString(String::From("hello"), | 103 provider->EchoString(String::From("hello"), |
121 StringRecorder(&buf, run_loop.QuitClosure())); | 104 base::Bind(&RecordString, &buf, run_loop.QuitClosure())); |
122 | 105 |
123 run_loop.Run(); | 106 run_loop.Run(); |
124 | 107 |
125 EXPECT_EQ(std::string("hello"), buf); | 108 EXPECT_EQ(std::string("hello"), buf); |
126 } | 109 } |
127 | 110 |
128 TEST_F(RequestResponseTest, EchoStrings) { | 111 TEST_F(RequestResponseTest, EchoStrings) { |
129 sample::ProviderPtr provider; | 112 sample::ProviderPtr provider; |
130 ProviderImpl provider_impl(GetProxy(&provider)); | 113 ProviderImpl provider_impl(GetProxy(&provider)); |
131 | 114 |
132 std::string buf; | 115 std::string buf; |
133 base::RunLoop run_loop; | 116 base::RunLoop run_loop; |
134 provider->EchoStrings( | 117 provider->EchoStrings( |
135 String::From("hello"), String::From(" world"), | 118 String::From("hello"), String::From(" world"), |
136 StringRecorder(&buf, run_loop.QuitClosure())); | 119 base::Bind(&RecordStrings, &buf, run_loop.QuitClosure())); |
137 | 120 |
138 run_loop.Run(); | 121 run_loop.Run(); |
139 | 122 |
140 EXPECT_EQ(std::string("hello world"), buf); | 123 EXPECT_EQ(std::string("hello world"), buf); |
141 } | 124 } |
142 | 125 |
143 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { | 126 TEST_F(RequestResponseTest, EchoMessagePipeHandle) { |
144 sample::ProviderPtr provider; | 127 sample::ProviderPtr provider; |
145 ProviderImpl provider_impl(GetProxy(&provider)); | 128 ProviderImpl provider_impl(GetProxy(&provider)); |
146 | 129 |
147 MessagePipe pipe2; | 130 MessagePipe pipe2; |
148 base::RunLoop run_loop; | 131 base::RunLoop run_loop; |
149 provider->EchoMessagePipeHandle( | 132 provider->EchoMessagePipeHandle( |
150 std::move(pipe2.handle1), | 133 std::move(pipe2.handle1), |
151 MessagePipeWriter("hello", run_loop.QuitClosure())); | 134 base::Bind(&WriteToMessagePipe, "hello", run_loop.QuitClosure())); |
152 | 135 |
153 run_loop.Run(); | 136 run_loop.Run(); |
154 | 137 |
155 std::string value; | 138 std::string value; |
156 ReadTextMessage(pipe2.handle0.get(), &value); | 139 ReadTextMessage(pipe2.handle0.get(), &value); |
157 | 140 |
158 EXPECT_EQ(std::string("hello"), value); | 141 EXPECT_EQ(std::string("hello"), value); |
159 } | 142 } |
160 | 143 |
161 TEST_F(RequestResponseTest, EchoEnum) { | 144 TEST_F(RequestResponseTest, EchoEnum) { |
162 sample::ProviderPtr provider; | 145 sample::ProviderPtr provider; |
163 ProviderImpl provider_impl(GetProxy(&provider)); | 146 ProviderImpl provider_impl(GetProxy(&provider)); |
164 | 147 |
165 sample::Enum value; | 148 sample::Enum value; |
166 base::RunLoop run_loop; | 149 base::RunLoop run_loop; |
167 provider->EchoEnum(sample::Enum::VALUE, | 150 provider->EchoEnum(sample::Enum::VALUE, |
168 EnumRecorder(&value, run_loop.QuitClosure())); | 151 base::Bind(&RecordEnum, &value, run_loop.QuitClosure())); |
169 | |
170 run_loop.Run(); | 152 run_loop.Run(); |
171 | 153 |
172 EXPECT_EQ(sample::Enum::VALUE, value); | 154 EXPECT_EQ(sample::Enum::VALUE, value); |
173 } | 155 } |
174 | 156 |
175 } // namespace | 157 } // namespace |
176 } // namespace test | 158 } // namespace test |
177 } // namespace mojo | 159 } // namespace mojo |
OLD | NEW |