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_host.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/message_loop.h" | |
9 #include "content/public/browser/render_view_host.h" | |
10 #include "content/shell/shell_messages.h" | |
11 #include "content/shell/shell_switches.h" | |
12 #include "webkit/support/webkit_support_gfx.h" | |
13 | |
14 namespace content { | |
15 | |
16 namespace { | |
17 const int kTestTimeoutMilliseconds = 30 * 1000; | |
18 } // namespace | |
19 | |
20 std::map<RenderViewHost*, LayoutTestControllerHost*> | |
21 LayoutTestControllerHost::controllers_; | |
22 std::string LayoutTestControllerHost::expected_pixel_hash_; | |
23 | |
24 // static | |
25 LayoutTestControllerHost* LayoutTestControllerHost::FromRenderViewHost( | |
26 RenderViewHost* render_view_host) { | |
27 const std::map<RenderViewHost*, LayoutTestControllerHost*>::iterator it = | |
28 controllers_.find(render_view_host); | |
29 if (it == controllers_.end()) | |
30 return NULL; | |
31 return it->second; | |
32 } | |
33 | |
34 // static | |
35 void LayoutTestControllerHost::Init(const std::string& expected_pixel_hash) { | |
36 // TODO(jochen): We should only dump the results for the "main window". | |
37 expected_pixel_hash_ = expected_pixel_hash; | |
38 } | |
39 | |
40 LayoutTestControllerHost::LayoutTestControllerHost( | |
41 RenderViewHost* render_view_host) | |
42 : RenderViewHostObserver(render_view_host), | |
43 captured_dump_(false), | |
44 dump_as_text_(false), | |
45 dump_child_frames_(false), | |
46 is_printing_(false), | |
47 should_stay_on_page_after_handling_before_unload_(false), | |
48 wait_until_done_(false) { | |
49 controllers_[render_view_host] = this; | |
50 } | |
51 | |
52 LayoutTestControllerHost::~LayoutTestControllerHost() { | |
53 controllers_.erase(render_view_host()); | |
54 watchdog_.Cancel(); | |
55 } | |
56 | |
57 void LayoutTestControllerHost::CaptureDump() { | |
58 if (captured_dump_) | |
59 return; | |
60 captured_dump_ = true; | |
61 | |
62 render_view_host()->Send( | |
63 new ShellViewMsg_CaptureTextDump(render_view_host()->GetRoutingID(), | |
64 dump_as_text_, | |
65 is_printing_, | |
66 dump_child_frames_)); | |
67 if (!dump_as_text_) { | |
68 render_view_host()->Send( | |
69 new ShellViewMsg_CaptureImageDump(render_view_host()->GetRoutingID(), | |
70 expected_pixel_hash_)); | |
71 } | |
72 } | |
73 | |
74 void LayoutTestControllerHost::TimeoutHandler() { | |
75 printf("FAIL: Timed out waiting for notifyDone to be called\n"); | |
76 fprintf(stderr, "FAIL: Timed out waiting for notifyDone to be called\n"); | |
77 CaptureDump(); | |
78 } | |
79 | |
80 bool LayoutTestControllerHost::OnMessageReceived( | |
81 const IPC::Message& message) { | |
82 bool handled = true; | |
83 IPC_BEGIN_MESSAGE_MAP(LayoutTestControllerHost, message) | |
84 IPC_MESSAGE_HANDLER(ShellViewHostMsg_DidFinishLoad, OnDidFinishLoad) | |
85 IPC_MESSAGE_HANDLER(ShellViewHostMsg_TextDump, OnTextDump) | |
86 IPC_MESSAGE_HANDLER(ShellViewHostMsg_ImageDump, OnImageDump) | |
87 IPC_MESSAGE_HANDLER(ShellViewHostMsg_NotifyDone, OnNotifyDone) | |
88 IPC_MESSAGE_HANDLER(ShellViewHostMsg_DumpAsText, OnDumpAsText) | |
89 IPC_MESSAGE_HANDLER(ShellViewHostMsg_DumpChildFramesAsText, | |
90 OnDumpChildFramesAsText) | |
91 IPC_MESSAGE_HANDLER(ShellViewHostMsg_SetPrinting, OnSetPrinting) | |
92 IPC_MESSAGE_HANDLER( | |
93 ShellViewHostMsg_SetShouldStayOnPageAfterHandlingBeforeUnload, | |
94 OnSetShouldStayOnPageAfterHandlingBeforeUnload) | |
95 IPC_MESSAGE_HANDLER(ShellViewHostMsg_WaitUntilDone, OnWaitUntilDone) | |
96 IPC_MESSAGE_HANDLER(ShellViewHostMsg_NotImplemented, OnNotImplemented) | |
97 IPC_MESSAGE_UNHANDLED(handled = false) | |
98 IPC_END_MESSAGE_MAP() | |
99 | |
100 return handled; | |
101 } | |
102 | |
103 void LayoutTestControllerHost::OnDidFinishLoad() { | |
104 if (wait_until_done_) | |
105 return; | |
106 | |
107 CaptureDump(); | |
108 } | |
109 | |
110 void LayoutTestControllerHost::OnTextDump(const std::string& dump) { | |
111 printf("%s#EOF\n", dump.c_str()); | |
112 fprintf(stderr, "#EOF\n"); | |
113 | |
114 if (dump_as_text_) | |
115 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
116 } | |
117 | |
118 void LayoutTestControllerHost::OnImageDump( | |
119 const std::string& actual_pixel_hash, | |
120 const SkBitmap& image) { | |
121 SkAutoLockPixels image_lock(image); | |
122 | |
123 printf("\nActualHash: %s\n", actual_pixel_hash.c_str()); | |
124 if (!expected_pixel_hash_.empty()) | |
125 printf("\nExpectedHash: %s\n", expected_pixel_hash_.c_str()); | |
126 | |
127 // Only encode and dump the png if the hashes don't match. Encoding the | |
128 // image is really expensive. | |
129 if (actual_pixel_hash != expected_pixel_hash_) { | |
130 std::vector<unsigned char> png; | |
131 | |
132 // Only the expected PNGs for Mac have a valid alpha channel. | |
133 #if defined(OS_MACOSX) | |
134 bool discard_transparency = false; | |
135 #else | |
136 bool discard_transparency = true; | |
137 #endif | |
138 | |
139 bool success = false; | |
140 #if defined(OS_ANDROID) | |
141 success = webkit_support::EncodeRGBAPNGWithChecksum( | |
142 reinterpret_cast<const unsigned char*>(image.getPixels()), | |
143 image.width(), | |
144 image.height(), | |
145 static_cast<int>(image.rowBytes()), | |
146 discard_transparency, | |
147 actual_pixel_hash, | |
148 &png); | |
149 #else | |
150 success = webkit_support::EncodeBGRAPNGWithChecksum( | |
151 reinterpret_cast<const unsigned char*>(image.getPixels()), | |
152 image.width(), | |
153 image.height(), | |
154 static_cast<int>(image.rowBytes()), | |
155 discard_transparency, | |
156 actual_pixel_hash, | |
157 &png); | |
158 #endif | |
159 if (success) { | |
160 printf("Content-Type: image/png\n"); | |
161 printf("Content-Length: %u\n", static_cast<unsigned>(png.size())); | |
162 fwrite(&png[0], 1, png.size(), stdout); | |
163 } | |
164 } | |
165 | |
166 MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
167 } | |
168 | |
169 void LayoutTestControllerHost::OnNotifyDone() { | |
170 if (!wait_until_done_) | |
171 return; | |
172 watchdog_.Cancel(); | |
173 CaptureDump(); | |
174 } | |
175 | |
176 void LayoutTestControllerHost::OnDumpAsText() { | |
177 dump_as_text_ = true; | |
178 } | |
179 | |
180 void LayoutTestControllerHost::OnSetPrinting() { | |
181 is_printing_ = true; | |
182 } | |
183 | |
184 void LayoutTestControllerHost::OnSetShouldStayOnPageAfterHandlingBeforeUnload( | |
185 bool should_stay_on_page) { | |
186 should_stay_on_page_after_handling_before_unload_ = should_stay_on_page; | |
187 } | |
188 | |
189 void LayoutTestControllerHost::OnDumpChildFramesAsText() { | |
190 dump_child_frames_ = true; | |
191 } | |
192 | |
193 void LayoutTestControllerHost::OnWaitUntilDone() { | |
194 if (wait_until_done_) | |
195 return; | |
196 if (!CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoTimeout)) { | |
197 watchdog_.Reset(base::Bind(&LayoutTestControllerHost::TimeoutHandler, | |
198 base::Unretained(this))); | |
199 MessageLoop::current()->PostDelayedTask( | |
200 FROM_HERE, | |
201 watchdog_.callback(), | |
202 base::TimeDelta::FromMilliseconds(kTestTimeoutMilliseconds)); | |
203 } | |
204 wait_until_done_ = true; | |
205 } | |
206 | |
207 void LayoutTestControllerHost::OnNotImplemented( | |
208 const std::string& object_name, | |
209 const std::string& property_name) { | |
210 if (captured_dump_) | |
211 return; | |
212 printf("FAIL: NOT IMPLEMENTED: %s.%s\n", | |
213 object_name.c_str(), property_name.c_str()); | |
214 fprintf(stderr, "FAIL: NOT IMPLEMENTED: %s.%s\n", | |
215 object_name.c_str(), property_name.c_str()); | |
216 watchdog_.Cancel(); | |
217 CaptureDump(); | |
218 } | |
219 | |
220 } // namespace content | |
OLD | NEW |