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

Unified Diff: chrome/browser/prerender/prerender_test_utils.h

Issue 2309443002: NoState Prefetch: Refactor prerender_browsertests.cc (Closed)
Patch Set: Remove unnecessary diff Created 4 years, 3 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
Index: chrome/browser/prerender/prerender_test_utils.h
diff --git a/chrome/browser/prerender/prerender_test_utils.h b/chrome/browser/prerender/prerender_test_utils.h
new file mode 100644
index 0000000000000000000000000000000000000000..98bacfa9edec9918af317b92292eb01d723ffa3f
--- /dev/null
+++ b/chrome/browser/prerender/prerender_test_utils.h
@@ -0,0 +1,326 @@
+// Copyright (c) 2016 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 CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_
+#define CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_
+
+#include "base/memory/weak_ptr.h"
+#include "base/run_loop.h"
+#include "chrome/browser/external_protocol/external_protocol_handler.h"
+#include "chrome/browser/prerender/prerender_contents.h"
+#include "chrome/browser/safe_browsing/test_safe_browsing_service.h"
+#include "chrome/test/base/in_process_browser_test.h"
+#include "components/safe_browsing_db/test_database_manager.h"
+#include "content/public/browser/browser_thread.h"
+#include "net/test/url_request/url_request_mock_http_job.h"
+#include "net/url_request/url_request_filter.h"
+#include "net/url_request/url_request_interceptor.h"
+#include "url/gurl.h"
+
+namespace base {
+class FilePath;
+} // namespace base
+
+namespace net {
+class URLRequest;
+class NetworkDelegate;
+} // namespace net
+
+namespace prerender {
+
+namespace test_utils {
+
+// Dummy counter class to live on the UI thread for counting requests.
+class RequestCounter : public base::SupportsWeakPtr<RequestCounter> {
+ public:
+ RequestCounter();
+
+ ~RequestCounter();
+
+ int count() const { return count_; }
+
+ void RequestStarted();
+ void WaitForCount(int expected_count);
+
+ private:
+ int count_;
+ int expected_count_;
+ std::unique_ptr<base::RunLoop> loop_;
+};
+
+// A SafeBrowsingDatabaseManager implementation that returns a fixed result for
+// a given URL.
+class FakeSafeBrowsingDatabaseManager
+ : public safe_browsing::TestSafeBrowsingDatabaseManager {
+ public:
+ FakeSafeBrowsingDatabaseManager();
+
+ // Called on the IO thread to check if the given url is safe or not. If we
+ // can synchronously determine that the url is safe, CheckUrl returns true.
+ // Otherwise it returns false, and "client" is called asynchronously with the
+ // result when it is ready.
+ // Returns true, indicating a SAFE result, unless the URL is the fixed URL
+ // specified by the user, and the user-specified result is not SAFE
+ // (in which that result will be communicated back via a call into the
+ // client, and false will be returned).
+ // Overrides SafeBrowsingDatabaseManager::CheckBrowseUrl.
+ bool CheckBrowseUrl(const GURL& gurl, Client* client) override;
+
+ void SetThreatTypeForUrl(const GURL& url,
+ safe_browsing::SBThreatType threat_type) {
+ bad_urls_[url.spec()] = threat_type;
+ }
+
+ // These are called when checking URLs, so we implement them.
+ bool IsSupported() const override;
+ bool ChecksAreAlwaysAsync() const override;
+ bool CanCheckResourceType(
+ content::ResourceType /* resource_type */) const override;
+
+ bool CheckExtensionIDs(const std::set<std::string>& extension_ids,
+ Client* client) override;
+
+ private:
+ ~FakeSafeBrowsingDatabaseManager() override;
+
+ void OnCheckBrowseURLDone(const GURL& gurl, Client* client);
+
+ std::unordered_map<std::string, safe_browsing::SBThreatType> bad_urls_;
+ DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager);
+};
+
+// PrerenderContents that stops the UI message loop on DidStopLoading().
+class TestPrerenderContents : public PrerenderContents {
+ public:
+ TestPrerenderContents(PrerenderManager* prerender_manager,
+ Profile* profile,
+ const GURL& url,
+ const content::Referrer& referrer,
+ Origin origin,
+ FinalStatus expected_final_status);
+
+ ~TestPrerenderContents() override;
+
+ void RenderProcessGone(base::TerminationStatus status) override;
+ bool CheckURL(const GURL& url) override;
+
+ // For tests that open the prerender in a new background tab, the RenderView
+ // will not have been made visible when the PrerenderContents is destroyed
+ // even though it is used.
+ void set_should_be_shown(bool value) { should_be_shown_ = value; }
+
+ // For tests which do not know whether the prerender will be used.
+ void set_skip_final_checks(bool value) { skip_final_checks_ = value; }
+
+ FinalStatus expected_final_status() const { return expected_final_status_; }
+
+ private:
+ void OnRenderViewHostCreated(
+ content::RenderViewHost* new_render_view_host) override;
+ void Observe(int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) override;
+
+ FinalStatus expected_final_status_;
+
+ // The RenderViewHost created for the prerender, if any.
+ content::RenderViewHost* new_render_view_host_;
+ // Set to true when the prerendering RenderWidget is hidden.
+ bool was_hidden_;
+ // Set to true when the prerendering RenderWidget is shown, after having been
+ // hidden.
+ bool was_shown_;
+ // Expected final value of was_shown_. Defaults to true for
+ // FINAL_STATUS_USED, and false otherwise.
+ bool should_be_shown_;
+ // If true, |expected_final_status_| and other shutdown checks are skipped.
+ bool skip_final_checks_;
+};
+
+// A handle to a TestPrerenderContents whose lifetime is under the caller's
+// control. A PrerenderContents may be destroyed at any point. This allows
+// tracking the final status, etc.
+class TestPrerender : public PrerenderContents::Observer,
+ public base::SupportsWeakPtr<TestPrerender> {
+ public:
+ TestPrerender();
+ ~TestPrerender() override;
+
+ TestPrerenderContents* contents() const { return contents_; }
+ int number_of_loads() const { return number_of_loads_; }
+
+ void WaitForCreate() { create_loop_.Run(); }
+ void WaitForStart() { start_loop_.Run(); }
+ void WaitForStop() { stop_loop_.Run(); }
+
+ // Waits for |number_of_loads()| to be at least |expected_number_of_loads| OR
+ // for the prerender to stop running (just to avoid a timeout if the prerender
+ // dies). Note: this does not assert equality on the number of loads; the
+ // caller must do it instead.
+ void WaitForLoads(int expected_number_of_loads);
+
+ void OnPrerenderCreated(TestPrerenderContents* contents);
+
+ // PrerenderContents::Observer implementation:
+ void OnPrerenderStart(PrerenderContents* contents) override;
+
+ void OnPrerenderStopLoading(PrerenderContents* contents) override;
+
+ void OnPrerenderStop(PrerenderContents* contents) override;
+
+ private:
+ TestPrerenderContents* contents_;
+ int number_of_loads_;
+
+ int expected_number_of_loads_;
+ std::unique_ptr<base::RunLoop> load_waiter_;
+
+ base::RunLoop create_loop_;
+ base::RunLoop start_loop_;
+ base::RunLoop stop_loop_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestPrerender);
+};
+
+// PrerenderManager that uses TestPrerenderContents.
+class TestPrerenderContentsFactory : public PrerenderContents::Factory {
+ public:
+ TestPrerenderContentsFactory();
+
+ ~TestPrerenderContentsFactory() override;
+
+ std::unique_ptr<TestPrerender> ExpectPrerenderContents(
+ FinalStatus final_status);
+
+ PrerenderContents* CreatePrerenderContents(
+ PrerenderManager* prerender_manager,
+ Profile* profile,
+ const GURL& url,
+ const content::Referrer& referrer,
+ Origin origin) override;
+
+ private:
+ struct ExpectedContents {
+ ExpectedContents();
+ ExpectedContents(const ExpectedContents& other);
+ ExpectedContents(FinalStatus final_status,
+ const base::WeakPtr<TestPrerender>& handle);
+ ~ExpectedContents();
+
+ FinalStatus final_status;
+ base::WeakPtr<TestPrerender> handle;
+ };
+
+ std::deque<ExpectedContents> expected_contents_queue_;
+};
+
+class PrerenderInProcessBrowserTest : virtual public InProcessBrowserTest {
+ public:
+ PrerenderInProcessBrowserTest();
+
+ ~PrerenderInProcessBrowserTest() override;
+
+ void SetUpCommandLine(base::CommandLine* command_line) override;
+ void SetUpInProcessBrowserTestFixture() override;
+ void TearDownInProcessBrowserTestFixture() override;
+ void SetUpOnMainThread() override;
+ content::SessionStorageNamespace* GetSessionStorageNamespace() const;
+
+ bool UrlIsInPrerenderManager(const std::string& html_file) const;
+ bool UrlIsInPrerenderManager(const GURL& url) const;
+
+ // Convenience function to get the currently active WebContents in
+ // current_browser().
+ content::WebContents* GetActiveWebContents() const;
+
+ PrerenderManager* GetPrerenderManager() const;
+
+ TestPrerenderContents* GetPrerenderContentsFor(const GURL& url) const;
+
+ // Overload for a single expected final status
+ std::unique_ptr<TestPrerender> PrerenderTestURL(
+ const std::string& html_file,
+ FinalStatus expected_final_status,
+ int expected_number_of_loads) {
+ GURL url = embedded_test_server()->GetURL(html_file);
+ return PrerenderTestURL(url,
+ expected_final_status,
+ expected_number_of_loads);
+ }
+
+ ScopedVector<TestPrerender> PrerenderTestURL(
+ const std::string& html_file,
+ const std::vector<FinalStatus>& expected_final_status_queue,
+ int expected_number_of_loads) {
+ GURL url = embedded_test_server()->GetURL(html_file);
+ return PrerenderTestURLImpl(url,
+ expected_final_status_queue,
+ expected_number_of_loads);
+ }
+
+ std::unique_ptr<TestPrerender> PrerenderTestURL(
+ const GURL& url,
+ FinalStatus expected_final_status,
+ int expected_number_of_loads) {
+ std::vector<FinalStatus> expected_final_status_queue(
+ 1, expected_final_status);
+ std::vector<TestPrerender*> prerenders;
+ PrerenderTestURLImpl(url,
+ expected_final_status_queue,
+ expected_number_of_loads).release(&prerenders);
+ CHECK_EQ(1u, prerenders.size());
+ return std::unique_ptr<TestPrerender>(prerenders[0]);
droger 2016/09/02 14:33:54 Inlining is discouraged in Chromium, although I'm
mattcary 2016/09/02 15:02:07 I did hit lint warnings for other ftns, there seem
+ }
+
+ safe_browsing::TestSafeBrowsingServiceFactory* safe_browsing_factory() const {
+ return safe_browsing_factory_.get();
+ }
+
+ TestPrerenderContentsFactory* prerender_contents_factory() const {
+ return prerender_contents_factory_;
+ }
+
+ void set_autostart_test_server(bool value) {
+ autostart_test_server_ = value;
+ }
+
+ void set_browser(Browser* browser) {
+ explicitly_set_browser_ = browser;
+ }
+
+ Browser* current_browser() const {
+ return explicitly_set_browser_ ? explicitly_set_browser_ : browser();
+ }
+
+ protected:
+ virtual ScopedVector<TestPrerender> PrerenderTestURLImpl(
droger 2016/09/02 14:33:54 nit: looks like this could be private.
mattcary 2016/09/02 15:02:07 It needs to be protected to be overridden by subcl
droger 2016/09/02 15:05:27 No. A virtual private function can be overriden b
+ const GURL& prerender_url,
+ const std::vector<FinalStatus>& expected_final_status_queue,
+ int expected_number_of_loads) = 0;
+
+ private:
+ std::unique_ptr<ExternalProtocolHandler::Delegate>
+ external_protocol_handler_delegate_;
+ std::unique_ptr<safe_browsing::TestSafeBrowsingServiceFactory>
+ safe_browsing_factory_;
+ TestPrerenderContentsFactory* prerender_contents_factory_;
+ Browser* explicitly_set_browser_;
+ bool autostart_test_server_;
+};
+
+// Makes |url| respond to requests with the contents of |file|, counting the
+// number that start in |counter|.
+void CreateCountingInterceptorOnIO(
+ const GURL& url,
+ const base::FilePath& file,
+ const base::WeakPtr<RequestCounter>& counter);
+
+// Makes |url| respond to requests with the contents of |file|.
+void CreateMockInterceptorOnIO(const GURL& url, const base::FilePath& file);
+
+} // namespace test_utils
+
+} // namespace prerender
+
+#endif // CHROME_BROWSER_PRERENDER_PRERENDER_TEST_UTILS_H_

Powered by Google App Engine
This is Rietveld 408576698