Roll src/third_party/WebKit 38132d1:d3e6747 (svn 202464:202465)
[chromium-blink-merge.git] / gin / test / file_runner.cc
blob4e84b88c3a9fc1527fa717eeed3244e82bcf086e
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
3 // found in the LICENSE file.
5 #include "gin/test/file_runner.h"
7 #include "base/files/file_util.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/path_service.h"
10 #include "gin/array_buffer.h"
11 #include "gin/converter.h"
12 #include "gin/modules/console.h"
13 #include "gin/modules/module_registry.h"
14 #include "gin/public/context_holder.h"
15 #include "gin/public/isolate_holder.h"
16 #include "gin/test/file.h"
17 #include "gin/test/gc.h"
18 #include "gin/test/gtest.h"
19 #include "gin/try_catch.h"
20 #include "gin/v8_initializer.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace gin {
25 namespace {
27 std::vector<base::FilePath> GetModuleSearchPaths() {
28 std::vector<base::FilePath> search_paths(2);
29 PathService::Get(base::DIR_SOURCE_ROOT, &search_paths[0]);
30 PathService::Get(base::DIR_EXE, &search_paths[1]);
31 search_paths[1] = search_paths[1].AppendASCII("gen");
32 return search_paths;
35 } // namespace
37 FileRunnerDelegate::FileRunnerDelegate()
38 : ModuleRunnerDelegate(GetModuleSearchPaths()) {
39 AddBuiltinModule(Console::kModuleName, Console::GetModule);
40 AddBuiltinModule(GTest::kModuleName, GTest::GetModule);
41 AddBuiltinModule(GC::kModuleName, GC::GetModule);
42 AddBuiltinModule(File::kModuleName, File::GetModule);
45 FileRunnerDelegate::~FileRunnerDelegate() {
48 void FileRunnerDelegate::UnhandledException(ShellRunner* runner,
49 TryCatch& try_catch) {
50 ModuleRunnerDelegate::UnhandledException(runner, try_catch);
51 FAIL() << try_catch.GetStackTrace();
54 void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate,
55 bool run_until_idle) {
56 ASSERT_TRUE(base::PathExists(path)) << path.LossyDisplayName();
57 std::string source;
58 ASSERT_TRUE(ReadFileToString(path, &source));
60 base::MessageLoop message_loop;
62 #ifdef V8_USE_EXTERNAL_STARTUP_DATA
63 gin::V8Initializer::LoadV8Snapshot();
64 gin::V8Initializer::LoadV8Natives();
65 #endif
67 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
68 gin::ArrayBufferAllocator::SharedInstance());
70 gin::IsolateHolder instance;
71 gin::ShellRunner runner(delegate, instance.isolate());
73 gin::Runner::Scope scope(&runner);
74 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
75 runner.Run(source, path.AsUTF8Unsafe());
77 if (run_until_idle) {
78 message_loop.RunUntilIdle();
79 } else {
80 message_loop.Run();
83 v8::Local<v8::Value> result = runner.global()->Get(
84 StringToSymbol(runner.GetContextHolder()->isolate(), "result"));
85 EXPECT_EQ("PASS", V8ToString(result));
89 } // namespace gin