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_service.h" | 5 #include "components/suggestions/suggestions_service.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <sstream> | 8 #include <sstream> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 28 matching lines...) Expand all Loading... |
39 namespace { | 39 namespace { |
40 | 40 |
41 const char kFakeSuggestionsURL[] = "https://mysuggestions.com/proto"; | 41 const char kFakeSuggestionsURL[] = "https://mysuggestions.com/proto"; |
42 const char kFakeSuggestionsCommonParams[] = "foo=bar"; | 42 const char kFakeSuggestionsCommonParams[] = "foo=bar"; |
43 const char kFakeBlacklistPath[] = "/blacklist"; | 43 const char kFakeBlacklistPath[] = "/blacklist"; |
44 const char kFakeBlacklistUrlParam[] = "baz"; | 44 const char kFakeBlacklistUrlParam[] = "baz"; |
45 | 45 |
46 const char kTestTitle[] = "a title"; | 46 const char kTestTitle[] = "a title"; |
47 const char kTestUrl[] = "http://go.com"; | 47 const char kTestUrl[] = "http://go.com"; |
48 const char kBlacklistUrl[] = "http://blacklist.com"; | 48 const char kBlacklistUrl[] = "http://blacklist.com"; |
| 49 const int64 kTestDefaultExpiry = 1402200000000000; |
| 50 const int64 kTestSetExpiry = 1404792000000000; |
49 | 51 |
50 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( | 52 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( |
51 const GURL& url, net::URLFetcherDelegate* delegate, | 53 const GURL& url, net::URLFetcherDelegate* delegate, |
52 const std::string& response_data, net::HttpStatusCode response_code, | 54 const std::string& response_data, net::HttpStatusCode response_code, |
53 net::URLRequestStatus::Status status) { | 55 net::URLRequestStatus::Status status) { |
54 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( | 56 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( |
55 url, delegate, response_data, response_code, status)); | 57 url, delegate, response_data, response_code, status)); |
56 | 58 |
57 if (response_code == net::HTTP_OK) { | 59 if (response_code == net::HTTP_OK) { |
58 scoped_refptr<net::HttpResponseHeaders> download_headers( | 60 scoped_refptr<net::HttpResponseHeaders> download_headers( |
(...skipping 25 matching lines...) Expand all Loading... |
84 | 86 |
85 } // namespace | 87 } // namespace |
86 | 88 |
87 namespace suggestions { | 89 namespace suggestions { |
88 | 90 |
89 scoped_ptr<SuggestionsProfile> CreateSuggestionsProfile() { | 91 scoped_ptr<SuggestionsProfile> CreateSuggestionsProfile() { |
90 scoped_ptr<SuggestionsProfile> profile(new SuggestionsProfile()); | 92 scoped_ptr<SuggestionsProfile> profile(new SuggestionsProfile()); |
91 ChromeSuggestion* suggestion = profile->add_suggestions(); | 93 ChromeSuggestion* suggestion = profile->add_suggestions(); |
92 suggestion->set_title(kTestTitle); | 94 suggestion->set_title(kTestTitle); |
93 suggestion->set_url(kTestUrl); | 95 suggestion->set_url(kTestUrl); |
| 96 suggestion->set_expiry_ts(kTestSetExpiry); |
94 return profile.Pass(); | 97 return profile.Pass(); |
95 } | 98 } |
96 | 99 |
| 100 // Creates one suggestion with expiry timestamp and one without. |
| 101 SuggestionsProfile CreateSuggestionsProfileWithExpiryTimestamps() { |
| 102 SuggestionsProfile profile; |
| 103 ChromeSuggestion* suggestion = profile.add_suggestions(); |
| 104 suggestion->set_title(kTestTitle); |
| 105 suggestion->set_url(kTestUrl); |
| 106 suggestion->set_expiry_ts(kTestSetExpiry); |
| 107 |
| 108 suggestion = profile.add_suggestions(); |
| 109 suggestion->set_title(kTestTitle); |
| 110 suggestion->set_url(kTestUrl); |
| 111 |
| 112 return profile; |
| 113 } |
| 114 |
97 class MockSuggestionsStore : public suggestions::SuggestionsStore { | 115 class MockSuggestionsStore : public suggestions::SuggestionsStore { |
98 public: | 116 public: |
99 MOCK_METHOD1(LoadSuggestions, bool(SuggestionsProfile*)); | 117 MOCK_METHOD1(LoadSuggestions, bool(SuggestionsProfile*)); |
100 MOCK_METHOD1(StoreSuggestions, bool(const SuggestionsProfile&)); | 118 MOCK_METHOD1(StoreSuggestions, bool(const SuggestionsProfile&)); |
101 MOCK_METHOD0(ClearSuggestions, void()); | 119 MOCK_METHOD0(ClearSuggestions, void()); |
102 }; | 120 }; |
103 | 121 |
104 class MockImageManager : public suggestions::ImageManager { | 122 class MockImageManager : public suggestions::ImageManager { |
105 public: | 123 public: |
106 MockImageManager() {} | 124 MockImageManager() {} |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 | 213 |
196 void FetchSuggestionsDataNoTimeoutHelper(bool interleaved_requests) { | 214 void FetchSuggestionsDataNoTimeoutHelper(bool interleaved_requests) { |
197 // Field trial enabled with a specific suggestions URL. | 215 // Field trial enabled with a specific suggestions URL. |
198 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, | 216 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
199 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); | 217 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
200 scoped_ptr<SuggestionsService> suggestions_service( | 218 scoped_ptr<SuggestionsService> suggestions_service( |
201 CreateSuggestionsServiceWithMocks()); | 219 CreateSuggestionsServiceWithMocks()); |
202 EXPECT_TRUE(suggestions_service != NULL); | 220 EXPECT_TRUE(suggestions_service != NULL); |
203 scoped_ptr<SuggestionsProfile> suggestions_profile( | 221 scoped_ptr<SuggestionsProfile> suggestions_profile( |
204 CreateSuggestionsProfile()); | 222 CreateSuggestionsProfile()); |
205 | |
206 // Set up net::FakeURLFetcherFactory. | 223 // Set up net::FakeURLFetcherFactory. |
207 std::string expected_url = | 224 std::string expected_url = |
208 (std::string(kFakeSuggestionsURL) + "?") + kFakeSuggestionsCommonParams; | 225 (std::string(kFakeSuggestionsURL) + "?") + kFakeSuggestionsCommonParams; |
209 factory_.SetFakeResponse(GURL(expected_url), | 226 factory_.SetFakeResponse(GURL(expected_url), |
210 suggestions_profile->SerializeAsString(), | 227 suggestions_profile->SerializeAsString(), |
211 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 228 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
212 | |
213 // Set up expectations on the SuggestionsStore. The number depends on | 229 // Set up expectations on the SuggestionsStore. The number depends on |
214 // whether the second request is issued (it won't be issued if the second | 230 // whether the second request is issued (it won't be issued if the second |
215 // fetch occurs before the first request has completed). | 231 // fetch occurs before the first request has completed). |
216 int expected_count = interleaved_requests ? 1 : 2; | 232 int expected_count = interleaved_requests ? 1 : 2; |
217 EXPECT_CALL(*mock_suggestions_store_, | 233 EXPECT_CALL(*mock_suggestions_store_, |
218 StoreSuggestions(EqualsProto(*suggestions_profile))) | 234 StoreSuggestions(EqualsProto(*suggestions_profile))) |
219 .Times(expected_count) | 235 .Times(expected_count) |
220 .WillRepeatedly(Return(true)); | 236 .WillRepeatedly(Return(true)); |
221 | 237 |
222 // Expect a call to the blacklist store. Return that there's nothing to | 238 // Expect a call to the blacklist store. Return that there's nothing to |
(...skipping 267 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 | 506 |
491 // Delay increases on failure. | 507 // Delay increases on failure. |
492 suggestions_service->UpdateBlacklistDelay(false); | 508 suggestions_service->UpdateBlacklistDelay(false); |
493 EXPECT_GT(suggestions_service->blacklist_delay(), initial_delay); | 509 EXPECT_GT(suggestions_service->blacklist_delay(), initial_delay); |
494 | 510 |
495 // Delay resets on success. | 511 // Delay resets on success. |
496 suggestions_service->UpdateBlacklistDelay(true); | 512 suggestions_service->UpdateBlacklistDelay(true); |
497 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); | 513 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); |
498 } | 514 } |
499 | 515 |
| 516 TEST_F(SuggestionsServiceTest, CheckDefaultTimeStamps) { |
| 517 scoped_ptr<SuggestionsService> suggestions_service( |
| 518 CreateSuggestionsServiceWithMocks()); |
| 519 SuggestionsProfile suggestions = |
| 520 CreateSuggestionsProfileWithExpiryTimestamps(); |
| 521 suggestions_service->SetDefaultExpiryTimestamp(&suggestions, |
| 522 kTestDefaultExpiry); |
| 523 EXPECT_EQ(kTestSetExpiry, suggestions.suggestions(0).expiry_ts()); |
| 524 EXPECT_EQ(kTestDefaultExpiry, suggestions.suggestions(1).expiry_ts()); |
| 525 } |
500 } // namespace suggestions | 526 } // namespace suggestions |
OLD | NEW |