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