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