• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
19 class 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   // A helper class to ensure that the lifetimes of the Isolate and the
72   // HandleScope are correctly nested.
73   class IsolateScope {
74    public:
IsolateScope()75     IsolateScope() : isolate_(v8::Isolate::New()) { isolate_->Enter(); }
76 
~IsolateScope()77     ~IsolateScope() {
78       isolate_->Exit();
79       isolate_->Dispose();
80     }
81 
isolate()82     v8::Isolate* isolate() const { return isolate_; }
83 
84    private:
85     v8::Isolate* isolate_;
86 
87     DISALLOW_COPY_AND_ASSIGN(IsolateScope);
88   };
89 
90   // A handy shortcut.
isolate()91   v8::Isolate* isolate() const { return isolate_scope_.isolate(); }
92 
93   // Executes all added javascript libraries. Returns true if no errors.
94   bool ExecuteJavascriptLibraries();
95 
96   // Initializes paths and libraries.
97   void InitPathsAndLibraries();
98 
99   IsolateScope isolate_scope_;
100 
101   // Handle scope that is used throughout the life of this class.
102   v8::HandleScope handle_scope_;
103 
104   // Context for the JavaScript in the test.
105   v8::Persistent<v8::Context> context_;
106 
107   // User added libraries.
108   std::vector<base::FilePath> user_libraries_;
109 };
110 
111 #endif  // CHROME_TEST_BASE_V8_UNIT_TEST_H_
112