Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / public / test / web_contents_observer_sanity_checker.h
blob2fab3ab2f09b5b28442282c1a6c7221319705d3a
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_PUBLIC_TEST_WEB_CONTENTS_OBSERVER_SANITY_CHECKER_H_
6 #define CONTENT_PUBLIC_TEST_WEB_CONTENTS_OBSERVER_SANITY_CHECKER_H_
8 #include <set>
9 #include <string>
11 #include "base/supports_user_data.h"
12 #include "content/public/browser/web_contents_observer.h"
14 namespace content {
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
23 // observers.
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 {
30 public:
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 DidStartProvisionalLoadForFrame(RenderFrameHost* render_frame_host,
44 const GURL& validated_url,
45 bool is_error_page,
46 bool is_iframe_srcdoc) override;
47 void DidCommitProvisionalLoadForFrame(
48 RenderFrameHost* render_frame_host,
49 const GURL& url,
50 ui::PageTransition transition_type) override;
51 void DidFailProvisionalLoad(RenderFrameHost* render_frame_host,
52 const GURL& validated_url,
53 int error_code,
54 const base::string16& error_description) override;
55 void DidNavigateMainFrame(const LoadCommittedDetails& details,
56 const FrameNavigateParams& params) override;
57 void DidNavigateAnyFrame(RenderFrameHost* render_frame_host,
58 const LoadCommittedDetails& details,
59 const FrameNavigateParams& params) override;
60 void DocumentAvailableInMainFrame() override;
61 void DocumentOnLoadCompletedInMainFrame() override;
62 void DocumentLoadedInFrame(RenderFrameHost* render_frame_host) override;
63 void DidFinishLoad(RenderFrameHost* render_frame_host,
64 const GURL& validated_url) override;
65 void DidFailLoad(RenderFrameHost* render_frame_host,
66 const GURL& validated_url,
67 int error_code,
68 const base::string16& error_description) override;
69 void DidGetRedirectForResourceRequest(
70 RenderFrameHost* render_frame_host,
71 const ResourceRedirectDetails& details) override;
72 void DidOpenRequestedURL(WebContents* new_contents,
73 RenderFrameHost* source_render_frame_host,
74 const GURL& url,
75 const Referrer& referrer,
76 WindowOpenDisposition disposition,
77 ui::PageTransition transition) override;
78 bool OnMessageReceived(const IPC::Message& message,
79 RenderFrameHost* render_frame_host) override;
80 void WebContentsDestroyed() override;
82 private:
83 explicit WebContentsObserverSanityChecker(WebContents* web_contents);
84 ~WebContentsObserverSanityChecker() override;
86 std::string Format(RenderFrameHost* render_frame_host);
87 void AssertRenderFrameExists(RenderFrameHost* render_frame_host);
88 void AssertMainFrameExists();
90 std::set<std::pair<int, int>> current_hosts_;
91 std::set<std::pair<int, int>> live_routes_;
92 std::set<std::pair<int, int>> deleted_routes_;
94 bool web_contents_destroyed_;
96 DISALLOW_COPY_AND_ASSIGN(WebContentsObserverSanityChecker);
99 } // namespace content
101 #endif // CONTENT_PUBLIC_TEST_WEB_CONTENTS_OBSERVER_SANITY_CHECKER_H_