[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / shell / webkit_test_controller.h
bloba249a1bfea37563d8af11bf323dd314bd6094f98
1 // Copyright (c) 2012 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_WEBKIT_TEST_CONTROLLER_H_
6 #define CONTENT_SHELL_WEBKIT_TEST_CONTROLLER_H_
8 #include <ostream>
9 #include <string>
11 #include "base/cancelable_callback.h"
12 #include "base/file_path.h"
13 #include "base/synchronization/lock.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "content/public/browser/render_view_host_observer.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "webkit/glue/webpreferences.h"
19 class SkBitmap;
21 namespace content {
23 class Shell;
25 class WebKitTestResultPrinter {
26 public:
27 WebKitTestResultPrinter(std::ostream* output, std::ostream* error);
28 ~WebKitTestResultPrinter();
30 void reset() {
31 state_ = BEFORE_TEST;
33 bool in_text_block() const { return state_ == IN_TEXT_BLOCK; }
34 void set_capture_text_only(bool capture_text_only) {
35 capture_text_only_ = capture_text_only;
38 void PrintTextHeader();
39 void PrintTextBlock(const std::string& block);
40 void PrintTextFooter();
42 void PrintImageHeader(const std::string& actual_hash,
43 const std::string& expected_hash);
44 void PrintImageBlock(const std::vector<unsigned char>& png_image);
45 void PrintImageFooter();
47 void AddMessage(const std::string& message);
48 void AddMessageRaw(const std::string& message);
49 void AddErrorMessage(const std::string& message);
51 private:
52 enum State {
53 BEFORE_TEST,
54 IN_TEXT_BLOCK,
55 IN_IMAGE_BLOCK,
56 AFTER_TEST
58 State state_;
59 bool capture_text_only_;
61 std::ostream* output_;
62 std::ostream* error_;
64 DISALLOW_COPY_AND_ASSIGN(WebKitTestResultPrinter);
67 class WebKitTestController : public base::NonThreadSafe,
68 public WebContentsObserver {
69 public:
70 static WebKitTestController* Get();
72 WebKitTestController();
73 virtual ~WebKitTestController();
75 // True if the controller is ready for testing.
76 bool PrepareForLayoutTest(const GURL& test_url,
77 const FilePath& current_working_directory,
78 bool enable_pixel_dumping,
79 const std::string& expected_pixel_hash);
80 // True if the controller was reset successfully.
81 bool ResetAfterLayoutTest();
83 void RendererUnresponsive();
85 WebKitTestResultPrinter* printer() { return printer_.get(); }
86 void set_printer(WebKitTestResultPrinter* printer) {
87 printer_.reset(printer);
89 const webkit_glue::WebPreferences& web_preferences() const { return prefs_; }
90 bool should_stay_on_page_after_handling_before_unload() const {
91 return should_stay_on_page_after_handling_before_unload_;
94 // This method can be invoked on any thread.
95 bool CanOpenWindows() const;
97 // WebContentsObserver implementation.
98 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
99 virtual void PluginCrashed(const FilePath& plugin_path) OVERRIDE;
100 virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE;
101 virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE;
102 virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;
104 private:
105 static WebKitTestController* instance_;
107 void CaptureDump();
108 void TimeoutHandler();
110 // Message handlers.
111 void OnDidFinishLoad();
112 void OnImageDump(const std::string& actual_pixel_hash, const SkBitmap& image);
113 void OnTextDump(const std::string& dump);
114 void OnPrintMessage(const std::string& message);
115 void OnReadFileToString(const FilePath& file_path, std::string* contents);
116 void OnOverridePreferences(const webkit_glue::WebPreferences& prefs);
117 void OnNotifyDone();
118 void OnDumpAsText();
119 void OnDumpChildFramesAsText();
120 void OnSetPrinting();
121 void OnSetShouldStayOnPageAfterHandlingBeforeUnload(bool should_stay_on_page);
122 void OnWaitUntilDone();
123 void OnCanOpenWindows();
124 void OnShowWebInspector();
125 void OnCloseWebInspector();
126 void OnRegisterIsolatedFileSystem(
127 const std::vector<FilePath>& absolute_filenames,
128 std::string* filesystem_id);
130 void OnNotImplemented(const std::string& object_name,
131 const std::string& method_name);
133 scoped_ptr<WebKitTestResultPrinter> printer_;
135 FilePath current_working_directory_;
137 Shell* main_window_;
139 bool enable_pixel_dumping_;
140 std::string expected_pixel_hash_;
142 bool captured_dump_;
144 bool dump_as_text_;
145 bool dump_child_frames_;
146 bool is_printing_;
147 bool should_stay_on_page_after_handling_before_unload_;
148 bool wait_until_done_;
149 bool did_finish_load_;
151 webkit_glue::WebPreferences prefs_;
153 base::CancelableClosure watchdog_;
155 // Access to the following variables needs to be guarded by |lock_|.
156 mutable base::Lock lock_;
157 bool can_open_windows_;
159 DISALLOW_COPY_AND_ASSIGN(WebKitTestController);
162 } // namespace content
164 #endif // CONTENT_SHELL_WEBKIT_TEST_CONTROLLER_H_