| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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/autocomplete/search_provider.h" | 5 #include "chrome/browser/autocomplete/search_provider.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| 11 #include "base/metrics/user_metrics.h" |
| 11 #include "base/prefs/pref_service.h" | 12 #include "base/prefs/pref_service.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 19 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" | 20 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h" |
| 20 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 21 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 | 59 |
| 59 // Returns the first match in |matches| with |allowed_to_be_default_match| | 60 // Returns the first match in |matches| with |allowed_to_be_default_match| |
| 60 // set to true. | 61 // set to true. |
| 61 ACMatches::const_iterator FindDefaultMatch(const ACMatches& matches) { | 62 ACMatches::const_iterator FindDefaultMatch(const ACMatches& matches) { |
| 62 ACMatches::const_iterator it = matches.begin(); | 63 ACMatches::const_iterator it = matches.begin(); |
| 63 while ((it != matches.end()) && !it->allowed_to_be_default_match) | 64 while ((it != matches.end()) && !it->allowed_to_be_default_match) |
| 64 ++it; | 65 ++it; |
| 65 return it; | 66 return it; |
| 66 } | 67 } |
| 67 | 68 |
| 68 class SuggestionDeletionHandler; | 69 void CopyString(std::string* out, const std::string& in) { *out = in; } |
| 69 class SearchProviderForTest : public SearchProvider { | |
| 70 public: | |
| 71 SearchProviderForTest(AutocompleteProviderListener* listener, | |
| 72 TemplateURLService* template_url_service, | |
| 73 Profile* profile); | |
| 74 bool is_success() { return is_success_; }; | |
| 75 | |
| 76 protected: | |
| 77 virtual ~SearchProviderForTest(); | |
| 78 | |
| 79 private: | |
| 80 virtual void RecordDeletionResult(bool success) OVERRIDE; | |
| 81 bool is_success_; | |
| 82 DISALLOW_COPY_AND_ASSIGN(SearchProviderForTest); | |
| 83 }; | |
| 84 | |
| 85 SearchProviderForTest::SearchProviderForTest( | |
| 86 AutocompleteProviderListener* listener, | |
| 87 TemplateURLService* template_url_service, | |
| 88 Profile* profile) | |
| 89 : SearchProvider(listener, template_url_service, profile), | |
| 90 is_success_(false) { | |
| 91 } | |
| 92 | |
| 93 SearchProviderForTest::~SearchProviderForTest() { | |
| 94 } | |
| 95 | |
| 96 void SearchProviderForTest::RecordDeletionResult(bool success) { | |
| 97 is_success_ = success; | |
| 98 } | |
| 99 | 70 |
| 100 } // namespace | 71 } // namespace |
| 101 | 72 |
| 102 // SearchProviderTest --------------------------------------------------------- | 73 // SearchProviderTest --------------------------------------------------------- |
| 103 | 74 |
| 104 // The following environment is configured for these tests: | 75 // The following environment is configured for these tests: |
| 105 // . The TemplateURL default_t_url_ is set as the default provider. | 76 // . The TemplateURL default_t_url_ is set as the default provider. |
| 106 // . The TemplateURL keyword_t_url_ is added to the TemplateURLService. This | 77 // . The TemplateURL keyword_t_url_ is added to the TemplateURLService. This |
| 107 // TemplateURL has a valid suggest and search URL. | 78 // TemplateURL has a valid suggest and search URL. |
| 108 // . The URL created by using the search term term1_ with default_t_url_ is | 79 // . The URL created by using the search term term1_ with default_t_url_ is |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 | 195 |
| 225 content::TestBrowserThreadBundle thread_bundle_; | 196 content::TestBrowserThreadBundle thread_bundle_; |
| 226 | 197 |
| 227 // URLFetcherFactory implementation registered. | 198 // URLFetcherFactory implementation registered. |
| 228 net::TestURLFetcherFactory test_factory_; | 199 net::TestURLFetcherFactory test_factory_; |
| 229 | 200 |
| 230 // Profile we use. | 201 // Profile we use. |
| 231 TestingProfile profile_; | 202 TestingProfile profile_; |
| 232 | 203 |
| 233 // The provider. | 204 // The provider. |
| 234 scoped_refptr<SearchProviderForTest> provider_; | 205 scoped_refptr<SearchProvider> provider_; |
| 206 |
| 207 // The last action recorded with base::RecordAction(). |
| 208 std::string last_user_metrics_action_; |
| 235 | 209 |
| 236 // If non-NULL, OnProviderUpdate quits the current |run_loop_|. | 210 // If non-NULL, OnProviderUpdate quits the current |run_loop_|. |
| 237 base::RunLoop* run_loop_; | 211 base::RunLoop* run_loop_; |
| 238 | 212 |
| 239 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest); | 213 DISALLOW_COPY_AND_ASSIGN(SearchProviderTest); |
| 240 }; | 214 }; |
| 241 | 215 |
| 242 // static | 216 // static |
| 243 const std::string SearchProviderTest::kNotApplicable = "Not Applicable"; | 217 const std::string SearchProviderTest::kNotApplicable = "Not Applicable"; |
| 244 | 218 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 ASSERT_NE(0, keyword_t_url_->id()); | 256 ASSERT_NE(0, keyword_t_url_->id()); |
| 283 | 257 |
| 284 // Add a page and search term for keyword_t_url_. | 258 // Add a page and search term for keyword_t_url_. |
| 285 keyword_url_ = AddSearchToHistory(keyword_t_url_, keyword_term_, 1); | 259 keyword_url_ = AddSearchToHistory(keyword_t_url_, keyword_term_, 1); |
| 286 | 260 |
| 287 // Keywords are updated by the InMemoryHistoryBackend only after the message | 261 // Keywords are updated by the InMemoryHistoryBackend only after the message |
| 288 // has been processed on the history thread. Block until history processes all | 262 // has been processed on the history thread. Block until history processes all |
| 289 // requests to ensure the InMemoryDatabase is the state we expect it. | 263 // requests to ensure the InMemoryDatabase is the state we expect it. |
| 290 profile_.BlockUntilHistoryProcessesPendingRequests(); | 264 profile_.BlockUntilHistoryProcessesPendingRequests(); |
| 291 | 265 |
| 292 provider_ = new SearchProviderForTest(this, turl_model, &profile_); | 266 provider_ = new SearchProvider(this, turl_model, &profile_); |
| 293 provider_->kMinimumTimeBetweenSuggestQueriesMs = 0; | 267 provider_->kMinimumTimeBetweenSuggestQueriesMs = 0; |
| 268 |
| 269 base::AddActionCallback(base::Bind(&CopyString, &last_user_metrics_action_)); |
| 294 } | 270 } |
| 295 | 271 |
| 296 void SearchProviderTest::TearDown() { | 272 void SearchProviderTest::TearDown() { |
| 297 base::RunLoop().RunUntilIdle(); | 273 base::RunLoop().RunUntilIdle(); |
| 298 | 274 |
| 299 // Shutdown the provider before the profile. | 275 // Shutdown the provider before the profile. |
| 300 provider_ = NULL; | 276 provider_ = NULL; |
| 301 } | 277 } |
| 302 | 278 |
| 303 void SearchProviderTest::RunTest(TestData* cases, | 279 void SearchProviderTest::RunTest(TestData* cases, |
| (...skipping 2767 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3071 provider_->DeleteMatch(match); | 3047 provider_->DeleteMatch(match); |
| 3072 EXPECT_FALSE(provider_->deletion_handlers_.empty()); | 3048 EXPECT_FALSE(provider_->deletion_handlers_.empty()); |
| 3073 EXPECT_TRUE(provider_->matches_.empty()); | 3049 EXPECT_TRUE(provider_->matches_.empty()); |
| 3074 // Set up a default fetcher with provided results. | 3050 // Set up a default fetcher with provided results. |
| 3075 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | 3051 net::TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 3076 SearchProvider::kDeletionURLFetcherID); | 3052 SearchProvider::kDeletionURLFetcherID); |
| 3077 ASSERT_TRUE(fetcher); | 3053 ASSERT_TRUE(fetcher); |
| 3078 fetcher->set_response_code(200); | 3054 fetcher->set_response_code(200); |
| 3079 fetcher->delegate()->OnURLFetchComplete(fetcher); | 3055 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 3080 EXPECT_TRUE(provider_->deletion_handlers_.empty()); | 3056 EXPECT_TRUE(provider_->deletion_handlers_.empty()); |
| 3081 EXPECT_TRUE(provider_->is_success()); | 3057 EXPECT_EQ("Omnibox.ServerSuggestDelete.Success", last_user_metrics_action_); |
| 3082 | 3058 |
| 3083 // Test a failing deletion request. | 3059 // Test a failing deletion request. |
| 3084 provider_->matches_.push_back(match); | 3060 provider_->matches_.push_back(match); |
| 3085 provider_->DeleteMatch(match); | 3061 provider_->DeleteMatch(match); |
| 3086 EXPECT_FALSE(provider_->deletion_handlers_.empty()); | 3062 EXPECT_FALSE(provider_->deletion_handlers_.empty()); |
| 3087 // Set up a default fetcher with provided results. | 3063 // Set up a default fetcher with provided results. |
| 3088 fetcher = test_factory_.GetFetcherByID( | 3064 fetcher = test_factory_.GetFetcherByID( |
| 3089 SearchProvider::kDeletionURLFetcherID); | 3065 SearchProvider::kDeletionURLFetcherID); |
| 3090 ASSERT_TRUE(fetcher); | 3066 ASSERT_TRUE(fetcher); |
| 3091 fetcher->set_response_code(500); | 3067 fetcher->set_response_code(500); |
| 3092 fetcher->delegate()->OnURLFetchComplete(fetcher); | 3068 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 3093 EXPECT_TRUE(provider_->deletion_handlers_.empty()); | 3069 EXPECT_TRUE(provider_->deletion_handlers_.empty()); |
| 3094 EXPECT_FALSE(provider_->is_success()); | 3070 EXPECT_EQ("Omnibox.ServerSuggestDelete.Failure", last_user_metrics_action_); |
| 3095 } | 3071 } |
| 3096 | 3072 |
| 3097 TEST_F(SearchProviderTest, TestDeleteHistoryQueryMatch) { | 3073 TEST_F(SearchProviderTest, TestDeleteHistoryQueryMatch) { |
| 3098 GURL term_url( | 3074 GURL term_url( |
| 3099 AddSearchToHistory(default_t_url_, ASCIIToUTF16("flash games"), 1)); | 3075 AddSearchToHistory(default_t_url_, ASCIIToUTF16("flash games"), 1)); |
| 3100 profile_.BlockUntilHistoryProcessesPendingRequests(); | 3076 profile_.BlockUntilHistoryProcessesPendingRequests(); |
| 3101 | 3077 |
| 3102 AutocompleteMatch games; | 3078 AutocompleteMatch games; |
| 3103 QueryForInput(ASCIIToUTF16("fla"), false, false); | 3079 QueryForInput(ASCIIToUTF16("fla"), false, false); |
| 3104 profile_.BlockUntilHistoryProcessesPendingRequests(); | 3080 profile_.BlockUntilHistoryProcessesPendingRequests(); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3268 // Mismatching input will return empty prefetch data. | 3244 // Mismatching input will return empty prefetch data. |
| 3269 AutocompleteInput input2(base::ASCIIToUTF16("weather n"), | 3245 AutocompleteInput input2(base::ASCIIToUTF16("weather n"), |
| 3270 base::string16::npos, base::string16(), GURL(), | 3246 base::string16::npos, base::string16(), GURL(), |
| 3271 metrics::OmniboxEventProto::INVALID_SPEC, false, | 3247 metrics::OmniboxEventProto::INVALID_SPEC, false, |
| 3272 false, true, true, | 3248 false, true, true, |
| 3273 ChromeAutocompleteSchemeClassifier(&profile_)); | 3249 ChromeAutocompleteSchemeClassifier(&profile_)); |
| 3274 provider_->DoAnswersQuery(input2); | 3250 provider_->DoAnswersQuery(input2); |
| 3275 EXPECT_TRUE(provider_->prefetch_data_.full_query_text.empty()); | 3251 EXPECT_TRUE(provider_->prefetch_data_.full_query_text.empty()); |
| 3276 EXPECT_TRUE(provider_->prefetch_data_.query_type.empty()); | 3252 EXPECT_TRUE(provider_->prefetch_data_.query_type.empty()); |
| 3277 } | 3253 } |
| OLD | NEW |