OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 9 #include "chrome/browser/search_engines/template_url.h" |
| 10 #include "chrome/browser/search_engines/template_url_service.h" |
| 11 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 12 #include "chrome/test/base/testing_profile.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 |
| 15 class AutoCompletePopupModelTest : public testing::Test { |
| 16 public: |
| 17 AutoCompletePopupModelTest() { |
| 18 } |
| 19 |
| 20 virtual void SetUp(); |
| 21 virtual void TearDown(); |
| 22 |
| 23 protected: |
| 24 AutocompleteMatch CreateMatch(const string16& keyword, |
| 25 const string16& query_string, |
| 26 AutocompleteMatch::Type type); |
| 27 void RunTest(const char* input, AutocompleteMatch::Type type, |
| 28 const char* keyword); |
| 29 |
| 30 scoped_ptr<TestingProfile> profile_; |
| 31 scoped_ptr<AutocompletePopupModel> model_; |
| 32 scoped_ptr<AutocompleteEditModel> edit_model_; |
| 33 }; |
| 34 |
| 35 void AutoCompletePopupModelTest::SetUp() { |
| 36 profile_.reset(new TestingProfile()); |
| 37 profile_->CreateTemplateURLService(); |
| 38 edit_model_.reset(new AutocompleteEditModel(NULL, NULL, profile_.get())); |
| 39 model_.reset(new AutocompletePopupModel(NULL, edit_model_.get())); |
| 40 |
| 41 TemplateURLService* turl_model = |
| 42 TemplateURLServiceFactory::GetForProfile(profile_.get()); |
| 43 |
| 44 turl_model->Load(); |
| 45 |
| 46 // Reset the default TemplateURL. |
| 47 TemplateURL* default_t_url = new TemplateURL(); |
| 48 default_t_url->set_keyword(ASCIIToUTF16("t")); |
| 49 default_t_url->SetURL("http://defaultturl/{searchTerms}", 0, 0); |
| 50 turl_model->Add(default_t_url); |
| 51 turl_model->SetDefaultSearchProvider(default_t_url); |
| 52 ASSERT_NE(0, default_t_url->id()); |
| 53 |
| 54 // Create another TemplateURL for KeywordProvider. |
| 55 TemplateURL* keyword_t_url = new TemplateURL(); |
| 56 keyword_t_url->set_short_name(ASCIIToUTF16("k")); |
| 57 keyword_t_url->set_keyword(ASCIIToUTF16("k")); |
| 58 keyword_t_url->SetURL("http://keyword/{searchTerms}", 0, 0); |
| 59 turl_model->Add(keyword_t_url); |
| 60 ASSERT_NE(0, keyword_t_url->id()); |
| 61 } |
| 62 |
| 63 void AutoCompletePopupModelTest::TearDown() { |
| 64 profile_.reset(); |
| 65 model_.reset(); |
| 66 edit_model_.reset(); |
| 67 } |
| 68 |
| 69 AutocompleteMatch AutoCompletePopupModelTest::CreateMatch( |
| 70 const string16& keyword, |
| 71 const string16& query_string, |
| 72 AutocompleteMatch::Type type) { |
| 73 AutocompleteMatch match(NULL, 0, false, type); |
| 74 match.contents = query_string; |
| 75 TemplateURLService* template_url_service = |
| 76 TemplateURLServiceFactory::GetForProfile(profile_.get()); |
| 77 if (!keyword.empty()) { |
| 78 const TemplateURL* template_url = |
| 79 template_url_service->GetTemplateURLForKeyword(keyword); |
| 80 match.template_url = |
| 81 TemplateURL::SupportsReplacement(template_url) ? template_url : NULL; |
| 82 } |
| 83 if (match.template_url) |
| 84 match.fill_into_edit = match.template_url->keyword() + char16(' '); |
| 85 else |
| 86 match.template_url = template_url_service->GetDefaultSearchProvider(); |
| 87 match.fill_into_edit.append(query_string); |
| 88 match.transition = keyword.empty() ? |
| 89 PageTransition::GENERATED : PageTransition::KEYWORD; |
| 90 return match; |
| 91 } |
| 92 |
| 93 void AutoCompletePopupModelTest::RunTest(const char* input, |
| 94 AutocompleteMatch::Type type, |
| 95 const char* keyword) { |
| 96 string16 keyword16(ASCIIToUTF16(keyword)); |
| 97 string16 detected_keyword; |
| 98 EXPECT_FALSE(model_->GetKeywordForMatch( |
| 99 CreateMatch(keyword16, ASCIIToUTF16(input), type), &detected_keyword)); |
| 100 EXPECT_EQ(keyword16, detected_keyword); |
| 101 } |
| 102 |
| 103 TEST_F(AutoCompletePopupModelTest, GetKeywordForMatch) { |
| 104 string16 keyword; |
| 105 |
| 106 // Possible matches when the input is "tfoo" |
| 107 RunTest("tfoo", AutocompleteMatch::SEARCH_WHAT_YOU_TYPED, ""); |
| 108 RunTest("tfoo", AutocompleteMatch::SEARCH_HISTORY, ""); |
| 109 RunTest("tfoo", AutocompleteMatch::SEARCH_SUGGEST, ""); |
| 110 |
| 111 // Possible matches when the input is "t foo" |
| 112 RunTest("foo", AutocompleteMatch::SEARCH_HISTORY, "t"); |
| 113 RunTest("foo", AutocompleteMatch::SEARCH_OTHER_ENGINE, "t"); |
| 114 |
| 115 // Possible matches when the input is "k foo" |
| 116 RunTest("foo", AutocompleteMatch::SEARCH_HISTORY, "k"); |
| 117 RunTest("foo", AutocompleteMatch::SEARCH_OTHER_ENGINE, "k"); |
| 118 } |
OLD | NEW |