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.
5 #ifndef CONTENT_SHELL_BROWSER_WEBKIT_TEST_CONTROLLER_H_
6 #define CONTENT_SHELL_BROWSER_WEBKIT_TEST_CONTROLLER_H_
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/shell/common/leak_detection_result.h"
20 #include "ui/gfx/size.h"
21 #include "webkit/common/webpreferences.h"
23 #if defined(OS_ANDROID)
24 #include "base/threading/thread_restrictions.h"
33 #if defined(OS_ANDROID)
34 // Android uses a nested message loop for running layout tests because the
35 // default message loop, provided by the system, does not offer a blocking
36 // Run() method. The loop itself, implemented as NestedMessagePumpAndroid,
37 // uses a base::WaitableEvent allowing it to sleep until more events arrive.
38 class ScopedAllowWaitForAndroidLayoutTests {
40 base::ThreadRestrictions::ScopedAllowWait wait;
44 class WebKitTestResultPrinter {
46 WebKitTestResultPrinter(std::ostream* output, std::ostream* error);
47 ~WebKitTestResultPrinter();
52 bool output_finished() const { return state_ == AFTER_TEST; }
53 void set_capture_text_only(bool capture_text_only) {
54 capture_text_only_ = capture_text_only;
57 void set_encode_binary_data(bool encode_binary_data) {
58 encode_binary_data_ = encode_binary_data;
61 void PrintTextHeader();
62 void PrintTextBlock(const std::string& block);
63 void PrintTextFooter();
65 void PrintImageHeader(const std::string& actual_hash,
66 const std::string& expected_hash);
67 void PrintImageBlock(const std::vector<unsigned char>& png_image);
68 void PrintImageFooter();
70 void PrintAudioHeader();
71 void PrintAudioBlock(const std::vector<unsigned char>& audio_data);
72 void PrintAudioFooter();
74 void AddMessage(const std::string& message);
75 void AddMessageRaw(const std::string& message);
76 void AddErrorMessage(const std::string& message);
81 void PrintEncodedBinaryData(const std::vector<unsigned char>& data);
92 bool capture_text_only_;
93 bool encode_binary_data_;
95 std::ostream* output_;
98 DISALLOW_COPY_AND_ASSIGN(WebKitTestResultPrinter);
101 class WebKitTestController : public base::NonThreadSafe,
102 public WebContentsObserver,
103 public NotificationObserver,
104 public GpuDataManagerObserver {
106 static WebKitTestController* Get();
108 WebKitTestController();
109 virtual ~WebKitTestController();
111 // True if the controller is ready for testing.
112 bool PrepareForLayoutTest(const GURL& test_url,
113 const base::FilePath& current_working_directory,
114 bool enable_pixel_dumping,
115 const std::string& expected_pixel_hash);
116 // True if the controller was reset successfully.
117 bool ResetAfterLayoutTest();
119 void SetTempPath(const base::FilePath& temp_path);
120 void RendererUnresponsive();
121 void WorkerCrashed();
122 void OverrideWebkitPrefs(WebPreferences* prefs);
123 void OpenURL(const GURL& url);
124 void TestFinishedInSecondaryWindow();
125 bool IsMainWindow(WebContents* web_contents) const;
127 WebKitTestResultPrinter* printer() { return printer_.get(); }
128 void set_printer(WebKitTestResultPrinter* printer) {
129 printer_.reset(printer);
132 // WebContentsObserver implementation.
133 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
134 virtual void PluginCrashed(const base::FilePath& plugin_path,
135 base::ProcessId plugin_pid) OVERRIDE;
136 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE;
137 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
138 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;
140 // NotificationObserver implementation.
141 virtual void Observe(int type,
142 const NotificationSource& source,
143 const NotificationDetails& details) OVERRIDE;
145 // GpuDataManagerObserver implementation.
146 virtual void OnGpuProcessCrashed(base::TerminationStatus exit_code) OVERRIDE;
155 static WebKitTestController* instance_;
157 void DiscardMainWindow();
158 void SendTestConfiguration();
161 void OnAudioDump(const std::vector<unsigned char>& audio_dump);
162 void OnImageDump(const std::string& actual_pixel_hash, const SkBitmap& image);
163 void OnTextDump(const std::string& dump);
164 void OnPrintMessage(const std::string& message);
165 void OnOverridePreferences(const WebPreferences& prefs);
166 void OnTestFinished();
167 void OnClearDevToolsLocalStorage();
168 void OnShowDevTools(const std::string& settings);
169 void OnCloseDevTools();
170 void OnGoToOffset(int offset);
172 void OnLoadURLForFrame(const GURL& url, const std::string& frame_name);
173 void OnCaptureSessionHistory();
174 void OnCloseRemainingWindows();
176 void OnLeakDetectionDone(const content::LeakDetectionResult& result);
178 scoped_ptr<WebKitTestResultPrinter> printer_;
180 base::FilePath current_working_directory_;
181 base::FilePath temp_path_;
185 // The PID of the render process of the render view host of main_window_.
188 // True if we should set the test configuration to the next RenderViewHost
190 bool send_configuration_to_next_host_;
192 // What phase of running an individual test we are currently in.
193 TestPhase test_phase_;
195 // True if the currently running test is a compositing test.
196 bool is_compositing_test_;
199 bool enable_pixel_dumping_;
200 std::string expected_pixel_hash_;
201 gfx::Size initial_size_;
204 // True if the WebPreferences of newly created RenderViewHost should be
205 // overridden with prefs_.
206 bool should_override_prefs_;
207 WebPreferences prefs_;
209 NotificationRegistrar registrar_;
211 const bool is_leak_detection_enabled_;
213 #if defined(OS_ANDROID)
214 // Because of the nested message pump implementation, Android needs to allow
215 // waiting on the UI thread while layout tests are being ran.
216 ScopedAllowWaitForAndroidLayoutTests reduced_restrictions_;
219 DISALLOW_COPY_AND_ASSIGN(WebKitTestController);
222 } // namespace content
224 #endif // CONTENT_SHELL_BROWSER_WEBKIT_TEST_CONTROLLER_H_