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/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"
23 #if defined(OS_ANDROID)
24 #include "base/threading/thread_restrictions.h"
31 class LayoutTestDevToolsFrontend
;
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
{
41 base::ThreadRestrictions::ScopedAllowWait wait
;
45 class BlinkTestResultPrinter
{
47 BlinkTestResultPrinter(std::ostream
* output
, std::ostream
* error
);
48 ~BlinkTestResultPrinter();
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
;
58 void set_encode_binary_data(bool encode_binary_data
) {
59 encode_binary_data_
= encode_binary_data
;
62 void PrintTextHeader();
63 void PrintTextBlock(const std::string
& block
);
64 void PrintTextFooter();
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();
71 void PrintAudioHeader();
72 void PrintAudioBlock(const std::vector
<unsigned char>& audio_data
);
73 void PrintAudioFooter();
75 void AddMessage(const std::string
& message
);
76 void AddMessageRaw(const std::string
& message
);
77 void AddErrorMessage(const std::string
& message
);
82 void PrintEncodedBinaryData(const std::vector
<unsigned char>& data
);
93 bool capture_text_only_
;
94 bool encode_binary_data_
;
96 std::ostream
* output_
;
99 DISALLOW_COPY_AND_ASSIGN(BlinkTestResultPrinter
);
102 class BlinkTestController
: public base::NonThreadSafe
,
103 public WebContentsObserver
,
104 public NotificationObserver
,
105 public GpuDataManagerObserver
{
107 static BlinkTestController
* Get();
109 BlinkTestController();
110 ~BlinkTestController() override
;
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();
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;
128 BlinkTestResultPrinter
* printer() { return printer_
.get(); }
129 void set_printer(BlinkTestResultPrinter
* printer
) { printer_
.reset(printer
); }
131 void DevToolsProcessCrashed();
133 // WebContentsObserver implementation.
134 bool OnMessageReceived(const IPC::Message
& message
) override
;
135 void PluginCrashed(const base::FilePath
& plugin_path
,
136 base::ProcessId plugin_pid
) override
;
137 void RenderViewCreated(RenderViewHost
* render_view_host
) override
;
138 void RenderProcessGone(base::TerminationStatus status
) override
;
139 void WebContentsDestroyed() override
;
141 // NotificationObserver implementation.
142 void Observe(int type
,
143 const NotificationSource
& source
,
144 const NotificationDetails
& details
) override
;
146 // GpuDataManagerObserver implementation.
147 void OnGpuProcessCrashed(base::TerminationStatus exit_code
) override
;
156 static BlinkTestController
* instance_
;
158 void DiscardMainWindow();
159 void SendTestConfiguration();
162 void OnAudioDump(const std::vector
<unsigned char>& audio_dump
);
163 void OnImageDump(const std::string
& actual_pixel_hash
, const SkBitmap
& image
);
164 void OnTextDump(const std::string
& dump
);
165 void OnPrintMessage(const std::string
& message
);
166 void OnOverridePreferences(const WebPreferences
& prefs
);
167 void OnTestFinished();
168 void OnClearDevToolsLocalStorage();
169 void OnShowDevTools(const std::string
& settings
,
170 const std::string
& frontend_url
);
171 void OnCloseDevTools();
172 void OnGoToOffset(int offset
);
174 void OnLoadURLForFrame(const GURL
& url
, const std::string
& frame_name
);
175 void OnCaptureSessionHistory();
176 void OnCloseRemainingWindows();
178 void OnLeakDetectionDone(const content::LeakDetectionResult
& result
);
180 scoped_ptr
<BlinkTestResultPrinter
> printer_
;
182 base::FilePath current_working_directory_
;
183 base::FilePath temp_path_
;
187 // The PID of the render process of the render view host of main_window_.
190 // True if we should set the test configuration to the next RenderViewHost
192 bool send_configuration_to_next_host_
;
194 // What phase of running an individual test we are currently in.
195 TestPhase test_phase_
;
197 // True if the currently running test is a compositing test.
198 bool is_compositing_test_
;
201 bool enable_pixel_dumping_
;
202 std::string expected_pixel_hash_
;
203 gfx::Size initial_size_
;
206 // True if the WebPreferences of newly created RenderViewHost should be
207 // overridden with prefs_.
208 bool should_override_prefs_
;
209 WebPreferences prefs_
;
211 NotificationRegistrar registrar_
;
213 const bool is_leak_detection_enabled_
;
214 bool crash_when_leak_found_
;
216 LayoutTestDevToolsFrontend
* devtools_frontend_
;
218 #if defined(OS_ANDROID)
219 // Because of the nested message pump implementation, Android needs to allow
220 // waiting on the UI thread while layout tests are being ran.
221 ScopedAllowWaitForAndroidLayoutTests reduced_restrictions_
;
224 DISALLOW_COPY_AND_ASSIGN(BlinkTestController
);
227 } // namespace content
229 #endif // CONTENT_SHELL_BROWSER_BLINK_TEST_CONTROLLER_H_