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.
5 #ifndef CONTENT_TEST_WEB_CONTENTS_OBSERVER_SANITY_CHECKER_H_
6 #define CONTENT_TEST_WEB_CONTENTS_OBSERVER_SANITY_CHECKER_H_
11 #include "base/supports_user_data.h"
12 #include "content/public/browser/web_contents_observer.h"
16 // If your test framework enables a ContentBrowserSanityChecker, this sanity
17 // check is automatically installed on all WebContentses during your test.
19 // WebContentsObserverSanityChecker is a WebContentsObserver that sanity-checks
20 // the sequence of observer calls, and CHECK()s if they are inconsistent. These
21 // checks are test-only code designed to find bugs in the implementation of the
22 // content layer by validating the contract between WebContents and its
25 // For example, WebContentsObserver::RenderFrameCreated announces the existence
26 // of a new RenderFrameHost, so that method call must occur before the
27 // RenderFrameHost is referenced by some other WebContentsObserver method.
28 class WebContentsObserverSanityChecker
: public WebContentsObserver
,
29 public base::SupportsUserData::Data
{
31 // Enables these checks on |web_contents|. Usually ContentBrowserSanityChecker
32 // should call this for you.
33 static void Enable(WebContents
* web_contents
);
35 // WebContentsObserver implementation.
36 void RenderFrameCreated(RenderFrameHost
* render_frame_host
) override
;
37 void RenderFrameDeleted(RenderFrameHost
* render_frame_host
) override
;
38 void RenderFrameForInterstitialPageCreated(
39 RenderFrameHost
* render_frame_host
) override
;
40 void RenderFrameHostChanged(RenderFrameHost
* old_host
,
41 RenderFrameHost
* new_host
) override
;
42 void FrameDeleted(RenderFrameHost
* render_frame_host
) override
;
43 void DidStartNavigation(NavigationHandle
* navigation_handle
) override
;
44 void DidRedirectNavigation(NavigationHandle
* navigation_handle
) override
;
45 void ReadyToCommitNavigation(NavigationHandle
* navigation_handle
) override
;
46 void DidCommitNavigation(NavigationHandle
* navigation_handle
) override
;
47 void DidFinishNavigation(NavigationHandle
* navigation_handle
) override
;
48 void DidStartProvisionalLoadForFrame(RenderFrameHost
* render_frame_host
,
49 const GURL
& validated_url
,
51 bool is_iframe_srcdoc
) override
;
52 void DidCommitProvisionalLoadForFrame(
53 RenderFrameHost
* render_frame_host
,
55 ui::PageTransition transition_type
) override
;
56 void DidFailProvisionalLoad(RenderFrameHost
* render_frame_host
,
57 const GURL
& validated_url
,
59 const base::string16
& error_description
,
60 bool was_ignored_by_handler
) override
;
61 void DidNavigateMainFrame(const LoadCommittedDetails
& details
,
62 const FrameNavigateParams
& params
) override
;
63 void DidNavigateAnyFrame(RenderFrameHost
* render_frame_host
,
64 const LoadCommittedDetails
& details
,
65 const FrameNavigateParams
& params
) override
;
66 void DocumentAvailableInMainFrame() override
;
67 void DocumentOnLoadCompletedInMainFrame() override
;
68 void DocumentLoadedInFrame(RenderFrameHost
* render_frame_host
) override
;
69 void DidFinishLoad(RenderFrameHost
* render_frame_host
,
70 const GURL
& validated_url
) override
;
71 void DidFailLoad(RenderFrameHost
* render_frame_host
,
72 const GURL
& validated_url
,
74 const base::string16
& error_description
,
75 bool was_ignored_by_handler
) override
;
76 void DidGetRedirectForResourceRequest(
77 RenderFrameHost
* render_frame_host
,
78 const ResourceRedirectDetails
& details
) override
;
79 void DidOpenRequestedURL(WebContents
* new_contents
,
80 RenderFrameHost
* source_render_frame_host
,
82 const Referrer
& referrer
,
83 WindowOpenDisposition disposition
,
84 ui::PageTransition transition
) override
;
85 bool OnMessageReceived(const IPC::Message
& message
,
86 RenderFrameHost
* render_frame_host
) override
;
87 void WebContentsDestroyed() override
;
90 explicit WebContentsObserverSanityChecker(WebContents
* web_contents
);
91 ~WebContentsObserverSanityChecker() override
;
93 std::string
Format(RenderFrameHost
* render_frame_host
);
94 void AssertRenderFrameExists(RenderFrameHost
* render_frame_host
);
95 void AssertMainFrameExists();
97 bool NavigationIsOngoing(NavigationHandle
* navigation_handle
);
98 bool NavigationIsOngoingAndCommitted(NavigationHandle
* navigation_handle
);
100 std::set
<std::pair
<int, int>> current_hosts_
;
101 std::set
<std::pair
<int, int>> live_routes_
;
102 std::set
<std::pair
<int, int>> deleted_routes_
;
104 std::set
<NavigationHandle
*> ongoing_navigations_
;
105 std::set
<NavigationHandle
*> ongoing_committed_navigations_
;
107 bool web_contents_destroyed_
;
109 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverSanityChecker
);
112 } // namespace content
114 #endif // CONTENT_TEST_WEB_CONTENTS_OBSERVER_SANITY_CHECKER_H_