1 // Copyright (c) 2012 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.
5 #include "base/strings/utf_string_conversions.h"
6 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
7 #include "chrome/browser/search_engines/template_url_service_factory.h"
8 #include "chrome/browser/ui/omnibox/omnibox_edit_controller.h"
9 #include "chrome/browser/ui/omnibox/omnibox_edit_model.h"
10 #include "chrome/browser/ui/omnibox/omnibox_view.h"
11 #include "chrome/browser/ui/toolbar/test_toolbar_model.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using base::ASCIIToUTF16
;
16 using base::UTF8ToUTF16
;
17 using content::WebContents
;
21 class TestingOmniboxView
: public OmniboxView
{
23 explicit TestingOmniboxView(OmniboxEditController
* controller
)
24 : OmniboxView(NULL
, controller
, NULL
) {}
27 virtual void SaveStateToTab(WebContents
* tab
) OVERRIDE
{}
28 virtual void OnTabChanged(const WebContents
* web_contents
) OVERRIDE
{}
29 virtual void Update() OVERRIDE
{}
30 virtual void OpenMatch(const AutocompleteMatch
& match
,
31 WindowOpenDisposition disposition
,
32 const GURL
& alternate_nav_url
,
33 const base::string16
& pasted_text
,
34 size_t selected_line
) OVERRIDE
{}
35 virtual base::string16
GetText() const OVERRIDE
{ return text_
; }
36 virtual void SetUserText(const base::string16
& text
,
37 const base::string16
& display_text
,
38 bool update_popup
) OVERRIDE
{
41 virtual void SetWindowTextAndCaretPos(const base::string16
& text
,
44 bool notify_text_changed
) OVERRIDE
{
47 virtual void SetForcedQuery() OVERRIDE
{}
48 virtual bool IsSelectAll() const OVERRIDE
{ return false; }
49 virtual bool DeleteAtEndPressed() OVERRIDE
{ return false; }
50 virtual void GetSelectionBounds(size_t* start
, size_t* end
) const OVERRIDE
{}
51 virtual void SelectAll(bool reversed
) OVERRIDE
{}
52 virtual void RevertAll() OVERRIDE
{}
53 virtual void UpdatePopup() OVERRIDE
{}
54 virtual void SetFocus() OVERRIDE
{}
55 virtual void ApplyCaretVisibility() OVERRIDE
{}
56 virtual void OnTemporaryTextMaybeChanged(
57 const base::string16
& display_text
,
58 bool save_original_selection
,
59 bool notify_text_changed
) OVERRIDE
{
62 virtual bool OnInlineAutocompleteTextMaybeChanged(
63 const base::string16
& display_text
, size_t user_text_length
) OVERRIDE
{
64 const bool text_changed
= text_
!= display_text
;
66 inline_autocomplete_text_
= display_text
.substr(user_text_length
);
69 virtual void OnInlineAutocompleteTextCleared() OVERRIDE
{
70 inline_autocomplete_text_
.clear();
72 virtual void OnRevertTemporaryText() OVERRIDE
{}
73 virtual void OnBeforePossibleChange() OVERRIDE
{}
74 virtual bool OnAfterPossibleChange() OVERRIDE
{ return false; }
75 virtual gfx::NativeView
GetNativeView() const OVERRIDE
{ return NULL
; }
76 virtual gfx::NativeView
GetRelativeWindowForPopup() const OVERRIDE
{
79 virtual void SetGrayTextAutocompletion(
80 const base::string16
& input
) OVERRIDE
{}
81 virtual base::string16
GetGrayTextAutocompletion() const OVERRIDE
{
82 return base::string16();
84 virtual int GetTextWidth() const OVERRIDE
{ return 0; }
85 virtual int GetWidth() const OVERRIDE
{ return 0; }
86 virtual bool IsImeComposing() const OVERRIDE
{ return false; }
87 virtual int GetOmniboxTextLength() const OVERRIDE
{ return 0; }
88 virtual void EmphasizeURLComponents() OVERRIDE
{ }
90 const base::string16
& inline_autocomplete_text() const {
91 return inline_autocomplete_text_
;
96 base::string16 inline_autocomplete_text_
;
98 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxView
);
101 class TestingOmniboxEditController
: public OmniboxEditController
{
103 explicit TestingOmniboxEditController(ToolbarModel
* toolbar_model
)
104 : OmniboxEditController(NULL
),
105 toolbar_model_(toolbar_model
) {
109 // OmniboxEditController:
110 virtual void Update(const content::WebContents
* contents
) OVERRIDE
{}
111 virtual void OnChanged() OVERRIDE
{}
112 virtual void OnSetFocus() OVERRIDE
{}
113 virtual void ShowURL() OVERRIDE
{}
114 virtual void HideURL() OVERRIDE
{}
115 virtual InstantController
* GetInstant() OVERRIDE
{ return NULL
; }
116 virtual WebContents
* GetWebContents() OVERRIDE
{ return NULL
; }
117 virtual ToolbarModel
* GetToolbarModel() OVERRIDE
{ return toolbar_model_
; }
118 virtual const ToolbarModel
* GetToolbarModel() const OVERRIDE
{
119 return toolbar_model_
;
123 ToolbarModel
* toolbar_model_
;
125 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController
);
130 class AutocompleteEditTest
: public ::testing::Test
{
132 TestToolbarModel
* toolbar_model() { return &toolbar_model_
; }
135 TestToolbarModel toolbar_model_
;
138 // Tests various permutations of AutocompleteModel::AdjustTextForCopy.
139 TEST_F(AutocompleteEditTest
, AdjustTextForCopy
) {
141 const char* perm_text
;
143 const bool is_all_selected
;
145 const char* expected_output
;
146 const bool write_url
;
147 const char* expected_url
;
148 const bool extracted_search_terms
;
150 // Test that http:// is inserted if all text is selected.
151 { "a.de/b", 0, true, "a.de/b", "http://a.de/b", true, "http://a.de/b",
154 // Test that http:// is inserted if the host is selected.
155 { "a.de/b", 0, false, "a.de/", "http://a.de/", true, "http://a.de/",
158 // Tests that http:// is inserted if the path is modified.
159 { "a.de/b", 0, false, "a.de/c", "http://a.de/c", true, "http://a.de/c",
162 // Tests that http:// isn't inserted if the host is modified.
163 { "a.de/b", 0, false, "a.com/b", "a.com/b", false, "", false },
165 // Tests that http:// isn't inserted if the start of the selection is 1.
166 { "a.de/b", 1, false, "a.de/b", "a.de/b", false, "", false },
168 // Tests that http:// isn't inserted if a portion of the host is selected.
169 { "a.de/", 0, false, "a.d", "a.d", false, "", false },
171 // Tests that http:// isn't inserted for an https url after the user nukes
173 { "https://a.com/", 0, false, "a.com/", "a.com/", false, "", false },
175 // Tests that http:// isn't inserted if the user adds to the host.
176 { "a.de/", 0, false, "a.de.com/", "a.de.com/", false, "", false },
178 // Tests that we don't get double http if the user manually inserts http.
179 { "a.de/", 0, false, "http://a.de/", "http://a.de/", true, "http://a.de/",
182 // Makes sure intranet urls get 'http://' prefixed to them.
183 { "b/foo", 0, true, "b/foo", "http://b/foo", true, "http://b/foo", false },
185 // Verifies a search term 'foo' doesn't end up with http.
186 { "www.google.com/search?", 0, false, "foo", "foo", false, "", false },
188 // Makes sure extracted search terms are not modified.
189 { "www.google.com/webhp?", 0, true, "hello world", "hello world", false,
192 TestingOmniboxEditController
controller(toolbar_model());
193 TestingOmniboxView
view(&controller
);
194 TestingProfile profile
;
195 // NOTE: The TemplateURLService must be created before the
196 // AutocompleteClassifier so that the SearchProvider gets a non-NULL
197 // TemplateURLService at construction time.
198 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
199 &profile
, &TemplateURLServiceFactory::BuildInstanceFor
);
200 AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
201 &profile
, &AutocompleteClassifierFactory::BuildInstanceFor
);
202 OmniboxEditModel
model(&view
, &controller
, &profile
);
204 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(input
); ++i
) {
205 toolbar_model()->set_text(ASCIIToUTF16(input
[i
].perm_text
));
206 model
.UpdatePermanentText();
208 toolbar_model()->set_perform_search_term_replacement(
209 input
[i
].extracted_search_terms
);
211 base::string16 result
= ASCIIToUTF16(input
[i
].input
);
214 model
.AdjustTextForCopy(input
[i
].sel_start
, input
[i
].is_all_selected
,
215 &result
, &url
, &write_url
);
216 EXPECT_EQ(ASCIIToUTF16(input
[i
].expected_output
), result
) << "@: " << i
;
217 EXPECT_EQ(input
[i
].write_url
, write_url
) << " @" << i
;
219 EXPECT_EQ(input
[i
].expected_url
, url
.spec()) << " @" << i
;
223 TEST_F(AutocompleteEditTest
, InlineAutocompleteText
) {
224 TestingOmniboxEditController
controller(toolbar_model());
225 TestingOmniboxView
view(&controller
);
226 TestingProfile profile
;
227 // NOTE: The TemplateURLService must be created before the
228 // AutocompleteClassifier so that the SearchProvider gets a non-NULL
229 // TemplateURLService at construction time.
230 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
231 &profile
, &TemplateURLServiceFactory::BuildInstanceFor
);
232 AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
233 &profile
, &AutocompleteClassifierFactory::BuildInstanceFor
);
234 OmniboxEditModel
model(&view
, &controller
, &profile
);
236 // Test if the model updates the inline autocomplete text in the view.
237 EXPECT_EQ(base::string16(), view
.inline_autocomplete_text());
238 model
.SetUserText(UTF8ToUTF16("he"));
239 model
.OnPopupDataChanged(UTF8ToUTF16("llo"), NULL
, base::string16(), false);
240 EXPECT_EQ(UTF8ToUTF16("hello"), view
.GetText());
241 EXPECT_EQ(UTF8ToUTF16("llo"), view
.inline_autocomplete_text());
243 model
.OnAfterPossibleChange(UTF8ToUTF16("he"), UTF8ToUTF16("hel"), 3, 3,
244 false, true, false, true);
245 EXPECT_EQ(base::string16(), view
.inline_autocomplete_text());
246 model
.OnPopupDataChanged(UTF8ToUTF16("lo"), NULL
, base::string16(), false);
247 EXPECT_EQ(UTF8ToUTF16("hello"), view
.GetText());
248 EXPECT_EQ(UTF8ToUTF16("lo"), view
.inline_autocomplete_text());
251 EXPECT_EQ(base::string16(), view
.GetText());
252 EXPECT_EQ(base::string16(), view
.inline_autocomplete_text());
254 model
.SetUserText(UTF8ToUTF16("he"));
255 model
.OnPopupDataChanged(UTF8ToUTF16("llo"), NULL
, base::string16(), false);
256 EXPECT_EQ(UTF8ToUTF16("hello"), view
.GetText());
257 EXPECT_EQ(UTF8ToUTF16("llo"), view
.inline_autocomplete_text());
259 model
.AcceptTemporaryTextAsUserText();
260 EXPECT_EQ(UTF8ToUTF16("hello"), view
.GetText());
261 EXPECT_EQ(base::string16(), view
.inline_autocomplete_text());