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