Chromium Code Reviews| Index: chrome/browser/autocomplete/autocomplete_unittest.cc |
| =================================================================== |
| --- chrome/browser/autocomplete/autocomplete_unittest.cc (revision 116848) |
| +++ chrome/browser/autocomplete/autocomplete_unittest.cc (working copy) |
| @@ -29,8 +29,6 @@ |
| return os << static_cast<const AutocompleteMatch*>(&(*it)); |
| } |
| -namespace { |
| - |
| const size_t num_results_per_provider = 3; |
|
Peter Kasting
2012/01/11 03:00:16
Nit: Enclosing this constant in a namespace before
|
| // Autocomplete provider that provides known results. Note that this is |
| @@ -108,19 +106,32 @@ |
| class AutocompleteProviderTest : public testing::Test, |
| public content::NotificationObserver { |
| protected: |
| + struct KeywordTestData { |
| + const string16 fill_into_edit; |
| + const string16 keyword; |
| + const bool expected_keyword_result; |
| + }; |
| + |
| + protected: |
| void ResetControllerWithTestProviders(bool same_destinations); |
| // Runs a query on the input "a", and makes sure both providers' input is |
| // properly collected. |
| void RunTest(); |
| + void RunRedundantKeywordTest(const KeywordTestData* match_data, size_t size); |
| + |
| + void RunQuery(const string16 query); |
| + |
| void ResetControllerWithTestProvidersWithKeywordAndSearchProviders(); |
| + void ResetControllerWithKeywordProvider(); |
| void RunExactKeymatchTest(bool allow_exact_keyword_match); |
| // These providers are owned by the controller once it's created. |
| ACProviders providers_; |
| AutocompleteResult result_; |
| + scoped_ptr<AutocompleteController> controller_; |
| private: |
| // content::NotificationObserver |
| @@ -129,7 +140,6 @@ |
| const content::NotificationDetails& details); |
| MessageLoopForUI message_loop_; |
| - scoped_ptr<AutocompleteController> controller_; |
| content::NotificationRegistrar registrar_; |
| TestingProfile profile_; |
| }; |
| @@ -201,19 +211,84 @@ |
| search_provider->AddRef(); |
| providers_.push_back(search_provider); |
| + controller_.reset(new AutocompleteController(providers_, &profile_)); |
| +} |
| + |
| +void AutocompleteProviderTest:: |
| + ResetControllerWithKeywordProvider() { |
| + profile_.CreateTemplateURLService(); |
| + |
| + TemplateURLService* turl_model = |
| + TemplateURLServiceFactory::GetForProfile(&profile_); |
| + |
| + // Create a TemplateURL for KeywordProvider. |
| + TemplateURL* keyword_t_url = new TemplateURL(); |
| + keyword_t_url->set_short_name(ASCIIToUTF16("foo.com")); |
| + keyword_t_url->set_keyword(ASCIIToUTF16("foo.com")); |
| + keyword_t_url->SetURL("http://foo.com/{searchTerms}", 0, 0); |
| + turl_model->Add(keyword_t_url); |
| + ASSERT_NE(0, keyword_t_url->id()); |
| + |
| + // Create another TemplateURL for KeywordProvider. |
| + keyword_t_url = new TemplateURL(); |
| + keyword_t_url->set_short_name(ASCIIToUTF16("bar.com")); |
| + keyword_t_url->set_keyword(ASCIIToUTF16("bar.com")); |
| + keyword_t_url->SetURL("http://bar.com/{searchTerms}", 0, 0); |
| + turl_model->Add(keyword_t_url); |
| + ASSERT_NE(0, keyword_t_url->id()); |
| + |
| + // Forget about any existing providers. The controller owns them and will |
| + // Release() them below, when we delete it during the call to reset(). |
| + providers_.clear(); |
| + |
| + // Create both a keyword and search provider, and add them in that order. |
| + // (Order is important; see comments in RunExactKeymatchTest().) |
| + KeywordProvider* keyword_provider = new KeywordProvider(NULL, |
| + &profile_); |
| + keyword_provider->AddRef(); |
| + providers_.push_back(keyword_provider); |
| + |
| AutocompleteController* controller = |
| new AutocompleteController(providers_, &profile_); |
| + controller->set_keyword_provider(keyword_provider); |
| controller_.reset(controller); |
| } |
| void AutocompleteProviderTest::RunTest() { |
| + RunQuery(ASCIIToUTF16("a")); |
| +} |
| + |
| +void AutocompleteProviderTest::RunRedundantKeywordTest( |
| + const KeywordTestData* match_data, |
| + size_t size) { |
| + ACMatches matches; |
| + for (size_t i = 0; i < size; ++i) { |
| + AutocompleteMatch match; |
| + match.fill_into_edit = match_data[i].fill_into_edit; |
| + match.keyword = match_data[i].keyword; |
| + matches.push_back(match); |
| + } |
| + |
| + AutocompleteResult result; |
| + result.AppendMatches(matches); |
| + controller_->UpdateAssociatedKeywords(&result); |
| + |
| + for (size_t j = 0; j < result.size(); ++j) { |
| + EXPECT_EQ(match_data[j].expected_keyword_result, |
| + result.match_at(j).associated_keyword.get() != NULL); |
| + } |
| +} |
| + |
| + |
| +void AutocompleteProviderTest::RunQuery(const string16 query) { |
| result_.Reset(); |
| - controller_->Start(ASCIIToUTF16("a"), string16(), true, false, true, |
| - AutocompleteInput::ALL_MATCHES); |
| + controller_->Start(query, string16(), true, false, true, |
| + AutocompleteInput::ALL_MATCHES); |
| - // The message loop will terminate when all autocomplete input has been |
| - // collected. |
| - MessageLoop::current()->Run(); |
| + if (!controller_->done()) |
| + // The message loop will terminate when all autocomplete input has been |
| + // collected. |
| + MessageLoop::current()->Run(); |
| } |
| void AutocompleteProviderTest::RunExactKeymatchTest( |
| @@ -273,6 +348,48 @@ |
| RunExactKeymatchTest(false); |
| } |
| +// Test that redundant associated keywords are removed. |
| +TEST_F(AutocompleteProviderTest, RedundantKeywordsIgnoredInResult) { |
| + ResetControllerWithKeywordProvider(); |
| + |
| + // Get the controller's internal members in the correct state. |
| + RunQuery(ASCIIToUTF16("fo")); |
| + |
| + { |
| + KeywordTestData duplicate_url[] = { |
| + { ASCIIToUTF16("fo"), string16(), false }, |
| + { ASCIIToUTF16("foo.com"), string16(), true }, |
| + { ASCIIToUTF16("foo.com"), string16(), false } |
| + }; |
| + |
| + SCOPED_TRACE("Duplicate url"); |
| + RunRedundantKeywordTest(duplicate_url, ARRAYSIZE_UNSAFE(duplicate_url)); |
| + } |
| + |
| + { |
| + KeywordTestData keyword_match[] = { |
| + { ASCIIToUTF16("foo.com"), ASCIIToUTF16("foo.com"), false }, |
| + { ASCIIToUTF16("foo.com"), string16(), false } |
| + }; |
| + |
| + SCOPED_TRACE("Duplicate url with keyword match"); |
| + RunRedundantKeywordTest(keyword_match, ARRAYSIZE_UNSAFE(keyword_match)); |
| + } |
| + |
| + { |
| + KeywordTestData multiple_keyword[] = { |
| + { ASCIIToUTF16("fo"), string16(), false }, |
| + { ASCIIToUTF16("foo.com"), string16(), true }, |
| + { ASCIIToUTF16("foo.com"), string16(), false }, |
| + { ASCIIToUTF16("bar.com"), string16(), true }, |
| + }; |
| + |
| + SCOPED_TRACE("Duplicate url with multiple keywords"); |
| + RunRedundantKeywordTest(multiple_keyword, |
| + ARRAYSIZE_UNSAFE(multiple_keyword)); |
| + } |
| +} |
| + |
| typedef testing::Test AutocompleteTest; |
| TEST_F(AutocompleteTest, InputType) { |
| @@ -484,5 +601,3 @@ |
| EXPECT_EQ(input_cases[i].host.len, host.len); |
| } |
| } |
| - |
| -} // namespace |