| 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"; |
| 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 |
| 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); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 const GURL& url, | 241 const GURL& url, |
| 168 net::RequestPriority priority, | 242 net::RequestPriority priority, |
| 169 content::ResourceType resource_type, | 243 content::ResourceType resource_type, |
| 170 bool is_main_frame); | 244 bool is_main_frame); |
| 171 | 245 |
| 172 // For printing failures nicely. | 246 // For printing failures nicely. |
| 173 std::ostream& operator<<(std::ostream& stream, const PrefetchData& data); | 247 std::ostream& operator<<(std::ostream& stream, const PrefetchData& data); |
| 174 std::ostream& operator<<(std::ostream& stream, const ResourceData& resource); | 248 std::ostream& operator<<(std::ostream& stream, const ResourceData& resource); |
| 175 std::ostream& operator<<(std::ostream& stream, const RedirectData& data); | 249 std::ostream& operator<<(std::ostream& stream, const RedirectData& data); |
| 176 std::ostream& operator<<(std::ostream& stream, const RedirectStat& redirect); | 250 std::ostream& operator<<(std::ostream& stream, const RedirectStat& redirect); |
| 177 std::ostream& operator<<( | 251 std::ostream& operator<<(std::ostream& stream, |
| 178 std::ostream& stream, | 252 const PageRequestSummary& summary); |
| 179 const ResourcePrefetchPredictor::PageRequestSummary& summary); | 253 std::ostream& operator<<(std::ostream& stream, |
| 180 std::ostream& operator<<( | 254 const URLRequestSummary& summary); |
| 181 std::ostream& stream, | |
| 182 const ResourcePrefetchPredictor::URLRequestSummary& summary); | |
| 183 std::ostream& operator<<(std::ostream& stream, const NavigationID& id); | 255 std::ostream& operator<<(std::ostream& stream, const NavigationID& id); |
| 184 | 256 |
| 185 std::ostream& operator<<(std::ostream& os, const OriginData& data); | 257 std::ostream& operator<<(std::ostream& os, const OriginData& data); |
| 186 std::ostream& operator<<(std::ostream& os, const OriginStat& redirect); | 258 std::ostream& operator<<(std::ostream& os, const OriginStat& redirect); |
| 187 | 259 |
| 188 bool operator==(const PrefetchData& lhs, const PrefetchData& rhs); | 260 bool operator==(const PrefetchData& lhs, const PrefetchData& rhs); |
| 189 bool operator==(const ResourceData& lhs, const ResourceData& rhs); | 261 bool operator==(const ResourceData& lhs, const ResourceData& rhs); |
| 190 bool operator==(const RedirectData& lhs, const RedirectData& rhs); | 262 bool operator==(const RedirectData& lhs, const RedirectData& rhs); |
| 191 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs); | 263 bool operator==(const RedirectStat& lhs, const RedirectStat& rhs); |
| 192 bool operator==(const ResourcePrefetchPredictor::PageRequestSummary& lhs, | 264 bool operator==(const PageRequestSummary& lhs, const PageRequestSummary& rhs); |
| 193 const ResourcePrefetchPredictor::PageRequestSummary& rhs); | 265 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); | 266 bool operator==(const OriginData& lhs, const OriginData& rhs); |
| 197 bool operator==(const OriginStat& lhs, const OriginStat& rhs); | 267 bool operator==(const OriginStat& lhs, const OriginStat& rhs); |
| 198 | 268 |
| 199 } // namespace predictors | 269 } // namespace predictors |
| 200 | 270 |
| 201 namespace precache { | 271 namespace precache { |
| 202 | 272 |
| 203 std::ostream& operator<<(std::ostream& stream, | 273 std::ostream& operator<<(std::ostream& stream, |
| 204 const PrecacheManifest& manifest); | 274 const PrecacheManifest& manifest); |
| 205 std::ostream& operator<<(std::ostream& stream, | 275 std::ostream& operator<<(std::ostream& stream, |
| 206 const PrecacheResource& resource); | 276 const PrecacheResource& resource); |
| 207 | 277 |
| 208 bool operator==(const PrecacheManifest& lhs, const PrecacheManifest& rhs); | 278 bool operator==(const PrecacheManifest& lhs, const PrecacheManifest& rhs); |
| 209 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs); | 279 bool operator==(const PrecacheResource& lhs, const PrecacheResource& rhs); |
| 210 | 280 |
| 211 } // namespace precache | 281 } // namespace precache |
| 212 | 282 |
| 213 #endif // CHROME_BROWSER_PREDICTORS_LOADING_TEST_UTIL_H_ | 283 #endif // CHROME_BROWSER_PREDICTORS_LOADING_TEST_UTIL_H_ |
| OLD | NEW |