Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1990)

Unified Diff: content/public/test/navigation_simulator.h

Issue 2682313002: Introduce NavigationSimulator to use in unit tests (Closed)
Patch Set: Addressed comments Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/public/common/referrer.cc ('k') | content/public/test/navigation_simulator.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/public/test/navigation_simulator.h
diff --git a/content/public/test/navigation_simulator.h b/content/public/test/navigation_simulator.h
new file mode 100644
index 0000000000000000000000000000000000000000..8c37ab7a34c3b1f147a1bec853128ee82a81a79c
--- /dev/null
+++ b/content/public/test/navigation_simulator.h
@@ -0,0 +1,170 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_
+#define CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_
+
+#include <memory>
+
+#include "content/public/browser/web_contents_observer.h"
+#include "content/public/common/referrer.h"
+#include "content/public/test/navigation_simulator.h"
+#include "ui/base/page_transition_types.h"
+
+class GURL;
+
+namespace content {
+
+class NavigationHandleImpl;
+class RenderFrameHost;
+class TestRenderFrameHost;
+struct Referrer;
+
+// An interface for simulating a navigation in unit tests. Currently this only
+// supports renderer-initiated navigations.
+// Note: this should not be used in browser tests.
+// TODO(clamy): support browser-initiated navigations.
+class NavigationSimulator : public WebContentsObserver {
+ public:
+ // Simulates a renderer-initiated navigation to |url| started in
+ // |render_frame_host| from start to commit.
+ static void NavigateAndCommitFromDocument(const GURL& original_url,
+ RenderFrameHost* render_frame_host);
+
+ // Simulates a failed renderer-initiated navigation to |url| started in
+ // |render_frame_host| from start to commit.
+ static void NavigateAndFailFromDocument(const GURL& original_url,
+ int net_error_code,
+ RenderFrameHost* render_frame_host);
+
+ // ---------------------------------------------------------------------------
+
+ // All the following methods should be used when more precise control over the
+ // navigation is needed.
+
+ // Creates a NavigationSimulator that will be used to simulate a
+ // renderer-initiated navigation to |original_url| started by
+ // |render_frame_host|.
+ static std::unique_ptr<NavigationSimulator> CreateRendererInitiated(
+ const GURL& original_url,
+ RenderFrameHost* render_frame_host);
+
+ NavigationSimulator(const GURL& original_url,
+ TestRenderFrameHost* render_frame_host);
+ ~NavigationSimulator() override;
+
+ // --------------------------------------------------------------------------
+
+ // The following functions should be used to simulate events happening during
+ // a navigation.
+ //
+ // Example of usage for a successful renderer-initiated navigation:
+ // unique_ptr<NavigationSimulator> simulator =
+ // NavigationSimulator::CreateRendererInitiated(
+ // original_url, render_frame_host);
+ // simulator->SetTransition(ui::PAGE_TRANSITION_LINK);
+ // simulator->Start();
+ // for (GURL redirect_url : redirects)
+ // simulator->Redirect(redirect_url);
+ // simulator->Commit();
+ //
+ // Example of usage for a failed renderer-initiated navigation:
+ // unique_ptr<NavigationSimulator> simulator =
+ // NavigationSimulator::CreateRendererInitiated(
+ // original_url, render_frame_host);
+ // simulator->SetTransition(ui::PAGE_TRANSITION_LINK);
+ // simulator->Start();
+ // for (GURL redirect_url : redirects)
+ // simulator->Redirect(redirect_url);
+ // simulator->Fail(net::ERR_TIMED_OUT);
+ // simulator->CommitErrorPage();
+ //
+ // Example of usage for a same-page renderer-initiated navigation:
+ // unique_ptr<NavigationSimulator> simulator =
+ // NavigationSimulator::CreateRendererInitiated(
+ // original_url, render_frame_host);
+ // simulator->CommitSamePage();
+
+ // Simulates the start of the navigation.
+ virtual void Start();
+
+ // Simulates a redirect to |new_url| for the navigation.
+ virtual void Redirect(const GURL& new_url);
+
+ // Simulates the commit of the navigation in the RenderFrameHost.
+ virtual void Commit();
+
+ // Simulates the navigation failing with the error code |error_code|.
+ virtual void Fail(int error_code);
+
+ // Simulates the commit of an error page following a navigation failure.
+ virtual void CommitErrorPage();
+
+ // Simulates the commit of a same-page navigation, ie fragment navigations or
+ // pushState/popState navigations.
+ virtual void CommitSamePage();
+
+ // --------------------------------------------------------------------------
+
+ // The following functions are used to specify the parameters of the
+ // navigation. Changes should be made before calling |Start|, unless they are
+ // meant to apply to a redirect. In that case, they should be made before
+ // calling |Redirect|.
+
+ // The following parameters are constant during the navigation and may only be
+ // specified before calling |Start|.
+ virtual void SetTransition(ui::PageTransition transition);
+
+ // The following parameters can change during redirects. They should be
+ // specified before calling |Start| if they need to apply to the navigation to
+ // the original url. Otherwise, they should be specified before calling
+ // |Redirect|.
+ virtual void SetReferrer(const Referrer& referrer);
+
+ private:
+ // WebContentsObserver:
+ void DidStartNavigation(NavigationHandle* navigation_handle) override;
+ void DidRedirectNavigation(NavigationHandle* navigation_handle) override;
+ void ReadyToCommitNavigation(NavigationHandle* navigation_handle) override;
+ void DidFinishNavigation(NavigationHandle* navigation_handle) override;
+
+ void OnWillStartRequest();
+ void OnWillRedirectRequest();
+ void OnWillProcessResponse();
+
+ enum State {
+ INITIALIZATION,
+ STARTED,
+ FAILED,
+ FINISHED,
+ };
+
+ State state_ = INITIALIZATION;
+
+ // The renderer associated with this navigation.
+ TestRenderFrameHost* render_frame_host_;
+
+ // The NavigationHandle associated with this navigation.
+ NavigationHandleImpl* handle_;
+
+ GURL navigation_url_;
+ Referrer referrer_;
+ ui::PageTransition transition_ = ui::PAGE_TRANSITION_LINK;
+
+ // These are used to sanity check the content/public/ API calls emitted as
+ // part of the navigation.
+ int num_did_start_navigation_called_ = 0;
+ int num_will_start_request_called_ = 0;
+ int num_will_redirect_request_called_ = 0;
+ int num_did_redirect_navigation_called_ = 0;
+ int num_will_process_response_called_ = 0;
+ int num_ready_to_commit_called_ = 0;
+ int num_did_finish_navigation_called_ = 0;
+
+ base::WeakPtrFactory<NavigationSimulator> weak_factory_;
+};
+
+} // namespace content
+
+#endif // CONTENT_PUBLIC_TEST_NAVIGATION_SIMULATOR_H_
« no previous file with comments | « content/public/common/referrer.cc ('k') | content/public/test/navigation_simulator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698