ExtensionInstallDialogView: fix scrolling behavior on Views (Win,Linux)
[chromium-blink-merge.git] / components / precache / core / precache_database_unittest.cc
blobe3c6534a0b8c341f9e753db0b364eeaa3ad635cb
1 // Copyright 2013 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.
5 #include "components/precache/core/precache_database.h"
7 #include <map>
9 #include "base/files/file_path.h"
10 #include "base/files/scoped_temp_dir.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/metrics/histogram.h"
13 #include "base/metrics/histogram_samples.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "base/time/time.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "url/gurl.h"
19 namespace {
21 const GURL kURL("http://url.com");
22 const base::Time kFetchTime = base::Time() + base::TimeDelta::FromHours(1000);
23 const base::Time kOldFetchTime = kFetchTime - base::TimeDelta::FromDays(1);
24 const int64 kSize = 5000;
26 const char* kHistogramNames[] = {"Precache.DownloadedPrecacheMotivated",
27 "Precache.DownloadedNonPrecache",
28 "Precache.DownloadedNonPrecache.Cellular",
29 "Precache.Saved",
30 "Precache.Saved.Cellular"};
32 scoped_ptr<base::HistogramSamples> GetHistogramSamples(
33 const char* histogram_name) {
34 base::HistogramBase* histogram =
35 base::StatisticsRecorder::FindHistogram(histogram_name);
37 EXPECT_NE(static_cast<base::HistogramBase*>(NULL), histogram);
39 return histogram->SnapshotSamples().Pass();
42 std::map<GURL, base::Time> BuildURLTableMap(const GURL& url,
43 const base::Time& precache_time) {
44 std::map<GURL, base::Time> url_table_map;
45 url_table_map[url] = precache_time;
46 return url_table_map;
49 } // namespace
51 namespace precache {
53 class PrecacheDatabaseTest : public testing::Test {
54 public:
55 PrecacheDatabaseTest() {}
56 ~PrecacheDatabaseTest() override {}
58 protected:
59 void SetUp() override {
60 base::StatisticsRecorder::Initialize();
61 precache_database_ = new PrecacheDatabase();
63 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
64 base::FilePath db_path = scoped_temp_dir_.path().Append(
65 base::FilePath(FILE_PATH_LITERAL("precache_database")));
66 precache_database_->Init(db_path);
68 // Log a sample for each histogram, to ensure that they are all created.
69 // This has to be done here, and not in the for loop below, because of the
70 // way that UMA_HISTOGRAM_COUNTS uses static variables.
71 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", 0);
72 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", 0);
73 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", 0);
74 UMA_HISTOGRAM_COUNTS("Precache.Saved", 0);
75 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", 0);
77 for (size_t i = 0; i < arraysize(kHistogramNames); i++) {
78 initial_histogram_samples_[i] =
79 GetHistogramSamples(kHistogramNames[i]).Pass();
80 initial_histogram_samples_map_[kHistogramNames[i]] =
81 initial_histogram_samples_[i].get();
85 std::map<GURL, base::Time> GetActualURLTableMap() {
86 // Flush any buffered writes so that the URL table will be up to date.
87 precache_database_->Flush();
89 std::map<GURL, base::Time> url_table_map;
90 precache_url_table()->GetAllDataForTesting(&url_table_map);
91 return url_table_map;
94 PrecacheURLTable* precache_url_table() {
95 return &precache_database_->precache_url_table_;
98 scoped_ptr<base::HistogramSamples> GetHistogramSamplesDelta(
99 const char* histogram_name) {
100 scoped_ptr<base::HistogramSamples> delta_samples(
101 GetHistogramSamples(histogram_name));
102 delta_samples->Subtract(*initial_histogram_samples_map_[histogram_name]);
104 return delta_samples.Pass();
107 void ExpectNewSample(const char* histogram_name,
108 base::HistogramBase::Sample sample) {
109 scoped_ptr<base::HistogramSamples> delta_samples(
110 GetHistogramSamplesDelta(histogram_name));
111 EXPECT_EQ(1, delta_samples->TotalCount());
112 EXPECT_EQ(1, delta_samples->GetCount(sample));
115 void ExpectNoNewSamples(const char* histogram_name) {
116 scoped_ptr<base::HistogramSamples> delta_samples(
117 GetHistogramSamplesDelta(histogram_name));
118 EXPECT_EQ(0, delta_samples->TotalCount());
121 // Convenience methods for recording different types of URL fetches. These
122 // exist to improve the readability of the tests.
123 void RecordPrecacheFromNetwork(const GURL& url, const base::Time& fetch_time,
124 int64 size);
125 void RecordPrecacheFromCache(const GURL& url, const base::Time& fetch_time,
126 int64 size);
127 void RecordFetchFromNetwork(const GURL& url, const base::Time& fetch_time,
128 int64 size);
129 void RecordFetchFromNetworkCellular(const GURL& url,
130 const base::Time& fetch_time, int64 size);
131 void RecordFetchFromCache(const GURL& url, const base::Time& fetch_time,
132 int64 size);
133 void RecordFetchFromCacheCellular(const GURL& url,
134 const base::Time& fetch_time, int64 size);
136 // Having this MessageLoop member variable causes base::MessageLoop::current()
137 // to be set properly.
138 base::MessageLoopForUI loop_;
140 scoped_refptr<PrecacheDatabase> precache_database_;
141 base::ScopedTempDir scoped_temp_dir_;
142 scoped_ptr<base::HistogramSamples> initial_histogram_samples_
143 [arraysize(kHistogramNames)];
144 std::map<std::string, base::HistogramSamples*> initial_histogram_samples_map_;
147 void PrecacheDatabaseTest::RecordPrecacheFromNetwork(
148 const GURL& url, const base::Time& fetch_time, int64 size) {
149 precache_database_->RecordURLPrecached(url, fetch_time, size,
150 false /* was_cached */);
153 void PrecacheDatabaseTest::RecordPrecacheFromCache(const GURL& url,
154 const base::Time& fetch_time,
155 int64 size) {
156 precache_database_->RecordURLPrecached(url, fetch_time, size,
157 true /* was_cached */);
160 void PrecacheDatabaseTest::RecordFetchFromNetwork(const GURL& url,
161 const base::Time& fetch_time,
162 int64 size) {
163 precache_database_->RecordURLFetched(url, fetch_time, size,
164 false /* was_cached */,
165 false /* is_connection_cellular */);
168 void PrecacheDatabaseTest::RecordFetchFromNetworkCellular(
169 const GURL& url, const base::Time& fetch_time, int64 size) {
170 precache_database_->RecordURLFetched(url, fetch_time, size,
171 false /* was_cached */,
172 true /* is_connection_cellular */);
175 void PrecacheDatabaseTest::RecordFetchFromCache(const GURL& url,
176 const base::Time& fetch_time,
177 int64 size) {
178 precache_database_->RecordURLFetched(url, fetch_time, size,
179 true /* was_cached */,
180 false /* is_connection_cellular */);
183 void PrecacheDatabaseTest::RecordFetchFromCacheCellular(
184 const GURL& url, const base::Time& fetch_time, int64 size) {
185 precache_database_->RecordURLFetched(url, fetch_time, size,
186 true /* was_cached */,
187 true /* is_connection_cellular */);
190 namespace {
192 TEST_F(PrecacheDatabaseTest, PrecacheOverNetwork) {
193 RecordPrecacheFromNetwork(kURL, kFetchTime, kSize);
195 EXPECT_EQ(BuildURLTableMap(kURL, kFetchTime), GetActualURLTableMap());
197 ExpectNewSample("Precache.DownloadedPrecacheMotivated", kSize);
198 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
199 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
200 ExpectNoNewSamples("Precache.Saved");
201 ExpectNoNewSamples("Precache.Saved.Cellular");
204 TEST_F(PrecacheDatabaseTest, PrecacheFromCacheWithURLTableEntry) {
205 precache_url_table()->AddURL(kURL, kOldFetchTime);
206 RecordPrecacheFromCache(kURL, kFetchTime, kSize);
208 // The URL table entry should have been updated to have |kFetchTime| as the
209 // timestamp.
210 EXPECT_EQ(BuildURLTableMap(kURL, kFetchTime), GetActualURLTableMap());
212 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
213 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
214 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
215 ExpectNoNewSamples("Precache.Saved");
216 ExpectNoNewSamples("Precache.Saved.Cellular");
219 TEST_F(PrecacheDatabaseTest, PrecacheFromCacheWithoutURLTableEntry) {
220 RecordPrecacheFromCache(kURL, kFetchTime, kSize);
222 EXPECT_TRUE(GetActualURLTableMap().empty());
224 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
225 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
226 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
227 ExpectNoNewSamples("Precache.Saved");
228 ExpectNoNewSamples("Precache.Saved.Cellular");
231 TEST_F(PrecacheDatabaseTest, FetchOverNetwork_NonCellular) {
232 RecordFetchFromNetwork(kURL, kFetchTime, kSize);
234 EXPECT_TRUE(GetActualURLTableMap().empty());
236 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
237 ExpectNewSample("Precache.DownloadedNonPrecache", kSize);
238 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
239 ExpectNoNewSamples("Precache.Saved");
240 ExpectNoNewSamples("Precache.Saved.Cellular");
243 TEST_F(PrecacheDatabaseTest, FetchOverNetwork_Cellular) {
244 RecordFetchFromNetworkCellular(kURL, kFetchTime, kSize);
246 EXPECT_TRUE(GetActualURLTableMap().empty());
248 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
249 ExpectNewSample("Precache.DownloadedNonPrecache", kSize);
250 ExpectNewSample("Precache.DownloadedNonPrecache.Cellular", kSize);
251 ExpectNoNewSamples("Precache.Saved");
252 ExpectNoNewSamples("Precache.Saved.Cellular");
255 TEST_F(PrecacheDatabaseTest, FetchOverNetworkWithURLTableEntry) {
256 precache_url_table()->AddURL(kURL, kOldFetchTime);
257 RecordFetchFromNetwork(kURL, kFetchTime, kSize);
259 // The URL table entry should have been deleted.
260 EXPECT_TRUE(GetActualURLTableMap().empty());
262 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
263 ExpectNewSample("Precache.DownloadedNonPrecache", kSize);
264 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
265 ExpectNoNewSamples("Precache.Saved");
266 ExpectNoNewSamples("Precache.Saved.Cellular");
269 TEST_F(PrecacheDatabaseTest, FetchFromCacheWithURLTableEntry_NonCellular) {
270 precache_url_table()->AddURL(kURL, kOldFetchTime);
271 RecordFetchFromCache(kURL, kFetchTime, kSize);
273 // The URL table entry should have been deleted.
274 EXPECT_TRUE(GetActualURLTableMap().empty());
276 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
277 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
278 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
279 ExpectNewSample("Precache.Saved", kSize);
280 ExpectNoNewSamples("Precache.Saved.Cellular");
283 TEST_F(PrecacheDatabaseTest, FetchFromCacheWithURLTableEntry_Cellular) {
284 precache_url_table()->AddURL(kURL, kOldFetchTime);
285 RecordFetchFromCacheCellular(kURL, kFetchTime, kSize);
287 // The URL table entry should have been deleted.
288 EXPECT_TRUE(GetActualURLTableMap().empty());
290 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
291 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
292 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
293 ExpectNewSample("Precache.Saved", kSize);
294 ExpectNewSample("Precache.Saved.Cellular", kSize);
297 TEST_F(PrecacheDatabaseTest, FetchFromCacheWithoutURLTableEntry) {
298 RecordFetchFromCache(kURL, kFetchTime, kSize);
300 EXPECT_TRUE(GetActualURLTableMap().empty());
302 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
303 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
304 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
305 ExpectNoNewSamples("Precache.Saved");
306 ExpectNoNewSamples("Precache.Saved.Cellular");
309 TEST_F(PrecacheDatabaseTest, DeleteExpiredPrecacheHistory) {
310 const base::Time kToday = base::Time() + base::TimeDelta::FromDays(1000);
311 const base::Time k59DaysAgo = kToday - base::TimeDelta::FromDays(59);
312 const base::Time k61DaysAgo = kToday - base::TimeDelta::FromDays(61);
314 precache_url_table()->AddURL(GURL("http://expired-precache.com"), k61DaysAgo);
315 precache_url_table()->AddURL(GURL("http://old-precache.com"), k59DaysAgo);
317 precache_database_->DeleteExpiredPrecacheHistory(kToday);
319 EXPECT_EQ(BuildURLTableMap(GURL("http://old-precache.com"), k59DaysAgo),
320 GetActualURLTableMap());
323 TEST_F(PrecacheDatabaseTest, SampleInteraction) {
324 const GURL kURL1("http://url1.com");
325 const int64 kSize1 = 1000;
326 const GURL kURL2("http://url2.com");
327 const int64 kSize2 = 2000;
328 const GURL kURL3("http://url3.com");
329 const int64 kSize3 = 3000;
330 const GURL kURL4("http://url4.com");
331 const int64 kSize4 = 4000;
332 const GURL kURL5("http://url5.com");
333 const int64 kSize5 = 5000;
335 RecordPrecacheFromNetwork(kURL1, kFetchTime, kSize1);
336 RecordPrecacheFromNetwork(kURL2, kFetchTime, kSize2);
337 RecordPrecacheFromNetwork(kURL3, kFetchTime, kSize3);
338 RecordPrecacheFromNetwork(kURL4, kFetchTime, kSize4);
340 RecordFetchFromCacheCellular(kURL1, kFetchTime, kSize1);
341 RecordFetchFromCacheCellular(kURL1, kFetchTime, kSize1);
342 RecordFetchFromNetworkCellular(kURL2, kFetchTime, kSize2);
343 RecordFetchFromNetworkCellular(kURL5, kFetchTime, kSize5);
344 RecordFetchFromCacheCellular(kURL5, kFetchTime, kSize5);
346 RecordPrecacheFromCache(kURL1, kFetchTime, kSize1);
347 RecordPrecacheFromNetwork(kURL2, kFetchTime, kSize2);
348 RecordPrecacheFromCache(kURL3, kFetchTime, kSize3);
349 RecordPrecacheFromCache(kURL4, kFetchTime, kSize4);
351 RecordFetchFromCache(kURL1, kFetchTime, kSize1);
352 RecordFetchFromNetwork(kURL2, kFetchTime, kSize2);
353 RecordFetchFromCache(kURL3, kFetchTime, kSize3);
354 RecordFetchFromCache(kURL5, kFetchTime, kSize5);
356 scoped_ptr<base::HistogramSamples> downloaded_precache_motivated_bytes(
357 GetHistogramSamplesDelta("Precache.DownloadedPrecacheMotivated"));
358 EXPECT_EQ(5, downloaded_precache_motivated_bytes->TotalCount());
359 EXPECT_EQ(1, downloaded_precache_motivated_bytes->GetCount(kSize1));
360 EXPECT_EQ(2, downloaded_precache_motivated_bytes->GetCount(kSize2));
361 EXPECT_EQ(1, downloaded_precache_motivated_bytes->GetCount(kSize3));
362 EXPECT_EQ(1, downloaded_precache_motivated_bytes->GetCount(kSize4));
364 scoped_ptr<base::HistogramSamples> downloaded_non_precache_bytes(
365 GetHistogramSamplesDelta("Precache.DownloadedNonPrecache"));
366 EXPECT_EQ(3, downloaded_non_precache_bytes->TotalCount());
367 EXPECT_EQ(2, downloaded_non_precache_bytes->GetCount(kSize2));
368 EXPECT_EQ(1, downloaded_non_precache_bytes->GetCount(kSize5));
370 scoped_ptr<base::HistogramSamples> downloaded_non_precache_bytes_cellular(
371 GetHistogramSamplesDelta("Precache.DownloadedNonPrecache.Cellular"));
372 EXPECT_EQ(2, downloaded_non_precache_bytes_cellular->TotalCount());
373 EXPECT_EQ(1, downloaded_non_precache_bytes_cellular->GetCount(kSize2));
374 EXPECT_EQ(1, downloaded_non_precache_bytes_cellular->GetCount(kSize5));
376 scoped_ptr<base::HistogramSamples> saved_bytes(
377 GetHistogramSamplesDelta("Precache.Saved"));
378 EXPECT_EQ(2, saved_bytes->TotalCount());
379 EXPECT_EQ(1, saved_bytes->GetCount(kSize1));
380 EXPECT_EQ(1, saved_bytes->GetCount(kSize3));
382 scoped_ptr<base::HistogramSamples> saved_bytes_cellular(
383 GetHistogramSamplesDelta("Precache.Saved.Cellular"));
384 EXPECT_EQ(1, saved_bytes_cellular->TotalCount());
385 EXPECT_EQ(1, saved_bytes_cellular->GetCount(kSize1));
388 } // namespace
390 } // namespace precache