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/time/time.h" | |
8 | |
7 #include "components/pref_registry/testing_pref_service_syncable.h" | 9 #include "components/pref_registry/testing_pref_service_syncable.h" |
8 #include "components/suggestions/proto/suggestions.pb.h" | 10 #include "components/suggestions/proto/suggestions.pb.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
10 | 12 |
11 using user_prefs::TestingPrefServiceSyncable; | 13 using user_prefs::TestingPrefServiceSyncable; |
12 | 14 |
13 namespace suggestions { | 15 namespace suggestions { |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 const char kTestTitle[] = "Foo site"; | 19 const char kTestTitle[] = "Foo site"; |
18 const char kTestUrl[] = "http://foo.com/"; | 20 const char kTestUrl[] = "http://foo.com/"; |
19 | 21 |
22 const int time_gap_usec = 1000; | |
Mathieu
2014/08/04 14:39:11
check naming style for constants
gayane -on leave until 09-2017
2014/08/04 16:34:59
Done.
| |
23 | |
24 void AddSuggestion(SuggestionsProfile* suggestions, const char *title, | |
25 const char *url, int64 expiry_ts = 0) { | |
Mathieu
2014/08/04 14:39:11
indent one more space
Mathieu
2014/08/04 14:39:11
Hmm, I don't think I've ever seen default values i
gayane -on leave until 09-2017
2014/08/04 16:34:59
Done.
gayane -on leave until 09-2017
2014/08/04 16:34:59
Done.
| |
26 ChromeSuggestion* suggestion = suggestions->add_suggestions(); | |
27 suggestion->set_url(title); | |
28 suggestion->set_title(url); | |
29 if (expiry_ts != 0) { | |
Mathieu
2014/08/04 14:39:11
if (expiry_ts) {
...
}
But then again you may n
gayane -on leave until 09-2017
2014/08/04 16:34:59
Done.
| |
30 suggestion->set_expiry_ts(expiry_ts); | |
31 } | |
32 } | |
33 | |
20 SuggestionsProfile CreateTestSuggestions() { | 34 SuggestionsProfile CreateTestSuggestions() { |
21 SuggestionsProfile suggestions; | 35 SuggestionsProfile suggestions; |
22 ChromeSuggestion* suggestion = suggestions.add_suggestions(); | 36 AddSuggestion(&suggestions, kTestTitle, kTestUrl); |
23 suggestion->set_url(kTestUrl); | |
24 suggestion->set_title(kTestTitle); | |
25 return suggestions; | 37 return suggestions; |
26 } | 38 } |
27 | 39 |
40 | |
41 SuggestionsProfile CreateTestSuggestionsProfileWithExpiry(int expired_count, | |
42 int valid_count) { | |
Mathieu
2014/08/04 14:39:11
indent 1 more space
gayane -on leave until 09-2017
2014/08/04 16:34:59
Done.
| |
43 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) | |
44 .ToInternalValue(); | |
45 srand(7); // Constant seed for rand() function. | |
46 const int64 offset_limit_usec = 30 * base::Time::kMicrosecondsPerDay; | |
Mathieu
2014/08/04 14:39:11
kConstantName
gayane -on leave until 09-2017
2014/08/04 16:34:59
removed const
| |
47 SuggestionsProfile suggestions; | |
48 for (int i = 0; i < valid_count; i++){ | |
49 int64 offset_usec = rand() % offset_limit_usec + time_gap_usec; | |
50 AddSuggestion(&suggestions, kTestTitle, kTestUrl, now_usec + offset_usec); | |
51 } | |
52 for (int i = 0; i < expired_count; i++){ | |
53 int64 offset_usec = rand() % offset_limit_usec - time_gap_usec; | |
54 AddSuggestion(&suggestions, kTestTitle, kTestUrl, now_usec - offset_usec); | |
55 } | |
56 return suggestions; | |
57 } | |
58 | |
28 void ValidateSuggestions(const SuggestionsProfile& expected, | 59 void ValidateSuggestions(const SuggestionsProfile& expected, |
29 const SuggestionsProfile& actual) { | 60 const SuggestionsProfile& actual) { |
30 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size()); | 61 EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size()); |
31 for (int i = 0; i < expected.suggestions_size(); ++i) { | 62 for (int i = 0; i < expected.suggestions_size(); ++i) { |
32 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); | 63 EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url()); |
33 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); | 64 EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title()); |
65 EXPECT_EQ(expected.suggestions(i).expiry_ts(), | |
66 actual.suggestions(i).expiry_ts()); | |
34 EXPECT_EQ(expected.suggestions(i).favicon_url(), | 67 EXPECT_EQ(expected.suggestions(i).favicon_url(), |
35 actual.suggestions(i).favicon_url()); | 68 actual.suggestions(i).favicon_url()); |
36 EXPECT_EQ(expected.suggestions(i).thumbnail(), | 69 EXPECT_EQ(expected.suggestions(i).thumbnail(), |
37 actual.suggestions(i).thumbnail()); | 70 actual.suggestions(i).thumbnail()); |
38 } | 71 } |
39 } | 72 } |
40 | 73 |
41 } // namespace | 74 } // namespace |
42 | 75 |
76 // Tests LoadSuggestions function to filter expired suggestions. | |
77 TEST(SuggestionsStoreTest, LoadAllExpired) { | |
78 TestingPrefServiceSyncable prefs; | |
79 SuggestionsStore::RegisterProfilePrefs(prefs.registry()); | |
80 SuggestionsStore suggestions_store(&prefs); | |
81 | |
82 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 0); | |
83 SuggestionsProfile filtered_suggestions; | |
84 | |
85 // store and load. expired suggestions should not be loaded. | |
86 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions)); | |
87 EXPECT_FALSE(suggestions_store.LoadSuggestions(&filtered_suggestions)); | |
88 EXPECT_EQ(0, filtered_suggestions.suggestions_size()); | |
89 } | |
90 | |
91 // Tests LoadSuggestions function to filter expired suggestions. | |
92 TEST(SuggestionsStoreTest, LoadValidAndExpired) { | |
93 TestingPrefServiceSyncable prefs; | |
94 SuggestionsStore::RegisterProfilePrefs(prefs.registry()); | |
95 SuggestionsStore suggestions_store(&prefs); | |
96 | |
97 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3); | |
98 SuggestionsProfile filtered_suggestions; | |
99 | |
100 // store and load. expired suggestions should not be loaded. | |
101 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions)); | |
102 EXPECT_TRUE(suggestions_store.LoadSuggestions(&filtered_suggestions)); | |
103 EXPECT_EQ(3, filtered_suggestions.suggestions_size()); | |
104 } | |
105 | |
106 // Tests LoadSuggestions function to filter expired suggestions. | |
107 TEST(SuggestionsStoreTest, CheckStoreAfterLoadExpired) { | |
108 TestingPrefServiceSyncable prefs; | |
109 SuggestionsStore::RegisterProfilePrefs(prefs.registry()); | |
110 SuggestionsStore suggestions_store(&prefs); | |
111 | |
112 SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3); | |
113 SuggestionsProfile filtered_suggestions; | |
114 | |
115 // store and load. expired suggestions should not be loaded. | |
116 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions)); | |
117 EXPECT_TRUE(suggestions_store.LoadSuggestions(&filtered_suggestions)); | |
118 EXPECT_EQ(3, filtered_suggestions.suggestions_size()); | |
119 | |
120 SuggestionsProfile loaded_suggestions; | |
121 EXPECT_TRUE(suggestions_store.LoadSuggestions(&loaded_suggestions)); | |
122 EXPECT_EQ(3, loaded_suggestions.suggestions_size()); | |
123 ValidateSuggestions(filtered_suggestions, loaded_suggestions); | |
124 } | |
125 | |
43 TEST(SuggestionsStoreTest, LoadStoreClear) { | 126 TEST(SuggestionsStoreTest, LoadStoreClear) { |
44 TestingPrefServiceSyncable prefs; | 127 TestingPrefServiceSyncable prefs; |
45 SuggestionsStore::RegisterProfilePrefs(prefs.registry()); | 128 SuggestionsStore::RegisterProfilePrefs(prefs.registry()); |
46 SuggestionsStore suggestions_store(&prefs); | 129 SuggestionsStore suggestions_store(&prefs); |
47 | 130 |
48 const SuggestionsProfile suggestions = CreateTestSuggestions(); | 131 const SuggestionsProfile suggestions = CreateTestSuggestions(); |
49 const SuggestionsProfile empty_suggestions; | 132 const SuggestionsProfile empty_suggestions; |
50 SuggestionsProfile recovered_suggestions; | 133 SuggestionsProfile recovered_suggestions; |
51 | 134 |
52 // Attempt to load when prefs are empty. | 135 // Attempt to load when prefs are empty. |
53 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions)); | 136 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions)); |
54 ValidateSuggestions(empty_suggestions, recovered_suggestions); | 137 ValidateSuggestions(empty_suggestions, recovered_suggestions); |
55 | 138 |
56 // Store then reload. | 139 // Store then reload. |
57 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions)); | 140 EXPECT_TRUE(suggestions_store.StoreSuggestions(suggestions)); |
58 EXPECT_TRUE(suggestions_store.LoadSuggestions(&recovered_suggestions)); | 141 EXPECT_TRUE(suggestions_store.LoadSuggestions(&recovered_suggestions)); |
59 ValidateSuggestions(suggestions, recovered_suggestions); | 142 ValidateSuggestions(suggestions, recovered_suggestions); |
60 | 143 |
61 // Clear. | 144 // Clear. |
62 suggestions_store.ClearSuggestions(); | 145 suggestions_store.ClearSuggestions(); |
63 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions)); | 146 EXPECT_FALSE(suggestions_store.LoadSuggestions(&recovered_suggestions)); |
64 ValidateSuggestions(empty_suggestions, recovered_suggestions); | 147 ValidateSuggestions(empty_suggestions, recovered_suggestions); |
65 } | 148 } |
66 | 149 |
67 } // namespace suggestions | 150 } // namespace suggestions |
OLD | NEW |