OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CONTENT_SHELL_BROWSER_WEBKIT_TEST_CONTROLLER_H_ | |
6 #define CONTENT_SHELL_BROWSER_WEBKIT_TEST_CONTROLLER_H_ | |
7 | |
8 #include <ostream> | |
9 #include <string> | |
10 | |
11 #include "base/cancelable_callback.h" | |
12 #include "base/files/file_path.h" | |
13 #include "base/synchronization/lock.h" | |
14 #include "base/threading/non_thread_safe.h" | |
15 #include "content/public/browser/gpu_data_manager_observer.h" | |
16 #include "content/public/browser/notification_observer.h" | |
17 #include "content/public/browser/notification_registrar.h" | |
18 #include "content/public/browser/web_contents_observer.h" | |
19 #include "content/public/common/web_preferences.h" | |
20 #include "content/shell/common/leak_detection_result.h" | |
21 #include "ui/gfx/geometry/size.h" | |
22 | |
23 #if defined(OS_ANDROID) | |
24 #include "base/threading/thread_restrictions.h" | |
25 #endif | |
26 | |
27 class SkBitmap; | |
28 | |
29 namespace content { | |
30 | |
31 class LayoutTestDevToolsFrontend; | |
32 class Shell; | |
33 | |
34 #if defined(OS_ANDROID) | |
35 // Android uses a nested message loop for running layout tests because the | |
36 // default message loop, provided by the system, does not offer a blocking | |
37 // Run() method. The loop itself, implemented as NestedMessagePumpAndroid, | |
38 // uses a base::WaitableEvent allowing it to sleep until more events arrive. | |
39 class ScopedAllowWaitForAndroidLayoutTests { | |
40 private: | |
41 base::ThreadRestrictions::ScopedAllowWait wait; | |
42 }; | |
43 #endif | |
44 | |
45 class WebKitTestResultPrinter { | |
46 public: | |
47 WebKitTestResultPrinter(std::ostream* output, std::ostream* error); | |
48 ~WebKitTestResultPrinter(); | |
49 | |
50 void reset() { | |
51 state_ = DURING_TEST; | |
52 } | |
53 bool output_finished() const { return state_ == AFTER_TEST; } | |
54 void set_capture_text_only(bool capture_text_only) { | |
55 capture_text_only_ = capture_text_only; | |
56 } | |
57 | |
58 void set_encode_binary_data(bool encode_binary_data) { | |
59 encode_binary_data_ = encode_binary_data; | |
60 } | |
61 | |
62 void PrintTextHeader(); | |
63 void PrintTextBlock(const std::string& block); | |
64 void PrintTextFooter(); | |
65 | |
66 void PrintImageHeader(const std::string& actual_hash, | |
67 const std::string& expected_hash); | |
68 void PrintImageBlock(const std::vector<unsigned char>& png_image); | |
69 void PrintImageFooter(); | |
70 | |
71 void PrintAudioHeader(); | |
72 void PrintAudioBlock(const std::vector<unsigned char>& audio_data); | |
73 void PrintAudioFooter(); | |
74 | |
75 void AddMessage(const std::string& message); | |
76 void AddMessageRaw(const std::string& message); | |
77 void AddErrorMessage(const std::string& message); | |
78 | |
79 void CloseStderr(); | |
80 | |
81 private: | |
82 void PrintEncodedBinaryData(const std::vector<unsigned char>& data); | |
83 | |
84 enum State { | |
85 DURING_TEST, | |
86 IN_TEXT_BLOCK, | |
87 IN_AUDIO_BLOCK, | |
88 IN_IMAGE_BLOCK, | |
89 AFTER_TEST | |
90 }; | |
91 State state_; | |
92 | |
93 bool capture_text_only_; | |
94 bool encode_binary_data_; | |
95 | |
96 std::ostream* output_; | |
97 std::ostream* error_; | |
98 | |
99 DISALLOW_COPY_AND_ASSIGN(WebKitTestResultPrinter); | |
100 }; | |
101 | |
102 class WebKitTestController : public base::NonThreadSafe, | |
103 public WebContentsObserver, | |
104 public NotificationObserver, | |
105 public GpuDataManagerObserver { | |
106 public: | |
107 static WebKitTestController* Get(); | |
108 | |
109 WebKitTestController(); | |
110 ~WebKitTestController() override; | |
111 | |
112 // True if the controller is ready for testing. | |
113 bool PrepareForLayoutTest(const GURL& test_url, | |
114 const base::FilePath& current_working_directory, | |
115 bool enable_pixel_dumping, | |
116 const std::string& expected_pixel_hash); | |
117 // True if the controller was reset successfully. | |
118 bool ResetAfterLayoutTest(); | |
119 | |
120 void SetTempPath(const base::FilePath& temp_path); | |
121 void RendererUnresponsive(); | |
122 void WorkerCrashed(); | |
123 void OverrideWebkitPrefs(WebPreferences* prefs); | |
124 void OpenURL(const GURL& url); | |
125 void TestFinishedInSecondaryWindow(); | |
126 bool IsMainWindow(WebContents* web_contents) const; | |
127 | |
128 WebKitTestResultPrinter* printer() { return printer_.get(); } | |
129 void set_printer(WebKitTestResultPrinter* printer) { | |
130 printer_.reset(printer); | |
131 } | |
132 | |
133 void DevToolsProcessCrashed(); | |
134 | |
135 // WebContentsObserver implementation. | |
136 bool OnMessageReceived(const IPC::Message& message) override; | |
137 void PluginCrashed(const base::FilePath& plugin_path, | |
138 base::ProcessId plugin_pid) override; | |
139 void RenderViewCreated(RenderViewHost* render_view_host) override; | |
140 void RenderProcessGone(base::TerminationStatus status) override; | |
141 void WebContentsDestroyed() override; | |
142 | |
143 // NotificationObserver implementation. | |
144 void Observe(int type, | |
145 const NotificationSource& source, | |
146 const NotificationDetails& details) override; | |
147 | |
148 // GpuDataManagerObserver implementation. | |
149 void OnGpuProcessCrashed(base::TerminationStatus exit_code) override; | |
150 | |
151 private: | |
152 enum TestPhase { | |
153 BETWEEN_TESTS, | |
154 DURING_TEST, | |
155 CLEAN_UP | |
156 }; | |
157 | |
158 static WebKitTestController* instance_; | |
159 | |
160 void DiscardMainWindow(); | |
161 void SendTestConfiguration(); | |
162 | |
163 // Message handlers. | |
164 void OnAudioDump(const std::vector<unsigned char>& audio_dump); | |
165 void OnImageDump(const std::string& actual_pixel_hash, const SkBitmap& image); | |
166 void OnTextDump(const std::string& dump); | |
167 void OnPrintMessage(const std::string& message); | |
168 void OnOverridePreferences(const WebPreferences& prefs); | |
169 void OnTestFinished(); | |
170 void OnClearDevToolsLocalStorage(); | |
171 void OnShowDevTools(const std::string& settings, | |
172 const std::string& frontend_url); | |
173 void OnCloseDevTools(); | |
174 void OnGoToOffset(int offset); | |
175 void OnReload(); | |
176 void OnLoadURLForFrame(const GURL& url, const std::string& frame_name); | |
177 void OnCaptureSessionHistory(); | |
178 void OnCloseRemainingWindows(); | |
179 void OnResetDone(); | |
180 void OnLeakDetectionDone(const content::LeakDetectionResult& result); | |
181 | |
182 scoped_ptr<WebKitTestResultPrinter> printer_; | |
183 | |
184 base::FilePath current_working_directory_; | |
185 base::FilePath temp_path_; | |
186 | |
187 Shell* main_window_; | |
188 | |
189 // The PID of the render process of the render view host of main_window_. | |
190 int current_pid_; | |
191 | |
192 // True if we should set the test configuration to the next RenderViewHost | |
193 // created. | |
194 bool send_configuration_to_next_host_; | |
195 | |
196 // What phase of running an individual test we are currently in. | |
197 TestPhase test_phase_; | |
198 | |
199 // True if the currently running test is a compositing test. | |
200 bool is_compositing_test_; | |
201 | |
202 // Per test config. | |
203 bool enable_pixel_dumping_; | |
204 std::string expected_pixel_hash_; | |
205 gfx::Size initial_size_; | |
206 GURL test_url_; | |
207 | |
208 // True if the WebPreferences of newly created RenderViewHost should be | |
209 // overridden with prefs_. | |
210 bool should_override_prefs_; | |
211 WebPreferences prefs_; | |
212 | |
213 NotificationRegistrar registrar_; | |
214 | |
215 const bool is_leak_detection_enabled_; | |
216 bool crash_when_leak_found_; | |
217 | |
218 LayoutTestDevToolsFrontend* devtools_frontend_; | |
219 | |
220 #if defined(OS_ANDROID) | |
221 // Because of the nested message pump implementation, Android needs to allow | |
222 // waiting on the UI thread while layout tests are being ran. | |
223 ScopedAllowWaitForAndroidLayoutTests reduced_restrictions_; | |
224 #endif | |
225 | |
226 DISALLOW_COPY_AND_ASSIGN(WebKitTestController); | |
227 }; | |
228 | |
229 } // namespace content | |
230 | |
231 #endif // CONTENT_SHELL_BROWSER_WEBKIT_TEST_CONTROLLER_H_ | |
OLD | NEW |