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_BLINK_TEST_CONTROLLER_H_
6 #define CONTENT_SHELL_BROWSER_BLINK_TEST_CONTROLLER_H_
11 #include "base/cancelable_callback.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/non_thread_safe.h"
16 #include "content/public/browser/bluetooth_chooser.h"
17 #include "content/public/browser/gpu_data_manager_observer.h"
18 #include "content/public/browser/notification_observer.h"
19 #include "content/public/browser/notification_registrar.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "content/public/common/web_preferences.h"
22 #include "content/shell/common/leak_detection_result.h"
23 #include "ui/gfx/geometry/size.h"
25 #if defined(OS_ANDROID)
26 #include "base/threading/thread_restrictions.h"
33 class LayoutTestBluetoothChooserFactory
;
34 class LayoutTestDevToolsFrontend
;
37 #if defined(OS_ANDROID)
38 // Android uses a nested message loop for running layout tests because the
39 // default message loop, provided by the system, does not offer a blocking
40 // Run() method. The loop itself, implemented as NestedMessagePumpAndroid,
41 // uses a base::WaitableEvent allowing it to sleep until more events arrive.
42 class ScopedAllowWaitForAndroidLayoutTests
{
44 base::ThreadRestrictions::ScopedAllowWait wait
;
48 class BlinkTestResultPrinter
{
50 BlinkTestResultPrinter(std::ostream
* output
, std::ostream
* error
);
51 ~BlinkTestResultPrinter();
56 bool output_finished() const { return state_
== AFTER_TEST
; }
57 void set_capture_text_only(bool capture_text_only
) {
58 capture_text_only_
= capture_text_only
;
61 void set_encode_binary_data(bool encode_binary_data
) {
62 encode_binary_data_
= encode_binary_data
;
65 void PrintTextHeader();
66 void PrintTextBlock(const std::string
& block
);
67 void PrintTextFooter();
69 void PrintImageHeader(const std::string
& actual_hash
,
70 const std::string
& expected_hash
);
71 void PrintImageBlock(const std::vector
<unsigned char>& png_image
);
72 void PrintImageFooter();
74 void PrintAudioHeader();
75 void PrintAudioBlock(const std::vector
<unsigned char>& audio_data
);
76 void PrintAudioFooter();
78 void AddMessage(const std::string
& message
);
79 void AddMessageRaw(const std::string
& message
);
80 void AddErrorMessage(const std::string
& message
);
85 void PrintEncodedBinaryData(const std::vector
<unsigned char>& data
);
96 bool capture_text_only_
;
97 bool encode_binary_data_
;
99 std::ostream
* output_
;
100 std::ostream
* error_
;
102 DISALLOW_COPY_AND_ASSIGN(BlinkTestResultPrinter
);
105 class BlinkTestController
: public base::NonThreadSafe
,
106 public WebContentsObserver
,
107 public NotificationObserver
,
108 public GpuDataManagerObserver
{
110 static BlinkTestController
* Get();
112 BlinkTestController();
113 ~BlinkTestController() override
;
115 // True if the controller is ready for testing.
116 bool PrepareForLayoutTest(const GURL
& test_url
,
117 const base::FilePath
& current_working_directory
,
118 bool enable_pixel_dumping
,
119 const std::string
& expected_pixel_hash
);
120 // True if the controller was reset successfully.
121 bool ResetAfterLayoutTest();
123 void SetTempPath(const base::FilePath
& temp_path
);
124 void RendererUnresponsive();
125 void OverrideWebkitPrefs(WebPreferences
* prefs
);
126 void OpenURL(const GURL
& url
);
127 void TestFinishedInSecondaryWindow();
128 bool IsMainWindow(WebContents
* web_contents
) const;
129 scoped_ptr
<BluetoothChooser
> RunBluetoothChooser(
130 WebContents
* web_contents
,
131 const BluetoothChooser::EventHandler
& event_handler
,
134 BlinkTestResultPrinter
* printer() { return printer_
.get(); }
135 void set_printer(BlinkTestResultPrinter
* printer
) { printer_
.reset(printer
); }
137 void DevToolsProcessCrashed();
139 // WebContentsObserver implementation.
140 bool OnMessageReceived(const IPC::Message
& message
) override
;
141 void PluginCrashed(const base::FilePath
& plugin_path
,
142 base::ProcessId plugin_pid
) override
;
143 void RenderViewCreated(RenderViewHost
* render_view_host
) override
;
144 void RenderProcessGone(base::TerminationStatus status
) override
;
145 void WebContentsDestroyed() override
;
147 // NotificationObserver implementation.
148 void Observe(int type
,
149 const NotificationSource
& source
,
150 const NotificationDetails
& details
) override
;
152 // GpuDataManagerObserver implementation.
153 void OnGpuProcessCrashed(base::TerminationStatus exit_code
) override
;
162 static BlinkTestController
* instance_
;
164 void DiscardMainWindow();
165 void SendTestConfiguration();
168 void OnAudioDump(const std::vector
<unsigned char>& audio_dump
);
169 void OnImageDump(const std::string
& actual_pixel_hash
, const SkBitmap
& image
);
170 void OnTextDump(const std::string
& dump
);
171 void OnPrintMessage(const std::string
& message
);
172 void OnOverridePreferences(const WebPreferences
& prefs
);
173 void OnTestFinished();
174 void OnClearDevToolsLocalStorage();
175 void OnShowDevTools(const std::string
& settings
,
176 const std::string
& frontend_url
);
177 void OnCloseDevTools();
178 void OnGoToOffset(int offset
);
180 void OnLoadURLForFrame(const GURL
& url
, const std::string
& frame_name
);
181 void OnCaptureSessionHistory();
182 void OnCloseRemainingWindows();
184 void OnLeakDetectionDone(const content::LeakDetectionResult
& result
);
185 void OnSetBluetoothManualChooser(bool enable
);
186 void OnGetBluetoothManualChooserEvents(std::vector
<std::string
>* events
);
187 void OnSendBluetoothManualChooserEvent(const std::string
& event
,
188 const std::string
& argument
);
190 scoped_ptr
<BlinkTestResultPrinter
> printer_
;
192 base::FilePath current_working_directory_
;
193 base::FilePath temp_path_
;
197 // The PID of the render process of the render view host of main_window_.
200 // True if we should set the test configuration to the next RenderViewHost
202 bool send_configuration_to_next_host_
;
204 // What phase of running an individual test we are currently in.
205 TestPhase test_phase_
;
207 // True if the currently running test is a compositing test.
208 bool is_compositing_test_
;
211 bool enable_pixel_dumping_
;
212 std::string expected_pixel_hash_
;
213 gfx::Size initial_size_
;
216 // True if the WebPreferences of newly created RenderViewHost should be
217 // overridden with prefs_.
218 bool should_override_prefs_
;
219 WebPreferences prefs_
;
221 NotificationRegistrar registrar_
;
223 const bool is_leak_detection_enabled_
;
224 bool crash_when_leak_found_
;
226 LayoutTestDevToolsFrontend
* devtools_frontend_
;
228 scoped_ptr
<LayoutTestBluetoothChooserFactory
> bluetooth_chooser_factory_
;
230 #if defined(OS_ANDROID)
231 // Because of the nested message pump implementation, Android needs to allow
232 // waiting on the UI thread while layout tests are being ran.
233 ScopedAllowWaitForAndroidLayoutTests reduced_restrictions_
;
236 DISALLOW_COPY_AND_ASSIGN(BlinkTestController
);
239 } // namespace content
241 #endif // CONTENT_SHELL_BROWSER_BLINK_TEST_CONTROLLER_H_