Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: chrome/test/base/v8_unit_test.cc

Issue 8418015: Allow javascript unit tests using webui test_api framework. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Don't double log errors (console.error). Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 #ifdef OS_WIN
15 #include "base/string_util.h"
16 #define FILE_PATH_TO_ASCII(FP) WideToASCII((FP).value())
17 #else
18 #define FILE_PATH_TO_ASCII(FP) ((FP).value())
19 #endif // OS_WIN
20
21 namespace {
22 std::string Args2String(const v8::Arguments& args) {
23 std::string message;
24 bool first = true;
25 for (int i = 0; i < args.Length(); i++) {
26 v8::HandleScope handle_scope;
27 if (first) {
28 first = false;
29 } else {
30 message += " ";
31 }
32 v8::String::Utf8Value str(args[i]);
33 message += *str;
34 }
35 return message;
36 }
37
38 } // namespace
39
40 bool V8UnitTest::had_errors_ = false;
41 bool V8UnitTest::testResult_ok_ = false;
42
43 V8UnitTest::V8UnitTest() {
44 InitPathsAndLibraries();
45 }
11 46
12 V8UnitTest::~V8UnitTest() {} 47 V8UnitTest::~V8UnitTest() {}
13 48
49 void V8UnitTest::AddLibrary(const FilePath& library_path) {
50 user_libraries_.push_back(library_path);
51 }
52
53 bool V8UnitTest::ExecuteJavascriptLibraries() {
54 std::string utf8_content;
55 std::vector<FilePath>::iterator user_libraries_iterator;
56 for (user_libraries_iterator = user_libraries_.begin();
57 user_libraries_iterator != user_libraries_.end();
58 ++user_libraries_iterator) {
59 std::string library_content;
60 FilePath library_file(*user_libraries_iterator);
61 if (!user_libraries_iterator->IsAbsolute()) {
62 FilePath gen_file = gen_test_data_directory_.Append(library_file);
63 library_file = file_util::PathExists(gen_file) ? gen_file :
64 test_data_directory_.Append(*user_libraries_iterator);
65 }
66 if (!file_util::ReadFileToString(library_file, &library_content)) {
67 ADD_FAILURE() << library_file.value();
68 return false;
69 }
70 ExecuteScriptInContext(library_content, FILE_PATH_TO_ASCII(library_file));
71 if (::testing::Test::HasFatalFailure())
72 return false;
73 }
74 return true;
75 }
76
77 bool V8UnitTest::RunJavascriptTestF(
78 const std::string& testFixture, const std::string& testName) {
79 had_errors_ = false;
80 testResult_ok_ = false;
81 std::string test_js;
82 if (!ExecuteJavascriptLibraries())
83 return false;
84
85 v8::Context::Scope context_scope(context_);
86 v8::HandleScope handle_scope;
87
88 v8::Handle<v8::Value> functionProperty =
89 context_->Global()->Get(v8::String::New("runTest"));
90 EXPECT_FALSE(functionProperty.IsEmpty());
91 if (::testing::Test::HasNonfatalFailure())
92 return false;
93 EXPECT_TRUE(functionProperty->IsFunction());
94 if (::testing::Test::HasNonfatalFailure())
95 return false;
96 v8::Handle<v8::Function> function =
97 v8::Handle<v8::Function>::Cast(functionProperty);
98
99 v8::Local<v8::Array> params = v8::Array::New();
100 params->Set(0, v8::String::New(testFixture.data(), testFixture.size()));
101 params->Set(1, v8::String::New(testName.data(), testName.size()));
102 v8::Handle<v8::Value> args[] = {
103 v8::Boolean::New(false),
104 v8::String::New("RUN_TEST_F"),
105 params
106 };
107
108 v8::TryCatch try_catch;
109 v8::Handle<v8::Value> result = function->Call(context_->Global(), 3, args);
110 // The test fails if an exception was thrown.
111 EXPECT_FALSE(result.IsEmpty());
112 if (::testing::Test::HasNonfatalFailure())
113 return false;
114
115 // Ok if ran successfully, passed tests, and didn't have console errors.
116 return result->BooleanValue() && testResult_ok_ && !had_errors_;
117 }
118
119 void V8UnitTest::InitPathsAndLibraries() {
120 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_data_directory_));
121 test_data_directory_ = test_data_directory_.AppendASCII("webui");
122 ASSERT_TRUE(PathService::Get(chrome::DIR_GEN_TEST_DATA,
123 &gen_test_data_directory_));
124
125 FilePath mockPath;
126 ASSERT_TRUE(PathService::Get(base::DIR_SOURCE_ROOT, &mockPath));
127 mockPath = mockPath.AppendASCII("chrome");
128 mockPath = mockPath.AppendASCII("third_party");
129 mockPath = mockPath.AppendASCII("mock4js");
130 mockPath = mockPath.AppendASCII("mock4js.js");
131 AddLibrary(mockPath);
132
133 FilePath testApiPath;
134 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &testApiPath));
135 testApiPath = testApiPath.AppendASCII("webui");
136 testApiPath = testApiPath.AppendASCII("test_api.js");
137 AddLibrary(testApiPath);
138 }
139
14 void V8UnitTest::SetUp() { 140 void V8UnitTest::SetUp() {
15 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 141 v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
16 global->Set(v8::String::New("log"), 142 v8::Handle<v8::String> logString = v8::String::New("log");
17 v8::FunctionTemplate::New(&V8UnitTest::Log)); 143 v8::Handle<v8::FunctionTemplate> logFunction =
144 v8::FunctionTemplate::New(&V8UnitTest::Log);
145 global->Set(logString, logFunction);
146
147 // Set up chrome object for chrome.send().
148 v8::Handle<v8::ObjectTemplate> chrome = v8::ObjectTemplate::New();
149 global->Set(v8::String::New("chrome"), chrome);
150 chrome->Set(v8::String::New("send"),
151 v8::FunctionTemplate::New(&V8UnitTest::ChromeSend));
152
153 // Set up console object for console.log(), etc.
154 v8::Handle<v8::ObjectTemplate> console = v8::ObjectTemplate::New();
155 global->Set(v8::String::New("console"), console);
156 console->Set(logString, logFunction);
157 console->Set(v8::String::New("info"), logFunction);
158 console->Set(v8::String::New("warn"), logFunction);
159 console->Set(v8::String::New("error"),
160 v8::FunctionTemplate::New(&V8UnitTest::Error));
161
18 context_ = v8::Context::New(NULL, global); 162 context_ = v8::Context::New(NULL, global);
19 } 163 }
20 164
21 void V8UnitTest::SetGlobalStringVar(const std::string& var_name, 165 void V8UnitTest::SetGlobalStringVar(const std::string& var_name,
22 const std::string& value) { 166 const std::string& value) {
23 v8::Context::Scope context_scope(context_); 167 v8::Context::Scope context_scope(context_);
24 context_->Global()->Set(v8::String::New(var_name.c_str(), var_name.length()), 168 context_->Global()->Set(v8::String::New(var_name.c_str(), var_name.length()),
25 v8::String::New(value.c_str(), value.length())); 169 v8::String::New(value.c_str(), value.length()));
26 } 170 }
27 171
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 222
79 v8::TryCatch try_catch; 223 v8::TryCatch try_catch;
80 v8::Handle<v8::Value> result = function->Call(context_->Global(), 0, NULL); 224 v8::Handle<v8::Value> result = function->Call(context_->Global(), 0, NULL);
81 // The test fails if an exception was thrown. 225 // The test fails if an exception was thrown.
82 if (result.IsEmpty()) 226 if (result.IsEmpty())
83 FAIL() << ExceptionToString(try_catch); 227 FAIL() << ExceptionToString(try_catch);
84 } 228 }
85 229
86 // static 230 // static
87 v8::Handle<v8::Value> V8UnitTest::Log(const v8::Arguments& args) { 231 v8::Handle<v8::Value> V8UnitTest::Log(const v8::Arguments& args) {
88 std::string message; 232 LOG(INFO) << Args2String(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(); 233 return v8::Undefined();
102 } 234 }
235
236 v8::Handle<v8::Value> V8UnitTest::Error(const v8::Arguments& args) {
237 had_errors_ = true;
238 LOG(ERROR) << Args2String(args);
239 return v8::Undefined();
240 }
241
242 v8::Handle<v8::Value> V8UnitTest::ChromeSend(const v8::Arguments& args) {
243 EXPECT_EQ(2, args.Length());
244 if (::testing::Test::HasNonfatalFailure())
245 return v8::Undefined();
246 v8::String::Utf8Value message(args[0]);
247 v8::Handle<v8::Array> testResult(args[1].As<v8::Array>());
248 EXPECT_EQ(2U, testResult->Length());
249 if (::testing::Test::HasNonfatalFailure())
250 return v8::Undefined();
251 testResult_ok_ = testResult->Get(0)->BooleanValue();
252 if (!testResult_ok_) {
253 v8::String::Utf8Value message(testResult->Get(1));
254 LOG(ERROR) << *message;
255 }
256 return v8::Undefined();
257 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698