1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/shell/webkit_test_runner_bindings.h"
7 #include "base/logging.h"
8 #include "base/string_piece.h"
9 #include "content/public/renderer/render_view.h"
10 #include "content/shell/shell_render_process_observer.h"
11 #include "content/shell/webkit_test_runner.h"
12 #include "grit/shell_resources.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebWorkerInfo.h"
14 #include "ui/base/resource/resource_bundle.h"
16 using WebKit::WebWorkerInfo
;
22 bool g_global_flag
= false;
24 base::StringPiece
GetStringResource(int resource_id
) {
25 return ResourceBundle::GetSharedInstance().GetRawDataResource(
29 v8::Handle
<v8::Value
> Display(const v8::Arguments
& args
) {
30 WebKitTestRunner
* runner
=
31 ShellRenderProcessObserver::GetInstance()->main_test_runner();
33 return v8::Undefined();
36 return v8::Undefined();
39 v8::Handle
<v8::Value
> NotifyDone(const v8::Arguments
& args
) {
40 WebKitTestRunner
* runner
=
41 ShellRenderProcessObserver::GetInstance()->main_test_runner();
43 return v8::Undefined();
46 return v8::Undefined();
49 v8::Handle
<v8::Value
> SetCanOpenWindows(const v8::Arguments
& args
) {
50 WebKitTestRunner
* runner
=
51 ShellRenderProcessObserver::GetInstance()->main_test_runner();
53 return v8::Undefined();
55 runner
->CanOpenWindows();
56 return v8::Undefined();
59 v8::Handle
<v8::Value
> SetDumpAsText(const v8::Arguments
& args
) {
60 WebKitTestRunner
* runner
=
61 ShellRenderProcessObserver::GetInstance()->main_test_runner();
63 return v8::Undefined();
66 return v8::Undefined();
69 v8::Handle
<v8::Value
> SetDumpChildFramesAsText(const v8::Arguments
& args
) {
70 WebKitTestRunner
* runner
=
71 ShellRenderProcessObserver::GetInstance()->main_test_runner();
73 return v8::Undefined();
75 runner
->DumpChildFramesAsText();
76 return v8::Undefined();
79 v8::Handle
<v8::Value
> SetPrinting(const v8::Arguments
& args
) {
80 WebKitTestRunner
* runner
=
81 ShellRenderProcessObserver::GetInstance()->main_test_runner();
83 return v8::Undefined();
85 runner
->SetPrinting();
86 return v8::Undefined();
89 v8::Handle
<v8::Value
> SetShouldStayOnPageAfterHandlingBeforeUnload(
90 const v8::Arguments
& args
) {
91 if (args
.Length() != 1 || !args
[0]->IsBoolean())
92 return v8::Undefined();
94 WebKitTestRunner
* runner
=
95 ShellRenderProcessObserver::GetInstance()->main_test_runner();
97 return v8::Undefined();
99 runner
->SetShouldStayOnPageAfterHandlingBeforeUnload(args
[0]->BooleanValue());
100 return v8::Undefined();
103 v8::Handle
<v8::Value
> SetWaitUntilDone(const v8::Arguments
& args
) {
104 WebKitTestRunner
* runner
=
105 ShellRenderProcessObserver::GetInstance()->main_test_runner();
107 return v8::Undefined();
109 runner
->WaitUntilDone();
110 return v8::Undefined();
113 v8::Handle
<v8::Value
> SetXSSAuditorEnabled(
114 const v8::Arguments
& args
) {
115 if (args
.Length() != 1 || !args
[0]->IsBoolean())
116 return v8::Undefined();
118 WebKitTestRunner
* runner
=
119 ShellRenderProcessObserver::GetInstance()->main_test_runner();
121 return v8::Undefined();
123 runner
->SetXSSAuditorEnabled(args
[0]->BooleanValue());
124 return v8::Undefined();
127 v8::Handle
<v8::Value
> ShowWebInspector(const v8::Arguments
& args
) {
128 WebKitTestRunner
* runner
=
129 ShellRenderProcessObserver::GetInstance()->main_test_runner();
131 return v8::Undefined();
133 runner
->ShowWebInspector();
134 return v8::Undefined();
137 v8::Handle
<v8::Value
> CloseWebInspector(const v8::Arguments
& args
) {
138 WebKitTestRunner
* runner
=
139 ShellRenderProcessObserver::GetInstance()->main_test_runner();
141 return v8::Undefined();
143 runner
->CloseWebInspector();
144 return v8::Undefined();
147 v8::Handle
<v8::Value
> EvaluateInWebInspector(const v8::Arguments
& args
) {
148 if (args
.Length() != 2 || !args
[0]->IsNumber() || !args
[1]->IsString())
149 return v8::Undefined();
151 WebKitTestRunner
* runner
=
152 ShellRenderProcessObserver::GetInstance()->main_test_runner();
154 return v8::Undefined();
156 runner
->EvaluateInWebInspector(args
[0]->Int32Value(),
157 *v8::String::AsciiValue(args
[1]));
158 return v8::Undefined();
161 v8::Handle
<v8::Value
> ExecCommand(const v8::Arguments
& args
) {
162 // This method takes one, two, or three parameters, however, we only care
163 // about the first and third parameter which (if present) need to be strings.
164 // We ignore the second parameter (user interface) since this command
165 // emulates a manual action.
166 if (args
.Length() == 0 || args
.Length() > 3)
167 return v8::Undefined();
169 if (!args
[0]->IsString() || (args
.Length() == 3 && !args
[2]->IsString()))
170 return v8::Undefined();
172 WebKitTestRunner
* runner
=
173 ShellRenderProcessObserver::GetInstance()->main_test_runner();
175 return v8::Undefined();
177 std::string
command(*v8::String::AsciiValue(args
[0]));
179 if (args
.Length() == 3)
180 value
= *v8::String::AsciiValue(args
[2]);
182 runner
->ExecCommand(command
, value
);
183 return v8::Undefined();
186 v8::Handle
<v8::Value
> OverridePreference(const v8::Arguments
& args
) {
187 if (args
.Length() != 2 || !args
[0]->IsString())
188 return v8::Undefined();
190 WebKitTestRunner
* runner
=
191 ShellRenderProcessObserver::GetInstance()->main_test_runner();
193 return v8::Undefined();
195 runner
->OverridePreference(*v8::String::AsciiValue(args
[0]), args
[1]);
196 return v8::Undefined();
199 v8::Handle
<v8::Value
> DumpEditingCallbacks(const v8::Arguments
& args
) {
200 WebKitTestRunner
* runner
=
201 ShellRenderProcessObserver::GetInstance()->main_test_runner();
203 return v8::Undefined();
205 runner
->DumpEditingCallbacks();
206 return v8::Undefined();
209 v8::Handle
<v8::Value
> GetGlobalFlag(const v8::Arguments
& args
) {
210 return v8::Boolean::New(g_global_flag
);
213 v8::Handle
<v8::Value
> SetGlobalFlag(const v8::Arguments
& args
) {
214 if (args
.Length() == 1 && args
[0]->IsBoolean())
215 g_global_flag
= args
[0]->BooleanValue();
216 return v8::Undefined();
219 v8::Handle
<v8::Value
> GetWorkerThreadCount(const v8::Arguments
& args
) {
220 return v8::Integer::NewFromUnsigned(WebWorkerInfo::dedicatedWorkerCount());
223 v8::Handle
<v8::Value
> NotImplemented(const v8::Arguments
& args
) {
224 if (args
.Length() != 2 || !args
[0]->IsString() || !args
[1]->IsString())
225 return v8::Undefined();
227 WebKitTestRunner
* runner
=
228 ShellRenderProcessObserver::GetInstance()->main_test_runner();
230 return v8::Undefined();
232 runner
->NotImplemented(*v8::String::AsciiValue(args
[0]),
233 *v8::String::AsciiValue(args
[1]));
234 return v8::Undefined();
239 WebKitTestRunnerBindings::WebKitTestRunnerBindings()
240 : v8::Extension("webkit_test_runner.js",
242 IDR_CONTENT_SHELL_WEBKIT_TEST_RUNNER_JS
).data(),
243 0, // num dependencies.
244 NULL
, // dependencies array.
246 IDR_CONTENT_SHELL_WEBKIT_TEST_RUNNER_JS
).size()) {
249 WebKitTestRunnerBindings::~WebKitTestRunnerBindings() {
253 void WebKitTestRunnerBindings::Reset() {
254 g_global_flag
= false;
257 v8::Handle
<v8::FunctionTemplate
>
258 WebKitTestRunnerBindings::GetNativeFunction(v8::Handle
<v8::String
> name
) {
259 if (name
->Equals(v8::String::New("Display")))
260 return v8::FunctionTemplate::New(Display
);
261 if (name
->Equals(v8::String::New("NotifyDone")))
262 return v8::FunctionTemplate::New(NotifyDone
);
263 if (name
->Equals(v8::String::New("SetCanOpenWindows")))
264 return v8::FunctionTemplate::New(SetCanOpenWindows
);
265 if (name
->Equals(v8::String::New("SetDumpAsText")))
266 return v8::FunctionTemplate::New(SetDumpAsText
);
267 if (name
->Equals(v8::String::New("SetDumpChildFramesAsText")))
268 return v8::FunctionTemplate::New(SetDumpChildFramesAsText
);
269 if (name
->Equals(v8::String::New("SetPrinting")))
270 return v8::FunctionTemplate::New(SetPrinting
);
271 if (name
->Equals(v8::String::New(
272 "SetShouldStayOnPageAfterHandlingBeforeUnload"))) {
273 return v8::FunctionTemplate::New(
274 SetShouldStayOnPageAfterHandlingBeforeUnload
);
276 if (name
->Equals(v8::String::New("SetWaitUntilDone")))
277 return v8::FunctionTemplate::New(SetWaitUntilDone
);
278 if (name
->Equals(v8::String::New("SetXSSAuditorEnabled")))
279 return v8::FunctionTemplate::New(SetXSSAuditorEnabled
);
280 if (name
->Equals(v8::String::New("GetWorkerThreadCount")))
281 return v8::FunctionTemplate::New(GetWorkerThreadCount
);
282 if (name
->Equals(v8::String::New("ShowWebInspector")))
283 return v8::FunctionTemplate::New(ShowWebInspector
);
284 if (name
->Equals(v8::String::New("CloseWebInspector")))
285 return v8::FunctionTemplate::New(CloseWebInspector
);
286 if (name
->Equals(v8::String::New("EvaluateInWebInspector")))
287 return v8::FunctionTemplate::New(EvaluateInWebInspector
);
288 if (name
->Equals(v8::String::New("ExecCommand")))
289 return v8::FunctionTemplate::New(ExecCommand
);
290 if (name
->Equals(v8::String::New("OverridePreference")))
291 return v8::FunctionTemplate::New(OverridePreference
);
292 if (name
->Equals(v8::String::New("DumpEditingCallbacks")))
293 return v8::FunctionTemplate::New(DumpEditingCallbacks
);
294 if (name
->Equals(v8::String::New("GetGlobalFlag")))
295 return v8::FunctionTemplate::New(GetGlobalFlag
);
296 if (name
->Equals(v8::String::New("SetGlobalFlag")))
297 return v8::FunctionTemplate::New(SetGlobalFlag
);
298 if (name
->Equals(v8::String::New("NotImplemented")))
299 return v8::FunctionTemplate::New(NotImplemented
);
302 return v8::FunctionTemplate::New();
305 } // namespace content