| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/message_loop.h" | 5 #include "base/message_loop.h" |
| 6 #include "base/scoped_ptr.h" | 6 #include "base/scoped_ptr.h" |
| 7 #include "base/string_number_conversions.h" | 7 #include "base/string_number_conversions.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" | 9 #include "base/utf_string_conversions.h" |
| 10 #include "chrome/browser/autocomplete/autocomplete.h" | 10 #include "chrome/browser/autocomplete/autocomplete.h" |
| 11 #include "chrome/browser/autocomplete/autocomplete_match.h" | 11 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 12 #include "chrome/browser/autocomplete/keyword_provider.h" | 12 #include "chrome/browser/autocomplete/keyword_provider.h" |
| 13 #include "chrome/browser/autocomplete/search_provider.h" | 13 #include "chrome/browser/autocomplete/search_provider.h" |
| 14 #include "chrome/browser/search_engines/template_url.h" | 14 #include "chrome/browser/search_engines/template_url.h" |
| 15 #include "chrome/browser/search_engines/template_url_model.h" | 15 #include "chrome/browser/search_engines/template_url_model.h" |
| 16 #include "chrome/common/notification_observer.h" | 16 #include "chrome/common/notification_observer.h" |
| 17 #include "chrome/common/notification_registrar.h" | 17 #include "chrome/common/notification_registrar.h" |
| 18 #include "chrome/common/notification_service.h" | 18 #include "chrome/common/notification_service.h" |
| 19 #include "chrome/test/testing_browser_process.h" |
| 20 #include "chrome/test/testing_browser_process_test.h" |
| 19 #include "chrome/test/testing_profile.h" | 21 #include "chrome/test/testing_profile.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 23 |
| 22 static std::ostream& operator<<(std::ostream& os, | 24 static std::ostream& operator<<(std::ostream& os, |
| 23 const AutocompleteResult::const_iterator& it) { | 25 const AutocompleteResult::const_iterator& it) { |
| 24 return os << static_cast<const AutocompleteMatch*>(&(*it)); | 26 return os << static_cast<const AutocompleteMatch*>(&(*it)); |
| 25 } | 27 } |
| 26 | 28 |
| 27 namespace { | 29 namespace { |
| 28 | 30 |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 ACProviders providers_; | 118 ACProviders providers_; |
| 117 | 119 |
| 118 AutocompleteResult result_; | 120 AutocompleteResult result_; |
| 119 | 121 |
| 120 private: | 122 private: |
| 121 // NotificationObserver | 123 // NotificationObserver |
| 122 virtual void Observe(NotificationType type, | 124 virtual void Observe(NotificationType type, |
| 123 const NotificationSource& source, | 125 const NotificationSource& source, |
| 124 const NotificationDetails& details); | 126 const NotificationDetails& details); |
| 125 | 127 |
| 128 ScopedTestingBrowserProcess browser_process_; |
| 129 |
| 126 MessageLoopForUI message_loop_; | 130 MessageLoopForUI message_loop_; |
| 127 scoped_ptr<AutocompleteController> controller_; | 131 scoped_ptr<AutocompleteController> controller_; |
| 128 NotificationRegistrar registrar_; | 132 NotificationRegistrar registrar_; |
| 129 TestingProfile profile_; | 133 TestingProfile profile_; |
| 130 }; | 134 }; |
| 131 | 135 |
| 132 void AutocompleteProviderTest::ResetControllerWithTestProviders( | 136 void AutocompleteProviderTest::ResetControllerWithTestProviders( |
| 133 bool same_destinations) { | 137 bool same_destinations) { |
| 134 // Forget about any existing providers. The controller owns them and will | 138 // Forget about any existing providers. The controller owns them and will |
| 135 // Release() them below, when we delete it during the call to reset(). | 139 // Release() them below, when we delete it during the call to reset(). |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 i != result_.end(); ++i) | 258 i != result_.end(); ++i) |
| 255 EXPECT_EQ(providers_[1], i->provider); | 259 EXPECT_EQ(providers_[1], i->provider); |
| 256 } | 260 } |
| 257 | 261 |
| 258 TEST_F(AutocompleteProviderTest, AllowExactKeywordMatch) { | 262 TEST_F(AutocompleteProviderTest, AllowExactKeywordMatch) { |
| 259 ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); | 263 ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); |
| 260 RunExactKeymatchTest(true); | 264 RunExactKeymatchTest(true); |
| 261 RunExactKeymatchTest(false); | 265 RunExactKeymatchTest(false); |
| 262 } | 266 } |
| 263 | 267 |
| 264 TEST(AutocompleteTest, InputType) { | 268 typedef TestingBrowserProcessTest AutocompleteTest; |
| 269 |
| 270 TEST_F(AutocompleteTest, InputType) { |
| 265 struct test_data { | 271 struct test_data { |
| 266 const string16 input; | 272 const string16 input; |
| 267 const AutocompleteInput::Type type; | 273 const AutocompleteInput::Type type; |
| 268 } input_cases[] = { | 274 } input_cases[] = { |
| 269 { ASCIIToUTF16(""), AutocompleteInput::INVALID }, | 275 { ASCIIToUTF16(""), AutocompleteInput::INVALID }, |
| 270 { ASCIIToUTF16("?"), AutocompleteInput::FORCED_QUERY }, | 276 { ASCIIToUTF16("?"), AutocompleteInput::FORCED_QUERY }, |
| 271 { ASCIIToUTF16("?foo"), AutocompleteInput::FORCED_QUERY }, | 277 { ASCIIToUTF16("?foo"), AutocompleteInput::FORCED_QUERY }, |
| 272 { ASCIIToUTF16("?foo bar"), AutocompleteInput::FORCED_QUERY }, | 278 { ASCIIToUTF16("?foo bar"), AutocompleteInput::FORCED_QUERY }, |
| 273 { ASCIIToUTF16("?http://foo.com/bar"), AutocompleteInput::FORCED_QUERY }, | 279 { ASCIIToUTF16("?http://foo.com/bar"), AutocompleteInput::FORCED_QUERY }, |
| 274 { ASCIIToUTF16("foo"), AutocompleteInput::UNKNOWN }, | 280 { ASCIIToUTF16("foo"), AutocompleteInput::UNKNOWN }, |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 }; | 357 }; |
| 352 | 358 |
| 353 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 359 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
| 354 SCOPED_TRACE(input_cases[i].input); | 360 SCOPED_TRACE(input_cases[i].input); |
| 355 AutocompleteInput input(input_cases[i].input, string16(), true, false, | 361 AutocompleteInput input(input_cases[i].input, string16(), true, false, |
| 356 true, false); | 362 true, false); |
| 357 EXPECT_EQ(input_cases[i].type, input.type()); | 363 EXPECT_EQ(input_cases[i].type, input.type()); |
| 358 } | 364 } |
| 359 } | 365 } |
| 360 | 366 |
| 361 TEST(AutocompleteTest, InputTypeWithDesiredTLD) { | 367 TEST_F(AutocompleteTest, InputTypeWithDesiredTLD) { |
| 362 struct test_data { | 368 struct test_data { |
| 363 const string16 input; | 369 const string16 input; |
| 364 const AutocompleteInput::Type type; | 370 const AutocompleteInput::Type type; |
| 365 } input_cases[] = { | 371 } input_cases[] = { |
| 366 { ASCIIToUTF16("401k"), AutocompleteInput::REQUESTED_URL }, | 372 { ASCIIToUTF16("401k"), AutocompleteInput::REQUESTED_URL }, |
| 367 { ASCIIToUTF16("999999999999999"), AutocompleteInput::REQUESTED_URL }, | 373 { ASCIIToUTF16("999999999999999"), AutocompleteInput::REQUESTED_URL }, |
| 368 }; | 374 }; |
| 369 | 375 |
| 370 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 376 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
| 371 AutocompleteInput input(input_cases[i].input, ASCIIToUTF16("com"), true, | 377 AutocompleteInput input(input_cases[i].input, ASCIIToUTF16("com"), true, |
| 372 false, true, false); | 378 false, true, false); |
| 373 EXPECT_EQ(input_cases[i].type, input.type()) << "Input: " << | 379 EXPECT_EQ(input_cases[i].type, input.type()) << "Input: " << |
| 374 input_cases[i].input; | 380 input_cases[i].input; |
| 375 } | 381 } |
| 376 } | 382 } |
| 377 | 383 |
| 378 // This tests for a regression where certain input in the omnibox caused us to | 384 // This tests for a regression where certain input in the omnibox caused us to |
| 379 // crash. As long as the test completes without crashing, we're fine. | 385 // crash. As long as the test completes without crashing, we're fine. |
| 380 TEST(AutocompleteTest, InputCrash) { | 386 TEST_F(AutocompleteTest, InputCrash) { |
| 381 AutocompleteInput input(WideToUTF16(L"\uff65@s"), string16(), true, false, | 387 AutocompleteInput input(WideToUTF16(L"\uff65@s"), string16(), true, false, |
| 382 true, false); | 388 true, false); |
| 383 } | 389 } |
| 384 | 390 |
| 385 // Test comparing matches relevance. | 391 // Test comparing matches relevance. |
| 386 TEST(AutocompleteMatch, MoreRelevant) { | 392 TEST(AutocompleteMatch, MoreRelevant) { |
| 387 struct RelevantCases { | 393 struct RelevantCases { |
| 388 int r1; | 394 int r1; |
| 389 int r2; | 395 int r2; |
| 390 bool expected_result; | 396 bool expected_result; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 Component(12, 11) }, | 439 Component(12, 11) }, |
| 434 { ASCIIToUTF16("view-source:"), Component(0, 11), kInvalidComponent }, | 440 { ASCIIToUTF16("view-source:"), Component(0, 11), kInvalidComponent }, |
| 435 { ASCIIToUTF16("view-source:garbage"), kInvalidComponent, | 441 { ASCIIToUTF16("view-source:garbage"), kInvalidComponent, |
| 436 Component(12, 7) }, | 442 Component(12, 7) }, |
| 437 { ASCIIToUTF16("view-source:http://http://foo"), Component(12, 4), | 443 { ASCIIToUTF16("view-source:http://http://foo"), Component(12, 4), |
| 438 Component(19, 4) }, | 444 Component(19, 4) }, |
| 439 { ASCIIToUTF16("view-source:view-source:http://example.com/"), | 445 { ASCIIToUTF16("view-source:view-source:http://example.com/"), |
| 440 Component(12, 11), kInvalidComponent } | 446 Component(12, 11), kInvalidComponent } |
| 441 }; | 447 }; |
| 442 | 448 |
| 449 ScopedTestingBrowserProcess browser_process; |
| 450 |
| 443 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { | 451 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input_cases); ++i) { |
| 444 Component scheme, host; | 452 Component scheme, host; |
| 445 AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input, | 453 AutocompleteInput::ParseForEmphasizeComponents(input_cases[i].input, |
| 446 string16(), | 454 string16(), |
| 447 &scheme, | 455 &scheme, |
| 448 &host); | 456 &host); |
| 449 AutocompleteInput input(input_cases[i].input, string16(), true, false, | 457 AutocompleteInput input(input_cases[i].input, string16(), true, false, |
| 450 true, false); | 458 true, false); |
| 451 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin) << "Input: " << | 459 EXPECT_EQ(input_cases[i].scheme.begin, scheme.begin) << "Input: " << |
| 452 input_cases[i].input; | 460 input_cases[i].input; |
| 453 EXPECT_EQ(input_cases[i].scheme.len, scheme.len) << "Input: " << | 461 EXPECT_EQ(input_cases[i].scheme.len, scheme.len) << "Input: " << |
| 454 input_cases[i].input; | 462 input_cases[i].input; |
| 455 EXPECT_EQ(input_cases[i].host.begin, host.begin) << "Input: " << | 463 EXPECT_EQ(input_cases[i].host.begin, host.begin) << "Input: " << |
| 456 input_cases[i].input; | 464 input_cases[i].input; |
| 457 EXPECT_EQ(input_cases[i].host.len, host.len) << "Input: " << | 465 EXPECT_EQ(input_cases[i].host.len, host.len) << "Input: " << |
| 458 input_cases[i].input; | 466 input_cases[i].input; |
| 459 } | 467 } |
| 460 } | 468 } |
| 461 | 469 |
| 462 } // namespace | 470 } // namespace |
| OLD | NEW |