Chromium Code Reviews| Index: chrome/test/base/v8_unit_test.cc |
| diff --git a/chrome/test/base/v8_unit_test.cc b/chrome/test/base/v8_unit_test.cc |
| index 798a03f5f98471d29721090bedaba1ac1cc9ab0b..912e4dfa2b6bcf049065518ee900e973f9f669b7 100644 |
| --- a/chrome/test/base/v8_unit_test.cc |
| +++ b/chrome/test/base/v8_unit_test.cc |
| @@ -4,17 +4,161 @@ |
| #include "chrome/test/base/v8_unit_test.h" |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/path_service.h" |
| #include "base/string_piece.h" |
| #include "base/stringprintf.h" |
| +#include "chrome/common/chrome_paths.h" |
| -V8UnitTest::V8UnitTest() {} |
| +#ifdef OS_WIN |
| +#include "base/string_util.h" |
| +#define FILE_PATH_TO_ASCII(FP) WideToASCII((FP).value()) |
| +#else |
| +#define FILE_PATH_TO_ASCII(FP) ((FP).value()) |
| +#endif // OS_WIN |
| + |
| +namespace { |
|
James Hawkins
2011/10/29 22:15:48
Inconsistent: No blank line between namespace and
Sheridan Rawlins
2011/10/30 17:04:21
Done.
|
| +std::string Args2String(const v8::Arguments& args) { |
|
James Hawkins
2011/10/29 22:15:48
Document the function and params.
Sheridan Rawlins
2011/10/30 17:04:21
Done.
|
| + std::string message; |
| + bool first = true; |
| + for (int i = 0; i < args.Length(); i++) { |
| + v8::HandleScope handle_scope; |
| + if (first) { |
|
James Hawkins
2011/10/29 22:15:48
Optional but recommended: no braces on single-line
Sheridan Rawlins
2011/10/30 17:04:21
Done.
|
| + first = false; |
| + } else { |
| + message += " "; |
| + } |
| + v8::String::Utf8Value str(args[i]); |
| + message += *str; |
| + } |
| + return message; |
| +} |
| + |
| +} // namespace |
| + |
| +bool V8UnitTest::had_errors_ = false; |
| +bool V8UnitTest::testResult_ok_ = false; |
| + |
| +V8UnitTest::V8UnitTest() { |
| + InitPathsAndLibraries(); |
| +} |
| V8UnitTest::~V8UnitTest() {} |
| +void V8UnitTest::AddLibrary(const FilePath& library_path) { |
| + user_libraries_.push_back(library_path); |
| +} |
| + |
| +bool V8UnitTest::ExecuteJavascriptLibraries() { |
| + std::string utf8_content; |
| + std::vector<FilePath>::iterator user_libraries_iterator; |
|
James Hawkins
2011/10/29 22:15:48
Declare the iterator in the context of the for loo
Sheridan Rawlins
2011/10/30 17:04:21
Done.
|
| + for (user_libraries_iterator = user_libraries_.begin(); |
| + user_libraries_iterator != user_libraries_.end(); |
| + ++user_libraries_iterator) { |
| + std::string library_content; |
| + FilePath library_file(*user_libraries_iterator); |
| + if (!user_libraries_iterator->IsAbsolute()) { |
| + FilePath gen_file = gen_test_data_directory_.Append(library_file); |
| + library_file = file_util::PathExists(gen_file) ? gen_file : |
| + test_data_directory_.Append(*user_libraries_iterator); |
| + } |
| + if (!file_util::ReadFileToString(library_file, &library_content)) { |
| + ADD_FAILURE() << library_file.value(); |
| + return false; |
| + } |
| + ExecuteScriptInContext(library_content, FILE_PATH_TO_ASCII(library_file)); |
| + if (::testing::Test::HasFatalFailure()) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +bool V8UnitTest::RunJavascriptTestF( |
| + const std::string& testFixture, const std::string& testName) { |
| + had_errors_ = false; |
| + testResult_ok_ = false; |
| + std::string test_js; |
| + if (!ExecuteJavascriptLibraries()) |
| + return false; |
| + |
| + v8::Context::Scope context_scope(context_); |
| + v8::HandleScope handle_scope; |
| + |
| + v8::Handle<v8::Value> functionProperty = |
| + context_->Global()->Get(v8::String::New("runTest")); |
| + EXPECT_FALSE(functionProperty.IsEmpty()); |
| + if (::testing::Test::HasNonfatalFailure()) |
| + return false; |
| + EXPECT_TRUE(functionProperty->IsFunction()); |
| + if (::testing::Test::HasNonfatalFailure()) |
| + return false; |
| + v8::Handle<v8::Function> function = |
| + v8::Handle<v8::Function>::Cast(functionProperty); |
| + |
| + v8::Local<v8::Array> params = v8::Array::New(); |
| + params->Set(0, v8::String::New(testFixture.data(), testFixture.size())); |
| + params->Set(1, v8::String::New(testName.data(), testName.size())); |
| + v8::Handle<v8::Value> args[] = { |
| + v8::Boolean::New(false), |
| + v8::String::New("RUN_TEST_F"), |
| + params |
| + }; |
| + |
| + v8::TryCatch try_catch; |
| + v8::Handle<v8::Value> result = function->Call(context_->Global(), 3, args); |
| + // The test fails if an exception was thrown. |
| + EXPECT_FALSE(result.IsEmpty()); |
| + if (::testing::Test::HasNonfatalFailure()) |
| + return false; |
| + |
| + // Ok if ran successfully, passed tests, and didn't have console errors. |
| + return result->BooleanValue() && testResult_ok_ && !had_errors_; |
| +} |
| + |
| +void V8UnitTest::InitPathsAndLibraries() { |
| + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_)); |
| + test_data_directory_ = test_data_directory_.AppendASCII("webui"); |
| + ASSERT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, |
| + &gen_test_data_directory_)); |
| + |
| + FilePath mockPath; |
| + ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &mockPath)); |
| + mockPath = mockPath.AppendASCII("chrome"); |
| + mockPath = mockPath.AppendASCII("third_party"); |
| + mockPath = mockPath.AppendASCII("mock4js"); |
| + mockPath = mockPath.AppendASCII("mock4js.js"); |
| + AddLibrary(mockPath); |
| + |
| + FilePath testApiPath; |
| + ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &testApiPath)); |
| + testApiPath = testApiPath.AppendASCII("webui"); |
| + testApiPath = testApiPath.AppendASCII("test_api.js"); |
| + AddLibrary(testApiPath); |
| +} |
| + |
| void V8UnitTest::SetUp() { |
| v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
| - global->Set(v8::String::New("log"), |
| - v8::FunctionTemplate::New(&V8UnitTest::Log)); |
| + v8::Handle<v8::String> logString = v8::String::New("log"); |
| + v8::Handle<v8::FunctionTemplate> logFunction = |
| + v8::FunctionTemplate::New(&V8UnitTest::Log); |
| + global->Set(logString, logFunction); |
| + |
| + // Set up chrome object for chrome.send(). |
| + v8::Handle<v8::ObjectTemplate> chrome = v8::ObjectTemplate::New(); |
| + global->Set(v8::String::New("chrome"), chrome); |
| + chrome->Set(v8::String::New("send"), |
| + v8::FunctionTemplate::New(&V8UnitTest::ChromeSend)); |
| + |
| + // Set up console object for console.log(), etc. |
| + v8::Handle<v8::ObjectTemplate> console = v8::ObjectTemplate::New(); |
| + global->Set(v8::String::New("console"), console); |
| + console->Set(logString, logFunction); |
| + console->Set(v8::String::New("info"), logFunction); |
| + console->Set(v8::String::New("warn"), logFunction); |
| + console->Set(v8::String::New("error"), |
| + v8::FunctionTemplate::New(&V8UnitTest::Error)); |
| + |
| context_ = v8::Context::New(NULL, global); |
| } |
| @@ -85,18 +229,29 @@ void V8UnitTest::TestFunction(const std::string& function_name) { |
| // static |
| v8::Handle<v8::Value> V8UnitTest::Log(const v8::Arguments& args) { |
| - std::string message; |
| - bool first = true; |
| - for (int i = 0; i < args.Length(); i++) { |
| - v8::HandleScope handle_scope; |
| - if (first) { |
| - first = false; |
| - } else { |
| - message += " "; |
| - } |
| - v8::String::Utf8Value str(args[i]); |
| - message += *str; |
| + LOG(INFO) << Args2String(args); |
| + return v8::Undefined(); |
| +} |
| + |
| +v8::Handle<v8::Value> V8UnitTest::Error(const v8::Arguments& args) { |
| + had_errors_ = true; |
| + LOG(ERROR) << Args2String(args); |
| + return v8::Undefined(); |
| +} |
| + |
| +v8::Handle<v8::Value> V8UnitTest::ChromeSend(const v8::Arguments& args) { |
|
Sheridan Rawlins
2011/10/30 17:04:21
Need a handle_scope. Done.
|
| + EXPECT_EQ(2, args.Length()); |
| + if (::testing::Test::HasNonfatalFailure()) |
| + return v8::Undefined(); |
| + v8::String::Utf8Value message(args[0]); |
| + v8::Handle<v8::Array> testResult(args[1].As<v8::Array>()); |
| + EXPECT_EQ(2U, testResult->Length()); |
| + if (::testing::Test::HasNonfatalFailure()) |
| + return v8::Undefined(); |
| + testResult_ok_ = testResult->Get(0)->BooleanValue(); |
| + if (!testResult_ok_) { |
| + v8::String::Utf8Value message(testResult->Get(1)); |
| + LOG(ERROR) << *message; |
| } |
| - std::cout << message << "\n"; |
| return v8::Undefined(); |
| } |