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

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

Powered by Google App Engine
This is Rietveld 408576698