Chromium Code Reviews| Index: chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc |
| diff --git a/chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc b/chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..70ef746a1ad95a7d75b8057c22d35ca301c35991 |
| --- /dev/null |
| +++ b/chrome/browser/predictors/resource_prefetch_predictor_tables_unittest.cc |
| @@ -0,0 +1,306 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include <set> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/message_loop.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "chrome/browser/predictors/predictor_database.h" |
| +#include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| +#include "chrome/test/base/testing_profile.h" |
| +#include "content/test/test_browser_thread.h" |
| +#include "sql/statement.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace predictors { |
| + |
| +ResourcePrefetchPredictorTables::UrlTableRow CreateUrlTableRow( |
|
dominich
2012/06/06 17:09:09
put this in an anonymous namespace
Shishir
2012/06/06 23:55:33
No longer required.
|
| + const std::string& main_frame_url, |
| + const std::string& resource_url, |
| + ResourceType::Type resource_type, |
| + int number_of_hits, |
| + int number_of_misses, |
| + int consecutive_misses, |
| + double average_position) { |
| + ResourcePrefetchPredictorTables::UrlTableRow row; |
| + row.main_frame_url = GURL(main_frame_url); |
| + row.resource_url = GURL(resource_url); |
| + row.resource_type = resource_type; |
| + row.number_of_hits = number_of_hits; |
| + row.number_of_misses = number_of_misses; |
| + row.consecutive_misses = consecutive_misses; |
| + row.average_position = average_position; |
| + row.UpdateScore(); |
| + return row; |
| +} |
| + |
| +class ResourcePrefetchPredictorTablesTest : public testing::Test { |
| + public: |
| + ResourcePrefetchPredictorTablesTest(); |
| + virtual ~ResourcePrefetchPredictorTablesTest(); |
| + |
| + virtual void SetUp(); |
|
dominich
2012/06/06 17:09:09
OVERRIDE
Shishir
2012/06/06 23:55:33
Done.
|
| + virtual void TearDown(); |
|
dominich
2012/06/06 17:09:09
OVERRIDE
Shishir
2012/06/06 23:55:33
Done.
|
| + |
| + protected: |
| + void TestGetAllRows(); |
| + void TestUpdateRowsForUrl(); |
| + void TestDeleteRowsForUrls(); |
| + void TestDeleteAllRows(); |
| + |
| + TestingProfile profile_; |
| + scoped_ptr<PredictorDatabase> db_; |
|
dominich
2012/06/06 17:09:09
can this just be an instance of a PredictorDatabas
Shishir
2012/06/06 23:55:33
I want to reset it for the ResourcePrefetchPredict
|
| + scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| + MessageLoop loop_; |
| + |
| + private: |
| + // Adds data to the URL table as well as test_url_rows_. |
| + void InitializeSampleUrlData(); |
| + |
| + // Checks that the input vectors have the same elements, although they may |
| + // be in different orders. |
| + void TestUrlRowVectorsAreEqual( |
| + const ResourcePrefetchPredictorTables::UrlTableRows& lhs, |
| + const ResourcePrefetchPredictorTables::UrlTableRows& rhs) const; |
| + bool UrlRowsAreEqual( |
| + const ResourcePrefetchPredictorTables::UrlTableRow& lhs, |
| + const ResourcePrefetchPredictorTables::UrlTableRow& rhs) const; |
| + |
| + content::TestBrowserThread db_thread_; |
| + ResourcePrefetchPredictorTables::UrlTableRows test_url_rows_; |
| +}; |
| + |
| +class ResourcePrefetchPredictorTablesReopenTest |
| + : public ResourcePrefetchPredictorTablesTest { |
| + public: |
| + virtual void SetUp() { |
| + // Write data to the table, and then reopen the db. |
| + ResourcePrefetchPredictorTablesTest::SetUp(); |
| + tables_ = NULL; |
| + db_.reset(new PredictorDatabase(&profile_)); |
| + loop_.RunAllPending(); |
| + tables_ = db_->resource_prefetch_tables(); |
| + } |
| +}; |
| + |
| +ResourcePrefetchPredictorTablesTest::ResourcePrefetchPredictorTablesTest() |
| + : loop_(MessageLoop::TYPE_DEFAULT), |
| + db_thread_(content::BrowserThread::DB, &loop_) { |
| +} |
| + |
| +ResourcePrefetchPredictorTablesTest::~ResourcePrefetchPredictorTablesTest() { |
| +} |
| + |
| +void ResourcePrefetchPredictorTablesTest::SetUp() { |
|
dominich
2012/06/06 17:09:09
you could do this in the constructor
Shishir
2012/06/06 23:55:33
Done
|
| + db_.reset(new PredictorDatabase(&profile_)); |
| + tables_ = db_->resource_prefetch_tables(); |
| + loop_.RunAllPending(); |
| + |
| + tables_->DeleteAllRows(); |
| + InitializeSampleUrlData(); |
| +} |
| + |
| +void ResourcePrefetchPredictorTablesTest::TearDown() { |
| + test_url_rows_.clear(); |
|
dominich
2012/06/06 17:09:09
no need to call clear
Shishir
2012/06/06 23:55:33
Removed.
|
| + tables_->DeleteAllRows(); |
|
dominich
2012/06/06 17:09:09
you could do this in the destructor, if at all.
Shishir
2012/06/06 23:55:33
Removed.
|
| + db_.reset(NULL); |
|
dominich
2012/06/06 17:09:09
should be no need to call this reset(NULL)
Shishir
2012/06/06 23:55:33
Done.
|
| +} |
| + |
| +void ResourcePrefetchPredictorTablesTest::TestGetAllRows() { |
| + ResourcePrefetchPredictorTables::UrlTableRows actual_rows; |
| + tables_->GetAllRows(&actual_rows); |
| + |
| + TestUrlRowVectorsAreEqual(test_url_rows_, actual_rows); |
| +} |
| + |
| +void ResourcePrefetchPredictorTablesTest::TestDeleteRowsForUrls() { |
| + std::vector<GURL> urls_to_delete; |
| + urls_to_delete.push_back(GURL("http://www.google.com")); |
| + urls_to_delete.push_back(GURL("http://www.yahoo.com")); |
| + |
| + tables_->DeleteRowsForUrls(urls_to_delete); |
| + |
| + ResourcePrefetchPredictorTables::UrlTableRows actual_rows; |
| + tables_->GetAllRows(&actual_rows); |
| + |
| + ResourcePrefetchPredictorTables::UrlTableRows expected_rows; |
| + expected_rows.push_back(test_url_rows_[5]); |
| + expected_rows.push_back(test_url_rows_[6]); |
| + |
| + TestUrlRowVectorsAreEqual(expected_rows, actual_rows); |
| +} |
| + |
| +void ResourcePrefetchPredictorTablesTest::TestUpdateRowsForUrl() { |
| + ResourcePrefetchPredictorTables::UrlTableRows new_rows; |
| + new_rows.push_back(CreateUrlTableRow( |
| + "http://www.google.com", |
| + "http://www.google.com/style.css", |
| + ResourceType::STYLESHEET, |
| + 6, 2, 0, 1.0)); |
| + new_rows.push_back(CreateUrlTableRow( |
| + "http://www.google.com", |
| + "http://www.google.com/image.png", |
| + ResourceType::IMAGE, |
| + 6, 4, 1, 4.2)); |
| + new_rows.push_back(CreateUrlTableRow( |
| + "http://www.google.com", |
| + "http://www.google.com/a.xml", |
| + ResourceType::LAST_TYPE, |
| + 1, 0, 0, 6.1)); |
| + new_rows.push_back(CreateUrlTableRow( |
| + "http://www.google.com", |
| + "http://www.resources.google.com/script.js", |
| + ResourceType::SCRIPT, |
| + 12, 0, 0, 8.5)); |
| + |
| + tables_->UpdateRowsForUrl(GURL("http://www.google.com"), new_rows); |
| + |
| + ResourcePrefetchPredictorTables::UrlTableRows actual_rows; |
| + tables_->GetAllRows(&actual_rows); |
| + |
| + ResourcePrefetchPredictorTables::UrlTableRows expected_rows(new_rows); |
| + expected_rows.push_back(test_url_rows_[5]); |
| + expected_rows.push_back(test_url_rows_[6]); |
| + expected_rows.push_back(test_url_rows_[7]); |
| + |
| + TestUrlRowVectorsAreEqual(expected_rows, actual_rows); |
| +} |
| + |
| +void ResourcePrefetchPredictorTablesTest::TestDeleteAllRows() { |
| + tables_->DeleteAllRows(); |
| + |
| + ResourcePrefetchPredictorTables::UrlTableRows actual_rows; |
| + tables_->GetAllRows(&actual_rows); |
| + EXPECT_EQ(0, static_cast<int>(actual_rows.size())); |
| +} |
| + |
| +void ResourcePrefetchPredictorTablesTest::TestUrlRowVectorsAreEqual( |
| + const ResourcePrefetchPredictorTables::UrlTableRows& lhs, |
| + const ResourcePrefetchPredictorTables::UrlTableRows& rhs) const { |
| + EXPECT_EQ(lhs.size(), rhs.size()); |
| + |
| + std::set<std::pair<GURL, GURL> > resources_seen; |
| + for (int i = 0; i < static_cast<int>(rhs.size()); ++i) { |
| + std::pair<GURL, GURL> resource_id = std::make_pair(rhs[i].main_frame_url, |
| + rhs[i].resource_url); |
| + EXPECT_EQ(resources_seen.find(resource_id), resources_seen.end()); |
| + |
| + for (int j = 0; j < static_cast<int>(lhs.size()); ++j) { |
| + if (UrlRowsAreEqual(rhs[i], lhs[j])) { |
| + resources_seen.insert(resource_id); |
| + break; |
| + } |
| + } |
| + EXPECT_NE(resources_seen.find(resource_id), resources_seen.end()); |
| + } |
| + EXPECT_EQ(lhs.size(), resources_seen.size()); |
| +} |
| + |
| +bool ResourcePrefetchPredictorTablesTest::UrlRowsAreEqual( |
| + const ResourcePrefetchPredictorTables::UrlTableRow& lhs, |
| + const ResourcePrefetchPredictorTables::UrlTableRow& rhs) const { |
| + return lhs.main_frame_url == rhs.main_frame_url && |
| + lhs.resource_url == rhs.resource_url && |
| + lhs.resource_type == rhs.resource_type && |
| + lhs.number_of_hits == rhs.number_of_hits && |
| + lhs.number_of_misses == rhs.number_of_misses && |
| + lhs.consecutive_misses == rhs.consecutive_misses && |
| + lhs.average_position == rhs.average_position && |
| + lhs.score == rhs.score; |
| +} |
| + |
| +void ResourcePrefetchPredictorTablesTest::InitializeSampleUrlData() { |
|
dominich
2012/06/06 17:09:09
clear test_url_rows_ here
Shishir
2012/06/06 23:55:33
Done.
|
| + test_url_rows_.push_back(CreateUrlTableRow( |
| + "http://www.google.com", |
| + "http://www.google.com/style.css", |
| + ResourceType::STYLESHEET, |
| + 5, 2, 1, 1.1)); |
| + test_url_rows_.push_back(CreateUrlTableRow( |
| + "http://www.google.com", |
| + "http://www.google.com/script.js", |
| + ResourceType::SCRIPT, |
| + 4, 0, 1, 2.1)); |
| + test_url_rows_.push_back(CreateUrlTableRow( |
| + "http://www.google.com", |
| + "http://www.google.com/image.png", |
| + ResourceType::IMAGE, |
| + 6, 3, 0, 2.2)); |
| + test_url_rows_.push_back(CreateUrlTableRow( |
| + "http://www.google.com", |
| + "http://www.google.com/a.font", |
| + ResourceType::LAST_TYPE, |
| + 2, 0, 0, 5.1)); |
| + test_url_rows_.push_back(CreateUrlTableRow( |
| + "http://www.google.com", |
| + "http://www.resources.google.com/script.js", |
| + ResourceType::SCRIPT, |
| + 11, 0, 0, 8.5)); |
| + |
| + test_url_rows_.push_back(CreateUrlTableRow( |
| + "http://www.reddit.com", |
| + "http://reddit-resource.com/script1.js", |
| + ResourceType::SCRIPT, |
| + 4, 0, 1, 1.0)); |
| + test_url_rows_.push_back(CreateUrlTableRow( |
| + "http://www.reddit.com", |
| + "http://reddit-resource.com/script2.js", |
| + ResourceType::SCRIPT, |
| + 2, 0, 0, 2.1)); |
| + |
| + test_url_rows_.push_back(CreateUrlTableRow( |
| + "http://www.yahoo.com", |
| + "http://www.google.com/image.png", |
| + ResourceType::IMAGE, |
| + 20, 1, 0, 10.0)); |
| + |
| + // Add all the rows to the table. |
| + tables_->UpdateRowsForUrl( |
| + GURL("http://www.google.com"), |
| + ResourcePrefetchPredictorTables::UrlTableRows( |
| + test_url_rows_.begin(), test_url_rows_.begin() + 5)); |
| + tables_->UpdateRowsForUrl( |
| + GURL("http://www.reddit.com"), |
| + ResourcePrefetchPredictorTables::UrlTableRows( |
| + test_url_rows_.begin() + 5, test_url_rows_.begin() + 7)); |
| + tables_->UpdateRowsForUrl( |
| + GURL("http://www.yahoo.com"), |
| + ResourcePrefetchPredictorTables::UrlTableRows( |
| + test_url_rows_.begin() + 7, test_url_rows_.begin() + 8)); |
| +} |
| + |
| +TEST_F(ResourcePrefetchPredictorTablesTest, GetAllRows) { |
| + TestGetAllRows(); |
| +} |
| + |
| +TEST_F(ResourcePrefetchPredictorTablesTest, UpdateRowsForUrl) { |
| + TestUpdateRowsForUrl(); |
| +} |
| + |
| +TEST_F(ResourcePrefetchPredictorTablesTest, DeleteRowsForUrls) { |
| + TestDeleteRowsForUrls(); |
| +} |
| + |
| +TEST_F(ResourcePrefetchPredictorTablesTest, DeleteAllRows) { |
| + TestDeleteAllRows(); |
| +} |
| + |
| +TEST_F(ResourcePrefetchPredictorTablesReopenTest, GetAllRows) { |
| + TestGetAllRows(); |
| +} |
| + |
| +TEST_F(ResourcePrefetchPredictorTablesReopenTest, UpdateRowsForUrl) { |
| + TestUpdateRowsForUrl(); |
| +} |
| + |
| +TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteRowsForUrls) { |
| + TestDeleteRowsForUrls(); |
| +} |
| + |
| +TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllRows) { |
| + TestDeleteAllRows(); |
| +} |
| + |
| +} // namespace predictors |