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