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