blob: faef0cdc7bcb8d6b940d395e1cc024baaea5ed2d [file] [log] [blame]
[email protected]31d71b02012-01-26 03:42:311// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]cccb5cf2012-06-06 22:20:045#include "content/shell/layout_test_controller.h"
[email protected]31d71b02012-01-26 03:42:316
[email protected]c272c5b2012-06-06 09:01:067#include "base/stringprintf.h"
[email protected]efb5f572012-01-29 10:57:338#include "content/public/renderer/render_view.h"
9#include "content/shell/shell_messages.h"
10#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h"
11#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
12#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
13#include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h"
14#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
15#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
16
17using WebKit::WebFrame;
18using WebKit::WebElement;
[email protected]c272c5b2012-06-06 09:01:0619using WebKit::WebSize;
[email protected]efb5f572012-01-29 10:57:3320
[email protected]31d71b02012-01-26 03:42:3121namespace content {
22
[email protected]efb5f572012-01-29 10:57:3323namespace {
24
25std::string DumpDocumentText(WebFrame* frame) {
26 // We use the document element's text instead of the body text here because
27 // not all documents have a body, such as XML documents.
28 WebElement documentElement = frame->document().documentElement();
29 if (documentElement.isNull())
30 return std::string();
31 return documentElement.innerText().utf8();
32}
33
[email protected]c272c5b2012-06-06 09:01:0634std::string DumpDocumentPrintedText(WebFrame* frame) {
35 return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8();
36}
37
38std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) {
[email protected]efb5f572012-01-29 10:57:3339 std::string result;
40
[email protected]c272c5b2012-06-06 09:01:0641 // Cannot do printed format for anything other than HTML.
42 if (printing && !frame->document().isHTMLDocument())
43 return std::string();
44
[email protected]efb5f572012-01-29 10:57:3345 // Add header for all but the main frame. Skip emtpy frames.
46 if (frame->parent() && !frame->document().documentElement().isNull()) {
47 result.append("\n--------\nFrame: '");
48 result.append(frame->name().utf8().data());
49 result.append("'\n--------\n");
50 }
51
[email protected]c272c5b2012-06-06 09:01:0652 result.append(
53 printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame));
[email protected]efb5f572012-01-29 10:57:3354 result.append("\n");
55
56 if (recursive) {
57 for (WebFrame* child = frame->firstChild(); child;
58 child = child->nextSibling()) {
[email protected]c272c5b2012-06-06 09:01:0659 result.append(DumpFramesAsText(child, printing, recursive));
60 }
61 }
62 return result;
63}
64
65std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) {
66 std::string result;
67
68 WebSize offset = frame->scrollOffset();
69 if (offset.width > 0 || offset.height > 0) {
70 if (frame->parent()) {
71 result.append(
72 base::StringPrintf("frame '%s' ", frame->name().utf8().data()));
73 }
74 result.append(
75 base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height));
76 }
77
78 if (recursive) {
79 for (WebFrame* child = frame->firstChild(); child;
80 child = child->nextSibling()) {
81 result.append(DumpFrameScrollPosition(child, recursive));
[email protected]efb5f572012-01-29 10:57:3382 }
83 }
84 return result;
85}
86
87} // namespace
[email protected]cccb5cf2012-06-06 22:20:0488
89LayoutTestController::LayoutTestController(RenderView* render_view)
[email protected]31d71b02012-01-26 03:42:3190 : RenderViewObserver(render_view) {
91}
92
[email protected]cccb5cf2012-06-06 22:20:0493LayoutTestController::~LayoutTestController() {
[email protected]31d71b02012-01-26 03:42:3194}
95
[email protected]cccb5cf2012-06-06 22:20:0496void LayoutTestController::DidFinishLoad(WebFrame* frame) {
[email protected]c272c5b2012-06-06 09:01:0697 if (!frame->parent())
98 Send(new ShellViewHostMsg_DidFinishLoad(routing_id()));
99}
100
[email protected]cccb5cf2012-06-06 22:20:04101bool LayoutTestController::OnMessageReceived(const IPC::Message& message) {
[email protected]efb5f572012-01-29 10:57:33102 bool handled = true;
[email protected]cccb5cf2012-06-06 22:20:04103 IPC_BEGIN_MESSAGE_MAP(LayoutTestController, message)
[email protected]efb5f572012-01-29 10:57:33104 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump)
105 IPC_MESSAGE_UNHANDLED(handled = false)
106 IPC_END_MESSAGE_MAP()
107
108 return handled;
109}
110
[email protected]cccb5cf2012-06-06 22:20:04111void LayoutTestController::OnCaptureTextDump(bool as_text,
112 bool printing,
113 bool recursive) {
[email protected]c272c5b2012-06-06 09:01:06114 WebFrame* frame = render_view()->GetWebView()->mainFrame();
115 std::string dump;
116 if (as_text) {
117 dump = DumpFramesAsText(frame, printing, recursive);
118 } else {
119 WebFrame::RenderAsTextControls render_text_behavior =
120 WebFrame::RenderAsTextNormal;
121 if (printing)
122 render_text_behavior |= WebFrame::RenderAsTextPrinting;
123 dump = frame->renderTreeAsText(render_text_behavior).utf8();
124 dump.append(DumpFrameScrollPosition(frame, recursive));
125 }
[email protected]efb5f572012-01-29 10:57:33126 Send(new ShellViewHostMsg_TextDump(routing_id(), dump));
[email protected]31d71b02012-01-26 03:42:31127}
128
129} // namespace content