Chromecast: extracts Linux window creation code to a common place.
[chromium-blink-merge.git] / gin / test / file_runner.cc
blob83228d6d014e5570a63847f149dca2df58941b07
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 "testing/gtest/include/gtest/gtest.h"
22 namespace gin {
24 namespace {
26 std::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;
34 } // namespace
36 FileRunnerDelegate::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);
44 FileRunnerDelegate::~FileRunnerDelegate() {
47 void FileRunnerDelegate::UnhandledException(ShellRunner* runner,
48 TryCatch& try_catch) {
49 ModuleRunnerDelegate::UnhandledException(runner, try_catch);
50 FAIL() << try_catch.GetStackTrace();
53 void 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));
59 base::MessageLoop message_loop;
61 gin::IsolateHolder::Initialize(gin::IsolateHolder::kStrictMode,
62 gin::ArrayBufferAllocator::SharedInstance());
63 gin::IsolateHolder instance;
64 gin::ShellRunner runner(delegate, instance.isolate());
66 gin::Runner::Scope scope(&runner);
67 v8::V8::SetCaptureStackTraceForUncaughtExceptions(true);
68 runner.Run(source, path.AsUTF8Unsafe());
70 if (run_until_idle) {
71 message_loop.RunUntilIdle();
72 } else {
73 message_loop.Run();
76 v8::Handle<v8::Value> result = runner.global()->Get(
77 StringToSymbol(runner.GetContextHolder()->isolate(), "result"));
78 EXPECT_EQ("PASS", V8ToString(result));
82 } // namespace gin