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/file_util.h"
8 #include "base/message_loop/message_loop.h"
9 #include "base/path_service.h"
10 #include "gin/converter.h"
11 #include "gin/modules/console.h"
12 #include "gin/modules/module_registry.h"
13 #include "gin/public/isolate_holder.h"
14 #include "gin/test/gtest.h"
15 #include "gin/try_catch.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17
18 namespace gin {
19
20 namespace {
21
GetModuleSearchPaths()22 std::vector<base::FilePath> GetModuleSearchPaths() {
23 std::vector<base::FilePath> search_paths(2);
24 PathService::Get(base::DIR_SOURCE_ROOT, &search_paths[0]);
25 PathService::Get(base::DIR_EXE, &search_paths[1]);
26 search_paths[1] = search_paths[1].AppendASCII("gen");
27 return search_paths;
28 }
29
30 } // namespace
31
FileRunnerDelegate()32 FileRunnerDelegate::FileRunnerDelegate()
33 : ModuleRunnerDelegate(GetModuleSearchPaths()) {
34 AddBuiltinModule(Console::kModuleName, Console::GetTemplate);
35 AddBuiltinModule(GTest::kModuleName, GTest::GetTemplate);
36 }
37
~FileRunnerDelegate()38 FileRunnerDelegate::~FileRunnerDelegate() {
39 }
40
UnhandledException(Runner * runner,TryCatch & try_catch)41 void FileRunnerDelegate::UnhandledException(Runner* runner,
42 TryCatch& try_catch) {
43 ModuleRunnerDelegate::UnhandledException(runner, try_catch);
44 FAIL() << try_catch.GetStackTrace();
45 }
46
RunTestFromFile(const base::FilePath & path,FileRunnerDelegate * delegate)47 void RunTestFromFile(const base::FilePath& path, FileRunnerDelegate* delegate) {
48 ASSERT_TRUE(base::PathExists(path)) << path.LossyDisplayName();
49 std::string source;
50 ASSERT_TRUE(ReadFileToString(path, &source));
51
52 base::MessageLoop message_loop;
53
54 gin::IsolateHolder instance;
55 gin::Runner runner(delegate, instance.isolate());
56 {
57 gin::Runner::Scope scope(&runner);
58 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
59 runner.Run(source, path.AsUTF8Unsafe());
60
61 message_loop.RunUntilIdle();
62
63 v8::Handle<v8::Value> result = runner.context()->Global()->Get(
64 StringToSymbol(runner.isolate(), "result"));
65 EXPECT_EQ("PASS", V8ToString(result));
66 }
67 }
68
69 } // namespace gin
70