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