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.
4
5#include "gin/test/file_runner.h"
6
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 "testing/gtest/include/gtest/gtest.h"
21
22namespace gin {
23
24namespace {
25
26std::vector<base::FilePath> GetModuleSearchPaths() {
27  std::vector<base::FilePath> search_paths(2);
28  PathService::Get(base::DIR_SOURCE_ROOT, &search_paths[0]);
29  PathService::Get(base::DIR_EXE, &search_paths[1]);
30  search_paths[1] = search_paths[1].AppendASCII("gen");
31  return search_paths;
32}
33
34}  // namespace
35
36FileRunnerDelegate::FileRunnerDelegate()
37    : ModuleRunnerDelegate(GetModuleSearchPaths()) {
38  AddBuiltinModule(Console::kModuleName, Console::GetModule);
39  AddBuiltinModule(GTest::kModuleName, GTest::GetModule);
40  AddBuiltinModule(GC::kModuleName, GC::GetModule);
41  AddBuiltinModule(File::kModuleName, File::GetModule);
42}
43
44FileRunnerDelegate::~FileRunnerDelegate() {
45}
46
47void FileRunnerDelegate::UnhandledException(ShellRunner* runner,
48                                            TryCatch& try_catch) {
49  ModuleRunnerDelegate::UnhandledException(runner, try_catch);
50  FAIL() << try_catch.GetStackTrace();
51}
52
53void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate,
54                     bool run_until_idle) {
55  ASSERT_TRUE(base::PathExists(path)) << path.LossyDisplayName();
56  std::string source;
57  ASSERT_TRUE(ReadFileToString(path, &source));
58
59  base::MessageLoop message_loop;
60
61  gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
62                                 gin::ArrayBufferAllocator::SharedInstance());
63  gin::IsolateHolder instance;
64  gin::ShellRunner runner(delegate, instance.isolate());
65  {
66    gin::Runner::Scope scope(&runner);
67    v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
68    runner.Run(source, path.AsUTF8Unsafe());
69
70    if (run_until_idle) {
71      message_loop.RunUntilIdle();
72    } else {
73      message_loop.Run();
74    }
75
76    v8::Handle<v8::Value> result = runner.global()->Get(
77        StringToSymbol(runner.GetContextHolder()->isolate(), "result"));
78    EXPECT_EQ("PASS", V8ToString(result));
79  }
80}
81
82}  // namespace gin
83