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