OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "chrome/browser/autofill/test_autofill_external_delegate.h" | 6 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
7 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" | 7 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
11 #include "ui/gfx/rect.h" | 11 #include "ui/gfx/rect.h" |
12 | 12 |
13 using ::testing::_; | 13 using ::testing::_; |
14 using ::testing::AtLeast; | 14 using ::testing::AtLeast; |
15 using WebKit::WebAutofillClient; | 15 using WebKit::WebAutofillClient; |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 class MockAutofillExternalDelegate : | 19 class MockAutofillExternalDelegate : |
20 public autofill::TestAutofillExternalDelegate { | 20 public autofill::TestAutofillExternalDelegate { |
21 public: | 21 public: |
22 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; | 22 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; |
23 virtual ~MockAutofillExternalDelegate() {}; | 23 virtual ~MockAutofillExternalDelegate() {}; |
24 | 24 |
25 virtual void SelectAutofillSuggestionAtIndex(int unique_id) | 25 virtual void SelectAutofillSuggestion(int unique_id) OVERRIDE {} |
26 OVERRIDE {} | |
27 virtual void RemoveAutocompleteEntry(const string16& value) OVERRIDE {} | 26 virtual void RemoveAutocompleteEntry(const string16& value) OVERRIDE {} |
28 virtual void RemoveAutofillProfileOrCreditCard(int unique_id) OVERRIDE {} | 27 virtual void RemoveAutofillProfileOrCreditCard(int unique_id) OVERRIDE {} |
29 virtual void ClearPreviewedForm() OVERRIDE {} | 28 virtual void ClearPreviewedForm() OVERRIDE {} |
30 | 29 |
31 MOCK_METHOD0(ControllerDestroyed, void()); | 30 MOCK_METHOD0(ControllerDestroyed, void()); |
32 }; | 31 }; |
33 | 32 |
34 class TestAutofillPopupController : public AutofillPopupControllerImpl { | 33 class TestAutofillPopupController : public AutofillPopupControllerImpl { |
35 public: | 34 public: |
36 explicit TestAutofillPopupController( | 35 explicit TestAutofillPopupController( |
37 AutofillExternalDelegate* external_delegate) | 36 AutofillExternalDelegate* external_delegate) |
38 : AutofillPopupControllerImpl(external_delegate, NULL, gfx::Rect()) {} | 37 : AutofillPopupControllerImpl(external_delegate, NULL, gfx::Rect()) {} |
39 virtual ~TestAutofillPopupController() {} | 38 virtual ~TestAutofillPopupController() {} |
40 | 39 |
41 // Making protected functions public for testing | 40 // Making protected functions public for testing |
42 const std::vector<string16>& autofill_values() const { | 41 const std::vector<string16>& labels() const { |
43 return AutofillPopupControllerImpl::autofill_values(); | 42 return AutofillPopupControllerImpl::labels(); |
44 } | 43 } |
45 int selected_line() const { | 44 int selected_line() const { |
46 return AutofillPopupControllerImpl::selected_line(); | 45 return AutofillPopupControllerImpl::selected_line(); |
47 } | 46 } |
48 void SetSelectedLine(size_t selected_line) { | 47 void SetSelectedLine(size_t selected_line) { |
49 AutofillPopupControllerImpl::SetSelectedLine(selected_line); | 48 AutofillPopupControllerImpl::SetSelectedLine(selected_line); |
50 } | 49 } |
51 void SelectNextLine() { | 50 void SelectNextLine() { |
52 AutofillPopupControllerImpl::SelectNextLine(); | 51 AutofillPopupControllerImpl::SelectNextLine(); |
53 } | 52 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
100 EXPECT_CALL(*autofill_popup_controller_, | 99 EXPECT_CALL(*autofill_popup_controller_, |
101 UpdateBoundsAndRedrawPopup()); | 100 UpdateBoundsAndRedrawPopup()); |
102 | 101 |
103 popup_controller()->SetPopupBounds(popup_bounds); | 102 popup_controller()->SetPopupBounds(popup_bounds); |
104 | 103 |
105 EXPECT_EQ(popup_bounds, popup_controller()->popup_bounds()); | 104 EXPECT_EQ(popup_bounds, popup_controller()->popup_bounds()); |
106 } | 105 } |
107 | 106 |
108 TEST_F(AutofillPopupControllerUnitTest, ChangeSelectedLine) { | 107 TEST_F(AutofillPopupControllerUnitTest, ChangeSelectedLine) { |
109 // Set up the popup. | 108 // Set up the popup. |
110 std::vector<string16> autofill_values(2, string16()); | 109 std::vector<string16> labels(2, string16()); |
111 std::vector<int> autofill_ids(2, 0); | 110 std::vector<int> autofill_ids(2, 0); |
112 autofill_popup_controller_->Show( | 111 autofill_popup_controller_->Show( |
113 autofill_values, autofill_values, autofill_values, autofill_ids); | 112 labels, labels, labels, autofill_ids); |
Ilya Sherman
2012/12/20 04:34:52
nit: This now fits on the previous line.
Evan Stade
2012/12/20 20:01:25
Done.
| |
114 | 113 |
115 EXPECT_LT(autofill_popup_controller_->selected_line(), 0); | 114 EXPECT_LT(autofill_popup_controller_->selected_line(), 0); |
116 // Check that there are at least 2 values so that the first and last selection | 115 // Check that there are at least 2 values so that the first and last selection |
117 // are different. | 116 // are different. |
118 EXPECT_GE(2, | 117 EXPECT_GE(2, |
119 static_cast<int>(autofill_popup_controller_->autofill_values().size())); | 118 static_cast<int>(autofill_popup_controller_->labels().size())); |
120 | 119 |
121 // Test wrapping before the front. | 120 // Test wrapping before the front. |
122 autofill_popup_controller_->SelectPreviousLine(); | 121 autofill_popup_controller_->SelectPreviousLine(); |
123 EXPECT_EQ(static_cast<int>( | 122 EXPECT_EQ(static_cast<int>( |
124 autofill_popup_controller_->autofill_values().size() - 1), | 123 autofill_popup_controller_->labels().size() - 1), |
125 autofill_popup_controller_->selected_line()); | 124 autofill_popup_controller_->selected_line()); |
126 | 125 |
127 // Test wrapping after the end. | 126 // Test wrapping after the end. |
128 autofill_popup_controller_->SelectNextLine(); | 127 autofill_popup_controller_->SelectNextLine(); |
129 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); | 128 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); |
130 } | 129 } |
131 | 130 |
132 TEST_F(AutofillPopupControllerUnitTest, RedrawSelectedLine) { | 131 TEST_F(AutofillPopupControllerUnitTest, RedrawSelectedLine) { |
133 // Set up the popup. | 132 // Set up the popup. |
134 std::vector<string16> autofill_values(2, string16()); | 133 std::vector<string16> labels(2, string16()); |
135 std::vector<int> autofill_ids(2, 0); | 134 std::vector<int> autofill_ids(2, 0); |
136 autofill_popup_controller_->Show( | 135 autofill_popup_controller_->Show( |
137 autofill_values, autofill_values, autofill_values, | 136 labels, labels, labels, |
138 autofill_ids); | 137 autofill_ids); |
Ilya Sherman
2012/12/20 04:34:52
nit: This now fits on the previous line.
Evan Stade
2012/12/20 20:01:25
Done.
| |
139 | 138 |
140 // Make sure that when a new line is selected, it is invalidated so it can | 139 // Make sure that when a new line is selected, it is invalidated so it can |
141 // be updated to show it is selected. | 140 // be updated to show it is selected. |
142 int selected_line = 0; | 141 int selected_line = 0; |
143 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); | 142 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); |
144 autofill_popup_controller_->SetSelectedLine(selected_line); | 143 autofill_popup_controller_->SetSelectedLine(selected_line); |
145 | 144 |
146 // Ensure that the row isn't invalidated if it didn't change. | 145 // Ensure that the row isn't invalidated if it didn't change. |
147 EXPECT_CALL(*autofill_popup_controller_, | 146 EXPECT_CALL(*autofill_popup_controller_, |
148 InvalidateRow(selected_line)).Times(0); | 147 InvalidateRow(selected_line)).Times(0); |
149 autofill_popup_controller_->SetSelectedLine(selected_line); | 148 autofill_popup_controller_->SetSelectedLine(selected_line); |
150 | 149 |
151 // Change back to no selection. | 150 // Change back to no selection. |
152 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); | 151 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); |
153 autofill_popup_controller_->SetSelectedLine(-1); | 152 autofill_popup_controller_->SetSelectedLine(-1); |
154 } | 153 } |
155 | 154 |
156 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) { | 155 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) { |
157 // Set up the popup. | 156 // Set up the popup. |
158 std::vector<string16> autofill_values(3, string16()); | 157 std::vector<string16> labels(3, string16()); |
159 std::vector<int> autofill_ids; | 158 std::vector<int> autofill_ids; |
160 autofill_ids.push_back(1); | 159 autofill_ids.push_back(1); |
161 autofill_ids.push_back(1); | 160 autofill_ids.push_back(1); |
162 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); | 161 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); |
163 autofill_popup_controller_->Show( | 162 autofill_popup_controller_->Show( |
164 autofill_values, autofill_values, autofill_values, autofill_ids); | 163 labels, labels, labels, autofill_ids); |
Ilya Sherman
2012/12/20 04:34:52
nit: This now fits on the previous line.
Evan Stade
2012/12/20 20:01:25
Done.
| |
165 | 164 |
166 // Generate a popup, so it can be hidden later. It doesn't matter what the | 165 // Generate a popup, so it can be hidden later. It doesn't matter what the |
167 // external_delegate thinks is being shown in the process, since we are just | 166 // external_delegate thinks is being shown in the process, since we are just |
168 // testing the popup here. | 167 // testing the popup here. |
169 autofill::GenerateTestAutofillPopup(&external_delegate_); | 168 autofill::GenerateTestAutofillPopup(&external_delegate_); |
170 | 169 |
171 // No line is selected so the removal should fail. | 170 // No line is selected so the removal should fail. |
172 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); | 171 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); |
173 | 172 |
174 // Try to remove the last entry and ensure it fails (it is an option). | 173 // Try to remove the last entry and ensure it fails (it is an option). |
175 autofill_popup_controller_->SetSelectedLine( | 174 autofill_popup_controller_->SetSelectedLine( |
176 autofill_popup_controller_->autofill_values().size() - 1); | 175 autofill_popup_controller_->labels().size() - 1); |
177 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); | 176 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); |
178 EXPECT_LE(0, autofill_popup_controller_->selected_line()); | 177 EXPECT_LE(0, autofill_popup_controller_->selected_line()); |
179 | 178 |
180 // Remove the first entry. The popup should be redrawn since its size has | 179 // Remove the first entry. The popup should be redrawn since its size has |
181 // changed. | 180 // changed. |
182 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); | 181 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); |
183 autofill_popup_controller_->SetSelectedLine(0); | 182 autofill_popup_controller_->SetSelectedLine(0); |
184 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 183 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); |
185 | 184 |
186 // Remove the last entry. The popup should then be hidden since there are | 185 // Remove the last entry. The popup should then be hidden since there are |
187 // no Autofill entries left. | 186 // no Autofill entries left. |
188 EXPECT_CALL(external_delegate_, ControllerDestroyed()); | 187 EXPECT_CALL(external_delegate_, ControllerDestroyed()); |
189 | 188 |
190 autofill_popup_controller_->SetSelectedLine(0); | 189 autofill_popup_controller_->SetSelectedLine(0); |
191 // The controller self-deletes here, don't double delete. | 190 // The controller self-deletes here, don't double delete. |
192 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 191 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); |
193 autofill_popup_controller_ = NULL; | 192 autofill_popup_controller_ = NULL; |
194 } | 193 } |
195 | 194 |
196 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { | 195 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { |
197 // Set up the popup. | 196 // Set up the popup. |
198 std::vector<string16> autofill_values(3, string16()); | 197 std::vector<string16> labels(3, string16()); |
199 std::vector<int> autofill_ids; | 198 std::vector<int> autofill_ids; |
200 autofill_ids.push_back(1); | 199 autofill_ids.push_back(1); |
201 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator); | 200 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator); |
202 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); | 201 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); |
203 autofill_popup_controller_->Show( | 202 autofill_popup_controller_->Show( |
204 autofill_values, autofill_values, autofill_values, autofill_ids); | 203 labels, labels, labels, autofill_ids); |
Ilya Sherman
2012/12/20 04:34:52
nit: This now fits on the previous line.
Evan Stade
2012/12/20 20:01:25
Done.
| |
205 | 204 |
206 autofill_popup_controller_->SetSelectedLine(0); | 205 autofill_popup_controller_->SetSelectedLine(0); |
207 | 206 |
208 // Make sure next skips the unselectable separator. | 207 // Make sure next skips the unselectable separator. |
209 autofill_popup_controller_->SelectNextLine(); | 208 autofill_popup_controller_->SelectNextLine(); |
210 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); | 209 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); |
211 | 210 |
212 // Make sure previous skips the unselectable separator. | 211 // Make sure previous skips the unselectable separator. |
213 autofill_popup_controller_->SelectPreviousLine(); | 212 autofill_popup_controller_->SelectPreviousLine(); |
214 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); | 213 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 NULL, | 256 NULL, |
258 bounds); | 257 bounds); |
259 EXPECT_EQ( | 258 EXPECT_EQ( |
260 bounds, | 259 bounds, |
261 static_cast<AutofillPopupController*>(controller3)->element_bounds()); | 260 static_cast<AutofillPopupController*>(controller3)->element_bounds()); |
262 controller3->Hide(); | 261 controller3->Hide(); |
263 | 262 |
264 EXPECT_CALL(delegate, ControllerDestroyed()); | 263 EXPECT_CALL(delegate, ControllerDestroyed()); |
265 delete test_controller; | 264 delete test_controller; |
266 } | 265 } |
OLD | NEW |