Chromium Code Reviews| 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_LOADING_TEST_UTIL_H_ | 4 #ifndef CHROME_BROWSER_PREDICTORS_LOADING_TEST_UTIL_H_ |
| 5 #define CHROME_BROWSER_PREDICTORS_LOADING_TEST_UTIL_H_ | 5 #define CHROME_BROWSER_PREDICTORS_LOADING_TEST_UTIL_H_ |
| 6 | 6 |
| 7 #include <map> | |
| 7 #include <memory> | 8 #include <memory> |
| 8 #include <set> | 9 #include <set> |
| 9 #include <string> | 10 #include <string> |
| 10 #include <vector> | 11 #include <vector> |
| 11 | 12 |
| 13 #include "chrome/browser/predictors/loading_data_collector.h" | |
| 12 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 14 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 13 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 15 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 14 #include "components/sessions/core/session_id.h" | 16 #include "components/sessions/core/session_id.h" |
| 15 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
| 16 #include "net/url_request/url_request_job.h" | 18 #include "net/url_request/url_request_job.h" |
| 17 #include "net/url_request/url_request_job_factory.h" | 19 #include "net/url_request/url_request_job_factory.h" |
| 18 #include "net/url_request/url_request_test_util.h" | 20 #include "net/url_request/url_request_test_util.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 21 #include "testing/gmock/include/gmock/gmock.h" |
| 20 | 22 |
| 21 namespace predictors { | 23 namespace predictors { |
| 22 | 24 |
| 23 // Does nothing, controls which URLs are prefetchable. | 25 // Does nothing, controls which URLs are prefetchable. |
| 24 class MockResourcePrefetchPredictor : public ResourcePrefetchPredictor { | 26 class MockResourcePrefetchPredictor : public ResourcePrefetchPredictor { |
| 25 public: | 27 public: |
| 26 MockResourcePrefetchPredictor(const LoadingPredictorConfig& config, | 28 MockResourcePrefetchPredictor(const LoadingPredictorConfig& config, |
| 27 Profile* profile); | 29 Profile* profile); |
| 28 ~MockResourcePrefetchPredictor(); | 30 ~MockResourcePrefetchPredictor(); |
| 29 | 31 |
| 32 virtual void RecordPageRequestSummary( | |
| 33 std::unique_ptr<PageRequestSummary> summary) { | |
| 34 LOG(WARNING) << "RecordPageRequestSummary"; | |
|
alexilin
2017/06/22 19:39:05
ditto
trevordixon
2017/06/27 21:28:55
Done.
| |
| 35 RecordPageRequestSummaryProxy(summary.get()); | |
| 36 } | |
| 37 | |
| 30 MOCK_CONST_METHOD2(GetPrefetchData, | 38 MOCK_CONST_METHOD2(GetPrefetchData, |
| 31 bool(const GURL&, ResourcePrefetchPredictor::Prediction*)); | 39 bool(const GURL&, ResourcePrefetchPredictor::Prediction*)); |
| 32 MOCK_METHOD0(StartInitialization, void()); | 40 MOCK_METHOD0(StartInitialization, void()); |
| 33 MOCK_METHOD0(Shutdown, void()); | 41 MOCK_METHOD0(Shutdown, void()); |
| 34 MOCK_METHOD2(StartPrefetching, | 42 MOCK_METHOD2(StartPrefetching, |
| 35 void(const GURL&, const ResourcePrefetchPredictor::Prediction&)); | 43 void(const GURL&, const ResourcePrefetchPredictor::Prediction&)); |
| 36 MOCK_METHOD1(StopPrefeching, void(const GURL&)); | 44 MOCK_METHOD1(StopPrefeching, void(const GURL&)); |
| 45 MOCK_METHOD1(RecordPageRequestSummaryProxy, void(PageRequestSummary*)); | |
| 46 }; | |
| 47 | |
| 48 template <typename T> | |
| 49 class FakeGlowplugKeyValueTable : public GlowplugKeyValueTable<T> { | |
| 50 public: | |
| 51 FakeGlowplugKeyValueTable() : GlowplugKeyValueTable<T>("") {} | |
| 52 void GetAllData(std::map<std::string, T>* data_map, | |
| 53 sql::Connection* db) const override { | |
| 54 *data_map = data_; | |
| 55 } | |
| 56 void UpdateData(const std::string& key, | |
| 57 const T& data, | |
| 58 sql::Connection* db) override { | |
| 59 data_[key] = data; | |
| 60 } | |
| 61 void DeleteData(const std::vector<std::string>& keys, | |
| 62 sql::Connection* db) override { | |
| 63 for (const auto& key : keys) | |
| 64 data_.erase(key); | |
| 65 } | |
| 66 void DeleteAllData(sql::Connection* db) override { data_.clear(); } | |
| 67 | |
| 68 std::map<std::string, T> data_; | |
| 69 }; | |
| 70 | |
| 71 class MockResourcePrefetchPredictorTables | |
|
alexilin
2017/06/22 19:39:05
Why did you move it here? As I can see ResourcePre
trevordixon
2017/06/27 21:28:55
You're right, moved back.
| |
| 72 : public ResourcePrefetchPredictorTables { | |
| 73 public: | |
| 74 MockResourcePrefetchPredictorTables(); | |
| 75 | |
| 76 void ScheduleDBTask(const tracked_objects::Location& from_here, | |
| 77 DBTask task) override; | |
| 78 | |
| 79 void ExecuteDBTaskOnDBThread(DBTask task) override; | |
| 80 | |
| 81 GlowplugKeyValueTable<PrefetchData>* url_resource_table() override; | |
| 82 | |
| 83 GlowplugKeyValueTable<RedirectData>* url_redirect_table() override; | |
| 84 | |
| 85 GlowplugKeyValueTable<PrefetchData>* host_resource_table() override; | |
| 86 | |
| 87 GlowplugKeyValueTable<RedirectData>* host_redirect_table() override; | |
| 88 | |
| 89 GlowplugKeyValueTable<precache::PrecacheManifest>* manifest_table() override; | |
| 90 | |
| 91 GlowplugKeyValueTable<OriginData>* origin_table() override; | |
| 92 | |
| 93 FakeGlowplugKeyValueTable<PrefetchData> url_resource_table_; | |
| 94 FakeGlowplugKeyValueTable<RedirectData> url_redirect_table_; | |
| 95 FakeGlowplugKeyValueTable<PrefetchData> host_resource_table_; | |
| 96 FakeGlowplugKeyValueTable<RedirectData> host_redirect_table_; | |
| 97 FakeGlowplugKeyValueTable<precache::PrecacheManifest> manifest_table_; | |
| 98 FakeGlowplugKeyValueTable<OriginData> origin_table_; | |
| 99 | |
| 100 protected: | |
| 101 ~MockResourcePrefetchPredictorTables() override; | |
| 102 }; | |
| 103 | |
| 104 class MockResourcePrefetchPredictorObserver : public TestObserver { | |
| 105 public: | |
| 106 explicit MockResourcePrefetchPredictorObserver( | |
| 107 ResourcePrefetchPredictor* predictor); | |
| 108 ~MockResourcePrefetchPredictorObserver(); | |
| 109 | |
| 110 MOCK_METHOD2(OnNavigationLearned, | |
| 111 void(size_t url_visit_count, const PageRequestSummary& summary)); | |
| 37 }; | 112 }; |
| 38 | 113 |
| 39 void InitializeResourceData(ResourceData* resource, | 114 void InitializeResourceData(ResourceData* resource, |
| 40 const std::string& resource_url, | 115 const std::string& resource_url, |
| 41 content::ResourceType resource_type, | 116 content::ResourceType resource_type, |
| 42 int number_of_hits, | 117 int number_of_hits, |
| 43 int number_of_misses, | 118 int number_of_misses, |
| 44 int consecutive_misses, | 119 int consecutive_misses, |
| 45 double average_position, | 120 double average_position, |
| 46 net::RequestPriority priority, | 121 net::RequestPriority priority, |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 75 uint64_t last_visit_time = 0); | 150 uint64_t last_visit_time = 0); |
| 76 RedirectData CreateRedirectData(const std::string& primary_key, | 151 RedirectData CreateRedirectData(const std::string& primary_key, |
| 77 uint64_t last_visit_time = 0); | 152 uint64_t last_visit_time = 0); |
| 78 precache::PrecacheManifest CreateManifestData(int64_t id = 0); | 153 precache::PrecacheManifest CreateManifestData(int64_t id = 0); |
| 79 OriginData CreateOriginData(const std::string& host, | 154 OriginData CreateOriginData(const std::string& host, |
| 80 uint64_t last_visit_time = 0); | 155 uint64_t last_visit_time = 0); |
| 81 | 156 |
| 82 NavigationID CreateNavigationID(SessionID::id_type tab_id, | 157 NavigationID CreateNavigationID(SessionID::id_type tab_id, |
| 83 const std::string& main_frame_url); | 158 const std::string& main_frame_url); |
| 84 | 159 |
| 85 ResourcePrefetchPredictor::PageRequestSummary CreatePageRequestSummary( | 160 PageRequestSummary CreatePageRequestSummary( |
| 86 const std::string& main_frame_url, | 161 const std::string& main_frame_url, |
| 87 const std::string& initial_url, | 162 const std::string& initial_url, |
| 88 const std::vector<ResourcePrefetchPredictor::URLRequestSummary>& | 163 const std::vector<URLRequestSummary>& subresource_requests); |
| 89 subresource_requests); | |
| 90 | 164 |
| 91 ResourcePrefetchPredictor::URLRequestSummary CreateURLRequestSummary( | 165 URLRequestSummary CreateURLRequestSummary( |
| 92 SessionID::id_type tab_id, | 166 SessionID::id_type tab_id, |
| 93 const std::string& main_frame_url, | 167 const std::string& main_frame_url, |
| 94 const std::string& resource_url = std::string(), | 168 const std::string& resource_url = std::string(), |
| 95 content::ResourceType resource_type = content::RESOURCE_TYPE_MAIN_FRAME, | 169 content::ResourceType resource_type = content::RESOURCE_TYPE_MAIN_FRAME, |
| 96 net::RequestPriority priority = net::MEDIUM, | 170 net::RequestPriority priority = net::MEDIUM, |
| 97 const std::string& mime_type = std::string(), | 171 const std::string& mime_type = std::string(), |
| 98 bool was_cached = false, | 172 bool was_cached = false, |
| 99 const std::string& redirect_url = std::string(), | 173 const std::string& redirect_url = std::string(), |
| 100 bool has_validators = false, | 174 bool has_validators = false, |
| 101 bool always_revalidate = false); | 175 bool always_revalidate = false); |
| 102 | 176 |
| 177 URLRequestSummary CreateRedirectRequestSummary( | |
| 178 SessionID::id_type session_id, | |
| 179 const std::string& main_frame_url, | |
| 180 const std::string& redirect_url); | |
| 181 | |
| 103 ResourcePrefetchPredictor::Prediction CreatePrediction( | 182 ResourcePrefetchPredictor::Prediction CreatePrediction( |
| 104 const std::string& main_frame_key, | 183 const std::string& main_frame_key, |
| 105 std::vector<GURL> subresource_urls); | 184 std::vector<GURL> subresource_urls); |
| 106 | 185 |
| 107 void PopulateTestConfig(LoadingPredictorConfig* config, bool small_db = true); | 186 void PopulateTestConfig(LoadingPredictorConfig* config, bool small_db = true); |
| 108 | 187 |
| 109 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( | 188 scoped_refptr<net::HttpResponseHeaders> MakeResponseHeaders( |
| 110 const char* headers); | 189 const char* headers); |
| 111 | 190 |
| 112 class MockURLRequestJob : public net::URLRequestJob { | 191 class MockURLRequestJob : public net::URLRequestJob { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 167 const GURL& url, | 246 const GURL& url, |
| 168 net::RequestPriority priority, | 247 net::RequestPriority priority, |
| 169 content::ResourceType resource_type, | 248 content::ResourceType resource_type, |
| 170 bool is_main_frame); | 249 bool is_main_frame); |
| 171 | 250 |
| 172 // For printing failures nicely. | 251 // For printing failures nicely. |
| 173 std::ostream& operator<<(std::ostream& stream, const PrefetchData& data); | 252 std::ostream& operator<<(std::ostream& stream, const PrefetchData& data); |
| 174 std::ostream& operator<<(std::ostream& stream, const ResourceData& resource); | 253 std::ostream& operator<<(std::ostream& stream, const ResourceData& resource); |
| 175 std::ostream& operator<<(std::ostream& stream, const RedirectData& data); | 254 std::ostream& operator<<(std::ostream& stream, const RedirectData& data); |
| 176 std::ostream& operator<<(std::ostream& stream, const RedirectStat& redirect); | 255 std::ostream& operator<<(std::ostream& stream, const RedirectStat& redirect); |
| 177 std::ostream& operator<<( | 256 std::ostream& operator<<(std::ostream& stream, |
| 178 std::ostream& stream, | 257 const PageRequestSummary& summary); |
| 179 const ResourcePrefetchPredictor::PageRequestSummary& summary); | 258 std::ostream& operator<<(std::ostream& stream, |
| 180 std::ostream& operator<<( | 259 const URLRequestSummary& summary); |
| 181 std::ostream& stream, | |
| 182 const ResourcePrefetchPredictor::URLRequestSummary& summary); | |
| 183 std::ostream& operator<<(std::ostream& stream, const NavigationID& id); | 260 std::ostream& operator<<(std::ostream& stream, const NavigationID& id); |
| 184 | 261 |
| 185 std::ostream& operator<<(std::ostream& os, const OriginData& data); | 262 std::ostream& operator<<(std::ostream& os, const OriginData& data); |
| 186 std::ostream& operator<<(std::ostream& os, const OriginStat& redirect); | 263 std::ostream& operator<<(std::ostream& os, const OriginStat& redirect); |
| 187 | 264 |
| 188 bool operator==(const PrefetchData& lhs, const PrefetchData& rhs); | 265 bool operator==(const PrefetchData& lhs, const PrefetchData& rhs); |
| 189 bool operator==(const ResourceData& lhs, const ResourceData& rhs); | 266 bool operator==(const ResourceData& lhs, const ResourceData& rhs); |
| 190 bool operator==(const RedirectData& lhs, const RedirectData& rhs); | 267 bool operator==(const RedirectData& lhs, const RedirectData& rhs); |
| 191 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs); | 268 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs); |
| 192 bool operator==(const ResourcePrefetchPredictor::PageRequestSummary& lhs, | 269 bool operator==(const PageRequestSummary& lhs, const PageRequestSummary& rhs); |
| 193 const ResourcePrefetchPredictor::PageRequestSummary& rhs); | 270 bool operator==(const URLRequestSummary& lhs, const URLRequestSummary& rhs); |
| 194 bool operator==(const ResourcePrefetchPredictor::URLRequestSummary& lhs, | |
| 195 const ResourcePrefetchPredictor::URLRequestSummary& rhs); | |
| 196 bool operator==(const OriginData& lhs, const OriginData& rhs); | 271 bool operator==(const OriginData& lhs, const OriginData& rhs); |
| 197 bool operator==(const OriginStat& lhs, const OriginStat& rhs); | 272 bool operator==(const OriginStat& lhs, const OriginStat& rhs); |
| 198 | 273 |
| 199 } // namespace predictors | 274 } // namespace predictors |
| 200 | 275 |
| 201 namespace precache { | 276 namespace precache { |
| 202 | 277 |
| 203 std::ostream& operator<<(std::ostream& stream, | 278 std::ostream& operator<<(std::ostream& stream, |
| 204 const PrecacheManifest& manifest); | 279 const PrecacheManifest& manifest); |
| 205 std::ostream& operator<<(std::ostream& stream, | 280 std::ostream& operator<<(std::ostream& stream, |
| 206 const PrecacheResource& resource); | 281 const PrecacheResource& resource); |
| 207 | 282 |
| 208 bool operator==(const PrecacheManifest& lhs, const PrecacheManifest& rhs); | 283 bool operator==(const PrecacheManifest& lhs, const PrecacheManifest& rhs); |
| 209 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs); | 284 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs); |
| 210 | 285 |
| 211 } // namespace precache | 286 } // namespace precache |
| 212 | 287 |
| 213 #endif // CHROME_BROWSER_PREDICTORS_LOADING_TEST_UTIL_H_ | 288 #endif // CHROME_BROWSER_PREDICTORS_LOADING_TEST_UTIL_H_ |
| OLD | NEW |