OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <stdint.h> |
| 6 |
5 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
6 #include "base/time/time.h" | 8 #include "base/time/time.h" |
7 #include "components/precache/core/precache_session_table.h" | 9 #include "components/precache/core/precache_session_table.h" |
8 #include "components/precache/core/proto/unfinished_work.pb.h" | 10 #include "components/precache/core/proto/unfinished_work.pb.h" |
9 #include "sql/connection.h" | 11 #include "sql/connection.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
11 #include "url/gurl.h" | 13 #include "url/gurl.h" |
12 | 14 |
13 namespace precache { | 15 namespace precache { |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 class PrecacheSessionTableTest : public testing::Test { | 19 class PrecacheSessionTableTest : public testing::Test { |
18 public: | 20 public: |
19 PrecacheSessionTableTest() {} | 21 PrecacheSessionTableTest() {} |
20 ~PrecacheSessionTableTest() override {} | 22 ~PrecacheSessionTableTest() override {} |
21 | 23 |
22 protected: | 24 protected: |
23 void SetUp() override { | 25 void SetUp() override { |
24 precache_session_table_.reset(new PrecacheSessionTable()); | 26 precache_session_table_.reset(new PrecacheSessionTable()); |
25 db_.reset(new sql::Connection()); | 27 db_.reset(new sql::Connection()); |
26 ASSERT_TRUE(db_->OpenInMemory()); | 28 ASSERT_TRUE(db_->OpenInMemory()); |
27 precache_session_table_->Init(db_.get()); | 29 precache_session_table_->Init(db_.get()); |
28 } | 30 } |
29 | 31 |
30 std::unique_ptr<PrecacheSessionTable> precache_session_table_; | 32 std::unique_ptr<PrecacheSessionTable> precache_session_table_; |
31 std::unique_ptr<sql::Connection> db_; | 33 std::unique_ptr<sql::Connection> db_; |
32 }; | 34 }; |
33 | 35 |
| 36 TEST_F(PrecacheSessionTableTest, LastPrecacheTimestamp) { |
| 37 const base::Time sometime = base::Time::FromDoubleT(42); |
| 38 |
| 39 precache_session_table_->SetLastPrecacheTimestamp(sometime); |
| 40 |
| 41 EXPECT_EQ(sometime, precache_session_table_->GetLastPrecacheTimestamp()); |
| 42 |
| 43 precache_session_table_->DeleteLastPrecacheTimestamp(); |
| 44 |
| 45 EXPECT_EQ(base::Time(), precache_session_table_->GetLastPrecacheTimestamp()); |
| 46 } |
| 47 |
| 48 TEST_F(PrecacheSessionTableTest, LastPrecacheTimestampSaturatedInt64) { |
| 49 // If I merely add 1 it won't change the double due to the lack of resolution: |
| 50 // 63 bits of int64 vs 55 of double. |
| 51 const double beyond_int64_seconds = |
| 52 static_cast<double>(std::numeric_limits<int64_t>::max()) * 2; |
| 53 const base::Time large_time = base::Time::FromDoubleT(beyond_int64_seconds); |
| 54 |
| 55 precache_session_table_->SetLastPrecacheTimestamp(large_time); |
| 56 |
| 57 EXPECT_EQ(large_time, precache_session_table_->GetLastPrecacheTimestamp()); |
| 58 // Anyway, base::Time does int64_t saturation for us, so these saturated casts |
| 59 // are not strictly needed with the current implementation of base::Time. |
| 60 EXPECT_TRUE(large_time.is_max()); |
| 61 } |
| 62 |
34 TEST_F(PrecacheSessionTableTest, SaveAndGetUnfinishedWork) { | 63 TEST_F(PrecacheSessionTableTest, SaveAndGetUnfinishedWork) { |
35 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( | 64 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( |
36 new PrecacheUnfinishedWork()); | 65 new PrecacheUnfinishedWork()); |
37 unfinished_work->add_top_host()->set_hostname("foo.com"); | 66 unfinished_work->add_top_host()->set_hostname("foo.com"); |
38 unfinished_work->add_top_host()->set_hostname("bar.com"); | 67 unfinished_work->add_top_host()->set_hostname("bar.com"); |
39 auto s = unfinished_work->mutable_config_settings(); | 68 auto s = unfinished_work->mutable_config_settings(); |
40 s->set_top_sites_count(11); | 69 s->set_top_sites_count(11); |
41 s->add_forced_site("baz.com"); | 70 s->add_forced_site("baz.com"); |
42 s->set_top_resources_count(12); | 71 s->set_top_resources_count(12); |
43 s->set_max_bytes_per_resource(501); | 72 s->set_max_bytes_per_resource(501); |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 | 170 |
142 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work2 = | 171 std::unique_ptr<PrecacheUnfinishedWork> unfinished_work2 = |
143 precache_session_table_->GetUnfinishedWork(); | 172 precache_session_table_->GetUnfinishedWork(); |
144 | 173 |
145 EXPECT_EQ(0, unfinished_work2->manifest_size()); | 174 EXPECT_EQ(0, unfinished_work2->manifest_size()); |
146 } | 175 } |
147 | 176 |
148 } // namespace | 177 } // namespace |
149 | 178 |
150 } // namespace precache | 179 } // namespace precache |
OLD | NEW |