shell_runner_unittest.cc 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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
  3. // found in the LICENSE file.
  4. #include "gin/shell_runner.h"
  5. #include "base/compiler_specific.h"
  6. #include "base/test/task_environment.h"
  7. #include "base/threading/thread_task_runner_handle.h"
  8. #include "gin/array_buffer.h"
  9. #include "gin/converter.h"
  10. #include "gin/public/isolate_holder.h"
  11. #include "gin/v8_initializer.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. #ifdef V8_USE_EXTERNAL_STARTUP_DATA
  14. #include "gin/public/isolate_holder.h"
  15. #endif
  16. using v8::Isolate;
  17. using v8::Object;
  18. using v8::Script;
  19. using v8::String;
  20. namespace gin {
  21. TEST(RunnerTest, Run) {
  22. // V8 is generally multi threaded and may use tasks for arbitrary reasons,
  23. // such as GC and off-thread compilation.
  24. base::test::TaskEnvironment task_environment;
  25. std::string source = "this.result = 'PASS';\n";
  26. #ifdef V8_USE_EXTERNAL_STARTUP_DATA
  27. gin::V8Initializer::LoadV8Snapshot();
  28. #endif
  29. gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
  30. gin::ArrayBufferAllocator::SharedInstance());
  31. gin::IsolateHolder instance(base::ThreadTaskRunnerHandle::Get(),
  32. gin::IsolateHolder::IsolateType::kTest);
  33. ShellRunnerDelegate delegate;
  34. Isolate* isolate = instance.isolate();
  35. ShellRunner runner(&delegate, isolate);
  36. Runner::Scope scope(&runner);
  37. runner.Run(source, "test_data.js");
  38. std::string result;
  39. EXPECT_TRUE(Converter<std::string>::FromV8(
  40. isolate,
  41. runner.global()
  42. ->Get(isolate->GetCurrentContext(), StringToV8(isolate, "result"))
  43. .ToLocalChecked(),
  44. &result));
  45. EXPECT_EQ("PASS", result);
  46. }
  47. } // namespace gin