| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/string_util.h" | 5 #include "base/string_util.h" |
| 6 #include "base/time.h" | 6 #include "base/time.h" |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 #include "chrome/browser/autocomplete/autocomplete_match.h" | 9 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 10 #include "chrome/browser/autocomplete/search_provider.h" | 10 #include "chrome/browser/autocomplete/search_provider.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 // ACProviderListener method. If we're waiting for the provider to finish, | 52 // ACProviderListener method. If we're waiting for the provider to finish, |
| 53 // this exits the message loop. | 53 // this exits the message loop. |
| 54 virtual void OnProviderUpdate(bool updated_matches); | 54 virtual void OnProviderUpdate(bool updated_matches); |
| 55 | 55 |
| 56 // Runs a nested message loop until provider_ is done. The message loop is | 56 // Runs a nested message loop until provider_ is done. The message loop is |
| 57 // exited by way of OnProviderUPdate. | 57 // exited by way of OnProviderUPdate. |
| 58 void RunTillProviderDone(); | 58 void RunTillProviderDone(); |
| 59 | 59 |
| 60 // Invokes Start on provider_, then runs all pending tasks. | 60 // Invokes Start on provider_, then runs all pending tasks. |
| 61 void QueryForInput(const string16& text); | 61 void QueryForInput(const string16& text, |
| 62 bool prevent_inline_autocomplete); |
| 62 | 63 |
| 63 // See description above class for details of these fields. | 64 // See description above class for details of these fields. |
| 64 TemplateURL* default_t_url_; | 65 TemplateURL* default_t_url_; |
| 65 const string16 term1_; | 66 const string16 term1_; |
| 66 GURL term1_url_; | 67 GURL term1_url_; |
| 67 TemplateURL* keyword_t_url_; | 68 TemplateURL* keyword_t_url_; |
| 68 const string16 keyword_term_; | 69 const string16 keyword_term_; |
| 69 GURL keyword_url_; | 70 GURL keyword_url_; |
| 70 | 71 |
| 71 MessageLoopForUI message_loop_; | 72 MessageLoopForUI message_loop_; |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 return; | 156 return; |
| 156 | 157 |
| 157 quit_when_done_ = true; | 158 quit_when_done_ = true; |
| 158 #if defined(OS_MACOSX) | 159 #if defined(OS_MACOSX) |
| 159 message_loop_.Run(); | 160 message_loop_.Run(); |
| 160 #else | 161 #else |
| 161 message_loop_.Run(NULL); | 162 message_loop_.Run(NULL); |
| 162 #endif | 163 #endif |
| 163 } | 164 } |
| 164 | 165 |
| 165 void SearchProviderTest::QueryForInput(const string16& text) { | 166 void SearchProviderTest::QueryForInput(const string16& text, |
| 167 bool prevent_inline_autocomplete) { |
| 166 // Start a query. | 168 // Start a query. |
| 167 AutocompleteInput input(UTF16ToWide(text), std::wstring(), | 169 AutocompleteInput input(UTF16ToWide(text), std::wstring(), |
| 168 false, false, true, false); | 170 prevent_inline_autocomplete, false, true, false); |
| 169 provider_->Start(input, false); | 171 provider_->Start(input, false); |
| 170 | 172 |
| 171 // RunAllPending so that the task scheduled by SearchProvider to create the | 173 // RunAllPending so that the task scheduled by SearchProvider to create the |
| 172 // URLFetchers runs. | 174 // URLFetchers runs. |
| 173 message_loop_.RunAllPending(); | 175 message_loop_.RunAllPending(); |
| 174 } | 176 } |
| 175 | 177 |
| 176 void SearchProviderTest::TearDown() { | 178 void SearchProviderTest::TearDown() { |
| 177 message_loop_.RunAllPending(); | 179 message_loop_.RunAllPending(); |
| 178 | 180 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 191 } | 193 } |
| 192 return AutocompleteMatch(NULL, 1, false, AutocompleteMatch::HISTORY_URL); | 194 return AutocompleteMatch(NULL, 1, false, AutocompleteMatch::HISTORY_URL); |
| 193 } | 195 } |
| 194 | 196 |
| 195 // Tests ----------------------------------------------------------------------- | 197 // Tests ----------------------------------------------------------------------- |
| 196 | 198 |
| 197 // Make sure we query history for the default provider and a URLFetcher is | 199 // Make sure we query history for the default provider and a URLFetcher is |
| 198 // created for the default provider suggest results. | 200 // created for the default provider suggest results. |
| 199 TEST_F(SearchProviderTest, QueryDefaultProvider) { | 201 TEST_F(SearchProviderTest, QueryDefaultProvider) { |
| 200 string16 term = term1_.substr(0, term1_.size() - 1); | 202 string16 term = term1_.substr(0, term1_.size() - 1); |
| 201 QueryForInput(term); | 203 QueryForInput(term, false); |
| 202 | 204 |
| 203 // Make sure the default providers suggest service was queried. | 205 // Make sure the default providers suggest service was queried. |
| 204 TestURLFetcher* fetcher = test_factory_.GetFetcherByID( | 206 TestURLFetcher* fetcher = test_factory_.GetFetcherByID( |
| 205 SearchProvider::kDefaultProviderURLFetcherID); | 207 SearchProvider::kDefaultProviderURLFetcherID); |
| 206 ASSERT_TRUE(fetcher); | 208 ASSERT_TRUE(fetcher); |
| 207 | 209 |
| 208 // And the URL matches what we expected. | 210 // And the URL matches what we expected. |
| 209 GURL expected_url = GURL(default_t_url_->suggestions_url()-> | 211 GURL expected_url = GURL(default_t_url_->suggestions_url()-> |
| 210 ReplaceSearchTerms(*default_t_url_, UTF16ToWide(term), | 212 ReplaceSearchTerms(*default_t_url_, UTF16ToWide(term), |
| 211 0, std::wstring())); | 213 0, std::wstring())); |
| 212 ASSERT_TRUE(fetcher->original_url() == expected_url); | 214 ASSERT_TRUE(fetcher->original_url() == expected_url); |
| 213 | 215 |
| 214 // Tell the SearchProvider the suggest query is done. | 216 // Tell the SearchProvider the suggest query is done. |
| 215 fetcher->delegate()->OnURLFetchComplete( | 217 fetcher->delegate()->OnURLFetchComplete( |
| 216 fetcher, GURL(), URLRequestStatus(), 200, ResponseCookies(), | 218 fetcher, GURL(), URLRequestStatus(), 200, ResponseCookies(), |
| 217 std::string()); | 219 std::string()); |
| 218 fetcher = NULL; | 220 fetcher = NULL; |
| 219 | 221 |
| 220 // Run till the history results complete. | 222 // Run till the history results complete. |
| 221 RunTillProviderDone(); | 223 RunTillProviderDone(); |
| 222 | 224 |
| 223 // The SearchProvider is done. Make sure it has a result for the history | 225 // The SearchProvider is done. Make sure it has a result for the history |
| 224 // term term1. | 226 // term term1. |
| 225 AutocompleteMatch match = FindMatchWithDestination(term1_url_); | 227 AutocompleteMatch match = FindMatchWithDestination(term1_url_); |
| 226 ASSERT_TRUE(!match.destination_url.is_empty()); | 228 ASSERT_TRUE(!match.destination_url.is_empty()); |
| 227 } | 229 } |
| 228 | 230 |
| 231 TEST_F(SearchProviderTest, HonorPreventInlineAutocomplete) { |
| 232 string16 term = term1_.substr(0, term1_.size() - 1); |
| 233 QueryForInput(term, true); |
| 234 |
| 235 ASSERT_FALSE(provider_->matches().empty()); |
| 236 ASSERT_EQ(AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, |
| 237 provider_->matches()[0].type); |
| 238 } |
| 239 |
| 229 // Issues a query that matches the registered keyword and makes sure history | 240 // Issues a query that matches the registered keyword and makes sure history |
| 230 // is queried as well as URLFetchers getting created. | 241 // is queried as well as URLFetchers getting created. |
| 231 TEST_F(SearchProviderTest, QueryKeywordProvider) { | 242 TEST_F(SearchProviderTest, QueryKeywordProvider) { |
| 232 string16 term = keyword_term_.substr(0, keyword_term_.size() - 1); | 243 string16 term = keyword_term_.substr(0, keyword_term_.size() - 1); |
| 233 QueryForInput(WideToUTF16(keyword_t_url_->keyword()) + | 244 QueryForInput(WideToUTF16(keyword_t_url_->keyword()) + |
| 234 UTF8ToUTF16(" ") + term); | 245 UTF8ToUTF16(" ") + term, false); |
| 235 | 246 |
| 236 // Make sure the default providers suggest service was queried. | 247 // Make sure the default providers suggest service was queried. |
| 237 TestURLFetcher* default_fetcher = test_factory_.GetFetcherByID( | 248 TestURLFetcher* default_fetcher = test_factory_.GetFetcherByID( |
| 238 SearchProvider::kDefaultProviderURLFetcherID); | 249 SearchProvider::kDefaultProviderURLFetcherID); |
| 239 ASSERT_TRUE(default_fetcher); | 250 ASSERT_TRUE(default_fetcher); |
| 240 | 251 |
| 241 // Tell the SearchProvider the default suggest query is done. | 252 // Tell the SearchProvider the default suggest query is done. |
| 242 default_fetcher->delegate()->OnURLFetchComplete( | 253 default_fetcher->delegate()->OnURLFetchComplete( |
| 243 default_fetcher, GURL(), URLRequestStatus(), 200, ResponseCookies(), | 254 default_fetcher, GURL(), URLRequestStatus(), 200, ResponseCookies(), |
| 244 std::string()); | 255 std::string()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 "http://username:password@hostname/", | 299 "http://username:password@hostname/", |
| 289 "file://filename", | 300 "file://filename", |
| 290 "data://data", | 301 "data://data", |
| 291 "unknownscheme:anything", | 302 "unknownscheme:anything", |
| 292 "http://hostname/?query=q", | 303 "http://hostname/?query=q", |
| 293 "http://hostname/path#ref", | 304 "http://hostname/path#ref", |
| 294 "https://hostname/path", | 305 "https://hostname/path", |
| 295 }; | 306 }; |
| 296 | 307 |
| 297 for (size_t i = 0; i < arraysize(inputs); ++i) { | 308 for (size_t i = 0; i < arraysize(inputs); ++i) { |
| 298 QueryForInput(ASCIIToUTF16(inputs[i])); | 309 QueryForInput(ASCIIToUTF16(inputs[i]), false); |
| 299 // Make sure the default providers suggest service was not queried. | 310 // Make sure the default providers suggest service was not queried. |
| 300 ASSERT_TRUE(test_factory_.GetFetcherByID( | 311 ASSERT_TRUE(test_factory_.GetFetcherByID( |
| 301 SearchProvider::kDefaultProviderURLFetcherID) == NULL); | 312 SearchProvider::kDefaultProviderURLFetcherID) == NULL); |
| 302 // Run till the history results complete. | 313 // Run till the history results complete. |
| 303 RunTillProviderDone(); | 314 RunTillProviderDone(); |
| 304 } | 315 } |
| 305 } | 316 } |
| OLD | NEW |