blob: ab2c8390e2bd5c98db723a332cff1c8cc4c3be08 [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2019 The Chromium Authors
Tsuyoshi Horof6017cd2019-05-07 03:01:522// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_LOADER_PREFETCH_BROWSERTEST_BASE_H_
6#define CONTENT_BROWSER_LOADER_PREFETCH_BROWSERTEST_BASE_H_
7
8#include <string>
9#include <vector>
10
Avi Drissmanadac21992023-01-11 23:46:3911#include "base/functional/callback.h"
Tsuyoshi Horo295224932019-06-07 09:41:0212#include "base/memory/ref_counted.h"
13#include "base/synchronization/lock.h"
14#include "base/thread_annotations.h"
Tsuyoshi Horof6017cd2019-05-07 03:01:5215#include "content/public/test/content_browser_test.h"
16#include "net/test/embedded_test_server/http_request.h"
17#include "net/test/embedded_test_server/http_response.h"
18
19namespace net {
20namespace test_server {
21class EmbeddedTestServer;
22struct HttpRequest;
23} // namespace test_server
24} // namespace net
25
26namespace content {
27
28class SignedExchangeHandlerFactory;
29
30class PrefetchBrowserTestBase : public ContentBrowserTest {
31 public:
32 struct ResponseEntry {
33 ResponseEntry();
Tsuyoshi Horo92499bf12019-06-05 04:21:2134 ResponseEntry(
Tsuyoshi Horof6017cd2019-05-07 03:01:5235 const std::string& content,
36 const std::string& content_types = "text/html",
Dominic Farolino693fd72d2019-08-05 11:09:4437 const std::vector<std::pair<std::string, std::string>>& headers = {},
38 net::HttpStatusCode code = net::HTTP_OK);
Tsuyoshi Horo92499bf12019-06-05 04:21:2139 ResponseEntry(const ResponseEntry&) = delete;
40 ResponseEntry(ResponseEntry&& other);
41 ResponseEntry& operator=(const ResponseEntry&) = delete;
42 ResponseEntry& operator=(ResponseEntry&&);
Tsuyoshi Horof6017cd2019-05-07 03:01:5243 ~ResponseEntry();
44
45 std::string content;
46 std::string content_type;
47 std::vector<std::pair<std::string, std::string>> headers;
Dominic Farolino693fd72d2019-08-05 11:09:4448 net::HttpStatusCode code;
Tsuyoshi Horof6017cd2019-05-07 03:01:5249 };
50
51 struct ScopedSignedExchangeHandlerFactory {
52 explicit ScopedSignedExchangeHandlerFactory(
53 SignedExchangeHandlerFactory* factory);
54 ~ScopedSignedExchangeHandlerFactory();
55 };
56
57 PrefetchBrowserTestBase();
Peter Boström828b9022021-09-21 02:28:4358
59 PrefetchBrowserTestBase(const PrefetchBrowserTestBase&) = delete;
60 PrefetchBrowserTestBase& operator=(const PrefetchBrowserTestBase&) = delete;
61
Tsuyoshi Horof6017cd2019-05-07 03:01:5262 ~PrefetchBrowserTestBase() override;
63
64 void SetUpOnMainThread() override;
65
66 protected:
Tsuyoshi Horo295224932019-06-07 09:41:0267 class RequestCounter : public base::RefCountedThreadSafe<RequestCounter> {
68 public:
69 // Create a counter that is to be incremented when |path| on the
70 // |test_server| is accessed. |waiter| can be optionally specified that will
71 // be run after the counter is incremented. The counter value can be
72 // obtained via GetRequestCount(). This class works across threads,
73 // GetRequestCount can be called on any threads.
74 static scoped_refptr<RequestCounter> CreateAndMonitor(
75 net::EmbeddedTestServer* test_server,
76 const std::string& path,
77 base::RunLoop* waiter = nullptr);
78 RequestCounter(const std::string& path, base::RunLoop* waiter);
79
Peter Boström9b036532021-10-28 23:37:2880 RequestCounter(const RequestCounter&) = delete;
81 RequestCounter& operator=(const RequestCounter&) = delete;
82
Tsuyoshi Horo295224932019-06-07 09:41:0283 int GetRequestCount();
84
85 private:
86 friend base::RefCountedThreadSafe<RequestCounter>;
87 ~RequestCounter();
88
89 void OnRequest(const net::test_server::HttpRequest& request);
90
91 base::OnceClosure waiter_closure_;
92 const std::string path_;
93 int request_count_ GUARDED_BY(lock_) = 0;
94 base::Lock lock_;
Tsuyoshi Horo295224932019-06-07 09:41:0295 };
96
Tsuyoshi Horo92499bf12019-06-05 04:21:2197 void RegisterResponse(const std::string& url, ResponseEntry&& entry);
Tsuyoshi Horof6017cd2019-05-07 03:01:5298
99 std::unique_ptr<net::test_server::HttpResponse> ServeResponses(
100 const net::test_server::HttpRequest& request);
Tsuyoshi Horof6017cd2019-05-07 03:01:52101 void OnPrefetchURLLoaderCalled();
Tsuyoshi Horof6017cd2019-05-07 03:01:52102
103 void RegisterRequestHandler(
104 net::test_server::EmbeddedTestServer* test_server);
105 void NavigateToURLAndWaitTitle(const GURL& url, const std::string& title);
106 void WaitUntilLoaded(const GURL& url);
107
Tsuyoshi Horo295224932019-06-07 09:41:02108 int GetPrefetchURLLoaderCallCount();
Tsuyoshi Horof6017cd2019-05-07 03:01:52109
Xiaochen Zhoue377ec82024-06-28 14:50:53110 // Register a callback to be called just before the `PrefetchURLLoader` is
111 // created and started.
112 void RegisterPrefetchLoaderCallback(base::OnceClosure callback);
113
Tsuyoshi Horof6017cd2019-05-07 03:01:52114 private:
115 std::map<std::string, ResponseEntry> response_map_;
116
Xiaochen Zhoue377ec82024-06-28 14:50:53117 base::OnceClosure prefetch_loader_callback_;
118
Tsuyoshi Horo295224932019-06-07 09:41:02119 int prefetch_url_loader_called_ GUARDED_BY(lock_) = 0;
120 base::Lock lock_;
Tsuyoshi Horof6017cd2019-05-07 03:01:52121};
122
123} // namespace content
124
125#endif // CONTENT_BROWSER_LOADER_PREFETCH_BROWSERTEST_BASE_H_