Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Unified Diff: components/precache/core/precache_statistics_table_unittest.cc

Issue 27047003: Precache tracking database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@precache
Patch Set: Addressed comments Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/precache/core/precache_statistics_table_unittest.cc
diff --git a/components/precache/core/precache_statistics_table_unittest.cc b/components/precache/core/precache_statistics_table_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6f811836739dd5d7ca3ae80d1fc35bcb1833dbdf
--- /dev/null
+++ b/components/precache/core/precache_statistics_table_unittest.cc
@@ -0,0 +1,102 @@
+// Copyright 2013 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 "components/precache/core/precache_statistics_table.h"
+
+#include "base/basictypes.h"
+#include "base/compiler_specific.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/time/time.h"
+#include "sql/connection.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace precache {
+
+namespace {
+
+typedef PrecacheStatisticsTable::PrecacheStatistics PrecacheStatistics;
+typedef PrecacheStatisticsTable::PrecacheStatisticsMap PrecacheStatisticsMap;
+
+class PrecacheStatisticsTableTest : public testing::Test {
+ public:
+ PrecacheStatisticsTableTest() {}
+ virtual ~PrecacheStatisticsTableTest() {}
+
+ protected:
+ virtual void SetUp() OVERRIDE {
+ precache_statistics_table_.reset(new PrecacheStatisticsTable());
+ db_.reset(new sql::Connection());
+ ASSERT_TRUE(db_->OpenInMemory());
+ precache_statistics_table_->Init(db_.get());
+ }
+
+ scoped_ptr<PrecacheStatisticsTable> precache_statistics_table_;
+ scoped_ptr<sql::Connection> db_;
+};
+
+TEST_F(PrecacheStatisticsTableTest, IncreaseDailyStats) {
+ const base::Time kFetchDay = base::Time() + base::TimeDelta::FromDays(100);
+
+ precache_statistics_table_->IncreaseDailyStats(
+ kFetchDay + base::TimeDelta::FromHours(5),
+ PrecacheStatistics(5, 4, 3, 2, 1));
+ precache_statistics_table_->IncreaseDailyStats(
+ kFetchDay + base::TimeDelta::FromHours(10),
+ PrecacheStatistics(50, 40, 30, 20, 10));
+
+ PrecacheStatisticsMap expected_stats_map;
+ expected_stats_map[kFetchDay] = PrecacheStatistics(55, 44, 33, 22, 11);
+
+ PrecacheStatisticsMap actual_stats_map;
+ precache_statistics_table_->GetOldStats(base::Time::Max(), &actual_stats_map);
+ EXPECT_EQ(expected_stats_map, actual_stats_map);
+}
+
+TEST_F(PrecacheStatisticsTableTest, GetOldStats) {
+ const base::Time kCurrentDate = base::Time() + base::TimeDelta::FromDays(100);
+ const base::Time kOldDate = kCurrentDate - base::TimeDelta::FromDays(10);
+ const base::Time kBeforeDate = kCurrentDate - base::TimeDelta::FromDays(1);
+ const base::Time kAfterDate = kCurrentDate + base::TimeDelta::FromDays(1);
+ const PrecacheStatistics kStats(5, 4, 3, 2, 1);
+
+ precache_statistics_table_->IncreaseDailyStats(kOldDate, kStats);
+ precache_statistics_table_->IncreaseDailyStats(kBeforeDate, kStats);
+ precache_statistics_table_->IncreaseDailyStats(kCurrentDate, kStats);
+ precache_statistics_table_->IncreaseDailyStats(kAfterDate, kStats);
+
+ PrecacheStatisticsMap expected_stats_map;
+ expected_stats_map[kOldDate] = kStats;
+ expected_stats_map[kBeforeDate] = kStats;
+
+ PrecacheStatisticsMap actual_stats_map;
+ precache_statistics_table_->GetOldStats(kCurrentDate, &actual_stats_map);
+ EXPECT_EQ(expected_stats_map, actual_stats_map);
+}
+
+TEST_F(PrecacheStatisticsTableTest, DeleteOldStats) {
+ const base::Time kCurrentDate = base::Time() + base::TimeDelta::FromDays(100);
+ const base::Time kOldDate = kCurrentDate - base::TimeDelta::FromDays(10);
+ const base::Time kBeforeDate = kCurrentDate - base::TimeDelta::FromDays(1);
+ const base::Time kAfterDate = kCurrentDate + base::TimeDelta::FromDays(1);
+ const PrecacheStatistics kStats(5, 4, 3, 2, 1);
+
+ precache_statistics_table_->IncreaseDailyStats(kOldDate, kStats);
+ precache_statistics_table_->IncreaseDailyStats(kBeforeDate, kStats);
+ precache_statistics_table_->IncreaseDailyStats(kCurrentDate, kStats);
+ precache_statistics_table_->IncreaseDailyStats(kAfterDate, kStats);
+
+ precache_statistics_table_->DeleteOldStats(kCurrentDate);
+
+ PrecacheStatisticsMap expected_stats_map;
+ expected_stats_map[kCurrentDate] = kStats;
+ expected_stats_map[kAfterDate] = kStats;
+
+ PrecacheStatisticsMap actual_stats_map;
+ precache_statistics_table_->GetOldStats(base::Time::Max(), &actual_stats_map);
+ EXPECT_EQ(expected_stats_map, actual_stats_map);
+}
+
+} // namespace
+
+} // namespace precache

Powered by Google App Engine
This is Rietveld 408576698