autocomplete_edit_unittest.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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 "chrome/browser/autocomplete/autocomplete_edit.h" 6#include "chrome/browser/autocomplete/autocomplete_edit_view.h" 7#include "chrome/test/testing_profile.h" 8#include "third_party/skia/include/core/SkBitmap.h" 9#include "testing/gtest/include/gtest/gtest.h" 10 11namespace { 12 13class TestingAutocompleteEditView : public AutocompleteEditView { 14 public: 15 TestingAutocompleteEditView() {} 16 17 virtual AutocompleteEditModel* model() { return NULL; } 18 virtual const AutocompleteEditModel* model() const { return NULL; } 19 virtual void SaveStateToTab(TabContents* tab) {} 20 virtual void Update(const TabContents* tab_for_state_restoring) {} 21 virtual void OpenURL(const GURL& url, 22 WindowOpenDisposition disposition, 23 PageTransition::Type transition, 24 const GURL& alternate_nav_url, 25 size_t selected_line, 26 const std::wstring& keyword) {} 27 virtual std::wstring GetText() const { return std::wstring(); } 28 virtual bool IsEditingOrEmpty() const { return true; } 29 virtual int GetIcon() const { return 0; } 30 virtual void SetUserText(const std::wstring& text) {} 31 virtual void SetUserText(const std::wstring& text, 32 const std::wstring& display_text, 33 bool update_popup) {} 34 virtual void SetWindowTextAndCaretPos(const std::wstring& text, 35 size_t caret_pos) {} 36 virtual void SetForcedQuery() {} 37 virtual bool IsSelectAll() { return false; } 38 virtual bool DeleteAtEndPressed() { return false; } 39 virtual void GetSelectionBounds(std::wstring::size_type* start, 40 std::wstring::size_type* end) {} 41 virtual void SelectAll(bool reversed) {} 42 virtual void RevertAll() {} 43 virtual void UpdatePopup() {} 44 virtual void ClosePopup() {} 45 virtual void SetFocus() {} 46 virtual void OnTemporaryTextMaybeChanged(const std::wstring& display_text, 47 bool save_original_selection) {} 48 virtual bool OnInlineAutocompleteTextMaybeChanged( 49 const std::wstring& display_text, size_t user_text_length) { 50 return false; 51 } 52 virtual void OnRevertTemporaryText() {} 53 virtual void OnBeforePossibleChange() {} 54 virtual bool OnAfterPossibleChange() { return false; } 55 virtual gfx::NativeView GetNativeView() const { return 0; } 56 virtual CommandUpdater* GetCommandUpdater() { return NULL; } 57 58 private: 59 DISALLOW_COPY_AND_ASSIGN(TestingAutocompleteEditView); 60}; 61 62class TestingAutocompleteEditController : public AutocompleteEditController { 63 public: 64 TestingAutocompleteEditController() {} 65 virtual void OnAutocompleteWillClosePopup() {} 66 virtual void OnAutocompleteLosingFocus(gfx::NativeView view_gaining_focus) {} 67 virtual void OnAutocompleteWillAccept() {} 68 virtual bool OnCommitSuggestedText(const std::wstring& typed_text) { 69 return false; 70 } 71 virtual bool AcceptCurrentInstantPreview() { 72 return false; 73 } 74 virtual void OnSetSuggestedSearchText(const string16& suggested_text) {} 75 virtual void OnPopupBoundsChanged(const gfx::Rect& bounds) {} 76 virtual void OnAutocompleteAccept(const GURL& url, 77 WindowOpenDisposition disposition, 78 PageTransition::Type transition, 79 const GURL& alternate_nav_url) {} 80 virtual void OnChanged() {} 81 virtual void OnSelectionBoundsChanged() {} 82 virtual void OnInputInProgress(bool in_progress) {} 83 virtual void OnKillFocus() {} 84 virtual void OnSetFocus() {} 85 virtual SkBitmap GetFavIcon() const { return SkBitmap(); } 86 virtual std::wstring GetTitle() const { return std::wstring(); } 87 88 private: 89 DISALLOW_COPY_AND_ASSIGN(TestingAutocompleteEditController); 90}; 91 92} 93 94typedef testing::Test AutocompleteEditTest; 95 96// Tests various permutations of AutocompleteModel::AdjustTextForCopy. 97TEST(AutocompleteEditTest, AdjustTextForCopy) { 98 struct Data { 99 const wchar_t* perm_text; 100 const int sel_start; 101 const bool is_all_selected; 102 const wchar_t* input; 103 const wchar_t* expected_output; 104 const bool write_url; 105 const char* expected_url; 106 } input[] = { 107 // Test that http:// is inserted if all text is selected. 108 { L"a.b/c", 0, true, L"a.b/c", L"http://a.b/c", true, "http://a.b/c" }, 109 110 // Test that http:// is inserted if the host is selected. 111 { L"a.b/c", 0, false, L"a.b/", L"http://a.b/", true, "http://a.b/" }, 112 113 // Tests that http:// is inserted if the path is modified. 114 { L"a.b/c", 0, false, L"a.b/d", L"http://a.b/d", true, "http://a.b/d" }, 115 116 // Tests that http:// isn't inserted if the host is modified. 117 { L"a.b/c", 0, false, L"a.c/", L"a.c/", false, "" }, 118 119 // Tests that http:// isn't inserted if the start of the selection is 1. 120 { L"a.b/c", 1, false, L"a.b/", L"a.b/", false, "" }, 121 122 // Tests that http:// isn't inserted if a portion of the host is selected. 123 { L"a.com/", 0, false, L"a.co", L"a.co", false, "" }, 124 125 // Tests that http:// isn't inserted for an https url after the user nukes 126 // https. 127 { L"https://a.com/", 0, false, L"a.com/", L"a.com/", false, "" }, 128 129 // Tests that http:// isn't inserted if the user adds to the host. 130 { L"a.b/", 0, false, L"a.bc/", L"a.bc/", false, "" }, 131 132 // Tests that we don't get double http if the user manually inserts http. 133 { L"a.b/", 0, false, L"http://a.b/", L"http://a.b/", true, "http://a.b/" }, 134 }; 135 TestingAutocompleteEditView view; 136 TestingAutocompleteEditController controller; 137 TestingProfile profile; 138 AutocompleteEditModel model(&view, &controller, &profile); 139 140 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input); ++i) { 141 model.UpdatePermanentText(input[i].perm_text); 142 143 std::wstring result(input[i].input); 144 GURL url; 145 bool write_url; 146 model.AdjustTextForCopy(input[i].sel_start, input[i].is_all_selected, 147 &result, &url, &write_url); 148 EXPECT_EQ(input[i].expected_output, result) << "@: " << i; 149 EXPECT_EQ(input[i].write_url, write_url) << " @" << i; 150 if (write_url) 151 EXPECT_EQ(input[i].expected_url, url.spec()) << " @" << i; 152 } 153} 154