Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / content / shell / renderer / test_runner / test_runner.cc
1 // Copyright 2014 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.
4
5 #include "content/shell/renderer/test_runner/test_runner.h"
6
7 #include <limits>
8
9 #include "content/shell/common/test_runner/WebPreferences.h"
10 #include "content/shell/renderer/test_runner/MockWebSpeechInputController.h"
11 #include "content/shell/renderer/test_runner/MockWebSpeechRecognizer.h"
12 #include "content/shell/renderer/test_runner/TestInterfaces.h"
13 #include "content/shell/renderer/test_runner/WebPermissions.h"
14 #include "content/shell/renderer/test_runner/WebTestDelegate.h"
15 #include "content/shell/renderer/test_runner/WebTestProxy.h"
16 #include "content/shell/renderer/test_runner/notification_presenter.h"
17 #include "gin/arguments.h"
18 #include "gin/array_buffer.h"
19 #include "gin/handle.h"
20 #include "gin/object_template_builder.h"
21 #include "gin/wrappable.h"
22 #include "third_party/WebKit/public/platform/WebCanvas.h"
23 #include "third_party/WebKit/public/platform/WebData.h"
24 #include "third_party/WebKit/public/platform/WebDeviceMotionData.h"
25 #include "third_party/WebKit/public/platform/WebDeviceOrientationData.h"
26 #include "third_party/WebKit/public/platform/WebPoint.h"
27 #include "third_party/WebKit/public/platform/WebURLResponse.h"
28 #include "third_party/WebKit/public/web/WebBindings.h"
29 #include "third_party/WebKit/public/web/WebDataSource.h"
30 #include "third_party/WebKit/public/web/WebDocument.h"
31 #include "third_party/WebKit/public/web/WebFindOptions.h"
32 #include "third_party/WebKit/public/web/WebFrame.h"
33 #include "third_party/WebKit/public/web/WebInputElement.h"
34 #include "third_party/WebKit/public/web/WebKit.h"
35 #include "third_party/WebKit/public/web/WebMIDIClientMock.h"
36 #include "third_party/WebKit/public/web/WebPageOverlay.h"
37 #include "third_party/WebKit/public/web/WebScriptSource.h"
38 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
39 #include "third_party/WebKit/public/web/WebSerializedScriptValue.h"
40 #include "third_party/WebKit/public/web/WebSettings.h"
41 #include "third_party/WebKit/public/web/WebSurroundingText.h"
42 #include "third_party/WebKit/public/web/WebView.h"
43 #include "third_party/skia/include/core/SkCanvas.h"
44
45 #if defined(__linux__) || defined(ANDROID)
46 #include "third_party/WebKit/public/web/linux/WebFontRendering.h"
47 #endif
48
49 using namespace blink;
50 using namespace WebTestRunner;
51
52 namespace {
53
54 WebString V8StringToWebString(v8::Handle<v8::String> v8_str) {
55   int length = v8_str->Utf8Length() + 1;
56   scoped_ptr<char[]> chars(new char[length]);
57   v8_str->WriteUtf8(chars.get(), length);
58   return WebString::fromUTF8(chars.get());
59 }
60
61 class HostMethodTask :
62       public ::WebTestRunner::WebMethodTask<content::TestRunner> {
63  public:
64   typedef void (content::TestRunner::*CallbackMethodType)();
65   HostMethodTask(content::TestRunner* object, CallbackMethodType callback)
66       : WebMethodTask<content::TestRunner>(object), callback_(callback) {}
67
68   virtual void runIfValid() OVERRIDE {
69     (m_object->*callback_)();
70   }
71
72  private:
73   CallbackMethodType callback_;
74 };
75
76 }  // namespace
77
78 namespace content {
79
80 class InvokeCallbackTask : public WebMethodTask<content::TestRunner> {
81  public:
82   InvokeCallbackTask(content::TestRunner* object,
83                      v8::Handle<v8::Function> callback)
84       : WebMethodTask<content::TestRunner>(object),
85         callback_(blink::mainThreadIsolate(), callback) {}
86
87   virtual void runIfValid() OVERRIDE {
88     v8::Isolate* isolate = blink::mainThreadIsolate();
89     v8::HandleScope handle_scope(isolate);
90     WebFrame* frame = m_object->web_view_->mainFrame();
91
92     v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
93     if (context.IsEmpty())
94       return;
95
96     v8::Context::Scope context_scope(context);
97
98     frame->callFunctionEvenIfScriptDisabled(
99         v8::Local<v8::Function>::New(isolate, callback_),
100         context->Global(),
101         0,
102         NULL);
103   }
104
105  private:
106   v8::Persistent<v8::Function> callback_;
107 };
108
109 class TestRunnerBindings : public gin::Wrappable<TestRunnerBindings> {
110  public:
111   static gin::WrapperInfo kWrapperInfo;
112
113   static void Install(base::WeakPtr<TestRunner> controller,
114                       WebFrame* frame);
115
116  private:
117   explicit TestRunnerBindings(
118       base::WeakPtr<TestRunner> controller);
119   virtual ~TestRunnerBindings();
120
121   // gin::Wrappable:
122   virtual gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
123       v8::Isolate* isolate) OVERRIDE;
124
125   void NotifyDone();
126   void WaitUntilDone();
127   void QueueBackNavigation(int how_far_back);
128   void QueueForwardNavigation(int how_far_forward);
129   void QueueReload();
130   void QueueLoadingScript(const std::string& script);
131   void QueueNonLoadingScript(const std::string& script);
132   void QueueLoad(gin::Arguments* args);
133   void QueueLoadHTMLString(gin::Arguments* args);
134   void SetCustomPolicyDelegate(gin::Arguments* args);
135   void WaitForPolicyDelegate();
136   int WindowCount();
137   void SetCloseRemainingWindowsWhenComplete(gin::Arguments* args);
138   void ResetTestHelperControllers();
139   void SetTabKeyCyclesThroughElements(bool tab_key_cycles_through_elements);
140   void ExecCommand(gin::Arguments* args);
141   bool IsCommandEnabled(const std::string& command);
142   bool CallShouldCloseOnWebView();
143   void SetDomainRelaxationForbiddenForURLScheme(bool forbidden,
144                                                 const std::string& scheme);
145   v8::Handle<v8::Value> EvaluateScriptInIsolatedWorldAndReturnValue(
146       int world_id, const std::string& script);
147   void EvaluateScriptInIsolatedWorld(int world_id, const std::string& script);
148   void SetIsolatedWorldSecurityOrigin(int world_id,
149                                       v8::Handle<v8::Value> origin);
150   void SetIsolatedWorldContentSecurityPolicy(int world_id,
151                                              const std::string& policy);
152   void AddOriginAccessWhitelistEntry(const std::string& source_origin,
153                                      const std::string& destination_protocol,
154                                      const std::string& destination_host,
155                                      bool allow_destination_subdomains);
156   void RemoveOriginAccessWhitelistEntry(const std::string& source_origin,
157                                         const std::string& destination_protocol,
158                                         const std::string& destination_host,
159                                         bool allow_destination_subdomains);
160   bool HasCustomPageSizeStyle(int page_index);
161   void ForceRedSelectionColors();
162   void InjectStyleSheet(const std::string& source_code, bool all_frames);
163   bool FindString(const std::string& search_text,
164                   const std::vector<std::string>& options_array);
165   std::string SelectionAsMarkup();
166   void SetTextSubpixelPositioning(bool value);
167   void SetPageVisibility(const std::string& new_visibility);
168   void SetTextDirection(const std::string& direction_name);
169   void UseUnfortunateSynchronousResizeMode();
170   bool EnableAutoResizeMode(int min_width,
171                             int min_height,
172                             int max_width,
173                             int max_height);
174   bool DisableAutoResizeMode(int new_width, int new_height);
175   void SetMockDeviceMotion(gin::Arguments* args);
176   void SetMockDeviceOrientation(gin::Arguments* args);
177   void SetMockScreenOrientation(const std::string& orientation);
178   void DidAcquirePointerLock();
179   void DidNotAcquirePointerLock();
180   void DidLosePointerLock();
181   void SetPointerLockWillFailSynchronously();
182   void SetPointerLockWillRespondAsynchronously();
183   void SetPopupBlockingEnabled(bool block_popups);
184   void SetJavaScriptCanAccessClipboard(bool can_access);
185   void SetXSSAuditorEnabled(bool enabled);
186   void SetAllowUniversalAccessFromFileURLs(bool allow);
187   void SetAllowFileAccessFromFileURLs(bool allow);
188   void OverridePreference(const std::string key, v8::Handle<v8::Value> value);
189   void SetPluginsEnabled(bool enabled);
190   void DumpEditingCallbacks();
191   void DumpAsText();
192   void DumpAsTextWithPixelResults();
193   void DumpChildFrameScrollPositions();
194   void DumpChildFramesAsText();
195   void DumpIconChanges();
196   void SetAudioData(const gin::ArrayBufferView& view);
197   void DumpFrameLoadCallbacks();
198   void DumpPingLoaderCallbacks();
199   void DumpUserGestureInFrameLoadCallbacks();
200   void DumpTitleChanges();
201   void DumpCreateView();
202   void SetCanOpenWindows();
203   void DumpResourceLoadCallbacks();
204   void DumpResourceRequestCallbacks();
205   void DumpResourceResponseMIMETypes();
206   void SetImagesAllowed(bool allowed);
207   void SetScriptsAllowed(bool allowed);
208   void SetStorageAllowed(bool allowed);
209   void SetPluginsAllowed(bool allowed);
210   void SetAllowDisplayOfInsecureContent(bool allowed);
211   void SetAllowRunningOfInsecureContent(bool allowed);
212   void DumpPermissionClientCallbacks();
213   void DumpWindowStatusChanges();
214   void DumpProgressFinishedCallback();
215   void DumpSpellCheckCallbacks();
216   void DumpBackForwardList();
217   void DumpSelectionRect();
218   void TestRepaint();
219   void RepaintSweepHorizontally();
220   void SetPrinting();
221   void SetShouldStayOnPageAfterHandlingBeforeUnload(bool value);
222   void SetWillSendRequestClearHeader(const std::string& header);
223   void DumpResourceRequestPriorities();
224   void SetUseMockTheme(bool use);
225   void WaitUntilExternalURLLoad();
226   void ShowWebInspector(gin::Arguments* args);
227   void CloseWebInspector();
228   bool IsChooserShown();
229   void EvaluateInWebInspector(int call_id, const std::string& script);
230   void ClearAllDatabases();
231   void SetDatabaseQuota(int quota);
232   void SetAlwaysAcceptCookies(bool accept);
233   void SetWindowIsKey(bool value);
234   std::string PathToLocalResource(const std::string& path);
235   void SetBackingScaleFactor(double value, v8::Handle<v8::Function> callback);
236   void SetPOSIXLocale(const std::string& locale);
237   void SetMIDIAccessorResult(bool result);
238   void SetMIDISysexPermission(bool value);
239   void GrantWebNotificationPermission(gin::Arguments* args);
240   bool SimulateWebNotificationClick(const std::string& value);
241   void AddMockSpeechInputResult(const std::string& result,
242                                 double confidence,
243                                 const std::string& language);
244   void SetMockSpeechInputDumpRect(bool value);
245   void AddMockSpeechRecognitionResult(const std::string& transcript,
246                                       double confidence);
247   void SetMockSpeechRecognitionError(const std::string& error,
248                                      const std::string& message);
249   bool WasMockSpeechRecognitionAborted();
250   void AddWebPageOverlay();
251   void RemoveWebPageOverlay();
252   void Display();
253   void DisplayInvalidatedRegion();
254
255   bool GlobalFlag();
256   void SetGlobalFlag(bool value);
257   std::string PlatformName();
258   std::string TooltipText();
259   bool DisableNotifyDone();
260   int WebHistoryItemCount();
261   bool InterceptPostMessage();
262   void SetInterceptPostMessage(bool value);
263
264   void NotImplemented(const gin::Arguments& args);
265
266   base::WeakPtr<TestRunner> runner_;
267
268   DISALLOW_COPY_AND_ASSIGN(TestRunnerBindings);
269 };
270
271 gin::WrapperInfo TestRunnerBindings::kWrapperInfo = {
272     gin::kEmbedderNativeGin};
273
274 // static
275 void TestRunnerBindings::Install(base::WeakPtr<TestRunner> runner,
276                                  WebFrame* frame) {
277   v8::Isolate* isolate = blink::mainThreadIsolate();
278   v8::HandleScope handle_scope(isolate);
279   v8::Handle<v8::Context> context = frame->mainWorldScriptContext();
280   if (context.IsEmpty())
281     return;
282
283   v8::Context::Scope context_scope(context);
284
285   gin::Handle<TestRunnerBindings> bindings =
286       gin::CreateHandle(isolate, new TestRunnerBindings(runner));
287   v8::Handle<v8::Object> global = context->Global();
288   v8::Handle<v8::Value> v8_bindings = bindings.ToV8();
289   global->Set(gin::StringToV8(isolate, "testRunner"), v8_bindings);
290   global->Set(gin::StringToV8(isolate, "layoutTestController"), v8_bindings);
291 }
292
293 TestRunnerBindings::TestRunnerBindings(base::WeakPtr<TestRunner> runner)
294     : runner_(runner) {}
295
296 TestRunnerBindings::~TestRunnerBindings() {}
297
298 gin::ObjectTemplateBuilder TestRunnerBindings::GetObjectTemplateBuilder(
299     v8::Isolate* isolate) {
300   return gin::Wrappable<TestRunnerBindings>::GetObjectTemplateBuilder(
301       isolate)
302       // Methods controlling test execution.
303       .SetMethod("notifyDone", &TestRunnerBindings::NotifyDone)
304       .SetMethod("waitUntilDone", &TestRunnerBindings::WaitUntilDone)
305       .SetMethod("queueBackNavigation",
306                  &TestRunnerBindings::QueueBackNavigation)
307       .SetMethod("queueForwardNavigation",
308                  &TestRunnerBindings::QueueForwardNavigation)
309       .SetMethod("queueReload", &TestRunnerBindings::QueueReload)
310       .SetMethod("queueLoadingScript", &TestRunnerBindings::QueueLoadingScript)
311       .SetMethod("queueNonLoadingScript",
312                  &TestRunnerBindings::QueueNonLoadingScript)
313       .SetMethod("queueLoad", &TestRunnerBindings::QueueLoad)
314       .SetMethod("queueLoadHTMLString",
315                  &TestRunnerBindings::QueueLoadHTMLString)
316       .SetMethod("setCustomPolicyDelegate",
317                  &TestRunnerBindings::SetCustomPolicyDelegate)
318       .SetMethod("waitForPolicyDelegate",
319                  &TestRunnerBindings::WaitForPolicyDelegate)
320       .SetMethod("windowCount", &TestRunnerBindings::WindowCount)
321       .SetMethod("setCloseRemainingWindowsWhenComplete",
322                  &TestRunnerBindings::SetCloseRemainingWindowsWhenComplete)
323       .SetMethod("resetTestHelperControllers",
324                  &TestRunnerBindings::ResetTestHelperControllers)
325       .SetMethod("setTabKeyCyclesThroughElements",
326                  &TestRunnerBindings::SetTabKeyCyclesThroughElements)
327       .SetMethod("execCommand", &TestRunnerBindings::ExecCommand)
328       .SetMethod("isCommandEnabled", &TestRunnerBindings::IsCommandEnabled)
329       .SetMethod("callShouldCloseOnWebView",
330                  &TestRunnerBindings::CallShouldCloseOnWebView)
331       .SetMethod("setDomainRelaxationForbiddenForURLScheme",
332                  &TestRunnerBindings::SetDomainRelaxationForbiddenForURLScheme)
333       .SetMethod("evaluateScriptInIsolatedWorldAndReturnValue",
334                  &TestRunnerBindings::
335                      EvaluateScriptInIsolatedWorldAndReturnValue)
336       .SetMethod("evaluateScriptInIsolatedWorld",
337                  &TestRunnerBindings::EvaluateScriptInIsolatedWorld)
338       .SetMethod("setIsolatedWorldSecurityOrigin",
339                  &TestRunnerBindings::SetIsolatedWorldSecurityOrigin)
340       .SetMethod("setIsolatedWorldContentSecurityPolicy",
341                  &TestRunnerBindings::SetIsolatedWorldContentSecurityPolicy)
342       .SetMethod("addOriginAccessWhitelistEntry",
343                  &TestRunnerBindings::AddOriginAccessWhitelistEntry)
344       .SetMethod("removeOriginAccessWhitelistEntry",
345                  &TestRunnerBindings::RemoveOriginAccessWhitelistEntry)
346       .SetMethod("hasCustomPageSizeStyle",
347                  &TestRunnerBindings::HasCustomPageSizeStyle)
348       .SetMethod("forceRedSelectionColors",
349                  &TestRunnerBindings::ForceRedSelectionColors)
350       .SetMethod("injectStyleSheet", &TestRunnerBindings::InjectStyleSheet)
351       .SetMethod("findString", &TestRunnerBindings::FindString)
352       .SetMethod("selectionAsMarkup", &TestRunnerBindings::SelectionAsMarkup)
353       .SetMethod("setTextSubpixelPositioning",
354                  &TestRunnerBindings::SetTextSubpixelPositioning)
355       .SetMethod("setPageVisibility", &TestRunnerBindings::SetPageVisibility)
356       .SetMethod("setTextDirection", &TestRunnerBindings::SetTextDirection)
357       .SetMethod("useUnfortunateSynchronousResizeMode",
358                  &TestRunnerBindings::UseUnfortunateSynchronousResizeMode)
359       .SetMethod("enableAutoResizeMode",
360                  &TestRunnerBindings::EnableAutoResizeMode)
361       .SetMethod("disableAutoResizeMode",
362                  &TestRunnerBindings::DisableAutoResizeMode)
363       .SetMethod("setMockDeviceMotion",
364                  &TestRunnerBindings::SetMockDeviceMotion)
365       .SetMethod("setMockDeviceOrientation",
366                  &TestRunnerBindings::SetMockDeviceOrientation)
367       .SetMethod("setMockScreenOrientation",
368                  &TestRunnerBindings::SetMockScreenOrientation)
369       .SetMethod("didAcquirePointerLock",
370                  &TestRunnerBindings::DidAcquirePointerLock)
371       .SetMethod("didNotAcquirePointerLock",
372                  &TestRunnerBindings::DidNotAcquirePointerLock)
373       .SetMethod("didLosePointerLock", &TestRunnerBindings::DidLosePointerLock)
374       .SetMethod("setPointerLockWillFailSynchronously",
375                  &TestRunnerBindings::SetPointerLockWillFailSynchronously)
376       .SetMethod("setPointerLockWillRespondAsynchronously",
377                  &TestRunnerBindings::SetPointerLockWillRespondAsynchronously)
378       .SetMethod("setPopupBlockingEnabled",
379                  &TestRunnerBindings::SetPopupBlockingEnabled)
380       .SetMethod("setJavaScriptCanAccessClipboard",
381                  &TestRunnerBindings::SetJavaScriptCanAccessClipboard)
382       .SetMethod("setXSSAuditorEnabled",
383                  &TestRunnerBindings::SetXSSAuditorEnabled)
384       .SetMethod("setAllowUniversalAccessFromFileURLs",
385                  &TestRunnerBindings::SetAllowUniversalAccessFromFileURLs)
386       .SetMethod("setAllowFileAccessFromFileURLs",
387                  &TestRunnerBindings::SetAllowFileAccessFromFileURLs)
388       .SetMethod("overridePreference", &TestRunnerBindings::OverridePreference)
389       .SetMethod("setPluginsEnabled", &TestRunnerBindings::SetPluginsEnabled)
390       .SetMethod("dumpEditingCallbacks",
391                  &TestRunnerBindings::DumpEditingCallbacks)
392       .SetMethod("dumpAsText", &TestRunnerBindings::DumpAsText)
393       .SetMethod("dumpAsTextWithPixelResults",
394                  &TestRunnerBindings::DumpAsTextWithPixelResults)
395       .SetMethod("dumpChildFrameScrollPositions",
396                  &TestRunnerBindings::DumpChildFrameScrollPositions)
397       .SetMethod("dumpChildFramesAsText",
398                  &TestRunnerBindings::DumpChildFramesAsText)
399       .SetMethod("dumpIconChanges", &TestRunnerBindings::DumpIconChanges)
400       .SetMethod("setAudioData", &TestRunnerBindings::SetAudioData)
401       .SetMethod("dumpFrameLoadCallbacks",
402                  &TestRunnerBindings::DumpFrameLoadCallbacks)
403       .SetMethod("dumpPingLoaderCallbacks",
404                  &TestRunnerBindings::DumpPingLoaderCallbacks)
405       .SetMethod("dumpUserGestureInFrameLoadCallbacks",
406                  &TestRunnerBindings::DumpUserGestureInFrameLoadCallbacks)
407       .SetMethod("dumpTitleChanges", &TestRunnerBindings::DumpTitleChanges)
408       .SetMethod("dumpCreateView", &TestRunnerBindings::DumpCreateView)
409       .SetMethod("setCanOpenWindows", &TestRunnerBindings::SetCanOpenWindows)
410       .SetMethod("dumpResourceLoadCallbacks",
411                  &TestRunnerBindings::DumpResourceLoadCallbacks)
412       .SetMethod("dumpResourceRequestCallbacks",
413                  &TestRunnerBindings::DumpResourceRequestCallbacks)
414       .SetMethod("dumpResourceResponseMIMETypes",
415                  &TestRunnerBindings::DumpResourceResponseMIMETypes)
416       .SetMethod("setImagesAllowed", &TestRunnerBindings::SetImagesAllowed)
417       .SetMethod("setScriptsAllowed", &TestRunnerBindings::SetScriptsAllowed)
418       .SetMethod("setStorageAllowed", &TestRunnerBindings::SetStorageAllowed)
419       .SetMethod("setPluginsAllowed", &TestRunnerBindings::SetPluginsAllowed)
420       .SetMethod("setAllowDisplayOfInsecureContent",
421                  &TestRunnerBindings::SetAllowDisplayOfInsecureContent)
422       .SetMethod("setAllowRunningOfInsecureContent",
423                  &TestRunnerBindings::SetAllowRunningOfInsecureContent)
424       .SetMethod("dumpPermissionClientCallbacks",
425                  &TestRunnerBindings::DumpPermissionClientCallbacks)
426       .SetMethod("dumpWindowStatusChanges",
427                  &TestRunnerBindings::DumpWindowStatusChanges)
428       .SetMethod("dumpProgressFinishedCallback",
429                  &TestRunnerBindings::DumpProgressFinishedCallback)
430       .SetMethod("dumpSpellCheckCallbacks",
431                  &TestRunnerBindings::DumpSpellCheckCallbacks)
432       .SetMethod("dumpBackForwardList",
433                  &TestRunnerBindings::DumpBackForwardList)
434       .SetMethod("dumpSelectionRect", &TestRunnerBindings::DumpSelectionRect)
435       .SetMethod("testRepaint", &TestRunnerBindings::TestRepaint)
436       .SetMethod("repaintSweepHorizontally",
437                  &TestRunnerBindings::RepaintSweepHorizontally)
438       .SetMethod("setPrinting", &TestRunnerBindings::SetPrinting)
439       .SetMethod("setShouldStayOnPageAfterHandlingBeforeUnload",
440                  &TestRunnerBindings::
441                      SetShouldStayOnPageAfterHandlingBeforeUnload)
442       .SetMethod("setWillSendRequestClearHeader",
443                  &TestRunnerBindings::SetWillSendRequestClearHeader)
444       .SetMethod("dumpResourceRequestPriorities",
445                  &TestRunnerBindings::DumpResourceRequestPriorities)
446       .SetMethod("setUseMockTheme", &TestRunnerBindings::SetUseMockTheme)
447       .SetMethod("waitUntilExternalURLLoad",
448                  &TestRunnerBindings::WaitUntilExternalURLLoad)
449       .SetMethod("showWebInspector", &TestRunnerBindings::ShowWebInspector)
450       .SetMethod("closeWebInspector", &TestRunnerBindings::CloseWebInspector)
451       .SetMethod("isChooserShown", &TestRunnerBindings::IsChooserShown)
452       .SetMethod("evaluateInWebInspector",
453                  &TestRunnerBindings::EvaluateInWebInspector)
454       .SetMethod("clearAllDatabases", &TestRunnerBindings::ClearAllDatabases)
455       .SetMethod("setDatabaseQuota", &TestRunnerBindings::SetDatabaseQuota)
456       .SetMethod("setAlwaysAcceptCookies",
457                  &TestRunnerBindings::SetAlwaysAcceptCookies)
458       .SetMethod("setWindowIsKey", &TestRunnerBindings::SetWindowIsKey)
459       .SetMethod("pathToLocalResource",
460                  &TestRunnerBindings::PathToLocalResource)
461       .SetMethod("setBackingScaleFactor",
462                  &TestRunnerBindings::SetBackingScaleFactor)
463       .SetMethod("setPOSIXLocale", &TestRunnerBindings::SetPOSIXLocale)
464       .SetMethod("setMIDIAccessorResult",
465                  &TestRunnerBindings::SetMIDIAccessorResult)
466       .SetMethod("setMIDISysexPermission",
467                  &TestRunnerBindings::SetMIDISysexPermission)
468       .SetMethod("grantWebNotificationPermission",
469                  &TestRunnerBindings::GrantWebNotificationPermission)
470       .SetMethod("simulateWebNotificationClick",
471                  &TestRunnerBindings::SimulateWebNotificationClick)
472       .SetMethod("addMockSpeechInputResult",
473                  &TestRunnerBindings::AddMockSpeechInputResult)
474       .SetMethod("setMockSpeechInputDumpRect",
475                  &TestRunnerBindings::SetMockSpeechInputDumpRect)
476       .SetMethod("addMockSpeechRecognitionResult",
477                  &TestRunnerBindings::AddMockSpeechRecognitionResult)
478       .SetMethod("setMockSpeechRecognitionError",
479                  &TestRunnerBindings::SetMockSpeechRecognitionError)
480       .SetMethod("wasMockSpeechRecognitionAborted",
481                  &TestRunnerBindings::WasMockSpeechRecognitionAborted)
482       .SetMethod("addWebPageOverlay", &TestRunnerBindings::AddWebPageOverlay)
483       .SetMethod("removeWebPageOverlay",
484                  &TestRunnerBindings::RemoveWebPageOverlay)
485       .SetMethod("display", &TestRunnerBindings::Display)
486       .SetMethod("displayInvalidatedRegion",
487                  &TestRunnerBindings::DisplayInvalidatedRegion)
488
489       // Properties.
490       .SetProperty("globalFlag", &TestRunnerBindings::GlobalFlag,
491                    &TestRunnerBindings::SetGlobalFlag)
492       .SetProperty("platformName", &TestRunnerBindings::PlatformName)
493       .SetProperty("tooltipText", &TestRunnerBindings::TooltipText)
494       .SetProperty("disableNotifyDone", &TestRunnerBindings::DisableNotifyDone)
495       // webHistoryItemCount is used by tests in LayoutTests\http\tests\history
496       .SetProperty("webHistoryItemCount",
497                    &TestRunnerBindings::WebHistoryItemCount)
498       .SetProperty("interceptPostMessage",
499                    &TestRunnerBindings::InterceptPostMessage,
500                    &TestRunnerBindings::SetInterceptPostMessage)
501
502       // The following are stubs.
503       .SetMethod("dumpDatabaseCallbacks", &TestRunnerBindings::NotImplemented)
504       .SetMethod("setIconDatabaseEnabled", &TestRunnerBindings::NotImplemented)
505       .SetMethod("setScrollbarPolicy", &TestRunnerBindings::NotImplemented)
506       .SetMethod("clearAllApplicationCaches",
507                  &TestRunnerBindings::NotImplemented)
508       .SetMethod("clearApplicationCacheForOrigin",
509                  &TestRunnerBindings::NotImplemented)
510       .SetMethod("clearBackForwardList", &TestRunnerBindings::NotImplemented)
511       .SetMethod("keepWebHistory", &TestRunnerBindings::NotImplemented)
512       .SetMethod("setApplicationCacheOriginQuota",
513                  &TestRunnerBindings::NotImplemented)
514       .SetMethod("setCallCloseOnWebViews", &TestRunnerBindings::NotImplemented)
515       .SetMethod("setMainFrameIsFirstResponder",
516                  &TestRunnerBindings::NotImplemented)
517       .SetMethod("setUseDashboardCompatibilityMode",
518                  &TestRunnerBindings::NotImplemented)
519       .SetMethod("deleteAllLocalStorage", &TestRunnerBindings::NotImplemented)
520       .SetMethod("localStorageDiskUsageForOrigin",
521                  &TestRunnerBindings::NotImplemented)
522       .SetMethod("originsWithLocalStorage", &TestRunnerBindings::NotImplemented)
523       .SetMethod("deleteLocalStorageForOrigin",
524                  &TestRunnerBindings::NotImplemented)
525       .SetMethod("observeStorageTrackerNotifications",
526                  &TestRunnerBindings::NotImplemented)
527       .SetMethod("syncLocalStorage", &TestRunnerBindings::NotImplemented)
528       .SetMethod("addDisallowedURL", &TestRunnerBindings::NotImplemented)
529       .SetMethod("applicationCacheDiskUsageForOrigin",
530                  &TestRunnerBindings::NotImplemented)
531       .SetMethod("abortModal", &TestRunnerBindings::NotImplemented)
532
533       // Aliases.
534       // Used at fast/dom/assign-to-window-status.html
535       .SetMethod("dumpStatusCallbacks",
536                  &TestRunnerBindings::DumpWindowStatusChanges);
537
538 }
539
540 void TestRunnerBindings::NotifyDone() {
541   if (runner_)
542     runner_->NotifyDone();
543 }
544
545 void TestRunnerBindings::WaitUntilDone() {
546   if (runner_)
547     runner_->WaitUntilDone();
548 }
549
550 void TestRunnerBindings::QueueBackNavigation(int how_far_back) {
551   if (runner_)
552     runner_->QueueBackNavigation(how_far_back);
553 }
554
555 void TestRunnerBindings::QueueForwardNavigation(int how_far_forward) {
556   if (runner_)
557     runner_->QueueForwardNavigation(how_far_forward);
558 }
559
560 void TestRunnerBindings::QueueReload() {
561   if (runner_)
562     runner_->QueueReload();
563 }
564
565 void TestRunnerBindings::QueueLoadingScript(const std::string& script) {
566   if (runner_)
567     runner_->QueueLoadingScript(script);
568 }
569
570 void TestRunnerBindings::QueueNonLoadingScript(const std::string& script) {
571   if (runner_)
572     runner_->QueueNonLoadingScript(script);
573 }
574
575 void TestRunnerBindings::QueueLoad(gin::Arguments* args) {
576   if (runner_) {
577     std::string url;
578     std::string target;
579     args->GetNext(&url);
580     args->GetNext(&target);
581     runner_->QueueLoad(url, target);
582   }
583 }
584
585 void TestRunnerBindings::QueueLoadHTMLString(gin::Arguments* args) {
586   if (runner_)
587     runner_->QueueLoadHTMLString(args);
588 }
589
590 void TestRunnerBindings::SetCustomPolicyDelegate(gin::Arguments* args) {
591   if (runner_)
592     runner_->SetCustomPolicyDelegate(args);
593 }
594
595 void TestRunnerBindings::WaitForPolicyDelegate() {
596   if (runner_)
597     runner_->WaitForPolicyDelegate();
598 }
599
600 int TestRunnerBindings::WindowCount() {
601   if (runner_)
602     return runner_->WindowCount();
603   return 0;
604 }
605
606 void TestRunnerBindings::SetCloseRemainingWindowsWhenComplete(
607     gin::Arguments* args) {
608   if (!runner_)
609     return;
610
611   // In the original implementation, nothing happens if the argument is
612   // ommitted.
613   bool close_remaining_windows = false;
614   if (args->GetNext(&close_remaining_windows))
615     runner_->SetCloseRemainingWindowsWhenComplete(close_remaining_windows);
616 }
617
618 void TestRunnerBindings::ResetTestHelperControllers() {
619   if (runner_)
620     runner_->ResetTestHelperControllers();
621 }
622
623 void TestRunnerBindings::SetTabKeyCyclesThroughElements(
624     bool tab_key_cycles_through_elements) {
625   if (runner_)
626     runner_->SetTabKeyCyclesThroughElements(tab_key_cycles_through_elements);
627 }
628
629 void TestRunnerBindings::ExecCommand(gin::Arguments* args) {
630   if (runner_)
631     runner_->ExecCommand(args);
632 }
633
634 bool TestRunnerBindings::IsCommandEnabled(const std::string& command) {
635   if (runner_)
636     return runner_->IsCommandEnabled(command);
637   return false;
638 }
639
640 bool TestRunnerBindings::CallShouldCloseOnWebView() {
641   if (runner_)
642     return runner_->CallShouldCloseOnWebView();
643   return false;
644 }
645
646 void TestRunnerBindings::SetDomainRelaxationForbiddenForURLScheme(
647     bool forbidden, const std::string& scheme) {
648   if (runner_)
649     runner_->SetDomainRelaxationForbiddenForURLScheme(forbidden, scheme);
650 }
651
652 v8::Handle<v8::Value>
653 TestRunnerBindings::EvaluateScriptInIsolatedWorldAndReturnValue(
654     int world_id, const std::string& script) {
655   if (!runner_)
656     return v8::Handle<v8::Value>();
657   return runner_->EvaluateScriptInIsolatedWorldAndReturnValue(world_id,
658                                                               script);
659 }
660
661 void TestRunnerBindings::EvaluateScriptInIsolatedWorld(
662     int world_id, const std::string& script) {
663   if (runner_)
664     runner_->EvaluateScriptInIsolatedWorld(world_id, script);
665 }
666
667 void TestRunnerBindings::SetIsolatedWorldSecurityOrigin(
668     int world_id, v8::Handle<v8::Value> origin) {
669   if (runner_)
670     runner_->SetIsolatedWorldSecurityOrigin(world_id, origin);
671 }
672
673 void TestRunnerBindings::SetIsolatedWorldContentSecurityPolicy(
674     int world_id, const std::string& policy) {
675   if (runner_)
676     runner_->SetIsolatedWorldContentSecurityPolicy(world_id, policy);
677 }
678
679 void TestRunnerBindings::AddOriginAccessWhitelistEntry(
680     const std::string& source_origin,
681     const std::string& destination_protocol,
682     const std::string& destination_host,
683     bool allow_destination_subdomains) {
684   if (runner_) {
685     runner_->AddOriginAccessWhitelistEntry(source_origin,
686                                            destination_protocol,
687                                            destination_host,
688                                            allow_destination_subdomains);
689   }
690 }
691
692 void TestRunnerBindings::RemoveOriginAccessWhitelistEntry(
693     const std::string& source_origin,
694     const std::string& destination_protocol,
695     const std::string& destination_host,
696     bool allow_destination_subdomains) {
697   if (runner_) {
698     runner_->RemoveOriginAccessWhitelistEntry(source_origin,
699                                               destination_protocol,
700                                               destination_host,
701                                               allow_destination_subdomains);
702   }
703 }
704
705 bool TestRunnerBindings::HasCustomPageSizeStyle(int page_index) {
706   if (runner_)
707     return runner_->HasCustomPageSizeStyle(page_index);
708   return false;
709 }
710
711 void TestRunnerBindings::ForceRedSelectionColors() {
712   if (runner_)
713     runner_->ForceRedSelectionColors();
714 }
715
716 void TestRunnerBindings::InjectStyleSheet(const std::string& source_code,
717                                           bool all_frames) {
718   if (runner_)
719     runner_->InjectStyleSheet(source_code, all_frames);
720 }
721
722 bool TestRunnerBindings::FindString(
723     const std::string& search_text,
724     const std::vector<std::string>& options_array) {
725   if (runner_)
726     return runner_->FindString(search_text, options_array);
727   return false;
728 }
729
730 std::string TestRunnerBindings::SelectionAsMarkup() {
731   if (runner_)
732     return runner_->SelectionAsMarkup();
733   return std::string();
734 }
735
736 void TestRunnerBindings::SetTextSubpixelPositioning(bool value) {
737   if (runner_)
738     runner_->SetTextSubpixelPositioning(value);
739 }
740
741 void TestRunnerBindings::SetPageVisibility(const std::string& new_visibility) {
742   if (runner_)
743     runner_->SetPageVisibility(new_visibility);
744 }
745
746 void TestRunnerBindings::SetTextDirection(const std::string& direction_name) {
747   if (runner_)
748     runner_->SetTextDirection(direction_name);
749 }
750
751 void TestRunnerBindings::UseUnfortunateSynchronousResizeMode() {
752   if (runner_)
753     runner_->UseUnfortunateSynchronousResizeMode();
754 }
755
756 bool TestRunnerBindings::EnableAutoResizeMode(int min_width,
757                                               int min_height,
758                                               int max_width,
759                                               int max_height) {
760   if (runner_) {
761     return runner_->EnableAutoResizeMode(min_width, min_height,
762                                          max_width, max_height);
763   }
764   return false;
765 }
766
767 bool TestRunnerBindings::DisableAutoResizeMode(int new_width, int new_height) {
768   if (runner_)
769     return runner_->DisableAutoResizeMode(new_width, new_height);
770   return false;
771 }
772
773 void TestRunnerBindings::SetMockDeviceMotion(gin::Arguments* args) {
774   if (!runner_)
775     return;
776
777   bool has_acceleration_x;
778   double acceleration_x;
779   bool has_acceleration_y;
780   double acceleration_y;
781   bool has_acceleration_z;
782   double acceleration_z;
783   bool has_acceleration_including_gravity_x;
784   double acceleration_including_gravity_x;
785   bool has_acceleration_including_gravity_y;
786   double acceleration_including_gravity_y;
787   bool has_acceleration_including_gravity_z;
788   double acceleration_including_gravity_z;
789   bool has_rotation_rate_alpha;
790   double rotation_rate_alpha;
791   bool has_rotation_rate_beta;
792   double rotation_rate_beta;
793   bool has_rotation_rate_gamma;
794   double rotation_rate_gamma;
795   double interval;
796
797   args->GetNext(&has_acceleration_x);
798   args->GetNext(& acceleration_x);
799   args->GetNext(&has_acceleration_y);
800   args->GetNext(& acceleration_y);
801   args->GetNext(&has_acceleration_z);
802   args->GetNext(& acceleration_z);
803   args->GetNext(&has_acceleration_including_gravity_x);
804   args->GetNext(& acceleration_including_gravity_x);
805   args->GetNext(&has_acceleration_including_gravity_y);
806   args->GetNext(& acceleration_including_gravity_y);
807   args->GetNext(&has_acceleration_including_gravity_z);
808   args->GetNext(& acceleration_including_gravity_z);
809   args->GetNext(&has_rotation_rate_alpha);
810   args->GetNext(& rotation_rate_alpha);
811   args->GetNext(&has_rotation_rate_beta);
812   args->GetNext(& rotation_rate_beta);
813   args->GetNext(&has_rotation_rate_gamma);
814   args->GetNext(& rotation_rate_gamma);
815   args->GetNext(& interval);
816
817   runner_->SetMockDeviceMotion(has_acceleration_x, acceleration_x,
818                                has_acceleration_y, acceleration_y,
819                                has_acceleration_z, acceleration_z,
820                                has_acceleration_including_gravity_x,
821                                acceleration_including_gravity_x,
822                                has_acceleration_including_gravity_y,
823                                acceleration_including_gravity_y,
824                                has_acceleration_including_gravity_z,
825                                acceleration_including_gravity_z,
826                                has_rotation_rate_alpha,
827                                rotation_rate_alpha,
828                                has_rotation_rate_beta,
829                                rotation_rate_beta,
830                                has_rotation_rate_gamma,
831                                rotation_rate_gamma,
832                                interval);
833 }
834
835 void TestRunnerBindings::SetMockDeviceOrientation(gin::Arguments* args) {
836   if (!runner_)
837     return;
838
839   bool has_alpha;
840   double alpha;
841   bool has_beta;
842   double beta;
843   bool has_gamma;
844   double gamma;
845   bool has_absolute;
846   bool absolute;
847
848   args->GetNext(&has_alpha);
849   args->GetNext(&alpha);
850   args->GetNext(&has_beta);
851   args->GetNext(&beta);
852   args->GetNext(&has_gamma);
853   args->GetNext(&gamma);
854   args->GetNext(&has_absolute);
855   args->GetNext(&absolute);
856
857   runner_->SetMockDeviceOrientation(has_alpha, alpha,
858                                     has_beta, beta,
859                                     has_gamma, gamma,
860                                     has_absolute, absolute);
861 }
862
863 void TestRunnerBindings::SetMockScreenOrientation(const std::string& orientation) {
864   if (!runner_)
865     return;
866
867   runner_->SetMockScreenOrientation(orientation);
868 }
869
870 void TestRunnerBindings::DidAcquirePointerLock() {
871   if (runner_)
872     runner_->DidAcquirePointerLock();
873 }
874
875 void TestRunnerBindings::DidNotAcquirePointerLock() {
876   if (runner_)
877     runner_->DidNotAcquirePointerLock();
878 }
879
880 void TestRunnerBindings::DidLosePointerLock() {
881   if (runner_)
882     runner_->DidLosePointerLock();
883 }
884
885 void TestRunnerBindings::SetPointerLockWillFailSynchronously() {
886   if (runner_)
887     runner_->SetPointerLockWillFailSynchronously();
888 }
889
890 void TestRunnerBindings::SetPointerLockWillRespondAsynchronously() {
891   if (runner_)
892     runner_->SetPointerLockWillRespondAsynchronously();
893 }
894
895 void TestRunnerBindings::SetPopupBlockingEnabled(bool block_popups) {
896   if (runner_)
897     runner_->SetPopupBlockingEnabled(block_popups);
898 }
899
900 void TestRunnerBindings::SetJavaScriptCanAccessClipboard(bool can_access) {
901   if (runner_)
902     runner_->SetJavaScriptCanAccessClipboard(can_access);
903 }
904
905 void TestRunnerBindings::SetXSSAuditorEnabled(bool enabled) {
906   if (runner_)
907     runner_->SetXSSAuditorEnabled(enabled);
908 }
909
910 void TestRunnerBindings::SetAllowUniversalAccessFromFileURLs(bool allow) {
911   if (runner_)
912     runner_->SetAllowUniversalAccessFromFileURLs(allow);
913 }
914
915 void TestRunnerBindings::SetAllowFileAccessFromFileURLs(bool allow) {
916   if (runner_)
917     runner_->SetAllowFileAccessFromFileURLs(allow);
918 }
919
920 void TestRunnerBindings::OverridePreference(const std::string key,
921                                             v8::Handle<v8::Value> value) {
922   if (runner_)
923     runner_->OverridePreference(key, value);
924 }
925
926 void TestRunnerBindings::SetPluginsEnabled(bool enabled) {
927   if (runner_)
928     runner_->SetPluginsEnabled(enabled);
929 }
930
931 void TestRunnerBindings::DumpEditingCallbacks() {
932   if (runner_)
933     runner_->DumpEditingCallbacks();
934 }
935
936 void TestRunnerBindings::DumpAsText() {
937   if (runner_)
938     runner_->DumpAsText();
939 }
940
941 void TestRunnerBindings::DumpAsTextWithPixelResults() {
942   if (runner_)
943     runner_->DumpAsTextWithPixelResults();
944 }
945
946 void TestRunnerBindings::DumpChildFrameScrollPositions() {
947   if (runner_)
948     runner_->DumpChildFrameScrollPositions();
949 }
950
951 void TestRunnerBindings::DumpChildFramesAsText() {
952   if (runner_)
953     runner_->DumpChildFramesAsText();
954 }
955
956 void TestRunnerBindings::DumpIconChanges() {
957   if (runner_)
958     runner_->DumpIconChanges();
959 }
960
961 void TestRunnerBindings::SetAudioData(const gin::ArrayBufferView& view) {
962   if (runner_)
963     runner_->SetAudioData(view);
964 }
965
966 void TestRunnerBindings::DumpFrameLoadCallbacks() {
967   if (runner_)
968     runner_->DumpFrameLoadCallbacks();
969 }
970
971 void TestRunnerBindings::DumpPingLoaderCallbacks() {
972   if (runner_)
973     runner_->DumpPingLoaderCallbacks();
974 }
975
976 void TestRunnerBindings::DumpUserGestureInFrameLoadCallbacks() {
977   if (runner_)
978     runner_->DumpUserGestureInFrameLoadCallbacks();
979 }
980
981 void TestRunnerBindings::DumpTitleChanges() {
982   if (runner_)
983     runner_->DumpTitleChanges();
984 }
985
986 void TestRunnerBindings::DumpCreateView() {
987   if (runner_)
988     runner_->DumpCreateView();
989 }
990
991 void TestRunnerBindings::SetCanOpenWindows() {
992   if (runner_)
993     runner_->SetCanOpenWindows();
994 }
995
996 void TestRunnerBindings::DumpResourceLoadCallbacks() {
997   if (runner_)
998     runner_->DumpResourceLoadCallbacks();
999 }
1000
1001 void TestRunnerBindings::DumpResourceRequestCallbacks() {
1002   if (runner_)
1003     runner_->DumpResourceRequestCallbacks();
1004 }
1005
1006 void TestRunnerBindings::DumpResourceResponseMIMETypes() {
1007   if (runner_)
1008     runner_->DumpResourceResponseMIMETypes();
1009 }
1010
1011 void TestRunnerBindings::SetImagesAllowed(bool allowed) {
1012   if (runner_)
1013     runner_->SetImagesAllowed(allowed);
1014 }
1015
1016 void TestRunnerBindings::SetScriptsAllowed(bool allowed) {
1017   if (runner_)
1018     runner_->SetScriptsAllowed(allowed);
1019 }
1020
1021 void TestRunnerBindings::SetStorageAllowed(bool allowed) {
1022   if (runner_)
1023     runner_->SetStorageAllowed(allowed);
1024 }
1025
1026 void TestRunnerBindings::SetPluginsAllowed(bool allowed) {
1027   if (runner_)
1028     runner_->SetPluginsAllowed(allowed);
1029 }
1030
1031 void TestRunnerBindings::SetAllowDisplayOfInsecureContent(bool allowed) {
1032   if (runner_)
1033     runner_->SetAllowDisplayOfInsecureContent(allowed);
1034 }
1035
1036 void TestRunnerBindings::SetAllowRunningOfInsecureContent(bool allowed) {
1037   if (runner_)
1038     runner_->SetAllowRunningOfInsecureContent(allowed);
1039 }
1040
1041 void TestRunnerBindings::DumpPermissionClientCallbacks() {
1042   if (runner_)
1043     runner_->DumpPermissionClientCallbacks();
1044 }
1045
1046 void TestRunnerBindings::DumpWindowStatusChanges() {
1047   if (runner_)
1048     runner_->DumpWindowStatusChanges();
1049 }
1050
1051 void TestRunnerBindings::DumpProgressFinishedCallback() {
1052   if (runner_)
1053     runner_->DumpProgressFinishedCallback();
1054 }
1055
1056 void TestRunnerBindings::DumpSpellCheckCallbacks() {
1057   if (runner_)
1058     runner_->DumpSpellCheckCallbacks();
1059 }
1060
1061 void TestRunnerBindings::DumpBackForwardList() {
1062   if (runner_)
1063     runner_->DumpBackForwardList();
1064 }
1065
1066 void TestRunnerBindings::DumpSelectionRect() {
1067   if (runner_)
1068     runner_->DumpSelectionRect();
1069 }
1070
1071 void TestRunnerBindings::TestRepaint() {
1072   if (runner_)
1073     runner_->TestRepaint();
1074 }
1075
1076 void TestRunnerBindings::RepaintSweepHorizontally() {
1077   if (runner_)
1078     runner_->RepaintSweepHorizontally();
1079 }
1080
1081 void TestRunnerBindings::SetPrinting() {
1082   if (runner_)
1083     runner_->SetPrinting();
1084 }
1085
1086 void TestRunnerBindings::SetShouldStayOnPageAfterHandlingBeforeUnload(
1087     bool value) {
1088   if (runner_)
1089     runner_->SetShouldStayOnPageAfterHandlingBeforeUnload(value);
1090 }
1091
1092 void TestRunnerBindings::SetWillSendRequestClearHeader(
1093     const std::string& header) {
1094   if (runner_)
1095     runner_->SetWillSendRequestClearHeader(header);
1096 }
1097
1098 void TestRunnerBindings::DumpResourceRequestPriorities() {
1099   if (runner_)
1100     runner_->DumpResourceRequestPriorities();
1101 }
1102
1103 void TestRunnerBindings::SetUseMockTheme(bool use) {
1104   if (runner_)
1105     runner_->SetUseMockTheme(use);
1106 }
1107
1108 void TestRunnerBindings::WaitUntilExternalURLLoad() {
1109   if (runner_)
1110     runner_->WaitUntilExternalURLLoad();
1111 }
1112
1113 void TestRunnerBindings::ShowWebInspector(gin::Arguments* args) {
1114   if (runner_) {
1115     std::string str;
1116     args->GetNext(&str);
1117     runner_->ShowWebInspector(str);
1118   }
1119 }
1120
1121 void TestRunnerBindings::CloseWebInspector() {
1122   if (runner_)
1123     runner_->CloseWebInspector();
1124 }
1125
1126 bool TestRunnerBindings::IsChooserShown() {
1127   if (runner_)
1128     return runner_->IsChooserShown();
1129   return false;
1130 }
1131
1132 void TestRunnerBindings::EvaluateInWebInspector(int call_id,
1133                                                 const std::string& script) {
1134   if (runner_)
1135     runner_->EvaluateInWebInspector(call_id, script);
1136 }
1137
1138 void TestRunnerBindings::ClearAllDatabases() {
1139   if (runner_)
1140     runner_->ClearAllDatabases();
1141 }
1142
1143 void TestRunnerBindings::SetDatabaseQuota(int quota) {
1144   if (runner_)
1145     runner_->SetDatabaseQuota(quota);
1146 }
1147
1148 void TestRunnerBindings::SetAlwaysAcceptCookies(bool accept) {
1149   if (runner_)
1150     runner_->SetAlwaysAcceptCookies(accept);
1151 }
1152
1153 void TestRunnerBindings::SetWindowIsKey(bool value) {
1154   if (runner_)
1155     runner_->SetWindowIsKey(value);
1156 }
1157
1158 std::string TestRunnerBindings::PathToLocalResource(const std::string& path) {
1159   if (runner_)
1160     return runner_->PathToLocalResource(path);
1161   return std::string();
1162 }
1163
1164 void TestRunnerBindings::SetBackingScaleFactor(
1165     double value, v8::Handle<v8::Function> callback) {
1166   if (runner_)
1167     runner_->SetBackingScaleFactor(value, callback);
1168 }
1169
1170 void TestRunnerBindings::SetPOSIXLocale(const std::string& locale) {
1171   if (runner_)
1172     runner_->SetPOSIXLocale(locale);
1173 }
1174
1175 void TestRunnerBindings::SetMIDIAccessorResult(bool result) {
1176   if (runner_)
1177     runner_->SetMIDIAccessorResult(result);
1178 }
1179
1180 void TestRunnerBindings::SetMIDISysexPermission(bool value) {
1181   if (runner_)
1182     runner_->SetMIDISysexPermission(value);
1183 }
1184
1185 void TestRunnerBindings::GrantWebNotificationPermission(gin::Arguments* args) {
1186   if (runner_) {
1187     std::string origin;
1188     bool permission_granted = true;
1189     args->GetNext(&origin);
1190     args->GetNext(&permission_granted);
1191     return runner_->GrantWebNotificationPermission(origin, permission_granted);
1192   }
1193 }
1194
1195 bool TestRunnerBindings::SimulateWebNotificationClick(
1196     const std::string& value) {
1197   if (runner_)
1198     return runner_->SimulateWebNotificationClick(value);
1199   return false;
1200 }
1201
1202 void TestRunnerBindings::AddMockSpeechInputResult(const std::string& result,
1203                                                   double confidence,
1204                                                   const std::string& language) {
1205   if (runner_)
1206     runner_->AddMockSpeechInputResult(result, confidence, language);
1207 }
1208
1209 void TestRunnerBindings::SetMockSpeechInputDumpRect(bool value) {
1210   if (runner_)
1211     runner_->SetMockSpeechInputDumpRect(value);
1212 }
1213
1214 void TestRunnerBindings::AddMockSpeechRecognitionResult(
1215     const std::string& transcript, double confidence) {
1216   if (runner_)
1217     runner_->AddMockSpeechRecognitionResult(transcript, confidence);
1218 }
1219
1220 void TestRunnerBindings::SetMockSpeechRecognitionError(
1221     const std::string& error, const std::string& message) {
1222   if (runner_)
1223     runner_->SetMockSpeechRecognitionError(error, message);
1224 }
1225
1226 bool TestRunnerBindings::WasMockSpeechRecognitionAborted() {
1227   if (runner_)
1228     return runner_->WasMockSpeechRecognitionAborted();
1229   return false;
1230 }
1231
1232 void TestRunnerBindings::AddWebPageOverlay() {
1233   if (runner_)
1234     runner_->AddWebPageOverlay();
1235 }
1236
1237 void TestRunnerBindings::RemoveWebPageOverlay() {
1238   if (runner_)
1239     runner_->RemoveWebPageOverlay();
1240 }
1241
1242 void TestRunnerBindings::Display() {
1243   if (runner_)
1244     runner_->Display();
1245 }
1246
1247 void TestRunnerBindings::DisplayInvalidatedRegion() {
1248   if (runner_)
1249     runner_->DisplayInvalidatedRegion();
1250 }
1251
1252 bool TestRunnerBindings::GlobalFlag() {
1253   if (runner_)
1254     return runner_->global_flag_;
1255   return false;
1256 }
1257
1258 void TestRunnerBindings::SetGlobalFlag(bool value) {
1259   if (runner_)
1260     runner_->global_flag_ = value;
1261 }
1262
1263 std::string TestRunnerBindings::PlatformName() {
1264   if (runner_)
1265     return runner_->platform_name_;
1266   return std::string();
1267 }
1268
1269 std::string TestRunnerBindings::TooltipText() {
1270   if (runner_)
1271     return runner_->tooltip_text_;
1272   return std::string();
1273 }
1274
1275 bool TestRunnerBindings::DisableNotifyDone() {
1276   if (runner_)
1277     return runner_->disable_notify_done_;
1278   return false;
1279 }
1280
1281 int TestRunnerBindings::WebHistoryItemCount() {
1282   if (runner_)
1283     return runner_->web_history_item_count_;
1284   return false;
1285 }
1286
1287 bool TestRunnerBindings::InterceptPostMessage() {
1288   if (runner_)
1289     return runner_->intercept_post_message_;
1290   return false;
1291 }
1292
1293 void TestRunnerBindings::SetInterceptPostMessage(bool value) {
1294   if (runner_)
1295     runner_->intercept_post_message_ = value;
1296 }
1297
1298 void TestRunnerBindings::NotImplemented(const gin::Arguments& args) {
1299 }
1300
1301 class TestPageOverlay : public WebPageOverlay {
1302  public:
1303   explicit TestPageOverlay(WebView* web_view)
1304       : web_view_(web_view) {
1305   }
1306   virtual ~TestPageOverlay() {}
1307
1308   virtual void paintPageOverlay(WebCanvas* canvas) OVERRIDE  {
1309     SkRect rect = SkRect::MakeWH(web_view_->size().width,
1310                                  web_view_->size().height);
1311     SkPaint paint;
1312     paint.setColor(SK_ColorCYAN);
1313     paint.setStyle(SkPaint::kFill_Style);
1314     canvas->drawRect(rect, paint);
1315   }
1316
1317  private:
1318   WebView* web_view_;
1319 };
1320
1321 TestRunner::WorkQueue::WorkQueue(TestRunner* controller)
1322     : frozen_(false)
1323     , controller_(controller) {}
1324
1325 TestRunner::WorkQueue::~WorkQueue() {
1326   Reset();
1327 }
1328
1329 void TestRunner::WorkQueue::ProcessWorkSoon() {
1330   if (controller_->topLoadingFrame())
1331     return;
1332
1333   if (!queue_.empty()) {
1334     // We delay processing queued work to avoid recursion problems.
1335     controller_->delegate_->postTask(new WorkQueueTask(this));
1336   } else if (!controller_->wait_until_done_) {
1337     controller_->delegate_->testFinished();
1338   }
1339 }
1340
1341 void TestRunner::WorkQueue::Reset() {
1342   frozen_ = false;
1343   while (!queue_.empty()) {
1344     delete queue_.front();
1345     queue_.pop_front();
1346   }
1347 }
1348
1349 void TestRunner::WorkQueue::AddWork(WorkItem* work) {
1350   if (frozen_) {
1351     delete work;
1352     return;
1353   }
1354   queue_.push_back(work);
1355 }
1356
1357 void TestRunner::WorkQueue::ProcessWork() {
1358   // Quit doing work once a load is in progress.
1359   while (!queue_.empty()) {
1360     bool startedLoad = queue_.front()->Run(controller_->delegate_,
1361                                            controller_->web_view_);
1362     delete queue_.front();
1363     queue_.pop_front();
1364     if (startedLoad)
1365       return;
1366   }
1367
1368   if (!controller_->wait_until_done_ && !controller_->topLoadingFrame())
1369     controller_->delegate_->testFinished();
1370 }
1371
1372 void TestRunner::WorkQueue::WorkQueueTask::runIfValid() {
1373   m_object->ProcessWork();
1374 }
1375
1376 TestRunner::TestRunner(TestInterfaces* interfaces)
1377     : test_is_running_(false),
1378       close_remaining_windows_(false),
1379       work_queue_(this),
1380       disable_notify_done_(false),
1381       web_history_item_count_(0),
1382       intercept_post_message_(false),
1383       test_interfaces_(interfaces),
1384       delegate_(NULL),
1385       web_view_(NULL),
1386       page_overlay_(NULL),
1387       web_permissions_(new WebPermissions()),
1388       notification_presenter_(new content::NotificationPresenter()),
1389       weak_factory_(this) {}
1390
1391 TestRunner::~TestRunner() {}
1392
1393 void TestRunner::Install(WebFrame* frame) {
1394   TestRunnerBindings::Install(weak_factory_.GetWeakPtr(), frame);
1395 }
1396
1397 void TestRunner::SetDelegate(WebTestDelegate* delegate) {
1398   delegate_ = delegate;
1399   web_permissions_->setDelegate(delegate);
1400   notification_presenter_->set_delegate(delegate);
1401 }
1402
1403 void TestRunner::SetWebView(WebView* webView, WebTestProxyBase* proxy) {
1404   web_view_ = webView;
1405   proxy_ = proxy;
1406 }
1407
1408 void TestRunner::Reset() {
1409   if (web_view_) {
1410     web_view_->setZoomLevel(0);
1411     web_view_->setTextZoomFactor(1);
1412     web_view_->setTabKeyCyclesThroughElements(true);
1413 #if !defined(__APPLE__) && !defined(WIN32) // Actually, TOOLKIT_GTK
1414     // (Constants copied because we can't depend on the header that defined
1415     // them from this file.)
1416     web_view_->setSelectionColors(
1417         0xff1e90ff, 0xff000000, 0xffc8c8c8, 0xff323232);
1418 #endif
1419     web_view_->removeInjectedStyleSheets();
1420     web_view_->setVisibilityState(WebPageVisibilityStateVisible, true);
1421     web_view_->mainFrame()->enableViewSourceMode(false);
1422
1423     if (page_overlay_) {
1424       web_view_->removePageOverlay(page_overlay_);
1425       delete page_overlay_;
1426       page_overlay_ = NULL;
1427     }
1428   }
1429
1430   top_loading_frame_ = NULL;
1431   wait_until_done_ = false;
1432   wait_until_external_url_load_ = false;
1433   policy_delegate_enabled_ = false;
1434   policy_delegate_is_permissive_ = false;
1435   policy_delegate_should_notify_done_ = false;
1436
1437   WebSecurityPolicy::resetOriginAccessWhitelists();
1438 #if defined(__linux__) || defined(ANDROID)
1439   WebFontRendering::setSubpixelPositioning(false);
1440 #endif
1441
1442   if (delegate_) {
1443     // Reset the default quota for each origin to 5MB
1444     delegate_->setDatabaseQuota(5 * 1024 * 1024);
1445     delegate_->setDeviceScaleFactor(1);
1446     delegate_->setAcceptAllCookies(false);
1447     delegate_->setLocale("");
1448     delegate_->useUnfortunateSynchronousResizeMode(false);
1449     delegate_->disableAutoResizeMode(WebSize());
1450     delegate_->deleteAllCookies();
1451   }
1452
1453   dump_editting_callbacks_ = false;
1454   dump_as_text_ = false;
1455   dump_as_markup_ = false;
1456   generate_pixel_results_ = true;
1457   dump_child_frame_scroll_positions_ = false;
1458   dump_child_frames_as_text_ = false;
1459   dump_icon_changes_ = false;
1460   dump_as_audio_ = false;
1461   dump_frame_load_callbacks_ = false;
1462   dump_ping_loader_callbacks_ = false;
1463   dump_user_gesture_in_frame_load_callbacks_ = false;
1464   dump_title_changes_ = false;
1465   dump_create_view_ = false;
1466   can_open_windows_ = false;
1467   dump_resource_load_callbacks_ = false;
1468   dump_resource_request_callbacks_ = false;
1469   dump_resource_reqponse_mime_types_ = false;
1470   dump_window_status_changes_ = false;
1471   dump_progress_finished_callback_ = false;
1472   dump_spell_check_callbacks_ = false;
1473   dump_back_forward_list_ = false;
1474   dump_selection_rect_ = false;
1475   test_repaint_ = false;
1476   sweep_horizontally_ = false;
1477   is_printing_ = false;
1478   midi_accessor_result_ = true;
1479   should_stay_on_page_after_handling_before_unload_ = false;
1480   should_dump_resource_priorities_ = false;
1481
1482   http_headers_to_clear_.clear();
1483
1484   global_flag_ = false;
1485   platform_name_ = "chromium";
1486   tooltip_text_ = std::string();
1487   disable_notify_done_ = false;
1488   web_history_item_count_ = 0;
1489   intercept_post_message_ = false;
1490
1491   web_permissions_->reset();
1492
1493   notification_presenter_->Reset();
1494   use_mock_theme_ = true;
1495   pointer_locked_ = false;
1496   pointer_lock_planned_result_ = PointerLockWillSucceed;
1497
1498   task_list_.revokeAll();
1499   work_queue_.Reset();
1500
1501   if (close_remaining_windows_ && delegate_)
1502     delegate_->closeRemainingWindows();
1503   else
1504     close_remaining_windows_ = true;
1505 }
1506
1507 void TestRunner::SetTestIsRunning(bool running) {
1508   test_is_running_ = running;
1509 }
1510
1511 bool TestRunner::shouldDumpEditingCallbacks() const {
1512   return dump_editting_callbacks_;
1513 }
1514
1515 bool TestRunner::shouldDumpAsText() {
1516   CheckResponseMimeType();
1517   return dump_as_text_;
1518 }
1519
1520 void TestRunner::setShouldDumpAsText(bool value) {
1521   dump_as_text_ = value;
1522 }
1523
1524 bool TestRunner::shouldDumpAsMarkup() {
1525   return dump_as_markup_;
1526 }
1527
1528 void TestRunner::setShouldDumpAsMarkup(bool value) {
1529   dump_as_markup_ = value;
1530 }
1531
1532 bool TestRunner::shouldGeneratePixelResults() {
1533   CheckResponseMimeType();
1534   return generate_pixel_results_;
1535 }
1536
1537 void TestRunner::setShouldGeneratePixelResults(bool value) {
1538   generate_pixel_results_ = value;
1539 }
1540
1541 bool TestRunner::shouldDumpChildFrameScrollPositions() const {
1542   return dump_child_frame_scroll_positions_;
1543 }
1544
1545 bool TestRunner::shouldDumpChildFramesAsText() const {
1546   return dump_child_frames_as_text_;
1547 }
1548
1549 bool TestRunner::shouldDumpAsAudio() const {
1550   return dump_as_audio_;
1551 }
1552
1553 void TestRunner::getAudioData(std::vector<unsigned char>* bufferView) const {
1554   *bufferView = audio_data_;
1555 }
1556
1557 bool TestRunner::shouldDumpFrameLoadCallbacks() const {
1558   return test_is_running_ && dump_frame_load_callbacks_;
1559 }
1560
1561 void TestRunner::setShouldDumpFrameLoadCallbacks(bool value) {
1562   dump_frame_load_callbacks_ = value;
1563 }
1564
1565 bool TestRunner::shouldDumpPingLoaderCallbacks() const {
1566   return test_is_running_ && dump_ping_loader_callbacks_;
1567 }
1568
1569 void TestRunner::setShouldDumpPingLoaderCallbacks(bool value) {
1570   dump_ping_loader_callbacks_ = value;
1571 }
1572
1573 void TestRunner::setShouldEnableViewSource(bool value) {
1574   web_view_->mainFrame()->enableViewSourceMode(value);
1575 }
1576
1577 bool TestRunner::shouldDumpUserGestureInFrameLoadCallbacks() const {
1578   return test_is_running_ && dump_user_gesture_in_frame_load_callbacks_;
1579 }
1580
1581 bool TestRunner::shouldDumpTitleChanges() const {
1582   return dump_title_changes_;
1583 }
1584
1585 bool TestRunner::shouldDumpIconChanges() const {
1586   return dump_icon_changes_;
1587 }
1588
1589 bool TestRunner::shouldDumpCreateView() const {
1590   return dump_create_view_;
1591 }
1592
1593 bool TestRunner::canOpenWindows() const {
1594   return can_open_windows_;
1595 }
1596
1597 bool TestRunner::shouldDumpResourceLoadCallbacks() const {
1598   return test_is_running_ && dump_resource_load_callbacks_;
1599 }
1600
1601 bool TestRunner::shouldDumpResourceRequestCallbacks() const {
1602   return test_is_running_ && dump_resource_request_callbacks_;
1603 }
1604
1605 bool TestRunner::shouldDumpResourceResponseMIMETypes() const {
1606   return test_is_running_ && dump_resource_reqponse_mime_types_;
1607 }
1608
1609 WebPermissionClient* TestRunner::webPermissions() const {
1610   return web_permissions_.get();
1611 }
1612
1613 bool TestRunner::shouldDumpStatusCallbacks() const {
1614   return dump_window_status_changes_;
1615 }
1616
1617 bool TestRunner::shouldDumpProgressFinishedCallback() const {
1618   return dump_progress_finished_callback_;
1619 }
1620
1621 bool TestRunner::shouldDumpSpellCheckCallbacks() const {
1622   return dump_spell_check_callbacks_;
1623 }
1624
1625 bool TestRunner::shouldDumpBackForwardList() const {
1626   return dump_back_forward_list_;
1627 }
1628
1629 bool TestRunner::shouldDumpSelectionRect() const {
1630   return dump_selection_rect_;
1631 }
1632
1633 bool TestRunner::testRepaint() const {
1634   return test_repaint_;
1635 }
1636
1637 bool TestRunner::sweepHorizontally() const {
1638   return sweep_horizontally_;
1639 }
1640
1641 bool TestRunner::isPrinting() const {
1642   return is_printing_;
1643 }
1644
1645 bool TestRunner::shouldStayOnPageAfterHandlingBeforeUnload() const {
1646   return should_stay_on_page_after_handling_before_unload_;
1647 }
1648
1649 bool TestRunner::shouldWaitUntilExternalURLLoad() const {
1650   return wait_until_external_url_load_;
1651 }
1652
1653 const std::set<std::string>* TestRunner::httpHeadersToClear() const {
1654   return &http_headers_to_clear_;
1655 }
1656
1657 void TestRunner::setTopLoadingFrame(WebFrame* frame, bool clear) {
1658   if (frame->top()->view() != web_view_)
1659     return;
1660   if (!test_is_running_)
1661     return;
1662   if (clear) {
1663     top_loading_frame_ = NULL;
1664     LocationChangeDone();
1665   } else if (!top_loading_frame_) {
1666     top_loading_frame_ = frame;
1667   }
1668 }
1669
1670 WebFrame* TestRunner::topLoadingFrame() const {
1671   return top_loading_frame_;
1672 }
1673
1674 void TestRunner::policyDelegateDone() {
1675   BLINK_ASSERT(wait_until_done_);
1676   delegate_->testFinished();
1677   wait_until_done_ = false;
1678 }
1679
1680 bool TestRunner::policyDelegateEnabled() const {
1681   return policy_delegate_enabled_;
1682 }
1683
1684 bool TestRunner::policyDelegateIsPermissive() const {
1685   return policy_delegate_is_permissive_;
1686 }
1687
1688 bool TestRunner::policyDelegateShouldNotifyDone() const {
1689   return policy_delegate_should_notify_done_;
1690 }
1691
1692 bool TestRunner::shouldInterceptPostMessage() const {
1693   return intercept_post_message_;
1694 }
1695
1696 bool TestRunner::shouldDumpResourcePriorities() const {
1697   return should_dump_resource_priorities_;
1698 }
1699
1700 WebNotificationPresenter* TestRunner::notification_presenter() const {
1701   return notification_presenter_.get();
1702 }
1703
1704 bool TestRunner::RequestPointerLock() {
1705   switch (pointer_lock_planned_result_) {
1706     case PointerLockWillSucceed:
1707       delegate_->postDelayedTask(
1708           new HostMethodTask(this, &TestRunner::DidAcquirePointerLockInternal),
1709           0);
1710       return true;
1711     case PointerLockWillRespondAsync:
1712       BLINK_ASSERT(!pointer_locked_);
1713       return true;
1714     case PointerLockWillFailSync:
1715       BLINK_ASSERT(!pointer_locked_);
1716       return false;
1717     default:
1718       BLINK_ASSERT_NOT_REACHED();
1719       return false;
1720   }
1721 }
1722
1723 void TestRunner::RequestPointerUnlock() {
1724   delegate_->postDelayedTask(
1725       new HostMethodTask(this, &TestRunner::DidLosePointerLockInternal), 0);
1726 }
1727
1728 bool TestRunner::isPointerLocked() {
1729   return pointer_locked_;
1730 }
1731
1732 void TestRunner::setToolTipText(const WebString& text) {
1733   tooltip_text_ = text.utf8();
1734 }
1735
1736 bool TestRunner::midiAccessorResult() {
1737   return midi_accessor_result_;
1738 }
1739
1740 void TestRunner::clearDevToolsLocalStorage() {
1741   delegate_->clearDevToolsLocalStorage();
1742 }
1743
1744 void TestRunner::showDevTools(const std::string& settings) {
1745   delegate_->showDevTools(settings);
1746 }
1747
1748 class WorkItemBackForward : public TestRunner::WorkItem {
1749  public:
1750   WorkItemBackForward(int distance) : distance_(distance) {}
1751
1752   virtual bool Run(WebTestDelegate* delegate, WebView*) OVERRIDE {
1753     delegate->goToOffset(distance_);
1754     return true; // FIXME: Did it really start a navigation?
1755   }
1756
1757  private:
1758   int distance_;
1759 };
1760
1761 void TestRunner::NotifyDone() {
1762   if (disable_notify_done_)
1763     return;
1764
1765   // Test didn't timeout. Kill the timeout timer.
1766   taskList()->revokeAll();
1767
1768   CompleteNotifyDone();
1769 }
1770
1771 void TestRunner::WaitUntilDone() {
1772   wait_until_done_ = true;
1773 }
1774
1775 void TestRunner::QueueBackNavigation(int how_far_back) {
1776   work_queue_.AddWork(new WorkItemBackForward(-how_far_back));
1777 }
1778
1779 void TestRunner::QueueForwardNavigation(int how_far_forward) {
1780   work_queue_.AddWork(new WorkItemBackForward(how_far_forward));
1781 }
1782
1783 class WorkItemReload : public TestRunner::WorkItem {
1784  public:
1785   virtual bool Run(WebTestDelegate* delegate, WebView*) OVERRIDE {
1786     delegate->reload();
1787     return true;
1788   }
1789 };
1790
1791 void TestRunner::QueueReload() {
1792   work_queue_.AddWork(new WorkItemReload());
1793 }
1794
1795 class WorkItemLoadingScript : public TestRunner::WorkItem {
1796  public:
1797   WorkItemLoadingScript(const std::string& script)
1798       : script_(script) {}
1799
1800   virtual bool Run(WebTestDelegate*, WebView* web_view) OVERRIDE {
1801     web_view->mainFrame()->executeScript(
1802         WebScriptSource(WebString::fromUTF8(script_)));
1803     return true; // FIXME: Did it really start a navigation?
1804   }
1805
1806  private:
1807   std::string script_;
1808 };
1809
1810 void TestRunner::QueueLoadingScript(const std::string& script) {
1811   work_queue_.AddWork(new WorkItemLoadingScript(script));
1812 }
1813
1814 class WorkItemNonLoadingScript : public TestRunner::WorkItem {
1815  public:
1816   WorkItemNonLoadingScript(const std::string& script)
1817       : script_(script) {}
1818
1819   virtual bool Run(WebTestDelegate*, WebView* web_view) OVERRIDE {
1820     web_view->mainFrame()->executeScript(
1821         WebScriptSource(WebString::fromUTF8(script_)));
1822     return false;
1823   }
1824
1825  private:
1826   std::string script_;
1827 };
1828
1829 void TestRunner::QueueNonLoadingScript(const std::string& script) {
1830   work_queue_.AddWork(new WorkItemNonLoadingScript(script));
1831 }
1832
1833 class WorkItemLoad : public TestRunner::WorkItem {
1834  public:
1835   WorkItemLoad(const WebURL& url, const std::string& target)
1836       : url_(url), target_(target) {}
1837
1838   virtual bool Run(WebTestDelegate* delegate, WebView*) OVERRIDE {
1839     delegate->loadURLForFrame(url_, target_);
1840     return true; // FIXME: Did it really start a navigation?
1841   }
1842
1843  private:
1844   WebURL url_;
1845   std::string target_;
1846 };
1847
1848 void TestRunner::QueueLoad(const std::string& url, const std::string& target) {
1849   // FIXME: Implement WebURL::resolve() and avoid GURL.
1850   GURL current_url = web_view_->mainFrame()->document().url();
1851   GURL full_url = current_url.Resolve(url);
1852   work_queue_.AddWork(new WorkItemLoad(full_url, target));
1853 }
1854
1855 class WorkItemLoadHTMLString : public TestRunner::WorkItem  {
1856  public:
1857   WorkItemLoadHTMLString(const std::string& html, const WebURL& base_url)
1858       : html_(html), base_url_(base_url) {}
1859
1860   WorkItemLoadHTMLString(const std::string& html, const WebURL& base_url,
1861                          const WebURL& unreachable_url)
1862       : html_(html), base_url_(base_url), unreachable_url_(unreachable_url) {}
1863
1864   virtual bool Run(WebTestDelegate*, WebView* web_view) OVERRIDE {
1865     web_view->mainFrame()->loadHTMLString(
1866         WebData(html_.data(), html_.length()),
1867         base_url_, unreachable_url_);
1868     return true;
1869   }
1870
1871  private:
1872   std::string html_;
1873   WebURL base_url_;
1874   WebURL unreachable_url_;
1875 };
1876
1877 void TestRunner::QueueLoadHTMLString(gin::Arguments* args) {
1878   std::string html;
1879   args->GetNext(&html);
1880
1881   std::string base_url_str;
1882   args->GetNext(&base_url_str);
1883   WebURL base_url = WebURL(GURL(base_url_str));
1884
1885   if (args->PeekNext()->IsString()) {
1886     std::string unreachable_url_str;
1887     args->GetNext(&unreachable_url_str);
1888     WebURL unreachable_url = WebURL(GURL(unreachable_url_str));
1889     work_queue_.AddWork(new WorkItemLoadHTMLString(html, base_url,
1890                                                    unreachable_url));
1891   } else {
1892     work_queue_.AddWork(new WorkItemLoadHTMLString(html, base_url));
1893   }
1894 }
1895
1896 void TestRunner::SetCustomPolicyDelegate(gin::Arguments* args) {
1897   args->GetNext(&policy_delegate_enabled_);
1898   if (!args->PeekNext().IsEmpty() && args->PeekNext()->IsBoolean())
1899     args->GetNext(&policy_delegate_is_permissive_);
1900 }
1901
1902 void TestRunner::WaitForPolicyDelegate() {
1903   policy_delegate_enabled_ = true;
1904   policy_delegate_should_notify_done_ = true;
1905   wait_until_done_ = true;
1906 }
1907
1908 int TestRunner::WindowCount() {
1909   return test_interfaces_->windowList().size();
1910 }
1911
1912 void TestRunner::SetCloseRemainingWindowsWhenComplete(
1913     bool close_remaining_windows) {
1914   close_remaining_windows_ = close_remaining_windows;
1915 }
1916
1917 void TestRunner::ResetTestHelperControllers() {
1918   test_interfaces_->resetTestHelperControllers();
1919 }
1920
1921 void TestRunner::SetTabKeyCyclesThroughElements(
1922     bool tab_key_cycles_through_elements) {
1923   web_view_->setTabKeyCyclesThroughElements(tab_key_cycles_through_elements);
1924 }
1925
1926 void TestRunner::ExecCommand(gin::Arguments* args) {
1927   std::string command;
1928   args->GetNext(&command);
1929
1930   std::string value;
1931   if (args->Length() >= 3) {
1932     // Ignore the second parameter (which is userInterface)
1933     // since this command emulates a manual action.
1934     args->Skip();
1935     args->GetNext(&value);
1936   }
1937
1938   // Note: webkit's version does not return the boolean, so neither do we.
1939   web_view_->focusedFrame()->executeCommand(WebString::fromUTF8(command),
1940                                             WebString::fromUTF8(value));
1941 }
1942
1943 bool TestRunner::IsCommandEnabled(const std::string& command) {
1944   return web_view_->focusedFrame()->isCommandEnabled(
1945       WebString::fromUTF8(command));
1946 }
1947
1948 bool TestRunner::CallShouldCloseOnWebView() {
1949   return web_view_->dispatchBeforeUnloadEvent();
1950 }
1951
1952 void TestRunner::SetDomainRelaxationForbiddenForURLScheme(
1953     bool forbidden, const std::string& scheme) {
1954   web_view_->setDomainRelaxationForbidden(forbidden,
1955                                           WebString::fromUTF8(scheme));
1956 }
1957
1958 v8::Handle<v8::Value> TestRunner::EvaluateScriptInIsolatedWorldAndReturnValue(
1959     int world_id,
1960     const std::string& script) {
1961   WebVector<v8::Local<v8::Value> > values;
1962   WebScriptSource source(WebString::fromUTF8(script));
1963   // This relies on the iframe focusing itself when it loads. This is a bit
1964   // sketchy, but it seems to be what other tests do.
1965   web_view_->focusedFrame()->executeScriptInIsolatedWorld(
1966       world_id, &source, 1, 1, &values);
1967   // Since only one script was added, only one result is expected
1968   if (values.size() == 1 && !values[0].IsEmpty())
1969     return values[0];
1970   return v8::Handle<v8::Value>();
1971 }
1972
1973 void TestRunner::EvaluateScriptInIsolatedWorld(int world_id,
1974                                                const std::string& script) {
1975   WebScriptSource source(WebString::fromUTF8(script));
1976   web_view_->focusedFrame()->executeScriptInIsolatedWorld(
1977       world_id, &source, 1, 1);
1978 }
1979
1980 void TestRunner::SetIsolatedWorldSecurityOrigin(int world_id,
1981                                                 v8::Handle<v8::Value> origin) {
1982   if (!(origin->IsString() || !origin->IsNull()))
1983     return;
1984
1985   WebSecurityOrigin web_origin;
1986   if (origin->IsString()) {
1987     web_origin = WebSecurityOrigin::createFromString(
1988         V8StringToWebString(origin->ToString()));
1989   }
1990   web_view_->focusedFrame()->setIsolatedWorldSecurityOrigin(world_id,
1991                                                             web_origin);
1992 }
1993
1994 void TestRunner::SetIsolatedWorldContentSecurityPolicy(
1995     int world_id,
1996     const std::string& policy) {
1997   web_view_->focusedFrame()->setIsolatedWorldContentSecurityPolicy(
1998       world_id, WebString::fromUTF8(policy));
1999 }
2000
2001 void TestRunner::AddOriginAccessWhitelistEntry(
2002     const std::string& source_origin,
2003     const std::string& destination_protocol,
2004     const std::string& destination_host,
2005     bool allow_destination_subdomains) {
2006   WebURL url((GURL(source_origin)));
2007   if (!url.isValid())
2008     return;
2009
2010   WebSecurityPolicy::addOriginAccessWhitelistEntry(
2011       url,
2012       WebString::fromUTF8(destination_protocol),
2013       WebString::fromUTF8(destination_host),
2014       allow_destination_subdomains);
2015 }
2016
2017 void TestRunner::RemoveOriginAccessWhitelistEntry(
2018     const std::string& source_origin,
2019     const std::string& destination_protocol,
2020     const std::string& destination_host,
2021     bool allow_destination_subdomains) {
2022   WebURL url((GURL(source_origin)));
2023   if (!url.isValid())
2024     return;
2025
2026   WebSecurityPolicy::removeOriginAccessWhitelistEntry(
2027       url,
2028       WebString::fromUTF8(destination_protocol),
2029       WebString::fromUTF8(destination_host),
2030       allow_destination_subdomains);
2031 }
2032
2033 bool TestRunner::HasCustomPageSizeStyle(int page_index) {
2034   WebFrame* frame = web_view_->mainFrame();
2035   if (!frame)
2036     return false;
2037   return frame->hasCustomPageSizeStyle(page_index);
2038 }
2039
2040 void TestRunner::ForceRedSelectionColors() {
2041   web_view_->setSelectionColors(0xffee0000, 0xff00ee00, 0xff000000, 0xffc0c0c0);
2042 }
2043
2044 void TestRunner::InjectStyleSheet(const std::string& source_code,
2045                                   bool all_frames) {
2046   WebView::injectStyleSheet(
2047       WebString::fromUTF8(source_code),
2048       WebVector<WebString>(),
2049       all_frames ? WebView::InjectStyleInAllFrames
2050                  : WebView::InjectStyleInTopFrameOnly);
2051 }
2052
2053 bool TestRunner::FindString(const std::string& search_text,
2054                             const std::vector<std::string>& options_array) {
2055   WebFindOptions find_options;
2056   bool wrap_around = false;
2057   find_options.matchCase = true;
2058   find_options.findNext = true;
2059
2060   for (size_t i = 0; i < options_array.size(); ++i) {
2061     const std::string& option = options_array[i];
2062     if (option == "CaseInsensitive")
2063       find_options.matchCase = false;
2064     else if (option == "Backwards")
2065       find_options.forward = false;
2066     else if (option == "StartInSelection")
2067       find_options.findNext = false;
2068     else if (option == "AtWordStarts")
2069       find_options.wordStart = true;
2070     else if (option == "TreatMedialCapitalAsWordStart")
2071       find_options.medialCapitalAsWordStart = true;
2072     else if (option == "WrapAround")
2073       wrap_around = true;
2074   }
2075
2076   WebFrame* frame = web_view_->mainFrame();
2077   const bool find_result = frame->find(0, WebString::fromUTF8(search_text),
2078                                        find_options, wrap_around, 0);
2079   frame->stopFinding(false);
2080   return find_result;
2081 }
2082
2083 std::string TestRunner::SelectionAsMarkup() {
2084   return web_view_->mainFrame()->selectionAsMarkup().utf8();
2085 }
2086
2087 void TestRunner::SetTextSubpixelPositioning(bool value) {
2088 #if defined(__linux__) || defined(ANDROID)
2089   // Since FontConfig doesn't provide a variable to control subpixel
2090   // positioning, we'll fall back to setting it globally for all fonts.
2091   WebFontRendering::setSubpixelPositioning(value);
2092 #endif
2093 }
2094
2095 void TestRunner::SetPageVisibility(const std::string& new_visibility) {
2096   if (new_visibility == "visible")
2097     web_view_->setVisibilityState(WebPageVisibilityStateVisible, false);
2098   else if (new_visibility == "hidden")
2099     web_view_->setVisibilityState(WebPageVisibilityStateHidden, false);
2100   else if (new_visibility == "prerender")
2101     web_view_->setVisibilityState(WebPageVisibilityStatePrerender, false);
2102 }
2103
2104 void TestRunner::SetTextDirection(const std::string& direction_name) {
2105   // Map a direction name to a WebTextDirection value.
2106   WebTextDirection direction;
2107   if (direction_name == "auto")
2108     direction = WebTextDirectionDefault;
2109   else if (direction_name == "rtl")
2110     direction = WebTextDirectionRightToLeft;
2111   else if (direction_name == "ltr")
2112     direction = WebTextDirectionLeftToRight;
2113   else
2114     return;
2115
2116   web_view_->setTextDirection(direction);
2117 }
2118
2119 void TestRunner::UseUnfortunateSynchronousResizeMode() {
2120   delegate_->useUnfortunateSynchronousResizeMode(true);
2121 }
2122
2123 bool TestRunner::EnableAutoResizeMode(int min_width,
2124                                       int min_height,
2125                                       int max_width,
2126                                       int max_height) {
2127   WebSize min_size(min_width, min_height);
2128   WebSize max_size(max_width, max_height);
2129   delegate_->enableAutoResizeMode(min_size, max_size);
2130   return true;
2131 }
2132
2133 bool TestRunner::DisableAutoResizeMode(int new_width, int new_height) {
2134   WebSize new_size(new_width, new_height);
2135   delegate_->disableAutoResizeMode(new_size);
2136   return true;
2137 }
2138
2139 void TestRunner::SetMockDeviceMotion(
2140     bool has_acceleration_x, double acceleration_x,
2141     bool has_acceleration_y, double acceleration_y,
2142     bool has_acceleration_z, double acceleration_z,
2143     bool has_acceleration_including_gravity_x,
2144     double acceleration_including_gravity_x,
2145     bool has_acceleration_including_gravity_y,
2146     double acceleration_including_gravity_y,
2147     bool has_acceleration_including_gravity_z,
2148     double acceleration_including_gravity_z,
2149     bool has_rotation_rate_alpha, double rotation_rate_alpha,
2150     bool has_rotation_rate_beta, double rotation_rate_beta,
2151     bool has_rotation_rate_gamma, double rotation_rate_gamma,
2152     double interval) {
2153   WebDeviceMotionData motion;
2154
2155   // acceleration
2156   motion.hasAccelerationX = has_acceleration_x;
2157   motion.accelerationX = acceleration_x;
2158   motion.hasAccelerationY = has_acceleration_y;
2159   motion.accelerationY = acceleration_y;
2160   motion.hasAccelerationZ = has_acceleration_z;
2161   motion.accelerationZ = acceleration_z;
2162
2163   // accelerationIncludingGravity
2164   motion.hasAccelerationIncludingGravityX =
2165       has_acceleration_including_gravity_x;
2166   motion.accelerationIncludingGravityX = acceleration_including_gravity_x;
2167   motion.hasAccelerationIncludingGravityY =
2168       has_acceleration_including_gravity_y;
2169   motion.accelerationIncludingGravityY = acceleration_including_gravity_y;
2170   motion.hasAccelerationIncludingGravityZ =
2171       has_acceleration_including_gravity_z;
2172   motion.accelerationIncludingGravityZ = acceleration_including_gravity_z;
2173
2174   // rotationRate
2175   motion.hasRotationRateAlpha = has_rotation_rate_alpha;
2176   motion.rotationRateAlpha = rotation_rate_alpha;
2177   motion.hasRotationRateBeta = has_rotation_rate_beta;
2178   motion.rotationRateBeta = rotation_rate_beta;
2179   motion.hasRotationRateGamma = has_rotation_rate_gamma;
2180   motion.rotationRateGamma = rotation_rate_gamma;
2181
2182   // interval
2183   motion.interval = interval;
2184
2185   delegate_->setDeviceMotionData(motion);
2186 }
2187
2188 void TestRunner::SetMockDeviceOrientation(bool has_alpha, double alpha,
2189                                           bool has_beta, double beta,
2190                                           bool has_gamma, double gamma,
2191                                           bool has_absolute, bool absolute) {
2192   WebDeviceOrientationData orientation;
2193
2194   // alpha
2195   orientation.hasAlpha = has_alpha;
2196   orientation.alpha = alpha;
2197
2198   // beta
2199   orientation.hasBeta = has_beta;
2200   orientation.beta = beta;
2201
2202   // gamma
2203   orientation.hasGamma = has_gamma;
2204   orientation.gamma = gamma;
2205
2206   // absolute
2207   orientation.hasAbsolute = has_absolute;
2208   orientation.absolute = absolute;
2209
2210   delegate_->setDeviceOrientationData(orientation);
2211 }
2212
2213 void TestRunner::SetMockScreenOrientation(const std::string& orientation_str) {
2214   blink::WebScreenOrientation orientation;
2215
2216   if (orientation_str == "portrait-primary") {
2217     orientation = WebScreenOrientationPortraitPrimary;
2218   } else if (orientation_str == "portrait-secondary") {
2219     orientation = WebScreenOrientationPortraitSecondary;
2220   } else if (orientation_str == "landscape-primary") {
2221     orientation = WebScreenOrientationLandscapePrimary;
2222   } else if (orientation_str == "landscape-secondary") {
2223     orientation = WebScreenOrientationLandscapeSecondary;
2224   }
2225
2226   delegate_->setScreenOrientation(orientation);
2227 }
2228
2229 void TestRunner::DidAcquirePointerLock() {
2230   DidAcquirePointerLockInternal();
2231 }
2232
2233 void TestRunner::DidNotAcquirePointerLock() {
2234   DidNotAcquirePointerLockInternal();
2235 }
2236
2237 void TestRunner::DidLosePointerLock() {
2238   DidLosePointerLockInternal();
2239 }
2240
2241 void TestRunner::SetPointerLockWillFailSynchronously() {
2242   pointer_lock_planned_result_ = PointerLockWillFailSync;
2243 }
2244
2245 void TestRunner::SetPointerLockWillRespondAsynchronously() {
2246   pointer_lock_planned_result_ = PointerLockWillRespondAsync;
2247 }
2248
2249 void TestRunner::SetPopupBlockingEnabled(bool block_popups) {
2250   delegate_->preferences()->javaScriptCanOpenWindowsAutomatically =
2251       !block_popups;
2252   delegate_->applyPreferences();
2253 }
2254
2255 void TestRunner::SetJavaScriptCanAccessClipboard(bool can_access) {
2256   delegate_->preferences()->javaScriptCanAccessClipboard = can_access;
2257   delegate_->applyPreferences();
2258 }
2259
2260 void TestRunner::SetXSSAuditorEnabled(bool enabled) {
2261   delegate_->preferences()->XSSAuditorEnabled = enabled;
2262   delegate_->applyPreferences();
2263 }
2264
2265 void TestRunner::SetAllowUniversalAccessFromFileURLs(bool allow) {
2266   delegate_->preferences()->allowUniversalAccessFromFileURLs = allow;
2267   delegate_->applyPreferences();
2268 }
2269
2270 void TestRunner::SetAllowFileAccessFromFileURLs(bool allow) {
2271   delegate_->preferences()->allowFileAccessFromFileURLs = allow;
2272   delegate_->applyPreferences();
2273 }
2274
2275 void TestRunner::OverridePreference(const std::string key,
2276                                     v8::Handle<v8::Value> value) {
2277   WebPreferences* prefs = delegate_->preferences();
2278   if (key == "WebKitDefaultFontSize") {
2279     prefs->defaultFontSize = value->Int32Value();
2280   } else if (key == "WebKitMinimumFontSize") {
2281     prefs->minimumFontSize = value->Int32Value();
2282   } else if (key == "WebKitDefaultTextEncodingName") {
2283     prefs->defaultTextEncodingName = V8StringToWebString(value->ToString());
2284   } else if (key == "WebKitJavaScriptEnabled") {
2285     prefs->javaScriptEnabled = value->BooleanValue();
2286   } else if (key == "WebKitSupportsMultipleWindows") {
2287     prefs->supportsMultipleWindows = value->BooleanValue();
2288   } else if (key == "WebKitDisplayImagesKey") {
2289     prefs->loadsImagesAutomatically = value->BooleanValue();
2290   } else if (key == "WebKitPluginsEnabled") {
2291     prefs->pluginsEnabled = value->BooleanValue();
2292   } else if (key == "WebKitJavaEnabled") {
2293     prefs->javaEnabled = value->BooleanValue();
2294   } else if (key == "WebKitOfflineWebApplicationCacheEnabled") {
2295     prefs->offlineWebApplicationCacheEnabled = value->BooleanValue();
2296   } else if (key == "WebKitTabToLinksPreferenceKey") {
2297     prefs->tabsToLinks = value->BooleanValue();
2298   } else if (key == "WebKitWebGLEnabled") {
2299     prefs->experimentalWebGLEnabled = value->BooleanValue();
2300   } else if (key == "WebKitCSSRegionsEnabled") {
2301     prefs->experimentalCSSRegionsEnabled = value->BooleanValue();
2302   } else if (key == "WebKitCSSGridLayoutEnabled") {
2303     prefs->experimentalCSSGridLayoutEnabled = value->BooleanValue();
2304   } else if (key == "WebKitHyperlinkAuditingEnabled") {
2305     prefs->hyperlinkAuditingEnabled = value->BooleanValue();
2306   } else if (key == "WebKitEnableCaretBrowsing") {
2307     prefs->caretBrowsingEnabled = value->BooleanValue();
2308   } else if (key == "WebKitAllowDisplayingInsecureContent") {
2309     prefs->allowDisplayOfInsecureContent = value->BooleanValue();
2310   } else if (key == "WebKitAllowRunningInsecureContent") {
2311     prefs->allowRunningOfInsecureContent = value->BooleanValue();
2312   } else if (key == "WebKitShouldRespectImageOrientation") {
2313     prefs->shouldRespectImageOrientation = value->BooleanValue();
2314   } else if (key == "WebKitWebAudioEnabled") {
2315     BLINK_ASSERT(value->BooleanValue());
2316   } else {
2317     std::string message("Invalid name for preference: ");
2318     message.append(key);
2319     delegate_->printMessage(std::string("CONSOLE MESSAGE: ") + message + "\n");
2320   }
2321   delegate_->applyPreferences();
2322 }
2323
2324 void TestRunner::SetPluginsEnabled(bool enabled) {
2325   delegate_->preferences()->pluginsEnabled = enabled;
2326   delegate_->applyPreferences();
2327 }
2328
2329 void TestRunner::DumpEditingCallbacks() {
2330   dump_editting_callbacks_ = true;
2331 }
2332
2333 void TestRunner::DumpAsText() {
2334   dump_as_text_ = true;
2335   generate_pixel_results_ = false;
2336 }
2337
2338 void TestRunner::DumpAsTextWithPixelResults() {
2339   dump_as_text_ = true;
2340   generate_pixel_results_ = true;
2341 }
2342
2343 void TestRunner::DumpChildFrameScrollPositions() {
2344   dump_child_frame_scroll_positions_ = true;
2345 }
2346
2347 void TestRunner::DumpChildFramesAsText() {
2348   dump_child_frames_as_text_ = true;
2349 }
2350
2351 void TestRunner::DumpIconChanges() {
2352   dump_icon_changes_ = true;
2353 }
2354
2355 void TestRunner::SetAudioData(const gin::ArrayBufferView& view) {
2356   unsigned char* bytes = static_cast<unsigned char*>(view.bytes());
2357   audio_data_.resize(view.num_bytes());
2358   std::copy(bytes, bytes + view.num_bytes(), audio_data_.begin());
2359   dump_as_audio_ = true;
2360 }
2361
2362 void TestRunner::DumpFrameLoadCallbacks() {
2363   dump_frame_load_callbacks_ = true;
2364 }
2365
2366 void TestRunner::DumpPingLoaderCallbacks() {
2367   dump_ping_loader_callbacks_ = true;
2368 }
2369
2370 void TestRunner::DumpUserGestureInFrameLoadCallbacks() {
2371   dump_user_gesture_in_frame_load_callbacks_ = true;
2372 }
2373
2374 void TestRunner::DumpTitleChanges() {
2375   dump_title_changes_ = true;
2376 }
2377
2378 void TestRunner::DumpCreateView() {
2379   dump_create_view_ = true;
2380 }
2381
2382 void TestRunner::SetCanOpenWindows() {
2383   can_open_windows_ = true;
2384 }
2385
2386 void TestRunner::DumpResourceLoadCallbacks() {
2387   dump_resource_load_callbacks_ = true;
2388 }
2389
2390 void TestRunner::DumpResourceRequestCallbacks() {
2391   dump_resource_request_callbacks_ = true;
2392 }
2393
2394 void TestRunner::DumpResourceResponseMIMETypes() {
2395   dump_resource_reqponse_mime_types_ = true;
2396 }
2397
2398 void TestRunner::SetImagesAllowed(bool allowed) {
2399   web_permissions_->setImagesAllowed(allowed);
2400 }
2401
2402 void TestRunner::SetScriptsAllowed(bool allowed) {
2403   web_permissions_->setScriptsAllowed(allowed);
2404 }
2405
2406 void TestRunner::SetStorageAllowed(bool allowed) {
2407   web_permissions_->setStorageAllowed(allowed);
2408 }
2409
2410 void TestRunner::SetPluginsAllowed(bool allowed) {
2411   web_permissions_->setPluginsAllowed(allowed);
2412 }
2413
2414 void TestRunner::SetAllowDisplayOfInsecureContent(bool allowed) {
2415   web_permissions_->setDisplayingInsecureContentAllowed(allowed);
2416 }
2417
2418 void TestRunner::SetAllowRunningOfInsecureContent(bool allowed) {
2419   web_permissions_->setRunningInsecureContentAllowed(allowed);
2420 }
2421
2422 void TestRunner::DumpPermissionClientCallbacks() {
2423   web_permissions_->setDumpCallbacks(true);
2424 }
2425
2426 void TestRunner::DumpWindowStatusChanges() {
2427   dump_window_status_changes_ = true;
2428 }
2429
2430 void TestRunner::DumpProgressFinishedCallback() {
2431   dump_progress_finished_callback_ = true;
2432 }
2433
2434 void TestRunner::DumpSpellCheckCallbacks() {
2435   dump_spell_check_callbacks_ = true;
2436 }
2437
2438 void TestRunner::DumpBackForwardList() {
2439   dump_back_forward_list_ = true;
2440 }
2441
2442 void TestRunner::DumpSelectionRect() {
2443   dump_selection_rect_ = true;
2444 }
2445
2446 void TestRunner::TestRepaint() {
2447   test_repaint_ = true;
2448 }
2449
2450 void TestRunner::RepaintSweepHorizontally() {
2451   sweep_horizontally_ = true;
2452 }
2453
2454 void TestRunner::SetPrinting() {
2455   is_printing_ = true;
2456 }
2457
2458 void TestRunner::SetShouldStayOnPageAfterHandlingBeforeUnload(bool value) {
2459   should_stay_on_page_after_handling_before_unload_ = value;
2460 }
2461
2462 void TestRunner::SetWillSendRequestClearHeader(const std::string& header) {
2463   if (!header.empty())
2464     http_headers_to_clear_.insert(header);
2465 }
2466
2467 void TestRunner::DumpResourceRequestPriorities() {
2468   should_dump_resource_priorities_ = true;
2469 }
2470
2471 void TestRunner::SetUseMockTheme(bool use) {
2472   use_mock_theme_ = use;
2473 }
2474
2475 void TestRunner::ShowWebInspector(const std::string& str) {
2476   showDevTools(str);
2477 }
2478
2479 void TestRunner::WaitUntilExternalURLLoad() {
2480   wait_until_external_url_load_ = true;
2481 }
2482
2483 void TestRunner::CloseWebInspector() {
2484   delegate_->closeDevTools();
2485 }
2486
2487 bool TestRunner::IsChooserShown() {
2488   return proxy_->isChooserShown();
2489 }
2490
2491 void TestRunner::EvaluateInWebInspector(int call_id,
2492                                         const std::string& script) {
2493   delegate_->evaluateInWebInspector(call_id, script);
2494 }
2495
2496 void TestRunner::ClearAllDatabases() {
2497   delegate_->clearAllDatabases();
2498 }
2499
2500 void TestRunner::SetDatabaseQuota(int quota) {
2501   delegate_->setDatabaseQuota(quota);
2502 }
2503
2504 void TestRunner::SetAlwaysAcceptCookies(bool accept) {
2505   delegate_->setAcceptAllCookies(accept);
2506 }
2507
2508 void TestRunner::SetWindowIsKey(bool value) {
2509   delegate_->setFocus(proxy_, value);
2510 }
2511
2512 std::string TestRunner::PathToLocalResource(const std::string& path) {
2513   return delegate_->pathToLocalResource(path);
2514 }
2515
2516 void TestRunner::SetBackingScaleFactor(double value,
2517                                        v8::Handle<v8::Function> callback) {
2518   delegate_->setDeviceScaleFactor(value);
2519   proxy_->discardBackingStore();
2520   delegate_->postTask(new InvokeCallbackTask(this, callback));
2521 }
2522
2523 void TestRunner::SetPOSIXLocale(const std::string& locale) {
2524   delegate_->setLocale(locale);
2525 }
2526
2527 void TestRunner::SetMIDIAccessorResult(bool result) {
2528   midi_accessor_result_ = result;
2529 }
2530
2531 void TestRunner::SetMIDISysexPermission(bool value) {
2532   const std::vector<WebTestProxyBase*>& windowList =
2533       test_interfaces_->windowList();
2534   for (unsigned i = 0; i < windowList.size(); ++i)
2535     windowList.at(i)->midiClientMock()->setSysexPermission(value);
2536 }
2537
2538 void TestRunner::GrantWebNotificationPermission(const std::string& origin,
2539                                                 bool permission_granted) {
2540   notification_presenter_->GrantPermission(origin, permission_granted);
2541 }
2542
2543 bool TestRunner::SimulateWebNotificationClick(const std::string& value) {
2544   return notification_presenter_->SimulateClick(value);
2545 }
2546
2547 void TestRunner::AddMockSpeechInputResult(const std::string& result,
2548                                           double confidence,
2549                                           const std::string& language) {
2550 #if ENABLE_INPUT_SPEECH
2551   proxy_->speechInputControllerMock()->addMockRecognitionResult(
2552       WebString::fromUTF8(result), confidence, WebString::fromUTF8(language));
2553 #endif
2554 }
2555
2556 void TestRunner::SetMockSpeechInputDumpRect(bool value) {
2557 #if ENABLE_INPUT_SPEECH
2558   proxy_->speechInputControllerMock()->setDumpRect(value);
2559 #endif
2560 }
2561
2562 void TestRunner::AddMockSpeechRecognitionResult(const std::string& transcript,
2563                                                 double confidence) {
2564   proxy_->speechRecognizerMock()->addMockResult(
2565       WebString::fromUTF8(transcript), confidence);
2566 }
2567
2568 void TestRunner::SetMockSpeechRecognitionError(const std::string& error,
2569                                                const std::string& message) {
2570   proxy_->speechRecognizerMock()->setError(WebString::fromUTF8(error),
2571                                            WebString::fromUTF8(message));
2572 }
2573
2574 bool TestRunner::WasMockSpeechRecognitionAborted() {
2575   return proxy_->speechRecognizerMock()->wasAborted();
2576 }
2577
2578 void TestRunner::AddWebPageOverlay() {
2579   if (web_view_ && !page_overlay_) {
2580     page_overlay_ = new TestPageOverlay(web_view_);
2581     web_view_->addPageOverlay(page_overlay_, 0);
2582   }
2583 }
2584
2585 void TestRunner::RemoveWebPageOverlay() {
2586   if (web_view_ && page_overlay_) {
2587     web_view_->removePageOverlay(page_overlay_);
2588     delete page_overlay_;
2589     page_overlay_ = NULL;
2590   }
2591 }
2592
2593 void TestRunner::Display() {
2594   proxy_->display();
2595 }
2596
2597 void TestRunner::DisplayInvalidatedRegion() {
2598   proxy_->displayInvalidatedRegion();
2599 }
2600
2601 void TestRunner::LocationChangeDone() {
2602   web_history_item_count_ = delegate_->navigationEntryCount();
2603
2604   // No more new work after the first complete load.
2605   work_queue_.set_frozen(true);
2606
2607   if (!wait_until_done_)
2608     work_queue_.ProcessWorkSoon();
2609 }
2610
2611 void TestRunner::CheckResponseMimeType() {
2612   // Text output: the test page can request different types of output which we
2613   // handle here.
2614   if (!dump_as_text_) {
2615     std::string mimeType =
2616         web_view_->mainFrame()->dataSource()->response().mimeType().utf8();
2617     if (mimeType == "text/plain") {
2618       dump_as_text_ = true;
2619       generate_pixel_results_ = false;
2620     }
2621   }
2622 }
2623
2624 void TestRunner::CompleteNotifyDone() {
2625   if (wait_until_done_ && !topLoadingFrame() && work_queue_.is_empty())
2626     delegate_->testFinished();
2627   wait_until_done_ = false;
2628 }
2629
2630 void TestRunner::DidAcquirePointerLockInternal() {
2631   pointer_locked_ = true;
2632   web_view_->didAcquirePointerLock();
2633
2634   // Reset planned result to default.
2635   pointer_lock_planned_result_ = PointerLockWillSucceed;
2636 }
2637
2638 void TestRunner::DidNotAcquirePointerLockInternal() {
2639   BLINK_ASSERT(!pointer_locked_);
2640   pointer_locked_ = false;
2641   web_view_->didNotAcquirePointerLock();
2642
2643   // Reset planned result to default.
2644   pointer_lock_planned_result_ = PointerLockWillSucceed;
2645 }
2646
2647 void TestRunner::DidLosePointerLockInternal() {
2648   bool was_locked = pointer_locked_;
2649   pointer_locked_ = false;
2650   if (was_locked)
2651     web_view_->didLosePointerLock();
2652 }
2653
2654 }  // namespace content