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/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" 17 18 using content::WebContents; 19 20 namespace { 21 22 class 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 SetGrayTextAutocompletion(const string16& input) OVERRIDE {} 66 virtual string16 GetGrayTextAutocompletion() const OVERRIDE { 67 return string16(); 68 } 69 virtual int TextWidth() const OVERRIDE { return 0; } 70 virtual bool IsImeComposing() const OVERRIDE { return false; } 71 72 #if defined(TOOLKIT_VIEWS) 73 virtual int GetMaxEditWidth(int entry_width) const OVERRIDE { 74 return entry_width; 75 } 76 virtual views::View* AddToView(views::View* parent) OVERRIDE { return NULL; } 77 virtual int OnPerformDrop(const ui::DropTargetEvent& event) OVERRIDE { 78 return 0; 79 } 80 #endif 81 82 virtual int GetOmniboxTextLength() const OVERRIDE { return 0; } 83 virtual void EmphasizeURLComponents() OVERRIDE { } 84 85 private: 86 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxView); 87 }; 88 89 class TestingOmniboxEditController : public OmniboxEditController { 90 public: 91 TestingOmniboxEditController() {} 92 virtual void OnAutocompleteAccept(const GURL& url, 93 WindowOpenDisposition disposition, 94 content::PageTransition transition, 95 const GURL& alternate_nav_url) OVERRIDE {} 96 virtual void OnChanged() OVERRIDE {} 97 virtual void OnSelectionBoundsChanged() OVERRIDE {} 98 virtual void OnInputInProgress(bool in_progress) OVERRIDE {} 99 virtual void OnKillFocus() OVERRIDE {} 100 virtual void OnSetFocus() OVERRIDE {} 101 virtual gfx::Image GetFavicon() const OVERRIDE { return gfx::Image(); } 102 virtual string16 GetTitle() const OVERRIDE { return string16(); } 103 virtual InstantController* GetInstant() OVERRIDE { return NULL; } 104 virtual WebContents* GetWebContents() const OVERRIDE { 105 return NULL; 106 } 107 108 private: 109 DISALLOW_COPY_AND_ASSIGN(TestingOmniboxEditController); 110 }; 111 112 } // namespace 113 114 class AutocompleteEditTest : public ::testing::Test { 115 public: 116 TestToolbarModel* toolbar_model() { return &toolbar_model_; } 117 118 private: 119 TestToolbarModel toolbar_model_; 120 }; 121 122 // Tests various permutations of AutocompleteModel::AdjustTextForCopy. 123 TEST_F(AutocompleteEditTest, AdjustTextForCopy) { 124 struct Data { 125 const char* perm_text; 126 const int sel_start; 127 const bool is_all_selected; 128 const char* input; 129 const char* expected_output; 130 const bool write_url; 131 const char* expected_url; 132 const bool extracted_search_terms; 133 } input[] = { 134 // Test that http:// is inserted if all text is selected. 135 { "a.de/b", 0, true, "a.de/b", "http://a.de/b", true, "http://a.de/b", 136 false }, 137 138 // Test that http:// is inserted if the host is selected. 139 { "a.de/b", 0, false, "a.de/", "http://a.de/", true, "http://a.de/", 140 false }, 141 142 // Tests that http:// is inserted if the path is modified. 143 { "a.de/b", 0, false, "a.de/c", "http://a.de/c", true, "http://a.de/c", 144 false }, 145 146 // Tests that http:// isn't inserted if the host is modified. 147 { "a.de/b", 0, false, "a.com/b", "a.com/b", false, "", false }, 148 149 // Tests that http:// isn't inserted if the start of the selection is 1. 150 { "a.de/b", 1, false, "a.de/b", "a.de/b", false, "", false }, 151 152 // Tests that http:// isn't inserted if a portion of the host is selected. 153 { "a.de/", 0, false, "a.d", "a.d", false, "", false }, 154 155 // Tests that http:// isn't inserted for an https url after the user nukes 156 // https. 157 { "https://a.com/", 0, false, "a.com/", "a.com/", false, "", false }, 158 159 // Tests that http:// isn't inserted if the user adds to the host. 160 { "a.de/", 0, false, "a.de.com/", "a.de.com/", false, "", false }, 161 162 // Tests that we don't get double http if the user manually inserts http. 163 { "a.de/", 0, false, "http://a.de/", "http://a.de/", true, "http://a.de/", 164 false }, 165 166 // Makes sure intranet urls get 'http://' prefixed to them. 167 { "b/foo", 0, true, "b/foo", "http://b/foo", true, "http://b/foo", false }, 168 169 // Verifies a search term 'foo' doesn't end up with http. 170 { "www.google.com/search?", 0, false, "foo", "foo", false, "", false }, 171 172 // Makes sure extracted search terms are not modified. 173 { "www.google.com/webhp?", 0, true, "hello world", "hello world", false, 174 "", true }, 175 }; 176 TestingOmniboxView view(toolbar_model()); 177 TestingOmniboxEditController controller; 178 TestingProfile profile; 179 // NOTE: The TemplateURLService must be created before the 180 // AutocompleteClassifier so that the SearchProvider gets a non-NULL 181 // TemplateURLService at construction time. 182 TemplateURLServiceFactory::GetInstance()->SetTestingFactoryAndUse( 183 &profile, &TemplateURLServiceFactory::BuildInstanceFor); 184 AutocompleteClassifierFactory::GetInstance()->SetTestingFactoryAndUse( 185 &profile, &AutocompleteClassifierFactory::BuildInstanceFor); 186 OmniboxEditModel model(&view, &controller, &profile); 187 188 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input); ++i) { 189 model.UpdatePermanentText(ASCIIToUTF16(input[i].perm_text)); 190 191 toolbar_model()->set_replace_search_url_with_search_terms( 192 input[i].extracted_search_terms); 193 194 string16 result = ASCIIToUTF16(input[i].input); 195 GURL url; 196 bool write_url; 197 model.AdjustTextForCopy(input[i].sel_start, input[i].is_all_selected, 198 &result, &url, &write_url); 199 EXPECT_EQ(ASCIIToUTF16(input[i].expected_output), result) << "@: " << i; 200 EXPECT_EQ(input[i].write_url, write_url) << " @" << i; 201 if (write_url) 202 EXPECT_EQ(input[i].expected_url, url.spec()) << " @" << i; 203 } 204 } 205