Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(619)

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc

Issue 2285673002: predictors: Version the resource_prefetch_predictor database. (Closed)
Patch Set: Address comments. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/predictors/resource_prefetch_predictor_tables.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <set> 5 #include <set>
6 #include <utility> 6 #include <utility>
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/predictors/predictor_database.h" 12 #include "chrome/browser/predictors/predictor_database.h"
13 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" 13 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
14 #include "chrome/test/base/testing_profile.h" 14 #include "chrome/test/base/testing_profile.h"
15 #include "content/public/test/test_browser_thread.h" 15 #include "content/public/test/test_browser_thread.h"
16 #include "net/base/request_priority.h" 16 #include "net/base/request_priority.h"
17 #include "sql/statement.h" 17 #include "sql/statement.h"
18 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
19 19
20 namespace predictors { 20 namespace predictors {
21 21
22 class ResourcePrefetchPredictorTablesTest : public testing::Test { 22 class ResourcePrefetchPredictorTablesTest : public testing::Test {
23 public: 23 public:
24 ResourcePrefetchPredictorTablesTest(); 24 ResourcePrefetchPredictorTablesTest();
25 ~ResourcePrefetchPredictorTablesTest() override; 25 ~ResourcePrefetchPredictorTablesTest() override;
26 void SetUp() override; 26 void SetUp() override;
27 void TearDown() override; 27 void TearDown() override;
28 28
29 protected: 29 protected:
30 void ReopenDatabase();
30 void TestGetAllData(); 31 void TestGetAllData();
31 void TestUpdateData(); 32 void TestUpdateData();
32 void TestDeleteData(); 33 void TestDeleteData();
33 void TestDeleteSingleDataPoint(); 34 void TestDeleteSingleDataPoint();
34 void TestDeleteAllData(); 35 void TestDeleteAllData();
35 36
36 base::MessageLoop loop_; 37 base::MessageLoop loop_;
37 content::TestBrowserThread db_thread_; 38 content::TestBrowserThread db_thread_;
38 TestingProfile profile_; 39 TestingProfile profile_;
39 std::unique_ptr<PredictorDatabase> db_; 40 std::unique_ptr<PredictorDatabase> db_;
40 scoped_refptr<ResourcePrefetchPredictorTables> tables_; 41 scoped_refptr<ResourcePrefetchPredictorTables> tables_;
41 42
43 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
pasko 2016/08/26 17:52:18 nit: using ResourcePrefetchPredictorTables::Prefet
Benoit L 2016/08/29 08:57:16 Done.
44
42 private: 45 private:
43 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; 46 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow;
44 typedef std::vector<ResourceRow> ResourceRows; 47 typedef std::vector<ResourceRow> ResourceRows;
45 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData; 48 typedef ResourcePrefetchPredictorTables::PrefetchData PrefetchData;
46 typedef ResourcePrefetchPredictorTables::PrefetchDataMap PrefetchDataMap;
47 49
48 // Initializes the tables, |test_url_data_| and |test_host_data_|. 50 // Initializes the tables, |test_url_data_| and |test_host_data_|.
49 void InitializeSampleData(); 51 void InitializeSampleData();
50 52
51 // Checks that the input PrefetchData are the same, although the resources 53 // Checks that the input PrefetchData are the same, although the resources
52 // can be in different order. 54 // can be in different order.
53 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs, 55 void TestPrefetchDataAreEqual(const PrefetchDataMap& lhs,
54 const PrefetchDataMap& rhs) const; 56 const PrefetchDataMap& rhs) const;
55 void TestResourceRowsAreEqual(const ResourceRows& lhs, 57 void TestResourceRowsAreEqual(const ResourceRows& lhs,
56 const ResourceRows& rhs) const; 58 const ResourceRows& rhs) const;
(...skipping 21 matching lines...) Expand all
78 }; 80 };
79 81
80 class ResourcePrefetchPredictorTablesReopenTest 82 class ResourcePrefetchPredictorTablesReopenTest
81 : public ResourcePrefetchPredictorTablesTest { 83 : public ResourcePrefetchPredictorTablesTest {
82 public: 84 public:
83 void SetUp() override { 85 void SetUp() override {
84 // Write data to the table, and then reopen the db. 86 // Write data to the table, and then reopen the db.
85 ResourcePrefetchPredictorTablesTest::SetUp(); 87 ResourcePrefetchPredictorTablesTest::SetUp();
86 ResourcePrefetchPredictorTablesTest::TearDown(); 88 ResourcePrefetchPredictorTablesTest::TearDown();
87 89
88 db_.reset(new PredictorDatabase(&profile_)); 90 ReopenDatabase();
89 base::RunLoop().RunUntilIdle();
90 tables_ = db_->resource_prefetch_tables();
91 } 91 }
92 }; 92 };
93 93
94 ResourcePrefetchPredictorTablesTest::ResourcePrefetchPredictorTablesTest() 94 ResourcePrefetchPredictorTablesTest::ResourcePrefetchPredictorTablesTest()
95 : loop_(base::MessageLoop::TYPE_DEFAULT), 95 : loop_(base::MessageLoop::TYPE_DEFAULT),
96 db_thread_(content::BrowserThread::DB, &loop_), 96 db_thread_(content::BrowserThread::DB, &loop_),
97 db_(new PredictorDatabase(&profile_)), 97 db_(new PredictorDatabase(&profile_)),
98 tables_(db_->resource_prefetch_tables()) { 98 tables_(db_->resource_prefetch_tables()) {
99 base::RunLoop().RunUntilIdle(); 99 base::RunLoop().RunUntilIdle();
100 } 100 }
(...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 test_host_data_.clear(); 354 test_host_data_.clear();
355 test_host_data_.insert(std::make_pair("www.facebook.com", facebook)); 355 test_host_data_.insert(std::make_pair("www.facebook.com", facebook));
356 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo)); 356 test_host_data_.insert(std::make_pair("www.yahoo.com", yahoo));
357 357
358 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string()); 358 PrefetchData empty_url_data(PREFETCH_KEY_TYPE_URL, std::string());
359 tables_->UpdateData(empty_url_data, facebook); 359 tables_->UpdateData(empty_url_data, facebook);
360 tables_->UpdateData(empty_url_data, yahoo); 360 tables_->UpdateData(empty_url_data, yahoo);
361 } 361 }
362 } 362 }
363 363
364 void ResourcePrefetchPredictorTablesTest::ReopenDatabase() {
365 db_.reset(new PredictorDatabase(&profile_));
366 base::RunLoop().RunUntilIdle();
367 tables_ = db_->resource_prefetch_tables();
368 }
369
364 // Test cases. 370 // Test cases.
365 371
366 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) { 372 TEST_F(ResourcePrefetchPredictorTablesTest, ComputeScore) {
367 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow; 373 typedef ResourcePrefetchPredictorTables::ResourceRow ResourceRow;
368 ResourceRow js_resource( 374 ResourceRow js_resource(
369 std::string(), "http://www.resources.google.com/script.js", 375 std::string(), "http://www.resources.google.com/script.js",
370 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM, false, false); 376 content::RESOURCE_TYPE_SCRIPT, 11, 0, 0, 1., net::MEDIUM, false, false);
371 ResourceRow image_resource( 377 ResourceRow image_resource(
372 std::string(), "http://www.resources.google.com/image.jpg", 378 std::string(), "http://www.resources.google.com/image.jpg",
373 content::RESOURCE_TYPE_IMAGE, 11, 0, 0, 1., net::MEDIUM, false, false); 379 content::RESOURCE_TYPE_IMAGE, 11, 0, 0, 1., net::MEDIUM, false, false);
(...skipping 24 matching lines...) Expand all
398 } 404 }
399 405
400 TEST_F(ResourcePrefetchPredictorTablesTest, DeleteSingleDataPoint) { 406 TEST_F(ResourcePrefetchPredictorTablesTest, DeleteSingleDataPoint) {
401 TestDeleteSingleDataPoint(); 407 TestDeleteSingleDataPoint();
402 } 408 }
403 409
404 TEST_F(ResourcePrefetchPredictorTablesTest, DeleteAllData) { 410 TEST_F(ResourcePrefetchPredictorTablesTest, DeleteAllData) {
405 TestDeleteAllData(); 411 TestDeleteAllData();
406 } 412 }
407 413
414 TEST_F(ResourcePrefetchPredictorTablesTest, DatabaseVersionIsSet) {
415 sql::Connection* db = tables_->DB();
416 const int version = ResourcePrefetchPredictorTables::kDatabaseVersion;
417 EXPECT_EQ(version, ResourcePrefetchPredictorTables::GetDatabaseVersion(db));
418 }
419
420 TEST_F(ResourcePrefetchPredictorTablesTest, DatabaseIsResetWhenIncompatible) {
421 const int version = ResourcePrefetchPredictorTables::kDatabaseVersion;
422 sql::Connection* db = tables_->DB();
423 ASSERT_TRUE(
424 ResourcePrefetchPredictorTables::SetDatabaseVersion(db, version + 1));
425 EXPECT_EQ(version + 1,
426 ResourcePrefetchPredictorTables::GetDatabaseVersion(db));
427
428 ReopenDatabase();
429
430 db = tables_->DB();
431 ASSERT_EQ(version, ResourcePrefetchPredictorTables::GetDatabaseVersion(db));
432
433 PrefetchDataMap url_data, host_data;
434 tables_->GetAllData(&url_data, &host_data);
435 EXPECT_TRUE(url_data.empty());
436 EXPECT_TRUE(host_data.empty());
437 }
438
408 TEST_F(ResourcePrefetchPredictorTablesReopenTest, GetAllData) { 439 TEST_F(ResourcePrefetchPredictorTablesReopenTest, GetAllData) {
409 TestGetAllData(); 440 TestGetAllData();
410 } 441 }
411 442
412 TEST_F(ResourcePrefetchPredictorTablesReopenTest, UpdateData) { 443 TEST_F(ResourcePrefetchPredictorTablesReopenTest, UpdateData) {
413 TestUpdateData(); 444 TestUpdateData();
414 } 445 }
415 446
416 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteData) { 447 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteData) {
417 TestDeleteData(); 448 TestDeleteData();
418 } 449 }
419 450
420 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) { 451 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteSingleDataPoint) {
421 TestDeleteSingleDataPoint(); 452 TestDeleteSingleDataPoint();
422 } 453 }
423 454
424 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) { 455 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllData) {
425 TestDeleteAllData(); 456 TestDeleteAllData();
426 } 457 }
427 458
428 } // namespace predictors 459 } // namespace predictors
OLDNEW
« no previous file with comments | « chrome/browser/predictors/resource_prefetch_predictor_tables.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698