| 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> | |
| 11 | 10 |
| 12 #include "base/file_path.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "v8/include/v8.h" | 12 #include "v8/include/v8.h" |
| 15 | 13 |
| 16 namespace base { | 14 namespace base { |
| 17 class StringPiece; | 15 class StringPiece; |
| 18 } | 16 } |
| 19 | 17 |
| 20 // A superclass for unit tests that involve running JavaScript. This class | 18 // A superclass for unit tests that involve running JavaScript. This class |
| 21 // sets up V8 context and has methods that make it easy to execute scripts in | 19 // sets up V8 context and has methods that make it easy to execute scripts in |
| 22 // this context as well as call functions in the context. | 20 // this context as well as call functions in the context. |
| 23 class V8UnitTest : public testing::Test { | 21 class V8UnitTest : public testing::Test { |
| 24 public: | 22 public: |
| 25 V8UnitTest(); | 23 V8UnitTest(); |
| 26 virtual ~V8UnitTest(); | 24 virtual ~V8UnitTest(); |
| 27 | 25 |
| 28 // Methods from testing::Test. | 26 virtual void SetUp(); |
| 29 virtual void SetUp() OVERRIDE; | |
| 30 | 27 |
| 31 protected: | 28 protected: |
| 32 // Add a custom helper JS library for your test. If |library_path| is | 29 // Executes the given script source in the context. The specified script |
| 33 // relative, it'll be read as relative to the test data dir. | 30 // name is used when reporting errors. |
| 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. | |
| 42 virtual void ExecuteScriptInContext(const base::StringPiece& script_source, | 31 virtual void ExecuteScriptInContext(const base::StringPiece& script_source, |
| 43 const base::StringPiece& script_name); | 32 const base::StringPiece& script_name); |
| 44 | 33 |
| 45 // Set the variable |var_name| to a string |value| in the global scope. | 34 // Set a variable to a string value in the global scope. |
| 46 virtual void SetGlobalStringVar(const std::string& var_name, | 35 virtual void SetGlobalStringVar(const std::string& var_name, |
| 47 const std::string& value); | 36 const std::string& value); |
| 48 | 37 |
| 49 // Converts the v8::TryCatch |try_catch| into a human readable string. | 38 // Converts a v8::TryCatch into a human readable string. |
| 50 virtual std::string ExceptionToString(const v8::TryCatch& try_catch); | 39 virtual std::string ExceptionToString(const v8::TryCatch& try_catch); |
| 51 | 40 |
| 52 // Calls the specified |function_name| that resides in the global scope of the | 41 // Calls the specified function that resides in the global scope of the |
| 53 // context. If the function throws an exception, FAIL() is called to indicate | 42 // context. If the function throws an exception, FAIL() is called to |
| 54 // a unit test failure. This is useful for executing unit test functions | 43 // indicate a unit test failure. This is useful for executing unit test |
| 55 // implemented in JavaScript. | 44 // functions implemented in JavaScript. |
| 56 virtual void TestFunction(const std::string& function_name); | 45 virtual void TestFunction(const std::string& function_name); |
| 57 | 46 |
| 58 // This method is bound to a global function "log" in the context, as well as | 47 // This method is bound to a global function "log" in the context. |
| 59 // to log, warn, and info of the console object. Scripts running in the | 48 // Scripts running in the context can call this to print out logging |
| 60 // context can call this with |args| to print out logging information to the | 49 // information to the console. |
| 61 // console. | |
| 62 static v8::Handle<v8::Value> Log(const v8::Arguments& args); | 50 static v8::Handle<v8::Value> Log(const v8::Arguments& args); |
| 63 | 51 |
| 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 | |
| 81 // Handle scope that is used throughout the life of this class. | 52 // Handle scope that is used throughout the life of this class. |
| 82 v8::HandleScope handle_scope_; | 53 v8::HandleScope handle_scope_; |
| 83 | 54 |
| 84 // Context for the JavaScript in the test. | 55 // Context for the JavaScript in the test. |
| 85 v8::Handle<v8::Context> context_; | 56 v8::Handle<v8::Context> context_; |
| 86 | |
| 87 // User added libraries. | |
| 88 std::vector<FilePath> user_libraries_; | |
| 89 }; | 57 }; |
| 90 | 58 |
| 91 #endif // CHROME_TEST_BASE_V8_UNIT_TEST_H_ | 59 #endif // CHROME_TEST_BASE_V8_UNIT_TEST_H_ |
| OLD | NEW |