OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 #ifndef CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TEST_UTIL_H_ | 4 #ifndef CHROME_BROWSER_PREDICTORS_LOADING_TEST_UTIL_H_ |
5 #define CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TEST_UTIL_H_ | 5 #define CHROME_BROWSER_PREDICTORS_LOADING_TEST_UTIL_H_ |
6 | 6 |
| 7 #include <memory> |
| 8 #include <set> |
7 #include <string> | 9 #include <string> |
8 #include <vector> | 10 #include <vector> |
9 | 11 |
10 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 12 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
11 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 13 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
12 #include "components/sessions/core/session_id.h" | 14 #include "components/sessions/core/session_id.h" |
| 15 #include "net/url_request/url_request_context.h" |
| 16 #include "net/url_request/url_request_job.h" |
| 17 #include "net/url_request/url_request_job_factory.h" |
| 18 #include "net/url_request/url_request_test_util.h" |
13 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
14 | 20 |
15 namespace predictors { | 21 namespace predictors { |
16 | 22 |
17 // Does nothing, controls which URLs are prefetchable. | 23 // Does nothing, controls which URLs are prefetchable. |
18 class MockResourcePrefetchPredictor : public ResourcePrefetchPredictor { | 24 class MockResourcePrefetchPredictor : public ResourcePrefetchPredictor { |
19 public: | 25 public: |
20 MockResourcePrefetchPredictor(const LoadingPredictorConfig& config, | 26 MockResourcePrefetchPredictor(const LoadingPredictorConfig& config, |
21 Profile* profile); | 27 Profile* profile); |
22 ~MockResourcePrefetchPredictor(); | 28 ~MockResourcePrefetchPredictor(); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 const std::string& redirect_url = std::string(), | 99 const std::string& redirect_url = std::string(), |
94 bool has_validators = false, | 100 bool has_validators = false, |
95 bool always_revalidate = false); | 101 bool always_revalidate = false); |
96 | 102 |
97 ResourcePrefetchPredictor::Prediction CreatePrediction( | 103 ResourcePrefetchPredictor::Prediction CreatePrediction( |
98 const std::string& main_frame_key, | 104 const std::string& main_frame_key, |
99 std::vector<GURL> subresource_urls); | 105 std::vector<GURL> subresource_urls); |
100 | 106 |
101 void PopulateTestConfig(LoadingPredictorConfig* config, bool small_db = true); | 107 void PopulateTestConfig(LoadingPredictorConfig* config, bool small_db = true); |
102 | 108 |
| 109 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( |
| 110 const char* headers); |
| 111 |
| 112 class MockURLRequestJob : public net::URLRequestJob { |
| 113 public: |
| 114 MockURLRequestJob(net::URLRequest* request, |
| 115 const net::HttpResponseInfo& response_info, |
| 116 const std::string& mime_type); |
| 117 |
| 118 bool GetMimeType(std::string* mime_type) const override; |
| 119 |
| 120 protected: |
| 121 void Start() override; |
| 122 void GetResponseInfo(net::HttpResponseInfo* info) override; |
| 123 |
| 124 private: |
| 125 net::HttpResponseInfo response_info_; |
| 126 std::string mime_type_; |
| 127 }; |
| 128 |
| 129 class MockURLRequestJobFactory : public net::URLRequestJobFactory { |
| 130 public: |
| 131 MockURLRequestJobFactory(); |
| 132 ~MockURLRequestJobFactory() override; |
| 133 |
| 134 void Reset(); |
| 135 |
| 136 net::URLRequestJob* MaybeCreateJobWithProtocolHandler( |
| 137 const std::string& scheme, |
| 138 net::URLRequest* request, |
| 139 net::NetworkDelegate* network_delegate) const override; |
| 140 |
| 141 net::URLRequestJob* MaybeInterceptRedirect( |
| 142 net::URLRequest* request, |
| 143 net::NetworkDelegate* network_delegate, |
| 144 const GURL& location) const override; |
| 145 |
| 146 net::URLRequestJob* MaybeInterceptResponse( |
| 147 net::URLRequest* request, |
| 148 net::NetworkDelegate* network_delegate) const override; |
| 149 |
| 150 bool IsHandledProtocol(const std::string& scheme) const override; |
| 151 |
| 152 bool IsSafeRedirectTarget(const GURL& location) const override; |
| 153 |
| 154 void set_response_info(const net::HttpResponseInfo& response_info) { |
| 155 response_info_ = response_info; |
| 156 } |
| 157 |
| 158 void set_mime_type(const std::string& mime_type) { mime_type_ = mime_type; } |
| 159 |
| 160 private: |
| 161 net::HttpResponseInfo response_info_; |
| 162 std::string mime_type_; |
| 163 }; |
| 164 |
| 165 std::unique_ptr<net::URLRequest> CreateURLRequest( |
| 166 const net::TestURLRequestContext& url_request_context, |
| 167 const GURL& url, |
| 168 net::RequestPriority priority, |
| 169 content::ResourceType resource_type, |
| 170 bool is_main_frame); |
| 171 |
103 // For printing failures nicely. | 172 // For printing failures nicely. |
104 std::ostream& operator<<(std::ostream& stream, const PrefetchData& data); | 173 std::ostream& operator<<(std::ostream& stream, const PrefetchData& data); |
105 std::ostream& operator<<(std::ostream& stream, const ResourceData& resource); | 174 std::ostream& operator<<(std::ostream& stream, const ResourceData& resource); |
106 std::ostream& operator<<(std::ostream& stream, const RedirectData& data); | 175 std::ostream& operator<<(std::ostream& stream, const RedirectData& data); |
107 std::ostream& operator<<(std::ostream& stream, const RedirectStat& redirect); | 176 std::ostream& operator<<(std::ostream& stream, const RedirectStat& redirect); |
108 std::ostream& operator<<( | 177 std::ostream& operator<<( |
109 std::ostream& stream, | 178 std::ostream& stream, |
110 const ResourcePrefetchPredictor::PageRequestSummary& summary); | 179 const ResourcePrefetchPredictor::PageRequestSummary& summary); |
111 std::ostream& operator<<( | 180 std::ostream& operator<<( |
112 std::ostream& stream, | 181 std::ostream& stream, |
(...skipping 21 matching lines...) Expand all Loading... |
134 std::ostream& operator<<(std::ostream& stream, | 203 std::ostream& operator<<(std::ostream& stream, |
135 const PrecacheManifest& manifest); | 204 const PrecacheManifest& manifest); |
136 std::ostream& operator<<(std::ostream& stream, | 205 std::ostream& operator<<(std::ostream& stream, |
137 const PrecacheResource& resource); | 206 const PrecacheResource& resource); |
138 | 207 |
139 bool operator==(const PrecacheManifest& lhs, const PrecacheManifest& rhs); | 208 bool operator==(const PrecacheManifest& lhs, const PrecacheManifest& rhs); |
140 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs); | 209 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs); |
141 | 210 |
142 } // namespace precache | 211 } // namespace precache |
143 | 212 |
144 #endif // CHROME_BROWSER_PREDICTORS_RESOURCE_PREFETCH_PREDICTOR_TEST_UTIL_H_ | 213 #endif // CHROME_BROWSER_PREDICTORS_LOADING_TEST_UTIL_H_ |
OLD | NEW |