1// Copyright (c) 2011 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#ifndef CHROME_TEST_BASE_V8_UNIT_TEST_H_ 6#define CHROME_TEST_BASE_V8_UNIT_TEST_H_ 7 8#include <string> 9#include <vector> 10 11#include "base/files/file_path.h" 12#include "base/strings/string_piece.h" 13#include "testing/gtest/include/gtest/gtest.h" 14#include "v8/include/v8.h" 15 16// A superclass for unit tests that involve running JavaScript. This class 17// sets up V8 context and has methods that make it easy to execute scripts in 18// this context as well as call functions in the context. 19class V8UnitTest : public testing::Test { 20 public: 21 V8UnitTest(); 22 virtual ~V8UnitTest(); 23 24 // Methods from testing::Test. 25 virtual void SetUp() OVERRIDE; 26 27 protected: 28 // Add a custom helper JS library for your test. If |library_path| is 29 // relative, it'll be read as relative to the test data dir. 30 void AddLibrary(const base::FilePath& library_path); 31 32 // Runs |test_fixture|.|test_name| using the framework in test_api.js. 33 bool RunJavascriptTestF(const std::string& test_fixture, 34 const std::string& test_name); 35 36 // Executes the given |script_source| in the context. The specified 37 // |script_name| is used when reporting errors. 38 virtual void ExecuteScriptInContext(const base::StringPiece& script_source, 39 const base::StringPiece& script_name); 40 41 // Set the variable |var_name| to a string |value| in the global scope. 42 virtual void SetGlobalStringVar(const std::string& var_name, 43 const std::string& value); 44 45 // Converts the v8::TryCatch |try_catch| into a human readable string. 46 virtual std::string ExceptionToString(const v8::TryCatch& try_catch); 47 48 // Calls the specified |function_name| that resides in the global scope of the 49 // context. If the function throws an exception, FAIL() is called to indicate 50 // a unit test failure. This is useful for executing unit test functions 51 // implemented in JavaScript. 52 virtual void TestFunction(const std::string& function_name); 53 54 // This method is bound to a global function "log" in the context, as well as 55 // to log, warn, and info of the console object. Scripts running in the 56 // context can call this with |args| to print out logging information to the 57 // console. 58 static void Log(const v8::FunctionCallbackInfo<v8::Value>& args); 59 60 // This method is bound to console.error in the context. Any calls to this 61 // will log |args| to the console and also signal an error condition causing 62 // |RunJavascriptF| to fail. 63 static void Error(const v8::FunctionCallbackInfo<v8::Value>& args); 64 65 // This method is bound to a method "chrome.send" in the context. When 66 // test_api calls testDone with |args| to report its results, this will 67 // capture and hold the results for analysis by |RunJavascriptF|. 68 static void ChromeSend(const v8::FunctionCallbackInfo<v8::Value>& args); 69 70 private: 71 // Executes all added javascript libraries. Returns true if no errors. 72 bool ExecuteJavascriptLibraries(); 73 74 // Initializes paths and libraries. 75 void InitPathsAndLibraries(); 76 77 v8::Isolate* isolate_; 78 79 // Handle scope that is used throughout the life of this class. 80 v8::HandleScope handle_scope_; 81 82 // Context for the JavaScript in the test. 83 v8::Persistent<v8::Context> context_; 84 85 // User added libraries. 86 std::vector<base::FilePath> user_libraries_; 87}; 88 89#endif // CHROME_TEST_BASE_V8_UNIT_TEST_H_ 90