OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <set> |
| 6 #include <utility> |
| 7 #include <vector> |
| 8 |
| 9 #include "base/message_loop.h" |
| 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/predictors/predictor_database.h" |
| 12 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 13 #include "chrome/test/base/testing_profile.h" |
| 14 #include "content/public/test/test_browser_thread.h" |
| 15 #include "sql/statement.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace predictors { |
| 19 |
| 20 class ResourcePrefetchPredictorTablesTest : public testing::Test { |
| 21 public: |
| 22 ResourcePrefetchPredictorTablesTest(); |
| 23 virtual ~ResourcePrefetchPredictorTablesTest(); |
| 24 virtual void SetUp() OVERRIDE; |
| 25 |
| 26 protected: |
| 27 void TestGetAllRows(); |
| 28 void TestUpdateRowsForUrl(); |
| 29 void TestDeleteRowsForUrls(); |
| 30 void TestDeleteAllRows(); |
| 31 |
| 32 MessageLoop loop_; |
| 33 content::TestBrowserThread db_thread_; |
| 34 TestingProfile profile_; |
| 35 scoped_ptr<PredictorDatabase> db_; |
| 36 scoped_refptr<ResourcePrefetchPredictorTables> tables_; |
| 37 |
| 38 private: |
| 39 // Adds data to the URL table as well as test_url_rows_. |
| 40 void InitializeSampleUrlData(); |
| 41 |
| 42 // Checks that the input vectors have the same elements, although they may |
| 43 // be in different orders. |
| 44 void TestUrlRowVectorsAreEqual( |
| 45 const ResourcePrefetchPredictorTables::UrlTableRows& lhs, |
| 46 const ResourcePrefetchPredictorTables::UrlTableRows& rhs) const; |
| 47 |
| 48 ResourcePrefetchPredictorTables::UrlTableRows test_url_rows_; |
| 49 }; |
| 50 |
| 51 class ResourcePrefetchPredictorTablesReopenTest |
| 52 : public ResourcePrefetchPredictorTablesTest { |
| 53 public: |
| 54 virtual void SetUp() OVERRIDE { |
| 55 // Write data to the table, and then reopen the db. |
| 56 ResourcePrefetchPredictorTablesTest::SetUp(); |
| 57 tables_ = NULL; |
| 58 db_.reset(new PredictorDatabase(&profile_)); |
| 59 loop_.RunAllPending(); |
| 60 tables_ = db_->resource_prefetch_tables(); |
| 61 } |
| 62 }; |
| 63 |
| 64 ResourcePrefetchPredictorTablesTest::ResourcePrefetchPredictorTablesTest() |
| 65 : loop_(MessageLoop::TYPE_DEFAULT), |
| 66 db_thread_(content::BrowserThread::DB, &loop_), |
| 67 db_(new PredictorDatabase(&profile_)), |
| 68 tables_(db_->resource_prefetch_tables()) { |
| 69 loop_.RunAllPending(); |
| 70 } |
| 71 |
| 72 ResourcePrefetchPredictorTablesTest::~ResourcePrefetchPredictorTablesTest() { |
| 73 } |
| 74 |
| 75 void ResourcePrefetchPredictorTablesTest::SetUp() { |
| 76 tables_->DeleteAllRows(); |
| 77 InitializeSampleUrlData(); |
| 78 } |
| 79 |
| 80 void ResourcePrefetchPredictorTablesTest::TestGetAllRows() { |
| 81 ResourcePrefetchPredictorTables::UrlTableRows actual_rows; |
| 82 tables_->GetAllRows(&actual_rows); |
| 83 |
| 84 TestUrlRowVectorsAreEqual(test_url_rows_, actual_rows); |
| 85 } |
| 86 |
| 87 void ResourcePrefetchPredictorTablesTest::TestDeleteRowsForUrls() { |
| 88 std::vector<GURL> urls_to_delete; |
| 89 urls_to_delete.push_back(GURL("http://www.google.com")); |
| 90 urls_to_delete.push_back(GURL("http://www.yahoo.com")); |
| 91 |
| 92 tables_->DeleteRowsForUrls(urls_to_delete); |
| 93 |
| 94 ResourcePrefetchPredictorTables::UrlTableRows actual_rows; |
| 95 tables_->GetAllRows(&actual_rows); |
| 96 |
| 97 ResourcePrefetchPredictorTables::UrlTableRows expected_rows; |
| 98 expected_rows.push_back(test_url_rows_[5]); |
| 99 expected_rows.push_back(test_url_rows_[6]); |
| 100 |
| 101 TestUrlRowVectorsAreEqual(expected_rows, actual_rows); |
| 102 } |
| 103 |
| 104 void ResourcePrefetchPredictorTablesTest::TestUpdateRowsForUrl() { |
| 105 ResourcePrefetchPredictorTables::UrlTableRows new_rows; |
| 106 new_rows.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 107 "http://www.google.com", |
| 108 "http://www.google.com/style.css", |
| 109 ResourceType::STYLESHEET, |
| 110 6, 2, 0, 1.0)); |
| 111 new_rows.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 112 "http://www.google.com", |
| 113 "http://www.google.com/image.png", |
| 114 ResourceType::IMAGE, |
| 115 6, 4, 1, 4.2)); |
| 116 new_rows.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 117 "http://www.google.com", |
| 118 "http://www.google.com/a.xml", |
| 119 ResourceType::LAST_TYPE, |
| 120 1, 0, 0, 6.1)); |
| 121 new_rows.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 122 "http://www.google.com", |
| 123 "http://www.resources.google.com/script.js", |
| 124 ResourceType::SCRIPT, |
| 125 12, 0, 0, 8.5)); |
| 126 |
| 127 tables_->UpdateRowsForUrl(GURL("http://www.google.com"), new_rows); |
| 128 |
| 129 ResourcePrefetchPredictorTables::UrlTableRows actual_rows; |
| 130 tables_->GetAllRows(&actual_rows); |
| 131 |
| 132 ResourcePrefetchPredictorTables::UrlTableRows expected_rows(new_rows); |
| 133 expected_rows.push_back(test_url_rows_[5]); |
| 134 expected_rows.push_back(test_url_rows_[6]); |
| 135 expected_rows.push_back(test_url_rows_[7]); |
| 136 |
| 137 TestUrlRowVectorsAreEqual(expected_rows, actual_rows); |
| 138 } |
| 139 |
| 140 void ResourcePrefetchPredictorTablesTest::TestDeleteAllRows() { |
| 141 tables_->DeleteAllRows(); |
| 142 |
| 143 ResourcePrefetchPredictorTables::UrlTableRows actual_rows; |
| 144 tables_->GetAllRows(&actual_rows); |
| 145 EXPECT_EQ(0, static_cast<int>(actual_rows.size())); |
| 146 } |
| 147 |
| 148 void ResourcePrefetchPredictorTablesTest::TestUrlRowVectorsAreEqual( |
| 149 const ResourcePrefetchPredictorTables::UrlTableRows& lhs, |
| 150 const ResourcePrefetchPredictorTables::UrlTableRows& rhs) const { |
| 151 EXPECT_EQ(lhs.size(), rhs.size()); |
| 152 |
| 153 std::set<std::pair<GURL, GURL> > resources_seen; |
| 154 for (int i = 0; i < static_cast<int>(rhs.size()); ++i) { |
| 155 std::pair<GURL, GURL> resource_id = std::make_pair(rhs[i].main_frame_url, |
| 156 rhs[i].resource_url); |
| 157 EXPECT_TRUE(resources_seen.find(resource_id) == resources_seen.end()); |
| 158 |
| 159 for (int j = 0; j < static_cast<int>(lhs.size()); ++j) { |
| 160 if (rhs[i] == lhs[j]) { |
| 161 resources_seen.insert(resource_id); |
| 162 break; |
| 163 } |
| 164 } |
| 165 EXPECT_TRUE(resources_seen.find(resource_id) != resources_seen.end()); |
| 166 } |
| 167 EXPECT_EQ(lhs.size(), resources_seen.size()); |
| 168 } |
| 169 |
| 170 void ResourcePrefetchPredictorTablesTest::InitializeSampleUrlData() { |
| 171 test_url_rows_.clear(); |
| 172 test_url_rows_.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 173 "http://www.google.com", |
| 174 "http://www.google.com/style.css", |
| 175 ResourceType::STYLESHEET, |
| 176 5, 2, 1, 1.1)); |
| 177 test_url_rows_.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 178 "http://www.google.com", |
| 179 "http://www.google.com/script.js", |
| 180 ResourceType::SCRIPT, |
| 181 4, 0, 1, 2.1)); |
| 182 test_url_rows_.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 183 "http://www.google.com", |
| 184 "http://www.google.com/image.png", |
| 185 ResourceType::IMAGE, |
| 186 6, 3, 0, 2.2)); |
| 187 test_url_rows_.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 188 "http://www.google.com", |
| 189 "http://www.google.com/a.font", |
| 190 ResourceType::LAST_TYPE, |
| 191 2, 0, 0, 5.1)); |
| 192 test_url_rows_.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 193 "http://www.google.com", |
| 194 "http://www.resources.google.com/script.js", |
| 195 ResourceType::SCRIPT, |
| 196 11, 0, 0, 8.5)); |
| 197 |
| 198 test_url_rows_.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 199 "http://www.reddit.com", |
| 200 "http://reddit-resource.com/script1.js", |
| 201 ResourceType::SCRIPT, |
| 202 4, 0, 1, 1.0)); |
| 203 test_url_rows_.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 204 "http://www.reddit.com", |
| 205 "http://reddit-resource.com/script2.js", |
| 206 ResourceType::SCRIPT, |
| 207 2, 0, 0, 2.1)); |
| 208 |
| 209 test_url_rows_.push_back(ResourcePrefetchPredictorTables::UrlTableRow( |
| 210 "http://www.yahoo.com", |
| 211 "http://www.google.com/image.png", |
| 212 ResourceType::IMAGE, |
| 213 20, 1, 0, 10.0)); |
| 214 |
| 215 // Add all the rows to the table. |
| 216 tables_->UpdateRowsForUrl( |
| 217 GURL("http://www.google.com"), |
| 218 ResourcePrefetchPredictorTables::UrlTableRows( |
| 219 test_url_rows_.begin(), test_url_rows_.begin() + 5)); |
| 220 tables_->UpdateRowsForUrl( |
| 221 GURL("http://www.reddit.com"), |
| 222 ResourcePrefetchPredictorTables::UrlTableRows( |
| 223 test_url_rows_.begin() + 5, test_url_rows_.begin() + 7)); |
| 224 tables_->UpdateRowsForUrl( |
| 225 GURL("http://www.yahoo.com"), |
| 226 ResourcePrefetchPredictorTables::UrlTableRows( |
| 227 test_url_rows_.begin() + 7, test_url_rows_.begin() + 8)); |
| 228 } |
| 229 |
| 230 TEST_F(ResourcePrefetchPredictorTablesTest, GetAllRows) { |
| 231 TestGetAllRows(); |
| 232 } |
| 233 |
| 234 TEST_F(ResourcePrefetchPredictorTablesTest, UpdateRowsForUrl) { |
| 235 TestUpdateRowsForUrl(); |
| 236 } |
| 237 |
| 238 TEST_F(ResourcePrefetchPredictorTablesTest, DeleteRowsForUrls) { |
| 239 TestDeleteRowsForUrls(); |
| 240 } |
| 241 |
| 242 TEST_F(ResourcePrefetchPredictorTablesTest, DeleteAllRows) { |
| 243 TestDeleteAllRows(); |
| 244 } |
| 245 |
| 246 TEST_F(ResourcePrefetchPredictorTablesReopenTest, GetAllRows) { |
| 247 TestGetAllRows(); |
| 248 } |
| 249 |
| 250 TEST_F(ResourcePrefetchPredictorTablesReopenTest, UpdateRowsForUrl) { |
| 251 TestUpdateRowsForUrl(); |
| 252 } |
| 253 |
| 254 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteRowsForUrls) { |
| 255 TestDeleteRowsForUrls(); |
| 256 } |
| 257 |
| 258 TEST_F(ResourcePrefetchPredictorTablesReopenTest, DeleteAllRows) { |
| 259 TestDeleteAllRows(); |
| 260 } |
| 261 |
| 262 } // namespace predictors |
OLD | NEW |