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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "gin/public/isolate_holder.h" | 10 #include "gin/public/isolate_holder.h" |
11 #include "mojo/apps/js/mojo_runner_delegate.h" | 11 #include "mojo/apps/js/mojo_runner_delegate.h" |
12 #include "mojo/apps/js/test/js_to_cpp.mojom.h" | 12 #include "mojo/apps/js/test/js_to_cpp.mojom.h" |
13 #include "mojo/common/common_type_converters.h" | 13 #include "mojo/common/common_type_converters.h" |
14 #include "mojo/common/test/test_utils.h" | 14 #include "mojo/common/test/test_utils.h" |
15 #include "mojo/public/cpp/bindings/allocation_scope.h" | 15 #include "mojo/public/cpp/bindings/allocation_scope.h" |
16 #include "mojo/public/cpp/bindings/remote_ptr.h" | |
17 #include "mojo/public/cpp/environment/environment.h" | 16 #include "mojo/public/cpp/environment/environment.h" |
18 #include "mojo/public/cpp/system/core.h" | 17 #include "mojo/public/cpp/system/core.h" |
19 #include "mojo/public/cpp/system/macros.h" | 18 #include "mojo/public/cpp/system/macros.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
21 | 20 |
22 namespace mojo { | 21 namespace mojo { |
23 namespace js { | 22 namespace js { |
24 namespace { | 23 namespace { |
25 | 24 |
26 // Negative numbers with different values in each byte, the last of | 25 // Negative numbers with different values in each byte, the last of |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 EXPECT_EQ(std::string("one"), arg.string_array()[0].To<std::string>()); | 106 EXPECT_EQ(std::string("one"), arg.string_array()[0].To<std::string>()); |
108 EXPECT_EQ(std::string("two"), arg.string_array()[1].To<std::string>()); | 107 EXPECT_EQ(std::string("two"), arg.string_array()[1].To<std::string>()); |
109 EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>()); | 108 EXPECT_EQ(std::string("three"), arg.string_array()[2].To<std::string>()); |
110 } | 109 } |
111 | 110 |
112 // Base Provider implementation class. It's expected that tests subclass and | 111 // Base Provider implementation class. It's expected that tests subclass and |
113 // override the appropriate Provider functions. When test is done quit the | 112 // override the appropriate Provider functions. When test is done quit the |
114 // run_loop(). | 113 // run_loop(). |
115 class CppSideConnection : public js_to_cpp::CppSide { | 114 class CppSideConnection : public js_to_cpp::CppSide { |
116 public: | 115 public: |
117 CppSideConnection() : run_loop_(NULL), client_(NULL) { | 116 CppSideConnection() : run_loop_(NULL), js_side_(NULL) { |
118 } | 117 } |
119 virtual ~CppSideConnection() {} | 118 virtual ~CppSideConnection() {} |
120 | 119 |
121 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | 120 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
122 base::RunLoop* run_loop() { return run_loop_; } | 121 base::RunLoop* run_loop() { return run_loop_; } |
123 | 122 |
124 void set_client(js_to_cpp::JsSide* client) { client_ = client; } | 123 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; } |
125 js_to_cpp::JsSide* client() { return client_; } | 124 js_to_cpp::JsSide* js_side() { return js_side_; } |
126 | 125 |
127 // js_to_cpp::CppSide: | 126 // js_to_cpp::CppSide: |
128 virtual void StartTest() OVERRIDE { | 127 virtual void StartTest() OVERRIDE { |
129 NOTREACHED(); | 128 NOTREACHED(); |
130 } | 129 } |
131 | 130 |
132 virtual void TestFinished() OVERRIDE { | 131 virtual void TestFinished() OVERRIDE { |
133 NOTREACHED(); | 132 NOTREACHED(); |
134 } | 133 } |
135 | 134 |
136 virtual void PingResponse() OVERRIDE { | 135 virtual void PingResponse() OVERRIDE { |
137 NOTREACHED(); | 136 NOTREACHED(); |
138 } | 137 } |
139 | 138 |
140 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, | 139 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, |
141 const js_to_cpp::EchoArgs& arg2) OVERRIDE { | 140 const js_to_cpp::EchoArgs& arg2) OVERRIDE { |
142 NOTREACHED(); | 141 NOTREACHED(); |
143 } | 142 } |
144 | 143 |
145 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE { | 144 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE { |
146 NOTREACHED(); | 145 NOTREACHED(); |
147 } | 146 } |
148 | 147 |
149 protected: | 148 protected: |
150 base::RunLoop* run_loop_; | 149 base::RunLoop* run_loop_; |
151 js_to_cpp::JsSide* client_; | 150 js_to_cpp::JsSide* js_side_; |
152 | 151 |
153 private: | 152 private: |
154 Environment environment; | 153 Environment environment; |
155 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); | 154 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); |
156 }; | 155 }; |
157 | 156 |
158 // Trivial test to verify a message sent from JS is received. | 157 // Trivial test to verify a message sent from JS is received. |
159 class PingCppSideConnection : public CppSideConnection { | 158 class PingCppSideConnection : public CppSideConnection { |
160 public: | 159 public: |
161 explicit PingCppSideConnection() : got_message_(false) {} | 160 explicit PingCppSideConnection() : got_message_(false) {} |
162 virtual ~PingCppSideConnection() {} | 161 virtual ~PingCppSideConnection() {} |
163 | 162 |
164 // js_to_cpp::CppSide: | 163 // js_to_cpp::CppSide: |
165 virtual void StartTest() OVERRIDE { | 164 virtual void StartTest() OVERRIDE { |
166 client_->Ping(); | 165 js_side_->Ping(); |
167 } | 166 } |
168 | 167 |
169 virtual void PingResponse() OVERRIDE { | 168 virtual void PingResponse() OVERRIDE { |
170 got_message_ = true; | 169 got_message_ = true; |
171 run_loop()->Quit(); | 170 run_loop()->Quit(); |
172 } | 171 } |
173 | 172 |
174 bool DidSucceed() { | 173 bool DidSucceed() { |
175 return got_message_; | 174 return got_message_; |
176 } | 175 } |
177 | 176 |
178 private: | 177 private: |
179 bool got_message_; | 178 bool got_message_; |
180 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); | 179 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); |
181 }; | 180 }; |
182 | 181 |
183 // Test that parameters are passed with correct values. | 182 // Test that parameters are passed with correct values. |
184 class EchoCppSideConnection : public CppSideConnection { | 183 class EchoCppSideConnection : public CppSideConnection { |
185 public: | 184 public: |
186 explicit EchoCppSideConnection() : | 185 explicit EchoCppSideConnection() : |
187 message_count_(0), | 186 message_count_(0), |
188 termination_seen_(false) { | 187 termination_seen_(false) { |
189 } | 188 } |
190 virtual ~EchoCppSideConnection() {} | 189 virtual ~EchoCppSideConnection() {} |
191 | 190 |
192 // js_to_cpp::CppSide: | 191 // js_to_cpp::CppSide: |
193 virtual void StartTest() OVERRIDE { | 192 virtual void StartTest() OVERRIDE { |
194 AllocationScope scope; | 193 AllocationScope scope; |
195 client_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); | 194 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); |
196 } | 195 } |
197 | 196 |
198 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, | 197 virtual void EchoResponse(const js_to_cpp::EchoArgs& arg1, |
199 const js_to_cpp::EchoArgs& arg2) OVERRIDE { | 198 const js_to_cpp::EchoArgs& arg2) OVERRIDE { |
200 message_count_ += 1; | 199 message_count_ += 1; |
201 CheckSampleEchoArgs(arg1); | 200 CheckSampleEchoArgs(arg1); |
202 EXPECT_EQ(-1, arg2.si64()); | 201 EXPECT_EQ(-1, arg2.si64()); |
203 EXPECT_EQ(-1, arg2.si32()); | 202 EXPECT_EQ(-1, arg2.si32()); |
204 EXPECT_EQ(-1, arg2.si16()); | 203 EXPECT_EQ(-1, arg2.si16()); |
205 EXPECT_EQ(-1, arg2.si8()); | 204 EXPECT_EQ(-1, arg2.si8()); |
(...skipping 18 matching lines...) Expand all Loading... |
224 | 223 |
225 // Test that corrupted messages don't wreak havoc. | 224 // Test that corrupted messages don't wreak havoc. |
226 class BitFlipCppSideConnection : public CppSideConnection { | 225 class BitFlipCppSideConnection : public CppSideConnection { |
227 public: | 226 public: |
228 explicit BitFlipCppSideConnection() : termination_seen_(false) {} | 227 explicit BitFlipCppSideConnection() : termination_seen_(false) {} |
229 virtual ~BitFlipCppSideConnection() {} | 228 virtual ~BitFlipCppSideConnection() {} |
230 | 229 |
231 // js_to_cpp::CppSide: | 230 // js_to_cpp::CppSide: |
232 virtual void StartTest() OVERRIDE { | 231 virtual void StartTest() OVERRIDE { |
233 AllocationScope scope; | 232 AllocationScope scope; |
234 client_->BitFlip(BuildSampleEchoArgs()); | 233 js_side_->BitFlip(BuildSampleEchoArgs()); |
235 } | 234 } |
236 | 235 |
237 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE { | 236 virtual void BitFlipResponse(const js_to_cpp::EchoArgs& arg1) OVERRIDE { |
238 // TODO(tsepez): How to check, may be corrupt in various ways. | 237 // TODO(tsepez): How to check, may be corrupt in various ways. |
239 } | 238 } |
240 | 239 |
241 virtual void TestFinished() OVERRIDE { | 240 virtual void TestFinished() OVERRIDE { |
242 termination_seen_ = true; | 241 termination_seen_ = true; |
243 run_loop()->Quit(); | 242 run_loop()->Quit(); |
244 } | 243 } |
245 | 244 |
246 bool DidSucceed() { | 245 bool DidSucceed() { |
247 return termination_seen_; | 246 return termination_seen_; |
248 } | 247 } |
249 | 248 |
250 private: | 249 private: |
251 bool termination_seen_; | 250 bool termination_seen_; |
252 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection); | 251 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection); |
253 }; | 252 }; |
254 | 253 |
255 } // namespace | 254 } // namespace |
256 | 255 |
257 class JsToCppTest : public testing::Test { | 256 class JsToCppTest : public testing::Test { |
258 public: | 257 public: |
259 JsToCppTest() {} | 258 JsToCppTest() {} |
260 | 259 |
261 void RunTest(const std::string& test, CppSideConnection* cpp_side) { | 260 void RunTest(const std::string& test, CppSideConnection* cpp_side) { |
262 cpp_side->set_run_loop(&run_loop_); | 261 cpp_side->set_run_loop(&run_loop_); |
263 InterfacePipe<js_to_cpp::CppSide, js_to_cpp::JsSide> pipe; | 262 |
264 RemotePtr<js_to_cpp::JsSide> js_side; | 263 MessagePipe pipe; |
265 js_side.reset(pipe.handle_to_peer.Pass(), cpp_side); | 264 js_to_cpp::JsSidePtr js_side = |
266 js_side.router_for_testing()-> | 265 MakeProxy<js_to_cpp::JsSide>(pipe.handle0.Pass()); |
| 266 js_side->SetClient(cpp_side); |
| 267 |
| 268 js_side.internal_state()->router()-> |
267 set_enforce_errors_from_incoming_receiver(false); | 269 set_enforce_errors_from_incoming_receiver(false); |
268 cpp_side->set_client(js_side.get()); | 270 |
| 271 cpp_side->set_js_side(js_side.get()); |
269 | 272 |
270 gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); | 273 gin::IsolateHolder instance(gin::IsolateHolder::kStrictMode); |
271 apps::MojoRunnerDelegate delegate; | 274 apps::MojoRunnerDelegate delegate; |
272 gin::ShellRunner runner(&delegate, instance.isolate()); | 275 gin::ShellRunner runner(&delegate, instance.isolate()); |
273 delegate.Start(&runner, pipe.handle_to_self.release().value(), | 276 delegate.Start(&runner, pipe.handle1.release().value(), test); |
274 test); | |
275 | 277 |
276 run_loop_.Run(); | 278 run_loop_.Run(); |
277 } | 279 } |
278 | 280 |
279 private: | 281 private: |
280 base::MessageLoop loop; | 282 base::MessageLoop loop; |
281 base::RunLoop run_loop_; | 283 base::RunLoop run_loop_; |
282 | 284 |
283 DISALLOW_COPY_AND_ASSIGN(JsToCppTest); | 285 DISALLOW_COPY_AND_ASSIGN(JsToCppTest); |
284 }; | 286 }; |
(...skipping 21 matching lines...) Expand all Loading... |
306 if (IsRunningOnIsolatedBot()) | 308 if (IsRunningOnIsolatedBot()) |
307 return; | 309 return; |
308 | 310 |
309 BitFlipCppSideConnection cpp_side_connection; | 311 BitFlipCppSideConnection cpp_side_connection; |
310 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); | 312 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); |
311 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 313 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
312 } | 314 } |
313 | 315 |
314 } // namespace js | 316 } // namespace js |
315 } // namespace mojo | 317 } // namespace mojo |
OLD | NEW |