1// Copyright 2014 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.
4
5#ifndef CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_FRAME_TEST_PROXY_H_
6#define CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_FRAME_TEST_PROXY_H_
7
8#include "base/basictypes.h"
9#include "content/shell/renderer/test_runner/mock_screen_orientation_client.h"
10#include "content/shell/renderer/test_runner/test_interfaces.h"
11#include "content/shell/renderer/test_runner/test_runner.h"
12#include "content/shell/renderer/test_runner/web_test_delegate.h"
13#include "content/shell/renderer/test_runner/web_test_proxy.h"
14#include "content/test/test_media_stream_renderer_factory.h"
15#include "third_party/WebKit/public/platform/WebString.h"
16
17namespace content {
18
19// Templetized wrapper around RenderFrameImpl objects, which implement
20// the WebFrameClient interface.
21template <class Base, typename P, typename R>
22class WebFrameTestProxy : public Base {
23 public:
24  WebFrameTestProxy(P p, R r) : Base(p, r), base_proxy_(NULL) {}
25
26  virtual ~WebFrameTestProxy() {}
27
28  void set_base_proxy(WebTestProxyBase* proxy) { base_proxy_ = proxy; }
29
30  // WebFrameClient implementation.
31  virtual blink::WebPlugin* createPlugin(blink::WebLocalFrame* frame,
32                                         const blink::WebPluginParams& params) {
33    blink::WebPlugin* plugin = base_proxy_->CreatePlugin(frame, params);
34    if (plugin)
35      return plugin;
36    return Base::createPlugin(frame, params);
37  }
38
39  virtual blink::WebScreenOrientationClient* webScreenOrientationClient() {
40    return base_proxy_->GetScreenOrientationClientMock();
41  }
42
43  virtual void didAddMessageToConsole(const blink::WebConsoleMessage& message,
44                                      const blink::WebString& source_name,
45                                      unsigned source_line,
46                                      const blink::WebString& stack_trace) {
47    base_proxy_->DidAddMessageToConsole(message, source_name, source_line);
48    Base::didAddMessageToConsole(
49        message, source_name, source_line, stack_trace);
50  }
51
52  virtual bool canCreatePluginWithoutRenderer(
53      const blink::WebString& mime_type) {
54    using blink::WebString;
55
56    const CR_DEFINE_STATIC_LOCAL(
57        WebString, suffix, ("-can-create-without-renderer"));
58    return mime_type.utf8().find(suffix.utf8()) != std::string::npos;
59  }
60
61  virtual void loadURLExternally(blink::WebLocalFrame* frame,
62                                 const blink::WebURLRequest& request,
63                                 blink::WebNavigationPolicy policy,
64                                 const blink::WebString& suggested_name) {
65    base_proxy_->LoadURLExternally(frame, request, policy, suggested_name);
66    Base::loadURLExternally(frame, request, policy, suggested_name);
67  }
68
69  virtual void didStartProvisionalLoad(blink::WebLocalFrame* frame,
70                                       bool isTransitionNavigation) {
71    base_proxy_->DidStartProvisionalLoad(frame);
72    Base::didStartProvisionalLoad(frame, isTransitionNavigation);
73  }
74
75  virtual void didReceiveServerRedirectForProvisionalLoad(
76      blink::WebLocalFrame* frame) {
77    base_proxy_->DidReceiveServerRedirectForProvisionalLoad(frame);
78    Base::didReceiveServerRedirectForProvisionalLoad(frame);
79  }
80
81  virtual void didFailProvisionalLoad(blink::WebLocalFrame* frame,
82                                      const blink::WebURLError& error) {
83    // If the test finished, don't notify the embedder of the failed load,
84    // as we already destroyed the document loader.
85    if (base_proxy_->DidFailProvisionalLoad(frame, error))
86      return;
87    Base::didFailProvisionalLoad(frame, error);
88  }
89
90  virtual void didCommitProvisionalLoad(
91      blink::WebLocalFrame* frame,
92      const blink::WebHistoryItem& item,
93      blink::WebHistoryCommitType commit_type) {
94    base_proxy_->DidCommitProvisionalLoad(frame, item, commit_type);
95    Base::didCommitProvisionalLoad(frame, item, commit_type);
96  }
97
98  virtual void didReceiveTitle(blink::WebLocalFrame* frame,
99                               const blink::WebString& title,
100                               blink::WebTextDirection direction) {
101    base_proxy_->DidReceiveTitle(frame, title, direction);
102    Base::didReceiveTitle(frame, title, direction);
103  }
104
105  virtual void didChangeIcon(blink::WebLocalFrame* frame,
106                             blink::WebIconURL::Type icon_type) {
107    base_proxy_->DidChangeIcon(frame, icon_type);
108    Base::didChangeIcon(frame, icon_type);
109  }
110
111  virtual void didFinishDocumentLoad(blink::WebLocalFrame* frame) {
112    base_proxy_->DidFinishDocumentLoad(frame);
113    Base::didFinishDocumentLoad(frame);
114  }
115
116  virtual void didHandleOnloadEvents(blink::WebLocalFrame* frame) {
117    base_proxy_->DidHandleOnloadEvents(frame);
118    Base::didHandleOnloadEvents(frame);
119  }
120
121  virtual void didFailLoad(blink::WebLocalFrame* frame,
122                           const blink::WebURLError& error) {
123    base_proxy_->DidFailLoad(frame, error);
124    Base::didFailLoad(frame, error);
125  }
126
127  virtual void didFinishLoad(blink::WebLocalFrame* frame) {
128    Base::didFinishLoad(frame);
129    base_proxy_->DidFinishLoad(frame);
130  }
131
132  virtual blink::WebNotificationPresenter* notificationPresenter() {
133    return base_proxy_->GetNotificationPresenter();
134  }
135
136  virtual void didChangeSelection(bool is_selection_empty) {
137    base_proxy_->DidChangeSelection(is_selection_empty);
138    Base::didChangeSelection(is_selection_empty);
139  }
140
141  virtual blink::WebColorChooser* createColorChooser(
142      blink::WebColorChooserClient* client,
143      const blink::WebColor& initial_color,
144      const blink::WebVector<blink::WebColorSuggestion>& suggestions) {
145    return base_proxy_->CreateColorChooser(client, initial_color, suggestions);
146  }
147
148  virtual void runModalAlertDialog(const blink::WebString& message) {
149    base_proxy_->delegate_->PrintMessage(std::string("ALERT: ") +
150                                         message.utf8().data() + "\n");
151  }
152
153  virtual bool runModalConfirmDialog(const blink::WebString& message) {
154    base_proxy_->delegate_->PrintMessage(std::string("CONFIRM: ") +
155                                         message.utf8().data() + "\n");
156    return true;
157  }
158
159  virtual bool runModalPromptDialog(const blink::WebString& message,
160                                    const blink::WebString& default_value,
161                                    blink::WebString*) {
162    base_proxy_->delegate_->PrintMessage(
163        std::string("PROMPT: ") + message.utf8().data() + ", default text: " +
164        default_value.utf8().data() + "\n");
165    return true;
166  }
167
168  virtual bool runModalBeforeUnloadDialog(bool is_reload,
169                                          const blink::WebString& message) {
170    base_proxy_->delegate_->PrintMessage(std::string("CONFIRM NAVIGATION: ") +
171                                         message.utf8().data() + "\n");
172    return !base_proxy_->test_interfaces_->GetTestRunner()
173                ->shouldStayOnPageAfterHandlingBeforeUnload();
174  }
175
176  virtual void showContextMenu(
177      const blink::WebContextMenuData& context_menu_data) {
178    base_proxy_->ShowContextMenu(Base::GetWebFrame(),
179                                 context_menu_data);
180    Base::showContextMenu(context_menu_data);
181  }
182
183  virtual void didDetectXSS(blink::WebLocalFrame* frame,
184                            const blink::WebURL& insecure_url,
185                            bool did_block_entire_page) {
186    // This is not implemented in RenderFrameImpl, so need to explicitly call
187    // into the base proxy.
188    base_proxy_->DidDetectXSS(frame, insecure_url, did_block_entire_page);
189    Base::didDetectXSS(frame, insecure_url, did_block_entire_page);
190  }
191
192  virtual void didDispatchPingLoader(blink::WebLocalFrame* frame,
193                                     const blink::WebURL& url) {
194    // This is not implemented in RenderFrameImpl, so need to explicitly call
195    // into the base proxy.
196    base_proxy_->DidDispatchPingLoader(frame, url);
197    Base::didDispatchPingLoader(frame, url);
198  }
199
200  virtual void willRequestResource(blink::WebLocalFrame* frame,
201                                   const blink::WebCachedURLRequest& request) {
202    // This is not implemented in RenderFrameImpl, so need to explicitly call
203    // into the base proxy.
204    base_proxy_->WillRequestResource(frame, request);
205    Base::willRequestResource(frame, request);
206  }
207
208  virtual void didCreateDataSource(blink::WebLocalFrame* frame,
209                                   blink::WebDataSource* ds) {
210    Base::didCreateDataSource(frame, ds);
211  }
212
213  virtual void willSendRequest(blink::WebLocalFrame* frame,
214                               unsigned identifier,
215                               blink::WebURLRequest& request,
216                               const blink::WebURLResponse& redirect_response) {
217    Base::willSendRequest(frame, identifier, request, redirect_response);
218    base_proxy_->WillSendRequest(frame, identifier, request, redirect_response);
219  }
220
221  virtual void didReceiveResponse(blink::WebLocalFrame* frame,
222                                  unsigned identifier,
223                                  const blink::WebURLResponse& response) {
224    base_proxy_->DidReceiveResponse(frame, identifier, response);
225    Base::didReceiveResponse(frame, identifier, response);
226  }
227
228  virtual void didChangeResourcePriority(
229      blink::WebLocalFrame* frame,
230      unsigned identifier,
231      const blink::WebURLRequest::Priority& priority,
232      int intra_priority_value) {
233    // This is not implemented in RenderFrameImpl, so need to explicitly call
234    // into the base proxy.
235    base_proxy_->DidChangeResourcePriority(
236        frame, identifier, priority, intra_priority_value);
237    Base::didChangeResourcePriority(
238        frame, identifier, priority, intra_priority_value);
239  }
240
241  virtual void didFinishResourceLoad(blink::WebLocalFrame* frame,
242                                     unsigned identifier) {
243    base_proxy_->DidFinishResourceLoad(frame, identifier);
244    Base::didFinishResourceLoad(frame, identifier);
245  }
246
247  virtual blink::WebNavigationPolicy decidePolicyForNavigation(
248      const blink::WebFrameClient::NavigationPolicyInfo& info) {
249    blink::WebNavigationPolicy policy = base_proxy_->DecidePolicyForNavigation(
250        info);
251    if (policy == blink::WebNavigationPolicyIgnore)
252      return policy;
253
254    return Base::decidePolicyForNavigation(info);
255  }
256
257  virtual void willStartUsingPeerConnectionHandler(
258      blink::WebLocalFrame* frame,
259      blink::WebRTCPeerConnectionHandler* handler) {
260    // RenderFrameImpl::willStartUsingPeerConnectionHandler can not be mocked.
261    // See http://crbug/363285.
262  }
263
264  virtual blink::WebUserMediaClient* userMediaClient() {
265    return base_proxy_->GetUserMediaClient();
266  }
267
268  virtual blink::WebMIDIClient* webMIDIClient() {
269    return base_proxy_->GetWebMIDIClient();
270  }
271
272  virtual bool willCheckAndDispatchMessageEvent(
273      blink::WebLocalFrame* source_frame,
274      blink::WebFrame* target_frame,
275      blink::WebSecurityOrigin target,
276      blink::WebDOMMessageEvent event) {
277    if (base_proxy_->WillCheckAndDispatchMessageEvent(
278            source_frame, target_frame, target, event))
279      return true;
280    return Base::willCheckAndDispatchMessageEvent(
281        source_frame, target_frame, target, event);
282  }
283
284  virtual void didStopLoading() {
285    base_proxy_->DidStopLoading();
286    Base::didStopLoading();
287  }
288
289 private:
290#if defined(ENABLE_WEBRTC)
291  virtual scoped_ptr<MediaStreamRendererFactory> CreateRendererFactory()
292      OVERRIDE {
293    return scoped_ptr<MediaStreamRendererFactory>(
294        new TestMediaStreamRendererFactory());
295  }
296#endif
297
298  WebTestProxyBase* base_proxy_;
299
300  DISALLOW_COPY_AND_ASSIGN(WebFrameTestProxy);
301};
302
303}  // namespace content
304
305#endif  // CONTENT_SHELL_RENDERER_TEST_RUNNER_WEB_FRAME_TEST_PROXY_H_
306