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 #include "chrome/test/base/v8_unit_test.h" | 5 #include "chrome/test/base/v8_unit_test.h" |
6 | 6 |
| 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/path_service.h" |
7 #include "base/string_piece.h" | 10 #include "base/string_piece.h" |
8 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
| 12 #include "chrome/common/chrome_paths.h" |
9 | 13 |
10 V8UnitTest::V8UnitTest() {} | 14 namespace { |
| 15 |
| 16 // |args| are passed through the various JavaScript logging functions such as |
| 17 // console.log. Returns a string appropriate for logging with LOG(severity). |
| 18 std::string LogArgs2String(const v8::Arguments& args) { |
| 19 std::string message; |
| 20 bool first = true; |
| 21 for (int i = 0; i < args.Length(); i++) { |
| 22 v8::HandleScope handle_scope; |
| 23 if (first) |
| 24 first = false; |
| 25 else |
| 26 message += " "; |
| 27 |
| 28 v8::String::Utf8Value str(args[i]); |
| 29 message += *str; |
| 30 } |
| 31 return message; |
| 32 } |
| 33 |
| 34 // Whether errors were seen. |
| 35 bool had_errors = false; |
| 36 |
| 37 // testDone results. |
| 38 bool testResult_ok = false; |
| 39 |
| 40 // Location of test data (currently test/data/webui). |
| 41 FilePath test_data_directory; |
| 42 |
| 43 // Location of generated test data (<(PROGRAM_DIR)/test_data). |
| 44 FilePath gen_test_data_directory; |
| 45 |
| 46 } // namespace |
| 47 |
| 48 V8UnitTest::V8UnitTest() { |
| 49 InitPathsAndLibraries(); |
| 50 } |
11 | 51 |
12 V8UnitTest::~V8UnitTest() {} | 52 V8UnitTest::~V8UnitTest() {} |
13 | 53 |
| 54 void V8UnitTest::AddLibrary(const FilePath& library_path) { |
| 55 user_libraries_.push_back(library_path); |
| 56 } |
| 57 |
| 58 bool V8UnitTest::ExecuteJavascriptLibraries() { |
| 59 std::string utf8_content; |
| 60 for (std::vector<FilePath>::iterator user_libraries_iterator = |
| 61 user_libraries_.begin(); |
| 62 user_libraries_iterator != user_libraries_.end(); |
| 63 ++user_libraries_iterator) { |
| 64 std::string library_content; |
| 65 FilePath library_file(*user_libraries_iterator); |
| 66 if (!user_libraries_iterator->IsAbsolute()) { |
| 67 FilePath gen_file = gen_test_data_directory.Append(library_file); |
| 68 library_file = file_util::PathExists(gen_file) ? gen_file : |
| 69 test_data_directory.Append(*user_libraries_iterator); |
| 70 } |
| 71 if (!file_util::ReadFileToString(library_file, &library_content)) { |
| 72 ADD_FAILURE() << library_file.value(); |
| 73 return false; |
| 74 } |
| 75 ExecuteScriptInContext(library_content, library_file.MaybeAsASCII()); |
| 76 if (::testing::Test::HasFatalFailure()) |
| 77 return false; |
| 78 } |
| 79 return true; |
| 80 } |
| 81 |
| 82 bool V8UnitTest::RunJavascriptTestF( |
| 83 const std::string& testFixture, const std::string& testName) { |
| 84 had_errors = false; |
| 85 testResult_ok = false; |
| 86 std::string test_js; |
| 87 if (!ExecuteJavascriptLibraries()) |
| 88 return false; |
| 89 |
| 90 v8::Context::Scope context_scope(context_); |
| 91 v8::HandleScope handle_scope; |
| 92 |
| 93 v8::Handle<v8::Value> functionProperty = |
| 94 context_->Global()->Get(v8::String::New("runTest")); |
| 95 EXPECT_FALSE(functionProperty.IsEmpty()); |
| 96 if (::testing::Test::HasNonfatalFailure()) |
| 97 return false; |
| 98 EXPECT_TRUE(functionProperty->IsFunction()); |
| 99 if (::testing::Test::HasNonfatalFailure()) |
| 100 return false; |
| 101 v8::Handle<v8::Function> function = |
| 102 v8::Handle<v8::Function>::Cast(functionProperty); |
| 103 |
| 104 v8::Local<v8::Array> params = v8::Array::New(); |
| 105 params->Set(0, v8::String::New(testFixture.data(), testFixture.size())); |
| 106 params->Set(1, v8::String::New(testName.data(), testName.size())); |
| 107 v8::Handle<v8::Value> args[] = { |
| 108 v8::Boolean::New(false), |
| 109 v8::String::New("RUN_TEST_F"), |
| 110 params |
| 111 }; |
| 112 |
| 113 v8::TryCatch try_catch; |
| 114 v8::Handle<v8::Value> result = function->Call(context_->Global(), 3, args); |
| 115 // The test fails if an exception was thrown. |
| 116 EXPECT_FALSE(result.IsEmpty()); |
| 117 if (::testing::Test::HasNonfatalFailure()) |
| 118 return false; |
| 119 |
| 120 // Ok if ran successfully, passed tests, and didn't have console errors. |
| 121 return result->BooleanValue() && testResult_ok && !had_errors; |
| 122 } |
| 123 |
| 124 void V8UnitTest::InitPathsAndLibraries() { |
| 125 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory)); |
| 126 test_data_directory = test_data_directory.AppendASCII("webui"); |
| 127 ASSERT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA, |
| 128 &gen_test_data_directory)); |
| 129 |
| 130 FilePath mockPath; |
| 131 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &mockPath)); |
| 132 mockPath = mockPath.AppendASCII("chrome"); |
| 133 mockPath = mockPath.AppendASCII("third_party"); |
| 134 mockPath = mockPath.AppendASCII("mock4js"); |
| 135 mockPath = mockPath.AppendASCII("mock4js.js"); |
| 136 AddLibrary(mockPath); |
| 137 |
| 138 FilePath testApiPath; |
| 139 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &testApiPath)); |
| 140 testApiPath = testApiPath.AppendASCII("webui"); |
| 141 testApiPath = testApiPath.AppendASCII("test_api.js"); |
| 142 AddLibrary(testApiPath); |
| 143 } |
| 144 |
14 void V8UnitTest::SetUp() { | 145 void V8UnitTest::SetUp() { |
15 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 146 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
16 global->Set(v8::String::New("log"), | 147 v8::Handle<v8::String> logString = v8::String::New("log"); |
17 v8::FunctionTemplate::New(&V8UnitTest::Log)); | 148 v8::Handle<v8::FunctionTemplate> logFunction = |
| 149 v8::FunctionTemplate::New(&V8UnitTest::Log); |
| 150 global->Set(logString, logFunction); |
| 151 |
| 152 // Set up chrome object for chrome.send(). |
| 153 v8::Handle<v8::ObjectTemplate> chrome = v8::ObjectTemplate::New(); |
| 154 global->Set(v8::String::New("chrome"), chrome); |
| 155 chrome->Set(v8::String::New("send"), |
| 156 v8::FunctionTemplate::New(&V8UnitTest::ChromeSend)); |
| 157 |
| 158 // Set up console object for console.log(), etc. |
| 159 v8::Handle<v8::ObjectTemplate> console = v8::ObjectTemplate::New(); |
| 160 global->Set(v8::String::New("console"), console); |
| 161 console->Set(logString, logFunction); |
| 162 console->Set(v8::String::New("info"), logFunction); |
| 163 console->Set(v8::String::New("warn"), logFunction); |
| 164 console->Set(v8::String::New("error"), |
| 165 v8::FunctionTemplate::New(&V8UnitTest::Error)); |
| 166 |
18 context_ = v8::Context::New(NULL, global); | 167 context_ = v8::Context::New(NULL, global); |
19 } | 168 } |
20 | 169 |
21 void V8UnitTest::SetGlobalStringVar(const std::string& var_name, | 170 void V8UnitTest::SetGlobalStringVar(const std::string& var_name, |
22 const std::string& value) { | 171 const std::string& value) { |
23 v8::Context::Scope context_scope(context_); | 172 v8::Context::Scope context_scope(context_); |
24 context_->Global()->Set(v8::String::New(var_name.c_str(), var_name.length()), | 173 context_->Global()->Set(v8::String::New(var_name.c_str(), var_name.length()), |
25 v8::String::New(value.c_str(), value.length())); | 174 v8::String::New(value.c_str(), value.length())); |
26 } | 175 } |
27 | 176 |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 | 227 |
79 v8::TryCatch try_catch; | 228 v8::TryCatch try_catch; |
80 v8::Handle<v8::Value> result = function->Call(context_->Global(), 0, NULL); | 229 v8::Handle<v8::Value> result = function->Call(context_->Global(), 0, NULL); |
81 // The test fails if an exception was thrown. | 230 // The test fails if an exception was thrown. |
82 if (result.IsEmpty()) | 231 if (result.IsEmpty()) |
83 FAIL() << ExceptionToString(try_catch); | 232 FAIL() << ExceptionToString(try_catch); |
84 } | 233 } |
85 | 234 |
86 // static | 235 // static |
87 v8::Handle<v8::Value> V8UnitTest::Log(const v8::Arguments& args) { | 236 v8::Handle<v8::Value> V8UnitTest::Log(const v8::Arguments& args) { |
88 std::string message; | 237 LOG(INFO) << LogArgs2String(args); |
89 bool first = true; | |
90 for (int i = 0; i < args.Length(); i++) { | |
91 v8::HandleScope handle_scope; | |
92 if (first) { | |
93 first = false; | |
94 } else { | |
95 message += " "; | |
96 } | |
97 v8::String::Utf8Value str(args[i]); | |
98 message += *str; | |
99 } | |
100 std::cout << message << "\n"; | |
101 return v8::Undefined(); | 238 return v8::Undefined(); |
102 } | 239 } |
| 240 |
| 241 v8::Handle<v8::Value> V8UnitTest::Error(const v8::Arguments& args) { |
| 242 had_errors = true; |
| 243 LOG(ERROR) << LogArgs2String(args); |
| 244 return v8::Undefined(); |
| 245 } |
| 246 |
| 247 v8::Handle<v8::Value> V8UnitTest::ChromeSend(const v8::Arguments& args) { |
| 248 v8::HandleScope handle_scope; |
| 249 EXPECT_EQ(2, args.Length()); |
| 250 if (::testing::Test::HasNonfatalFailure()) |
| 251 return v8::Undefined(); |
| 252 v8::String::Utf8Value message(args[0]); |
| 253 v8::Handle<v8::Array> testResult(args[1].As<v8::Array>()); |
| 254 EXPECT_EQ(2U, testResult->Length()); |
| 255 if (::testing::Test::HasNonfatalFailure()) |
| 256 return v8::Undefined(); |
| 257 testResult_ok = testResult->Get(0)->BooleanValue(); |
| 258 if (!testResult_ok) { |
| 259 v8::String::Utf8Value message(testResult->Get(1)); |
| 260 LOG(ERROR) << *message; |
| 261 } |
| 262 return v8::Undefined(); |
| 263 } |
OLD | NEW |