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 DidCommitNavigation(NavigationHandle
* navigation_handle
) override
;
46 void DidFinishNavigation(NavigationHandle
* navigation_handle
) override
;
47 void DidStartProvisionalLoadForFrame(RenderFrameHost
* render_frame_host
,
48 const GURL
& validated_url
,
50 bool is_iframe_srcdoc
) override
;
51 void DidCommitProvisionalLoadForFrame(
52 RenderFrameHost
* render_frame_host
,
54 ui::PageTransition transition_type
) override
;
55 void DidFailProvisionalLoad(RenderFrameHost
* render_frame_host
,
56 const GURL
& validated_url
,
58 const base::string16
& error_description
,
59 bool was_ignored_by_handler
) override
;
60 void DidNavigateMainFrame(const LoadCommittedDetails
& details
,
61 const FrameNavigateParams
& params
) override
;
62 void DidNavigateAnyFrame(RenderFrameHost
* render_frame_host
,
63 const LoadCommittedDetails
& details
,
64 const FrameNavigateParams
& params
) override
;
65 void DocumentAvailableInMainFrame() override
;
66 void DocumentOnLoadCompletedInMainFrame() override
;
67 void DocumentLoadedInFrame(RenderFrameHost
* render_frame_host
) override
;
68 void DidFinishLoad(RenderFrameHost
* render_frame_host
,
69 const GURL
& validated_url
) override
;
70 void DidFailLoad(RenderFrameHost
* render_frame_host
,
71 const GURL
& validated_url
,
73 const base::string16
& error_description
,
74 bool was_ignored_by_handler
) override
;
75 void DidGetRedirectForResourceRequest(
76 RenderFrameHost
* render_frame_host
,
77 const ResourceRedirectDetails
& details
) override
;
78 void DidOpenRequestedURL(WebContents
* new_contents
,
79 RenderFrameHost
* source_render_frame_host
,
81 const Referrer
& referrer
,
82 WindowOpenDisposition disposition
,
83 ui::PageTransition transition
) override
;
84 bool OnMessageReceived(const IPC::Message
& message
,
85 RenderFrameHost
* render_frame_host
) override
;
86 void WebContentsDestroyed() override
;
89 explicit WebContentsObserverSanityChecker(WebContents
* web_contents
);
90 ~WebContentsObserverSanityChecker() override
;
92 std::string
Format(RenderFrameHost
* render_frame_host
);
93 void AssertRenderFrameExists(RenderFrameHost
* render_frame_host
);
94 void AssertMainFrameExists();
96 bool NavigationIsOngoing(NavigationHandle
* navigation_handle
);
97 bool NavigationIsOngoingAndCommitted(NavigationHandle
* navigation_handle
);
99 std::set
<std::pair
<int, int>> current_hosts_
;
100 std::set
<std::pair
<int, int>> live_routes_
;
101 std::set
<std::pair
<int, int>> deleted_routes_
;
103 std::set
<NavigationHandle
*> ongoing_navigations_
;
104 std::set
<NavigationHandle
*> ongoing_committed_navigations_
;
106 bool web_contents_destroyed_
;
108 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverSanityChecker
);
111 } // namespace content
113 #endif // CONTENT_TEST_WEB_CONTENTS_OBSERVER_SANITY_CHECKER_H_