| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #include "components/precache/core/precache_statistics_table.h" |
| 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/time/time.h" |
| 11 #include "sql/connection.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace precache { |
| 15 |
| 16 namespace { |
| 17 |
| 18 typedef PrecacheStatisticsTable::PrecacheStatistics PrecacheStatistics; |
| 19 typedef PrecacheStatisticsTable::PrecacheStatisticsMap PrecacheStatisticsMap; |
| 20 |
| 21 base::Time GetTimeFromString(const char* time_string) { |
| 22 base::Time time; |
| 23 EXPECT_TRUE(base::Time::FromString(time_string, &time)); |
| 24 return time; |
| 25 } |
| 26 |
| 27 class PrecacheStatisticsTableTest : public testing::Test { |
| 28 public: |
| 29 PrecacheStatisticsTableTest() {} |
| 30 virtual ~PrecacheStatisticsTableTest() {} |
| 31 |
| 32 protected: |
| 33 virtual void SetUp() OVERRIDE { |
| 34 precache_statistics_table_.reset(new PrecacheStatisticsTable()); |
| 35 db_.reset(new sql::Connection()); |
| 36 ASSERT_TRUE(db_->OpenInMemory()); |
| 37 precache_statistics_table_->Init(db_.get()); |
| 38 } |
| 39 |
| 40 scoped_ptr<PrecacheStatisticsTable> precache_statistics_table_; |
| 41 scoped_ptr<sql::Connection> db_; |
| 42 }; |
| 43 |
| 44 TEST_F(PrecacheStatisticsTableTest, IncreaseDailyStats) { |
| 45 const base::Time kFetchDay = GetTimeFromString("11 Oct 2013, 00:00:00"); |
| 46 |
| 47 precache_statistics_table_->IncreaseDailyStats( |
| 48 kFetchDay + base::TimeDelta::FromHours(5), |
| 49 PrecacheStatistics(5, 4, 3, 2, 1)); |
| 50 precache_statistics_table_->IncreaseDailyStats( |
| 51 kFetchDay + base::TimeDelta::FromHours(10), |
| 52 PrecacheStatistics(50, 40, 30, 20, 10)); |
| 53 |
| 54 PrecacheStatisticsMap expected_stats_map; |
| 55 expected_stats_map[kFetchDay] = PrecacheStatistics(55, 44, 33, 22, 11); |
| 56 |
| 57 PrecacheStatisticsMap actual_stats_map; |
| 58 precache_statistics_table_->GetAllStatsUntil(base::Time::Max(), |
| 59 &actual_stats_map); |
| 60 |
| 61 EXPECT_EQ(expected_stats_map, actual_stats_map); |
| 62 } |
| 63 |
| 64 TEST_F(PrecacheStatisticsTableTest, GetAllStatsUntil) { |
| 65 const base::Time kBeforeDate = GetTimeFromString("11 Oct 2013, 00:00:00"); |
| 66 const base::Time kEndDate = GetTimeFromString("12 Oct 2013, 00:00:00"); |
| 67 const base::Time kAfterDate = GetTimeFromString("13 Oct 2013, 00:00:00"); |
| 68 const PrecacheStatistics kStats(5, 4, 3, 2, 1); |
| 69 |
| 70 precache_statistics_table_->IncreaseDailyStats(kBeforeDate, kStats); |
| 71 precache_statistics_table_->IncreaseDailyStats(kEndDate, kStats); |
| 72 precache_statistics_table_->IncreaseDailyStats(kAfterDate, kStats); |
| 73 |
| 74 PrecacheStatisticsMap expected_stats_map; |
| 75 expected_stats_map[kBeforeDate] = kStats; |
| 76 expected_stats_map[kEndDate] = kStats; |
| 77 |
| 78 PrecacheStatisticsMap actual_stats_map; |
| 79 precache_statistics_table_->GetAllStatsUntil(kEndDate, &actual_stats_map); |
| 80 |
| 81 EXPECT_EQ(expected_stats_map, actual_stats_map); |
| 82 } |
| 83 |
| 84 TEST_F(PrecacheStatisticsTableTest, DeleteAllStatsUntil) { |
| 85 const base::Time kBeforeDate = GetTimeFromString("11 Oct 2013, 00:00:00"); |
| 86 const base::Time kEndDate = GetTimeFromString("12 Oct 2013, 00:00:00"); |
| 87 const base::Time kAfterDate = GetTimeFromString("13 Oct 2013, 00:00:00"); |
| 88 const PrecacheStatistics kStats(5, 4, 3, 2, 1); |
| 89 |
| 90 precache_statistics_table_->IncreaseDailyStats(kBeforeDate, kStats); |
| 91 precache_statistics_table_->IncreaseDailyStats(kEndDate, kStats); |
| 92 precache_statistics_table_->IncreaseDailyStats(kAfterDate, kStats); |
| 93 |
| 94 precache_statistics_table_->DeleteAllStatsUntil(kEndDate); |
| 95 |
| 96 PrecacheStatisticsMap expected_stats_map; |
| 97 expected_stats_map[kAfterDate] = kStats; |
| 98 |
| 99 PrecacheStatisticsMap actual_stats_map; |
| 100 precache_statistics_table_->GetAllStatsUntil(base::Time::Max(), |
| 101 &actual_stats_map); |
| 102 |
| 103 EXPECT_EQ(expected_stats_map, actual_stats_map); |
| 104 } |
| 105 |
| 106 } // namespace |
| 107 |
| 108 } // namespace precache |
| OLD | NEW |