| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 | 4 |
| 5 #include <memory> | 5 #include <memory> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 void TestDeleteSingleDataPoint(); | 36 void TestDeleteSingleDataPoint(); |
| 37 void TestDeleteAllData(); | 37 void TestDeleteAllData(); |
| 38 | 38 |
| 39 content::TestBrowserThreadBundle thread_bundle_; | 39 content::TestBrowserThreadBundle thread_bundle_; |
| 40 TestingProfile profile_; | 40 TestingProfile profile_; |
| 41 std::unique_ptr<PredictorDatabase> db_; | 41 std::unique_ptr<PredictorDatabase> db_; |
| 42 scoped_refptr<ResourcePrefetchPredictorTables> tables_; | 42 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 43 | 43 |
| 44 using PrefetchDataMap = ResourcePrefetchPredictorTables::PrefetchDataMap; | 44 using PrefetchDataMap = ResourcePrefetchPredictorTables::PrefetchDataMap; |
| 45 using RedirectDataMap = ResourcePrefetchPredictorTables::RedirectDataMap; | 45 using RedirectDataMap = ResourcePrefetchPredictorTables::RedirectDataMap; |
| 46 using ManifestDataMap = ResourcePrefetchPredictorTables::ManifestDataMap; |
| 46 | 47 |
| 47 private: | 48 private: |
| 48 // Initializes the tables, |test_url_data_| and |test_host_data_|. | 49 // Initializes the tables, |test_url_data_|, |test_host_data_|, |
| 50 // |test_url_redirect_data_|, |test_host_redirect_data_| and |
| 51 // |test_manifest_data_|. |
| 49 void InitializeSampleData(); | 52 void InitializeSampleData(); |
| 50 | 53 |
| 51 // Checks that the input PrefetchData are the same, although the resources | 54 // Checks that the input PrefetchData are the same, although the resources |
| 52 // can be in different order. | 55 // can be in different order. |
| 53 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, | 56 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, |
| 54 const PrefetchDataMap& rhs) const; | 57 const PrefetchDataMap& rhs) const; |
| 55 void TestResourcesAreEqual(const std::vector<ResourceData>& lhs, | 58 void TestResourcesAreEqual(const std::vector<ResourceData>& lhs, |
| 56 const std::vector<ResourceData>& rhs) const; | 59 const std::vector<ResourceData>& rhs) const; |
| 57 | 60 |
| 58 // Checks that the input RedirectData are the same, although the redirects | 61 // Checks that the input RedirectData are the same, although the redirects |
| 59 // can be in different order. | 62 // can be in different order. |
| 60 void TestRedirectDataAreEqual(const RedirectDataMap& lhs, | 63 void TestRedirectDataAreEqual(const RedirectDataMap& lhs, |
| 61 const RedirectDataMap& rhs) const; | 64 const RedirectDataMap& rhs) const; |
| 62 void TestRedirectsAreEqual(const std::vector<RedirectStat>& lhs, | 65 void TestRedirectsAreEqual(const std::vector<RedirectStat>& lhs, |
| 63 const std::vector<RedirectStat>& rhs) const; | 66 const std::vector<RedirectStat>& rhs) const; |
| 64 | 67 |
| 68 // Checks that the input ManifestData are the same, although the resources |
| 69 // can be in different order. |
| 70 void TestManifestDataAreEqual(const ManifestDataMap& lhs, |
| 71 const ManifestDataMap& rhs) const; |
| 72 void TestManifestResourcesAreEqual( |
| 73 const std::vector<precache::PrecacheResource>& lhs, |
| 74 const std::vector<precache::PrecacheResource>& rhs) const; |
| 75 |
| 65 void AddKey(PrefetchDataMap* m, const std::string& key) const; | 76 void AddKey(PrefetchDataMap* m, const std::string& key) const; |
| 66 void AddKey(RedirectDataMap* m, const std::string& key) const; | 77 void AddKey(RedirectDataMap* m, const std::string& key) const; |
| 78 void AddKey(ManifestDataMap* m, const std::string& key) const; |
| 67 | 79 |
| 68 PrefetchDataMap test_url_data_; | 80 PrefetchDataMap test_url_data_; |
| 69 PrefetchDataMap test_host_data_; | 81 PrefetchDataMap test_host_data_; |
| 70 RedirectDataMap test_url_redirect_data_; | 82 RedirectDataMap test_url_redirect_data_; |
| 71 RedirectDataMap test_host_redirect_data_; | 83 RedirectDataMap test_host_redirect_data_; |
| 84 ManifestDataMap test_manifest_data_; |
| 72 }; | 85 }; |
| 73 | 86 |
| 74 class ResourcePrefetchPredictorTablesReopenTest | 87 class ResourcePrefetchPredictorTablesReopenTest |
| 75 : public ResourcePrefetchPredictorTablesTest { | 88 : public ResourcePrefetchPredictorTablesTest { |
| 76 public: | 89 public: |
| 77 void SetUp() override { | 90 void SetUp() override { |
| 78 // Write data to the table, and then reopen the db. | 91 // Write data to the table, and then reopen the db. |
| 79 ResourcePrefetchPredictorTablesTest::SetUp(); | 92 ResourcePrefetchPredictorTablesTest::SetUp(); |
| 80 ResourcePrefetchPredictorTablesTest::TearDown(); | 93 ResourcePrefetchPredictorTablesTest::TearDown(); |
| 81 | 94 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 99 | 112 |
| 100 void ResourcePrefetchPredictorTablesTest::TearDown() { | 113 void ResourcePrefetchPredictorTablesTest::TearDown() { |
| 101 tables_ = NULL; | 114 tables_ = NULL; |
| 102 db_.reset(); | 115 db_.reset(); |
| 103 base::RunLoop().RunUntilIdle(); | 116 base::RunLoop().RunUntilIdle(); |
| 104 } | 117 } |
| 105 | 118 |
| 106 void ResourcePrefetchPredictorTablesTest::TestGetAllData() { | 119 void ResourcePrefetchPredictorTablesTest::TestGetAllData() { |
| 107 PrefetchDataMap actual_url_data, actual_host_data; | 120 PrefetchDataMap actual_url_data, actual_host_data; |
| 108 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; | 121 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 122 ManifestDataMap actual_manifest_data; |
| 109 tables_->GetAllData(&actual_url_data, &actual_host_data, | 123 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 110 &actual_url_redirect_data, &actual_host_redirect_data); | 124 &actual_url_redirect_data, &actual_host_redirect_data, |
| 125 &actual_manifest_data); |
| 111 | 126 |
| 112 TestPrefetchDataAreEqual(test_url_data_, actual_url_data); | 127 TestPrefetchDataAreEqual(test_url_data_, actual_url_data); |
| 113 TestPrefetchDataAreEqual(test_host_data_, actual_host_data); | 128 TestPrefetchDataAreEqual(test_host_data_, actual_host_data); |
| 114 TestRedirectDataAreEqual(test_url_redirect_data_, actual_url_redirect_data); | 129 TestRedirectDataAreEqual(test_url_redirect_data_, actual_url_redirect_data); |
| 115 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); | 130 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); |
| 131 TestManifestDataAreEqual(test_manifest_data_, actual_manifest_data); |
| 116 } | 132 } |
| 117 | 133 |
| 118 void ResourcePrefetchPredictorTablesTest::TestDeleteData() { | 134 void ResourcePrefetchPredictorTablesTest::TestDeleteData() { |
| 119 std::vector<std::string> urls_to_delete = {"http://www.google.com", | 135 std::vector<std::string> urls_to_delete = {"http://www.google.com", |
| 120 "http://www.yahoo.com"}; | 136 "http://www.yahoo.com"}; |
| 121 std::vector<std::string> hosts_to_delete = {"www.yahoo.com"}; | 137 std::vector<std::string> hosts_to_delete = {"www.yahoo.com"}; |
| 122 | |
| 123 tables_->DeleteResourceData(urls_to_delete, hosts_to_delete); | 138 tables_->DeleteResourceData(urls_to_delete, hosts_to_delete); |
| 124 | 139 |
| 125 urls_to_delete = {"http://fb.com/google", "http://google.com"}; | 140 urls_to_delete = {"http://fb.com/google", "http://google.com"}; |
| 126 hosts_to_delete = {"microsoft.com"}; | 141 hosts_to_delete = {"microsoft.com"}; |
| 142 tables_->DeleteRedirectData(urls_to_delete, hosts_to_delete); |
| 127 | 143 |
| 128 tables_->DeleteRedirectData(urls_to_delete, hosts_to_delete); | 144 hosts_to_delete = {"en.wikipedia.org"}; |
| 145 tables_->DeleteManifestData(hosts_to_delete); |
| 129 | 146 |
| 130 PrefetchDataMap actual_url_data, actual_host_data; | 147 PrefetchDataMap actual_url_data, actual_host_data; |
| 131 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; | 148 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 149 ManifestDataMap actual_manifest_data; |
| 132 tables_->GetAllData(&actual_url_data, &actual_host_data, | 150 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 133 &actual_url_redirect_data, &actual_host_redirect_data); | 151 &actual_url_redirect_data, &actual_host_redirect_data, |
| 152 &actual_manifest_data); |
| 134 | 153 |
| 135 PrefetchDataMap expected_url_data, expected_host_data; | 154 PrefetchDataMap expected_url_data, expected_host_data; |
| 136 RedirectDataMap expected_url_redirect_data, expected_host_redirect_data; | 155 RedirectDataMap expected_url_redirect_data, expected_host_redirect_data; |
| 156 ManifestDataMap expected_manifest_data; |
| 137 AddKey(&expected_url_data, "http://www.reddit.com"); | 157 AddKey(&expected_url_data, "http://www.reddit.com"); |
| 138 AddKey(&expected_host_data, "www.facebook.com"); | 158 AddKey(&expected_host_data, "www.facebook.com"); |
| 139 AddKey(&expected_url_redirect_data, "http://nyt.com"); | 159 AddKey(&expected_url_redirect_data, "http://nyt.com"); |
| 140 AddKey(&expected_host_redirect_data, "bbc.com"); | 160 AddKey(&expected_host_redirect_data, "bbc.com"); |
| 161 AddKey(&expected_manifest_data, "youtube.com"); |
| 141 | 162 |
| 142 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 163 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 143 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 164 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 144 TestRedirectDataAreEqual(expected_url_redirect_data, | 165 TestRedirectDataAreEqual(expected_url_redirect_data, |
| 145 actual_url_redirect_data); | 166 actual_url_redirect_data); |
| 146 TestRedirectDataAreEqual(expected_host_redirect_data, | 167 TestRedirectDataAreEqual(expected_host_redirect_data, |
| 147 actual_host_redirect_data); | 168 actual_host_redirect_data); |
| 169 TestManifestDataAreEqual(expected_manifest_data, actual_manifest_data); |
| 148 } | 170 } |
| 149 | 171 |
| 150 void ResourcePrefetchPredictorTablesTest::TestDeleteSingleDataPoint() { | 172 void ResourcePrefetchPredictorTablesTest::TestDeleteSingleDataPoint() { |
| 151 // Delete a URL. | 173 // Delete a URL. |
| 152 tables_->DeleteSingleResourceDataPoint("http://www.reddit.com", | 174 tables_->DeleteSingleResourceDataPoint("http://www.reddit.com", |
| 153 PREFETCH_KEY_TYPE_URL); | 175 PREFETCH_KEY_TYPE_URL); |
| 154 | 176 |
| 155 PrefetchDataMap actual_url_data, actual_host_data; | 177 PrefetchDataMap actual_url_data, actual_host_data; |
| 156 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; | 178 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 179 ManifestDataMap actual_manifest_data; |
| 157 tables_->GetAllData(&actual_url_data, &actual_host_data, | 180 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 158 &actual_url_redirect_data, &actual_host_redirect_data); | 181 &actual_url_redirect_data, &actual_host_redirect_data, |
| 182 &actual_manifest_data); |
| 159 | 183 |
| 160 PrefetchDataMap expected_url_data; | 184 PrefetchDataMap expected_url_data; |
| 161 AddKey(&expected_url_data, "http://www.google.com"); | 185 AddKey(&expected_url_data, "http://www.google.com"); |
| 162 AddKey(&expected_url_data, "http://www.yahoo.com"); | 186 AddKey(&expected_url_data, "http://www.yahoo.com"); |
| 163 | 187 |
| 164 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 188 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 165 TestPrefetchDataAreEqual(test_host_data_, actual_host_data); | 189 TestPrefetchDataAreEqual(test_host_data_, actual_host_data); |
| 166 TestRedirectDataAreEqual(test_url_redirect_data_, actual_url_redirect_data); | 190 TestRedirectDataAreEqual(test_url_redirect_data_, actual_url_redirect_data); |
| 167 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); | 191 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); |
| 168 | 192 |
| 169 // Delete a host. | 193 // Delete a host. |
| 170 tables_->DeleteSingleResourceDataPoint("www.facebook.com", | 194 tables_->DeleteSingleResourceDataPoint("www.facebook.com", |
| 171 PREFETCH_KEY_TYPE_HOST); | 195 PREFETCH_KEY_TYPE_HOST); |
| 172 actual_url_data.clear(); | 196 actual_url_data.clear(); |
| 173 actual_host_data.clear(); | 197 actual_host_data.clear(); |
| 174 actual_url_redirect_data.clear(); | 198 actual_url_redirect_data.clear(); |
| 175 actual_host_redirect_data.clear(); | 199 actual_host_redirect_data.clear(); |
| 200 actual_manifest_data.clear(); |
| 176 tables_->GetAllData(&actual_url_data, &actual_host_data, | 201 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 177 &actual_url_redirect_data, &actual_host_redirect_data); | 202 &actual_url_redirect_data, &actual_host_redirect_data, |
| 203 &actual_manifest_data); |
| 178 | 204 |
| 179 PrefetchDataMap expected_host_data; | 205 PrefetchDataMap expected_host_data; |
| 180 AddKey(&expected_host_data, "www.yahoo.com"); | 206 AddKey(&expected_host_data, "www.yahoo.com"); |
| 181 | 207 |
| 182 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 208 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 183 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 209 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 184 TestRedirectDataAreEqual(test_url_redirect_data_, actual_url_redirect_data); | 210 TestRedirectDataAreEqual(test_url_redirect_data_, actual_url_redirect_data); |
| 185 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); | 211 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); |
| 186 | 212 |
| 187 // Delete a URL redirect. | 213 // Delete a URL redirect. |
| 188 tables_->DeleteSingleRedirectDataPoint("http://nyt.com", | 214 tables_->DeleteSingleRedirectDataPoint("http://nyt.com", |
| 189 PREFETCH_KEY_TYPE_URL); | 215 PREFETCH_KEY_TYPE_URL); |
| 190 actual_url_data.clear(); | 216 actual_url_data.clear(); |
| 191 actual_host_data.clear(); | 217 actual_host_data.clear(); |
| 192 actual_url_redirect_data.clear(); | 218 actual_url_redirect_data.clear(); |
| 193 actual_host_redirect_data.clear(); | 219 actual_host_redirect_data.clear(); |
| 220 actual_manifest_data.clear(); |
| 194 tables_->GetAllData(&actual_url_data, &actual_host_data, | 221 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 195 &actual_url_redirect_data, &actual_host_redirect_data); | 222 &actual_url_redirect_data, &actual_host_redirect_data, |
| 223 &actual_manifest_data); |
| 196 | 224 |
| 197 RedirectDataMap expected_url_redirect_data; | 225 RedirectDataMap expected_url_redirect_data; |
| 198 AddKey(&expected_url_redirect_data, "http://fb.com/google"); | 226 AddKey(&expected_url_redirect_data, "http://fb.com/google"); |
| 199 AddKey(&expected_url_redirect_data, "http://google.com"); | 227 AddKey(&expected_url_redirect_data, "http://google.com"); |
| 200 | 228 |
| 201 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 229 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 202 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 230 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 203 TestRedirectDataAreEqual(expected_url_redirect_data, | 231 TestRedirectDataAreEqual(expected_url_redirect_data, |
| 204 actual_url_redirect_data); | 232 actual_url_redirect_data); |
| 205 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); | 233 TestRedirectDataAreEqual(test_host_redirect_data_, actual_host_redirect_data); |
| 206 | 234 |
| 207 // Delete a host redirect. | 235 // Delete a host redirect. |
| 208 tables_->DeleteSingleRedirectDataPoint("bbc.com", PREFETCH_KEY_TYPE_HOST); | 236 tables_->DeleteSingleRedirectDataPoint("bbc.com", PREFETCH_KEY_TYPE_HOST); |
| 209 actual_url_data.clear(); | 237 actual_url_data.clear(); |
| 210 actual_host_data.clear(); | 238 actual_host_data.clear(); |
| 211 actual_url_redirect_data.clear(); | 239 actual_url_redirect_data.clear(); |
| 212 actual_host_redirect_data.clear(); | 240 actual_host_redirect_data.clear(); |
| 241 actual_manifest_data.clear(); |
| 213 tables_->GetAllData(&actual_url_data, &actual_host_data, | 242 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 214 &actual_url_redirect_data, &actual_host_redirect_data); | 243 &actual_url_redirect_data, &actual_host_redirect_data, |
| 244 &actual_manifest_data); |
| 215 | 245 |
| 216 RedirectDataMap expected_host_redirect_data; | 246 RedirectDataMap expected_host_redirect_data; |
| 217 AddKey(&expected_host_redirect_data, "microsoft.com"); | 247 AddKey(&expected_host_redirect_data, "microsoft.com"); |
| 218 | 248 |
| 219 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 249 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 220 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 250 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 221 TestRedirectDataAreEqual(expected_url_redirect_data, | 251 TestRedirectDataAreEqual(expected_url_redirect_data, |
| 222 actual_url_redirect_data); | 252 actual_url_redirect_data); |
| 223 TestRedirectDataAreEqual(expected_host_redirect_data, | 253 TestRedirectDataAreEqual(expected_host_redirect_data, |
| 224 actual_host_redirect_data); | 254 actual_host_redirect_data); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 250 "https://facebook.fr/google", 4, 2, 1); | 280 "https://facebook.fr/google", 4, 2, 1); |
| 251 | 281 |
| 252 RedirectData microsoft = CreateRedirectData("microsoft.com", 21); | 282 RedirectData microsoft = CreateRedirectData("microsoft.com", 21); |
| 253 InitializeRedirectStat(microsoft.add_redirect_endpoints(), "m.microsoft.com", | 283 InitializeRedirectStat(microsoft.add_redirect_endpoints(), "m.microsoft.com", |
| 254 5, 7, 1); | 284 5, 7, 1); |
| 255 InitializeRedirectStat(microsoft.add_redirect_endpoints(), "microsoft.org", 7, | 285 InitializeRedirectStat(microsoft.add_redirect_endpoints(), "microsoft.org", 7, |
| 256 2, 0); | 286 2, 0); |
| 257 | 287 |
| 258 tables_->UpdateData(google, yahoo, facebook, microsoft); | 288 tables_->UpdateData(google, yahoo, facebook, microsoft); |
| 259 | 289 |
| 290 precache::PrecacheManifest theverge; |
| 291 InitializePrecacheResource(theverge.add_resource(), |
| 292 "https://www.theverge.com/main.js", 0.7); |
| 293 |
| 294 tables_->UpdateManifestData("theverge.com", theverge); |
| 295 |
| 260 PrefetchDataMap actual_url_data, actual_host_data; | 296 PrefetchDataMap actual_url_data, actual_host_data; |
| 261 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; | 297 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 298 ManifestDataMap actual_manifest_data; |
| 262 tables_->GetAllData(&actual_url_data, &actual_host_data, | 299 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 263 &actual_url_redirect_data, &actual_host_redirect_data); | 300 &actual_url_redirect_data, &actual_host_redirect_data, |
| 301 &actual_manifest_data); |
| 264 | 302 |
| 265 PrefetchDataMap expected_url_data, expected_host_data; | 303 PrefetchDataMap expected_url_data, expected_host_data; |
| 266 RedirectDataMap expected_url_redirect_data, expected_host_redirect_data; | 304 RedirectDataMap expected_url_redirect_data, expected_host_redirect_data; |
| 305 ManifestDataMap expected_manifest_data; |
| 267 AddKey(&expected_url_data, "http://www.reddit.com"); | 306 AddKey(&expected_url_data, "http://www.reddit.com"); |
| 268 AddKey(&expected_url_data, "http://www.yahoo.com"); | 307 AddKey(&expected_url_data, "http://www.yahoo.com"); |
| 269 expected_url_data.insert(std::make_pair("http://www.google.com", google)); | 308 expected_url_data.insert(std::make_pair("http://www.google.com", google)); |
| 270 | 309 |
| 271 AddKey(&expected_host_data, "www.facebook.com"); | 310 AddKey(&expected_host_data, "www.facebook.com"); |
| 272 expected_host_data.insert(std::make_pair("www.yahoo.com", yahoo)); | 311 expected_host_data.insert(std::make_pair("www.yahoo.com", yahoo)); |
| 273 | 312 |
| 274 AddKey(&expected_url_redirect_data, "http://nyt.com"); | 313 AddKey(&expected_url_redirect_data, "http://nyt.com"); |
| 275 AddKey(&expected_url_redirect_data, "http://google.com"); | 314 AddKey(&expected_url_redirect_data, "http://google.com"); |
| 276 expected_url_redirect_data.insert( | 315 expected_url_redirect_data.insert( |
| 277 std::make_pair("http://fb.com/google", facebook)); | 316 std::make_pair("http://fb.com/google", facebook)); |
| 278 | 317 |
| 279 AddKey(&expected_host_redirect_data, "bbc.com"); | 318 AddKey(&expected_host_redirect_data, "bbc.com"); |
| 280 expected_host_redirect_data.insert( | 319 expected_host_redirect_data.insert( |
| 281 std::make_pair("microsoft.com", microsoft)); | 320 std::make_pair("microsoft.com", microsoft)); |
| 282 | 321 |
| 322 AddKey(&expected_manifest_data, "youtube.com"); |
| 323 AddKey(&expected_manifest_data, "en.wikipedia.org"); |
| 324 expected_manifest_data.insert(std::make_pair("theverge.com", theverge)); |
| 325 |
| 283 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); | 326 TestPrefetchDataAreEqual(expected_url_data, actual_url_data); |
| 284 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); | 327 TestPrefetchDataAreEqual(expected_host_data, actual_host_data); |
| 285 TestRedirectDataAreEqual(expected_url_redirect_data, | 328 TestRedirectDataAreEqual(expected_url_redirect_data, |
| 286 actual_url_redirect_data); | 329 actual_url_redirect_data); |
| 287 TestRedirectDataAreEqual(expected_host_redirect_data, | 330 TestRedirectDataAreEqual(expected_host_redirect_data, |
| 288 actual_host_redirect_data); | 331 actual_host_redirect_data); |
| 332 TestManifestDataAreEqual(expected_manifest_data, actual_manifest_data); |
| 289 } | 333 } |
| 290 | 334 |
| 291 void ResourcePrefetchPredictorTablesTest::TestDeleteAllData() { | 335 void ResourcePrefetchPredictorTablesTest::TestDeleteAllData() { |
| 292 tables_->DeleteAllData(); | 336 tables_->DeleteAllData(); |
| 293 | 337 |
| 294 PrefetchDataMap actual_url_data, actual_host_data; | 338 PrefetchDataMap actual_url_data, actual_host_data; |
| 295 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; | 339 RedirectDataMap actual_url_redirect_data, actual_host_redirect_data; |
| 340 ManifestDataMap actual_manifest_data; |
| 296 tables_->GetAllData(&actual_url_data, &actual_host_data, | 341 tables_->GetAllData(&actual_url_data, &actual_host_data, |
| 297 &actual_url_redirect_data, &actual_host_redirect_data); | 342 &actual_url_redirect_data, &actual_host_redirect_data, |
| 343 &actual_manifest_data); |
| 298 EXPECT_TRUE(actual_url_data.empty()); | 344 EXPECT_TRUE(actual_url_data.empty()); |
| 299 EXPECT_TRUE(actual_host_data.empty()); | 345 EXPECT_TRUE(actual_host_data.empty()); |
| 300 EXPECT_TRUE(actual_url_redirect_data.empty()); | 346 EXPECT_TRUE(actual_url_redirect_data.empty()); |
| 301 EXPECT_TRUE(actual_host_redirect_data.empty()); | 347 EXPECT_TRUE(actual_host_redirect_data.empty()); |
| 348 EXPECT_TRUE(actual_manifest_data.empty()); |
| 302 } | 349 } |
| 303 | 350 |
| 304 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( | 351 void ResourcePrefetchPredictorTablesTest::TestPrefetchDataAreEqual( |
| 305 const PrefetchDataMap& lhs, | 352 const PrefetchDataMap& lhs, |
| 306 const PrefetchDataMap& rhs) const { | 353 const PrefetchDataMap& rhs) const { |
| 307 EXPECT_EQ(lhs.size(), rhs.size()); | 354 EXPECT_EQ(lhs.size(), rhs.size()); |
| 308 | 355 |
| 309 for (const auto& p : rhs) { | 356 for (const auto& p : rhs) { |
| 310 const auto lhs_it = lhs.find(p.first); | 357 const auto lhs_it = lhs.find(p.first); |
| 311 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; | 358 ASSERT_TRUE(lhs_it != lhs.end()) << p.first; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 EXPECT_EQ(r, lhs_it->second); | 429 EXPECT_EQ(r, lhs_it->second); |
| 383 lhs_index.erase(lhs_it); | 430 lhs_index.erase(lhs_it); |
| 384 } else { | 431 } else { |
| 385 ADD_FAILURE() << r.url(); | 432 ADD_FAILURE() << r.url(); |
| 386 } | 433 } |
| 387 } | 434 } |
| 388 | 435 |
| 389 EXPECT_TRUE(lhs_index.empty()); | 436 EXPECT_TRUE(lhs_index.empty()); |
| 390 } | 437 } |
| 391 | 438 |
| 439 void ResourcePrefetchPredictorTablesTest::TestManifestDataAreEqual( |
| 440 const ManifestDataMap& lhs, |
| 441 const ManifestDataMap& rhs) const { |
| 442 EXPECT_EQ(lhs.size(), rhs.size()); |
| 443 |
| 444 for (const auto& m : rhs) { |
| 445 const auto lhs_it = lhs.find(m.first); |
| 446 ASSERT_TRUE(lhs_it != lhs.end()) << m.first; |
| 447 EXPECT_EQ(lhs_it->second.id().id(), m.second.id().id()); |
| 448 |
| 449 std::vector<precache::PrecacheResource> lhs_resources( |
| 450 lhs_it->second.resource().begin(), lhs_it->second.resource().end()); |
| 451 std::vector<precache::PrecacheResource> rhs_resources( |
| 452 m.second.resource().begin(), m.second.resource().end()); |
| 453 |
| 454 TestManifestResourcesAreEqual(lhs_resources, rhs_resources); |
| 455 } |
| 456 } |
| 457 |
| 458 void ResourcePrefetchPredictorTablesTest::TestManifestResourcesAreEqual( |
| 459 const std::vector<precache::PrecacheResource>& lhs, |
| 460 const std::vector<precache::PrecacheResource>& rhs) const { |
| 461 EXPECT_EQ(lhs.size(), rhs.size()); |
| 462 |
| 463 std::map<std::string, precache::PrecacheResource> lhs_index; |
| 464 // Repeated resources are not allowed. |
| 465 for (const auto& r : lhs) |
| 466 EXPECT_TRUE(lhs_index.insert(std::make_pair(r.url(), r)).second); |
| 467 |
| 468 for (const auto& r : rhs) { |
| 469 auto lhs_it = lhs_index.find(r.url()); |
| 470 if (lhs_it != lhs_index.end()) { |
| 471 EXPECT_EQ(r, lhs_it->second); |
| 472 lhs_index.erase(lhs_it); |
| 473 } else { |
| 474 ADD_FAILURE() << r.url(); |
| 475 } |
| 476 } |
| 477 |
| 478 EXPECT_TRUE(lhs_index.empty()); |
| 479 } |
| 480 |
| 392 void ResourcePrefetchPredictorTablesTest::AddKey(PrefetchDataMap* m, | 481 void ResourcePrefetchPredictorTablesTest::AddKey(PrefetchDataMap* m, |
| 393 const std::string& key) const { | 482 const std::string& key) const { |
| 394 PrefetchDataMap::const_iterator it = test_url_data_.find(key); | 483 PrefetchDataMap::const_iterator it = test_url_data_.find(key); |
| 395 if (it != test_url_data_.end()) { | 484 if (it != test_url_data_.end()) { |
| 396 m->insert(*it); | 485 m->insert(*it); |
| 397 return; | 486 return; |
| 398 } | 487 } |
| 399 it = test_host_data_.find(key); | 488 it = test_host_data_.find(key); |
| 400 ASSERT_TRUE(it != test_host_data_.end()); | 489 ASSERT_TRUE(it != test_host_data_.end()); |
| 401 m->insert(*it); | 490 m->insert(*it); |
| 402 } | 491 } |
| 403 | 492 |
| 404 void ResourcePrefetchPredictorTablesTest::AddKey(RedirectDataMap* m, | 493 void ResourcePrefetchPredictorTablesTest::AddKey(RedirectDataMap* m, |
| 405 const std::string& key) const { | 494 const std::string& key) const { |
| 406 auto it = test_url_redirect_data_.find(key); | 495 auto it = test_url_redirect_data_.find(key); |
| 407 if (it != test_url_redirect_data_.end()) { | 496 if (it != test_url_redirect_data_.end()) { |
| 408 m->insert(*it); | 497 m->insert(*it); |
| 409 return; | 498 return; |
| 410 } | 499 } |
| 411 it = test_host_redirect_data_.find(key); | 500 it = test_host_redirect_data_.find(key); |
| 412 ASSERT_TRUE(it != test_host_redirect_data_.end()); | 501 ASSERT_TRUE(it != test_host_redirect_data_.end()); |
| 413 m->insert(*it); | 502 m->insert(*it); |
| 414 } | 503 } |
| 415 | 504 |
| 505 void ResourcePrefetchPredictorTablesTest::AddKey(ManifestDataMap* m, |
| 506 const std::string& key) const { |
| 507 auto it = test_manifest_data_.find(key); |
| 508 ASSERT_TRUE(it != test_manifest_data_.end()); |
| 509 m->insert(*it); |
| 510 } |
| 511 |
| 416 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { | 512 void ResourcePrefetchPredictorTablesTest::InitializeSampleData() { |
| 417 PrefetchData empty_resource_data; | 513 PrefetchData empty_resource_data; |
| 418 RedirectData empty_redirect_data; | 514 RedirectData empty_redirect_data; |
| 419 | 515 |
| 420 { // Url data. | 516 { // Url data. |
| 421 PrefetchData google = CreatePrefetchData("http://www.google.com", 1); | 517 PrefetchData google = CreatePrefetchData("http://www.google.com", 1); |
| 422 InitializeResourceData(google.add_resources(), | 518 InitializeResourceData(google.add_resources(), |
| 423 "http://www.google.com/style.css", | 519 "http://www.google.com/style.css", |
| 424 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, | 520 content::RESOURCE_TYPE_STYLESHEET, 5, 2, 1, 1.1, |
| 425 net::MEDIUM, false, false); | 521 net::MEDIUM, false, false); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 | 642 |
| 547 test_host_redirect_data_.clear(); | 643 test_host_redirect_data_.clear(); |
| 548 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc)); | 644 test_host_redirect_data_.insert(std::make_pair(bbc.primary_key(), bbc)); |
| 549 test_host_redirect_data_.insert( | 645 test_host_redirect_data_.insert( |
| 550 std::make_pair(microsoft.primary_key(), microsoft)); | 646 std::make_pair(microsoft.primary_key(), microsoft)); |
| 551 tables_->UpdateData(empty_resource_data, empty_resource_data, | 647 tables_->UpdateData(empty_resource_data, empty_resource_data, |
| 552 empty_redirect_data, bbc); | 648 empty_redirect_data, bbc); |
| 553 tables_->UpdateData(empty_resource_data, empty_resource_data, | 649 tables_->UpdateData(empty_resource_data, empty_resource_data, |
| 554 empty_redirect_data, microsoft); | 650 empty_redirect_data, microsoft); |
| 555 } | 651 } |
| 652 |
| 653 { // Manifest data. |
| 654 precache::PrecacheManifest wikipedia; |
| 655 InitializePrecacheResource(wikipedia.add_resource(), |
| 656 "https://en.wikipedia.org/script.js", 0.7); |
| 657 InitializePrecacheResource(wikipedia.add_resource(), |
| 658 "https://en.wikipedia.org/image.png", 0.3); |
| 659 |
| 660 precache::PrecacheManifest youtube; |
| 661 InitializePrecacheResource(youtube.add_resource(), |
| 662 "https://youtube.com/photo.jpg", 0.5); |
| 663 InitializePrecacheResource(youtube.add_resource(), |
| 664 "https://youtube.com/base.js", 0.2); |
| 665 |
| 666 test_manifest_data_.clear(); |
| 667 test_manifest_data_.insert(std::make_pair("en.wikipedia.org", wikipedia)); |
| 668 test_manifest_data_.insert(std::make_pair("youtube.com", youtube)); |
| 669 tables_->UpdateManifestData("en.wikipedia.org", wikipedia); |
| 670 tables_->UpdateManifestData("youtube.com", youtube); |
| 671 } |
| 556 } | 672 } |
| 557 | 673 |
| 558 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() { | 674 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() { |
| 559 db_.reset(new PredictorDatabase(&profile_)); | 675 db_.reset(new PredictorDatabase(&profile_)); |
| 560 base::RunLoop().RunUntilIdle(); | 676 base::RunLoop().RunUntilIdle(); |
| 561 tables_ = db_->resource_prefetch_tables(); | 677 tables_ = db_->resource_prefetch_tables(); |
| 562 } | 678 } |
| 563 | 679 |
| 564 // Test cases. | 680 // Test cases. |
| 565 | 681 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 642 EXPECT_EQ(version + 1, | 758 EXPECT_EQ(version + 1, |
| 643 ResourcePrefetchPredictorTables::GetDatabaseVersion(db)); | 759 ResourcePrefetchPredictorTables::GetDatabaseVersion(db)); |
| 644 | 760 |
| 645 ReopenDatabase(); | 761 ReopenDatabase(); |
| 646 | 762 |
| 647 db = tables_->DB(); | 763 db = tables_->DB(); |
| 648 ASSERT_EQ(version, ResourcePrefetchPredictorTables::GetDatabaseVersion(db)); | 764 ASSERT_EQ(version, ResourcePrefetchPredictorTables::GetDatabaseVersion(db)); |
| 649 | 765 |
| 650 PrefetchDataMap url_data, host_data; | 766 PrefetchDataMap url_data, host_data; |
| 651 RedirectDataMap url_redirect_data, host_redirect_data; | 767 RedirectDataMap url_redirect_data, host_redirect_data; |
| 768 ManifestDataMap manifest_data; |
| 652 tables_->GetAllData(&url_data, &host_data, &url_redirect_data, | 769 tables_->GetAllData(&url_data, &host_data, &url_redirect_data, |
| 653 &host_redirect_data); | 770 &host_redirect_data, &manifest_data); |
| 654 EXPECT_TRUE(url_data.empty()); | 771 EXPECT_TRUE(url_data.empty()); |
| 655 EXPECT_TRUE(host_data.empty()); | 772 EXPECT_TRUE(host_data.empty()); |
| 656 EXPECT_TRUE(url_redirect_data.empty()); | 773 EXPECT_TRUE(url_redirect_data.empty()); |
| 657 EXPECT_TRUE(host_redirect_data.empty()); | 774 EXPECT_TRUE(host_redirect_data.empty()); |
| 775 EXPECT_TRUE(manifest_data.empty()); |
| 658 } | 776 } |
| 659 | 777 |
| 660 TEST_F(ResourcePrefetchPredictorTablesReopenTest, GetAllData) { | 778 TEST_F(ResourcePrefetchPredictorTablesReopenTest, GetAllData) { |
| 661 TestGetAllData(); | 779 TestGetAllData(); |
| 662 } | 780 } |
| 663 | 781 |
| 664 TEST_F(ResourcePrefetchPredictorTablesReopenTest, UpdateData) { | 782 TEST_F(ResourcePrefetchPredictorTablesReopenTest, UpdateData) { |
| 665 TestUpdateData(); | 783 TestUpdateData(); |
| 666 } | 784 } |
| 667 | 785 |
| 668 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteData) { | 786 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteData) { |
| 669 TestDeleteData(); | 787 TestDeleteData(); |
| 670 } | 788 } |
| 671 | 789 |
| 672 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { | 790 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { |
| 673 TestDeleteSingleDataPoint(); | 791 TestDeleteSingleDataPoint(); |
| 674 } | 792 } |
| 675 | 793 |
| 676 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { | 794 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { |
| 677 TestDeleteAllData(); | 795 TestDeleteAllData(); |
| 678 } | 796 } |
| 679 | 797 |
| 680 } // namespace predictors | 798 } // namespace predictors |
| OLD | NEW |