omnibox_edit_unittest.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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.
4
5#include "base/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"
17
18using content::WebContents;
19
20namespace {
21
22class TestingOmniboxView : public OmniboxView {
23 public:
24  explicit TestingOmniboxView(ToolbarModel* model)
25      : OmniboxView(NULL, NULL, model, NULL) {}
26
27  virtual void SaveStateToTab(WebContents* tab) OVERRIDE {}
28  virtual void Update(const WebContents* tab_for_state_restoring) OVERRIDE {}
29  virtual void OpenMatch(const AutocompleteMatch& match,
30                         WindowOpenDisposition disposition,
31                         const GURL& alternate_nav_url,
32                         size_t selected_line) OVERRIDE {}
33  virtual string16 GetText() const OVERRIDE { return string16(); }
34  virtual void SetUserText(const string16& text,
35                           const string16& display_text,
36                           bool update_popup) OVERRIDE {}
37  virtual void SetWindowTextAndCaretPos(const string16& text,
38                                        size_t caret_pos,
39                                        bool update_popup,
40                                        bool notify_text_changed) OVERRIDE {}
41  virtual void SetForcedQuery() OVERRIDE {}
42  virtual bool IsSelectAll() const OVERRIDE { return false; }
43  virtual bool DeleteAtEndPressed() OVERRIDE { return false; }
44  virtual void GetSelectionBounds(size_t* start, size_t* end) const OVERRIDE {}
45  virtual void SelectAll(bool reversed) OVERRIDE {}
46  virtual void RevertAll() OVERRIDE {}
47  virtual void UpdatePopup() OVERRIDE {}
48  virtual void SetFocus() OVERRIDE {}
49  virtual void ApplyCaretVisibility() OVERRIDE {}
50  virtual void OnTemporaryTextMaybeChanged(
51      const string16& display_text,
52      bool save_original_selection,
53      bool notify_text_changed) OVERRIDE {}
54  virtual bool OnInlineAutocompleteTextMaybeChanged(
55      const string16& display_text, size_t user_text_length) OVERRIDE {
56    return false;
57  }
58  virtual void OnRevertTemporaryText() OVERRIDE {}
59  virtual void OnBeforePossibleChange() OVERRIDE {}
60  virtual bool OnAfterPossibleChange() OVERRIDE { return false; }
61  virtual gfx::NativeView GetNativeView() const OVERRIDE { return NULL; }
62  virtual gfx::NativeView GetRelativeWindowForPopup() const OVERRIDE {
63    return NULL;
64  }
65  virtual void SetInstantSuggestion(const string16& input) OVERRIDE {}
66  virtual string16 GetInstantSuggestion() const OVERRIDE { return string16(); }
67  virtual int TextWidth() const OVERRIDE { return 0; }
68  virtual bool IsImeComposing() const OVERRIDE { return false; }
69
70#if defined(TOOLKIT_VIEWS)
71  virtual int GetMaxEditWidth(int entry_width) const OVERRIDE {
72    return entry_width;
73  }
74  virtual views::View* AddToView(views::View* parent) OVERRIDE { return NULL; }
75  virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE {
76    return 0;
77  }
78  virtual gfx::Font GetFont() OVERRIDE { return gfx::Font(); }
79#endif
80
81  virtual int GetOmniboxTextLength() const OVERRIDE { return 0; }
82  virtual void EmphasizeURLComponents() OVERRIDE { }
83
84 private:
85  DISALLOW_COPY_AND_ASSIGN(TestingOmniboxView);
86};
87
88class TestingOmniboxEditController : public OmniboxEditController {
89 public:
90  TestingOmniboxEditController() {}
91  virtual void OnAutocompleteAccept(const GURL& url,
92                                    WindowOpenDisposition disposition,
93                                    content::PageTransition transition,
94                                    const GURL& alternate_nav_url) OVERRIDE {}
95  virtual void OnChanged() OVERRIDE {}
96  virtual void OnSelectionBoundsChanged() OVERRIDE {}
97  virtual void OnInputInProgress(bool in_progress) OVERRIDE {}
98  virtual void OnKillFocus() OVERRIDE {}
99  virtual void OnSetFocus() OVERRIDE {}
100  virtual gfx::Image GetFavicon() const OVERRIDE { return gfx::Image(); }
101  virtual string16 GetTitle() const OVERRIDE { return string16(); }
102  virtual InstantController* GetInstant() OVERRIDE { return NULL; }
103  virtual WebContents* GetWebContents() const OVERRIDE {
104    return NULL;
105  }
106
107 private:
108  DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController);
109};
110
111}  // namespace
112
113class AutocompleteEditTest : public ::testing::Test {
114 public:
115   TestToolbarModel* toolbar_model() { return &toolbar_model_; }
116
117  private:
118   TestToolbarModel toolbar_model_;
119};
120
121// Tests various permutations of AutocompleteModel::AdjustTextForCopy.
122TEST_F(AutocompleteEditTest, AdjustTextForCopy) {
123  struct Data {
124    const char* perm_text;
125    const int sel_start;
126    const bool is_all_selected;
127    const char* input;
128    const char* expected_output;
129    const bool write_url;
130    const char* expected_url;
131    const bool extracted_search_terms;
132  } input[] = {
133    // Test that http:// is inserted if all text is selected.
134    { "a.de/b", 0, true, "a.de/b", "http://a.de/b", true, "http://a.de/b",
135      false },
136
137    // Test that http:// is inserted if the host is selected.
138    { "a.de/b", 0, false, "a.de/", "http://a.de/", true, "http://a.de/",
139      false },
140
141    // Tests that http:// is inserted if the path is modified.
142    { "a.de/b", 0, false, "a.de/c", "http://a.de/c", true, "http://a.de/c",
143      false },
144
145    // Tests that http:// isn't inserted if the host is modified.
146    { "a.de/b", 0, false, "a.com/b", "a.com/b", false, "", false },
147
148    // Tests that http:// isn't inserted if the start of the selection is 1.
149    { "a.de/b", 1, false, "a.de/b", "a.de/b", false, "", false },
150
151    // Tests that http:// isn't inserted if a portion of the host is selected.
152    { "a.de/", 0, false, "a.d", "a.d", false, "", false },
153
154    // Tests that http:// isn't inserted for an https url after the user nukes
155    // https.
156    { "https://a.com/", 0, false, "a.com/", "a.com/", false, "", false },
157
158    // Tests that http:// isn't inserted if the user adds to the host.
159    { "a.de/", 0, false, "a.de.com/", "a.de.com/", false, "", false },
160
161    // Tests that we don't get double http if the user manually inserts http.
162    { "a.de/", 0, false, "http://a.de/", "http://a.de/", true, "http://a.de/",
163      false },
164
165    // Makes sure intranet urls get 'http://' prefixed to them.
166    { "b/foo", 0, true, "b/foo", "http://b/foo", true, "http://b/foo", false },
167
168    // Verifies a search term 'foo' doesn't end up with http.
169    { "www.google.com/search?", 0, false, "foo", "foo", false, "", false },
170
171    // Makes sure extracted search terms are not modified.
172    { "www.google.com/webhp?", 0, true, "hello world", "hello world", false,
173      "", true },
174  };
175  TestingOmniboxView view(toolbar_model());
176  TestingOmniboxEditController controller;
177  TestingProfile profile;
178  // NOTE: The TemplateURLService must be created before the
179  // AutocompleteClassifier so that the SearchProvider gets a non-NULL
180  // TemplateURLService at construction time.
181  TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse(
182      &profile, &TemplateURLServiceFactory::BuildInstanceFor);
183  AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse(
184      &profile, &AutocompleteClassifierFactory::BuildInstanceFor);
185  OmniboxEditModel model(&view, &controller, &profile);
186
187  for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input); ++i) {
188    model.UpdatePermanentText(ASCIIToUTF16(input[i].perm_text));
189
190    toolbar_model()->set_search_terms_type(input[i].extracted_search_terms ?
191        ToolbarModel::NORMAL_SEARCH_TERMS : ToolbarModel::NO_SEARCH_TERMS);
192
193    string16 result = ASCIIToUTF16(input[i].input);
194    GURL url;
195    bool write_url;
196    model.AdjustTextForCopy(input[i].sel_start, input[i].is_all_selected,
197                            &result, &url, &write_url);
198    EXPECT_EQ(ASCIIToUTF16(input[i].expected_output), result) << "@: " << i;
199    EXPECT_EQ(input[i].write_url, write_url) << " @" << i;
200    if (write_url)
201      EXPECT_EQ(input[i].expected_url, url.spec()) << " @" << i;
202  }
203}
204