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 // See description above class for what this registers. | |
Peter Kasting
2011/09/14 17:49:37
Nit: This comment makes no sense, remove.
keishi
2011/09/15 02:23:13
Done.
| |
21 virtual void SetUp(); | |
22 | |
Peter Kasting
2011/09/14 17:49:37
Nit: Unnecessary blank line
keishi
2011/09/15 02:23:13
Done.
| |
23 virtual void TearDown(); | |
24 | |
25 protected: | |
26 AutocompleteMatch CreateMatch(const string16& keyword, | |
27 const string16& query_string, | |
28 AutocompleteMatch::Type type); | |
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(NULL); | |
Peter Kasting
2011/09/14 17:49:37
Nit: Don't pass an argument in any of these.
keishi
2011/09/15 02:23:13
Done.
| |
65 model_.reset(NULL); | |
66 edit_model_.reset(NULL); | |
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.assign(query_string); | |
Peter Kasting
2011/09/14 17:49:37
Nit: Use = instead of assign()
keishi
2011/09/15 02:23:13
Done.
| |
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) { | |
Peter Kasting
2011/09/14 17:49:37
Nit: No {}
keishi
2011/09/15 02:23:13
Done.
| |
84 match.fill_into_edit.append(match.template_url->keyword() + char16(' ')); | |
Peter Kasting
2011/09/14 17:49:37
Nit: Use = instead of append()
keishi
2011/09/15 02:23:13
Done.
| |
85 } else { | |
86 match.template_url = template_url_service->GetDefaultSearchProvider(); | |
87 } | |
88 match.fill_into_edit.append(query_string); | |
89 match.transition = keyword.empty() ? PageTransition::GENERATED : | |
Peter Kasting
2011/09/14 17:49:37
Nit: Wrap after ? instead of :
keishi
2011/09/15 02:23:13
Done.
| |
90 PageTransition::KEYWORD; | |
91 return match; | |
92 } | |
93 | |
94 TEST_F(AutoCompletePopupModelTest, GetKeywordForMatch) { | |
95 string16 keyword; | |
96 | |
97 // Possible matches when the input is "tfoo" | |
98 EXPECT_FALSE(model_->GetKeywordForMatch( | |
Peter Kasting
2011/09/14 17:49:37
Nit: Write a helper function:
void AutoCompletePo
keishi
2011/09/15 02:23:13
Done.
| |
99 CreateMatch(string16(), ASCIIToUTF16("tfoo"), | |
100 AutocompleteMatch::SEARCH_WHAT_YOU_TYPED), | |
101 &keyword)); | |
102 EXPECT_EQ(string16(), keyword); | |
103 EXPECT_FALSE(model_->GetKeywordForMatch( | |
104 CreateMatch(string16(), ASCIIToUTF16("tfoo"), | |
105 AutocompleteMatch::SEARCH_HISTORY), | |
106 &keyword)); | |
107 EXPECT_EQ(string16(), keyword); | |
108 EXPECT_FALSE(model_->GetKeywordForMatch( | |
109 CreateMatch(string16(), ASCIIToUTF16("tfoo"), | |
110 AutocompleteMatch::SEARCH_SUGGEST), | |
111 &keyword)); | |
112 EXPECT_EQ(string16(), keyword); | |
113 | |
114 // Possible matches when the input is "t foo" | |
115 EXPECT_FALSE(model_->GetKeywordForMatch( | |
116 CreateMatch(ASCIIToUTF16("t"), ASCIIToUTF16("foo"), | |
117 AutocompleteMatch::SEARCH_HISTORY), | |
118 &keyword)); | |
119 EXPECT_EQ(ASCIIToUTF16("t"), keyword); | |
120 EXPECT_FALSE(model_->GetKeywordForMatch( | |
121 CreateMatch(ASCIIToUTF16("t"), ASCIIToUTF16("foo"), | |
122 AutocompleteMatch::SEARCH_OTHER_ENGINE), | |
123 &keyword)); | |
124 EXPECT_EQ(ASCIIToUTF16("t"), keyword); | |
125 | |
126 // Possible matches when the input is "k foo" | |
127 EXPECT_FALSE(model_->GetKeywordForMatch( | |
128 CreateMatch(ASCIIToUTF16("k"), ASCIIToUTF16("foo"), | |
129 AutocompleteMatch::SEARCH_HISTORY), | |
130 &keyword)); | |
131 EXPECT_EQ(ASCIIToUTF16("k"), keyword); | |
132 EXPECT_FALSE(model_->GetKeywordForMatch( | |
133 CreateMatch(ASCIIToUTF16("k"), ASCIIToUTF16("foo"), | |
134 AutocompleteMatch::SEARCH_OTHER_ENGINE), | |
135 &keyword)); | |
136 EXPECT_EQ(ASCIIToUTF16("k"), keyword); | |
137 } | |
OLD | NEW |