Chromium Code Reviews| 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 "chrome/browser/search/suggestions/suggestions_service.h" | 5 #include "chrome/browser/search/suggestions/suggestions_service.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <sstream> | |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/field_trial.h" | 13 #include "base/metrics/field_trial.h" |
| 13 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chrome/browser/history/history_types.h" | 16 #include "chrome/browser/history/history_types.h" |
| 17 #include "chrome/browser/search/suggestions/blacklist_store.h" | |
| 16 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 18 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
| 17 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 19 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 18 #include "chrome/browser/search/suggestions/suggestions_store.h" | 20 #include "chrome/browser/search/suggestions/suggestions_store.h" |
| 19 #include "chrome/test/base/testing_profile.h" | 21 #include "chrome/test/base/testing_profile.h" |
| 20 #include "components/variations/entropy_provider.h" | 22 #include "components/variations/entropy_provider.h" |
| 21 #include "components/variations/variations_associated_data.h" | 23 #include "components/variations/variations_associated_data.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" | 24 #include "content/public/test/test_browser_thread_bundle.h" |
| 23 #include "net/http/http_response_headers.h" | 25 #include "net/http/http_response_headers.h" |
| 24 #include "net/http/http_status_code.h" | 26 #include "net/http/http_status_code.h" |
| 25 #include "net/url_request/test_url_fetcher_factory.h" | 27 #include "net/url_request/test_url_fetcher_factory.h" |
| 26 #include "net/url_request/url_request_status.h" | 28 #include "net/url_request/url_request_status.h" |
| 27 #include "net/url_request/url_request_test_util.h" | 29 #include "net/url_request/url_request_test_util.h" |
| 28 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 29 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 30 | 32 |
| 33 using testing::DoAll; | |
| 34 using ::testing::Eq; | |
| 31 using ::testing::Return; | 35 using ::testing::Return; |
| 36 using testing::SetArgPointee; | |
| 32 using ::testing::StrictMock; | 37 using ::testing::StrictMock; |
| 33 using ::testing::_; | 38 using ::testing::_; |
| 34 | 39 |
| 35 namespace { | 40 namespace { |
| 36 | 41 |
| 37 const char kFakeSuggestionsURL[] = "https://mysuggestions.com/proto"; | 42 const char kFakeSuggestionsURL[] = "https://mysuggestions.com/proto"; |
| 38 const char kFakeSuggestionsSuffix[] = "?foo=bar"; | 43 const char kFakeSuggestionsCommonParams[] = "foo=bar"; |
| 39 const char kFakeBlacklistSuffix[] = "/blacklist?foo=bar&baz="; | 44 const char kFakeBlacklistPath[] = "/blacklist"; |
| 45 const char kFakeBlacklistUrlParam[] = "baz"; | |
| 40 | 46 |
| 41 const char kTestTitle[] = "a title"; | 47 const char kTestTitle[] = "a title"; |
| 42 const char kTestUrl[] = "http://go.com"; | 48 const char kTestUrl[] = "http://go.com"; |
| 43 const char kBlacklistUrl[] = "http://blacklist.com"; | 49 const char kBlacklistUrl[] = "http://blacklist.com"; |
| 44 | 50 |
| 45 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( | 51 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( |
| 46 const GURL& url, net::URLFetcherDelegate* delegate, | 52 const GURL& url, net::URLFetcherDelegate* delegate, |
| 47 const std::string& response_data, net::HttpStatusCode response_code, | 53 const std::string& response_data, net::HttpStatusCode response_code, |
| 48 net::URLRequestStatus::Status status) { | 54 net::URLRequestStatus::Status status) { |
| 49 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( | 55 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( |
| 50 url, delegate, response_data, response_code, status)); | 56 url, delegate, response_data, response_code, status)); |
| 51 | 57 |
| 52 if (response_code == net::HTTP_OK) { | 58 if (response_code == net::HTTP_OK) { |
| 53 scoped_refptr<net::HttpResponseHeaders> download_headers( | 59 scoped_refptr<net::HttpResponseHeaders> download_headers( |
| 54 new net::HttpResponseHeaders("")); | 60 new net::HttpResponseHeaders("")); |
| 55 download_headers->AddHeader("Content-Type: text/html"); | 61 download_headers->AddHeader("Content-Type: text/html"); |
| 56 fetcher->set_response_headers(download_headers); | 62 fetcher->set_response_headers(download_headers); |
| 57 } | 63 } |
| 58 return fetcher.Pass(); | 64 return fetcher.Pass(); |
| 59 } | 65 } |
| 60 | 66 |
| 67 std::string GetExpectedBlacklistRequestUrl(const GURL& blacklist_url) { | |
| 68 std::stringstream request_url; | |
| 69 request_url << kFakeSuggestionsURL << kFakeBlacklistPath << "?" | |
| 70 << kFakeSuggestionsCommonParams << "&" << kFakeBlacklistUrlParam | |
| 71 << "=" << net::EscapeQueryParamValue(blacklist_url.spec(), true); | |
| 72 return request_url.str(); | |
| 73 } | |
| 74 | |
| 61 // GMock matcher for protobuf equality. | 75 // GMock matcher for protobuf equality. |
| 62 MATCHER_P(EqualsProto, message, "") { | 76 MATCHER_P(EqualsProto, message, "") { |
| 63 // This implementation assumes protobuf serialization is deterministic, which | 77 // This implementation assumes protobuf serialization is deterministic, which |
| 64 // is true in practice but technically not something that code is supposed | 78 // is true in practice but technically not something that code is supposed |
| 65 // to rely on. However, it vastly simplifies the implementation. | 79 // to rely on. However, it vastly simplifies the implementation. |
| 66 std::string expected_serialized, actual_serialized; | 80 std::string expected_serialized, actual_serialized; |
| 67 message.SerializeToString(&expected_serialized); | 81 message.SerializeToString(&expected_serialized); |
| 68 arg.SerializeToString(&actual_serialized); | 82 arg.SerializeToString(&actual_serialized); |
| 69 return expected_serialized == actual_serialized; | 83 return expected_serialized == actual_serialized; |
| 70 } | 84 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 83 return profile.Pass(); | 97 return profile.Pass(); |
| 84 } | 98 } |
| 85 | 99 |
| 86 class MockSuggestionsStore : public suggestions::SuggestionsStore { | 100 class MockSuggestionsStore : public suggestions::SuggestionsStore { |
| 87 public: | 101 public: |
| 88 MOCK_METHOD1(LoadSuggestions, bool(SuggestionsProfile*)); | 102 MOCK_METHOD1(LoadSuggestions, bool(SuggestionsProfile*)); |
| 89 MOCK_METHOD1(StoreSuggestions, bool(const SuggestionsProfile&)); | 103 MOCK_METHOD1(StoreSuggestions, bool(const SuggestionsProfile&)); |
| 90 MOCK_METHOD0(ClearSuggestions, void()); | 104 MOCK_METHOD0(ClearSuggestions, void()); |
| 91 }; | 105 }; |
| 92 | 106 |
| 107 class MockBlacklistStore : public suggestions::BlacklistStore { | |
| 108 public: | |
| 109 MOCK_METHOD1(BlacklistUrl, bool(const GURL&)); | |
| 110 MOCK_METHOD1(GetFirstUrlFromBlacklist, bool(GURL*)); | |
| 111 MOCK_METHOD1(RemoveUrl, bool(const GURL&)); | |
| 112 MOCK_METHOD1(FilterSuggestions, void(SuggestionsProfile*)); | |
| 113 }; | |
| 114 | |
| 93 } // namespace | 115 } // namespace |
| 94 | 116 |
| 95 class SuggestionsServiceTest : public testing::Test { | 117 class SuggestionsServiceTest : public testing::Test { |
| 96 public: | 118 public: |
| 97 void CheckSuggestionsData(const SuggestionsProfile& suggestions_profile) { | 119 void CheckSuggestionsData(const SuggestionsProfile& suggestions_profile) { |
| 98 EXPECT_EQ(1, suggestions_profile.suggestions_size()); | 120 EXPECT_EQ(1, suggestions_profile.suggestions_size()); |
| 99 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); | 121 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); |
| 100 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); | 122 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); |
| 101 ++suggestions_data_check_count_; | 123 ++suggestions_data_check_count_; |
| 102 } | 124 } |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 114 : suggestions_data_check_count_(0), | 136 : suggestions_data_check_count_(0), |
| 115 suggestions_empty_data_count_(0), | 137 suggestions_empty_data_count_(0), |
| 116 factory_(NULL, base::Bind(&CreateURLFetcher)), | 138 factory_(NULL, base::Bind(&CreateURLFetcher)), |
| 117 mock_suggestions_store_(NULL) { | 139 mock_suggestions_store_(NULL) { |
| 118 profile_ = profile_builder_.Build(); | 140 profile_ = profile_builder_.Build(); |
| 119 } | 141 } |
| 120 virtual ~SuggestionsServiceTest() {} | 142 virtual ~SuggestionsServiceTest() {} |
| 121 | 143 |
| 122 // Enables the "ChromeSuggestions.Group1" field trial. | 144 // Enables the "ChromeSuggestions.Group1" field trial. |
| 123 void EnableFieldTrial(const std::string& url, | 145 void EnableFieldTrial(const std::string& url, |
| 124 const std::string& suggestions_suffix, | 146 const std::string& common_params, |
| 125 const std::string& blacklist_suffix) { | 147 const std::string& blacklist_path, |
| 148 const std::string& blacklist_url_param) { | |
| 126 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. | 149 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. |
| 127 field_trial_list_.reset(NULL); | 150 field_trial_list_.reset(NULL); |
| 128 field_trial_list_.reset( | 151 field_trial_list_.reset( |
| 129 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); | 152 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); |
| 130 | 153 |
| 131 chrome_variations::testing::ClearAllVariationParams(); | 154 chrome_variations::testing::ClearAllVariationParams(); |
| 132 std::map<std::string, std::string> params; | 155 std::map<std::string, std::string> params; |
| 133 params[kSuggestionsFieldTrialStateParam] = | 156 params[kSuggestionsFieldTrialStateParam] = |
| 134 kSuggestionsFieldTrialStateEnabled; | 157 kSuggestionsFieldTrialStateEnabled; |
| 135 params[kSuggestionsFieldTrialURLParam] = url; | 158 params[kSuggestionsFieldTrialURLParam] = url; |
| 136 params[kSuggestionsFieldTrialSuggestionsSuffixParam] = suggestions_suffix; | 159 params[kSuggestionsFieldTrialCommonParamsParam] = common_params; |
| 137 params[kSuggestionsFieldTrialBlacklistSuffixParam] = blacklist_suffix; | 160 params[kSuggestionsFieldTrialBlacklistPathParam] = blacklist_path; |
| 161 params[kSuggestionsFieldTrialBlacklistUrlParamParam] = blacklist_url_param; | |
| 138 chrome_variations::AssociateVariationParams(kSuggestionsFieldTrialName, | 162 chrome_variations::AssociateVariationParams(kSuggestionsFieldTrialName, |
| 139 "Group1", params); | 163 "Group1", params); |
| 140 field_trial_ = base::FieldTrialList::CreateFieldTrial( | 164 field_trial_ = base::FieldTrialList::CreateFieldTrial( |
| 141 kSuggestionsFieldTrialName, "Group1"); | 165 kSuggestionsFieldTrialName, "Group1"); |
| 142 field_trial_->group(); | 166 field_trial_->group(); |
| 143 } | 167 } |
| 144 | 168 |
| 145 SuggestionsService* CreateSuggestionsService() { | 169 SuggestionsService* CreateSuggestionsService() { |
| 146 SuggestionsServiceFactory* suggestions_service_factory = | 170 SuggestionsServiceFactory* suggestions_service_factory = |
| 147 SuggestionsServiceFactory::GetInstance(); | 171 SuggestionsServiceFactory::GetInstance(); |
| 148 return suggestions_service_factory->GetForProfile(profile_.get()); | 172 return suggestions_service_factory->GetForProfile(profile_.get()); |
| 149 } | 173 } |
| 150 | 174 |
| 151 // Should not be called more than once per test since it stashes the | 175 // Should not be called more than once per test since it stashes the |
| 152 // SuggestionsStore in |mock_suggestions_store_|. | 176 // SuggestionsStore in |mock_suggestions_store_|. |
| 153 SuggestionsService* CreateSuggestionsServiceWithMockStore() { | 177 SuggestionsService* CreateSuggestionsServiceWithMocks() { |
| 154 mock_suggestions_store_ = new StrictMock<MockSuggestionsStore>(); | 178 mock_suggestions_store_ = new StrictMock<MockSuggestionsStore>(); |
| 179 mock_blacklist_store_ = new MockBlacklistStore(); | |
| 155 return new SuggestionsService( | 180 return new SuggestionsService( |
| 156 profile_.get(), scoped_ptr<SuggestionsStore>(mock_suggestions_store_)); | 181 profile_.get(), scoped_ptr<SuggestionsStore>(mock_suggestions_store_), |
| 182 scoped_ptr<BlacklistStore>(mock_blacklist_store_)); | |
| 157 } | 183 } |
| 158 | 184 |
| 159 void FetchSuggestionsDataNoTimeoutHelper(bool interleaved_requests) { | 185 void FetchSuggestionsDataNoTimeoutHelper(bool interleaved_requests) { |
| 160 // Field trial enabled with a specific suggestions URL. | 186 // Field trial enabled with a specific suggestions URL. |
| 161 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 187 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 162 kFakeBlacklistSuffix); | 188 kFakeBlacklistPath, kFakeBlacklistUrlParam); |
| 163 scoped_ptr<SuggestionsService> suggestions_service( | 189 scoped_ptr<SuggestionsService> suggestions_service( |
| 164 CreateSuggestionsServiceWithMockStore()); | 190 CreateSuggestionsServiceWithMocks()); |
| 165 EXPECT_TRUE(suggestions_service != NULL); | 191 EXPECT_TRUE(suggestions_service != NULL); |
| 166 scoped_ptr<SuggestionsProfile> suggestions_profile( | 192 scoped_ptr<SuggestionsProfile> suggestions_profile( |
| 167 CreateSuggestionsProfile()); | 193 CreateSuggestionsProfile()); |
| 168 | 194 |
| 169 // Set up net::FakeURLFetcherFactory. | 195 // Set up net::FakeURLFetcherFactory. |
| 170 std::string expected_url = | 196 std::string expected_url = |
| 171 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 197 (std::string(kFakeSuggestionsURL) + "?") + kFakeSuggestionsCommonParams; |
| 172 factory_.SetFakeResponse(GURL(expected_url), | 198 factory_.SetFakeResponse(GURL(expected_url), |
| 173 suggestions_profile->SerializeAsString(), | 199 suggestions_profile->SerializeAsString(), |
| 174 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 200 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 175 | 201 |
| 176 // Set up expectations on the SuggestionsStore. The number depends on | 202 // Set up expectations on the SuggestionsStore. The number depends on |
| 177 // whether the second request is issued (it won't be issued if the second | 203 // whether the second request is issued (it won't be issued if the second |
| 178 // fetch occurs before the first request has completed). | 204 // fetch occurs before the first request has completed). |
| 179 int expected_count = 2; | 205 int expected_count = 2; |
| 180 if (interleaved_requests) | 206 if (interleaved_requests) expected_count = 1; |
| 181 expected_count = 1; | |
| 182 EXPECT_CALL(*mock_suggestions_store_, | 207 EXPECT_CALL(*mock_suggestions_store_, |
| 183 StoreSuggestions(EqualsProto(*suggestions_profile))) | 208 StoreSuggestions(EqualsProto(*suggestions_profile))) |
| 184 .Times(expected_count) | 209 .Times(expected_count) |
| 185 .WillRepeatedly(Return(true)); | 210 .WillRepeatedly(Return(true)); |
| 186 | 211 |
| 212 // Expect a call to the blacklist store. Return that there's nothing to | |
| 213 // blacklist. | |
| 214 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)) | |
| 215 .Times(expected_count); | |
| 216 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) | |
| 217 .Times(expected_count) | |
| 218 .WillRepeatedly(Return(false)); | |
| 219 | |
| 187 // Send the request. The data will be returned to the callback. | 220 // Send the request. The data will be returned to the callback. |
| 188 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( | 221 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( |
| 189 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); | 222 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); |
| 190 | 223 |
| 191 if (!interleaved_requests) | 224 if (!interleaved_requests) |
| 192 base::MessageLoop::current()->RunUntilIdle(); // Let request complete. | 225 base::MessageLoop::current()->RunUntilIdle(); // Let request complete. |
| 193 | 226 |
| 194 // Send the request a second time. | 227 // Send the request a second time. |
| 195 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( | 228 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( |
| 196 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); | 229 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); |
| 197 | 230 |
| 198 // (Testing only) wait until suggestion fetch is complete. | 231 // (Testing only) wait until suggestion fetch is complete. |
| 199 base::MessageLoop::current()->RunUntilIdle(); | 232 base::MessageLoop::current()->RunUntilIdle(); |
| 200 | 233 |
| 201 // Ensure that CheckSuggestionsData() ran twice. | 234 // Ensure that CheckSuggestionsData() ran twice. |
| 202 EXPECT_EQ(2, suggestions_data_check_count_); | 235 EXPECT_EQ(2, suggestions_data_check_count_); |
| 203 } | 236 } |
| 204 | 237 |
| 205 protected: | 238 protected: |
| 206 net::FakeURLFetcherFactory factory_; | 239 net::FakeURLFetcherFactory factory_; |
| 207 // Only used if the SuggestionsService is built with a MockSuggestionsStore. | 240 // Only used if the SuggestionsService is built with a mocks. Not owned. |
| 208 // Not owned. | |
| 209 MockSuggestionsStore* mock_suggestions_store_; | 241 MockSuggestionsStore* mock_suggestions_store_; |
| 242 MockBlacklistStore* mock_blacklist_store_; | |
| 210 | 243 |
| 211 private: | 244 private: |
| 212 content::TestBrowserThreadBundle thread_bundle_; | 245 content::TestBrowserThreadBundle thread_bundle_; |
| 213 scoped_ptr<base::FieldTrialList> field_trial_list_; | 246 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 214 scoped_refptr<base::FieldTrial> field_trial_; | 247 scoped_refptr<base::FieldTrial> field_trial_; |
| 215 TestingProfile::Builder profile_builder_; | 248 TestingProfile::Builder profile_builder_; |
| 216 scoped_ptr<TestingProfile> profile_; | 249 scoped_ptr<TestingProfile> profile_; |
| 217 | 250 |
| 218 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); | 251 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); |
| 219 }; | 252 }; |
| 220 | 253 |
| 221 TEST_F(SuggestionsServiceTest, ServiceBeingCreated) { | 254 TEST_F(SuggestionsServiceTest, ServiceBeingCreated) { |
| 222 // Field trial not enabled. | 255 // Field trial not enabled. |
| 223 EXPECT_TRUE(CreateSuggestionsService() == NULL); | 256 EXPECT_TRUE(CreateSuggestionsService() == NULL); |
| 224 | 257 |
| 225 // Field trial enabled. | 258 // Field trial enabled. |
| 226 EnableFieldTrial("", "", ""); | 259 EnableFieldTrial("", "", "", ""); |
| 227 EXPECT_TRUE(CreateSuggestionsService() != NULL); | 260 EXPECT_TRUE(CreateSuggestionsService() != NULL); |
| 228 } | 261 } |
| 229 | 262 |
| 230 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeout) { | 263 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeout) { |
| 231 FetchSuggestionsDataNoTimeoutHelper(false); | 264 FetchSuggestionsDataNoTimeoutHelper(false); |
| 232 } | 265 } |
| 233 | 266 |
| 234 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeoutInterleaved) { | 267 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeoutInterleaved) { |
| 235 FetchSuggestionsDataNoTimeoutHelper(true); | 268 FetchSuggestionsDataNoTimeoutHelper(true); |
| 236 } | 269 } |
| 237 | 270 |
| 238 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataRequestError) { | 271 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataRequestError) { |
| 239 // Field trial enabled with a specific suggestions URL. | 272 // Field trial enabled with a specific suggestions URL. |
| 240 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 273 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 241 kFakeBlacklistSuffix); | 274 kFakeBlacklistPath, kFakeBlacklistUrlParam); |
| 242 scoped_ptr<SuggestionsService> suggestions_service( | 275 scoped_ptr<SuggestionsService> suggestions_service( |
| 243 CreateSuggestionsServiceWithMockStore()); | 276 CreateSuggestionsServiceWithMocks()); |
| 244 EXPECT_TRUE(suggestions_service != NULL); | 277 EXPECT_TRUE(suggestions_service != NULL); |
| 245 | 278 |
| 246 // Fake a request error. | 279 // Fake a request error. |
| 247 std::string expected_url = | 280 std::string expected_url = |
| 248 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 281 (std::string(kFakeSuggestionsURL) + "?") + kFakeSuggestionsCommonParams; |
| 249 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", net::HTTP_OK, | 282 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", net::HTTP_OK, |
| 250 net::URLRequestStatus::FAILED); | 283 net::URLRequestStatus::FAILED); |
| 251 | 284 |
| 252 // Set up expectations on the SuggestionsStore. | 285 // Set up expectations on the SuggestionsStore. |
| 253 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_)) | 286 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_)) |
| 254 .WillOnce(Return(true)); | 287 .WillOnce(Return(true)); |
| 255 | 288 |
| 289 // Expect a call to the blacklist store. Return that there's nothing to | |
| 290 // blacklist. | |
| 291 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | |
| 292 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) | |
| 293 .WillOnce(Return(false)); | |
| 294 | |
| 256 // Send the request. Empty data will be returned to the callback. | 295 // Send the request. Empty data will be returned to the callback. |
| 257 suggestions_service->FetchSuggestionsData( | 296 suggestions_service->FetchSuggestionsData( |
| 258 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 297 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
| 259 base::Unretained(this))); | 298 base::Unretained(this))); |
| 260 | 299 |
| 261 // (Testing only) wait until suggestion fetch is complete. | 300 // (Testing only) wait until suggestion fetch is complete. |
| 262 base::MessageLoop::current()->RunUntilIdle(); | 301 base::MessageLoop::current()->RunUntilIdle(); |
| 263 | 302 |
| 264 // Ensure that ExpectEmptySuggestionsProfile ran once. | 303 // Ensure that ExpectEmptySuggestionsProfile ran once. |
| 265 EXPECT_EQ(1, suggestions_empty_data_count_); | 304 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 266 } | 305 } |
| 267 | 306 |
| 268 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { | 307 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { |
| 269 // Field trial enabled with a specific suggestions URL. | 308 // Field trial enabled with a specific suggestions URL. |
| 270 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 309 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 271 kFakeBlacklistSuffix); | 310 kFakeBlacklistPath, kFakeBlacklistUrlParam); |
| 272 scoped_ptr<SuggestionsService> suggestions_service( | 311 scoped_ptr<SuggestionsService> suggestions_service( |
| 273 CreateSuggestionsServiceWithMockStore()); | 312 CreateSuggestionsServiceWithMocks()); |
| 274 EXPECT_TRUE(suggestions_service != NULL); | 313 EXPECT_TRUE(suggestions_service != NULL); |
| 275 | 314 |
| 276 // Response code != 200. | 315 // Response code != 200. |
| 277 std::string expected_url = | 316 std::string expected_url = |
| 278 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 317 (std::string(kFakeSuggestionsURL) + "?") + kFakeSuggestionsCommonParams; |
| 279 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", | 318 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", |
| 280 net::HTTP_BAD_REQUEST, | 319 net::HTTP_BAD_REQUEST, |
| 281 net::URLRequestStatus::SUCCESS); | 320 net::URLRequestStatus::SUCCESS); |
| 282 | 321 |
| 283 // Set up expectations on the SuggestionsStore. | 322 // Set up expectations on the SuggestionsStore. |
| 284 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions()); | 323 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions()); |
| 285 | 324 |
| 325 // Expect a call to the blacklist store. Return that there's nothing to | |
| 326 // blacklist. | |
| 327 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) | |
| 328 .WillOnce(Return(false)); | |
| 329 | |
| 286 // Send the request. Empty data will be returned to the callback. | 330 // Send the request. Empty data will be returned to the callback. |
| 287 suggestions_service->FetchSuggestionsData( | 331 suggestions_service->FetchSuggestionsData( |
| 288 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 332 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
| 289 base::Unretained(this))); | 333 base::Unretained(this))); |
| 290 | 334 |
| 291 // (Testing only) wait until suggestion fetch is complete. | 335 // (Testing only) wait until suggestion fetch is complete. |
| 292 base::MessageLoop::current()->RunUntilIdle(); | 336 base::MessageLoop::current()->RunUntilIdle(); |
| 293 | 337 |
| 294 // Ensure that ExpectEmptySuggestionsProfile ran once. | 338 // Ensure that ExpectEmptySuggestionsProfile ran once. |
| 295 EXPECT_EQ(1, suggestions_empty_data_count_); | 339 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 296 } | 340 } |
| 297 | 341 |
| 298 TEST_F(SuggestionsServiceTest, BlacklistURL) { | 342 TEST_F(SuggestionsServiceTest, BlacklistURL) { |
| 299 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 343 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 300 kFakeBlacklistSuffix); | 344 kFakeBlacklistPath, kFakeBlacklistUrlParam); |
| 301 scoped_ptr<SuggestionsService> suggestions_service( | 345 scoped_ptr<SuggestionsService> suggestions_service( |
| 302 CreateSuggestionsServiceWithMockStore()); | 346 CreateSuggestionsServiceWithMocks()); |
| 303 EXPECT_TRUE(suggestions_service != NULL); | 347 EXPECT_TRUE(suggestions_service != NULL); |
| 304 | 348 |
| 305 std::string expected_url(kFakeSuggestionsURL); | 349 GURL blacklist_url(kBlacklistUrl); |
| 306 expected_url.append(kFakeBlacklistSuffix) | 350 std::string request_url = GetExpectedBlacklistRequestUrl(blacklist_url); |
| 307 .append(net::EscapeQueryParamValue(GURL(kBlacklistUrl).spec(), true)); | |
| 308 scoped_ptr<SuggestionsProfile> suggestions_profile( | 351 scoped_ptr<SuggestionsProfile> suggestions_profile( |
| 309 CreateSuggestionsProfile()); | 352 CreateSuggestionsProfile()); |
| 310 factory_.SetFakeResponse(GURL(expected_url), | 353 factory_.SetFakeResponse(GURL(request_url), |
| 311 suggestions_profile->SerializeAsString(), | 354 suggestions_profile->SerializeAsString(), |
| 312 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 355 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 313 | 356 |
| 314 // Set up expectations on the SuggestionsStore. | 357 // Set up expectations on the SuggestionsStore. |
| 315 EXPECT_CALL(*mock_suggestions_store_, | 358 EXPECT_CALL(*mock_suggestions_store_, |
| 316 StoreSuggestions(EqualsProto(*suggestions_profile))) | 359 StoreSuggestions(EqualsProto(*suggestions_profile))) |
| 317 .WillOnce(Return(true)); | 360 .WillOnce(Return(true)); |
| 318 | 361 |
| 362 // Expected calls to the blacklist store. | |
| 363 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url))) | |
| 364 .WillOnce(Return(true)); | |
| 365 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url))) | |
| 366 .WillOnce(Return(true)); | |
| 367 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); | |
| 368 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) | |
| 369 .WillOnce(Return(false)); | |
| 370 | |
| 319 // Send the request. The data will be returned to the callback. | 371 // Send the request. The data will be returned to the callback. |
| 320 suggestions_service->BlacklistURL( | 372 suggestions_service->BlacklistURL( |
| 321 GURL(kBlacklistUrl), | 373 blacklist_url, base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, |
| 322 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | 374 base::Unretained(this))); |
| 323 base::Unretained(this))); | |
| 324 | 375 |
| 325 // (Testing only) wait until blacklist request is complete. | 376 // (Testing only) wait until blacklist request is complete. |
| 326 base::MessageLoop::current()->RunUntilIdle(); | 377 base::MessageLoop::current()->RunUntilIdle(); |
| 327 | 378 |
| 328 // Ensure that CheckSuggestionsData() ran once. | 379 // Ensure that CheckSuggestionsData() ran once. |
| 329 EXPECT_EQ(1, suggestions_data_check_count_); | 380 EXPECT_EQ(1, suggestions_data_check_count_); |
| 330 } | 381 } |
| 331 | 382 |
| 383 // Initial blacklist request fails, triggering a scheduled upload which | |
| 384 // succeeds. | |
| 385 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { | |
| 386 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, | |
| 387 kFakeBlacklistPath, kFakeBlacklistUrlParam); | |
| 388 scoped_ptr<SuggestionsService> suggestions_service( | |
| 389 CreateSuggestionsServiceWithMocks()); | |
| 390 EXPECT_TRUE(suggestions_service != NULL); | |
| 391 suggestions_service->SetBlacklistDelay(0); // Don't wait during a test! | |
| 392 scoped_ptr<SuggestionsProfile> suggestions_profile( | |
| 393 CreateSuggestionsProfile()); | |
| 394 GURL blacklist_url(kBlacklistUrl); | |
| 395 | |
| 396 // Set up behavior for the first call to blacklist. | |
| 397 std::string request_url = GetExpectedBlacklistRequestUrl(blacklist_url); | |
| 398 factory_.SetFakeResponse(GURL(request_url), "irrelevant", net::HTTP_OK, | |
| 399 net::URLRequestStatus::FAILED); | |
| 400 | |
| 401 // Expectations specific to the first request. | |
| 402 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url))) | |
| 403 .WillOnce(Return(true)); | |
| 404 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_)) | |
| 405 .WillOnce(DoAll(SetArgPointee<0>(*suggestions_profile), Return(true))); | |
| 406 | |
| 407 // Expectations specific to the second request. | |
|
Mathieu
2014/06/18 13:56:23
Can this be a bit closer to the "second request"
manzagop (departed)
2014/06/18 18:44:55
I believe (could be wrong) all expectations should
| |
| 408 EXPECT_CALL(*mock_suggestions_store_, | |
| 409 StoreSuggestions(EqualsProto(*suggestions_profile))) | |
| 410 .WillOnce(Return(true)); | |
| 411 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url))) | |
| 412 .WillOnce(Return(true)); | |
| 413 | |
| 414 // Expectations pertaining to both requests. | |
| 415 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(2); | |
| 416 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) | |
| 417 .WillOnce(Return(true)) | |
| 418 .WillOnce(DoAll(SetArgPointee<0>(blacklist_url), Return(true))) | |
| 419 .WillOnce(Return(false)); | |
| 420 | |
| 421 // Send the request. The data will be returned to the callback. | |
| 422 suggestions_service->BlacklistURL( | |
| 423 blacklist_url, base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | |
| 424 base::Unretained(this))); | |
| 425 | |
| 426 // The first FakeURLFetcher was created; we can now set up behavior for the | |
| 427 // second call to blacklist. | |
| 428 factory_.SetFakeResponse(GURL(request_url), | |
| 429 suggestions_profile->SerializeAsString(), | |
| 430 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | |
| 431 | |
| 432 // (Testing only) wait until both requests are complete. | |
| 433 base::MessageLoop::current()->RunUntilIdle(); | |
| 434 base::MessageLoop::current()->RunUntilIdle(); | |
|
Mathieu
2014/06/18 13:56:23
pretty sure two calls is redundant
manzagop (departed)
2014/06/18 18:44:55
Done.
| |
| 435 | |
| 436 // Ensure that CheckSuggestionsData() ran once. | |
| 437 EXPECT_EQ(1, suggestions_data_check_count_); | |
| 438 } | |
| 439 | |
| 440 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { | |
| 441 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, | |
| 442 kFakeBlacklistPath, kFakeBlacklistUrlParam); | |
| 443 | |
| 444 scoped_ptr<GURL> request_url; | |
| 445 scoped_ptr<net::FakeURLFetcher> fetcher; | |
| 446 GURL retrieved_url; | |
| 447 | |
| 448 // Not a blacklist request. | |
| 449 request_url.reset(new GURL("http://not-blacklisting.com/a?b=c")); | |
| 450 fetcher = CreateURLFetcher(*request_url, NULL, "", net::HTTP_OK, | |
| 451 net::URLRequestStatus::SUCCESS); | |
| 452 EXPECT_FALSE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); | |
| 453 | |
| 454 // An actual blacklist request. | |
| 455 string blacklisted_url = "http://blacklisted.com/a?b=c&d=e"; | |
| 456 string encoded_blacklisted_url = | |
| 457 "http%3A%2F%2Fblacklisted.com%2Fa%3Fb%3Dc%26d%3De"; | |
| 458 string blacklist_request_prefix = | |
| 459 "https://mysuggestions.com/proto/blacklist?foo=bar&baz="; | |
| 460 request_url.reset( | |
| 461 new GURL(blacklist_request_prefix + encoded_blacklisted_url)); | |
| 462 fetcher.reset(); | |
| 463 fetcher = CreateURLFetcher(*request_url, NULL, "", net::HTTP_OK, | |
| 464 net::URLRequestStatus::SUCCESS); | |
| 465 EXPECT_TRUE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); | |
| 466 EXPECT_EQ(blacklisted_url, retrieved_url.spec()); | |
| 467 } | |
| 468 | |
| 469 TEST_F(SuggestionsServiceTest, UpdateBlacklistDelay) { | |
| 470 scoped_ptr<SuggestionsService> suggestions_service( | |
| 471 CreateSuggestionsServiceWithMocks()); | |
| 472 int initial_delay = suggestions_service->GetBlacklistDelay(); | |
| 473 | |
| 474 // Delay unchanged on success. | |
| 475 suggestions_service->UpdateBlacklistDelay(true); | |
| 476 EXPECT_EQ(initial_delay, suggestions_service->GetBlacklistDelay()); | |
| 477 | |
| 478 // Delay increases on failure. | |
| 479 suggestions_service->UpdateBlacklistDelay(false); | |
| 480 EXPECT_GE(suggestions_service->GetBlacklistDelay(), initial_delay); | |
|
Mathieu
2014/06/18 13:56:23
EXPECT_GT?
manzagop (departed)
2014/06/18 18:44:55
Done. You're right it's probably better to break t
| |
| 481 | |
| 482 // Delay resets on success. | |
| 483 suggestions_service->UpdateBlacklistDelay(true); | |
| 484 EXPECT_EQ(initial_delay, suggestions_service->GetBlacklistDelay()); | |
| 485 } | |
| 486 | |
| 332 } // namespace suggestions | 487 } // namespace suggestions |
| OLD | NEW |