| 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_url_table.h" |
| 6 |
| 7 #include <map> |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/bind.h" |
| 12 #include "base/bind_helpers.h" |
| 13 #include "base/callback.h" |
| 14 #include "base/time/time.h" |
| 15 #include "sql/connection.h" |
| 16 #include "sql/statement.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace precache { |
| 20 |
| 21 namespace { |
| 22 |
| 23 class PrecacheURLTableTest : public testing::Test { |
| 24 public: |
| 25 PrecacheURLTableTest() {} |
| 26 virtual ~PrecacheURLTableTest() {} |
| 27 |
| 28 protected: |
| 29 virtual void SetUp() { |
| 30 precache_url_table_.reset(new PrecacheURLTable()); |
| 31 db_.reset(new sql::Connection()); |
| 32 ASSERT_TRUE(db_->OpenInMemory()); |
| 33 precache_url_table_->Init(db_.get()); |
| 34 } |
| 35 |
| 36 scoped_ptr<PrecacheURLTable> precache_url_table_; |
| 37 scoped_ptr<sql::Connection> db_; |
| 38 }; |
| 39 |
| 40 TEST_F(PrecacheURLTableTest, AddURLWithNoExistingRow) { |
| 41 precache_url_table_->AddURL(GURL("http://url.com"), |
| 42 base::Time::FromInternalValue(100)); |
| 43 |
| 44 std::map<GURL, base::Time> expected_map; |
| 45 expected_map[GURL("http://url.com")] = base::Time::FromInternalValue(100); |
| 46 |
| 47 std::map<GURL, base::Time> actual_map; |
| 48 precache_url_table_->GetAllDataForTesting(&actual_map); |
| 49 EXPECT_EQ(expected_map, actual_map); |
| 50 } |
| 51 |
| 52 TEST_F(PrecacheURLTableTest, AddURLWithExistingRow) { |
| 53 precache_url_table_->AddURL(GURL("http://url.com"), |
| 54 base::Time::FromInternalValue(90)); |
| 55 precache_url_table_->AddURL(GURL("http://url.com"), |
| 56 base::Time::FromInternalValue(100)); |
| 57 |
| 58 std::map<GURL, base::Time> expected_map; |
| 59 expected_map[GURL("http://url.com")] = base::Time::FromInternalValue(100); |
| 60 |
| 61 std::map<GURL, base::Time> actual_map; |
| 62 precache_url_table_->GetAllDataForTesting(&actual_map); |
| 63 EXPECT_EQ(expected_map, actual_map); |
| 64 } |
| 65 |
| 66 TEST_F(PrecacheURLTableTest, DeleteURL) { |
| 67 precache_url_table_->AddURL(GURL("http://stays.com"), |
| 68 base::Time::FromInternalValue(100)); |
| 69 precache_url_table_->AddURL(GURL("http://deleted.com"), |
| 70 base::Time::FromInternalValue(200)); |
| 71 |
| 72 precache_url_table_->DeleteURL(GURL("http://deleted.com")); |
| 73 |
| 74 std::map<GURL, base::Time> expected_map; |
| 75 expected_map[GURL("http://stays.com")] = base::Time::FromInternalValue(100); |
| 76 |
| 77 std::map<GURL, base::Time> actual_map; |
| 78 precache_url_table_->GetAllDataForTesting(&actual_map); |
| 79 EXPECT_EQ(expected_map, actual_map); |
| 80 } |
| 81 |
| 82 TEST_F(PrecacheURLTableTest, HasURL) { |
| 83 EXPECT_FALSE(precache_url_table_->HasURL(GURL("http://url.com"))); |
| 84 |
| 85 precache_url_table_->AddURL(GURL("http://url.com"), |
| 86 base::Time::FromInternalValue(100)); |
| 87 |
| 88 EXPECT_TRUE(precache_url_table_->HasURL(GURL("http://url.com"))); |
| 89 |
| 90 precache_url_table_->DeleteURL(GURL("http://url.com")); |
| 91 |
| 92 EXPECT_FALSE(precache_url_table_->HasURL(GURL("http://url.com"))); |
| 93 } |
| 94 |
| 95 TEST_F(PrecacheURLTableTest, DeleteAllPrecachedBetween) { |
| 96 precache_url_table_->AddURL(GURL("http://before.com"), |
| 97 base::Time::FromInternalValue(40)); |
| 98 precache_url_table_->AddURL(GURL("http://begin.com"), |
| 99 base::Time::FromInternalValue(50)); |
| 100 precache_url_table_->AddURL(GURL("http://between.com"), |
| 101 base::Time::FromInternalValue(100)); |
| 102 precache_url_table_->AddURL(GURL("http://end.com"), |
| 103 base::Time::FromInternalValue(150)); |
| 104 precache_url_table_->AddURL(GURL("http://after.com"), |
| 105 base::Time::FromInternalValue(160)); |
| 106 |
| 107 precache_url_table_->DeleteAllPrecachedBetween( |
| 108 base::Time::FromInternalValue(50), base::Time::FromInternalValue(150)); |
| 109 |
| 110 std::map<GURL, base::Time> expected_map; |
| 111 expected_map[GURL("http://before.com")] = base::Time::FromInternalValue(40); |
| 112 expected_map[GURL("http://after.com")] = base::Time::FromInternalValue(160); |
| 113 |
| 114 std::map<GURL, base::Time> actual_map; |
| 115 precache_url_table_->GetAllDataForTesting(&actual_map); |
| 116 EXPECT_EQ(expected_map, actual_map); |
| 117 } |
| 118 |
| 119 } // namespace |
| 120 |
| 121 } // namespace precache |
| OLD | NEW |