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_url_table.h"
9 #include "base/basictypes.h"
10 #include "base/compiler_specific.h"
11 #include "base/time/time.h"
12 #include "sql/connection.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 class PrecacheURLTableTest
: public testing::Test
{
21 PrecacheURLTableTest() {}
22 ~PrecacheURLTableTest() override
{}
25 void SetUp() override
{
26 precache_url_table_
.reset(new PrecacheURLTable());
27 db_
.reset(new sql::Connection());
28 ASSERT_TRUE(db_
->OpenInMemory());
29 precache_url_table_
->Init(db_
.get());
32 scoped_ptr
<PrecacheURLTable
> precache_url_table_
;
33 scoped_ptr
<sql::Connection
> db_
;
36 TEST_F(PrecacheURLTableTest
, AddURLWithNoExistingRow
) {
37 const base::Time kTime
= base::Time::FromInternalValue(100);
38 precache_url_table_
->AddURL(GURL("http://url.com"), kTime
);
40 std::map
<GURL
, base::Time
> expected_map
;
41 expected_map
[GURL("http://url.com")] = kTime
;
43 std::map
<GURL
, base::Time
> actual_map
;
44 precache_url_table_
->GetAllDataForTesting(&actual_map
);
45 EXPECT_EQ(expected_map
, actual_map
);
48 TEST_F(PrecacheURLTableTest
, AddURLWithExistingRow
) {
49 const base::Time kOldTime
= base::Time::FromInternalValue(50);
50 const base::Time kNewTime
= base::Time::FromInternalValue(100);
51 precache_url_table_
->AddURL(GURL("http://url.com"), kOldTime
);
52 precache_url_table_
->AddURL(GURL("http://url.com"), kNewTime
);
54 std::map
<GURL
, base::Time
> expected_map
;
55 expected_map
[GURL("http://url.com")] = kNewTime
;
57 std::map
<GURL
, base::Time
> actual_map
;
58 precache_url_table_
->GetAllDataForTesting(&actual_map
);
59 EXPECT_EQ(expected_map
, actual_map
);
62 TEST_F(PrecacheURLTableTest
, DeleteURL
) {
63 const base::Time kStaysTime
= base::Time::FromInternalValue(50);
64 const base::Time kDeletedTime
= base::Time::FromInternalValue(100);
66 precache_url_table_
->AddURL(GURL("http://stays.com"), kStaysTime
);
67 precache_url_table_
->AddURL(GURL("http://deleted.com"), kDeletedTime
);
69 precache_url_table_
->DeleteURL(GURL("http://deleted.com"));
71 std::map
<GURL
, base::Time
> expected_map
;
72 expected_map
[GURL("http://stays.com")] = kStaysTime
;
74 std::map
<GURL
, base::Time
> actual_map
;
75 precache_url_table_
->GetAllDataForTesting(&actual_map
);
76 EXPECT_EQ(expected_map
, actual_map
);
79 TEST_F(PrecacheURLTableTest
, HasURL
) {
80 EXPECT_FALSE(precache_url_table_
->HasURL(GURL("http://url.com")));
82 precache_url_table_
->AddURL(GURL("http://url.com"),
83 base::Time::FromInternalValue(100));
85 EXPECT_TRUE(precache_url_table_
->HasURL(GURL("http://url.com")));
87 precache_url_table_
->DeleteURL(GURL("http://url.com"));
89 EXPECT_FALSE(precache_url_table_
->HasURL(GURL("http://url.com")));
92 TEST_F(PrecacheURLTableTest
, DeleteAllPrecachedBefore
) {
93 const base::Time kOldTime
= base::Time::FromInternalValue(10);
94 const base::Time kBeforeTime
= base::Time::FromInternalValue(20);
95 const base::Time kEndTime
= base::Time::FromInternalValue(30);
96 const base::Time kAfterTime
= base::Time::FromInternalValue(40);
98 precache_url_table_
->AddURL(GURL("http://old.com"), kOldTime
);
99 precache_url_table_
->AddURL(GURL("http://before.com"), kBeforeTime
);
100 precache_url_table_
->AddURL(GURL("http://end.com"), kEndTime
);
101 precache_url_table_
->AddURL(GURL("http://after.com"), kAfterTime
);
103 precache_url_table_
->DeleteAllPrecachedBefore(kEndTime
);
105 std::map
<GURL
, base::Time
> expected_map
;
106 expected_map
[GURL("http://end.com")] = kEndTime
;
107 expected_map
[GURL("http://after.com")] = kAfterTime
;
109 std::map
<GURL
, base::Time
> actual_map
;
110 precache_url_table_
->GetAllDataForTesting(&actual_map
);
111 EXPECT_EQ(expected_map
, actual_map
);
116 } // namespace precache