web_contents_tester.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
4
5#ifndef CONTENT_PUBLIC_TEST_WEB_CONTENTS_TESTER_H_
6#define CONTENT_PUBLIC_TEST_WEB_CONTENTS_TESTER_H_
7
8#include "content/public/common/page_transition_types.h"
9
10class GURL;
11
12namespace webkit_glue {
13struct WebPreferences;
14}
15
16namespace content {
17
18class BrowserContext;
19struct Referrer;
20class RenderViewHost;
21class SiteInstance;
22class WebContents;
23
24// This interface allows embedders of content/ to write tests that depend on a
25// test version of WebContents.  This interface can be retrieved from any
26// WebContents that was retrieved via a call to
27// RenderViewHostTestHarness::GetWebContents() (directly or indirectly) or
28// constructed explicitly via one of the WebContentsTester::Create...  methods.
29//
30// Tests within content/ can directly static_cast WebContents objects retrieved
31// or created as described above to TestWebContents.
32//
33// Design note: We considered two alternatives to this separate test interface
34// approach:
35//
36// a) Define a TestWebContents interface that inherits from WebContents, and
37// have the concrete TestWebContents inherit from it as well as from
38// WebContentsImpl.  This approach was discarded as it introduces a diamond
39// inheritance pattern, which means we wouldn't be e.g. able to downcast from
40// WebContents to WebContentsImpl using static_cast.
41//
42// b) Define a TestWebContents interface that inherits from WebContents, and
43// have the concrete TestWebContents implement it, using composition of a
44// WebContentsImpl to implement most methods.  This approach was discarded as
45// there is a fundamental assumption in content/ that a WebContents* can be
46// downcast to a WebContentsImpl*, and this wouldn't be true for TestWebContents
47// objects.
48class WebContentsTester {
49 public:
50  // Retrieves a WebContentsTester to drive tests of the specified WebContents.
51  // As noted above you need to be sure the 'contents' object supports testing,
52  // i.e. is either created using one of the Create... functions below, or is
53  // retrieved via RenderViewHostTestHarness::GetWebContents().
54  static WebContentsTester* For(WebContents* contents);
55
56  // Creates a WebContents enabled for testing.
57  static WebContents* CreateTestWebContents(
58      BrowserContext* browser_context,
59      SiteInstance* instance);
60
61  // Deprecated.  Creates a WebContents enabled for testing, that
62  // counts the number of times SetFocusToLocationBar is called.
63  static WebContents*
64      CreateTestWebContentsCountSetFocusToLocationBar(
65          BrowserContext* browser_context,
66          SiteInstance* instance);
67
68  // Deprecated.  Creates a WebContents enabled for testing, that
69  // counts the number of times Focus is called.
70  static WebContents* CreateTestWebContentsCountFocus(
71      BrowserContext* browser_context,
72      SiteInstance* instance);
73
74  // Simulates the appropriate RenderView (pending if any, current otherwise)
75  // sending a navigate notification for the NavigationController pending entry.
76  virtual void CommitPendingNavigation() = 0;
77
78  // Only implementations retrieved via the deprecated
79  // CreateTestWebContentsFor... methods above will implement this
80  // method.  It retrieves the number of times the focus-related calls
81  // in question have been made.
82  virtual int GetNumberOfFocusCalls() = 0;
83
84  virtual RenderViewHost* GetPendingRenderViewHost() const = 0;
85
86  // Creates a pending navigation to the given URL with the default parameters
87  // and then commits the load with a page ID one larger than any seen. This
88  // emulates what happens on a new navigation.
89  virtual void NavigateAndCommit(const GURL& url) = 0;
90
91  // Sets the loading state to the given value.
92  virtual void TestSetIsLoading(bool value) = 0;
93
94  // Simulates the current RVH notifying that it has unloaded so that the
95  // pending RVH navigation can proceed.
96  // Does nothing if no cross-navigation is pending.
97  virtual void ProceedWithCrossSiteNavigation() = 0;
98
99  virtual void TestDidNavigate(RenderViewHost* render_view_host,
100                               int page_id,
101                               const GURL& url,
102                               PageTransition transition) = 0;
103
104  virtual void TestDidNavigateWithReferrer(
105      RenderViewHost* render_view_host,
106      int page_id,
107      const GURL& url,
108      const Referrer& referrer,
109      PageTransition transition) = 0;
110
111  // Promote GetWebkitPrefs to public.
112  virtual webkit_glue::WebPreferences TestGetWebkitPrefs() = 0;
113};
114
115}  // namespace content
116
117#endif  // CONTENT_PUBLIC_TEST_WEB_CONTENTS_TESTER_H_
118