| 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 // Add a custom helper JS library for your test. |
| 32 // If a relative path is specified, it'll be read |
| 33 // as relative to the test data dir. |
| 34 void AddLibrary(const FilePath& library_path); |
| 35 |
| 36 // Runs a test fixture that may include calls to functions in test_api.js. |
| 37 bool RunJavascriptTestF(const std::string& testFixture, |
| 38 const std::string& testName); |
| 39 |
| 29 // Executes the given script source in the context. The specified script | 40 // Executes the given script source in the context. The specified script |
| 30 // name is used when reporting errors. | 41 // 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 a variable 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 a v8::TryCatch 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 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 |
| 43 // indicate a unit test failure. This is useful for executing unit test | 54 // indicate a unit test failure. This is useful for executing unit test |
| 44 // functions implemented in JavaScript. | 55 // functions 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 to print out logging information to the console. |
| 50 static v8::Handle<v8::Value> Log(const v8::Arguments& args); | 61 static v8::Handle<v8::Value> Log(const v8::Arguments& args); |
| 51 | 62 |
| 63 // This method is bound to console.error in the context. Any calls to this |
| 64 // also signal an error condition and |RunJavascriptF| will fail. |
| 65 static v8::Handle<v8::Value> Error(const v8::Arguments& args); |
| 66 |
| 67 // This method is bound to a method "chrome.send" in the context. When |
| 68 // test_api calls testDone to report its results, this will capture and hold |
| 69 // the results for analysis by |RunJavascriptF|. |
| 70 static v8::Handle<v8::Value> ChromeSend(const v8::Arguments& args); |
| 71 |
| 72 private: |
| 73 // Executes all added javascript libraries. Returns true if no errors. |
| 74 bool ExecuteJavascriptLibraries(); |
| 75 |
| 76 // Initializes paths and libraries. |
| 77 void InitPathsAndLibraries(); |
| 78 |
| 52 // Handle scope that is used throughout the life of this class. | 79 // Handle scope that is used throughout the life of this class. |
| 53 v8::HandleScope handle_scope_; | 80 v8::HandleScope handle_scope_; |
| 54 | 81 |
| 55 // Context for the JavaScript in the test. | 82 // Context for the JavaScript in the test. |
| 56 v8::Handle<v8::Context> context_; | 83 v8::Handle<v8::Context> context_; |
| 84 |
| 85 // User added libraries. |
| 86 std::vector<FilePath> user_libraries_; |
| 57 }; | 87 }; |
| 58 | 88 |
| 59 #endif // CHROME_TEST_BASE_V8_UNIT_TEST_H_ | 89 #endif // CHROME_TEST_BASE_V8_UNIT_TEST_H_ |
| OLD | NEW |