1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/suggestions/suggestions_store.h"
#include "base/time/time.h"
#include "components/pref_registry/testing_pref_service_syncable.h"
#include "components/suggestions/proto/suggestions.pb.h"
#include "testing/gtest/include/gtest/gtest.h"
using user_prefs::TestingPrefServiceSyncable;
namespace suggestions {
namespace {
const char kTestTitle[] = "Foo site";
const char kTestUrl[] = "http://foo.com/";
const int kTimeGapUsec = 100000;
void AddSuggestion(SuggestionsProfile* suggestions, const char *title,
const char *url, int64 expiry_ts) {
ChromeSuggestion* suggestion = suggestions->add_suggestions();
suggestion->set_url(title);
suggestion->set_title(url);
suggestion->set_expiry_ts(expiry_ts);
}
SuggestionsProfile CreateTestSuggestions() {
SuggestionsProfile suggestions;
ChromeSuggestion* suggestion = suggestions.add_suggestions();
suggestion->set_url(kTestTitle);
suggestion->set_title(kTestUrl);
return suggestions;
}
SuggestionsProfile CreateTestSuggestionsProfileWithExpiry(int expired_count,
int valid_count) {
int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch())
.ToInternalValue();
srand(7); // Constant seed for rand() function.
int64 offset_limit_usec = 30 * base::Time::kMicrosecondsPerDay;
int64 offset_usec = rand() % offset_limit_usec + kTimeGapUsec;
SuggestionsProfile suggestions;
for (int i = 0; i < valid_count; i++)
AddSuggestion(&suggestions, kTestTitle, kTestUrl, now_usec + offset_usec);
for (int i = 0; i < expired_count; i++)
AddSuggestion(&suggestions, kTestTitle, kTestUrl, now_usec - offset_usec);
return suggestions;
}
void ValidateSuggestions(const SuggestionsProfile& expected,
const SuggestionsProfile& actual) {
EXPECT_EQ(expected.suggestions_size(), actual.suggestions_size());
for (int i = 0; i < expected.suggestions_size(); ++i) {
EXPECT_EQ(expected.suggestions(i).url(), actual.suggestions(i).url());
EXPECT_EQ(expected.suggestions(i).title(), actual.suggestions(i).title());
EXPECT_EQ(expected.suggestions(i).expiry_ts(),
actual.suggestions(i).expiry_ts());
EXPECT_EQ(expected.suggestions(i).favicon_url(),
actual.suggestions(i).favicon_url());
EXPECT_EQ(expected.suggestions(i).thumbnail(),
actual.suggestions(i).thumbnail());
}
}
} // namespace
class SuggestionsStoreTest : public testing::Test {
public:
SuggestionsStoreTest()
: pref_service_(new user_prefs::TestingPrefServiceSyncable) {}
virtual void SetUp() OVERRIDE {
SuggestionsStore::RegisterProfilePrefs(pref_service_->registry());
suggestions_store_.reset(new SuggestionsStore(pref_service_.get()));
}
protected:
scoped_ptr<user_prefs::TestingPrefServiceSyncable> pref_service_;
scoped_ptr<SuggestionsStore> suggestions_store_;
DISALLOW_COPY_AND_ASSIGN(SuggestionsStoreTest);
};
// Tests LoadSuggestions function to filter expired suggestions.
TEST_F(SuggestionsStoreTest, LoadAllExpired) {
SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 0);
SuggestionsProfile filtered_suggestions;
// Store and load. Expired suggestions should not be loaded.
EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions));
EXPECT_FALSE(suggestions_store_->LoadSuggestions(&filtered_suggestions));
EXPECT_EQ(0, filtered_suggestions.suggestions_size());
}
// Tests LoadSuggestions function to filter expired suggestions.
TEST_F(SuggestionsStoreTest, LoadValidAndExpired) {
SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3);
SuggestionsProfile filtered_suggestions;
// Store and load. Expired suggestions should not be loaded.
EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions));
EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions));
EXPECT_EQ(3, filtered_suggestions.suggestions_size());
}
// Tests LoadSuggestions function to filter expired suggestions.
TEST_F(SuggestionsStoreTest, CheckStoreAfterLoadExpired) {
SuggestionsProfile suggestions = CreateTestSuggestionsProfileWithExpiry(5, 3);
SuggestionsProfile filtered_suggestions;
// Store and load. Expired suggestions should not be loaded.
EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions));
EXPECT_TRUE(suggestions_store_->LoadSuggestions(&filtered_suggestions));
SuggestionsProfile loaded_suggestions;
EXPECT_TRUE(suggestions_store_->LoadSuggestions(&loaded_suggestions));
EXPECT_EQ(3, loaded_suggestions.suggestions_size());
ValidateSuggestions(filtered_suggestions, loaded_suggestions);
}
TEST_F(SuggestionsStoreTest, LoadStoreClear) {
const SuggestionsProfile suggestions = CreateTestSuggestions();
const SuggestionsProfile empty_suggestions;
SuggestionsProfile recovered_suggestions;
// Attempt to load when prefs are empty.
EXPECT_FALSE(suggestions_store_->LoadSuggestions(&recovered_suggestions));
ValidateSuggestions(empty_suggestions, recovered_suggestions);
// Store then reload.
EXPECT_TRUE(suggestions_store_->StoreSuggestions(suggestions));
EXPECT_TRUE(suggestions_store_->LoadSuggestions(&recovered_suggestions));
ValidateSuggestions(suggestions, recovered_suggestions);
// Clear.
suggestions_store_->ClearSuggestions();
EXPECT_FALSE(suggestions_store_->LoadSuggestions(&recovered_suggestions));
ValidateSuggestions(empty_suggestions, recovered_suggestions);
}
} // namespace suggestions
|