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 expiry_time, int expired_count, int valid_count) { |
Matt Giuca
2014/12/18 22:51:14
The name "expiry_time" is misleading. The expiry t
gayane -on leave until 09-2017
2014/12/22 16:29:38
Done.
| |
39 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) | 40 int64 expiry_usec = (expiry_time - base::Time::UnixEpoch()) |
Matt Giuca
2014/12/18 22:51:13
Indentation (git cl format).
Matt Giuca
2014/12/18 22:51:14
As above, this is not an expiry time, it is a simu
gayane -on leave until 09-2017
2014/12/22 16:29:38
Done.
gayane -on leave until 09-2017
2014/12/22 16:29:38
Renamed to current_time_usec, because having one n
| |
40 .ToInternalValue(); | 41 .ToInternalValue(); |
41 int64 offset_usec = 5 * base::Time::kMicrosecondsPerMinute; | 42 int64 offset_usec = 5 * base::Time::kMicrosecondsPerMinute; |
42 | 43 |
43 SuggestionsProfile suggestions; | 44 SuggestionsProfile suggestions; |
44 for (int i = 1; i <= valid_count; i++) | 45 for (int i = 1; i <= valid_count; i++) |
45 AddSuggestion(&suggestions, kTestTitle, kTestUrl, | 46 AddSuggestion(&suggestions, kTestTitle, kTestUrl, |
46 now_usec + offset_usec * i); | 47 expiry_usec + offset_usec * i); |
47 for (int i = 1; i <= expired_count; i++) | 48 for (int i = 1; i <= expired_count; i++) |
48 AddSuggestion(&suggestions, kTestTitle, kTestUrl, | 49 AddSuggestion(&suggestions, kTestTitle, kTestUrl, |
49 now_usec - offset_usec * i); | 50 expiry_usec - offset_usec * i); |
50 | 51 |
51 return suggestions; | 52 return suggestions; |
52 } | 53 } |
53 | 54 |
54 void ValidateSuggestions(const SuggestionsProfile& expected, | 55 void ValidateSuggestions(const SuggestionsProfile& expected, |
55 const SuggestionsProfile& actual) { | 56 const SuggestionsProfile& actual) { |
56 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size()); | 57 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size()); |
57 for (int i = 0; i < expected.suggestions_size(); ++i) { | 58 for (int i = 0; i < expected.suggestions_size(); ++i) { |
58 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); | 59 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); |
59 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); | 60 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); |
60 EXPECT_EQ(expected.suggestions(i).expiry_ts(), | 61 EXPECT_EQ(expected.suggestions(i).expiry_ts(), |
61 actual.suggestions(i).expiry_ts()); | 62 actual.suggestions(i).expiry_ts()); |
62 EXPECT_EQ(expected.suggestions(i).favicon_url(), | 63 EXPECT_EQ(expected.suggestions(i).favicon_url(), |
63 actual.suggestions(i).favicon_url()); | 64 actual.suggestions(i).favicon_url()); |
64 EXPECT_EQ(expected.suggestions(i).thumbnail(), | 65 EXPECT_EQ(expected.suggestions(i).thumbnail(), |
65 actual.suggestions(i).thumbnail()); | 66 actual.suggestions(i).thumbnail()); |
66 } | 67 } |
67 } | 68 } |
68 | 69 |
69 } // namespace | 70 } // namespace |
70 | 71 |
71 class SuggestionsStoreTest : public testing::Test { | 72 class SuggestionsStoreTest : public testing::Test { |
72 public: | 73 public: |
73 SuggestionsStoreTest() | 74 SuggestionsStoreTest() |
74 : pref_service_(new user_prefs::TestingPrefServiceSyncable) {} | 75 : pref_service_(new user_prefs::TestingPrefServiceSyncable) {} |
75 | 76 |
76 void SetUp() override { | 77 void SetUp() override { |
77 SuggestionsStore::RegisterProfilePrefs(pref_service_->registry()); | 78 SuggestionsStore::RegisterProfilePrefs(pref_service_->registry()); |
78 suggestions_store_.reset(new SuggestionsStore(pref_service_.get())); | 79 suggestions_store_.reset(new SuggestionsStore(pref_service_.get())); |
80 | |
81 base::SimpleTestClock* test_clock(new base::SimpleTestClock()); | |
82 expiry_time = base::Time::FromInternalValue(13063394337546738); | |
83 test_clock->SetNow(expiry_time); | |
84 suggestions_store_->SetClockForTesting(scoped_ptr<base::Clock>(test_clock)); | |
79 } | 85 } |
80 | 86 |
81 protected: | 87 protected: |
82 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; | 88 scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_; |
83 scoped_ptr<SuggestionsStore> suggestions_store_; | 89 scoped_ptr<SuggestionsStore> suggestions_store_; |
90 base::Time expiry_time; | |
Matt Giuca
2014/12/18 22:51:14
As above, this is not an expiry time. Rename to cu
gayane -on leave until 09-2017
2014/12/22 16:29:38
Done.
| |
84 | 91 |
85 DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest); | 92 DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest); |
86 }; | 93 }; |
87 | 94 |
88 // Tests LoadSuggestions function to filter expired suggestions. | 95 // Tests LoadSuggestions function to filter expired suggestions. |
89 TEST_F(SuggestionsStoreTest, LoadAllExpired) { | 96 TEST_F(SuggestionsStoreTest, LoadAllExpired) { |
90 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 0); | 97 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry( |
98 expiry_time, 5, 0); | |
91 SuggestionsProfile filtered_suggestions; | 99 SuggestionsProfile filtered_suggestions; |
92 | 100 |
93 // Store and load. Expired suggestions should not be loaded. | 101 // Store and load. Expired suggestions should not be loaded. |
94 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); | 102 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); |
95 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); | 103 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); |
96 EXPECT_EQ(0, filtered_suggestions.suggestions_size()); | 104 EXPECT_EQ(0, filtered_suggestions.suggestions_size()); |
97 } | 105 } |
98 | 106 |
99 // Tests LoadSuggestions function to filter expired suggestions. | 107 // Tests LoadSuggestions function to filter expired suggestions. |
100 TEST_F(SuggestionsStoreTest, LoadValidAndExpired) { | 108 TEST_F(SuggestionsStoreTest, LoadValidAndExpired) { |
101 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3); | 109 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry( |
110 expiry_time, 5, 3); | |
102 SuggestionsProfile filtered_suggestions; | 111 SuggestionsProfile filtered_suggestions; |
103 | 112 |
104 // Store and load. Expired suggestions should not be loaded. | 113 // Store and load. Expired suggestions should not be loaded. |
105 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); | 114 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); |
106 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); | 115 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); |
107 EXPECT_EQ(3, filtered_suggestions.suggestions_size()); | 116 EXPECT_EQ(3, filtered_suggestions.suggestions_size()); |
108 } | 117 } |
109 | 118 |
110 // Tests LoadSuggestions function to filter expired suggestions. | 119 // Tests LoadSuggestions function to filter expired suggestions. |
111 TEST_F(SuggestionsStoreTest, CheckStoreAfterLoadExpired) { | 120 TEST_F(SuggestionsStoreTest, CheckStoreAfterLoadExpired) { |
112 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3); | 121 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry( |
122 expiry_time, 5, 3); | |
113 SuggestionsProfile filtered_suggestions; | 123 SuggestionsProfile filtered_suggestions; |
114 | 124 |
115 // Store and load. Expired suggestions should not be loaded. | 125 // Store and load. Expired suggestions should not be loaded. |
116 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); | 126 EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions)); |
117 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); | 127 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions)); |
118 | 128 |
119 SuggestionsProfile loaded_suggestions; | 129 SuggestionsProfile loaded_suggestions; |
120 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&loaded_suggestions)); | 130 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&loaded_suggestions)); |
121 EXPECT_EQ(3, loaded_suggestions.suggestions_size()); | 131 EXPECT_EQ(3, loaded_suggestions.suggestions_size()); |
122 ValidateSuggestions(filtered_suggestions, loaded_suggestions); | 132 ValidateSuggestions(filtered_suggestions, loaded_suggestions); |
(...skipping 13 matching lines...) Expand all Loading... | |
136 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); | 146 EXPECT_TRUE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); |
137 ValidateSuggestions(suggestions, recovered_suggestions); | 147 ValidateSuggestions(suggestions, recovered_suggestions); |
138 | 148 |
139 // Clear. | 149 // Clear. |
140 suggestions_store_->ClearSuggestions(); | 150 suggestions_store_->ClearSuggestions(); |
141 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); | 151 EXPECT_FALSE(suggestions_store_->LoadSuggestions(&recovered_suggestions)); |
142 ValidateSuggestions(empty_suggestions, recovered_suggestions); | 152 ValidateSuggestions(empty_suggestions, recovered_suggestions); |
143 } | 153 } |
144 | 154 |
145 } // namespace suggestions | 155 } // namespace suggestions |
OLD | NEW |