1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/shell/renderer/test_runner/WebTestProxy.h"
9 #include "base/callback_helpers.h"
10 #include "base/debug/trace_event.h"
11 #include "base/logging.h"
12 #include "content/shell/renderer/test_runner/event_sender.h"
13 #include "content/shell/renderer/test_runner/MockColorChooser.h"
14 #include "content/shell/renderer/test_runner/MockWebSpeechRecognizer.h"
15 #include "content/shell/renderer/test_runner/SpellCheckClient.h"
16 #include "content/shell/renderer/test_runner/TestCommon.h"
17 #include "content/shell/renderer/test_runner/TestInterfaces.h"
18 #include "content/shell/renderer/test_runner/TestPlugin.h"
19 #include "content/shell/renderer/test_runner/WebTestDelegate.h"
20 #include "content/shell/renderer/test_runner/WebTestInterfaces.h"
21 #include "content/shell/renderer/test_runner/WebTestRunner.h"
22 #include "content/shell/renderer/test_runner/WebUserMediaClientMock.h"
23 #include "content/shell/renderer/test_runner/accessibility_controller.h"
24 #include "content/shell/renderer/test_runner/test_runner.h"
25 // FIXME: Including platform_canvas.h here is a layering violation.
26 #include "skia/ext/platform_canvas.h"
27 #include "third_party/WebKit/public/platform/WebCString.h"
28 #include "third_party/WebKit/public/platform/WebURLError.h"
29 #include "third_party/WebKit/public/platform/WebURLRequest.h"
30 #include "third_party/WebKit/public/platform/WebURLResponse.h"
31 #include "third_party/WebKit/public/web/WebAXEnums.h"
32 #include "third_party/WebKit/public/web/WebAXObject.h"
33 #include "third_party/WebKit/public/web/WebCachedURLRequest.h"
34 #include "third_party/WebKit/public/web/WebConsoleMessage.h"
35 #include "third_party/WebKit/public/web/WebDataSource.h"
36 #include "third_party/WebKit/public/web/WebDocument.h"
37 #include "third_party/WebKit/public/web/WebElement.h"
38 #include "third_party/WebKit/public/web/WebHistoryItem.h"
39 #include "third_party/WebKit/public/web/WebLocalFrame.h"
40 #include "third_party/WebKit/public/web/WebMIDIClientMock.h"
41 #include "third_party/WebKit/public/web/WebNode.h"
42 #include "third_party/WebKit/public/web/WebPluginParams.h"
43 #include "third_party/WebKit/public/web/WebPrintParams.h"
44 #include "third_party/WebKit/public/web/WebRange.h"
45 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h"
46 #include "third_party/WebKit/public/web/WebView.h"
48 using namespace blink;
55 class HostMethodTask : public WebMethodTask<WebTestProxyBase> {
57 typedef void (WebTestProxyBase::*CallbackMethodType)();
58 HostMethodTask(WebTestProxyBase* object, CallbackMethodType callback)
59 : WebMethodTask<WebTestProxyBase>(object)
60 , m_callback(callback)
63 virtual void runIfValid() OVERRIDE { (m_object->*m_callback)(); }
66 CallbackMethodType m_callback;
69 class ClosureTask : public WebMethodTask<WebTestProxyBase> {
71 ClosureTask(WebTestProxyBase* object, base::Closure callback)
72 : WebMethodTask<WebTestProxyBase>(object), m_callback(callback) {}
74 virtual void runIfValid() OVERRIDE {
75 if (!m_callback.is_null())
80 base::Closure m_callback;
83 void printFrameDescription(WebTestDelegate* delegate, WebFrame* frame)
85 string name8 = frame->uniqueName().utf8();
86 if (frame == frame->view()->mainFrame()) {
87 if (!name8.length()) {
88 delegate->printMessage("main frame");
91 delegate->printMessage(string("main frame \"") + name8 + "\"");
94 if (!name8.length()) {
95 delegate->printMessage("frame (anonymous)");
98 delegate->printMessage(string("frame \"") + name8 + "\"");
101 void printFrameUserGestureStatus(WebTestDelegate* delegate, WebFrame* frame, const char* msg)
103 bool isUserGesture = WebUserGestureIndicator::isProcessingUserGesture();
104 delegate->printMessage(string("Frame with user gesture \"") + (isUserGesture ? "true" : "false") + "\"" + msg);
107 // Used to write a platform neutral file:/// URL by taking the
108 // filename and its directory. (e.g., converts
109 // "file:///tmp/foo/bar.txt" to just "bar.txt").
110 string descriptionSuitableForTestResult(const string& url)
112 if (url.empty() || string::npos == url.find("file://"))
115 size_t pos = url.rfind('/');
116 if (pos == string::npos || !pos)
117 return "ERROR:" + url;
118 pos = url.rfind('/', pos - 1);
119 if (pos == string::npos)
120 return "ERROR:" + url;
122 return url.substr(pos + 1);
125 void printResponseDescription(WebTestDelegate* delegate, const WebURLResponse& response)
127 if (response.isNull()) {
128 delegate->printMessage("(null)");
131 string url = response.url().spec();
133 snprintf(data, sizeof(data), "%d", response. httpStatusCode());
134 delegate->printMessage(string("<NSURLResponse ") + descriptionSuitableForTestResult(url) + ", http status code " + data + ">");
137 string URLDescription(const GURL& url)
139 if (url.SchemeIs("file"))
140 return url.ExtractFileName();
141 return url.possibly_invalid_spec();
144 string PriorityDescription(const WebURLRequest::Priority& priority)
147 case WebURLRequest::PriorityVeryLow:
149 case WebURLRequest::PriorityLow:
151 case WebURLRequest::PriorityMedium:
153 case WebURLRequest::PriorityHigh:
155 case WebURLRequest::PriorityVeryHigh:
157 case WebURLRequest::PriorityUnresolved:
163 void blockRequest(WebURLRequest& request)
165 request.setURL(GURL("255.255.255.255"));
168 bool isLocalhost(const string& host)
170 return host == "127.0.0.1" || host == "localhost";
173 bool hostIsUsedBySomeTestsToGenerateError(const string& host)
175 return host == "255.255.255.255";
178 // Used to write a platform neutral file:/// URL by only taking the filename
179 // (e.g., converts "file:///tmp/foo.txt" to just "foo.txt").
180 string urlSuitableForTestResult(const string& url)
182 if (url.empty() || string::npos == url.find("file://"))
185 size_t pos = url.rfind('/');
186 if (pos == string::npos) {
188 pos = url.rfind('\\');
189 if (pos == string::npos)
195 string filename = url.substr(pos + 1);
196 if (filename.empty())
197 return "file:"; // A WebKit test has this in its expected output.
201 // WebNavigationType debugging strings taken from PolicyDelegate.mm.
202 const char* linkClickedString = "link clicked";
203 const char* formSubmittedString = "form submitted";
204 const char* backForwardString = "back/forward";
205 const char* reloadString = "reload";
206 const char* formResubmittedString = "form resubmitted";
207 const char* otherString = "other";
208 const char* illegalString = "illegal value";
210 // Get a debugging string from a WebNavigationType.
211 const char* webNavigationTypeToString(WebNavigationType type)
214 case blink::WebNavigationTypeLinkClicked:
215 return linkClickedString;
216 case blink::WebNavigationTypeFormSubmitted:
217 return formSubmittedString;
218 case blink::WebNavigationTypeBackForward:
219 return backForwardString;
220 case blink::WebNavigationTypeReload:
222 case blink::WebNavigationTypeFormResubmitted:
223 return formResubmittedString;
224 case blink::WebNavigationTypeOther:
227 return illegalString;
230 string dumpDocumentText(WebFrame* frame)
232 // We use the document element's text instead of the body text here because
233 // not all documents have a body, such as XML documents.
234 WebElement documentElement = frame->document().documentElement();
235 if (documentElement.isNull())
237 return documentElement.innerText().utf8();
240 string dumpFramesAsText(WebFrame* frame, bool recursive)
244 // Add header for all but the main frame. Skip empty frames.
245 if (frame->parent() && !frame->document().documentElement().isNull()) {
246 result.append("\n--------\nFrame: '");
247 result.append(frame->uniqueName().utf8().data());
248 result.append("'\n--------\n");
251 result.append(dumpDocumentText(frame));
255 for (WebFrame* child = frame->firstChild(); child; child = child->nextSibling())
256 result.append(dumpFramesAsText(child, recursive));
262 string dumpFramesAsPrintedText(WebFrame* frame, bool recursive)
266 // Cannot do printed format for anything other than HTML
267 if (!frame->document().isHTMLDocument())
270 // Add header for all but the main frame. Skip empty frames.
271 if (frame->parent() && !frame->document().documentElement().isNull()) {
272 result.append("\n--------\nFrame: '");
273 result.append(frame->uniqueName().utf8().data());
274 result.append("'\n--------\n");
277 result.append(frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8());
281 for (WebFrame* child = frame->firstChild(); child; child = child->nextSibling())
282 result.append(dumpFramesAsPrintedText(child, recursive));
288 string dumpFrameScrollPosition(WebFrame* frame, bool recursive)
291 WebSize offset = frame->scrollOffset();
292 if (offset.width > 0 || offset.height > 0) {
294 result = string("frame '") + frame->uniqueName().utf8().data() + "' ";
296 snprintf(data, sizeof(data), "scrolled to %d,%d\n", offset.width, offset.height);
302 for (WebFrame* child = frame->firstChild(); child; child = child->nextSibling())
303 result += dumpFrameScrollPosition(child, recursive);
307 string dumpAllBackForwardLists(TestInterfaces* interfaces, WebTestDelegate* delegate)
310 const vector<WebTestProxyBase*>& windowList = interfaces->windowList();
311 for (unsigned i = 0; i < windowList.size(); ++i)
312 result.append(delegate->dumpHistoryForWindow(windowList.at(i)));
318 WebTestProxyBase::WebTestProxyBase()
319 : m_testInterfaces(0)
322 , m_spellcheck(new SpellCheckClient(this))
328 WebTestProxyBase::~WebTestProxyBase()
330 m_testInterfaces->windowClosed(this);
333 void WebTestProxyBase::setInterfaces(WebTestInterfaces* interfaces)
335 m_testInterfaces = interfaces->testInterfaces();
336 m_testInterfaces->windowOpened(this);
339 void WebTestProxyBase::setDelegate(WebTestDelegate* delegate)
341 m_delegate = delegate;
342 m_spellcheck->setDelegate(delegate);
343 if (m_speechRecognizer.get())
344 m_speechRecognizer->setDelegate(delegate);
347 void WebTestProxyBase::setWidget(WebWidget* widget)
349 m_webWidget = widget;
352 WebWidget* WebTestProxyBase::webWidget()
357 WebView* WebTestProxyBase::webView()
360 // TestRunner does not support popup widgets. So m_webWidget is always a WebView.
361 return static_cast<WebView*>(m_webWidget);
364 void WebTestProxyBase::didForceResize()
367 discardBackingStore();
370 void WebTestProxyBase::reset()
372 m_paintRect = WebRect();
374 m_isPainting = false;
375 m_animateScheduled = false;
376 m_resourceIdentifierMap.clear();
377 m_logConsoleOutput = true;
378 if (m_midiClient.get())
379 m_midiClient->resetMock();
382 WebSpellCheckClient* WebTestProxyBase::spellCheckClient() const
384 return m_spellcheck.get();
387 WebColorChooser* WebTestProxyBase::createColorChooser(WebColorChooserClient* client, const blink::WebColor& color, const blink::WebVector<blink::WebColorSuggestion>& suggestions)
389 // This instance is deleted by WebCore::ColorInputType
390 return new MockColorChooser(client, m_delegate, this);
393 bool WebTestProxyBase::runFileChooser(const blink::WebFileChooserParams&, blink::WebFileChooserCompletion*)
395 m_delegate->printMessage("Mock: Opening a file chooser.\n");
396 // FIXME: Add ability to set file names to a file upload control.
400 void WebTestProxyBase::showValidationMessage(const WebRect&, const WebString& message, const WebString& subMessage, WebTextDirection)
402 m_delegate->printMessage(std::string("ValidationMessageClient: main-message=") + std::string(message.utf8()) + " sub-message=" + std::string(subMessage.utf8()) + "\n");
405 void WebTestProxyBase::hideValidationMessage()
409 void WebTestProxyBase::moveValidationMessage(const WebRect&)
413 string WebTestProxyBase::captureTree(bool debugRenderTree)
415 bool shouldDumpCustomText = m_testInterfaces->testRunner()->shouldDumpAsCustomText();
416 bool shouldDumpAsText = m_testInterfaces->testRunner()->shouldDumpAsText();
417 bool shouldDumpAsMarkup = m_testInterfaces->testRunner()->shouldDumpAsMarkup();
418 bool shouldDumpAsPrinted = m_testInterfaces->testRunner()->isPrinting();
419 WebFrame* frame = webView()->mainFrame();
421 if (shouldDumpCustomText) {
422 // Append a newline for the test driver.
423 dataUtf8 = m_testInterfaces->testRunner()->customDumpText() + "\n";
424 } else if (shouldDumpAsText) {
425 bool recursive = m_testInterfaces->testRunner()->shouldDumpChildFramesAsText();
426 dataUtf8 = shouldDumpAsPrinted ? dumpFramesAsPrintedText(frame, recursive) : dumpFramesAsText(frame, recursive);
427 } else if (shouldDumpAsMarkup) {
428 // Append a newline for the test driver.
429 dataUtf8 = frame->contentAsMarkup().utf8() + "\n";
431 bool recursive = m_testInterfaces->testRunner()->shouldDumpChildFrameScrollPositions();
432 WebFrame::RenderAsTextControls renderTextBehavior = WebFrame::RenderAsTextNormal;
433 if (shouldDumpAsPrinted)
434 renderTextBehavior |= WebFrame::RenderAsTextPrinting;
436 renderTextBehavior |= WebFrame::RenderAsTextDebug;
437 dataUtf8 = frame->renderTreeAsText(renderTextBehavior).utf8();
438 dataUtf8 += dumpFrameScrollPosition(frame, recursive);
441 if (m_testInterfaces->testRunner()->shouldDumpBackForwardList())
442 dataUtf8 += dumpAllBackForwardLists(m_testInterfaces, m_delegate);
447 SkCanvas* WebTestProxyBase::capturePixels()
449 TRACE_EVENT0("shell", "WebTestProxyBase::capturePixels");
450 webWidget()->layout();
451 if (m_testInterfaces->testRunner()->isPrinting())
452 paintPagesWithBoundaries();
454 paintInvalidatedRegion();
456 DrawSelectionRect(canvas());
461 void WebTestProxyBase::DrawSelectionRect(SkCanvas* canvas) {
462 // See if we need to draw the selection bounds rect. Selection bounds
463 // rect is the rect enclosing the (possibly transformed) selection.
464 // The rect should be drawn after everything is laid out and painted.
465 if (!m_testInterfaces->testRunner()->shouldDumpSelectionRect())
467 // If there is a selection rect - draw a red 1px border enclosing rect
468 WebRect wr = webView()->mainFrame()->selectionBoundsRect();
471 // Render a red rectangle bounding selection rect
473 paint.setColor(0xFFFF0000); // Fully opaque red
474 paint.setStyle(SkPaint::kStroke_Style);
475 paint.setFlags(SkPaint::kAntiAlias_Flag);
476 paint.setStrokeWidth(1.0f);
477 SkIRect rect; // Bounding rect
478 rect.set(wr.x, wr.y, wr.x + wr.width, wr.y + wr.height);
479 canvas->drawIRect(rect, paint);
482 void WebTestProxyBase::didCompositeAndReadback(const SkBitmap& bitmap) {
483 TRACE_EVENT2("shell",
484 "WebTestProxyBase::didCompositeAndReadback",
486 bitmap.info().fWidth,
488 bitmap.info().fHeight);
489 SkCanvas canvas(bitmap);
490 DrawSelectionRect(&canvas);
491 DCHECK(!m_compositeAndReadbackCallbacks.empty());
492 m_compositeAndReadbackCallbacks.front().Run(bitmap);
493 m_compositeAndReadbackCallbacks.pop_front();
496 void WebTestProxyBase::CapturePixelsAsync(
497 base::Callback<void(const SkBitmap&)> callback) {
498 TRACE_EVENT0("shell", "WebTestProxyBase::CapturePixelsAsync");
500 DCHECK(webWidget()->isAcceleratedCompositingActive());
501 DCHECK(!callback.is_null());
502 m_compositeAndReadbackCallbacks.push_back(callback);
503 webWidget()->compositeAndReadbackAsync(this);
506 void WebTestProxyBase::setLogConsoleOutput(bool enabled)
508 m_logConsoleOutput = enabled;
511 void WebTestProxyBase::paintRect(const WebRect& rect)
513 DCHECK(!m_isPainting);
516 float deviceScaleFactor = webView()->deviceScaleFactor();
517 int scaledX = static_cast<int>(static_cast<float>(rect.x) * deviceScaleFactor);
518 int scaledY = static_cast<int>(static_cast<float>(rect.y) * deviceScaleFactor);
519 int scaledWidth = static_cast<int>(ceil(static_cast<float>(rect.width) * deviceScaleFactor));
520 int scaledHeight = static_cast<int>(ceil(static_cast<float>(rect.height) * deviceScaleFactor));
521 WebRect deviceRect(scaledX, scaledY, scaledWidth, scaledHeight);
522 webWidget()->paint(canvas(), deviceRect);
523 m_isPainting = false;
526 void WebTestProxyBase::paintInvalidatedRegion()
528 webWidget()->animate(0.0);
529 webWidget()->layout();
530 WebSize widgetSize = webWidget()->size();
531 WebRect clientRect(0, 0, widgetSize.width, widgetSize.height);
533 // Paint the canvas if necessary. Allow painting to generate extra rects
534 // for the first two calls. This is necessary because some WebCore rendering
535 // objects update their layout only when painted.
536 // Store the total area painted in total_paint. Then tell the gdk window
537 // to update that area after we're done painting it.
538 for (int i = 0; i < 3; ++i) {
539 // rect = intersect(m_paintRect , clientRect)
540 WebRect damageRect = m_paintRect;
541 int left = max(damageRect.x, clientRect.x);
542 int top = max(damageRect.y, clientRect.y);
543 int right = min(damageRect.x + damageRect.width, clientRect.x + clientRect.width);
544 int bottom = min(damageRect.y + damageRect.height, clientRect.y + clientRect.height);
546 if (left < right && top < bottom)
547 rect = WebRect(left, top, right - left, bottom - top);
549 m_paintRect = WebRect();
554 DCHECK(m_paintRect.isEmpty());
557 void WebTestProxyBase::paintPagesWithBoundaries()
559 DCHECK(!m_isPainting);
563 WebSize pageSizeInPixels = webWidget()->size();
564 WebFrame* webFrame = webView()->mainFrame();
566 int pageCount = webFrame->printBegin(pageSizeInPixels);
567 int totalHeight = pageCount * (pageSizeInPixels.height + 1) - 1;
569 SkCanvas* testCanvas = skia::TryCreateBitmapCanvas(pageSizeInPixels.width, totalHeight, false);
571 discardBackingStore();
572 m_canvas.reset(testCanvas);
574 webFrame->printEnd();
578 webFrame->printPagesWithBoundaries(canvas(), pageSizeInPixels);
579 webFrame->printEnd();
581 m_isPainting = false;
584 SkCanvas* WebTestProxyBase::canvas()
587 return m_canvas.get();
588 WebSize widgetSize = webWidget()->size();
589 float deviceScaleFactor = webView()->deviceScaleFactor();
590 int scaledWidth = static_cast<int>(ceil(static_cast<float>(widgetSize.width) * deviceScaleFactor));
591 int scaledHeight = static_cast<int>(ceil(static_cast<float>(widgetSize.height) * deviceScaleFactor));
592 // We're allocating the canvas to be non-opaque (third parameter), so we
593 // don't end up with uninitialized memory if a layout test doesn't damage
595 m_canvas.reset(skia::CreateBitmapCanvas(scaledWidth, scaledHeight, false));
596 return m_canvas.get();
599 void WebTestProxyBase::DisplayForSoftwareMode(const base::Closure& callback) {
600 const blink::WebSize& size = webWidget()->size();
601 WebRect rect(0, 0, size.width, size.height);
603 paintInvalidatedRegion();
605 if (!callback.is_null())
609 void WebTestProxyBase::DidDisplayAsync(const base::Closure& callback,
610 const SkBitmap& bitmap) {
611 // Verify we actually composited.
612 CHECK_NE(0, bitmap.info().fWidth);
613 CHECK_NE(0, bitmap.info().fHeight);
614 if (!callback.is_null())
618 void WebTestProxyBase::displayAsyncThen(base::Closure callback) {
619 TRACE_EVENT0("shell", "WebTestProxyBase::displayAsyncThen");
621 // TODO(danakj): Remove when we have kForceCompositingMode everywhere.
622 if (!webWidget()->isAcceleratedCompositingActive()) {
623 TRACE_EVENT0("shell",
624 "WebTestProxyBase::displayAsyncThen "
625 "isAcceleratedCompositingActive false");
626 m_delegate->postTask(
627 new ClosureTask(this,
628 base::Bind(&WebTestProxyBase::DisplayForSoftwareMode,
629 base::Unretained(this),
634 CapturePixelsAsync(base::Bind(
635 &WebTestProxyBase::DidDisplayAsync, base::Unretained(this), callback));
638 void WebTestProxyBase::discardBackingStore()
643 WebMIDIClientMock* WebTestProxyBase::midiClientMock()
645 if (!m_midiClient.get())
646 m_midiClient.reset(new WebMIDIClientMock);
647 return m_midiClient.get();
650 MockWebSpeechRecognizer* WebTestProxyBase::speechRecognizerMock()
652 if (!m_speechRecognizer.get()) {
653 m_speechRecognizer.reset(new MockWebSpeechRecognizer());
654 m_speechRecognizer->setDelegate(m_delegate);
656 return m_speechRecognizer.get();
659 void WebTestProxyBase::didInvalidateRect(const WebRect& rect)
661 // m_paintRect = m_paintRect U rect
664 if (m_paintRect.isEmpty()) {
668 int left = min(m_paintRect.x, rect.x);
669 int top = min(m_paintRect.y, rect.y);
670 int right = max(m_paintRect.x + m_paintRect.width, rect.x + rect.width);
671 int bottom = max(m_paintRect.y + m_paintRect.height, rect.y + rect.height);
672 m_paintRect = WebRect(left, top, right - left, bottom - top);
675 void WebTestProxyBase::didScrollRect(int, int, const WebRect& clipRect)
677 didInvalidateRect(clipRect);
680 void WebTestProxyBase::invalidateAll()
682 m_paintRect = WebRect(0, 0, INT_MAX, INT_MAX);
685 void WebTestProxyBase::scheduleComposite()
690 void WebTestProxyBase::scheduleAnimation()
692 if (!m_testInterfaces->testRunner()->TestIsRunning())
695 if (!m_animateScheduled) {
696 m_animateScheduled = true;
697 m_delegate->postDelayedTask(new HostMethodTask(this, &WebTestProxyBase::animateNow), 1);
701 void WebTestProxyBase::animateNow()
703 if (m_animateScheduled) {
704 m_animateScheduled = false;
705 webWidget()->animate(0.0);
706 webWidget()->layout();
710 bool WebTestProxyBase::isCompositorFramePending() const
712 return m_animateScheduled || !m_paintRect.isEmpty();
715 void WebTestProxyBase::show(WebNavigationPolicy)
720 void WebTestProxyBase::setWindowRect(const WebRect& rect)
723 discardBackingStore();
726 void WebTestProxyBase::didAutoResize(const WebSize&)
731 void WebTestProxyBase::postAccessibilityEvent(const blink::WebAXObject& obj, blink::WebAXEvent event)
733 // Only hook the accessibility events occured during the test run.
734 // This check prevents false positives in WebLeakDetector.
735 // The pending tasks in browser/renderer message queue may trigger accessibility events,
736 // and AccessibilityController will hold on to their target nodes if we don't ignore them here.
737 if (!m_testInterfaces->testRunner()->TestIsRunning())
740 if (event == blink::WebAXEventFocus)
741 m_testInterfaces->accessibilityController()->SetFocusedElement(obj);
743 const char* eventName = 0;
745 case blink::WebAXEventActiveDescendantChanged:
746 eventName = "ActiveDescendantChanged";
748 case blink::WebAXEventAlert:
751 case blink::WebAXEventAriaAttributeChanged:
752 eventName = "AriaAttributeChanged";
754 case blink::WebAXEventAutocorrectionOccured:
755 eventName = "AutocorrectionOccured";
757 case blink::WebAXEventBlur:
760 case blink::WebAXEventCheckedStateChanged:
761 eventName = "CheckedStateChanged";
763 case blink::WebAXEventChildrenChanged:
764 eventName = "ChildrenChanged";
766 case blink::WebAXEventFocus:
769 case blink::WebAXEventHide:
772 case blink::WebAXEventInvalidStatusChanged:
773 eventName = "InvalidStatusChanged";
775 case blink::WebAXEventLayoutComplete:
776 eventName = "LayoutComplete";
778 case blink::WebAXEventLiveRegionChanged:
779 eventName = "LiveRegionChanged";
781 case blink::WebAXEventLoadComplete:
782 eventName = "LoadComplete";
784 case blink::WebAXEventLocationChanged:
785 eventName = "LocationChanged";
787 case blink::WebAXEventMenuListItemSelected:
788 eventName = "MenuListItemSelected";
790 case blink::WebAXEventMenuListValueChanged:
791 eventName = "MenuListValueChanged";
793 case blink::WebAXEventRowCollapsed:
794 eventName = "RowCollapsed";
796 case blink::WebAXEventRowCountChanged:
797 eventName = "RowCountChanged";
799 case blink::WebAXEventRowExpanded:
800 eventName = "RowExpanded";
802 case blink::WebAXEventScrollPositionChanged:
803 eventName = "ScrollPositionChanged";
805 case blink::WebAXEventScrolledToAnchor:
806 eventName = "ScrolledToAnchor";
808 case blink::WebAXEventSelectedChildrenChanged:
809 eventName = "SelectedChildrenChanged";
811 case blink::WebAXEventSelectedTextChanged:
812 eventName = "SelectedTextChanged";
814 case blink::WebAXEventShow:
817 case blink::WebAXEventTextChanged:
818 eventName = "TextChanged";
820 case blink::WebAXEventTextInserted:
821 eventName = "TextInserted";
823 case blink::WebAXEventTextRemoved:
824 eventName = "TextRemoved";
826 case blink::WebAXEventValueChanged:
827 eventName = "ValueChanged";
830 eventName = "Unknown";
834 m_testInterfaces->accessibilityController()->NotificationReceived(obj, eventName);
836 if (m_testInterfaces->accessibilityController()->ShouldLogAccessibilityEvents()) {
837 string message("AccessibilityNotification - ");
838 message += eventName;
840 blink::WebNode node = obj.node();
841 if (!node.isNull() && node.isElementNode()) {
842 blink::WebElement element = node.to<blink::WebElement>();
843 if (element.hasAttribute("id")) {
845 message += element.getAttribute("id").utf8().data();
849 m_delegate->printMessage(message + "\n");
853 void WebTestProxyBase::startDragging(WebLocalFrame*, const WebDragData& data, WebDragOperationsMask mask, const WebImage&, const WebPoint&)
855 // When running a test, we need to fake a drag drop operation otherwise
856 // Windows waits for real mouse events to know when the drag is over.
857 m_testInterfaces->eventSender()->DoDragDrop(data, mask);
860 // The output from these methods in layout test mode should match that
861 // expected by the layout tests. See EditingDelegate.m in DumpRenderTree.
863 void WebTestProxyBase::didChangeSelection(bool isEmptySelection)
865 if (m_testInterfaces->testRunner()->shouldDumpEditingCallbacks())
866 m_delegate->printMessage("EDITING DELEGATE: webViewDidChangeSelection:WebViewDidChangeSelectionNotification\n");
869 void WebTestProxyBase::didChangeContents()
871 if (m_testInterfaces->testRunner()->shouldDumpEditingCallbacks())
872 m_delegate->printMessage("EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification\n");
875 bool WebTestProxyBase::createView(WebLocalFrame*, const WebURLRequest& request, const WebWindowFeatures&, const WebString&, WebNavigationPolicy, bool)
877 if (!m_testInterfaces->testRunner()->canOpenWindows())
879 if (m_testInterfaces->testRunner()->shouldDumpCreateView())
880 m_delegate->printMessage(string("createView(") + URLDescription(request.url()) + ")\n");
884 WebPlugin* WebTestProxyBase::createPlugin(WebLocalFrame* frame, const WebPluginParams& params)
886 if (TestPlugin::isSupportedMimeType(params.mimeType))
887 return TestPlugin::create(frame, params, m_delegate);
891 void WebTestProxyBase::setStatusText(const WebString& text)
893 if (!m_testInterfaces->testRunner()->shouldDumpStatusCallbacks())
895 m_delegate->printMessage(string("UI DELEGATE STATUS CALLBACK: setStatusText:") + text.utf8().data() + "\n");
898 void WebTestProxyBase::didStopLoading()
900 if (m_testInterfaces->testRunner()->shouldDumpProgressFinishedCallback())
901 m_delegate->printMessage("postProgressFinishedNotification\n");
904 void WebTestProxyBase::showContextMenu(WebLocalFrame*, const WebContextMenuData& contextMenuData)
906 m_testInterfaces->eventSender()->SetContextMenuData(contextMenuData);
909 WebUserMediaClient* WebTestProxyBase::userMediaClient()
911 if (!m_userMediaClient.get())
912 m_userMediaClient.reset(new WebUserMediaClientMock(m_delegate));
913 return m_userMediaClient.get();
916 // Simulate a print by going into print mode and then exit straight away.
917 void WebTestProxyBase::printPage(WebLocalFrame* frame)
919 WebSize pageSizeInPixels = webWidget()->size();
920 if (pageSizeInPixels.isEmpty())
922 WebPrintParams printParams(pageSizeInPixels);
923 frame->printBegin(printParams);
927 WebNotificationPresenter* WebTestProxyBase::notificationPresenter()
929 return m_testInterfaces->testRunner()->notification_presenter();
932 WebMIDIClient* WebTestProxyBase::webMIDIClient()
934 return midiClientMock();
937 WebSpeechRecognizer* WebTestProxyBase::speechRecognizer()
939 return speechRecognizerMock();
942 bool WebTestProxyBase::requestPointerLock()
944 return m_testInterfaces->testRunner()->RequestPointerLock();
947 void WebTestProxyBase::requestPointerUnlock()
949 m_testInterfaces->testRunner()->RequestPointerUnlock();
952 bool WebTestProxyBase::isPointerLocked()
954 return m_testInterfaces->testRunner()->isPointerLocked();
957 void WebTestProxyBase::didFocus()
959 m_delegate->setFocus(this, true);
962 void WebTestProxyBase::didBlur()
964 m_delegate->setFocus(this, false);
967 void WebTestProxyBase::setToolTipText(const WebString& text, WebTextDirection)
969 m_testInterfaces->testRunner()->setToolTipText(text);
972 void WebTestProxyBase::didOpenChooser()
977 void WebTestProxyBase::didCloseChooser()
982 bool WebTestProxyBase::isChooserShown()
984 return 0 < m_chooserCount;
987 void WebTestProxyBase::loadURLExternally(WebLocalFrame* frame, const WebURLRequest& request, WebNavigationPolicy policy, const WebString& suggested_name)
989 if (m_testInterfaces->testRunner()->shouldWaitUntilExternalURLLoad()) {
990 if (policy == WebNavigationPolicyDownload) {
991 m_delegate->printMessage(string("Downloading URL with suggested filename \"") + suggested_name.utf8() + "\"\n");
993 m_delegate->printMessage(string("Loading URL externally - \"") + URLDescription(request.url()) + "\"\n");
995 m_delegate->testFinished();
999 void WebTestProxyBase::didStartProvisionalLoad(WebLocalFrame* frame)
1001 if (!m_testInterfaces->testRunner()->topLoadingFrame())
1002 m_testInterfaces->testRunner()->setTopLoadingFrame(frame, false);
1004 if (m_testInterfaces->testRunner()->shouldDumpFrameLoadCallbacks()) {
1005 printFrameDescription(m_delegate, frame);
1006 m_delegate->printMessage(" - didStartProvisionalLoadForFrame\n");
1009 if (m_testInterfaces->testRunner()->shouldDumpUserGestureInFrameLoadCallbacks())
1010 printFrameUserGestureStatus(m_delegate, frame, " - in didStartProvisionalLoadForFrame\n");
1013 void WebTestProxyBase::didReceiveServerRedirectForProvisionalLoad(WebLocalFrame* frame)
1015 if (m_testInterfaces->testRunner()->shouldDumpFrameLoadCallbacks()) {
1016 printFrameDescription(m_delegate, frame);
1017 m_delegate->printMessage(" - didReceiveServerRedirectForProvisionalLoadForFrame\n");
1021 bool WebTestProxyBase::didFailProvisionalLoad(WebLocalFrame* frame, const WebURLError&)
1023 if (m_testInterfaces->testRunner()->shouldDumpFrameLoadCallbacks()) {
1024 printFrameDescription(m_delegate, frame);
1025 m_delegate->printMessage(" - didFailProvisionalLoadWithError\n");
1027 locationChangeDone(frame);
1028 return !frame->provisionalDataSource();
1031 void WebTestProxyBase::didCommitProvisionalLoad(WebLocalFrame* frame, const WebHistoryItem&, blink::WebHistoryCommitType)
1033 if (m_testInterfaces->testRunner()->shouldDumpFrameLoadCallbacks()) {
1034 printFrameDescription(m_delegate, frame);
1035 m_delegate->printMessage(" - didCommitLoadForFrame\n");
1039 void WebTestProxyBase::didReceiveTitle(WebLocalFrame* frame, const WebString& title, WebTextDirection direction)
1041 WebCString title8 = title.utf8();
1043 if (m_testInterfaces->testRunner()->shouldDumpFrameLoadCallbacks()) {
1044 printFrameDescription(m_delegate, frame);
1045 m_delegate->printMessage(string(" - didReceiveTitle: ") + title8.data() + "\n");
1048 if (m_testInterfaces->testRunner()->shouldDumpTitleChanges())
1049 m_delegate->printMessage(string("TITLE CHANGED: '") + title8.data() + "'\n");
1052 void WebTestProxyBase::didChangeIcon(WebLocalFrame* frame, WebIconURL::Type)
1054 if (m_testInterfaces->testRunner()->shouldDumpIconChanges()) {
1055 printFrameDescription(m_delegate, frame);
1056 m_delegate->printMessage(string(" - didChangeIcons\n"));
1060 void WebTestProxyBase::didFinishDocumentLoad(WebLocalFrame* frame)
1062 if (m_testInterfaces->testRunner()->shouldDumpFrameLoadCallbacks()) {
1063 printFrameDescription(m_delegate, frame);
1064 m_delegate->printMessage(" - didFinishDocumentLoadForFrame\n");
1066 unsigned pendingUnloadEvents = frame->unloadListenerCount();
1067 if (pendingUnloadEvents) {
1068 printFrameDescription(m_delegate, frame);
1070 snprintf(buffer, sizeof(buffer), " - has %u onunload handler(s)\n", pendingUnloadEvents);
1071 m_delegate->printMessage(buffer);
1076 void WebTestProxyBase::didHandleOnloadEvents(WebLocalFrame* frame)
1078 if (m_testInterfaces->testRunner()->shouldDumpFrameLoadCallbacks()) {
1079 printFrameDescription(m_delegate, frame);
1080 m_delegate->printMessage(" - didHandleOnloadEventsForFrame\n");
1084 void WebTestProxyBase::didFailLoad(WebLocalFrame* frame, const WebURLError&)
1086 if (m_testInterfaces->testRunner()->shouldDumpFrameLoadCallbacks()) {
1087 printFrameDescription(m_delegate, frame);
1088 m_delegate->printMessage(" - didFailLoadWithError\n");
1090 locationChangeDone(frame);
1093 void WebTestProxyBase::didFinishLoad(WebLocalFrame* frame)
1095 if (m_testInterfaces->testRunner()->shouldDumpFrameLoadCallbacks()) {
1096 printFrameDescription(m_delegate, frame);
1097 m_delegate->printMessage(" - didFinishLoadForFrame\n");
1099 locationChangeDone(frame);
1102 void WebTestProxyBase::didDetectXSS(WebLocalFrame*, const WebURL&, bool)
1104 if (m_testInterfaces->testRunner()->shouldDumpFrameLoadCallbacks())
1105 m_delegate->printMessage("didDetectXSS\n");
1108 void WebTestProxyBase::didDispatchPingLoader(WebLocalFrame*, const WebURL& url)
1110 if (m_testInterfaces->testRunner()->shouldDumpPingLoaderCallbacks())
1111 m_delegate->printMessage(string("PingLoader dispatched to '") + URLDescription(url).c_str() + "'.\n");
1114 void WebTestProxyBase::willRequestResource(WebLocalFrame* frame, const blink::WebCachedURLRequest& request)
1116 if (m_testInterfaces->testRunner()->shouldDumpResourceRequestCallbacks()) {
1117 printFrameDescription(m_delegate, frame);
1118 m_delegate->printMessage(string(" - ") + request.initiatorName().utf8().data());
1119 m_delegate->printMessage(string(" requested '") + URLDescription(request.urlRequest().url()).c_str() + "'\n");
1123 void WebTestProxyBase::willSendRequest(WebLocalFrame*, unsigned identifier, blink::WebURLRequest& request, const blink::WebURLResponse& redirectResponse)
1125 // Need to use GURL for host() and SchemeIs()
1126 GURL url = request.url();
1127 string requestURL = url.possibly_invalid_spec();
1129 GURL mainDocumentURL = request.firstPartyForCookies();
1131 if (redirectResponse.isNull() && (m_testInterfaces->testRunner()->shouldDumpResourceLoadCallbacks() || m_testInterfaces->testRunner()->shouldDumpResourcePriorities())) {
1132 DCHECK(m_resourceIdentifierMap.find(identifier) == m_resourceIdentifierMap.end());
1133 m_resourceIdentifierMap[identifier] = descriptionSuitableForTestResult(requestURL);
1136 if (m_testInterfaces->testRunner()->shouldDumpResourceLoadCallbacks()) {
1137 if (m_resourceIdentifierMap.find(identifier) == m_resourceIdentifierMap.end())
1138 m_delegate->printMessage("<unknown>");
1140 m_delegate->printMessage(m_resourceIdentifierMap[identifier]);
1141 m_delegate->printMessage(" - willSendRequest <NSURLRequest URL ");
1142 m_delegate->printMessage(descriptionSuitableForTestResult(requestURL).c_str());
1143 m_delegate->printMessage(", main document URL ");
1144 m_delegate->printMessage(URLDescription(mainDocumentURL).c_str());
1145 m_delegate->printMessage(", http method ");
1146 m_delegate->printMessage(request.httpMethod().utf8().data());
1147 m_delegate->printMessage("> redirectResponse ");
1148 printResponseDescription(m_delegate, redirectResponse);
1149 m_delegate->printMessage("\n");
1152 if (m_testInterfaces->testRunner()->shouldDumpResourcePriorities()) {
1153 m_delegate->printMessage(descriptionSuitableForTestResult(requestURL).c_str());
1154 m_delegate->printMessage(" has priority ");
1155 m_delegate->printMessage(PriorityDescription(request.priority()));
1156 m_delegate->printMessage("\n");
1159 if (m_testInterfaces->testRunner()->httpHeadersToClear()) {
1160 const set<string> *clearHeaders = m_testInterfaces->testRunner()->httpHeadersToClear();
1161 for (set<string>::const_iterator header = clearHeaders->begin(); header != clearHeaders->end(); ++header)
1162 request.clearHTTPHeaderField(WebString::fromUTF8(*header));
1165 string host = url.host();
1166 if (!host.empty() && (url.SchemeIs("http") || url.SchemeIs("https"))) {
1167 if (!isLocalhost(host) && !hostIsUsedBySomeTestsToGenerateError(host)
1168 && ((!mainDocumentURL.SchemeIs("http") && !mainDocumentURL.SchemeIs("https")) || isLocalhost(mainDocumentURL.host()))
1169 && !m_delegate->allowExternalPages()) {
1170 m_delegate->printMessage(string("Blocked access to external URL ") + requestURL + "\n");
1171 blockRequest(request);
1176 // Set the new substituted URL.
1177 request.setURL(m_delegate->rewriteLayoutTestsURL(request.url().spec()));
1180 void WebTestProxyBase::didReceiveResponse(WebLocalFrame*, unsigned identifier, const blink::WebURLResponse& response)
1182 if (m_testInterfaces->testRunner()->shouldDumpResourceLoadCallbacks()) {
1183 if (m_resourceIdentifierMap.find(identifier) == m_resourceIdentifierMap.end())
1184 m_delegate->printMessage("<unknown>");
1186 m_delegate->printMessage(m_resourceIdentifierMap[identifier]);
1187 m_delegate->printMessage(" - didReceiveResponse ");
1188 printResponseDescription(m_delegate, response);
1189 m_delegate->printMessage("\n");
1191 if (m_testInterfaces->testRunner()->shouldDumpResourceResponseMIMETypes()) {
1192 GURL url = response.url();
1193 WebString mimeType = response.mimeType();
1194 m_delegate->printMessage(url.ExtractFileName());
1195 m_delegate->printMessage(" has MIME type ");
1196 // Simulate NSURLResponse's mapping of empty/unknown MIME types to application/octet-stream
1197 m_delegate->printMessage(mimeType.isEmpty() ? "application/octet-stream" : mimeType.utf8().data());
1198 m_delegate->printMessage("\n");
1202 void WebTestProxyBase::didChangeResourcePriority(WebLocalFrame*, unsigned identifier, const blink::WebURLRequest::Priority& priority, int intra_priority_value)
1204 if (m_testInterfaces->testRunner()->shouldDumpResourcePriorities()) {
1205 if (m_resourceIdentifierMap.find(identifier) == m_resourceIdentifierMap.end())
1206 m_delegate->printMessage("<unknown>");
1208 m_delegate->printMessage(m_resourceIdentifierMap[identifier]);
1209 m_delegate->printMessage(" changed priority to ");
1210 m_delegate->printMessage(PriorityDescription(priority));
1212 snprintf(buffer, sizeof(buffer), ", intra_priority %d", intra_priority_value);
1213 m_delegate->printMessage(buffer);
1214 m_delegate->printMessage("\n");
1218 void WebTestProxyBase::didFinishResourceLoad(WebLocalFrame*, unsigned identifier)
1220 if (m_testInterfaces->testRunner()->shouldDumpResourceLoadCallbacks()) {
1221 if (m_resourceIdentifierMap.find(identifier) == m_resourceIdentifierMap.end())
1222 m_delegate->printMessage("<unknown>");
1224 m_delegate->printMessage(m_resourceIdentifierMap[identifier]);
1225 m_delegate->printMessage(" - didFinishLoading\n");
1227 m_resourceIdentifierMap.erase(identifier);
1230 void WebTestProxyBase::didAddMessageToConsole(const WebConsoleMessage& message, const WebString& sourceName, unsigned sourceLine)
1232 // This matches win DumpRenderTree's UIDelegate.cpp.
1233 if (!m_logConsoleOutput)
1236 switch (message.level) {
1237 case WebConsoleMessage::LevelDebug:
1240 case WebConsoleMessage::LevelLog:
1243 case WebConsoleMessage::LevelInfo:
1246 case WebConsoleMessage::LevelWarning:
1249 case WebConsoleMessage::LevelError:
1253 m_delegate->printMessage(string("CONSOLE ") + level + ": ");
1256 snprintf(buffer, sizeof(buffer), "line %d: ", sourceLine);
1257 m_delegate->printMessage(buffer);
1259 if (!message.text.isEmpty()) {
1261 newMessage = message.text.utf8();
1262 size_t fileProtocol = newMessage.find("file://");
1263 if (fileProtocol != string::npos) {
1264 newMessage = newMessage.substr(0, fileProtocol)
1265 + urlSuitableForTestResult(newMessage.substr(fileProtocol));
1267 m_delegate->printMessage(newMessage);
1269 m_delegate->printMessage(string("\n"));
1272 void WebTestProxyBase::locationChangeDone(WebFrame* frame)
1274 if (frame != m_testInterfaces->testRunner()->topLoadingFrame())
1276 m_testInterfaces->testRunner()->setTopLoadingFrame(frame, true);
1279 WebNavigationPolicy WebTestProxyBase::decidePolicyForNavigation(WebLocalFrame*, WebDataSource::ExtraData*, const WebURLRequest& request, WebNavigationType type, WebNavigationPolicy defaultPolicy, bool isRedirect)
1281 WebNavigationPolicy result;
1282 if (!m_testInterfaces->testRunner()->policyDelegateEnabled())
1283 return defaultPolicy;
1285 m_delegate->printMessage(string("Policy delegate: attempt to load ") + URLDescription(request.url()) + " with navigation type '" + webNavigationTypeToString(type) + "'\n");
1286 if (m_testInterfaces->testRunner()->policyDelegateIsPermissive())
1287 result = blink::WebNavigationPolicyCurrentTab;
1289 result = blink::WebNavigationPolicyIgnore;
1291 if (m_testInterfaces->testRunner()->policyDelegateShouldNotifyDone())
1292 m_testInterfaces->testRunner()->policyDelegateDone();
1296 bool WebTestProxyBase::willCheckAndDispatchMessageEvent(WebLocalFrame*, WebFrame*, WebSecurityOrigin, WebDOMMessageEvent)
1298 if (m_testInterfaces->testRunner()->shouldInterceptPostMessage()) {
1299 m_delegate->printMessage("intercepted postMessage\n");
1306 void WebTestProxyBase::postSpellCheckEvent(const WebString& eventName)
1308 if (m_testInterfaces->testRunner()->shouldDumpSpellCheckCallbacks()) {
1309 m_delegate->printMessage(string("SpellCheckEvent: ") + eventName.utf8().data() + "\n");
1313 void WebTestProxyBase::resetInputMethod()
1315 // If a composition text exists, then we need to let the browser process
1316 // to cancel the input method's ongoing composition session.
1318 m_webWidget->confirmComposition();
1321 } // namespace content