Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / shell / browser / blink_test_controller.h
blobbd05323077170c92ed16657a1e88fcbbddeea9cc
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_
8 #include <ostream>
9 #include <string>
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"
25 #endif
27 class SkBitmap;
29 namespace content {
31 class LayoutTestDevToolsFrontend;
32 class Shell;
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;
43 #endif
45 class BlinkTestResultPrinter {
46 public:
47 BlinkTestResultPrinter(std::ostream* output, std::ostream* error);
48 ~BlinkTestResultPrinter();
50 void reset() {
51 state_ = DURING_TEST;
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);
79 void CloseStderr();
81 private:
82 void PrintEncodedBinaryData(const std::vector<unsigned char>& data);
84 enum State {
85 DURING_TEST,
86 IN_TEXT_BLOCK,
87 IN_AUDIO_BLOCK,
88 IN_IMAGE_BLOCK,
89 AFTER_TEST
91 State state_;
93 bool capture_text_only_;
94 bool encode_binary_data_;
96 std::ostream* output_;
97 std::ostream* error_;
99 DISALLOW_COPY_AND_ASSIGN(BlinkTestResultPrinter);
102 class BlinkTestController : public base::NonThreadSafe,
103 public WebContentsObserver,
104 public NotificationObserver,
105 public GpuDataManagerObserver {
106 public:
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 OverrideWebkitPrefs(WebPreferences* prefs);
123 void OpenURL(const GURL& url);
124 void TestFinishedInSecondaryWindow();
125 bool IsMainWindow(WebContents* web_contents) const;
127 BlinkTestResultPrinter* printer() { return printer_.get(); }
128 void set_printer(BlinkTestResultPrinter* printer) { printer_.reset(printer); }
130 void DevToolsProcessCrashed();
132 // WebContentsObserver implementation.
133 bool OnMessageReceived(const IPC::Message& message) override;
134 void PluginCrashed(const base::FilePath& plugin_path,
135 base::ProcessId plugin_pid) override;
136 void RenderViewCreated(RenderViewHost* render_view_host) override;
137 void RenderProcessGone(base::TerminationStatus status) override;
138 void WebContentsDestroyed() override;
140 // NotificationObserver implementation.
141 void Observe(int type,
142 const NotificationSource& source,
143 const NotificationDetails& details) override;
145 // GpuDataManagerObserver implementation.
146 void OnGpuProcessCrashed(base::TerminationStatus exit_code) override;
148 private:
149 enum TestPhase {
150 BETWEEN_TESTS,
151 DURING_TEST,
152 CLEAN_UP
155 static BlinkTestController* instance_;
157 void DiscardMainWindow();
158 void SendTestConfiguration();
160 // Message handlers.
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 const std::string& frontend_url);
170 void OnCloseDevTools();
171 void OnGoToOffset(int offset);
172 void OnReload();
173 void OnLoadURLForFrame(const GURL& url, const std::string& frame_name);
174 void OnCaptureSessionHistory();
175 void OnCloseRemainingWindows();
176 void OnResetDone();
177 void OnLeakDetectionDone(const content::LeakDetectionResult& result);
179 scoped_ptr<BlinkTestResultPrinter> printer_;
181 base::FilePath current_working_directory_;
182 base::FilePath temp_path_;
184 Shell* main_window_;
186 // The PID of the render process of the render view host of main_window_.
187 int current_pid_;
189 // True if we should set the test configuration to the next RenderViewHost
190 // created.
191 bool send_configuration_to_next_host_;
193 // What phase of running an individual test we are currently in.
194 TestPhase test_phase_;
196 // True if the currently running test is a compositing test.
197 bool is_compositing_test_;
199 // Per test config.
200 bool enable_pixel_dumping_;
201 std::string expected_pixel_hash_;
202 gfx::Size initial_size_;
203 GURL test_url_;
205 // True if the WebPreferences of newly created RenderViewHost should be
206 // overridden with prefs_.
207 bool should_override_prefs_;
208 WebPreferences prefs_;
210 NotificationRegistrar registrar_;
212 const bool is_leak_detection_enabled_;
213 bool crash_when_leak_found_;
215 LayoutTestDevToolsFrontend* devtools_frontend_;
217 #if defined(OS_ANDROID)
218 // Because of the nested message pump implementation, Android needs to allow
219 // waiting on the UI thread while layout tests are being ran.
220 ScopedAllowWaitForAndroidLayoutTests reduced_restrictions_;
221 #endif
223 DISALLOW_COPY_AND_ASSIGN(BlinkTestController);
226 } // namespace content
228 #endif // CONTENT_SHELL_BROWSER_BLINK_TEST_CONTROLLER_H_