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 virtual void SetUp(); |
27 | 29 |
28 protected: | 30 protected: |
| 31 void AddLibrary(const FilePath& library_path); |
| 32 bool RunJavascriptTestF(const std::string& testFixture, |
| 33 const std::string& testName); |
| 34 |
29 // Executes the given script source in the context. The specified script | 35 // Executes the given script source in the context. The specified script |
30 // name is used when reporting errors. | 36 // name is used when reporting errors. |
31 virtual void ExecuteScriptInContext(const base::StringPiece& script_source, | 37 virtual void ExecuteScriptInContext(const base::StringPiece& script_source, |
32 const base::StringPiece& script_name); | 38 const base::StringPiece& script_name); |
33 | 39 |
34 // Set a variable to a string value in the global scope. | 40 // Set a variable to a string value in the global scope. |
35 virtual void SetGlobalStringVar(const std::string& var_name, | 41 virtual void SetGlobalStringVar(const std::string& var_name, |
36 const std::string& value); | 42 const std::string& value); |
37 | 43 |
38 // Converts a v8::TryCatch into a human readable string. | 44 // Converts a v8::TryCatch into a human readable string. |
39 virtual std::string ExceptionToString(const v8::TryCatch& try_catch); | 45 virtual std::string ExceptionToString(const v8::TryCatch& try_catch); |
40 | 46 |
41 // Calls the specified function that resides in the global scope of the | 47 // Calls the specified function that resides in the global scope of the |
42 // context. If the function throws an exception, FAIL() is called to | 48 // context. If the function throws an exception, FAIL() is called to |
43 // indicate a unit test failure. This is useful for executing unit test | 49 // indicate a unit test failure. This is useful for executing unit test |
44 // functions implemented in JavaScript. | 50 // functions implemented in JavaScript. |
45 virtual void TestFunction(const std::string& function_name); | 51 virtual void TestFunction(const std::string& function_name); |
46 | 52 |
47 // This method is bound to a global function "log" in the context. | 53 // 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 | 54 // to log, warn, and info of the console object. Scripts running in the |
49 // information to the console. | 55 // context can call this to print out logging information to the console. |
50 static v8::Handle<v8::Value> Log(const v8::Arguments& args); | 56 static v8::Handle<v8::Value> Log(const v8::Arguments& args); |
51 | 57 |
| 58 // This method is bound to console.error in the context. Any calls to this |
| 59 // also signal an error condition and |RunJavascriptF| will fail. |
| 60 static v8::Handle<v8::Value> Error(const v8::Arguments& args); |
| 61 |
| 62 // This method is bound to a method "chrome.send" in the context. When |
| 63 // test_api calls testDone to report its results, this will capture and hold |
| 64 // the results for analysis by |RunJavascriptF|. |
| 65 static v8::Handle<v8::Value> ChromeSend(const v8::Arguments& args); |
| 66 |
| 67 private: |
| 68 // Executes all added javascript libraries. Returns true if passing so far. |
| 69 bool ExecuteJavascriptLibraries(); |
| 70 |
| 71 // Initialize paths and libraries. |
| 72 void InitPathsAndLibraries(); |
| 73 |
52 // Handle scope that is used throughout the life of this class. | 74 // Handle scope that is used throughout the life of this class. |
53 v8::HandleScope handle_scope_; | 75 v8::HandleScope handle_scope_; |
54 | 76 |
55 // Context for the JavaScript in the test. | 77 // Context for the JavaScript in the test. |
56 v8::Handle<v8::Context> context_; | 78 v8::Handle<v8::Context> context_; |
| 79 |
| 80 // Location of test data (currently test/data/webui). |
| 81 FilePath test_data_directory_; |
| 82 |
| 83 // Location of generated test data (<(PROGRAM_DIR)/test_data). |
| 84 FilePath gen_test_data_directory_; |
| 85 |
| 86 // User added libraries. |
| 87 std::vector<FilePath> user_libraries_; |
| 88 |
| 89 // Whether errors were seen. |
| 90 static bool had_errors_; |
| 91 |
| 92 // testDone results. |
| 93 static bool testResult_ok_; |
57 }; | 94 }; |
58 | 95 |
59 #endif // CHROME_TEST_BASE_V8_UNIT_TEST_H_ | 96 #endif // CHROME_TEST_BASE_V8_UNIT_TEST_H_ |
OLD | NEW |