| 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 <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 15 #include "chrome/browser/history/history_types.h" | 15 #include "chrome/browser/history/history_types.h" |
| 16 #include "chrome/browser/search/suggestions/blacklist_store.h" |
| 16 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" | 17 #include "chrome/browser/search/suggestions/proto/suggestions.pb.h" |
| 17 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" | 18 #include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| 18 #include "chrome/browser/search/suggestions/suggestions_store.h" | 19 #include "chrome/browser/search/suggestions/suggestions_store.h" |
| 19 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 20 #include "components/variations/entropy_provider.h" | 21 #include "components/variations/entropy_provider.h" |
| 21 #include "components/variations/variations_associated_data.h" | 22 #include "components/variations/variations_associated_data.h" |
| 22 #include "content/public/test/test_browser_thread_bundle.h" | 23 #include "content/public/test/test_browser_thread_bundle.h" |
| 23 #include "net/http/http_response_headers.h" | 24 #include "net/http/http_response_headers.h" |
| 24 #include "net/http/http_status_code.h" | 25 #include "net/http/http_status_code.h" |
| 25 #include "net/url_request/test_url_fetcher_factory.h" | 26 #include "net/url_request/test_url_fetcher_factory.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 return profile.Pass(); | 84 return profile.Pass(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 class MockSuggestionsStore : public suggestions::SuggestionsStore { | 87 class MockSuggestionsStore : public suggestions::SuggestionsStore { |
| 87 public: | 88 public: |
| 88 MOCK_METHOD1(LoadSuggestions, bool(SuggestionsProfile*)); | 89 MOCK_METHOD1(LoadSuggestions, bool(SuggestionsProfile*)); |
| 89 MOCK_METHOD1(StoreSuggestions, bool(const SuggestionsProfile&)); | 90 MOCK_METHOD1(StoreSuggestions, bool(const SuggestionsProfile&)); |
| 90 MOCK_METHOD0(ClearSuggestions, void()); | 91 MOCK_METHOD0(ClearSuggestions, void()); |
| 91 }; | 92 }; |
| 92 | 93 |
| 94 class MockBlacklistStore : public suggestions::BlacklistStore { |
| 95 public: |
| 96 MOCK_METHOD1(BlacklistUrl, bool(const GURL&)); |
| 97 MOCK_METHOD1(RemoveUrl, bool(const GURL&)); |
| 98 MOCK_METHOD1(FilterSuggestions, void(SuggestionsProfile*)); |
| 99 }; |
| 100 |
| 93 } // namespace | 101 } // namespace |
| 94 | 102 |
| 95 class SuggestionsServiceTest : public testing::Test { | 103 class SuggestionsServiceTest : public testing::Test { |
| 96 public: | 104 public: |
| 97 void CheckSuggestionsData(const SuggestionsProfile& suggestions_profile) { | 105 void CheckSuggestionsData(const SuggestionsProfile& suggestions_profile) { |
| 98 EXPECT_EQ(1, suggestions_profile.suggestions_size()); | 106 EXPECT_EQ(1, suggestions_profile.suggestions_size()); |
| 99 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); | 107 EXPECT_EQ(kTestTitle, suggestions_profile.suggestions(0).title()); |
| 100 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); | 108 EXPECT_EQ(kTestUrl, suggestions_profile.suggestions(0).url()); |
| 101 ++suggestions_data_check_count_; | 109 ++suggestions_data_check_count_; |
| 102 } | 110 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 } | 151 } |
| 144 | 152 |
| 145 SuggestionsService* CreateSuggestionsService() { | 153 SuggestionsService* CreateSuggestionsService() { |
| 146 SuggestionsServiceFactory* suggestions_service_factory = | 154 SuggestionsServiceFactory* suggestions_service_factory = |
| 147 SuggestionsServiceFactory::GetInstance(); | 155 SuggestionsServiceFactory::GetInstance(); |
| 148 return suggestions_service_factory->GetForProfile(profile_.get()); | 156 return suggestions_service_factory->GetForProfile(profile_.get()); |
| 149 } | 157 } |
| 150 | 158 |
| 151 // Should not be called more than once per test since it stashes the | 159 // Should not be called more than once per test since it stashes the |
| 152 // SuggestionsStore in |mock_suggestions_store_|. | 160 // SuggestionsStore in |mock_suggestions_store_|. |
| 153 SuggestionsService* CreateSuggestionsServiceWithMockStore() { | 161 SuggestionsService* CreateSuggestionsServiceWithMocks() { |
| 154 mock_suggestions_store_ = new StrictMock<MockSuggestionsStore>(); | 162 mock_suggestions_store_ = new StrictMock<MockSuggestionsStore>(); |
| 163 mock_blacklist_store_ = new MockBlacklistStore(); |
| 155 return new SuggestionsService( | 164 return new SuggestionsService( |
| 156 profile_.get(), scoped_ptr<SuggestionsStore>(mock_suggestions_store_)); | 165 profile_.get(), scoped_ptr<SuggestionsStore>(mock_suggestions_store_), |
| 166 scoped_ptr<BlacklistStore>(mock_blacklist_store_)); |
| 157 } | 167 } |
| 158 | 168 |
| 159 void FetchSuggestionsDataNoTimeoutHelper(bool interleaved_requests) { | 169 void FetchSuggestionsDataNoTimeoutHelper(bool interleaved_requests) { |
| 160 // Field trial enabled with a specific suggestions URL. | 170 // Field trial enabled with a specific suggestions URL. |
| 161 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 171 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 162 kFakeBlacklistSuffix); | 172 kFakeBlacklistSuffix); |
| 163 scoped_ptr<SuggestionsService> suggestions_service( | 173 scoped_ptr<SuggestionsService> suggestions_service( |
| 164 CreateSuggestionsServiceWithMockStore()); | 174 CreateSuggestionsServiceWithMockStore()); |
| 165 EXPECT_TRUE(suggestions_service != NULL); | 175 EXPECT_TRUE(suggestions_service != NULL); |
| 166 scoped_ptr<SuggestionsProfile> suggestions_profile( | 176 scoped_ptr<SuggestionsProfile> suggestions_profile( |
| (...skipping 30 matching lines...) Expand all Loading... |
| 197 | 207 |
| 198 // (Testing only) wait until suggestion fetch is complete. | 208 // (Testing only) wait until suggestion fetch is complete. |
| 199 base::MessageLoop::current()->RunUntilIdle(); | 209 base::MessageLoop::current()->RunUntilIdle(); |
| 200 | 210 |
| 201 // Ensure that CheckSuggestionsData() ran twice. | 211 // Ensure that CheckSuggestionsData() ran twice. |
| 202 EXPECT_EQ(2, suggestions_data_check_count_); | 212 EXPECT_EQ(2, suggestions_data_check_count_); |
| 203 } | 213 } |
| 204 | 214 |
| 205 protected: | 215 protected: |
| 206 net::FakeURLFetcherFactory factory_; | 216 net::FakeURLFetcherFactory factory_; |
| 207 // Only used if the SuggestionsService is built with a MockSuggestionsStore. | 217 // Only used if the SuggestionsService is built with a mocks. Not owned. |
| 208 // Not owned. | |
| 209 MockSuggestionsStore* mock_suggestions_store_; | 218 MockSuggestionsStore* mock_suggestions_store_; |
| 219 MockBlacklistStore* mock_blacklist_store_; |
| 210 | 220 |
| 211 private: | 221 private: |
| 212 content::TestBrowserThreadBundle thread_bundle_; | 222 content::TestBrowserThreadBundle thread_bundle_; |
| 213 scoped_ptr<base::FieldTrialList> field_trial_list_; | 223 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 214 scoped_refptr<base::FieldTrial> field_trial_; | 224 scoped_refptr<base::FieldTrial> field_trial_; |
| 215 TestingProfile::Builder profile_builder_; | 225 TestingProfile::Builder profile_builder_; |
| 216 scoped_ptr<TestingProfile> profile_; | 226 scoped_ptr<TestingProfile> profile_; |
| 217 | 227 |
| 218 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); | 228 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); |
| 219 }; | 229 }; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 233 | 243 |
| 234 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeoutInterleaved) { | 244 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeoutInterleaved) { |
| 235 FetchSuggestionsDataNoTimeoutHelper(true); | 245 FetchSuggestionsDataNoTimeoutHelper(true); |
| 236 } | 246 } |
| 237 | 247 |
| 238 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataRequestError) { | 248 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataRequestError) { |
| 239 // Field trial enabled with a specific suggestions URL. | 249 // Field trial enabled with a specific suggestions URL. |
| 240 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 250 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 241 kFakeBlacklistSuffix); | 251 kFakeBlacklistSuffix); |
| 242 scoped_ptr<SuggestionsService> suggestions_service( | 252 scoped_ptr<SuggestionsService> suggestions_service( |
| 243 CreateSuggestionsServiceWithMockStore()); | 253 CreateSuggestionsServiceWithMocks()); |
| 244 EXPECT_TRUE(suggestions_service != NULL); | 254 EXPECT_TRUE(suggestions_service != NULL); |
| 245 | 255 |
| 246 // Fake a request error. | 256 // Fake a request error. |
| 247 std::string expected_url = | 257 std::string expected_url = |
| 248 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 258 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; |
| 249 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", net::HTTP_OK, | 259 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", net::HTTP_OK, |
| 250 net::URLRequestStatus::FAILED); | 260 net::URLRequestStatus::FAILED); |
| 251 | 261 |
| 252 // Set up expectations on the SuggestionsStore. | 262 // Set up expectations on the SuggestionsStore. |
| 253 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_)) | 263 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_)) |
| 254 .WillOnce(Return(true)); | 264 .WillOnce(Return(true)); |
| 255 | 265 |
| 256 // Send the request. Empty data will be returned to the callback. | 266 // Send the request. Empty data will be returned to the callback. |
| 257 suggestions_service->FetchSuggestionsData( | 267 suggestions_service->FetchSuggestionsData( |
| 258 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 268 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
| 259 base::Unretained(this))); | 269 base::Unretained(this))); |
| 260 | 270 |
| 261 // (Testing only) wait until suggestion fetch is complete. | 271 // (Testing only) wait until suggestion fetch is complete. |
| 262 base::MessageLoop::current()->RunUntilIdle(); | 272 base::MessageLoop::current()->RunUntilIdle(); |
| 263 | 273 |
| 264 // Ensure that ExpectEmptySuggestionsProfile ran once. | 274 // Ensure that ExpectEmptySuggestionsProfile ran once. |
| 265 EXPECT_EQ(1, suggestions_empty_data_count_); | 275 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 266 } | 276 } |
| 267 | 277 |
| 268 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { | 278 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { |
| 269 // Field trial enabled with a specific suggestions URL. | 279 // Field trial enabled with a specific suggestions URL. |
| 270 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 280 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 271 kFakeBlacklistSuffix); | 281 kFakeBlacklistSuffix); |
| 272 scoped_ptr<SuggestionsService> suggestions_service( | 282 scoped_ptr<SuggestionsService> suggestions_service( |
| 273 CreateSuggestionsServiceWithMockStore()); | 283 CreateSuggestionsServiceWithMocks()); |
| 274 EXPECT_TRUE(suggestions_service != NULL); | 284 EXPECT_TRUE(suggestions_service != NULL); |
| 275 | 285 |
| 276 // Response code != 200. | 286 // Response code != 200. |
| 277 std::string expected_url = | 287 std::string expected_url = |
| 278 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 288 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; |
| 279 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", | 289 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", |
| 280 net::HTTP_BAD_REQUEST, | 290 net::HTTP_BAD_REQUEST, |
| 281 net::URLRequestStatus::SUCCESS); | 291 net::URLRequestStatus::SUCCESS); |
| 282 | 292 |
| 283 // Set up expectations on the SuggestionsStore. | 293 // Set up expectations on the SuggestionsStore. |
| 284 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions()); | 294 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions()); |
| 285 | 295 |
| 286 // Send the request. Empty data will be returned to the callback. | 296 // Send the request. Empty data will be returned to the callback. |
| 287 suggestions_service->FetchSuggestionsData( | 297 suggestions_service->FetchSuggestionsData( |
| 288 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 298 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
| 289 base::Unretained(this))); | 299 base::Unretained(this))); |
| 290 | 300 |
| 291 // (Testing only) wait until suggestion fetch is complete. | 301 // (Testing only) wait until suggestion fetch is complete. |
| 292 base::MessageLoop::current()->RunUntilIdle(); | 302 base::MessageLoop::current()->RunUntilIdle(); |
| 293 | 303 |
| 294 // Ensure that ExpectEmptySuggestionsProfile ran once. | 304 // Ensure that ExpectEmptySuggestionsProfile ran once. |
| 295 EXPECT_EQ(1, suggestions_empty_data_count_); | 305 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 296 } | 306 } |
| 297 | 307 |
| 298 TEST_F(SuggestionsServiceTest, BlacklistURL) { | 308 TEST_F(SuggestionsServiceTest, BlacklistURL) { |
| 299 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 309 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, |
| 300 kFakeBlacklistSuffix); | 310 kFakeBlacklistSuffix); |
| 301 scoped_ptr<SuggestionsService> suggestions_service( | 311 scoped_ptr<SuggestionsService> suggestions_service( |
| 302 CreateSuggestionsServiceWithMockStore()); | 312 CreateSuggestionsServiceWithMocks()); |
| 303 EXPECT_TRUE(suggestions_service != NULL); | 313 EXPECT_TRUE(suggestions_service != NULL); |
| 304 | 314 |
| 305 std::string expected_url(kFakeSuggestionsURL); | 315 std::string expected_url(kFakeSuggestionsURL); |
| 306 expected_url.append(kFakeBlacklistSuffix) | 316 expected_url.append(kFakeBlacklistSuffix) |
| 307 .append(net::EscapeQueryParamValue(GURL(kBlacklistUrl).spec(), true)); | 317 .append(net::EscapeQueryParamValue(GURL(kBlacklistUrl).spec(), true)); |
| 308 scoped_ptr<SuggestionsProfile> suggestions_profile( | 318 scoped_ptr<SuggestionsProfile> suggestions_profile( |
| 309 CreateSuggestionsProfile()); | 319 CreateSuggestionsProfile()); |
| 310 factory_.SetFakeResponse(GURL(expected_url), | 320 factory_.SetFakeResponse(GURL(expected_url), |
| 311 suggestions_profile->SerializeAsString(), | 321 suggestions_profile->SerializeAsString(), |
| 312 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 322 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 323 base::Unretained(this))); | 333 base::Unretained(this))); |
| 324 | 334 |
| 325 // (Testing only) wait until blacklist request is complete. | 335 // (Testing only) wait until blacklist request is complete. |
| 326 base::MessageLoop::current()->RunUntilIdle(); | 336 base::MessageLoop::current()->RunUntilIdle(); |
| 327 | 337 |
| 328 // Ensure that CheckSuggestionsData() ran once. | 338 // Ensure that CheckSuggestionsData() ran once. |
| 329 EXPECT_EQ(1, suggestions_data_check_count_); | 339 EXPECT_EQ(1, suggestions_data_check_count_); |
| 330 } | 340 } |
| 331 | 341 |
| 332 } // namespace suggestions | 342 } // namespace suggestions |
| OLD | NEW |