Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / omnibox / omnibox_edit_unittest.cc
blob87c9738a2ddcad9185776027069d18a4926bd3ef
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 "content/public/test/test_browser_thread_bundle.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using base::ASCIIToUTF16;
17 using base::UTF8ToUTF16;
18 using content::WebContents;
20 namespace {
22 class TestingOmniboxView : public OmniboxView {
23 public:
24 explicit TestingOmniboxView(OmniboxEditController* controller)
25 : OmniboxView(NULL, controller, NULL) {}
27 // OmniboxView:
28 void SaveStateToTab(WebContents* tab) override {}
29 void OnTabChanged(const WebContents* web_contents) override {}
30 void ResetTabState(WebContents* web_contents) override {}
31 void Update() override {}
32 void OpenMatch(const AutocompleteMatch& match,
33 WindowOpenDisposition disposition,
34 const GURL& alternate_nav_url,
35 const base::string16& pasted_text,
36 size_t selected_line) override {}
37 base::string16 GetText() const override { return text_; }
38 void SetUserText(const base::string16& text,
39 const base::string16& display_text,
40 bool update_popup) override {
41 text_ = display_text;
43 void SetWindowTextAndCaretPos(const base::string16& text,
44 size_t caret_pos,
45 bool update_popup,
46 bool notify_text_changed) override {
47 text_ = text;
49 void SetForcedQuery() override {}
50 bool IsSelectAll() const override { return false; }
51 bool DeleteAtEndPressed() override { return false; }
52 void GetSelectionBounds(size_t* start, size_t* end) const override {}
53 void SelectAll(bool reversed) override {}
54 void RevertAll() override {}
55 void UpdatePopup() override {}
56 void SetFocus() override {}
57 void ApplyCaretVisibility() override {}
58 void OnTemporaryTextMaybeChanged(const base::string16& display_text,
59 bool save_original_selection,
60 bool notify_text_changed) override {
61 text_ = display_text;
63 bool OnInlineAutocompleteTextMaybeChanged(const base::string16& display_text,
64 size_t user_text_length) override {
65 const bool text_changed = text_ != display_text;
66 text_ = display_text;
67 inline_autocomplete_text_ = display_text.substr(user_text_length);
68 return text_changed;
70 void OnInlineAutocompleteTextCleared() override {
71 inline_autocomplete_text_.clear();
73 void OnRevertTemporaryText() override {}
74 void OnBeforePossibleChange() override {}
75 bool OnAfterPossibleChange() override { return false; }
76 gfx::NativeView GetNativeView() const override { return NULL; }
77 gfx::NativeView GetRelativeWindowForPopup() const override { return NULL; }
78 void SetGrayTextAutocompletion(const base::string16& input) override {}
79 base::string16 GetGrayTextAutocompletion() const override {
80 return base::string16();
82 int GetTextWidth() const override { return 0; }
83 int GetWidth() const override { return 0; }
84 bool IsImeComposing() const override { return false; }
85 int GetOmniboxTextLength() const override { return 0; }
86 void EmphasizeURLComponents() override {}
88 const base::string16& inline_autocomplete_text() const {
89 return inline_autocomplete_text_;
92 private:
93 base::string16 text_;
94 base::string16 inline_autocomplete_text_;
96 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxView);
99 class TestingOmniboxEditController : public OmniboxEditController {
100 public:
101 explicit TestingOmniboxEditController(ToolbarModel* toolbar_model)
102 : OmniboxEditController(NULL),
103 toolbar_model_(toolbar_model) {
106 protected:
107 // OmniboxEditController:
108 void Update(const content::WebContents* contents) override {}
109 void OnChanged() override {}
110 void OnSetFocus() override {}
111 void ShowURL() override {}
112 InstantController* GetInstant() override { return NULL; }
113 WebContents* GetWebContents() override { return NULL; }
114 ToolbarModel* GetToolbarModel() override { return toolbar_model_; }
115 const ToolbarModel* GetToolbarModel() const override {
116 return toolbar_model_;
119 private:
120 ToolbarModel* toolbar_model_;
122 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController);
125 } // namespace
127 class AutocompleteEditTest : public ::testing::Test {
128 public:
129 TestToolbarModel* toolbar_model() { return &toolbar_model_; }
131 private:
132 content::TestBrowserThreadBundle thread_bundle_;
133 TestToolbarModel toolbar_model_;
136 // Tests various permutations of AutocompleteModel::AdjustTextForCopy.
137 TEST_F(AutocompleteEditTest, AdjustTextForCopy) {
138 struct Data {
139 const char* perm_text;
140 const int sel_start;
141 const bool is_all_selected;
142 const char* input;
143 const char* expected_output;
144 const bool write_url;
145 const char* expected_url;
146 const bool extracted_search_terms;
147 } input[] = {
148 // Test that http:// is inserted if all text is selected.
149 { "a.de/b", 0, true, "a.de/b", "http://a.de/b", true, "http://a.de/b",
150 false },
152 // Test that http:// is inserted if the host is selected.
153 { "a.de/b", 0, false, "a.de/", "http://a.de/", true, "http://a.de/",
154 false },
156 // Tests that http:// is inserted if the path is modified.
157 { "a.de/b", 0, false, "a.de/c", "http://a.de/c", true, "http://a.de/c",
158 false },
160 // Tests that http:// isn't inserted if the host is modified.
161 { "a.de/b", 0, false, "a.com/b", "a.com/b", false, "", false },
163 // Tests that http:// isn't inserted if the start of the selection is 1.
164 { "a.de/b", 1, false, "a.de/b", "a.de/b", false, "", false },
166 // Tests that http:// isn't inserted if a portion of the host is selected.
167 { "a.de/", 0, false, "a.d", "a.d", false, "", false },
169 // Tests that http:// isn't inserted for an https url after the user nukes
170 // https.
171 { "https://a.com/", 0, false, "a.com/", "a.com/", false, "", false },
173 // Tests that http:// isn't inserted if the user adds to the host.
174 { "a.de/", 0, false, "a.de.com/", "a.de.com/", false, "", false },
176 // Tests that we don't get double http if the user manually inserts http.
177 { "a.de/", 0, false, "http://a.de/", "http://a.de/", true, "http://a.de/",
178 false },
180 // Makes sure intranet urls get 'http://' prefixed to them.
181 { "b/foo", 0, true, "b/foo", "http://b/foo", true, "http://b/foo", false },
183 // Verifies a search term 'foo' doesn't end up with http.
184 { "www.google.com/search?", 0, false, "foo", "foo", false, "", false },
186 // Makes sure extracted search terms are not modified.
187 { "www.google.com/webhp?", 0, true, "hello world", "hello world", false,
188 "", true },
190 TestingOmniboxEditController controller(toolbar_model());
191 TestingOmniboxView view(&controller);
192 TestingProfile profile;
193 // NOTE: The TemplateURLService must be created before the
194 // AutocompleteClassifier so that the SearchProvider gets a non-NULL
195 // TemplateURLService at construction time.
196 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
197 &profile, &TemplateURLServiceFactory::BuildInstanceFor);
198 AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
199 &profile, &AutocompleteClassifierFactory::BuildInstanceFor);
200 OmniboxEditModel model(&view, &controller, &profile);
202 for (size_t i = 0; i < arraysize(input); ++i) {
203 toolbar_model()->set_text(ASCIIToUTF16(input[i].perm_text));
204 model.UpdatePermanentText();
206 toolbar_model()->set_perform_search_term_replacement(
207 input[i].extracted_search_terms);
209 base::string16 result = ASCIIToUTF16(input[i].input);
210 GURL url;
211 bool write_url;
212 model.AdjustTextForCopy(input[i].sel_start, input[i].is_all_selected,
213 &result, &url, &write_url);
214 EXPECT_EQ(ASCIIToUTF16(input[i].expected_output), result) << "@: " << i;
215 EXPECT_EQ(input[i].write_url, write_url) << " @" << i;
216 if (write_url)
217 EXPECT_EQ(input[i].expected_url, url.spec()) << " @" << i;
221 TEST_F(AutocompleteEditTest, InlineAutocompleteText) {
222 TestingOmniboxEditController controller(toolbar_model());
223 TestingOmniboxView view(&controller);
224 TestingProfile profile;
225 // NOTE: The TemplateURLService must be created before the
226 // AutocompleteClassifier so that the SearchProvider gets a non-NULL
227 // TemplateURLService at construction time.
228 TemplateURLServiceFactory::GetInstance()->SetTestingFactory(
229 &profile, &TemplateURLServiceFactory::BuildInstanceFor);
230 AutocompleteClassifierFactory::GetInstance()->SetTestingFactory(
231 &profile, &AutocompleteClassifierFactory::BuildInstanceFor);
232 OmniboxEditModel model(&view, &controller, &profile);
234 // Test if the model updates the inline autocomplete text in the view.
235 EXPECT_EQ(base::string16(), view.inline_autocomplete_text());
236 model.SetUserText(UTF8ToUTF16("he"));
237 model.OnPopupDataChanged(UTF8ToUTF16("llo"), NULL, base::string16(), false);
238 EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText());
239 EXPECT_EQ(UTF8ToUTF16("llo"), view.inline_autocomplete_text());
241 model.OnAfterPossibleChange(UTF8ToUTF16("he"), UTF8ToUTF16("hel"), 3, 3,
242 false, true, false, true);
243 EXPECT_EQ(base::string16(), view.inline_autocomplete_text());
244 model.OnPopupDataChanged(UTF8ToUTF16("lo"), NULL, base::string16(), false);
245 EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText());
246 EXPECT_EQ(UTF8ToUTF16("lo"), view.inline_autocomplete_text());
248 model.Revert();
249 EXPECT_EQ(base::string16(), view.GetText());
250 EXPECT_EQ(base::string16(), view.inline_autocomplete_text());
252 model.SetUserText(UTF8ToUTF16("he"));
253 model.OnPopupDataChanged(UTF8ToUTF16("llo"), NULL, base::string16(), false);
254 EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText());
255 EXPECT_EQ(UTF8ToUTF16("llo"), view.inline_autocomplete_text());
257 model.AcceptTemporaryTextAsUserText();
258 EXPECT_EQ(UTF8ToUTF16("hello"), view.GetText());
259 EXPECT_EQ(base::string16(), view.inline_autocomplete_text());