| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/precache/core/precache_url_table.h" | 5 #include "components/precache/core/precache_url_table.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
| 11 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 12 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 13 #include "sql/connection.h" | 15 #include "sql/connection.h" |
| 14 #include "sql/statement.h" | 16 #include "sql/statement.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 19 |
| 18 namespace precache { | 20 namespace precache { |
| 19 | 21 |
| 20 void PrintTo(const PrecacheURLInfo& url_info, ::std::ostream* os) { | 22 void PrintTo(const PrecacheURLInfo& url_info, ::std::ostream* os) { |
| 21 *os << "{" << url_info.was_precached << ", " << url_info.is_precached << ", " | 23 *os << "{" << url_info.was_precached << ", " << url_info.is_precached << ", " |
| 22 << url_info.was_used << "}"; | 24 << url_info.was_used << ", " << url_info.is_download_reported << "}"; |
| 23 } | 25 } |
| 24 | 26 |
| 25 namespace { | 27 namespace { |
| 26 | 28 |
| 27 class PrecacheURLTableTest : public testing::Test { | 29 class PrecacheURLTableTest : public testing::Test { |
| 28 public: | 30 public: |
| 29 PrecacheURLTableTest() {} | 31 PrecacheURLTableTest() {} |
| 30 ~PrecacheURLTableTest() override {} | 32 ~PrecacheURLTableTest() override {} |
| 31 | 33 |
| 32 protected: | 34 protected: |
| 33 void SetUp() override { | 35 void SetUp() override { |
| 34 precache_url_table_.reset(new PrecacheURLTable()); | 36 precache_url_table_.reset(new PrecacheURLTable()); |
| 35 db_.reset(new sql::Connection()); | 37 db_.reset(new sql::Connection()); |
| 36 ASSERT_TRUE(db_->OpenInMemory()); | 38 ASSERT_TRUE(db_->OpenInMemory()); |
| 37 ASSERT_TRUE(precache_url_table_->Init(db_.get())); | 39 ASSERT_TRUE(precache_url_table_->Init(db_.get())); |
| 38 } | 40 } |
| 39 | 41 |
| 40 std::unique_ptr<PrecacheURLTable> precache_url_table_; | 42 std::unique_ptr<PrecacheURLTable> precache_url_table_; |
| 41 std::unique_ptr<sql::Connection> db_; | 43 std::unique_ptr<sql::Connection> db_; |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 TEST_F(PrecacheURLTableTest, AddURLWithNoExistingRow) { | 46 TEST_F(PrecacheURLTableTest, AddURLWithNoExistingRow) { |
| 45 const base::Time kTime = base::Time::FromInternalValue(100); | 47 const base::Time kTime = base::Time::FromInternalValue(100); |
| 46 precache_url_table_->AddURL(GURL("http://url.com"), 1, true, kTime); | 48 precache_url_table_->AddURL(GURL("http://url.com"), 1, true, kTime, false); |
| 47 | 49 |
| 48 std::map<GURL, base::Time> expected_map; | 50 std::map<GURL, base::Time> expected_map; |
| 49 expected_map[GURL("http://url.com")] = kTime; | 51 expected_map[GURL("http://url.com")] = kTime; |
| 50 | 52 |
| 51 std::map<GURL, base::Time> actual_map; | 53 std::map<GURL, base::Time> actual_map; |
| 52 precache_url_table_->GetAllDataForTesting(&actual_map); | 54 precache_url_table_->GetAllDataForTesting(&actual_map); |
| 53 EXPECT_EQ(expected_map, actual_map); | 55 EXPECT_EQ(expected_map, actual_map); |
| 54 } | 56 } |
| 55 | 57 |
| 56 TEST_F(PrecacheURLTableTest, AddURLWithExistingRow) { | 58 TEST_F(PrecacheURLTableTest, AddURLWithExistingRow) { |
| 57 const base::Time kOldTime = base::Time::FromInternalValue(50); | 59 const base::Time kOldTime = base::Time::FromInternalValue(50); |
| 58 const base::Time kNewTime = base::Time::FromInternalValue(100); | 60 const base::Time kNewTime = base::Time::FromInternalValue(100); |
| 59 precache_url_table_->AddURL(GURL("http://url.com"), 1, true, kOldTime); | 61 precache_url_table_->AddURL(GURL("http://url.com"), 1, true, kOldTime, false); |
| 60 precache_url_table_->AddURL(GURL("http://url.com"), 1, true, kNewTime); | 62 precache_url_table_->AddURL(GURL("http://url.com"), 1, true, kNewTime, false); |
| 61 | 63 |
| 62 std::map<GURL, base::Time> expected_map; | 64 std::map<GURL, base::Time> expected_map; |
| 63 expected_map[GURL("http://url.com")] = kNewTime; | 65 expected_map[GURL("http://url.com")] = kNewTime; |
| 64 | 66 |
| 65 std::map<GURL, base::Time> actual_map; | 67 std::map<GURL, base::Time> actual_map; |
| 66 precache_url_table_->GetAllDataForTesting(&actual_map); | 68 precache_url_table_->GetAllDataForTesting(&actual_map); |
| 67 EXPECT_EQ(expected_map, actual_map); | 69 EXPECT_EQ(expected_map, actual_map); |
| 68 } | 70 } |
| 69 | 71 |
| 70 TEST_F(PrecacheURLTableTest, SetURLAsNotPrecached) { | 72 TEST_F(PrecacheURLTableTest, SetURLAsNotPrecached) { |
| 71 const base::Time kStaysTime = base::Time::FromInternalValue(50); | 73 const base::Time kStaysTime = base::Time::FromInternalValue(50); |
| 72 const base::Time kDeletedTime = base::Time::FromInternalValue(100); | 74 const base::Time kDeletedTime = base::Time::FromInternalValue(100); |
| 73 | 75 |
| 74 precache_url_table_->AddURL(GURL("http://stays.com"), 1, true, kStaysTime); | 76 precache_url_table_->AddURL(GURL("http://stays.com"), 1, true, kStaysTime, |
| 75 precache_url_table_->AddURL(GURL("http://deleted.com"), 1, true, | 77 false); |
| 76 kDeletedTime); | 78 precache_url_table_->AddURL(GURL("http://deleted.com"), 1, true, kDeletedTime, |
| 79 false); |
| 77 | 80 |
| 78 precache_url_table_->SetURLAsNotPrecached(GURL("http://deleted.com")); | 81 precache_url_table_->SetURLAsNotPrecached(GURL("http://deleted.com")); |
| 79 | 82 |
| 80 std::map<GURL, base::Time> expected_map; | 83 std::map<GURL, base::Time> expected_map; |
| 81 expected_map[GURL("http://stays.com")] = kStaysTime; | 84 expected_map[GURL("http://stays.com")] = kStaysTime; |
| 82 | 85 |
| 83 std::map<GURL, base::Time> actual_map; | 86 std::map<GURL, base::Time> actual_map; |
| 84 precache_url_table_->GetAllDataForTesting(&actual_map); | 87 precache_url_table_->GetAllDataForTesting(&actual_map); |
| 85 EXPECT_EQ(expected_map, actual_map); | 88 EXPECT_EQ(expected_map, actual_map); |
| 86 } | 89 } |
| 87 | 90 |
| 91 TEST_F(PrecacheURLTableTest, SetDownloadReported) { |
| 92 const GURL url("http://stays.com"); |
| 93 precache_url_table_->AddURL(url, 1, true, base::Time::FromInternalValue(50), |
| 94 false); |
| 95 |
| 96 precache_url_table_->SetDownloadReported(1); |
| 97 |
| 98 EXPECT_EQ((PrecacheURLInfo{true, true, false, true}), |
| 99 precache_url_table_->GetURLInfo(url)); |
| 100 } |
| 101 |
| 88 TEST_F(PrecacheURLTableTest, GetURLInfo) { | 102 TEST_F(PrecacheURLTableTest, GetURLInfo) { |
| 89 const GURL url("http://url.com"); | 103 const GURL url("http://url.com"); |
| 90 | 104 |
| 91 EXPECT_EQ((PrecacheURLInfo{false, false, false}), | 105 EXPECT_EQ((PrecacheURLInfo{false, false, false, false}), |
| 92 precache_url_table_->GetURLInfo(url)); | 106 precache_url_table_->GetURLInfo(url)); |
| 93 | 107 |
| 94 precache_url_table_->AddURL(url, 1, true, base::Time::FromInternalValue(100)); | 108 precache_url_table_->AddURL(url, 1, true, base::Time::FromInternalValue(100), |
| 109 true); |
| 95 | 110 |
| 96 EXPECT_EQ((PrecacheURLInfo{true, true, false}), | 111 EXPECT_EQ((PrecacheURLInfo{true, true, false, true}), |
| 97 precache_url_table_->GetURLInfo(url)); | 112 precache_url_table_->GetURLInfo(url)); |
| 98 | 113 |
| 99 precache_url_table_->SetPrecachedURLAsUsed(url); | 114 precache_url_table_->SetPrecachedURLAsUsed(url); |
| 100 | 115 |
| 101 EXPECT_EQ((PrecacheURLInfo{true, false, true}), | 116 EXPECT_EQ((PrecacheURLInfo{true, false, true, true}), |
| 102 precache_url_table_->GetURLInfo(url)); | 117 precache_url_table_->GetURLInfo(url)); |
| 103 | 118 |
| 104 precache_url_table_->AddURL(url, 1, true, base::Time::FromInternalValue(100)); | 119 precache_url_table_->AddURL(url, 1, true, base::Time::FromInternalValue(100), |
| 120 false); |
| 105 | 121 |
| 106 EXPECT_EQ((PrecacheURLInfo{true, true, false}), | 122 EXPECT_EQ((PrecacheURLInfo{true, true, false, false}), |
| 107 precache_url_table_->GetURLInfo(url)); | 123 precache_url_table_->GetURLInfo(url)); |
| 108 | 124 |
| 109 precache_url_table_->SetURLAsNotPrecached(url); | 125 precache_url_table_->SetURLAsNotPrecached(url); |
| 110 | 126 |
| 111 EXPECT_EQ((PrecacheURLInfo{true, false, false}), | 127 EXPECT_EQ((PrecacheURLInfo{true, false, false, false}), |
| 128 precache_url_table_->GetURLInfo(url)); |
| 129 |
| 130 precache_url_table_->SetDownloadReported(1); |
| 131 |
| 132 EXPECT_EQ((PrecacheURLInfo{true, false, false, true}), |
| 112 precache_url_table_->GetURLInfo(url)); | 133 precache_url_table_->GetURLInfo(url)); |
| 113 } | 134 } |
| 114 | 135 |
| 115 TEST_F(PrecacheURLTableTest, DeleteAllPrecachedBefore) { | 136 TEST_F(PrecacheURLTableTest, DeleteAllPrecachedBefore) { |
| 116 const base::Time kOldTime = base::Time::FromInternalValue(10); | 137 const base::Time kOldTime = base::Time::FromInternalValue(10); |
| 117 const base::Time kBeforeTime = base::Time::FromInternalValue(20); | 138 const base::Time kBeforeTime = base::Time::FromInternalValue(20); |
| 118 const base::Time kEndTime = base::Time::FromInternalValue(30); | 139 const base::Time kEndTime = base::Time::FromInternalValue(30); |
| 119 const base::Time kAfterTime = base::Time::FromInternalValue(40); | 140 const base::Time kAfterTime = base::Time::FromInternalValue(40); |
| 120 | 141 |
| 121 precache_url_table_->AddURL(GURL("http://old.com"), 1, true, kOldTime); | 142 precache_url_table_->AddURL(GURL("http://old.com"), 1, true, kOldTime, false); |
| 122 precache_url_table_->AddURL(GURL("http://before.com"), 1, true, kBeforeTime); | 143 precache_url_table_->AddURL(GURL("http://before.com"), 1, true, kBeforeTime, |
| 123 precache_url_table_->AddURL(GURL("http://end.com"), 1, true, kEndTime); | 144 false); |
| 124 precache_url_table_->AddURL(GURL("http://after.com"), 1, true, kAfterTime); | 145 precache_url_table_->AddURL(GURL("http://end.com"), 1, true, kEndTime, false); |
| 146 precache_url_table_->AddURL(GURL("http://after.com"), 1, true, kAfterTime, |
| 147 false); |
| 125 | 148 |
| 126 precache_url_table_->DeleteAllPrecachedBefore(kEndTime); | 149 precache_url_table_->DeleteAllPrecachedBefore(kEndTime); |
| 127 | 150 |
| 128 std::map<GURL, base::Time> expected_map; | 151 std::map<GURL, base::Time> expected_map; |
| 129 expected_map[GURL("http://end.com")] = kEndTime; | 152 expected_map[GURL("http://end.com")] = kEndTime; |
| 130 expected_map[GURL("http://after.com")] = kAfterTime; | 153 expected_map[GURL("http://after.com")] = kAfterTime; |
| 131 | 154 |
| 132 std::map<GURL, base::Time> actual_map; | 155 std::map<GURL, base::Time> actual_map; |
| 133 precache_url_table_->GetAllDataForTesting(&actual_map); | 156 precache_url_table_->GetAllDataForTesting(&actual_map); |
| 134 EXPECT_EQ(expected_map, actual_map); | 157 EXPECT_EQ(expected_map, actual_map); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 EXPECT_EQ(0, statement.ColumnInt(2)); | 193 EXPECT_EQ(0, statement.ColumnInt(2)); |
| 171 EXPECT_EQ(1, statement.ColumnInt(3)); | 194 EXPECT_EQ(1, statement.ColumnInt(3)); |
| 172 } | 195 } |
| 173 EXPECT_THAT(std::set<std::string>(begin(old_urls), end(old_urls)), | 196 EXPECT_THAT(std::set<std::string>(begin(old_urls), end(old_urls)), |
| 174 ::testing::ContainerEq(actual_urls)); | 197 ::testing::ContainerEq(actual_urls)); |
| 175 } | 198 } |
| 176 | 199 |
| 177 } // namespace | 200 } // namespace |
| 178 | 201 |
| 179 } // namespace precache | 202 } // namespace precache |
| OLD | NEW |