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_browser_process.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15 #include "ui/gfx/font.h"
16 #include "ui/gfx/image/image.h"
18 using content::WebContents
;
22 class TestingOmniboxView
: public OmniboxView
{
24 explicit TestingOmniboxView(OmniboxEditController
* controller
)
25 : 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 size_t selected_line
) OVERRIDE
{}
34 virtual string16
GetText() const OVERRIDE
{ return string16(); }
35 virtual void SetUserText(const string16
& text
,
36 const string16
& display_text
,
37 bool update_popup
) OVERRIDE
{}
38 virtual void SetWindowTextAndCaretPos(const string16
& text
,
41 bool notify_text_changed
) OVERRIDE
{}
42 virtual void SetForcedQuery() OVERRIDE
{}
43 virtual bool IsSelectAll() const OVERRIDE
{ return false; }
44 virtual bool DeleteAtEndPressed() OVERRIDE
{ return false; }
45 virtual void GetSelectionBounds(size_t* start
, size_t* end
) const OVERRIDE
{}
46 virtual void SelectAll(bool reversed
) OVERRIDE
{}
47 virtual void RevertAll() OVERRIDE
{}
48 virtual void UpdatePopup() OVERRIDE
{}
49 virtual void SetFocus() OVERRIDE
{}
50 virtual void ApplyCaretVisibility() OVERRIDE
{}
51 virtual void OnTemporaryTextMaybeChanged(
52 const string16
& display_text
,
53 bool save_original_selection
,
54 bool notify_text_changed
) OVERRIDE
{}
55 virtual bool OnInlineAutocompleteTextMaybeChanged(
56 const string16
& display_text
, size_t user_text_length
) OVERRIDE
{
59 virtual void OnRevertTemporaryText() OVERRIDE
{}
60 virtual void OnBeforePossibleChange() OVERRIDE
{}
61 virtual bool OnAfterPossibleChange() OVERRIDE
{ return false; }
62 virtual gfx::NativeView
GetNativeView() const OVERRIDE
{ return NULL
; }
63 virtual gfx::NativeView
GetRelativeWindowForPopup() const OVERRIDE
{
66 virtual void SetGrayTextAutocompletion(const string16
& input
) OVERRIDE
{}
67 virtual string16
GetGrayTextAutocompletion() const OVERRIDE
{
70 virtual int TextWidth() const OVERRIDE
{ return 0; }
71 virtual bool IsImeComposing() const OVERRIDE
{ return false; }
73 #if defined(TOOLKIT_VIEWS)
74 virtual int GetMaxEditWidth(int entry_width
) const OVERRIDE
{
77 virtual views::View
* AddToView(views::View
* parent
) OVERRIDE
{ return NULL
; }
78 virtual int OnPerformDrop(const ui::DropTargetEvent
& event
) OVERRIDE
{
83 virtual int GetOmniboxTextLength() const OVERRIDE
{ return 0; }
84 virtual void EmphasizeURLComponents() OVERRIDE
{ }
87 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxView
);
90 class TestingOmniboxEditController
: public OmniboxEditController
{
92 explicit TestingOmniboxEditController(ToolbarModel
* toolbar_model
)
93 : toolbar_model_(toolbar_model
) {}
94 virtual void OnAutocompleteAccept(const GURL
& url
,
95 WindowOpenDisposition disposition
,
96 content::PageTransition transition
,
97 const GURL
& alternate_nav_url
) OVERRIDE
{}
98 virtual void OnChanged() OVERRIDE
{}
99 virtual void OnSelectionBoundsChanged() OVERRIDE
{}
100 virtual void OnInputInProgress(bool in_progress
) OVERRIDE
{}
101 virtual void OnKillFocus() OVERRIDE
{}
102 virtual void OnSetFocus() OVERRIDE
{}
103 virtual gfx::Image
GetFavicon() const OVERRIDE
{ return gfx::Image(); }
104 virtual string16
GetTitle() const OVERRIDE
{ return string16(); }
105 virtual InstantController
* GetInstant() OVERRIDE
{ return NULL
; }
106 virtual WebContents
* GetWebContents() OVERRIDE
{ return NULL
; }
107 virtual ToolbarModel
* GetToolbarModel() OVERRIDE
{ return toolbar_model_
; }
108 virtual const ToolbarModel
* GetToolbarModel() const OVERRIDE
{
109 return toolbar_model_
;
113 ToolbarModel
* toolbar_model_
;
115 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController
);
120 class AutocompleteEditTest
: public ::testing::Test
{
122 TestToolbarModel
* toolbar_model() { return &toolbar_model_
; }
125 TestToolbarModel toolbar_model_
;
128 // Tests various permutations of AutocompleteModel::AdjustTextForCopy.
129 TEST_F(AutocompleteEditTest
, AdjustTextForCopy
) {
131 const char* perm_text
;
133 const bool is_all_selected
;
135 const char* expected_output
;
136 const bool write_url
;
137 const char* expected_url
;
138 const bool extracted_search_terms
;
140 // Test that http:// is inserted if all text is selected.
141 { "a.de/b", 0, true, "a.de/b", "http://a.de/b", true, "http://a.de/b",
144 // Test that http:// is inserted if the host is selected.
145 { "a.de/b", 0, false, "a.de/", "http://a.de/", true, "http://a.de/",
148 // Tests that http:// is inserted if the path is modified.
149 { "a.de/b", 0, false, "a.de/c", "http://a.de/c", true, "http://a.de/c",
152 // Tests that http:// isn't inserted if the host is modified.
153 { "a.de/b", 0, false, "a.com/b", "a.com/b", false, "", false },
155 // Tests that http:// isn't inserted if the start of the selection is 1.
156 { "a.de/b", 1, false, "a.de/b", "a.de/b", false, "", false },
158 // Tests that http:// isn't inserted if a portion of the host is selected.
159 { "a.de/", 0, false, "a.d", "a.d", false, "", false },
161 // Tests that http:// isn't inserted for an https url after the user nukes
163 { "https://a.com/", 0, false, "a.com/", "a.com/", false, "", false },
165 // Tests that http:// isn't inserted if the user adds to the host.
166 { "a.de/", 0, false, "a.de.com/", "a.de.com/", false, "", false },
168 // Tests that we don't get double http if the user manually inserts http.
169 { "a.de/", 0, false, "http://a.de/", "http://a.de/", true, "http://a.de/",
172 // Makes sure intranet urls get 'http://' prefixed to them.
173 { "b/foo", 0, true, "b/foo", "http://b/foo", true, "http://b/foo", false },
175 // Verifies a search term 'foo' doesn't end up with http.
176 { "www.google.com/search?", 0, false, "foo", "foo", false, "", false },
178 // Makes sure extracted search terms are not modified.
179 { "www.google.com/webhp?", 0, true, "hello world", "hello world", false,
182 TestingOmniboxEditController
controller(toolbar_model());
183 TestingOmniboxView
view(&controller
);
184 TestingProfile profile
;
185 // NOTE: The TemplateURLService must be created before the
186 // AutocompleteClassifier so that the SearchProvider gets a non-NULL
187 // TemplateURLService at construction time.
188 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
189 &profile
, &TemplateURLServiceFactory::BuildInstanceFor
);
190 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
191 &profile
, &AutocompleteClassifierFactory::BuildInstanceFor
);
192 OmniboxEditModel
model(&view
, &controller
, &profile
);
194 for (size_t i
= 0; i
< ARRAYSIZE_UNSAFE(input
); ++i
) {
195 toolbar_model()->set_text(ASCIIToUTF16(input
[i
].perm_text
));
196 model
.UpdatePermanentText();
198 toolbar_model()->set_replace_search_url_with_search_terms(
199 input
[i
].extracted_search_terms
);
201 string16 result
= ASCIIToUTF16(input
[i
].input
);
204 model
.AdjustTextForCopy(input
[i
].sel_start
, input
[i
].is_all_selected
,
205 &result
, &url
, &write_url
);
206 EXPECT_EQ(ASCIIToUTF16(input
[i
].expected_output
), result
) << "@: " << i
;
207 EXPECT_EQ(input
[i
].write_url
, write_url
) << " @" << i
;
209 EXPECT_EQ(input
[i
].expected_url
, url
.spec()) << " @" << i
;