| OLD | NEW |
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 "components/previews/core/previews_opt_out_store_sql.h" | 5 #include "components/previews/core/previews_opt_out_store_sql.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 const base::FilePath::CharType kOptOutFilename[] = FILE_PATH_LITERAL("OptOut"); | 33 const base::FilePath::CharType kOptOutFilename[] = FILE_PATH_LITERAL("OptOut"); |
| 34 | 34 |
| 35 } // namespace | 35 } // namespace |
| 36 | 36 |
| 37 class PreviewsOptOutStoreSQLTest : public testing::Test { | 37 class PreviewsOptOutStoreSQLTest : public testing::Test { |
| 38 public: | 38 public: |
| 39 PreviewsOptOutStoreSQLTest() {} | 39 PreviewsOptOutStoreSQLTest() {} |
| 40 ~PreviewsOptOutStoreSQLTest() override {} | 40 ~PreviewsOptOutStoreSQLTest() override {} |
| 41 | 41 |
| 42 // Called when |store_| is done loading. | 42 // Called when |store_| is done loading. |
| 43 void OnLoaded(std::unique_ptr<BlackListItemMap> black_list_map) { | 43 void OnLoaded(std::unique_ptr<BlackListItemMap> black_list_map, |
| 44 std::unique_ptr<PreviewsBlackListItem> host_indifferent_item) { |
| 44 black_list_map_ = std::move(black_list_map); | 45 black_list_map_ = std::move(black_list_map); |
| 46 host_indifferent_item_ = std::move(host_indifferent_item); |
| 45 } | 47 } |
| 46 | 48 |
| 47 // Initializes the store and get the data from it. | 49 // Initializes the store and get the data from it. |
| 48 void Load() { | 50 void Load() { |
| 49 store_->LoadBlackList(base::Bind(&PreviewsOptOutStoreSQLTest::OnLoaded, | 51 store_->LoadBlackList(base::Bind(&PreviewsOptOutStoreSQLTest::OnLoaded, |
| 50 base::Unretained(this))); | 52 base::Unretained(this))); |
| 51 base::RunLoop().RunUntilIdle(); | 53 base::RunLoop().RunUntilIdle(); |
| 52 } | 54 } |
| 53 | 55 |
| 54 // Destroys the database connection and |store_|. | 56 // Destroys the database connection and |store_|. |
| (...skipping 26 matching lines...) Expand all Loading... |
| 81 base::HistogramTester histogram_tester_; | 83 base::HistogramTester histogram_tester_; |
| 82 | 84 |
| 83 base::MessageLoop message_loop_; | 85 base::MessageLoop message_loop_; |
| 84 | 86 |
| 85 // The backing SQL store. | 87 // The backing SQL store. |
| 86 std::unique_ptr<PreviewsOptOutStoreSQL> store_; | 88 std::unique_ptr<PreviewsOptOutStoreSQL> store_; |
| 87 | 89 |
| 88 // The map returned from |store_|. | 90 // The map returned from |store_|. |
| 89 std::unique_ptr<BlackListItemMap> black_list_map_; | 91 std::unique_ptr<BlackListItemMap> black_list_map_; |
| 90 | 92 |
| 93 // The host indifferent item from |store_| |
| 94 std::unique_ptr<PreviewsBlackListItem> host_indifferent_item_; |
| 95 |
| 91 // The directory for the database. | 96 // The directory for the database. |
| 92 base::ScopedTempDir temp_dir_; | 97 base::ScopedTempDir temp_dir_; |
| 93 }; | 98 }; |
| 94 | 99 |
| 95 TEST_F(PreviewsOptOutStoreSQLTest, TestErrorRecovery) { | 100 TEST_F(PreviewsOptOutStoreSQLTest, TestErrorRecovery) { |
| 96 // Creates the database and corrupt to test the recovery method. | 101 // Creates the database and corrupt to test the recovery method. |
| 97 std::string test_host = "host.com"; | 102 std::string test_host = "host.com"; |
| 98 CreateAndLoad(); | 103 CreateAndLoad(); |
| 99 store_->AddPreviewNavigation(true, test_host, PreviewsType::OFFLINE, | 104 store_->AddPreviewNavigation(true, test_host, PreviewsType::OFFLINE, |
| 100 base::Time::Now()); | 105 base::Time::Now()); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 129 DestroyStore(); | 134 DestroyStore(); |
| 130 | 135 |
| 131 // Reload and test for persistence | 136 // Reload and test for persistence |
| 132 CreateAndLoad(); | 137 CreateAndLoad(); |
| 133 EXPECT_EQ(1U, black_list_map_->size()); | 138 EXPECT_EQ(1U, black_list_map_->size()); |
| 134 auto iter = black_list_map_->find(test_host); | 139 auto iter = black_list_map_->find(test_host); |
| 135 | 140 |
| 136 EXPECT_NE(black_list_map_->end(), iter); | 141 EXPECT_NE(black_list_map_->end(), iter); |
| 137 EXPECT_EQ(1U, iter->second->OptOutRecordsSizeForTesting()); | 142 EXPECT_EQ(1U, iter->second->OptOutRecordsSizeForTesting()); |
| 138 EXPECT_EQ(now, iter->second->most_recent_opt_out_time().value()); | 143 EXPECT_EQ(now, iter->second->most_recent_opt_out_time().value()); |
| 144 EXPECT_EQ(1U, host_indifferent_item_->OptOutRecordsSizeForTesting()); |
| 145 EXPECT_EQ(now, host_indifferent_item_->most_recent_opt_out_time().value()); |
| 139 histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount", 1, 1); | 146 histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount", 1, 1); |
| 140 histogram_tester_.ExpectTotalCount("Previews.OptOut.DBRowCount", 2); | 147 histogram_tester_.ExpectTotalCount("Previews.OptOut.DBRowCount", 2); |
| 141 } | 148 } |
| 142 | 149 |
| 143 TEST_F(PreviewsOptOutStoreSQLTest, TestMaxRows) { | 150 TEST_F(PreviewsOptOutStoreSQLTest, TestMaxRows) { |
| 144 // Tests that the number of rows are culled down to the row limit at each | 151 // Tests that the number of rows are culled down to the row limit at each |
| 145 // load. | 152 // load. |
| 146 std::string test_host_a = "host_a.com"; | 153 std::string test_host_a = "host_a.com"; |
| 147 std::string test_host_b = "host_b.com"; | 154 std::string test_host_b = "host_b.com"; |
| 148 std::string test_host_c = "host_c.com"; | 155 std::string test_host_c = "host_c.com"; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 172 // to write its data to disk. | 179 // to write its data to disk. |
| 173 DestroyStore(); | 180 DestroyStore(); |
| 174 | 181 |
| 175 // Reload and test for persistence | 182 // Reload and test for persistence |
| 176 CreateAndLoad(); | 183 CreateAndLoad(); |
| 177 histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount", | 184 histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount", |
| 178 static_cast<int>(row_limit) + 1, 1); | 185 static_cast<int>(row_limit) + 1, 1); |
| 179 // The delete happens after the load, so it is possible to load more than | 186 // The delete happens after the load, so it is possible to load more than |
| 180 // |row_limit| into the in memory map. | 187 // |row_limit| into the in memory map. |
| 181 EXPECT_EQ(row_limit + 1, black_list_map_->size()); | 188 EXPECT_EQ(row_limit + 1, black_list_map_->size()); |
| 189 EXPECT_EQ(row_limit + 1, |
| 190 host_indifferent_item_->OptOutRecordsSizeForTesting()); |
| 182 | 191 |
| 183 DestroyStore(); | 192 DestroyStore(); |
| 184 CreateAndLoad(); | 193 CreateAndLoad(); |
| 185 histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount", | 194 histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount", |
| 186 static_cast<int>(row_limit), 1); | 195 static_cast<int>(row_limit), 1); |
| 187 | 196 |
| 188 EXPECT_EQ(row_limit, black_list_map_->size()); | 197 EXPECT_EQ(row_limit, black_list_map_->size()); |
| 189 auto iter_host_b = black_list_map_->find(test_host_b); | 198 auto iter_host_b = black_list_map_->find(test_host_b); |
| 190 auto iter_host_c = black_list_map_->find(test_host_c); | 199 auto iter_host_c = black_list_map_->find(test_host_c); |
| 191 | 200 |
| 192 EXPECT_EQ(black_list_map_->end(), black_list_map_->find(test_host_a)); | 201 EXPECT_EQ(black_list_map_->end(), black_list_map_->find(test_host_a)); |
| 193 EXPECT_NE(black_list_map_->end(), iter_host_b); | 202 EXPECT_NE(black_list_map_->end(), iter_host_b); |
| 194 EXPECT_NE(black_list_map_->end(), iter_host_c); | 203 EXPECT_NE(black_list_map_->end(), iter_host_c); |
| 195 EXPECT_EQ(host_b_time, | 204 EXPECT_EQ(host_b_time, |
| 196 iter_host_b->second->most_recent_opt_out_time().value()); | 205 iter_host_b->second->most_recent_opt_out_time().value()); |
| 197 EXPECT_EQ(1U, iter_host_b->second->OptOutRecordsSizeForTesting()); | 206 EXPECT_EQ(1U, iter_host_b->second->OptOutRecordsSizeForTesting()); |
| 207 EXPECT_EQ(host_b_time, |
| 208 host_indifferent_item_->most_recent_opt_out_time().value()); |
| 209 EXPECT_EQ(row_limit, host_indifferent_item_->OptOutRecordsSizeForTesting()); |
| 198 histogram_tester_.ExpectTotalCount("Previews.OptOut.DBRowCount", 3); | 210 histogram_tester_.ExpectTotalCount("Previews.OptOut.DBRowCount", 3); |
| 199 } | 211 } |
| 200 | 212 |
| 201 TEST_F(PreviewsOptOutStoreSQLTest, TestMaxRowsPerHost) { | 213 TEST_F(PreviewsOptOutStoreSQLTest, TestMaxRowsPerHost) { |
| 202 // Tests that each host is limited to |row_limit| rows. | 214 // Tests that each host is limited to |row_limit| rows. |
| 203 std::string test_host = "host.com"; | 215 std::string test_host = "host.com"; |
| 204 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 216 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
| 205 size_t row_limit = 2; | 217 size_t row_limit = 2; |
| 206 std::string row_limit_string = base::SizeTToString(row_limit); | 218 std::string row_limit_string = base::SizeTToString(row_limit); |
| 207 command_line->AppendSwitchASCII("previews-max-opt-out-rows-per-host", | 219 command_line->AppendSwitchASCII("previews-max-opt-out-rows-per-host", |
| (...skipping 24 matching lines...) Expand all Loading... |
| 232 histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount", | 244 histogram_tester_.ExpectBucketCount("Previews.OptOut.DBRowCount", |
| 233 static_cast<int>(row_limit), 1); | 245 static_cast<int>(row_limit), 1); |
| 234 | 246 |
| 235 EXPECT_EQ(1U, black_list_map_->size()); | 247 EXPECT_EQ(1U, black_list_map_->size()); |
| 236 auto iter = black_list_map_->find(test_host); | 248 auto iter = black_list_map_->find(test_host); |
| 237 | 249 |
| 238 EXPECT_NE(black_list_map_->end(), iter); | 250 EXPECT_NE(black_list_map_->end(), iter); |
| 239 EXPECT_EQ(last_opt_out_time, | 251 EXPECT_EQ(last_opt_out_time, |
| 240 iter->second->most_recent_opt_out_time().value()); | 252 iter->second->most_recent_opt_out_time().value()); |
| 241 EXPECT_EQ(row_limit, iter->second->OptOutRecordsSizeForTesting()); | 253 EXPECT_EQ(row_limit, iter->second->OptOutRecordsSizeForTesting()); |
| 254 EXPECT_EQ(row_limit, host_indifferent_item_->OptOutRecordsSizeForTesting()); |
| 242 clock.Advance(base::TimeDelta::FromSeconds(1)); | 255 clock.Advance(base::TimeDelta::FromSeconds(1)); |
| 243 // If both entries' opt out states are stored correctly, then this should not | 256 // If both entries' opt out states are stored correctly, then this should not |
| 244 // be black listed. | 257 // be black listed. |
| 245 EXPECT_FALSE(iter->second->IsBlackListed(clock.Now())); | 258 EXPECT_FALSE(iter->second->IsBlackListed(clock.Now())); |
| 246 histogram_tester_.ExpectTotalCount("Previews.OptOut.DBRowCount", 2); | 259 histogram_tester_.ExpectTotalCount("Previews.OptOut.DBRowCount", 2); |
| 247 } | 260 } |
| 248 | 261 |
| 249 } // namespace net | 262 } // namespace net |
| OLD | NEW |