summaryrefslogtreecommitdiffstats
path: root/content/shell/webkit_test_runner_host.h
blob: 53573bf427253d2423c3c3ee1507c5c7b77accb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_SHELL_WEBKIT_TEST_RUNNER_HOST_H_
#define CONTENT_SHELL_WEBKIT_TEST_RUNNER_HOST_H_

#include <ostream>
#include <string>

#include "base/cancelable_callback.h"
#include "base/file_path.h"
#include "base/threading/non_thread_safe.h"
#include "content/public/browser/render_view_host_observer.h"
#include "content/public/browser/web_contents_observer.h"

class SkBitmap;

namespace content {

class Shell;

class WebKitTestResultPrinter {
 public:
  WebKitTestResultPrinter(std::ostream* output, std::ostream* error);
  ~WebKitTestResultPrinter();

  void reset() {
    state_ = BEFORE_TEST;
  }
  bool in_text_block() const { return state_ == IN_TEXT_BLOCK; }
  void set_capture_text_only(bool capture_text_only) {
    capture_text_only_ = capture_text_only;
  }

  void PrintTextHeader();
  void PrintTextBlock(const std::string& block);
  void PrintTextFooter();

  void PrintImageHeader(const std::string& actual_hash,
                        const std::string& expected_hash);
  void PrintImageBlock(const std::vector<unsigned char>& png_image);
  void PrintImageFooter();

  void AddMessage(const std::string& message);
  void AddMessageRaw(const std::string& message);
  void AddErrorMessage(const std::string& message);

 private:
  enum State {
    BEFORE_TEST,
    IN_TEXT_BLOCK,
    IN_IMAGE_BLOCK,
    AFTER_TEST
  };
  State state_;
  bool capture_text_only_;

  std::ostream* output_;
  std::ostream* error_;

  DISALLOW_COPY_AND_ASSIGN(WebKitTestResultPrinter);
};

class WebKitTestController : public base::NonThreadSafe,
                             public WebContentsObserver {
 public:
  static WebKitTestController* Get();

  WebKitTestController();
  virtual ~WebKitTestController();

  // True if the controller is ready for testing.
  bool PrepareForLayoutTest(const GURL& test_url,
                            const FilePath& current_working_directory,
                            bool enable_pixel_dumping,
                            const std::string& expected_pixel_hash);
  // True if the controller was reset successfully.
  bool ResetAfterLayoutTest();

  void RendererUnresponsive();

  WebKitTestResultPrinter* printer() { return printer_.get(); }
  void set_printer(WebKitTestResultPrinter* printer) {
    printer_.reset(printer);
  }

  // Interface for WebKitTestRunnerHost.
  void NotifyDone();
  void WaitUntilDone();
  void NotImplemented(const std::string& object_name,
                      const std::string& method_name);

  bool should_stay_on_page_after_handling_before_unload() const {
    return should_stay_on_page_after_handling_before_unload_;
  }
  void set_should_stay_on_page_after_handling_before_unload(
      bool should_stay_on_page_after_handling_before_unload) {
    should_stay_on_page_after_handling_before_unload_ =
        should_stay_on_page_after_handling_before_unload;
  }
  bool dump_as_text() const { return dump_as_text_; }
  void set_dump_as_text(bool dump_as_text) { dump_as_text_ = dump_as_text; }
  bool dump_child_frames() const { return dump_child_frames_; }
  void set_dump_child_frames(bool dump_child_frames) {
    dump_child_frames_ = dump_child_frames;
  }
  bool is_printing() const { return is_printing_; }
  void set_is_printing(bool is_printing) { is_printing_ = is_printing; }

  // WebContentsObserver implementation.
  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
  virtual void PluginCrashed(const FilePath& plugin_path) OVERRIDE;
  virtual void RenderViewReady() OVERRIDE;
  virtual void RenderViewGone(base::TerminationStatus status) OVERRIDE;
  virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE;

 private:
  static WebKitTestController* instance_;

  void CaptureDump();
  void TimeoutHandler();

  // Message handlers.
  void OnDidFinishLoad();
  void OnImageDump(const std::string& actual_pixel_hash, const SkBitmap& image);
  void OnTextDump(const std::string& dump);
  void OnPrintMessage(const std::string& message);

  scoped_ptr<WebKitTestResultPrinter> printer_;

  FilePath current_working_directory_;

  Shell* main_window_;

  bool enable_pixel_dumping_;
  std::string expected_pixel_hash_;

  bool captured_dump_;

  bool dump_as_text_;
  bool dump_child_frames_;
  bool is_printing_;
  bool should_stay_on_page_after_handling_before_unload_;
  bool wait_until_done_;

  bool did_set_as_main_window_;

  base::CancelableClosure watchdog_;

  DISALLOW_COPY_AND_ASSIGN(WebKitTestController);
};

class WebKitTestRunnerHost : public RenderViewHostObserver {
 public:
  explicit WebKitTestRunnerHost(RenderViewHost* render_view_host);
  virtual ~WebKitTestRunnerHost();

  // RenderViewHostObserver implementation.
  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;

 private:
  // testRunner handlers.
  void OnNotifyDone();
  void OnDumpAsText();
  void OnDumpChildFramesAsText();
  void OnSetPrinting();
  void OnSetShouldStayOnPageAfterHandlingBeforeUnload(bool should_stay_on_page);
  void OnWaitUntilDone();

  void OnNotImplemented(const std::string& object_name,
                        const std::string& method_name);

  DISALLOW_COPY_AND_ASSIGN(WebKitTestRunnerHost);
};

}  // namespace content

#endif  // CONTENT_SHELL_WEBKIT_TEST_RUNNER_HOST_H_