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.
5 #ifndef CHROME_TEST_BASE_V8_UNIT_TEST_H_
6 #define CHROME_TEST_BASE_V8_UNIT_TEST_H_
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"
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.
19 class V8UnitTest
: public testing::Test
{
22 ~V8UnitTest() override
;
24 // Methods from testing::Test.
25 void SetUp() override
;
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
);
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
);
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
);
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
);
45 // Converts the v8::TryCatch |try_catch| into a human readable string.
46 virtual std::string
ExceptionToString(const v8::TryCatch
& try_catch
);
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
);
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
58 static void Log(const v8::FunctionCallbackInfo
<v8::Value
>& args
);
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
);
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
);
71 // Executes all added javascript libraries. Returns true if no errors.
72 bool ExecuteJavascriptLibraries();
74 // Initializes paths and libraries.
75 void InitPathsAndLibraries();
77 // Handle scope that is used throughout the life of this class.
78 v8::HandleScope handle_scope_
;
80 // Context for the JavaScript in the test.
81 v8::Persistent
<v8::Context
> context_
;
83 // User added libraries.
84 std::vector
<base::FilePath
> user_libraries_
;
87 #endif // CHROME_TEST_BASE_V8_UNIT_TEST_H_