| 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 bool control_group) { | 149 bool control_group) { |
| 127 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. | 150 // Clear the existing |field_trial_list_| to avoid firing a DCHECK. |
| 128 field_trial_list_.reset(NULL); | 151 field_trial_list_.reset(NULL); |
| 129 field_trial_list_.reset( | 152 field_trial_list_.reset( |
| 130 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); | 153 new base::FieldTrialList(new metrics::SHA1EntropyProvider("foo"))); |
| 131 | 154 |
| 132 chrome_variations::testing::ClearAllVariationParams(); | 155 chrome_variations::testing::ClearAllVariationParams(); |
| 133 std::map<std::string, std::string> params; | 156 std::map<std::string, std::string> params; |
| 134 params[kSuggestionsFieldTrialStateParam] = | 157 params[kSuggestionsFieldTrialStateParam] = |
| 135 kSuggestionsFieldTrialStateEnabled; | 158 kSuggestionsFieldTrialStateEnabled; |
| 136 if (control_group) { | 159 if (control_group) { |
| 137 params[kSuggestionsFieldTrialControlParam] = | 160 params[kSuggestionsFieldTrialControlParam] = |
| 138 kSuggestionsFieldTrialStateEnabled; | 161 kSuggestionsFieldTrialStateEnabled; |
| 139 } | 162 } |
| 140 params[kSuggestionsFieldTrialURLParam] = url; | 163 params[kSuggestionsFieldTrialURLParam] = url; |
| 141 params[kSuggestionsFieldTrialSuggestionsSuffixParam] = suggestions_suffix; | 164 params[kSuggestionsFieldTrialCommonParamsParam] = common_params; |
| 142 params[kSuggestionsFieldTrialBlacklistSuffixParam] = blacklist_suffix; | 165 params[kSuggestionsFieldTrialBlacklistPathParam] = blacklist_path; |
| 166 params[kSuggestionsFieldTrialBlacklistUrlParam] = blacklist_url_param; |
| 143 chrome_variations::AssociateVariationParams(kSuggestionsFieldTrialName, | 167 chrome_variations::AssociateVariationParams(kSuggestionsFieldTrialName, |
| 144 "Group1", params); | 168 "Group1", params); |
| 145 field_trial_ = base::FieldTrialList::CreateFieldTrial( | 169 field_trial_ = base::FieldTrialList::CreateFieldTrial( |
| 146 kSuggestionsFieldTrialName, "Group1"); | 170 kSuggestionsFieldTrialName, "Group1"); |
| 147 field_trial_->group(); | 171 field_trial_->group(); |
| 148 } | 172 } |
| 149 | 173 |
| 150 SuggestionsService* CreateSuggestionsService() { | 174 SuggestionsService* CreateSuggestionsService() { |
| 151 SuggestionsServiceFactory* suggestions_service_factory = | 175 SuggestionsServiceFactory* suggestions_service_factory = |
| 152 SuggestionsServiceFactory::GetInstance(); | 176 SuggestionsServiceFactory::GetInstance(); |
| 153 return suggestions_service_factory->GetForProfile(profile_.get()); | 177 return suggestions_service_factory->GetForProfile(profile_.get()); |
| 154 } | 178 } |
| 155 | 179 |
| 156 // Should not be called more than once per test since it stashes the | 180 // Should not be called more than once per test since it stashes the |
| 157 // SuggestionsStore in |mock_suggestions_store_|. | 181 // SuggestionsStore in |mock_suggestions_store_|. |
| 158 SuggestionsService* CreateSuggestionsServiceWithMockStore() { | 182 SuggestionsService* CreateSuggestionsServiceWithMocks() { |
| 159 mock_suggestions_store_ = new StrictMock<MockSuggestionsStore>(); | 183 mock_suggestions_store_ = new StrictMock<MockSuggestionsStore>(); |
| 184 mock_blacklist_store_ = new MockBlacklistStore(); |
| 160 return new SuggestionsService( | 185 return new SuggestionsService( |
| 161 profile_.get(), scoped_ptr<SuggestionsStore>(mock_suggestions_store_)); | 186 profile_.get(), scoped_ptr<SuggestionsStore>(mock_suggestions_store_), |
| 187 scoped_ptr<BlacklistStore>(mock_blacklist_store_)); |
| 162 } | 188 } |
| 163 | 189 |
| 164 void FetchSuggestionsDataNoTimeoutHelper(bool interleaved_requests) { | 190 void FetchSuggestionsDataNoTimeoutHelper(bool interleaved_requests) { |
| 165 // Field trial enabled with a specific suggestions URL. | 191 // Field trial enabled with a specific suggestions URL. |
| 166 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 192 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 167 kFakeBlacklistSuffix, false); | 193 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
| 168 scoped_ptr<SuggestionsService> suggestions_service( | 194 scoped_ptr<SuggestionsService> suggestions_service( |
| 169 CreateSuggestionsServiceWithMockStore()); | 195 CreateSuggestionsServiceWithMocks()); |
| 170 EXPECT_TRUE(suggestions_service != NULL); | 196 EXPECT_TRUE(suggestions_service != NULL); |
| 171 scoped_ptr<SuggestionsProfile> suggestions_profile( | 197 scoped_ptr<SuggestionsProfile> suggestions_profile( |
| 172 CreateSuggestionsProfile()); | 198 CreateSuggestionsProfile()); |
| 173 | 199 |
| 174 // Set up net::FakeURLFetcherFactory. | 200 // Set up net::FakeURLFetcherFactory. |
| 175 std::string expected_url = | 201 std::string expected_url = |
| 176 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 202 (std::string(kFakeSuggestionsURL) + "?") + kFakeSuggestionsCommonParams; |
| 177 factory_.SetFakeResponse(GURL(expected_url), | 203 factory_.SetFakeResponse(GURL(expected_url), |
| 178 suggestions_profile->SerializeAsString(), | 204 suggestions_profile->SerializeAsString(), |
| 179 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 205 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 180 | 206 |
| 181 // Set up expectations on the SuggestionsStore. The number depends on | 207 // Set up expectations on the SuggestionsStore. The number depends on |
| 182 // whether the second request is issued (it won't be issued if the second | 208 // whether the second request is issued (it won't be issued if the second |
| 183 // fetch occurs before the first request has completed). | 209 // fetch occurs before the first request has completed). |
| 184 int expected_count = 2; | 210 int expected_count = 2; |
| 185 if (interleaved_requests) | 211 if (interleaved_requests) expected_count = 1; |
| 186 expected_count = 1; | |
| 187 EXPECT_CALL(*mock_suggestions_store_, | 212 EXPECT_CALL(*mock_suggestions_store_, |
| 188 StoreSuggestions(EqualsProto(*suggestions_profile))) | 213 StoreSuggestions(EqualsProto(*suggestions_profile))) |
| 189 .Times(expected_count) | 214 .Times(expected_count) |
| 190 .WillRepeatedly(Return(true)); | 215 .WillRepeatedly(Return(true)); |
| 191 | 216 |
| 217 // Expect a call to the blacklist store. Return that there's nothing to |
| 218 // blacklist. |
| 219 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)) |
| 220 .Times(expected_count); |
| 221 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) |
| 222 .Times(expected_count) |
| 223 .WillRepeatedly(Return(false)); |
| 224 |
| 192 // Send the request. The data will be returned to the callback. | 225 // Send the request. The data will be returned to the callback. |
| 193 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( | 226 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( |
| 194 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); | 227 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); |
| 195 | 228 |
| 196 if (!interleaved_requests) | 229 if (!interleaved_requests) |
| 197 base::MessageLoop::current()->RunUntilIdle(); // Let request complete. | 230 base::MessageLoop::current()->RunUntilIdle(); // Let request complete. |
| 198 | 231 |
| 199 // Send the request a second time. | 232 // Send the request a second time. |
| 200 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( | 233 suggestions_service->FetchSuggestionsDataNoTimeout(base::Bind( |
| 201 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); | 234 &SuggestionsServiceTest::CheckSuggestionsData, base::Unretained(this))); |
| 202 | 235 |
| 203 // (Testing only) wait until suggestion fetch is complete. | 236 // (Testing only) wait until suggestion fetch is complete. |
| 204 base::MessageLoop::current()->RunUntilIdle(); | 237 base::MessageLoop::current()->RunUntilIdle(); |
| 205 | 238 |
| 206 // Ensure that CheckSuggestionsData() ran twice. | 239 // Ensure that CheckSuggestionsData() ran twice. |
| 207 EXPECT_EQ(2, suggestions_data_check_count_); | 240 EXPECT_EQ(2, suggestions_data_check_count_); |
| 208 } | 241 } |
| 209 | 242 |
| 210 protected: | 243 protected: |
| 211 net::FakeURLFetcherFactory factory_; | 244 net::FakeURLFetcherFactory factory_; |
| 212 // Only used if the SuggestionsService is built with a MockSuggestionsStore. | 245 // Only used if the SuggestionsService is built with a mocks. Not owned. |
| 213 // Not owned. | |
| 214 MockSuggestionsStore* mock_suggestions_store_; | 246 MockSuggestionsStore* mock_suggestions_store_; |
| 247 MockBlacklistStore* mock_blacklist_store_; |
| 215 | 248 |
| 216 private: | 249 private: |
| 217 content::TestBrowserThreadBundle thread_bundle_; | 250 content::TestBrowserThreadBundle thread_bundle_; |
| 218 scoped_ptr<base::FieldTrialList> field_trial_list_; | 251 scoped_ptr<base::FieldTrialList> field_trial_list_; |
| 219 scoped_refptr<base::FieldTrial> field_trial_; | 252 scoped_refptr<base::FieldTrial> field_trial_; |
| 220 TestingProfile::Builder profile_builder_; | 253 TestingProfile::Builder profile_builder_; |
| 221 scoped_ptr<TestingProfile> profile_; | 254 scoped_ptr<TestingProfile> profile_; |
| 222 | 255 |
| 223 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); | 256 DISALLOW_COPY_AND_ASSIGN(SuggestionsServiceTest); |
| 224 }; | 257 }; |
| 225 | 258 |
| 226 TEST_F(SuggestionsServiceTest, ServiceBeingCreated) { | 259 TEST_F(SuggestionsServiceTest, ServiceBeingCreated) { |
| 227 // Field trial not enabled. | 260 // Field trial not enabled. |
| 228 EXPECT_TRUE(CreateSuggestionsService() == NULL); | 261 EXPECT_TRUE(CreateSuggestionsService() == NULL); |
| 229 | 262 |
| 230 // Field trial enabled. | 263 // Field trial enabled. |
| 231 EnableFieldTrial("", "", "", false); | 264 EnableFieldTrial("", "", "", "", false); |
| 232 EXPECT_TRUE(CreateSuggestionsService() != NULL); | 265 EXPECT_TRUE(CreateSuggestionsService() != NULL); |
| 233 } | 266 } |
| 234 | 267 |
| 235 TEST_F(SuggestionsServiceTest, IsControlGroup) { | 268 TEST_F(SuggestionsServiceTest, IsControlGroup) { |
| 236 // Field trial enabled. | 269 // Field trial enabled. |
| 237 EnableFieldTrial("", "", "", false); | 270 EnableFieldTrial("", "", "", "", false); |
| 238 EXPECT_FALSE(SuggestionsService::IsControlGroup()); | 271 EXPECT_FALSE(SuggestionsService::IsControlGroup()); |
| 239 | 272 |
| 240 EnableFieldTrial("", "", "", true); | 273 EnableFieldTrial("", "", "", "", true); |
| 241 EXPECT_TRUE(SuggestionsService::IsControlGroup()); | 274 EXPECT_TRUE(SuggestionsService::IsControlGroup()); |
| 242 } | 275 } |
| 243 | 276 |
| 244 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeout) { | 277 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeout) { |
| 245 FetchSuggestionsDataNoTimeoutHelper(false); | 278 FetchSuggestionsDataNoTimeoutHelper(false); |
| 246 } | 279 } |
| 247 | 280 |
| 248 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeoutInterleaved) { | 281 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataNoTimeoutInterleaved) { |
| 249 FetchSuggestionsDataNoTimeoutHelper(true); | 282 FetchSuggestionsDataNoTimeoutHelper(true); |
| 250 } | 283 } |
| 251 | 284 |
| 252 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataRequestError) { | 285 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataRequestError) { |
| 253 // Field trial enabled with a specific suggestions URL. | 286 // Field trial enabled with a specific suggestions URL. |
| 254 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 287 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 255 kFakeBlacklistSuffix, false); | 288 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
| 256 scoped_ptr<SuggestionsService> suggestions_service( | 289 scoped_ptr<SuggestionsService> suggestions_service( |
| 257 CreateSuggestionsServiceWithMockStore()); | 290 CreateSuggestionsServiceWithMocks()); |
| 258 EXPECT_TRUE(suggestions_service != NULL); | 291 EXPECT_TRUE(suggestions_service != NULL); |
| 259 | 292 |
| 260 // Fake a request error. | 293 // Fake a request error. |
| 261 std::string expected_url = | 294 std::string expected_url = |
| 262 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 295 (std::string(kFakeSuggestionsURL) + "?") + kFakeSuggestionsCommonParams; |
| 263 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", net::HTTP_OK, | 296 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", net::HTTP_OK, |
| 264 net::URLRequestStatus::FAILED); | 297 net::URLRequestStatus::FAILED); |
| 265 | 298 |
| 266 // Set up expectations on the SuggestionsStore. | 299 // Set up expectations on the SuggestionsStore. |
| 267 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_)) | 300 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_)) |
| 268 .WillOnce(Return(true)); | 301 .WillOnce(Return(true)); |
| 269 | 302 |
| 303 // Expect a call to the blacklist store. Return that there's nothing to |
| 304 // blacklist. |
| 305 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); |
| 306 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) |
| 307 .WillOnce(Return(false)); |
| 308 |
| 270 // Send the request. Empty data will be returned to the callback. | 309 // Send the request. Empty data will be returned to the callback. |
| 271 suggestions_service->FetchSuggestionsData( | 310 suggestions_service->FetchSuggestionsData( |
| 272 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 311 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
| 273 base::Unretained(this))); | 312 base::Unretained(this))); |
| 274 | 313 |
| 275 // (Testing only) wait until suggestion fetch is complete. | 314 // (Testing only) wait until suggestion fetch is complete. |
| 276 base::MessageLoop::current()->RunUntilIdle(); | 315 base::MessageLoop::current()->RunUntilIdle(); |
| 277 | 316 |
| 278 // Ensure that ExpectEmptySuggestionsProfile ran once. | 317 // Ensure that ExpectEmptySuggestionsProfile ran once. |
| 279 EXPECT_EQ(1, suggestions_empty_data_count_); | 318 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 280 } | 319 } |
| 281 | 320 |
| 282 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { | 321 TEST_F(SuggestionsServiceTest, FetchSuggestionsDataResponseNotOK) { |
| 283 // Field trial enabled with a specific suggestions URL. | 322 // Field trial enabled with a specific suggestions URL. |
| 284 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 323 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 285 kFakeBlacklistSuffix, false); | 324 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
| 286 scoped_ptr<SuggestionsService> suggestions_service( | 325 scoped_ptr<SuggestionsService> suggestions_service( |
| 287 CreateSuggestionsServiceWithMockStore()); | 326 CreateSuggestionsServiceWithMocks()); |
| 288 EXPECT_TRUE(suggestions_service != NULL); | 327 EXPECT_TRUE(suggestions_service != NULL); |
| 289 | 328 |
| 290 // Response code != 200. | 329 // Response code != 200. |
| 291 std::string expected_url = | 330 std::string expected_url = |
| 292 std::string(kFakeSuggestionsURL) + kFakeSuggestionsSuffix; | 331 (std::string(kFakeSuggestionsURL) + "?") + kFakeSuggestionsCommonParams; |
| 293 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", | 332 factory_.SetFakeResponse(GURL(expected_url), "irrelevant", |
| 294 net::HTTP_BAD_REQUEST, | 333 net::HTTP_BAD_REQUEST, |
| 295 net::URLRequestStatus::SUCCESS); | 334 net::URLRequestStatus::SUCCESS); |
| 296 | 335 |
| 297 // Set up expectations on the SuggestionsStore. | 336 // Set up expectations on the SuggestionsStore. |
| 298 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions()); | 337 EXPECT_CALL(*mock_suggestions_store_, ClearSuggestions()); |
| 299 | 338 |
| 339 // Expect a call to the blacklist store. Return that there's nothing to |
| 340 // blacklist. |
| 341 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) |
| 342 .WillOnce(Return(false)); |
| 343 |
| 300 // Send the request. Empty data will be returned to the callback. | 344 // Send the request. Empty data will be returned to the callback. |
| 301 suggestions_service->FetchSuggestionsData( | 345 suggestions_service->FetchSuggestionsData( |
| 302 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, | 346 base::Bind(&SuggestionsServiceTest::ExpectEmptySuggestionsProfile, |
| 303 base::Unretained(this))); | 347 base::Unretained(this))); |
| 304 | 348 |
| 305 // (Testing only) wait until suggestion fetch is complete. | 349 // (Testing only) wait until suggestion fetch is complete. |
| 306 base::MessageLoop::current()->RunUntilIdle(); | 350 base::MessageLoop::current()->RunUntilIdle(); |
| 307 | 351 |
| 308 // Ensure that ExpectEmptySuggestionsProfile ran once. | 352 // Ensure that ExpectEmptySuggestionsProfile ran once. |
| 309 EXPECT_EQ(1, suggestions_empty_data_count_); | 353 EXPECT_EQ(1, suggestions_empty_data_count_); |
| 310 } | 354 } |
| 311 | 355 |
| 312 TEST_F(SuggestionsServiceTest, BlacklistURL) { | 356 TEST_F(SuggestionsServiceTest, BlacklistURL) { |
| 313 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsSuffix, | 357 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 314 kFakeBlacklistSuffix, false); | 358 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
| 315 scoped_ptr<SuggestionsService> suggestions_service( | 359 scoped_ptr<SuggestionsService> suggestions_service( |
| 316 CreateSuggestionsServiceWithMockStore()); | 360 CreateSuggestionsServiceWithMocks()); |
| 317 EXPECT_TRUE(suggestions_service != NULL); | 361 EXPECT_TRUE(suggestions_service != NULL); |
| 318 | 362 |
| 319 std::string expected_url(kFakeSuggestionsURL); | 363 GURL blacklist_url(kBlacklistUrl); |
| 320 expected_url.append(kFakeBlacklistSuffix) | 364 std::string request_url = GetExpectedBlacklistRequestUrl(blacklist_url); |
| 321 .append(net::EscapeQueryParamValue(GURL(kBlacklistUrl).spec(), true)); | |
| 322 scoped_ptr<SuggestionsProfile> suggestions_profile( | 365 scoped_ptr<SuggestionsProfile> suggestions_profile( |
| 323 CreateSuggestionsProfile()); | 366 CreateSuggestionsProfile()); |
| 324 factory_.SetFakeResponse(GURL(expected_url), | 367 factory_.SetFakeResponse(GURL(request_url), |
| 325 suggestions_profile->SerializeAsString(), | 368 suggestions_profile->SerializeAsString(), |
| 326 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 369 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 327 | 370 |
| 328 // Set up expectations on the SuggestionsStore. | 371 // Set up expectations on the SuggestionsStore. |
| 329 EXPECT_CALL(*mock_suggestions_store_, | 372 EXPECT_CALL(*mock_suggestions_store_, |
| 330 StoreSuggestions(EqualsProto(*suggestions_profile))) | 373 StoreSuggestions(EqualsProto(*suggestions_profile))) |
| 331 .WillOnce(Return(true)); | 374 .WillOnce(Return(true)); |
| 332 | 375 |
| 376 // Expected calls to the blacklist store. |
| 377 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url))) |
| 378 .WillOnce(Return(true)); |
| 379 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url))) |
| 380 .WillOnce(Return(true)); |
| 381 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)); |
| 382 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) |
| 383 .WillOnce(Return(false)); |
| 384 |
| 333 // Send the request. The data will be returned to the callback. | 385 // Send the request. The data will be returned to the callback. |
| 334 suggestions_service->BlacklistURL( | 386 suggestions_service->BlacklistURL( |
| 335 GURL(kBlacklistUrl), | 387 blacklist_url, base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, |
| 336 base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, | 388 base::Unretained(this))); |
| 337 base::Unretained(this))); | |
| 338 | 389 |
| 339 // (Testing only) wait until blacklist request is complete. | 390 // (Testing only) wait until blacklist request is complete. |
| 340 base::MessageLoop::current()->RunUntilIdle(); | 391 base::MessageLoop::current()->RunUntilIdle(); |
| 341 | 392 |
| 342 // Ensure that CheckSuggestionsData() ran once. | 393 // Ensure that CheckSuggestionsData() ran once. |
| 343 EXPECT_EQ(1, suggestions_data_check_count_); | 394 EXPECT_EQ(1, suggestions_data_check_count_); |
| 344 } | 395 } |
| 345 | 396 |
| 397 // Initial blacklist request fails, triggering a scheduled upload which |
| 398 // succeeds. |
| 399 TEST_F(SuggestionsServiceTest, BlacklistURLFails) { |
| 400 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 401 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
| 402 scoped_ptr<SuggestionsService> suggestions_service( |
| 403 CreateSuggestionsServiceWithMocks()); |
| 404 EXPECT_TRUE(suggestions_service != NULL); |
| 405 suggestions_service->set_blacklist_delay(0); // Don't wait during a test! |
| 406 scoped_ptr<SuggestionsProfile> suggestions_profile( |
| 407 CreateSuggestionsProfile()); |
| 408 GURL blacklist_url(kBlacklistUrl); |
| 409 |
| 410 // Set up behavior for the first call to blacklist. |
| 411 std::string request_url = GetExpectedBlacklistRequestUrl(blacklist_url); |
| 412 factory_.SetFakeResponse(GURL(request_url), "irrelevant", net::HTTP_OK, |
| 413 net::URLRequestStatus::FAILED); |
| 414 |
| 415 // Expectations specific to the first request. |
| 416 EXPECT_CALL(*mock_blacklist_store_, BlacklistUrl(Eq(blacklist_url))) |
| 417 .WillOnce(Return(true)); |
| 418 EXPECT_CALL(*mock_suggestions_store_, LoadSuggestions(_)) |
| 419 .WillOnce(DoAll(SetArgPointee<0>(*suggestions_profile), Return(true))); |
| 420 |
| 421 // Expectations specific to the second request. |
| 422 EXPECT_CALL(*mock_suggestions_store_, |
| 423 StoreSuggestions(EqualsProto(*suggestions_profile))) |
| 424 .WillOnce(Return(true)); |
| 425 EXPECT_CALL(*mock_blacklist_store_, RemoveUrl(Eq(blacklist_url))) |
| 426 .WillOnce(Return(true)); |
| 427 |
| 428 // Expectations pertaining to both requests. |
| 429 EXPECT_CALL(*mock_blacklist_store_, FilterSuggestions(_)).Times(2); |
| 430 EXPECT_CALL(*mock_blacklist_store_, GetFirstUrlFromBlacklist(_)) |
| 431 .WillOnce(Return(true)) |
| 432 .WillOnce(DoAll(SetArgPointee<0>(blacklist_url), Return(true))) |
| 433 .WillOnce(Return(false)); |
| 434 |
| 435 // Send the request. The data will be returned to the callback. |
| 436 suggestions_service->BlacklistURL( |
| 437 blacklist_url, base::Bind(&SuggestionsServiceTest::CheckSuggestionsData, |
| 438 base::Unretained(this))); |
| 439 |
| 440 // The first FakeURLFetcher was created; we can now set up behavior for the |
| 441 // second call to blacklist. |
| 442 factory_.SetFakeResponse(GURL(request_url), |
| 443 suggestions_profile->SerializeAsString(), |
| 444 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 445 |
| 446 // (Testing only) wait until both requests are complete. |
| 447 base::MessageLoop::current()->RunUntilIdle(); |
| 448 |
| 449 // Ensure that CheckSuggestionsData() ran once. |
| 450 EXPECT_EQ(1, suggestions_data_check_count_); |
| 451 } |
| 452 |
| 453 TEST_F(SuggestionsServiceTest, GetBlacklistedUrl) { |
| 454 EnableFieldTrial(kFakeSuggestionsURL, kFakeSuggestionsCommonParams, |
| 455 kFakeBlacklistPath, kFakeBlacklistUrlParam, false); |
| 456 |
| 457 scoped_ptr<GURL> request_url; |
| 458 scoped_ptr<net::FakeURLFetcher> fetcher; |
| 459 GURL retrieved_url; |
| 460 |
| 461 // Not a blacklist request. |
| 462 request_url.reset(new GURL("http://not-blacklisting.com/a?b=c")); |
| 463 fetcher = CreateURLFetcher(*request_url, NULL, "", net::HTTP_OK, |
| 464 net::URLRequestStatus::SUCCESS); |
| 465 EXPECT_FALSE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); |
| 466 |
| 467 // An actual blacklist request. |
| 468 string blacklisted_url = "http://blacklisted.com/a?b=c&d=e"; |
| 469 string encoded_blacklisted_url = |
| 470 "http%3A%2F%2Fblacklisted.com%2Fa%3Fb%3Dc%26d%3De"; |
| 471 string blacklist_request_prefix = |
| 472 "https://mysuggestions.com/proto/blacklist?foo=bar&baz="; |
| 473 request_url.reset( |
| 474 new GURL(blacklist_request_prefix + encoded_blacklisted_url)); |
| 475 fetcher.reset(); |
| 476 fetcher = CreateURLFetcher(*request_url, NULL, "", net::HTTP_OK, |
| 477 net::URLRequestStatus::SUCCESS); |
| 478 EXPECT_TRUE(SuggestionsService::GetBlacklistedUrl(*fetcher, &retrieved_url)); |
| 479 EXPECT_EQ(blacklisted_url, retrieved_url.spec()); |
| 480 } |
| 481 |
| 482 TEST_F(SuggestionsServiceTest, UpdateBlacklistDelay) { |
| 483 scoped_ptr<SuggestionsService> suggestions_service( |
| 484 CreateSuggestionsServiceWithMocks()); |
| 485 int initial_delay = suggestions_service->blacklist_delay(); |
| 486 |
| 487 // Delay unchanged on success. |
| 488 suggestions_service->UpdateBlacklistDelay(true); |
| 489 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); |
| 490 |
| 491 // Delay increases on failure. |
| 492 suggestions_service->UpdateBlacklistDelay(false); |
| 493 EXPECT_GT(suggestions_service->blacklist_delay(), initial_delay); |
| 494 |
| 495 // Delay resets on success. |
| 496 suggestions_service->UpdateBlacklistDelay(true); |
| 497 EXPECT_EQ(initial_delay, suggestions_service->blacklist_delay()); |
| 498 } |
| 499 |
| 346 } // namespace suggestions | 500 } // namespace suggestions |
| OLD | NEW |