1 // Copyright (c) 2009 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_V8_UNIT_TEST_H_
6 #define CHROME_TEST_V8_UNIT_TEST_H_
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "v8/include/v8.h"
18 // A superclass for unit tests that involve running JavaScript. This class
19 // sets up V8 context and has methods that make it easy to execute scripts in
20 // this context as well as call functions in the context.
21 class V8UnitTest
: public testing::Test
{
24 virtual ~V8UnitTest();
29 // Executes the given script source in the context. The specified script
30 // name is used when reporting errors.
31 virtual void ExecuteScriptInContext(const base::StringPiece
& script_source
,
32 const base::StringPiece
& script_name
);
34 // Set a variable to a string value in the global scope.
35 virtual void SetGlobalStringVar(const std::string
& var_name
,
36 const std::string
& value
);
38 // Converts a v8::TryCatch into a human readable string.
39 virtual std::string
ExceptionToString(v8::TryCatch
* try_catch
);
41 // Calls the specified function that resides in the global scope of the
42 // context. If the function throws an exception, FAIL() is called to
43 // indicate a unit test failure. This is useful for executing unit test
44 // functions implemented in JavaScript.
45 virtual void TestFunction(const std::string
& function_name
);
47 // This method is bound to a global function "log" in the context.
48 // Scripts running in the context can call this to print out logging
49 // information to the console.
50 static v8::Handle
<v8::Value
> Log(const v8::Arguments
& args
);
52 // Handle scope that is used throughout the life of this class.
53 v8::HandleScope handle_scope_
;
55 // Context for the JavaScript in the test.
56 v8::Handle
<v8::Context
> context_
;
59 #endif // CHROME_TEST_V8_UNIT_TEST_H_