OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/suggestions/suggestions_store.h" | 5 #include "components/suggestions/suggestions_store.h" |
6 | 6 |
| 7 #include "base/test/simple_test_clock.h" |
7 #include "base/time/time.h" | 8 #include "base/time/time.h" |
8 #include "components/pref_registry/testing_pref_service_syncable.h" | 9 #include "components/pref_registry/testing_pref_service_syncable.h" |
9 #include "components/suggestions/proto/suggestions.pb.h" | 10 #include "components/suggestions/proto/suggestions.pb.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
11 | 12 |
12 using user_prefs::TestingPrefServiceSyncable; | 13 using user_prefs::TestingPrefServiceSyncable; |
13 | 14 |
14 namespace suggestions { | 15 namespace suggestions { |
15 | 16 |
16 namespace { | 17 namespace { |
(...skipping 10 matching lines...) Expand all Loading... |
27 } | 28 } |
28 | 29 |
29 SuggestionsProfile CreateTestSuggestions() { | 30 SuggestionsProfile CreateTestSuggestions() { |
30 SuggestionsProfile suggestions; | 31 SuggestionsProfile suggestions; |
31 ChromeSuggestion* suggestion = suggestions.add_suggestions(); | 32 ChromeSuggestion* suggestion = suggestions.add_suggestions(); |
32 suggestion->set_url(kTestTitle); | 33 suggestion->set_url(kTestTitle); |
33 suggestion->set_title(kTestUrl); | 34 suggestion->set_title(kTestUrl); |
34 return suggestions; | 35 return suggestions; |
35 } | 36 } |
36 | 37 |
37 SuggestionsProfile CreateTestSuggestionsProfileWithExpiry(int expired_count, | 38 SuggestionsProfile CreateTestSuggestionsProfileWithExpiry( |
38 int valid_count) { | 39 base::Time current_time, |
39 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) | 40 int expired_count, |
40 .ToInternalValue(); | 41 int valid_count) { |
| 42 int64 current_time_usec = |
| 43 (current_time - base::Time::UnixEpoch()).ToInternalValue(); |
41 int64 offset_usec = 5 * base::Time::kMicrosecondsPerMinute; | 44 int64 offset_usec = 5 * base::Time::kMicrosecondsPerMinute; |
42 | 45 |
43 SuggestionsProfile suggestions; | 46 SuggestionsProfile suggestions; |
44 for (int i = 1; i <= valid_count; i++) | 47 for (int i = 1; i <= valid_count; i++) |
45 AddSuggestion(&suggestions, kTestTitle, kTestUrl, | 48 AddSuggestion(&suggestions, kTestTitle, kTestUrl, |
46 now_usec + offset_usec * i); | 49 current_time_usec + offset_usec * i); |
47 for (int i = 1; i <= expired_count; i++) | 50 for (int i = 1; i <= expired_count; i++) |
48 AddSuggestion(&suggestions, kTestTitle, kTestUrl, | 51 AddSuggestion(&suggestions, kTestTitle, kTestUrl, |
49 now_usec - offset_usec * i); | 52 current_time_usec - offset_usec * i); |
50 | 53 |
51 return suggestions; | 54 return suggestions; |
52 } | 55 } |
53 | 56 |
54 void ValidateSuggestions(const SuggestionsProfile& expected, | 57 void ValidateSuggestions(const SuggestionsProfile& expected, |
55 const SuggestionsProfile& actual) { | 58 const SuggestionsProfile& actual) { |
56 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size()); | 59 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size()); |
57 for (int i = 0; i < expected.suggestions_size(); ++i) { | 60 for (int i = 0; i < expected.suggestions_size(); ++i) { |
58 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); | 61 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); |
59 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); | 62 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); |
60 EXPECT_EQ(expected.suggestions(i).expiry_ts(), | 63 EXPECT_EQ(expected.suggestions(i).expiry_ts(), |
61 actual.suggestions(i).expiry_ts()); | 64 actual.suggestions(i).expiry_ts()); |
62 EXPECT_EQ(expected.suggestions(i).favicon_url(), | 65 EXPECT_EQ(expected.suggestions(i).favicon_url(), |
63 actual.suggestions(i).favicon_url()); | 66 actual.suggestions(i).favicon_url()); |
64 EXPECT_EQ(expected.suggestions(i).thumbnail(), | 67 EXPECT_EQ(expected.suggestions(i).thumbnail(), |
65 actual.suggestions(i).thumbnail()); | 68 actual.suggestions(i).thumbnail()); |
66 } | 69 } |
67 } | 70 } |
68 | 71 |
69 } // namespace | 72 } // namespace |
70 | 73 |
71 class SuggestionsStoreTest : public testing::Test { | 74 class SuggestionsStoreTest : public testing::Test { |
72 public: | 75 public: |
73 SuggestionsStoreTest() | 76 SuggestionsStoreTest() |
74 : pref_service_(new user_prefs::TestingPrefServiceSyncable) {} | 77 : pref_service_(new user_prefs::TestingPrefServiceSyncable) {} |
75 | 78 |
76 void SetUp() override { | 79 void SetUp() override { |
77 SuggestionsStore::RegisterProfilePrefs(pref_service_->registry()); | 80 SuggestionsStore::RegisterProfilePrefs(pref_service_->registry()); |
78 suggestions_store_.reset(new SuggestionsStore(pref_service_.get())); | 81 suggestions_store_.reset(new SuggestionsStore(pref_service_.get())); |
| 82 |
| 83 base::SimpleTestClock* test_clock(new base::SimpleTestClock()); |
| 84 current_time = base::Time::FromInternalValue(13063394337546738); |
| 85 test_clock->SetNow(current_time); |
| 86 suggestions_store_->SetClockForTesting(scoped_ptr<base::Clock>(test_clock)); |
79 } | 87 } |
80 | 88 |
81 protected: | 89 protected: |
82 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; | 90 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; |
83 scoped_ptr<SuggestionsStore> suggestions_store_; | 91 scoped_ptr<SuggestionsStore> suggestions_store_; |
| 92 base::Time current_time; |
84 | 93 |
85 DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest); | 94 DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest); |
86 }; | 95 }; |
87 | 96 |
88 // Tests LoadSuggestions function to filter expired suggestions. | 97 // Tests LoadSuggestions function to filter expired suggestions. |
89 TEST_F(SuggestionsStoreTest, LoadAllExpired) { | 98 TEST_F(SuggestionsStoreTest, LoadAllExpired) { |
90 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 0); | 99 SuggestionsProfile suggestions = |
| 100 CreateTestSuggestionsProfileWithExpiry(current_time, 5, 0); |
91 SuggestionsProfile filtered_suggestions; | 101 SuggestionsProfile filtered_suggestions; |
92 | 102 |
93 // Store and load. Expired suggestions should not be loaded. | 103 // Store and load. Expired suggestions should not be loaded. |
94 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); | 104 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); |
95 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); | 105 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); |
96 EXPECT_EQ(0, filtered_suggestions.suggestions_size()); | 106 EXPECT_EQ(0, filtered_suggestions.suggestions_size()); |
97 } | 107 } |
98 | 108 |
99 // Tests LoadSuggestions function to filter expired suggestions. | 109 // Tests LoadSuggestions function to filter expired suggestions. |
100 TEST_F(SuggestionsStoreTest, LoadValidAndExpired) { | 110 TEST_F(SuggestionsStoreTest, LoadValidAndExpired) { |
101 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3); | 111 SuggestionsProfile suggestions = |
| 112 CreateTestSuggestionsProfileWithExpiry(current_time, 5, 3); |
102 SuggestionsProfile filtered_suggestions; | 113 SuggestionsProfile filtered_suggestions; |
103 | 114 |
104 // Store and load. Expired suggestions should not be loaded. | 115 // Store and load. Expired suggestions should not be loaded. |
105 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); | 116 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); |
106 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); | 117 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); |
107 EXPECT_EQ(3, filtered_suggestions.suggestions_size()); | 118 EXPECT_EQ(3, filtered_suggestions.suggestions_size()); |
108 } | 119 } |
109 | 120 |
110 // Tests LoadSuggestions function to filter expired suggestions. | 121 // Tests LoadSuggestions function to filter expired suggestions. |
111 TEST_F(SuggestionsStoreTest, CheckStoreAfterLoadExpired) { | 122 TEST_F(SuggestionsStoreTest, CheckStoreAfterLoadExpired) { |
112 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3); | 123 SuggestionsProfile suggestions = |
| 124 CreateTestSuggestionsProfileWithExpiry(current_time, 5, 3); |
113 SuggestionsProfile filtered_suggestions; | 125 SuggestionsProfile filtered_suggestions; |
114 | 126 |
115 // Store and load. Expired suggestions should not be loaded. | 127 // Store and load. Expired suggestions should not be loaded. |
116 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); | 128 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); |
117 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); | 129 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); |
118 | 130 |
119 SuggestionsProfile loaded_suggestions; | 131 SuggestionsProfile loaded_suggestions; |
120 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&loaded_suggestions)); | 132 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&loaded_suggestions)); |
121 EXPECT_EQ(3, loaded_suggestions.suggestions_size()); | 133 EXPECT_EQ(3, loaded_suggestions.suggestions_size()); |
122 ValidateSuggestions(filtered_suggestions, loaded_suggestions); | 134 ValidateSuggestions(filtered_suggestions, loaded_suggestions); |
(...skipping 13 matching lines...) Expand all Loading... |
136 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); | 148 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); |
137 ValidateSuggestions(suggestions, recovered_suggestions); | 149 ValidateSuggestions(suggestions, recovered_suggestions); |
138 | 150 |
139 // Clear. | 151 // Clear. |
140 suggestions_store_->ClearSuggestions(); | 152 suggestions_store_->ClearSuggestions(); |
141 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); | 153 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); |
142 ValidateSuggestions(empty_suggestions, recovered_suggestions); | 154 ValidateSuggestions(empty_suggestions, recovered_suggestions); |
143 } | 155 } |
144 | 156 |
145 } // namespace suggestions | 157 } // namespace suggestions |
OLD | NEW |