OLD | NEW |
(Empty) | |
| 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 |
| 11 namespace { |
| 12 |
| 13 class 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 void SelectAll(bool reversed) {} |
| 39 virtual void RevertAll() {} |
| 40 virtual void UpdatePopup() {} |
| 41 virtual void ClosePopup() {} |
| 42 virtual void SetFocus() {} |
| 43 virtual void OnTemporaryTextMaybeChanged(const std::wstring& display_text, |
| 44 bool save_original_selection) {} |
| 45 virtual bool OnInlineAutocompleteTextMaybeChanged( |
| 46 const std::wstring& display_text, size_t user_text_length) { |
| 47 return false; |
| 48 } |
| 49 virtual void OnRevertTemporaryText() {} |
| 50 virtual void OnBeforePossibleChange() {} |
| 51 virtual bool OnAfterPossibleChange() { return false; } |
| 52 virtual gfx::NativeView GetNativeView() const { return 0; } |
| 53 virtual CommandUpdater* GetCommandUpdater() { return NULL; } |
| 54 |
| 55 private: |
| 56 DISALLOW_COPY_AND_ASSIGN(TestingAutocompleteEditView); |
| 57 }; |
| 58 |
| 59 class TestingAutocompleteEditController : public AutocompleteEditController { |
| 60 public: |
| 61 TestingAutocompleteEditController() {} |
| 62 virtual void OnAutocompleteAccept(const GURL& url, |
| 63 WindowOpenDisposition disposition, |
| 64 PageTransition::Type transition, |
| 65 const GURL& alternate_nav_url) {} |
| 66 virtual void OnChanged() {} |
| 67 virtual void OnInputInProgress(bool in_progress) {} |
| 68 virtual void OnKillFocus() {} |
| 69 virtual void OnSetFocus() {} |
| 70 virtual SkBitmap GetFavIcon() const { return SkBitmap(); } |
| 71 virtual std::wstring GetTitle() const { return std::wstring(); } |
| 72 |
| 73 private: |
| 74 DISALLOW_COPY_AND_ASSIGN(TestingAutocompleteEditController); |
| 75 }; |
| 76 |
| 77 } |
| 78 |
| 79 typedef testing::Test AutocompleteEditTest; |
| 80 |
| 81 // Tests various permutations of AutocompleteModel::AdjustTextForCopy. |
| 82 TEST(AutocompleteEditTest, AdjustTextForCopy) { |
| 83 struct Data { |
| 84 const int sel_start; |
| 85 const bool is_all_selected; |
| 86 const wchar_t* input; |
| 87 const wchar_t* expected_output; |
| 88 const bool write_url; |
| 89 const char* expected_url; |
| 90 } input[] = { |
| 91 // Test that http:// is inserted if all text is selected. |
| 92 { 0, true, L"a.b/c", L"http://a.b/c", true, "http://a.b/c" }, |
| 93 |
| 94 // Test that http:// is inserted if the host is selected. |
| 95 { 0, false, L"a.b/", L"http://a.b/", true, "http://a.b/" }, |
| 96 |
| 97 // Tests that http:// is inserted if the path is modified. |
| 98 { 0, false, L"a.b/d", L"http://a.b/d", true, "http://a.b/d" }, |
| 99 |
| 100 // Tests that http:// isn't inserted if the host is modified. |
| 101 { 0, false, L"a.c/", L"a.c/", false, "" }, |
| 102 |
| 103 // Tests that http:// isn't inserted if the start of the selection is 1. |
| 104 { 1, false, L"a.b/", L"a.b/", false, "" }, |
| 105 }; |
| 106 TestingAutocompleteEditView view; |
| 107 TestingAutocompleteEditController controller; |
| 108 TestingProfile profile; |
| 109 AutocompleteEditModel model(&view, &controller, &profile); |
| 110 |
| 111 model.UpdatePermanentText(L"a.b/c"); |
| 112 |
| 113 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(input); ++i) { |
| 114 std::wstring result(input[i].input); |
| 115 GURL url; |
| 116 bool write_url; |
| 117 model.AdjustTextForCopy(input[i].sel_start, input[i].is_all_selected, |
| 118 &result, &url, &write_url); |
| 119 EXPECT_EQ(input[i].expected_output, result) << "@: " << i; |
| 120 EXPECT_EQ(input[i].write_url, write_url) << " @" << i; |
| 121 if (write_url) |
| 122 EXPECT_EQ(input[i].expected_url, url.spec()) << " @" << i; |
| 123 } |
| 124 } |
OLD | NEW |