OLD | NEW |
| (Empty) |
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/web_test_proxy.h" | |
6 | |
7 #include <cctype> | |
8 | |
9 #include "base/callback_helpers.h" | |
10 #include "base/command_line.h" | |
11 #include "base/logging.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "base/strings/stringprintf.h" | |
14 #include "base/strings/utf_string_conversions.h" | |
15 #include "base/trace_event/trace_event.h" | |
16 #include "content/shell/renderer/test_runner/accessibility_controller.h" | |
17 #include "content/shell/renderer/test_runner/event_sender.h" | |
18 #include "content/shell/renderer/test_runner/mock_color_chooser.h" | |
19 #include "content/shell/renderer/test_runner/mock_credential_manager_client.h" | |
20 #include "content/shell/renderer/test_runner/mock_screen_orientation_client.h" | |
21 #include "content/shell/renderer/test_runner/mock_web_speech_recognizer.h" | |
22 #include "content/shell/renderer/test_runner/mock_web_user_media_client.h" | |
23 #include "content/shell/renderer/test_runner/spell_check_client.h" | |
24 #include "content/shell/renderer/test_runner/test_interfaces.h" | |
25 #include "content/shell/renderer/test_runner/test_plugin.h" | |
26 #include "content/shell/renderer/test_runner/test_runner.h" | |
27 #include "content/shell/renderer/test_runner/web_test_delegate.h" | |
28 #include "content/shell/renderer/test_runner/web_test_interfaces.h" | |
29 #include "content/shell/renderer/test_runner/web_test_runner.h" | |
30 // FIXME: Including platform_canvas.h here is a layering violation. | |
31 #include "skia/ext/platform_canvas.h" | |
32 #include "third_party/WebKit/public/platform/Platform.h" | |
33 #include "third_party/WebKit/public/platform/WebCString.h" | |
34 #include "third_party/WebKit/public/platform/WebClipboard.h" | |
35 #include "third_party/WebKit/public/platform/WebCompositeAndReadbackAsyncCallbac
k.h" | |
36 #include "third_party/WebKit/public/platform/WebLayoutAndPaintAsyncCallback.h" | |
37 #include "third_party/WebKit/public/platform/WebURLError.h" | |
38 #include "third_party/WebKit/public/platform/WebURLRequest.h" | |
39 #include "third_party/WebKit/public/platform/WebURLResponse.h" | |
40 #include "third_party/WebKit/public/web/WebAXEnums.h" | |
41 #include "third_party/WebKit/public/web/WebAXObject.h" | |
42 #include "third_party/WebKit/public/web/WebCachedURLRequest.h" | |
43 #include "third_party/WebKit/public/web/WebConsoleMessage.h" | |
44 #include "third_party/WebKit/public/web/WebDataSource.h" | |
45 #include "third_party/WebKit/public/web/WebDocument.h" | |
46 #include "third_party/WebKit/public/web/WebElement.h" | |
47 #include "third_party/WebKit/public/web/WebHistoryItem.h" | |
48 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
49 #include "third_party/WebKit/public/web/WebNode.h" | |
50 #include "third_party/WebKit/public/web/WebPagePopup.h" | |
51 #include "third_party/WebKit/public/web/WebPluginParams.h" | |
52 #include "third_party/WebKit/public/web/WebPrintParams.h" | |
53 #include "third_party/WebKit/public/web/WebRange.h" | |
54 #include "third_party/WebKit/public/web/WebUserGestureIndicator.h" | |
55 #include "third_party/WebKit/public/web/WebView.h" | |
56 #include "third_party/WebKit/public/web/WebWidgetClient.h" | |
57 | |
58 namespace content { | |
59 | |
60 namespace { | |
61 | |
62 class CaptureCallback : public blink::WebCompositeAndReadbackAsyncCallback { | |
63 public: | |
64 CaptureCallback(const base::Callback<void(const SkBitmap&)>& callback); | |
65 virtual ~CaptureCallback(); | |
66 | |
67 void set_wait_for_popup(bool wait) { wait_for_popup_ = wait; } | |
68 void set_popup_position(const gfx::Point& position) { | |
69 popup_position_ = position; | |
70 } | |
71 | |
72 // WebCompositeAndReadbackAsyncCallback implementation. | |
73 virtual void didCompositeAndReadback(const SkBitmap& bitmap); | |
74 | |
75 private: | |
76 base::Callback<void(const SkBitmap&)> callback_; | |
77 SkBitmap main_bitmap_; | |
78 bool wait_for_popup_; | |
79 gfx::Point popup_position_; | |
80 }; | |
81 | |
82 class LayoutAndPaintCallback : public blink::WebLayoutAndPaintAsyncCallback { | |
83 public: | |
84 LayoutAndPaintCallback(const base::Closure& callback) | |
85 : callback_(callback), wait_for_popup_(false) { | |
86 } | |
87 virtual ~LayoutAndPaintCallback() { | |
88 } | |
89 | |
90 void set_wait_for_popup(bool wait) { wait_for_popup_ = wait; } | |
91 | |
92 // WebLayoutAndPaintAsyncCallback implementation. | |
93 virtual void didLayoutAndPaint(); | |
94 | |
95 private: | |
96 base::Closure callback_; | |
97 bool wait_for_popup_; | |
98 }; | |
99 | |
100 class HostMethodTask : public WebMethodTask<WebTestProxyBase> { | |
101 public: | |
102 typedef void (WebTestProxyBase::*CallbackMethodType)(); | |
103 HostMethodTask(WebTestProxyBase* object, CallbackMethodType callback) | |
104 : WebMethodTask<WebTestProxyBase>(object), callback_(callback) {} | |
105 | |
106 void RunIfValid() override { (object_->*callback_)(); } | |
107 | |
108 private: | |
109 CallbackMethodType callback_; | |
110 }; | |
111 | |
112 void PrintFrameDescription(WebTestDelegate* delegate, blink::WebFrame* frame) { | |
113 std::string name8 = frame->uniqueName().utf8(); | |
114 if (frame == frame->view()->mainFrame()) { | |
115 if (!name8.length()) { | |
116 delegate->PrintMessage("main frame"); | |
117 return; | |
118 } | |
119 delegate->PrintMessage(std::string("main frame \"") + name8 + "\""); | |
120 return; | |
121 } | |
122 if (!name8.length()) { | |
123 delegate->PrintMessage("frame (anonymous)"); | |
124 return; | |
125 } | |
126 delegate->PrintMessage(std::string("frame \"") + name8 + "\""); | |
127 } | |
128 | |
129 void PrintFrameuserGestureStatus(WebTestDelegate* delegate, | |
130 blink::WebFrame* frame, | |
131 const char* msg) { | |
132 bool is_user_gesture = | |
133 blink::WebUserGestureIndicator::isProcessingUserGesture(); | |
134 delegate->PrintMessage(std::string("Frame with user gesture \"") + | |
135 (is_user_gesture ? "true" : "false") + "\"" + msg); | |
136 } | |
137 | |
138 // Used to write a platform neutral file:/// URL by taking the | |
139 // filename and its directory. (e.g., converts | |
140 // "file:///tmp/foo/bar.txt" to just "bar.txt"). | |
141 std::string DescriptionSuitableForTestResult(const std::string& url) { | |
142 if (url.empty() || std::string::npos == url.find("file://")) | |
143 return url; | |
144 | |
145 size_t pos = url.rfind('/'); | |
146 if (pos == std::string::npos || !pos) | |
147 return "ERROR:" + url; | |
148 pos = url.rfind('/', pos - 1); | |
149 if (pos == std::string::npos) | |
150 return "ERROR:" + url; | |
151 | |
152 return url.substr(pos + 1); | |
153 } | |
154 | |
155 void PrintResponseDescription(WebTestDelegate* delegate, | |
156 const blink::WebURLResponse& response) { | |
157 if (response.isNull()) { | |
158 delegate->PrintMessage("(null)"); | |
159 return; | |
160 } | |
161 delegate->PrintMessage(base::StringPrintf( | |
162 "<NSURLResponse %s, http status code %d>", | |
163 DescriptionSuitableForTestResult(response.url().spec()).c_str(), | |
164 response.httpStatusCode())); | |
165 } | |
166 | |
167 std::string URLDescription(const GURL& url) { | |
168 if (url.SchemeIs(url::kFileScheme)) | |
169 return url.ExtractFileName(); | |
170 return url.possibly_invalid_spec(); | |
171 } | |
172 | |
173 std::string PriorityDescription( | |
174 const blink::WebURLRequest::Priority& priority) { | |
175 switch (priority) { | |
176 case blink::WebURLRequest::PriorityVeryLow: | |
177 return "VeryLow"; | |
178 case blink::WebURLRequest::PriorityLow: | |
179 return "Low"; | |
180 case blink::WebURLRequest::PriorityMedium: | |
181 return "Medium"; | |
182 case blink::WebURLRequest::PriorityHigh: | |
183 return "High"; | |
184 case blink::WebURLRequest::PriorityVeryHigh: | |
185 return "VeryHigh"; | |
186 case blink::WebURLRequest::PriorityUnresolved: | |
187 default: | |
188 return "Unresolved"; | |
189 } | |
190 } | |
191 | |
192 void BlockRequest(blink::WebURLRequest& request) { | |
193 request.setURL(GURL("255.255.255.255")); | |
194 } | |
195 | |
196 bool IsLocalHost(const std::string& host) { | |
197 return host == "127.0.0.1" || host == "localhost"; | |
198 } | |
199 | |
200 bool IsTestHost(const std::string& host) { | |
201 return EndsWith(host, ".test", false); | |
202 } | |
203 | |
204 bool HostIsUsedBySomeTestsToGenerateError(const std::string& host) { | |
205 return host == "255.255.255.255"; | |
206 } | |
207 | |
208 // Used to write a platform neutral file:/// URL by only taking the filename | |
209 // (e.g., converts "file:///tmp/foo.txt" to just "foo.txt"). | |
210 std::string URLSuitableForTestResult(const std::string& url) { | |
211 if (url.empty() || std::string::npos == url.find("file://")) | |
212 return url; | |
213 | |
214 size_t pos = url.rfind('/'); | |
215 if (pos == std::string::npos) { | |
216 #ifdef WIN32 | |
217 pos = url.rfind('\\'); | |
218 if (pos == std::string::npos) | |
219 pos = 0; | |
220 #else | |
221 pos = 0; | |
222 #endif | |
223 } | |
224 std::string filename = url.substr(pos + 1); | |
225 if (filename.empty()) | |
226 return "file:"; // A WebKit test has this in its expected output. | |
227 return filename; | |
228 } | |
229 | |
230 // WebNavigationType debugging strings taken from PolicyDelegate.mm. | |
231 const char* kLinkClickedString = "link clicked"; | |
232 const char* kFormSubmittedString = "form submitted"; | |
233 const char* kBackForwardString = "back/forward"; | |
234 const char* kReloadString = "reload"; | |
235 const char* kFormResubmittedString = "form resubmitted"; | |
236 const char* kOtherString = "other"; | |
237 const char* kIllegalString = "illegal value"; | |
238 | |
239 // Get a debugging string from a WebNavigationType. | |
240 const char* WebNavigationTypeToString(blink::WebNavigationType type) { | |
241 switch (type) { | |
242 case blink::WebNavigationTypeLinkClicked: | |
243 return kLinkClickedString; | |
244 case blink::WebNavigationTypeFormSubmitted: | |
245 return kFormSubmittedString; | |
246 case blink::WebNavigationTypeBackForward: | |
247 return kBackForwardString; | |
248 case blink::WebNavigationTypeReload: | |
249 return kReloadString; | |
250 case blink::WebNavigationTypeFormResubmitted: | |
251 return kFormResubmittedString; | |
252 case blink::WebNavigationTypeOther: | |
253 return kOtherString; | |
254 } | |
255 return kIllegalString; | |
256 } | |
257 | |
258 const char* kPolicyIgnore = "Ignore"; | |
259 const char* kPolicyDownload = "download"; | |
260 const char* kPolicyCurrentTab = "current tab"; | |
261 const char* kPolicyNewBackgroundTab = "new background tab"; | |
262 const char* kPolicyNewForegroundTab = "new foreground tab"; | |
263 const char* kPolicyNewWindow = "new window"; | |
264 const char* kPolicyNewPopup = "new popup"; | |
265 | |
266 const char* WebNavigationPolicyToString(blink::WebNavigationPolicy policy) { | |
267 switch (policy) { | |
268 case blink::WebNavigationPolicyIgnore: | |
269 return kPolicyIgnore; | |
270 case blink::WebNavigationPolicyDownload: | |
271 return kPolicyDownload; | |
272 case blink::WebNavigationPolicyCurrentTab: | |
273 return kPolicyCurrentTab; | |
274 case blink::WebNavigationPolicyNewBackgroundTab: | |
275 return kPolicyNewBackgroundTab; | |
276 case blink::WebNavigationPolicyNewForegroundTab: | |
277 return kPolicyNewForegroundTab; | |
278 case blink::WebNavigationPolicyNewWindow: | |
279 return kPolicyNewWindow; | |
280 case blink::WebNavigationPolicyNewPopup: | |
281 return kPolicyNewPopup; | |
282 } | |
283 return kIllegalString; | |
284 } | |
285 | |
286 std::string DumpFrameHeaderIfNeeded(blink::WebFrame* frame) { | |
287 std::string result; | |
288 | |
289 // Add header for all but the main frame. Skip empty frames. | |
290 if (frame->parent() && !frame->document().documentElement().isNull()) { | |
291 result.append("\n--------\nFrame: '"); | |
292 result.append(frame->uniqueName().utf8().data()); | |
293 result.append("'\n--------\n"); | |
294 } | |
295 | |
296 return result; | |
297 } | |
298 | |
299 std::string DumpFramesAsMarkup(blink::WebFrame* frame, bool recursive) { | |
300 std::string result = DumpFrameHeaderIfNeeded(frame); | |
301 result.append(frame->contentAsMarkup().utf8()); | |
302 result.append("\n"); | |
303 | |
304 if (recursive) { | |
305 for (blink::WebFrame* child = frame->firstChild(); child; | |
306 child = child->nextSibling()) | |
307 result.append(DumpFramesAsMarkup(child, recursive)); | |
308 } | |
309 | |
310 return result; | |
311 } | |
312 | |
313 std::string DumpDocumentText(blink::WebFrame* frame) { | |
314 return frame->document().contentAsTextForTesting().utf8(); | |
315 } | |
316 | |
317 std::string DumpFramesAsText(blink::WebFrame* frame, bool recursive) { | |
318 std::string result = DumpFrameHeaderIfNeeded(frame); | |
319 result.append(DumpDocumentText(frame)); | |
320 result.append("\n"); | |
321 | |
322 if (recursive) { | |
323 for (blink::WebFrame* child = frame->firstChild(); child; | |
324 child = child->nextSibling()) | |
325 result.append(DumpFramesAsText(child, recursive)); | |
326 } | |
327 | |
328 return result; | |
329 } | |
330 | |
331 std::string DumpFramesAsPrintedText(blink::WebFrame* frame, bool recursive) { | |
332 // Cannot do printed format for anything other than HTML | |
333 if (!frame->document().isHTMLDocument()) | |
334 return std::string(); | |
335 | |
336 std::string result = DumpFrameHeaderIfNeeded(frame); | |
337 result.append( | |
338 frame->layoutTreeAsText(blink::WebFrame::LayoutAsTextPrinting).utf8()); | |
339 result.append("\n"); | |
340 | |
341 if (recursive) { | |
342 for (blink::WebFrame* child = frame->firstChild(); child; | |
343 child = child->nextSibling()) | |
344 result.append(DumpFramesAsPrintedText(child, recursive)); | |
345 } | |
346 | |
347 return result; | |
348 } | |
349 | |
350 std::string DumpFrameScrollPosition(blink::WebFrame* frame, bool recursive) { | |
351 std::string result; | |
352 blink::WebSize offset = frame->scrollOffset(); | |
353 if (offset.width > 0 || offset.height > 0) { | |
354 if (frame->parent()) { | |
355 result = | |
356 std::string("frame '") + frame->uniqueName().utf8().data() + "' "; | |
357 } | |
358 base::StringAppendF( | |
359 &result, "scrolled to %d,%d\n", offset.width, offset.height); | |
360 } | |
361 | |
362 if (!recursive) | |
363 return result; | |
364 for (blink::WebFrame* child = frame->firstChild(); child; | |
365 child = child->nextSibling()) | |
366 result += DumpFrameScrollPosition(child, recursive); | |
367 return result; | |
368 } | |
369 | |
370 std::string DumpAllBackForwardLists(TestInterfaces* interfaces, | |
371 WebTestDelegate* delegate) { | |
372 std::string result; | |
373 const std::vector<WebTestProxyBase*>& window_list = | |
374 interfaces->GetWindowList(); | |
375 for (size_t i = 0; i < window_list.size(); ++i) | |
376 result.append(delegate->DumpHistoryForWindow(window_list.at(i))); | |
377 return result; | |
378 } | |
379 } | |
380 | |
381 WebTestProxyBase::WebTestProxyBase() | |
382 : test_interfaces_(NULL), | |
383 delegate_(NULL), | |
384 web_widget_(NULL), | |
385 spellcheck_(new SpellCheckClient(this)), | |
386 chooser_count_(0) { | |
387 Reset(); | |
388 } | |
389 | |
390 WebTestProxyBase::~WebTestProxyBase() { | |
391 test_interfaces_->WindowClosed(this); | |
392 } | |
393 | |
394 void WebTestProxyBase::SetInterfaces(WebTestInterfaces* interfaces) { | |
395 test_interfaces_ = interfaces->GetTestInterfaces(); | |
396 test_interfaces_->WindowOpened(this); | |
397 } | |
398 | |
399 void WebTestProxyBase::SetDelegate(WebTestDelegate* delegate) { | |
400 delegate_ = delegate; | |
401 spellcheck_->SetDelegate(delegate); | |
402 if (speech_recognizer_.get()) | |
403 speech_recognizer_->SetDelegate(delegate); | |
404 } | |
405 | |
406 blink::WebView* WebTestProxyBase::GetWebView() const { | |
407 DCHECK(web_widget_); | |
408 // TestRunner does not support popup widgets. So |web_widget|_ is always a | |
409 // WebView. | |
410 return static_cast<blink::WebView*>(web_widget_); | |
411 } | |
412 | |
413 void WebTestProxyBase::Reset() { | |
414 drag_image_.reset(); | |
415 animate_scheduled_ = false; | |
416 resource_identifier_map_.clear(); | |
417 log_console_output_ = true; | |
418 accept_languages_ = ""; | |
419 } | |
420 | |
421 blink::WebSpellCheckClient* WebTestProxyBase::GetSpellCheckClient() const { | |
422 return spellcheck_.get(); | |
423 } | |
424 | |
425 blink::WebColorChooser* WebTestProxyBase::CreateColorChooser( | |
426 blink::WebColorChooserClient* client, | |
427 const blink::WebColor& color, | |
428 const blink::WebVector<blink::WebColorSuggestion>& suggestions) { | |
429 // This instance is deleted by WebCore::ColorInputType | |
430 return new MockColorChooser(client, delegate_, this); | |
431 } | |
432 | |
433 bool WebTestProxyBase::RunFileChooser( | |
434 const blink::WebFileChooserParams& params, | |
435 blink::WebFileChooserCompletion* completion) { | |
436 delegate_->PrintMessage("Mock: Opening a file chooser.\n"); | |
437 // FIXME: Add ability to set file names to a file upload control. | |
438 return false; | |
439 } | |
440 | |
441 void WebTestProxyBase::ShowValidationMessage( | |
442 const base::string16& message, | |
443 const base::string16& sub_message) { | |
444 delegate_->PrintMessage("ValidationMessageClient: main-message=" + | |
445 base::UTF16ToUTF8(message) + " sub-message=" + | |
446 base::UTF16ToUTF8(sub_message) + "\n"); | |
447 } | |
448 | |
449 std::string WebTestProxyBase::CaptureTree(bool debug_render_tree) { | |
450 bool should_dump_custom_text = | |
451 test_interfaces_->GetTestRunner()->shouldDumpAsCustomText(); | |
452 bool should_dump_as_text = | |
453 test_interfaces_->GetTestRunner()->shouldDumpAsText(); | |
454 bool should_dump_as_markup = | |
455 test_interfaces_->GetTestRunner()->shouldDumpAsMarkup(); | |
456 bool should_dump_as_printed = test_interfaces_->GetTestRunner()->isPrinting(); | |
457 blink::WebFrame* frame = GetWebView()->mainFrame(); | |
458 std::string data_utf8; | |
459 if (should_dump_custom_text) { | |
460 // Append a newline for the test driver. | |
461 data_utf8 = test_interfaces_->GetTestRunner()->customDumpText() + "\n"; | |
462 } else if (should_dump_as_text) { | |
463 bool recursive = | |
464 test_interfaces_->GetTestRunner()->shouldDumpChildFramesAsText(); | |
465 data_utf8 = should_dump_as_printed ? | |
466 DumpFramesAsPrintedText(frame, recursive) : | |
467 DumpFramesAsText(frame, recursive); | |
468 } else if (should_dump_as_markup) { | |
469 bool recursive = | |
470 test_interfaces_->GetTestRunner()->shouldDumpChildFramesAsMarkup(); | |
471 // Append a newline for the test driver. | |
472 data_utf8 = DumpFramesAsMarkup(frame, recursive); | |
473 } else { | |
474 bool recursive = test_interfaces_->GetTestRunner() | |
475 ->shouldDumpChildFrameScrollPositions(); | |
476 blink::WebFrame::LayoutAsTextControls layout_text_behavior = | |
477 blink::WebFrame::LayoutAsTextNormal; | |
478 if (should_dump_as_printed) | |
479 layout_text_behavior |= blink::WebFrame::LayoutAsTextPrinting; | |
480 if (debug_render_tree) | |
481 layout_text_behavior |= blink::WebFrame::LayoutAsTextDebug; | |
482 data_utf8 = frame->layoutTreeAsText(layout_text_behavior).utf8(); | |
483 data_utf8 += DumpFrameScrollPosition(frame, recursive); | |
484 } | |
485 | |
486 if (test_interfaces_->GetTestRunner()->ShouldDumpBackForwardList()) | |
487 data_utf8 += DumpAllBackForwardLists(test_interfaces_, delegate_); | |
488 | |
489 return data_utf8; | |
490 } | |
491 | |
492 void WebTestProxyBase::DrawSelectionRect(SkCanvas* canvas) { | |
493 // See if we need to draw the selection bounds rect. Selection bounds | |
494 // rect is the rect enclosing the (possibly transformed) selection. | |
495 // The rect should be drawn after everything is laid out and painted. | |
496 if (!test_interfaces_->GetTestRunner()->shouldDumpSelectionRect()) | |
497 return; | |
498 // If there is a selection rect - draw a red 1px border enclosing rect | |
499 blink::WebRect wr = GetWebView()->mainFrame()->selectionBoundsRect(); | |
500 if (wr.isEmpty()) | |
501 return; | |
502 // Render a red rectangle bounding selection rect | |
503 SkPaint paint; | |
504 paint.setColor(0xFFFF0000); // Fully opaque red | |
505 paint.setStyle(SkPaint::kStroke_Style); | |
506 paint.setFlags(SkPaint::kAntiAlias_Flag); | |
507 paint.setStrokeWidth(1.0f); | |
508 SkIRect rect; // Bounding rect | |
509 rect.set(wr.x, wr.y, wr.x + wr.width, wr.y + wr.height); | |
510 canvas->drawIRect(rect, paint); | |
511 } | |
512 | |
513 void WebTestProxyBase::SetAcceptLanguages(const std::string& accept_languages) { | |
514 bool notify = accept_languages_ != accept_languages; | |
515 accept_languages_ = accept_languages; | |
516 | |
517 if (notify) | |
518 GetWebView()->acceptLanguagesChanged(); | |
519 } | |
520 | |
521 void WebTestProxyBase::CopyImageAtAndCapturePixels( | |
522 int x, int y, const base::Callback<void(const SkBitmap&)>& callback) { | |
523 DCHECK(!callback.is_null()); | |
524 uint64_t sequence_number = blink::Platform::current()->clipboard()-> | |
525 sequenceNumber(blink::WebClipboard::Buffer()); | |
526 GetWebView()->copyImageAt(blink::WebPoint(x, y)); | |
527 if (sequence_number == blink::Platform::current()->clipboard()-> | |
528 sequenceNumber(blink::WebClipboard::Buffer())) { | |
529 SkBitmap emptyBitmap; | |
530 callback.Run(emptyBitmap); | |
531 return; | |
532 } | |
533 | |
534 blink::WebData data = blink::Platform::current()->clipboard()->readImage( | |
535 blink::WebClipboard::Buffer()); | |
536 blink::WebImage image = blink::WebImage::fromData(data, blink::WebSize()); | |
537 const SkBitmap& bitmap = image.getSkBitmap(); | |
538 SkAutoLockPixels autoLock(bitmap); | |
539 callback.Run(bitmap); | |
540 } | |
541 | |
542 void WebTestProxyBase::CapturePixelsForPrinting( | |
543 const base::Callback<void(const SkBitmap&)>& callback) { | |
544 web_widget_->layout(); | |
545 | |
546 blink::WebSize page_size_in_pixels = web_widget_->size(); | |
547 blink::WebFrame* web_frame = GetWebView()->mainFrame(); | |
548 | |
549 int page_count = web_frame->printBegin(page_size_in_pixels); | |
550 int totalHeight = page_count * (page_size_in_pixels.height + 1) - 1; | |
551 | |
552 bool is_opaque = false; | |
553 skia::RefPtr<SkCanvas> canvas(skia::AdoptRef(skia::TryCreateBitmapCanvas( | |
554 page_size_in_pixels.width, totalHeight, is_opaque))); | |
555 if (!canvas) { | |
556 callback.Run(SkBitmap()); | |
557 return; | |
558 } | |
559 web_frame->printPagesWithBoundaries(canvas.get(), page_size_in_pixels); | |
560 web_frame->printEnd(); | |
561 | |
562 DrawSelectionRect(canvas.get()); | |
563 SkBaseDevice* device = skia::GetTopDevice(*canvas); | |
564 const SkBitmap& bitmap = device->accessBitmap(false); | |
565 callback.Run(bitmap); | |
566 } | |
567 | |
568 CaptureCallback::CaptureCallback( | |
569 const base::Callback<void(const SkBitmap&)>& callback) | |
570 : callback_(callback), wait_for_popup_(false) { | |
571 } | |
572 | |
573 CaptureCallback::~CaptureCallback() { | |
574 } | |
575 | |
576 void CaptureCallback::didCompositeAndReadback(const SkBitmap& bitmap) { | |
577 TRACE_EVENT2("shell", | |
578 "CaptureCallback::didCompositeAndReadback", | |
579 "x", | |
580 bitmap.info().width(), | |
581 "y", | |
582 bitmap.info().height()); | |
583 if (!wait_for_popup_) { | |
584 callback_.Run(bitmap); | |
585 delete this; | |
586 return; | |
587 } | |
588 if (main_bitmap_.isNull()) { | |
589 bitmap.deepCopyTo(&main_bitmap_); | |
590 return; | |
591 } | |
592 SkCanvas canvas(main_bitmap_); | |
593 canvas.drawBitmap(bitmap, popup_position_.x(), popup_position_.y()); | |
594 callback_.Run(main_bitmap_); | |
595 delete this; | |
596 } | |
597 | |
598 void WebTestProxyBase::CapturePixelsAsync( | |
599 const base::Callback<void(const SkBitmap&)>& callback) { | |
600 TRACE_EVENT0("shell", "WebTestProxyBase::CapturePixelsAsync"); | |
601 DCHECK(!callback.is_null()); | |
602 | |
603 if (test_interfaces_->GetTestRunner()->shouldDumpDragImage()) { | |
604 if (drag_image_.isNull()) { | |
605 // This means the test called dumpDragImage but did not initiate a drag. | |
606 // Return a blank image so that the test fails. | |
607 SkBitmap bitmap; | |
608 bitmap.allocN32Pixels(1, 1); | |
609 { | |
610 SkAutoLockPixels lock(bitmap); | |
611 bitmap.eraseColor(0); | |
612 } | |
613 callback.Run(bitmap); | |
614 return; | |
615 } | |
616 | |
617 callback.Run(drag_image_.getSkBitmap()); | |
618 return; | |
619 } | |
620 | |
621 if (test_interfaces_->GetTestRunner()->isPrinting()) { | |
622 base::MessageLoopProxy::current()->PostTask( | |
623 FROM_HERE, | |
624 base::Bind(&WebTestProxyBase::CapturePixelsForPrinting, | |
625 base::Unretained(this), | |
626 callback)); | |
627 return; | |
628 } | |
629 | |
630 CaptureCallback* capture_callback = new CaptureCallback(base::Bind( | |
631 &WebTestProxyBase::DidCapturePixelsAsync, base::Unretained(this), | |
632 callback)); | |
633 web_widget_->compositeAndReadbackAsync(capture_callback); | |
634 if (blink::WebPagePopup* popup = web_widget_->pagePopup()) { | |
635 capture_callback->set_wait_for_popup(true); | |
636 capture_callback->set_popup_position(popup->positionRelativeToOwner()); | |
637 popup->compositeAndReadbackAsync(capture_callback); | |
638 } | |
639 } | |
640 | |
641 void WebTestProxyBase::DidCapturePixelsAsync(const base::Callback<void(const SkB
itmap&)>& callback, | |
642 const SkBitmap& bitmap) { | |
643 SkCanvas canvas(bitmap); | |
644 DrawSelectionRect(&canvas); | |
645 if (!callback.is_null()) | |
646 callback.Run(bitmap); | |
647 } | |
648 | |
649 void WebTestProxyBase::SetLogConsoleOutput(bool enabled) { | |
650 log_console_output_ = enabled; | |
651 } | |
652 | |
653 void LayoutAndPaintCallback::didLayoutAndPaint() { | |
654 TRACE_EVENT0("shell", "LayoutAndPaintCallback::didLayoutAndPaint"); | |
655 if (wait_for_popup_) { | |
656 wait_for_popup_ = false; | |
657 return; | |
658 } | |
659 | |
660 if (!callback_.is_null()) | |
661 callback_.Run(); | |
662 delete this; | |
663 } | |
664 | |
665 void WebTestProxyBase::LayoutAndPaintAsyncThen(const base::Closure& callback) { | |
666 TRACE_EVENT0("shell", "WebTestProxyBase::LayoutAndPaintAsyncThen"); | |
667 | |
668 LayoutAndPaintCallback* layout_and_paint_callback = | |
669 new LayoutAndPaintCallback(callback); | |
670 web_widget_->layoutAndPaintAsync(layout_and_paint_callback); | |
671 if (blink::WebPagePopup* popup = web_widget_->pagePopup()) { | |
672 layout_and_paint_callback->set_wait_for_popup(true); | |
673 popup->layoutAndPaintAsync(layout_and_paint_callback); | |
674 } | |
675 } | |
676 | |
677 void WebTestProxyBase::GetScreenOrientationForTesting( | |
678 blink::WebScreenInfo& screen_info) { | |
679 if (!screen_orientation_client_) | |
680 return; | |
681 // Override screen orientation information with mock data. | |
682 screen_info.orientationType = | |
683 screen_orientation_client_->CurrentOrientationType(); | |
684 screen_info.orientationAngle = | |
685 screen_orientation_client_->CurrentOrientationAngle(); | |
686 } | |
687 | |
688 MockScreenOrientationClient* | |
689 WebTestProxyBase::GetScreenOrientationClientMock() { | |
690 if (!screen_orientation_client_.get()) { | |
691 screen_orientation_client_.reset(new MockScreenOrientationClient); | |
692 } | |
693 return screen_orientation_client_.get(); | |
694 } | |
695 | |
696 MockWebSpeechRecognizer* WebTestProxyBase::GetSpeechRecognizerMock() { | |
697 if (!speech_recognizer_.get()) { | |
698 speech_recognizer_.reset(new MockWebSpeechRecognizer()); | |
699 speech_recognizer_->SetDelegate(delegate_); | |
700 } | |
701 return speech_recognizer_.get(); | |
702 } | |
703 | |
704 MockCredentialManagerClient* | |
705 WebTestProxyBase::GetCredentialManagerClientMock() { | |
706 if (!credential_manager_client_.get()) | |
707 credential_manager_client_.reset(new MockCredentialManagerClient()); | |
708 return credential_manager_client_.get(); | |
709 } | |
710 | |
711 void WebTestProxyBase::ScheduleAnimation() { | |
712 if (!test_interfaces_->GetTestRunner()->TestIsRunning()) | |
713 return; | |
714 | |
715 if (!animate_scheduled_) { | |
716 animate_scheduled_ = true; | |
717 delegate_->PostDelayedTask( | |
718 new HostMethodTask(this, &WebTestProxyBase::AnimateNow), 1); | |
719 } | |
720 } | |
721 | |
722 void WebTestProxyBase::AnimateNow() { | |
723 if (animate_scheduled_) { | |
724 animate_scheduled_ = false; | |
725 web_widget_->beginFrame(blink::WebBeginFrameArgs(0.0, 0.0, 0.0)); | |
726 web_widget_->layout(); | |
727 if (blink::WebPagePopup* popup = web_widget_->pagePopup()) { | |
728 popup->beginFrame(blink::WebBeginFrameArgs(0.0, 0.0, 0.0)); | |
729 popup->layout(); | |
730 } | |
731 } | |
732 } | |
733 | |
734 void WebTestProxyBase::PostAccessibilityEvent(const blink::WebAXObject& obj, | |
735 blink::WebAXEvent event) { | |
736 // Only hook the accessibility events occured during the test run. | |
737 // This check prevents false positives in WebLeakDetector. | |
738 // The pending tasks in browser/renderer message queue may trigger | |
739 // accessibility events, | |
740 // and AccessibilityController will hold on to their target nodes if we don't | |
741 // ignore them here. | |
742 if (!test_interfaces_->GetTestRunner()->TestIsRunning()) | |
743 return; | |
744 | |
745 if (event == blink::WebAXEventFocus) | |
746 test_interfaces_->GetAccessibilityController()->SetFocusedElement(obj); | |
747 | |
748 const char* event_name = NULL; | |
749 switch (event) { | |
750 case blink::WebAXEventActiveDescendantChanged: | |
751 event_name = "ActiveDescendantChanged"; | |
752 break; | |
753 case blink::WebAXEventAlert: | |
754 event_name = "Alert"; | |
755 break; | |
756 case blink::WebAXEventAriaAttributeChanged: | |
757 event_name = "AriaAttributeChanged"; | |
758 break; | |
759 case blink::WebAXEventAutocorrectionOccured: | |
760 event_name = "AutocorrectionOccured"; | |
761 break; | |
762 case blink::WebAXEventBlur: | |
763 event_name = "Blur"; | |
764 break; | |
765 case blink::WebAXEventCheckedStateChanged: | |
766 event_name = "CheckedStateChanged"; | |
767 break; | |
768 case blink::WebAXEventChildrenChanged: | |
769 event_name = "ChildrenChanged"; | |
770 break; | |
771 case blink::WebAXEventFocus: | |
772 event_name = "Focus"; | |
773 break; | |
774 case blink::WebAXEventHide: | |
775 event_name = "Hide"; | |
776 break; | |
777 case blink::WebAXEventInvalidStatusChanged: | |
778 event_name = "InvalidStatusChanged"; | |
779 break; | |
780 case blink::WebAXEventLayoutComplete: | |
781 event_name = "LayoutComplete"; | |
782 break; | |
783 case blink::WebAXEventLiveRegionChanged: | |
784 event_name = "LiveRegionChanged"; | |
785 break; | |
786 case blink::WebAXEventLoadComplete: | |
787 event_name = "LoadComplete"; | |
788 break; | |
789 case blink::WebAXEventLocationChanged: | |
790 event_name = "LocationChanged"; | |
791 break; | |
792 case blink::WebAXEventMenuListItemSelected: | |
793 event_name = "MenuListItemSelected"; | |
794 break; | |
795 case blink::WebAXEventMenuListItemUnselected: | |
796 event_name = "MenuListItemUnselected"; | |
797 break; | |
798 case blink::WebAXEventMenuListValueChanged: | |
799 event_name = "MenuListValueChanged"; | |
800 break; | |
801 case blink::WebAXEventRowCollapsed: | |
802 event_name = "RowCollapsed"; | |
803 break; | |
804 case blink::WebAXEventRowCountChanged: | |
805 event_name = "RowCountChanged"; | |
806 break; | |
807 case blink::WebAXEventRowExpanded: | |
808 event_name = "RowExpanded"; | |
809 break; | |
810 case blink::WebAXEventScrollPositionChanged: | |
811 event_name = "ScrollPositionChanged"; | |
812 break; | |
813 case blink::WebAXEventScrolledToAnchor: | |
814 event_name = "ScrolledToAnchor"; | |
815 break; | |
816 case blink::WebAXEventSelectedChildrenChanged: | |
817 event_name = "SelectedChildrenChanged"; | |
818 break; | |
819 case blink::WebAXEventSelectedTextChanged: | |
820 event_name = "SelectedTextChanged"; | |
821 break; | |
822 case blink::WebAXEventShow: | |
823 event_name = "Show"; | |
824 break; | |
825 case blink::WebAXEventTextChanged: | |
826 event_name = "TextChanged"; | |
827 break; | |
828 case blink::WebAXEventTextInserted: | |
829 event_name = "TextInserted"; | |
830 break; | |
831 case blink::WebAXEventTextRemoved: | |
832 event_name = "TextRemoved"; | |
833 break; | |
834 case blink::WebAXEventValueChanged: | |
835 event_name = "ValueChanged"; | |
836 break; | |
837 default: | |
838 event_name = "Unknown"; | |
839 break; | |
840 } | |
841 | |
842 test_interfaces_->GetAccessibilityController()->NotificationReceived( | |
843 obj, event_name); | |
844 | |
845 if (test_interfaces_->GetAccessibilityController() | |
846 ->ShouldLogAccessibilityEvents()) { | |
847 std::string message("AccessibilityNotification - "); | |
848 message += event_name; | |
849 | |
850 blink::WebNode node = obj.node(); | |
851 if (!node.isNull() && node.isElementNode()) { | |
852 blink::WebElement element = node.to<blink::WebElement>(); | |
853 if (element.hasAttribute("id")) { | |
854 message += " - id:"; | |
855 message += element.getAttribute("id").utf8().data(); | |
856 } | |
857 } | |
858 | |
859 delegate_->PrintMessage(message + "\n"); | |
860 } | |
861 } | |
862 | |
863 void WebTestProxyBase::StartDragging(blink::WebLocalFrame* frame, | |
864 const blink::WebDragData& data, | |
865 blink::WebDragOperationsMask mask, | |
866 const blink::WebImage& image, | |
867 const blink::WebPoint& point) { | |
868 if (test_interfaces_->GetTestRunner()->shouldDumpDragImage()) { | |
869 if (drag_image_.isNull()) | |
870 drag_image_ = image; | |
871 } | |
872 // When running a test, we need to fake a drag drop operation otherwise | |
873 // Windows waits for real mouse events to know when the drag is over. | |
874 test_interfaces_->GetEventSender()->DoDragDrop(data, mask); | |
875 } | |
876 | |
877 // The output from these methods in layout test mode should match that | |
878 // expected by the layout tests. See EditingDelegate.m in DumpRenderTree. | |
879 | |
880 void WebTestProxyBase::DidChangeSelection(bool is_empty_callback) { | |
881 if (test_interfaces_->GetTestRunner()->shouldDumpEditingCallbacks()) | |
882 delegate_->PrintMessage( | |
883 "EDITING DELEGATE: " | |
884 "webViewDidChangeSelection:WebViewDidChangeSelectionNotification\n"); | |
885 } | |
886 | |
887 void WebTestProxyBase::DidChangeContents() { | |
888 if (test_interfaces_->GetTestRunner()->shouldDumpEditingCallbacks()) | |
889 delegate_->PrintMessage( | |
890 "EDITING DELEGATE: webViewDidChange:WebViewDidChangeNotification\n"); | |
891 } | |
892 | |
893 bool WebTestProxyBase::CreateView(blink::WebLocalFrame* frame, | |
894 const blink::WebURLRequest& request, | |
895 const blink::WebWindowFeatures& features, | |
896 const blink::WebString& frame_name, | |
897 blink::WebNavigationPolicy policy, | |
898 bool suppress_opener) { | |
899 if (test_interfaces_->GetTestRunner()->shouldDumpNavigationPolicy()) { | |
900 delegate_->PrintMessage("Default policy for createView for '" + | |
901 URLDescription(request.url()) + "' is '" + | |
902 WebNavigationPolicyToString(policy) + "'\n"); | |
903 } | |
904 | |
905 if (!test_interfaces_->GetTestRunner()->canOpenWindows()) | |
906 return false; | |
907 if (test_interfaces_->GetTestRunner()->shouldDumpCreateView()) | |
908 delegate_->PrintMessage(std::string("createView(") + | |
909 URLDescription(request.url()) + ")\n"); | |
910 return true; | |
911 } | |
912 | |
913 blink::WebPlugin* WebTestProxyBase::CreatePlugin( | |
914 blink::WebLocalFrame* frame, | |
915 const blink::WebPluginParams& params) { | |
916 if (TestPlugin::IsSupportedMimeType(params.mimeType)) | |
917 return TestPlugin::create(frame, params, delegate_); | |
918 return delegate_->CreatePluginPlaceholder(frame, params); | |
919 } | |
920 | |
921 void WebTestProxyBase::SetStatusText(const blink::WebString& text) { | |
922 if (!test_interfaces_->GetTestRunner()->shouldDumpStatusCallbacks()) | |
923 return; | |
924 delegate_->PrintMessage( | |
925 std::string("UI DELEGATE STATUS CALLBACK: setStatusText:") + | |
926 text.utf8().data() + "\n"); | |
927 } | |
928 | |
929 void WebTestProxyBase::DidStopLoading() { | |
930 if (test_interfaces_->GetTestRunner()->shouldDumpProgressFinishedCallback()) | |
931 delegate_->PrintMessage("postProgressFinishedNotification\n"); | |
932 } | |
933 | |
934 void WebTestProxyBase::ShowContextMenu( | |
935 blink::WebLocalFrame* frame, | |
936 const blink::WebContextMenuData& context_menu_data) { | |
937 test_interfaces_->GetEventSender()->SetContextMenuData(context_menu_data); | |
938 } | |
939 | |
940 blink::WebUserMediaClient* WebTestProxyBase::GetUserMediaClient() { | |
941 if (!user_media_client_.get()) | |
942 user_media_client_.reset(new MockWebUserMediaClient(delegate_)); | |
943 return user_media_client_.get(); | |
944 } | |
945 | |
946 // Simulate a print by going into print mode and then exit straight away. | |
947 void WebTestProxyBase::PrintPage(blink::WebLocalFrame* frame) { | |
948 blink::WebSize page_size_in_pixels = web_widget_->size(); | |
949 if (page_size_in_pixels.isEmpty()) | |
950 return; | |
951 blink::WebPrintParams printParams(page_size_in_pixels); | |
952 frame->printBegin(printParams); | |
953 frame->printEnd(); | |
954 } | |
955 | |
956 blink::WebSpeechRecognizer* WebTestProxyBase::GetSpeechRecognizer() { | |
957 return GetSpeechRecognizerMock(); | |
958 } | |
959 | |
960 bool WebTestProxyBase::RequestPointerLock() { | |
961 return test_interfaces_->GetTestRunner()->RequestPointerLock(); | |
962 } | |
963 | |
964 void WebTestProxyBase::RequestPointerUnlock() { | |
965 test_interfaces_->GetTestRunner()->RequestPointerUnlock(); | |
966 } | |
967 | |
968 bool WebTestProxyBase::IsPointerLocked() { | |
969 return test_interfaces_->GetTestRunner()->isPointerLocked(); | |
970 } | |
971 | |
972 void WebTestProxyBase::DidFocus() { | |
973 delegate_->SetFocus(this, true); | |
974 } | |
975 | |
976 void WebTestProxyBase::DidBlur() { | |
977 delegate_->SetFocus(this, false); | |
978 } | |
979 | |
980 void WebTestProxyBase::SetToolTipText(const blink::WebString& text, | |
981 blink::WebTextDirection direction) { | |
982 test_interfaces_->GetTestRunner()->setToolTipText(text); | |
983 } | |
984 | |
985 void WebTestProxyBase::DidOpenChooser() { | |
986 chooser_count_++; | |
987 } | |
988 | |
989 void WebTestProxyBase::DidCloseChooser() { | |
990 chooser_count_--; | |
991 } | |
992 | |
993 bool WebTestProxyBase::IsChooserShown() { | |
994 return 0 < chooser_count_; | |
995 } | |
996 | |
997 void WebTestProxyBase::LoadURLExternally( | |
998 blink::WebLocalFrame* frame, | |
999 const blink::WebURLRequest& request, | |
1000 blink::WebNavigationPolicy policy, | |
1001 const blink::WebString& suggested_name) { | |
1002 if (test_interfaces_->GetTestRunner()->shouldWaitUntilExternalURLLoad()) { | |
1003 if (policy == blink::WebNavigationPolicyDownload) { | |
1004 delegate_->PrintMessage( | |
1005 std::string("Downloading URL with suggested filename \"") + | |
1006 suggested_name.utf8() + "\"\n"); | |
1007 } else { | |
1008 delegate_->PrintMessage(std::string("Loading URL externally - \"") + | |
1009 URLDescription(request.url()) + "\"\n"); | |
1010 } | |
1011 delegate_->TestFinished(); | |
1012 } | |
1013 } | |
1014 | |
1015 void WebTestProxyBase::DidStartProvisionalLoad(blink::WebLocalFrame* frame) { | |
1016 if (!test_interfaces_->GetTestRunner()->topLoadingFrame()) | |
1017 test_interfaces_->GetTestRunner()->setTopLoadingFrame(frame, false); | |
1018 | |
1019 if (test_interfaces_->GetTestRunner()->shouldDumpFrameLoadCallbacks()) { | |
1020 PrintFrameDescription(delegate_, frame); | |
1021 delegate_->PrintMessage(" - didStartProvisionalLoadForFrame\n"); | |
1022 } | |
1023 | |
1024 if (test_interfaces_->GetTestRunner() | |
1025 ->shouldDumpUserGestureInFrameLoadCallbacks()) { | |
1026 PrintFrameuserGestureStatus( | |
1027 delegate_, frame, " - in didStartProvisionalLoadForFrame\n"); | |
1028 } | |
1029 } | |
1030 | |
1031 void WebTestProxyBase::DidReceiveServerRedirectForProvisionalLoad( | |
1032 blink::WebLocalFrame* frame) { | |
1033 if (test_interfaces_->GetTestRunner()->shouldDumpFrameLoadCallbacks()) { | |
1034 PrintFrameDescription(delegate_, frame); | |
1035 delegate_->PrintMessage( | |
1036 " - didReceiveServerRedirectForProvisionalLoadForFrame\n"); | |
1037 } | |
1038 } | |
1039 | |
1040 bool WebTestProxyBase::DidFailProvisionalLoad( | |
1041 blink::WebLocalFrame* frame, | |
1042 const blink::WebURLError& error, | |
1043 blink::WebHistoryCommitType commit_type) { | |
1044 if (test_interfaces_->GetTestRunner()->shouldDumpFrameLoadCallbacks()) { | |
1045 PrintFrameDescription(delegate_, frame); | |
1046 delegate_->PrintMessage(" - didFailProvisionalLoadWithError\n"); | |
1047 } | |
1048 CheckDone(frame, MainResourceLoadFailed); | |
1049 return !frame->provisionalDataSource(); | |
1050 } | |
1051 | |
1052 void WebTestProxyBase::DidCommitProvisionalLoad( | |
1053 blink::WebLocalFrame* frame, | |
1054 const blink::WebHistoryItem& history_item, | |
1055 blink::WebHistoryCommitType history_type) { | |
1056 if (test_interfaces_->GetTestRunner()->shouldDumpFrameLoadCallbacks()) { | |
1057 PrintFrameDescription(delegate_, frame); | |
1058 delegate_->PrintMessage(" - didCommitLoadForFrame\n"); | |
1059 } | |
1060 } | |
1061 | |
1062 void WebTestProxyBase::DidReceiveTitle(blink::WebLocalFrame* frame, | |
1063 const blink::WebString& title, | |
1064 blink::WebTextDirection direction) { | |
1065 blink::WebCString title8 = title.utf8(); | |
1066 | |
1067 if (test_interfaces_->GetTestRunner()->shouldDumpFrameLoadCallbacks()) { | |
1068 PrintFrameDescription(delegate_, frame); | |
1069 delegate_->PrintMessage(std::string(" - didReceiveTitle: ") + | |
1070 title8.data() + "\n"); | |
1071 } | |
1072 | |
1073 if (test_interfaces_->GetTestRunner()->shouldDumpTitleChanges()) | |
1074 delegate_->PrintMessage(std::string("TITLE CHANGED: '") + title8.data() + | |
1075 "'\n"); | |
1076 } | |
1077 | |
1078 void WebTestProxyBase::DidChangeIcon(blink::WebLocalFrame* frame, | |
1079 blink::WebIconURL::Type icon_type) { | |
1080 if (test_interfaces_->GetTestRunner()->shouldDumpIconChanges()) { | |
1081 PrintFrameDescription(delegate_, frame); | |
1082 delegate_->PrintMessage(std::string(" - didChangeIcons\n")); | |
1083 } | |
1084 } | |
1085 | |
1086 void WebTestProxyBase::DidFinishDocumentLoad(blink::WebLocalFrame* frame) { | |
1087 if (test_interfaces_->GetTestRunner()->shouldDumpFrameLoadCallbacks()) { | |
1088 PrintFrameDescription(delegate_, frame); | |
1089 delegate_->PrintMessage(" - didFinishDocumentLoadForFrame\n"); | |
1090 } else { | |
1091 unsigned pendingUnloadEvents = frame->unloadListenerCount(); | |
1092 if (pendingUnloadEvents) { | |
1093 PrintFrameDescription(delegate_, frame); | |
1094 delegate_->PrintMessage(base::StringPrintf( | |
1095 " - has %u onunload handler(s)\n", pendingUnloadEvents)); | |
1096 } | |
1097 } | |
1098 } | |
1099 | |
1100 void WebTestProxyBase::DidHandleOnloadEvents(blink::WebLocalFrame* frame) { | |
1101 if (test_interfaces_->GetTestRunner()->shouldDumpFrameLoadCallbacks()) { | |
1102 PrintFrameDescription(delegate_, frame); | |
1103 delegate_->PrintMessage(" - didHandleOnloadEventsForFrame\n"); | |
1104 } | |
1105 } | |
1106 | |
1107 void WebTestProxyBase::DidFailLoad(blink::WebLocalFrame* frame, | |
1108 const blink::WebURLError& error, | |
1109 blink::WebHistoryCommitType commit_type) { | |
1110 if (test_interfaces_->GetTestRunner()->shouldDumpFrameLoadCallbacks()) { | |
1111 PrintFrameDescription(delegate_, frame); | |
1112 delegate_->PrintMessage(" - didFailLoadWithError\n"); | |
1113 } | |
1114 CheckDone(frame, MainResourceLoadFailed); | |
1115 } | |
1116 | |
1117 void WebTestProxyBase::DidFinishLoad(blink::WebLocalFrame* frame) { | |
1118 if (test_interfaces_->GetTestRunner()->shouldDumpFrameLoadCallbacks()) { | |
1119 PrintFrameDescription(delegate_, frame); | |
1120 delegate_->PrintMessage(" - didFinishLoadForFrame\n"); | |
1121 } | |
1122 CheckDone(frame, LoadFinished); | |
1123 } | |
1124 | |
1125 void WebTestProxyBase::DidDetectXSS(blink::WebLocalFrame* frame, | |
1126 const blink::WebURL& insecure_url, | |
1127 bool did_block_entire_page) { | |
1128 if (test_interfaces_->GetTestRunner()->shouldDumpFrameLoadCallbacks()) | |
1129 delegate_->PrintMessage("didDetectXSS\n"); | |
1130 } | |
1131 | |
1132 void WebTestProxyBase::DidDispatchPingLoader(blink::WebLocalFrame* frame, | |
1133 const blink::WebURL& url) { | |
1134 if (test_interfaces_->GetTestRunner()->shouldDumpPingLoaderCallbacks()) | |
1135 delegate_->PrintMessage(std::string("PingLoader dispatched to '") + | |
1136 URLDescription(url).c_str() + "'.\n"); | |
1137 } | |
1138 | |
1139 void WebTestProxyBase::WillRequestResource( | |
1140 blink::WebLocalFrame* frame, | |
1141 const blink::WebCachedURLRequest& request) { | |
1142 if (test_interfaces_->GetTestRunner()->shouldDumpResourceRequestCallbacks()) { | |
1143 PrintFrameDescription(delegate_, frame); | |
1144 delegate_->PrintMessage(std::string(" - ") + | |
1145 request.initiatorName().utf8().data()); | |
1146 delegate_->PrintMessage(std::string(" requested '") + | |
1147 URLDescription(request.urlRequest().url()).c_str() + | |
1148 "'\n"); | |
1149 } | |
1150 } | |
1151 | |
1152 void WebTestProxyBase::WillSendRequest( | |
1153 blink::WebLocalFrame* frame, | |
1154 unsigned identifier, | |
1155 blink::WebURLRequest& request, | |
1156 const blink::WebURLResponse& redirect_response) { | |
1157 // Need to use GURL for host() and SchemeIs() | |
1158 GURL url = request.url(); | |
1159 std::string request_url = url.possibly_invalid_spec(); | |
1160 | |
1161 GURL main_document_url = request.firstPartyForCookies(); | |
1162 | |
1163 if (redirect_response.isNull() && | |
1164 (test_interfaces_->GetTestRunner()->shouldDumpResourceLoadCallbacks() || | |
1165 test_interfaces_->GetTestRunner()->shouldDumpResourcePriorities())) { | |
1166 DCHECK(resource_identifier_map_.find(identifier) == | |
1167 resource_identifier_map_.end()); | |
1168 resource_identifier_map_[identifier] = | |
1169 DescriptionSuitableForTestResult(request_url); | |
1170 } | |
1171 | |
1172 if (test_interfaces_->GetTestRunner()->shouldDumpResourceLoadCallbacks()) { | |
1173 if (resource_identifier_map_.find(identifier) == | |
1174 resource_identifier_map_.end()) | |
1175 delegate_->PrintMessage("<unknown>"); | |
1176 else | |
1177 delegate_->PrintMessage(resource_identifier_map_[identifier]); | |
1178 delegate_->PrintMessage(" - willSendRequest <NSURLRequest URL "); | |
1179 delegate_->PrintMessage( | |
1180 DescriptionSuitableForTestResult(request_url).c_str()); | |
1181 delegate_->PrintMessage(", main document URL "); | |
1182 delegate_->PrintMessage(URLDescription(main_document_url).c_str()); | |
1183 delegate_->PrintMessage(", http method "); | |
1184 delegate_->PrintMessage(request.httpMethod().utf8().data()); | |
1185 delegate_->PrintMessage("> redirectResponse "); | |
1186 PrintResponseDescription(delegate_, redirect_response); | |
1187 delegate_->PrintMessage("\n"); | |
1188 } | |
1189 | |
1190 if (test_interfaces_->GetTestRunner()->shouldDumpResourcePriorities()) { | |
1191 delegate_->PrintMessage( | |
1192 DescriptionSuitableForTestResult(request_url).c_str()); | |
1193 delegate_->PrintMessage(" has priority "); | |
1194 delegate_->PrintMessage(PriorityDescription(request.priority())); | |
1195 delegate_->PrintMessage("\n"); | |
1196 } | |
1197 | |
1198 if (test_interfaces_->GetTestRunner()->httpHeadersToClear()) { | |
1199 const std::set<std::string>* clearHeaders = | |
1200 test_interfaces_->GetTestRunner()->httpHeadersToClear(); | |
1201 for (std::set<std::string>::const_iterator header = clearHeaders->begin(); | |
1202 header != clearHeaders->end(); | |
1203 ++header) | |
1204 request.clearHTTPHeaderField(blink::WebString::fromUTF8(*header)); | |
1205 } | |
1206 | |
1207 std::string host = url.host(); | |
1208 if (!host.empty() && | |
1209 (url.SchemeIs(url::kHttpScheme) || url.SchemeIs(url::kHttpsScheme))) { | |
1210 if (!IsLocalHost(host) && !IsTestHost(host) && | |
1211 !HostIsUsedBySomeTestsToGenerateError(host) && | |
1212 ((!main_document_url.SchemeIs(url::kHttpScheme) && | |
1213 !main_document_url.SchemeIs(url::kHttpsScheme)) || | |
1214 IsLocalHost(main_document_url.host())) && | |
1215 !delegate_->AllowExternalPages()) { | |
1216 delegate_->PrintMessage(std::string("Blocked access to external URL ") + | |
1217 request_url + "\n"); | |
1218 BlockRequest(request); | |
1219 return; | |
1220 } | |
1221 } | |
1222 | |
1223 // Set the new substituted URL. | |
1224 request.setURL(delegate_->RewriteLayoutTestsURL(request.url().spec())); | |
1225 } | |
1226 | |
1227 void WebTestProxyBase::DidReceiveResponse( | |
1228 blink::WebLocalFrame* frame, | |
1229 unsigned identifier, | |
1230 const blink::WebURLResponse& response) { | |
1231 if (test_interfaces_->GetTestRunner()->shouldDumpResourceLoadCallbacks()) { | |
1232 if (resource_identifier_map_.find(identifier) == | |
1233 resource_identifier_map_.end()) | |
1234 delegate_->PrintMessage("<unknown>"); | |
1235 else | |
1236 delegate_->PrintMessage(resource_identifier_map_[identifier]); | |
1237 delegate_->PrintMessage(" - didReceiveResponse "); | |
1238 PrintResponseDescription(delegate_, response); | |
1239 delegate_->PrintMessage("\n"); | |
1240 } | |
1241 if (test_interfaces_->GetTestRunner() | |
1242 ->shouldDumpResourceResponseMIMETypes()) { | |
1243 GURL url = response.url(); | |
1244 blink::WebString mime_type = response.mimeType(); | |
1245 delegate_->PrintMessage(url.ExtractFileName()); | |
1246 delegate_->PrintMessage(" has MIME type "); | |
1247 // Simulate NSURLResponse's mapping of empty/unknown MIME types to | |
1248 // application/octet-stream | |
1249 delegate_->PrintMessage(mime_type.isEmpty() ? "application/octet-stream" | |
1250 : mime_type.utf8().data()); | |
1251 delegate_->PrintMessage("\n"); | |
1252 } | |
1253 } | |
1254 | |
1255 void WebTestProxyBase::DidChangeResourcePriority( | |
1256 blink::WebLocalFrame* frame, | |
1257 unsigned identifier, | |
1258 const blink::WebURLRequest::Priority& priority, | |
1259 int intra_priority_value) { | |
1260 if (test_interfaces_->GetTestRunner()->shouldDumpResourcePriorities()) { | |
1261 if (resource_identifier_map_.find(identifier) == | |
1262 resource_identifier_map_.end()) | |
1263 delegate_->PrintMessage("<unknown>"); | |
1264 else | |
1265 delegate_->PrintMessage(resource_identifier_map_[identifier]); | |
1266 delegate_->PrintMessage( | |
1267 base::StringPrintf(" changed priority to %s, intra_priority %d\n", | |
1268 PriorityDescription(priority).c_str(), | |
1269 intra_priority_value)); | |
1270 } | |
1271 } | |
1272 | |
1273 void WebTestProxyBase::DidFinishResourceLoad(blink::WebLocalFrame* frame, | |
1274 unsigned identifier) { | |
1275 if (test_interfaces_->GetTestRunner()->shouldDumpResourceLoadCallbacks()) { | |
1276 if (resource_identifier_map_.find(identifier) == | |
1277 resource_identifier_map_.end()) | |
1278 delegate_->PrintMessage("<unknown>"); | |
1279 else | |
1280 delegate_->PrintMessage(resource_identifier_map_[identifier]); | |
1281 delegate_->PrintMessage(" - didFinishLoading\n"); | |
1282 } | |
1283 resource_identifier_map_.erase(identifier); | |
1284 CheckDone(frame, ResourceLoadCompleted); | |
1285 } | |
1286 | |
1287 void WebTestProxyBase::DidAddMessageToConsole( | |
1288 const blink::WebConsoleMessage& message, | |
1289 const blink::WebString& source_name, | |
1290 unsigned source_line) { | |
1291 // This matches win DumpRenderTree's UIDelegate.cpp. | |
1292 if (!log_console_output_) | |
1293 return; | |
1294 std::string level; | |
1295 switch (message.level) { | |
1296 case blink::WebConsoleMessage::LevelDebug: | |
1297 level = "DEBUG"; | |
1298 break; | |
1299 case blink::WebConsoleMessage::LevelLog: | |
1300 level = "MESSAGE"; | |
1301 break; | |
1302 case blink::WebConsoleMessage::LevelInfo: | |
1303 level = "INFO"; | |
1304 break; | |
1305 case blink::WebConsoleMessage::LevelWarning: | |
1306 level = "WARNING"; | |
1307 break; | |
1308 case blink::WebConsoleMessage::LevelError: | |
1309 level = "ERROR"; | |
1310 break; | |
1311 default: | |
1312 level = "MESSAGE"; | |
1313 } | |
1314 delegate_->PrintMessage(std::string("CONSOLE ") + level + ": "); | |
1315 if (source_line) { | |
1316 delegate_->PrintMessage(base::StringPrintf("line %d: ", source_line)); | |
1317 } | |
1318 if (!message.text.isEmpty()) { | |
1319 std::string new_message; | |
1320 new_message = message.text.utf8(); | |
1321 size_t file_protocol = new_message.find("file://"); | |
1322 if (file_protocol != std::string::npos) { | |
1323 new_message = new_message.substr(0, file_protocol) + | |
1324 URLSuitableForTestResult(new_message.substr(file_protocol)); | |
1325 } | |
1326 delegate_->PrintMessage(new_message); | |
1327 } | |
1328 delegate_->PrintMessage(std::string("\n")); | |
1329 } | |
1330 | |
1331 void WebTestProxyBase::CheckDone(blink::WebLocalFrame* frame, | |
1332 CheckDoneReason reason) { | |
1333 if (frame != test_interfaces_->GetTestRunner()->topLoadingFrame()) | |
1334 return; | |
1335 if (reason != MainResourceLoadFailed && | |
1336 (frame->isResourceLoadInProgress() || frame->isLoading())) | |
1337 return; | |
1338 test_interfaces_->GetTestRunner()->setTopLoadingFrame(frame, true); | |
1339 } | |
1340 | |
1341 blink::WebNavigationPolicy WebTestProxyBase::DecidePolicyForNavigation( | |
1342 const blink::WebFrameClient::NavigationPolicyInfo& info) { | |
1343 if (test_interfaces_->GetTestRunner()->shouldDumpNavigationPolicy()) { | |
1344 delegate_->PrintMessage("Default policy for navigation to '" + | |
1345 URLDescription(info.urlRequest.url()) + "' is '" + | |
1346 WebNavigationPolicyToString(info.defaultPolicy) + | |
1347 "'\n"); | |
1348 } | |
1349 | |
1350 blink::WebNavigationPolicy result; | |
1351 if (!test_interfaces_->GetTestRunner()->policyDelegateEnabled()) | |
1352 return info.defaultPolicy; | |
1353 | |
1354 delegate_->PrintMessage( | |
1355 std::string("Policy delegate: attempt to load ") + | |
1356 URLDescription(info.urlRequest.url()) + " with navigation type '" + | |
1357 WebNavigationTypeToString(info.navigationType) + "'\n"); | |
1358 if (test_interfaces_->GetTestRunner()->policyDelegateIsPermissive()) | |
1359 result = blink::WebNavigationPolicyCurrentTab; | |
1360 else | |
1361 result = blink::WebNavigationPolicyIgnore; | |
1362 | |
1363 if (test_interfaces_->GetTestRunner()->policyDelegateShouldNotifyDone()) { | |
1364 test_interfaces_->GetTestRunner()->policyDelegateDone(); | |
1365 result = blink::WebNavigationPolicyIgnore; | |
1366 } | |
1367 | |
1368 return result; | |
1369 } | |
1370 | |
1371 bool WebTestProxyBase::WillCheckAndDispatchMessageEvent( | |
1372 blink::WebLocalFrame* source_frame, | |
1373 blink::WebFrame* target_frame, | |
1374 blink::WebSecurityOrigin target, | |
1375 blink::WebDOMMessageEvent event) { | |
1376 if (test_interfaces_->GetTestRunner()->shouldInterceptPostMessage()) { | |
1377 delegate_->PrintMessage("intercepted postMessage\n"); | |
1378 return true; | |
1379 } | |
1380 | |
1381 return false; | |
1382 } | |
1383 | |
1384 void WebTestProxyBase::PostSpellCheckEvent(const blink::WebString& event_name) { | |
1385 if (test_interfaces_->GetTestRunner()->shouldDumpSpellCheckCallbacks()) { | |
1386 delegate_->PrintMessage(std::string("SpellCheckEvent: ") + | |
1387 event_name.utf8().data() + "\n"); | |
1388 } | |
1389 } | |
1390 | |
1391 void WebTestProxyBase::ResetInputMethod() { | |
1392 // If a composition text exists, then we need to let the browser process | |
1393 // to cancel the input method's ongoing composition session. | |
1394 if (web_widget_) | |
1395 web_widget_->confirmComposition(); | |
1396 } | |
1397 | |
1398 blink::WebString WebTestProxyBase::acceptLanguages() { | |
1399 return blink::WebString::fromUTF8(accept_languages_); | |
1400 } | |
1401 | |
1402 } // namespace content | |
OLD | NEW |