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"
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 "sql/connection.h"
17 #include "testing/gtest/include/gtest/gtest.h"
22 const GURL
kURL("http://url.com");
23 const base::Time kFetchTime
= base::Time() + base::TimeDelta::FromHours(1000);
24 const base::Time kOldFetchTime
= kFetchTime
- base::TimeDelta::FromDays(1);
25 const int64 kSize
= 5000;
27 const char* kHistogramNames
[] = {"Precache.DownloadedPrecacheMotivated",
28 "Precache.DownloadedNonPrecache",
29 "Precache.DownloadedNonPrecache.Cellular",
31 "Precache.Saved.Cellular"};
33 scoped_ptr
<base::HistogramSamples
> GetHistogramSamples(
34 const char* histogram_name
) {
35 base::HistogramBase
* histogram
=
36 base::StatisticsRecorder::FindHistogram(histogram_name
);
38 EXPECT_NE(static_cast<base::HistogramBase
*>(NULL
), histogram
);
40 return histogram
->SnapshotSamples().Pass();
43 std::map
<GURL
, base::Time
> BuildURLTableMap(const GURL
& url
,
44 const base::Time
& precache_time
) {
45 std::map
<GURL
, base::Time
> url_table_map
;
46 url_table_map
[url
] = precache_time
;
54 class PrecacheDatabaseTest
: public testing::Test
{
56 PrecacheDatabaseTest() {}
57 virtual ~PrecacheDatabaseTest() {}
60 virtual void SetUp() OVERRIDE
{
61 base::StatisticsRecorder::Initialize();
62 precache_database_
= new PrecacheDatabase();
64 ASSERT_TRUE(scoped_temp_dir_
.CreateUniqueTempDir());
65 base::FilePath db_path
= scoped_temp_dir_
.path().Append(
66 base::FilePath(FILE_PATH_LITERAL("precache_database")));
67 precache_database_
->Init(db_path
);
69 // Log a sample for each histogram, to ensure that they are all created.
70 // This has to be done here, and not in the for loop below, because of the
71 // way that UMA_HISTOGRAM_COUNTS uses static variables.
72 UMA_HISTOGRAM_COUNTS("Precache.DownloadedPrecacheMotivated", 0);
73 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache", 0);
74 UMA_HISTOGRAM_COUNTS("Precache.DownloadedNonPrecache.Cellular", 0);
75 UMA_HISTOGRAM_COUNTS("Precache.Saved", 0);
76 UMA_HISTOGRAM_COUNTS("Precache.Saved.Cellular", 0);
78 for (size_t i
= 0; i
< arraysize(kHistogramNames
); i
++) {
79 initial_histogram_samples_
[i
] =
80 GetHistogramSamples(kHistogramNames
[i
]).Pass();
81 initial_histogram_samples_map_
[kHistogramNames
[i
]] =
82 initial_histogram_samples_
[i
].get();
86 std::map
<GURL
, base::Time
> GetActualURLTableMap() {
87 // Flush any buffered writes so that the URL table will be up to date.
88 precache_database_
->Flush();
90 std::map
<GURL
, base::Time
> url_table_map
;
91 precache_url_table()->GetAllDataForTesting(&url_table_map
);
95 PrecacheURLTable
* precache_url_table() {
96 return &precache_database_
->precache_url_table_
;
99 scoped_ptr
<base::HistogramSamples
> GetHistogramSamplesDelta(
100 const char* histogram_name
) {
101 scoped_ptr
<base::HistogramSamples
> delta_samples(
102 GetHistogramSamples(histogram_name
));
103 delta_samples
->Subtract(*initial_histogram_samples_map_
[histogram_name
]);
105 return delta_samples
.Pass();
108 void ExpectNewSample(const char* histogram_name
,
109 base::HistogramBase::Sample sample
) {
110 scoped_ptr
<base::HistogramSamples
> delta_samples(
111 GetHistogramSamplesDelta(histogram_name
));
112 EXPECT_EQ(1, delta_samples
->TotalCount());
113 EXPECT_EQ(1, delta_samples
->GetCount(sample
));
116 void ExpectNoNewSamples(const char* histogram_name
) {
117 scoped_ptr
<base::HistogramSamples
> delta_samples(
118 GetHistogramSamplesDelta(histogram_name
));
119 EXPECT_EQ(0, delta_samples
->TotalCount());
122 // Convenience methods for recording different types of URL fetches. These
123 // exist to improve the readability of the tests.
124 void RecordPrecacheFromNetwork(const GURL
& url
, const base::Time
& fetch_time
,
126 void RecordPrecacheFromCache(const GURL
& url
, const base::Time
& fetch_time
,
128 void RecordFetchFromNetwork(const GURL
& url
, const base::Time
& fetch_time
,
130 void RecordFetchFromNetworkCellular(const GURL
& url
,
131 const base::Time
& fetch_time
, int64 size
);
132 void RecordFetchFromCache(const GURL
& url
, const base::Time
& fetch_time
,
134 void RecordFetchFromCacheCellular(const GURL
& url
,
135 const base::Time
& fetch_time
, int64 size
);
137 // Having this MessageLoop member variable causes base::MessageLoop::current()
138 // to be set properly.
139 base::MessageLoopForUI loop_
;
141 scoped_refptr
<PrecacheDatabase
> precache_database_
;
142 base::ScopedTempDir scoped_temp_dir_
;
143 scoped_ptr
<base::HistogramSamples
> initial_histogram_samples_
144 [arraysize(kHistogramNames
)];
145 std::map
<std::string
, base::HistogramSamples
*> initial_histogram_samples_map_
;
148 void PrecacheDatabaseTest::RecordPrecacheFromNetwork(
149 const GURL
& url
, const base::Time
& fetch_time
, int64 size
) {
150 precache_database_
->RecordURLPrecached(url
, fetch_time
, size
,
151 false /* was_cached */);
154 void PrecacheDatabaseTest::RecordPrecacheFromCache(const GURL
& url
,
155 const base::Time
& fetch_time
,
157 precache_database_
->RecordURLPrecached(url
, fetch_time
, size
,
158 true /* was_cached */);
161 void PrecacheDatabaseTest::RecordFetchFromNetwork(const GURL
& url
,
162 const base::Time
& fetch_time
,
164 precache_database_
->RecordURLFetched(url
, fetch_time
, size
,
165 false /* was_cached */,
166 false /* is_connection_cellular */);
169 void PrecacheDatabaseTest::RecordFetchFromNetworkCellular(
170 const GURL
& url
, const base::Time
& fetch_time
, int64 size
) {
171 precache_database_
->RecordURLFetched(url
, fetch_time
, size
,
172 false /* was_cached */,
173 true /* is_connection_cellular */);
176 void PrecacheDatabaseTest::RecordFetchFromCache(const GURL
& url
,
177 const base::Time
& fetch_time
,
179 precache_database_
->RecordURLFetched(url
, fetch_time
, size
,
180 true /* was_cached */,
181 false /* is_connection_cellular */);
184 void PrecacheDatabaseTest::RecordFetchFromCacheCellular(
185 const GURL
& url
, const base::Time
& fetch_time
, int64 size
) {
186 precache_database_
->RecordURLFetched(url
, fetch_time
, size
,
187 true /* was_cached */,
188 true /* is_connection_cellular */);
193 TEST_F(PrecacheDatabaseTest
, PrecacheOverNetwork
) {
194 RecordPrecacheFromNetwork(kURL
, kFetchTime
, kSize
);
196 EXPECT_EQ(BuildURLTableMap(kURL
, kFetchTime
), GetActualURLTableMap());
198 ExpectNewSample("Precache.DownloadedPrecacheMotivated", kSize
);
199 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
200 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
201 ExpectNoNewSamples("Precache.Saved");
202 ExpectNoNewSamples("Precache.Saved.Cellular");
205 TEST_F(PrecacheDatabaseTest
, PrecacheFromCacheWithURLTableEntry
) {
206 precache_url_table()->AddURL(kURL
, kOldFetchTime
);
207 RecordPrecacheFromCache(kURL
, kFetchTime
, kSize
);
209 // The URL table entry should have been updated to have |kFetchTime| as the
211 EXPECT_EQ(BuildURLTableMap(kURL
, kFetchTime
), GetActualURLTableMap());
213 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
214 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
215 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
216 ExpectNoNewSamples("Precache.Saved");
217 ExpectNoNewSamples("Precache.Saved.Cellular");
220 TEST_F(PrecacheDatabaseTest
, PrecacheFromCacheWithoutURLTableEntry
) {
221 RecordPrecacheFromCache(kURL
, kFetchTime
, kSize
);
223 EXPECT_TRUE(GetActualURLTableMap().empty());
225 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
226 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
227 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
228 ExpectNoNewSamples("Precache.Saved");
229 ExpectNoNewSamples("Precache.Saved.Cellular");
232 TEST_F(PrecacheDatabaseTest
, FetchOverNetwork_NonCellular
) {
233 RecordFetchFromNetwork(kURL
, kFetchTime
, kSize
);
235 EXPECT_TRUE(GetActualURLTableMap().empty());
237 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
238 ExpectNewSample("Precache.DownloadedNonPrecache", kSize
);
239 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
240 ExpectNoNewSamples("Precache.Saved");
241 ExpectNoNewSamples("Precache.Saved.Cellular");
244 TEST_F(PrecacheDatabaseTest
, FetchOverNetwork_Cellular
) {
245 RecordFetchFromNetworkCellular(kURL
, kFetchTime
, kSize
);
247 EXPECT_TRUE(GetActualURLTableMap().empty());
249 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
250 ExpectNewSample("Precache.DownloadedNonPrecache", kSize
);
251 ExpectNewSample("Precache.DownloadedNonPrecache.Cellular", kSize
);
252 ExpectNoNewSamples("Precache.Saved");
253 ExpectNoNewSamples("Precache.Saved.Cellular");
256 TEST_F(PrecacheDatabaseTest
, FetchOverNetworkWithURLTableEntry
) {
257 precache_url_table()->AddURL(kURL
, kOldFetchTime
);
258 RecordFetchFromNetwork(kURL
, kFetchTime
, kSize
);
260 // The URL table entry should have been deleted.
261 EXPECT_TRUE(GetActualURLTableMap().empty());
263 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
264 ExpectNewSample("Precache.DownloadedNonPrecache", kSize
);
265 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
266 ExpectNoNewSamples("Precache.Saved");
267 ExpectNoNewSamples("Precache.Saved.Cellular");
270 TEST_F(PrecacheDatabaseTest
, FetchFromCacheWithURLTableEntry_NonCellular
) {
271 precache_url_table()->AddURL(kURL
, kOldFetchTime
);
272 RecordFetchFromCache(kURL
, kFetchTime
, kSize
);
274 // The URL table entry should have been deleted.
275 EXPECT_TRUE(GetActualURLTableMap().empty());
277 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
278 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
279 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
280 ExpectNewSample("Precache.Saved", kSize
);
281 ExpectNoNewSamples("Precache.Saved.Cellular");
284 TEST_F(PrecacheDatabaseTest
, FetchFromCacheWithURLTableEntry_Cellular
) {
285 precache_url_table()->AddURL(kURL
, kOldFetchTime
);
286 RecordFetchFromCacheCellular(kURL
, kFetchTime
, kSize
);
288 // The URL table entry should have been deleted.
289 EXPECT_TRUE(GetActualURLTableMap().empty());
291 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
292 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
293 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
294 ExpectNewSample("Precache.Saved", kSize
);
295 ExpectNewSample("Precache.Saved.Cellular", kSize
);
298 TEST_F(PrecacheDatabaseTest
, FetchFromCacheWithoutURLTableEntry
) {
299 RecordFetchFromCache(kURL
, kFetchTime
, kSize
);
301 EXPECT_TRUE(GetActualURLTableMap().empty());
303 ExpectNoNewSamples("Precache.DownloadedPrecacheMotivated");
304 ExpectNoNewSamples("Precache.DownloadedNonPrecache");
305 ExpectNoNewSamples("Precache.DownloadedNonPrecache.Cellular");
306 ExpectNoNewSamples("Precache.Saved");
307 ExpectNoNewSamples("Precache.Saved.Cellular");
310 TEST_F(PrecacheDatabaseTest
, DeleteExpiredPrecacheHistory
) {
311 const base::Time kToday
= base::Time() + base::TimeDelta::FromDays(1000);
312 const base::Time k59DaysAgo
= kToday
- base::TimeDelta::FromDays(59);
313 const base::Time k61DaysAgo
= kToday
- base::TimeDelta::FromDays(61);
315 precache_url_table()->AddURL(GURL("http://expired-precache.com"), k61DaysAgo
);
316 precache_url_table()->AddURL(GURL("http://old-precache.com"), k59DaysAgo
);
318 precache_database_
->DeleteExpiredPrecacheHistory(kToday
);
320 EXPECT_EQ(BuildURLTableMap(GURL("http://old-precache.com"), k59DaysAgo
),
321 GetActualURLTableMap());
324 TEST_F(PrecacheDatabaseTest
, SampleInteraction
) {
325 const GURL
kURL1("http://url1.com");
326 const int64 kSize1
= 1000;
327 const GURL
kURL2("http://url2.com");
328 const int64 kSize2
= 2000;
329 const GURL
kURL3("http://url3.com");
330 const int64 kSize3
= 3000;
331 const GURL
kURL4("http://url4.com");
332 const int64 kSize4
= 4000;
333 const GURL
kURL5("http://url5.com");
334 const int64 kSize5
= 5000;
336 RecordPrecacheFromNetwork(kURL1
, kFetchTime
, kSize1
);
337 RecordPrecacheFromNetwork(kURL2
, kFetchTime
, kSize2
);
338 RecordPrecacheFromNetwork(kURL3
, kFetchTime
, kSize3
);
339 RecordPrecacheFromNetwork(kURL4
, kFetchTime
, kSize4
);
341 RecordFetchFromCacheCellular(kURL1
, kFetchTime
, kSize1
);
342 RecordFetchFromCacheCellular(kURL1
, kFetchTime
, kSize1
);
343 RecordFetchFromNetworkCellular(kURL2
, kFetchTime
, kSize2
);
344 RecordFetchFromNetworkCellular(kURL5
, kFetchTime
, kSize5
);
345 RecordFetchFromCacheCellular(kURL5
, kFetchTime
, kSize5
);
347 RecordPrecacheFromCache(kURL1
, kFetchTime
, kSize1
);
348 RecordPrecacheFromNetwork(kURL2
, kFetchTime
, kSize2
);
349 RecordPrecacheFromCache(kURL3
, kFetchTime
, kSize3
);
350 RecordPrecacheFromCache(kURL4
, kFetchTime
, kSize4
);
352 RecordFetchFromCache(kURL1
, kFetchTime
, kSize1
);
353 RecordFetchFromNetwork(kURL2
, kFetchTime
, kSize2
);
354 RecordFetchFromCache(kURL3
, kFetchTime
, kSize3
);
355 RecordFetchFromCache(kURL5
, kFetchTime
, kSize5
);
357 scoped_ptr
<base::HistogramSamples
> downloaded_precache_motivated_bytes(
358 GetHistogramSamplesDelta("Precache.DownloadedPrecacheMotivated"));
359 EXPECT_EQ(5, downloaded_precache_motivated_bytes
->TotalCount());
360 EXPECT_EQ(1, downloaded_precache_motivated_bytes
->GetCount(kSize1
));
361 EXPECT_EQ(2, downloaded_precache_motivated_bytes
->GetCount(kSize2
));
362 EXPECT_EQ(1, downloaded_precache_motivated_bytes
->GetCount(kSize3
));
363 EXPECT_EQ(1, downloaded_precache_motivated_bytes
->GetCount(kSize4
));
365 scoped_ptr
<base::HistogramSamples
> downloaded_non_precache_bytes(
366 GetHistogramSamplesDelta("Precache.DownloadedNonPrecache"));
367 EXPECT_EQ(3, downloaded_non_precache_bytes
->TotalCount());
368 EXPECT_EQ(2, downloaded_non_precache_bytes
->GetCount(kSize2
));
369 EXPECT_EQ(1, downloaded_non_precache_bytes
->GetCount(kSize5
));
371 scoped_ptr
<base::HistogramSamples
> downloaded_non_precache_bytes_cellular(
372 GetHistogramSamplesDelta("Precache.DownloadedNonPrecache.Cellular"));
373 EXPECT_EQ(2, downloaded_non_precache_bytes_cellular
->TotalCount());
374 EXPECT_EQ(1, downloaded_non_precache_bytes_cellular
->GetCount(kSize2
));
375 EXPECT_EQ(1, downloaded_non_precache_bytes_cellular
->GetCount(kSize5
));
377 scoped_ptr
<base::HistogramSamples
> saved_bytes(
378 GetHistogramSamplesDelta("Precache.Saved"));
379 EXPECT_EQ(2, saved_bytes
->TotalCount());
380 EXPECT_EQ(1, saved_bytes
->GetCount(kSize1
));
381 EXPECT_EQ(1, saved_bytes
->GetCount(kSize3
));
383 scoped_ptr
<base::HistogramSamples
> saved_bytes_cellular(
384 GetHistogramSamplesDelta("Precache.Saved.Cellular"));
385 EXPECT_EQ(1, saved_bytes_cellular
->TotalCount());
386 EXPECT_EQ(1, saved_bytes_cellular
->GetCount(kSize1
));
391 } // namespace precache