OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/shell/layout_test_controller.h" | |
6 | |
7 #include "base/md5.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/stringprintf.h" | |
10 #include "content/public/renderer/render_view.h" | |
11 #include "content/shell/shell_messages.h" | |
12 #include "skia/ext/platform_canvas.h" | |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebCString.h
" | |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebRect.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebSize.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" | |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebTestingSupport.h" | |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | |
22 #include "webkit/glue/webkit_glue.h" | |
23 | |
24 using WebKit::WebFrame; | |
25 using WebKit::WebElement; | |
26 using WebKit::WebRect; | |
27 using WebKit::WebSize; | |
28 using WebKit::WebTestingSupport; | |
29 using WebKit::WebView; | |
30 | |
31 namespace content { | |
32 | |
33 namespace { | |
34 | |
35 std::string DumpDocumentText(WebFrame* frame) { | |
36 // We use the document element's text instead of the body text here because | |
37 // not all documents have a body, such as XML documents. | |
38 WebElement documentElement = frame->document().documentElement(); | |
39 if (documentElement.isNull()) | |
40 return std::string(); | |
41 return documentElement.innerText().utf8(); | |
42 } | |
43 | |
44 std::string DumpDocumentPrintedText(WebFrame* frame) { | |
45 return frame->renderTreeAsText(WebFrame::RenderAsTextPrinting).utf8(); | |
46 } | |
47 | |
48 std::string DumpFramesAsText(WebFrame* frame, bool printing, bool recursive) { | |
49 std::string result; | |
50 | |
51 // Cannot do printed format for anything other than HTML. | |
52 if (printing && !frame->document().isHTMLDocument()) | |
53 return std::string(); | |
54 | |
55 // Add header for all but the main frame. Skip emtpy frames. | |
56 if (frame->parent() && !frame->document().documentElement().isNull()) { | |
57 result.append("\n--------\nFrame: '"); | |
58 result.append(frame->name().utf8().data()); | |
59 result.append("'\n--------\n"); | |
60 } | |
61 | |
62 result.append( | |
63 printing ? DumpDocumentPrintedText(frame) : DumpDocumentText(frame)); | |
64 result.append("\n"); | |
65 | |
66 if (recursive) { | |
67 for (WebFrame* child = frame->firstChild(); child; | |
68 child = child->nextSibling()) { | |
69 result.append(DumpFramesAsText(child, printing, recursive)); | |
70 } | |
71 } | |
72 return result; | |
73 } | |
74 | |
75 std::string DumpFrameScrollPosition(WebFrame* frame, bool recursive) { | |
76 std::string result; | |
77 | |
78 WebSize offset = frame->scrollOffset(); | |
79 if (offset.width > 0 || offset.height > 0) { | |
80 if (frame->parent()) { | |
81 result.append( | |
82 base::StringPrintf("frame '%s' ", frame->name().utf8().data())); | |
83 } | |
84 result.append( | |
85 base::StringPrintf("scrolled to %d,%d\n", offset.width, offset.height)); | |
86 } | |
87 | |
88 if (recursive) { | |
89 for (WebFrame* child = frame->firstChild(); child; | |
90 child = child->nextSibling()) { | |
91 result.append(DumpFrameScrollPosition(child, recursive)); | |
92 } | |
93 } | |
94 return result; | |
95 } | |
96 | |
97 bool PaintViewIntoCanvas(WebView* view, skia::PlatformCanvas& canvas) { | |
98 view->layout(); | |
99 const WebSize& size = view->size(); | |
100 | |
101 if (!canvas.initialize(size.width, size.height, true)) | |
102 return false; | |
103 | |
104 view->paint(webkit_glue::ToWebCanvas(&canvas), | |
105 WebRect(0, 0, size.width, size.height)); | |
106 return true; | |
107 } | |
108 | |
109 #if !defined(OS_MACOSX) | |
110 void MakeBitmapOpaque(SkBitmap* bitmap) { | |
111 SkAutoLockPixels lock(*bitmap); | |
112 DCHECK(bitmap->config() == SkBitmap::kARGB_8888_Config); | |
113 for (int y = 0; y < bitmap->height(); ++y) { | |
114 uint32_t* row = bitmap->getAddr32(0, y); | |
115 for (int x = 0; x < bitmap->width(); ++x) | |
116 row[x] |= 0xFF000000; // Set alpha bits to 1. | |
117 } | |
118 } | |
119 #endif | |
120 | |
121 void CaptureSnapshot(WebView* view, SkBitmap* snapshot) { | |
122 skia::PlatformCanvas canvas; | |
123 if (!PaintViewIntoCanvas(view, canvas)) | |
124 return; | |
125 | |
126 SkDevice* device = skia::GetTopDevice(canvas); | |
127 | |
128 const SkBitmap& bitmap = device->accessBitmap(false); | |
129 bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config); | |
130 | |
131 #if !defined(OS_MACOSX) | |
132 // Only the expected PNGs for Mac have a valid alpha channel. | |
133 MakeBitmapOpaque(snapshot); | |
134 #endif | |
135 | |
136 } | |
137 | |
138 } // namespace | |
139 | |
140 LayoutTestController::LayoutTestController(RenderView* render_view) | |
141 : RenderViewObserver(render_view) { | |
142 } | |
143 | |
144 LayoutTestController::~LayoutTestController() { | |
145 } | |
146 | |
147 void LayoutTestController::DidClearWindowObject(WebFrame* frame) { | |
148 WebTestingSupport::injectInternalsObject(frame); | |
149 } | |
150 | |
151 void LayoutTestController::DidFinishLoad(WebFrame* frame) { | |
152 if (!frame->parent()) | |
153 Send(new ShellViewHostMsg_DidFinishLoad(routing_id())); | |
154 } | |
155 | |
156 bool LayoutTestController::OnMessageReceived(const IPC::Message& message) { | |
157 bool handled = true; | |
158 IPC_BEGIN_MESSAGE_MAP(LayoutTestController, message) | |
159 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureTextDump, OnCaptureTextDump) | |
160 IPC_MESSAGE_HANDLER(ShellViewMsg_CaptureImageDump, OnCaptureImageDump) | |
161 IPC_MESSAGE_UNHANDLED(handled = false) | |
162 IPC_END_MESSAGE_MAP() | |
163 | |
164 return handled; | |
165 } | |
166 | |
167 void LayoutTestController::OnCaptureTextDump(bool as_text, | |
168 bool printing, | |
169 bool recursive) { | |
170 WebFrame* frame = render_view()->GetWebView()->mainFrame(); | |
171 std::string dump; | |
172 if (as_text) { | |
173 dump = DumpFramesAsText(frame, printing, recursive); | |
174 } else { | |
175 WebFrame::RenderAsTextControls render_text_behavior = | |
176 WebFrame::RenderAsTextNormal; | |
177 if (printing) | |
178 render_text_behavior |= WebFrame::RenderAsTextPrinting; | |
179 dump = frame->renderTreeAsText(render_text_behavior).utf8(); | |
180 dump.append(DumpFrameScrollPosition(frame, recursive)); | |
181 } | |
182 Send(new ShellViewHostMsg_TextDump(routing_id(), dump)); | |
183 } | |
184 | |
185 void LayoutTestController::OnCaptureImageDump( | |
186 const std::string& expected_pixel_hash) { | |
187 SkBitmap snapshot; | |
188 CaptureSnapshot(render_view()->GetWebView(), &snapshot); | |
189 | |
190 SkAutoLockPixels snapshot_lock(snapshot); | |
191 base::MD5Digest digest; | |
192 #if defined(OS_ANDROID) | |
193 // On Android, pixel layout is RGBA, however, other Chrome platforms use BGRA. | |
194 const uint8_t* raw_pixels = | |
195 reinterpret_cast<const uint8_t*>(snapshot.getPixels()); | |
196 size_t snapshot_size = snapshot.getSize(); | |
197 scoped_array<uint8_t> reordered_pixels(new uint8_t[snapshot_size]); | |
198 for (size_t i = 0; i < snapshot_size; i += 4) { | |
199 reordered_pixels[i] = raw_pixels[i + 2]; | |
200 reordered_pixels[i + 1] = raw_pixels[i + 1]; | |
201 reordered_pixels[i + 2] = raw_pixels[i]; | |
202 reordered_pixels[i + 3] = raw_pixels[i + 3]; | |
203 } | |
204 base::MD5Sum(reordered_pixels.get(), snapshot_size, &digest); | |
205 #else | |
206 base::MD5Sum(snapshot.getPixels(), snapshot.getSize(), &digest); | |
207 #endif | |
208 std::string actual_pixel_hash = base::MD5DigestToBase16(digest); | |
209 | |
210 if (actual_pixel_hash == expected_pixel_hash) { | |
211 SkBitmap empty_image; | |
212 Send(new ShellViewHostMsg_ImageDump( | |
213 routing_id(), actual_pixel_hash, empty_image)); | |
214 return; | |
215 } | |
216 Send(new ShellViewHostMsg_ImageDump( | |
217 routing_id(), actual_pixel_hash, snapshot)); | |
218 } | |
219 | |
220 } // namespace content | |
OLD | NEW |