| 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/at_exit.h" | 5 #include "base/at_exit.h" |
| 6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/macros.h" |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 11 #include "gin/array_buffer.h" | 12 #include "gin/array_buffer.h" |
| 12 #include "gin/public/isolate_holder.h" | 13 #include "gin/public/isolate_holder.h" |
| 13 #include "mojo/apps/js/mojo_runner_delegate.h" | 14 #include "mojo/apps/js/mojo_runner_delegate.h" |
| 14 #include "mojo/apps/js/test/js_to_cpp.mojom.h" | 15 #include "mojo/apps/js/test/js_to_cpp.mojom.h" |
| 15 #include "mojo/common/common_type_converters.h" | 16 #include "mojo/common/common_type_converters.h" |
| 16 #include "mojo/common/test/test_utils.h" | 17 #include "mojo/common/test/test_utils.h" |
| 17 #include "mojo/public/cpp/system/core.h" | 18 #include "mojo/public/cpp/system/core.h" |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 } | 214 } |
| 214 virtual ~CppSideConnection() {} | 215 virtual ~CppSideConnection() {} |
| 215 | 216 |
| 216 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } | 217 void set_run_loop(base::RunLoop* run_loop) { run_loop_ = run_loop; } |
| 217 base::RunLoop* run_loop() { return run_loop_; } | 218 base::RunLoop* run_loop() { return run_loop_; } |
| 218 | 219 |
| 219 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; } | 220 void set_js_side(js_to_cpp::JsSide* js_side) { js_side_ = js_side; } |
| 220 js_to_cpp::JsSide* js_side() { return js_side_; } | 221 js_to_cpp::JsSide* js_side() { return js_side_; } |
| 221 | 222 |
| 222 // js_to_cpp::CppSide: | 223 // js_to_cpp::CppSide: |
| 223 virtual void StartTest() OVERRIDE { | 224 virtual void StartTest() override { |
| 224 NOTREACHED(); | 225 NOTREACHED(); |
| 225 } | 226 } |
| 226 | 227 |
| 227 virtual void TestFinished() OVERRIDE { | 228 virtual void TestFinished() override { |
| 228 NOTREACHED(); | 229 NOTREACHED(); |
| 229 } | 230 } |
| 230 | 231 |
| 231 virtual void PingResponse() OVERRIDE { | 232 virtual void PingResponse() override { |
| 232 mishandled_messages_ += 1; | 233 mishandled_messages_ += 1; |
| 233 } | 234 } |
| 234 | 235 |
| 235 virtual void EchoResponse(js_to_cpp::EchoArgsListPtr list) OVERRIDE { | 236 virtual void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { |
| 236 mishandled_messages_ += 1; | 237 mishandled_messages_ += 1; |
| 237 } | 238 } |
| 238 | 239 |
| 239 virtual void BitFlipResponse(js_to_cpp::EchoArgsListPtr list) OVERRIDE { | 240 virtual void BitFlipResponse(js_to_cpp::EchoArgsListPtr list) override { |
| 240 mishandled_messages_ += 1; | 241 mishandled_messages_ += 1; |
| 241 } | 242 } |
| 242 | 243 |
| 243 virtual void BackPointerResponse( | 244 virtual void BackPointerResponse( |
| 244 js_to_cpp::EchoArgsListPtr list) OVERRIDE { | 245 js_to_cpp::EchoArgsListPtr list) override { |
| 245 mishandled_messages_ += 1; | 246 mishandled_messages_ += 1; |
| 246 } | 247 } |
| 247 | 248 |
| 248 protected: | 249 protected: |
| 249 base::RunLoop* run_loop_; | 250 base::RunLoop* run_loop_; |
| 250 js_to_cpp::JsSide* js_side_; | 251 js_to_cpp::JsSide* js_side_; |
| 251 int mishandled_messages_; | 252 int mishandled_messages_; |
| 252 | 253 |
| 253 private: | 254 private: |
| 254 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); | 255 DISALLOW_COPY_AND_ASSIGN(CppSideConnection); |
| 255 }; | 256 }; |
| 256 | 257 |
| 257 // Trivial test to verify a message sent from JS is received. | 258 // Trivial test to verify a message sent from JS is received. |
| 258 class PingCppSideConnection : public CppSideConnection { | 259 class PingCppSideConnection : public CppSideConnection { |
| 259 public: | 260 public: |
| 260 PingCppSideConnection() : got_message_(false) {} | 261 PingCppSideConnection() : got_message_(false) {} |
| 261 virtual ~PingCppSideConnection() {} | 262 virtual ~PingCppSideConnection() {} |
| 262 | 263 |
| 263 // js_to_cpp::CppSide: | 264 // js_to_cpp::CppSide: |
| 264 virtual void StartTest() OVERRIDE { | 265 virtual void StartTest() override { |
| 265 js_side_->Ping(); | 266 js_side_->Ping(); |
| 266 } | 267 } |
| 267 | 268 |
| 268 virtual void PingResponse() OVERRIDE { | 269 virtual void PingResponse() override { |
| 269 got_message_ = true; | 270 got_message_ = true; |
| 270 run_loop()->Quit(); | 271 run_loop()->Quit(); |
| 271 } | 272 } |
| 272 | 273 |
| 273 bool DidSucceed() { | 274 bool DidSucceed() { |
| 274 return got_message_ && !mishandled_messages_; | 275 return got_message_ && !mishandled_messages_; |
| 275 } | 276 } |
| 276 | 277 |
| 277 private: | 278 private: |
| 278 bool got_message_; | 279 bool got_message_; |
| 279 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); | 280 DISALLOW_COPY_AND_ASSIGN(PingCppSideConnection); |
| 280 }; | 281 }; |
| 281 | 282 |
| 282 // Test that parameters are passed with correct values. | 283 // Test that parameters are passed with correct values. |
| 283 class EchoCppSideConnection : public CppSideConnection { | 284 class EchoCppSideConnection : public CppSideConnection { |
| 284 public: | 285 public: |
| 285 EchoCppSideConnection() : | 286 EchoCppSideConnection() : |
| 286 message_count_(0), | 287 message_count_(0), |
| 287 termination_seen_(false) { | 288 termination_seen_(false) { |
| 288 } | 289 } |
| 289 virtual ~EchoCppSideConnection() {} | 290 virtual ~EchoCppSideConnection() {} |
| 290 | 291 |
| 291 // js_to_cpp::CppSide: | 292 // js_to_cpp::CppSide: |
| 292 virtual void StartTest() OVERRIDE { | 293 virtual void StartTest() override { |
| 293 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); | 294 js_side_->Echo(kExpectedMessageCount, BuildSampleEchoArgs()); |
| 294 } | 295 } |
| 295 | 296 |
| 296 virtual void EchoResponse(js_to_cpp::EchoArgsListPtr list) OVERRIDE { | 297 virtual void EchoResponse(js_to_cpp::EchoArgsListPtr list) override { |
| 297 const js_to_cpp::EchoArgsPtr& special_arg = list->item; | 298 const js_to_cpp::EchoArgsPtr& special_arg = list->item; |
| 298 message_count_ += 1; | 299 message_count_ += 1; |
| 299 EXPECT_EQ(-1, special_arg->si64); | 300 EXPECT_EQ(-1, special_arg->si64); |
| 300 EXPECT_EQ(-1, special_arg->si32); | 301 EXPECT_EQ(-1, special_arg->si32); |
| 301 EXPECT_EQ(-1, special_arg->si16); | 302 EXPECT_EQ(-1, special_arg->si16); |
| 302 EXPECT_EQ(-1, special_arg->si8); | 303 EXPECT_EQ(-1, special_arg->si8); |
| 303 EXPECT_EQ(std::string("going"), special_arg->name.To<std::string>()); | 304 EXPECT_EQ(std::string("going"), special_arg->name.To<std::string>()); |
| 304 CheckSampleEchoArgsList(list->next); | 305 CheckSampleEchoArgsList(list->next); |
| 305 } | 306 } |
| 306 | 307 |
| 307 virtual void TestFinished() OVERRIDE { | 308 virtual void TestFinished() override { |
| 308 termination_seen_ = true; | 309 termination_seen_ = true; |
| 309 run_loop()->Quit(); | 310 run_loop()->Quit(); |
| 310 } | 311 } |
| 311 | 312 |
| 312 bool DidSucceed() { | 313 bool DidSucceed() { |
| 313 return termination_seen_ && | 314 return termination_seen_ && |
| 314 !mishandled_messages_ && | 315 !mishandled_messages_ && |
| 315 message_count_ == kExpectedMessageCount; | 316 message_count_ == kExpectedMessageCount; |
| 316 } | 317 } |
| 317 | 318 |
| 318 private: | 319 private: |
| 319 static const int kExpectedMessageCount = 10; | 320 static const int kExpectedMessageCount = 10; |
| 320 int message_count_; | 321 int message_count_; |
| 321 bool termination_seen_; | 322 bool termination_seen_; |
| 322 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); | 323 DISALLOW_COPY_AND_ASSIGN(EchoCppSideConnection); |
| 323 }; | 324 }; |
| 324 | 325 |
| 325 // Test that corrupted messages don't wreak havoc. | 326 // Test that corrupted messages don't wreak havoc. |
| 326 class BitFlipCppSideConnection : public CppSideConnection { | 327 class BitFlipCppSideConnection : public CppSideConnection { |
| 327 public: | 328 public: |
| 328 BitFlipCppSideConnection() : termination_seen_(false) {} | 329 BitFlipCppSideConnection() : termination_seen_(false) {} |
| 329 virtual ~BitFlipCppSideConnection() {} | 330 virtual ~BitFlipCppSideConnection() {} |
| 330 | 331 |
| 331 // js_to_cpp::CppSide: | 332 // js_to_cpp::CppSide: |
| 332 virtual void StartTest() OVERRIDE { | 333 virtual void StartTest() override { |
| 333 js_side_->BitFlip(BuildSampleEchoArgs()); | 334 js_side_->BitFlip(BuildSampleEchoArgs()); |
| 334 } | 335 } |
| 335 | 336 |
| 336 virtual void BitFlipResponse(js_to_cpp::EchoArgsListPtr list) OVERRIDE { | 337 virtual void BitFlipResponse(js_to_cpp::EchoArgsListPtr list) override { |
| 337 CheckCorruptedEchoArgsList(list); | 338 CheckCorruptedEchoArgsList(list); |
| 338 } | 339 } |
| 339 | 340 |
| 340 virtual void TestFinished() OVERRIDE { | 341 virtual void TestFinished() override { |
| 341 termination_seen_ = true; | 342 termination_seen_ = true; |
| 342 run_loop()->Quit(); | 343 run_loop()->Quit(); |
| 343 } | 344 } |
| 344 | 345 |
| 345 bool DidSucceed() { | 346 bool DidSucceed() { |
| 346 return termination_seen_; | 347 return termination_seen_; |
| 347 } | 348 } |
| 348 | 349 |
| 349 private: | 350 private: |
| 350 bool termination_seen_; | 351 bool termination_seen_; |
| 351 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection); | 352 DISALLOW_COPY_AND_ASSIGN(BitFlipCppSideConnection); |
| 352 }; | 353 }; |
| 353 | 354 |
| 354 // Test that severely random messages don't wreak havoc. | 355 // Test that severely random messages don't wreak havoc. |
| 355 class BackPointerCppSideConnection : public CppSideConnection { | 356 class BackPointerCppSideConnection : public CppSideConnection { |
| 356 public: | 357 public: |
| 357 BackPointerCppSideConnection() : termination_seen_(false) {} | 358 BackPointerCppSideConnection() : termination_seen_(false) {} |
| 358 virtual ~BackPointerCppSideConnection() {} | 359 virtual ~BackPointerCppSideConnection() {} |
| 359 | 360 |
| 360 // js_to_cpp::CppSide: | 361 // js_to_cpp::CppSide: |
| 361 virtual void StartTest() OVERRIDE { | 362 virtual void StartTest() override { |
| 362 js_side_->BackPointer(BuildSampleEchoArgs()); | 363 js_side_->BackPointer(BuildSampleEchoArgs()); |
| 363 } | 364 } |
| 364 | 365 |
| 365 virtual void BackPointerResponse( | 366 virtual void BackPointerResponse( |
| 366 js_to_cpp::EchoArgsListPtr list) OVERRIDE { | 367 js_to_cpp::EchoArgsListPtr list) override { |
| 367 CheckCorruptedEchoArgsList(list); | 368 CheckCorruptedEchoArgsList(list); |
| 368 } | 369 } |
| 369 | 370 |
| 370 virtual void TestFinished() OVERRIDE { | 371 virtual void TestFinished() override { |
| 371 termination_seen_ = true; | 372 termination_seen_ = true; |
| 372 run_loop()->Quit(); | 373 run_loop()->Quit(); |
| 373 } | 374 } |
| 374 | 375 |
| 375 bool DidSucceed() { | 376 bool DidSucceed() { |
| 376 return termination_seen_; | 377 return termination_seen_; |
| 377 } | 378 } |
| 378 | 379 |
| 379 private: | 380 private: |
| 380 bool termination_seen_; | 381 bool termination_seen_; |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 448 if (IsRunningOnIsolatedBot()) | 449 if (IsRunningOnIsolatedBot()) |
| 449 return; | 450 return; |
| 450 | 451 |
| 451 BackPointerCppSideConnection cpp_side_connection; | 452 BackPointerCppSideConnection cpp_side_connection; |
| 452 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); | 453 RunTest("mojo/apps/js/test/js_to_cpp_unittest", &cpp_side_connection); |
| 453 EXPECT_TRUE(cpp_side_connection.DidSucceed()); | 454 EXPECT_TRUE(cpp_side_connection.DidSucceed()); |
| 454 } | 455 } |
| 455 | 456 |
| 456 } // namespace js | 457 } // namespace js |
| 457 } // namespace mojo | 458 } // namespace mojo |
| OLD | NEW |