| 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 "base/memory/weak_ptr.h" | 6 #include "base/memory/weak_ptr.h" |
| 7 #include "base/prefs/testing_pref_service.h" | 7 #include "base/prefs/testing_pref_service.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" | 9 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
| 10 #include "chrome/browser/ui/autofill/autofill_popup_view.h" | 10 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 class MockAutofillExternalDelegate : public AutofillExternalDelegate { | 33 class MockAutofillExternalDelegate : public AutofillExternalDelegate { |
| 34 public: | 34 public: |
| 35 MockAutofillExternalDelegate(content::WebContents* web_contents, | 35 MockAutofillExternalDelegate(content::WebContents* web_contents, |
| 36 AutofillManager* autofill_manager, | 36 AutofillManager* autofill_manager, |
| 37 AutofillDriver* autofill_driver) | 37 AutofillDriver* autofill_driver) |
| 38 : AutofillExternalDelegate(web_contents, autofill_manager, | 38 : AutofillExternalDelegate(web_contents, autofill_manager, |
| 39 autofill_driver) {} | 39 autofill_driver) {} |
| 40 virtual ~MockAutofillExternalDelegate() {} | 40 virtual ~MockAutofillExternalDelegate() {} |
| 41 | 41 |
| 42 virtual void DidSelectSuggestion(int identifier) OVERRIDE {} | 42 virtual void DidPreselectSuggestion(int identifier) OVERRIDE {} |
| 43 virtual void RemoveSuggestion(const string16& value, int identifier) OVERRIDE | 43 virtual void RemoveSuggestion(const string16& value, int identifier) OVERRIDE |
| 44 {} | 44 {} |
| 45 virtual void ClearPreviewedForm() OVERRIDE {} | 45 virtual void ClearPreviewedForm() OVERRIDE {} |
| 46 base::WeakPtr<AutofillExternalDelegate> GetWeakPtr() { | 46 base::WeakPtr<AutofillExternalDelegate> GetWeakPtr() { |
| 47 return AutofillExternalDelegate::GetWeakPtr(); | 47 return AutofillExternalDelegate::GetWeakPtr(); |
| 48 } | 48 } |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 class MockAutofillManagerDelegate | 51 class MockAutofillManagerDelegate |
| 52 : public autofill::TestAutofillManagerDelegate { | 52 : public autofill::TestAutofillManagerDelegate { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 78 virtual gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const | 78 virtual gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const |
| 79 OVERRIDE { | 79 OVERRIDE { |
| 80 return display_; | 80 return display_; |
| 81 } | 81 } |
| 82 | 82 |
| 83 // Making protected functions public for testing | 83 // Making protected functions public for testing |
| 84 using AutofillPopupControllerImpl::SetPopupBounds; | 84 using AutofillPopupControllerImpl::SetPopupBounds; |
| 85 using AutofillPopupControllerImpl::names; | 85 using AutofillPopupControllerImpl::names; |
| 86 using AutofillPopupControllerImpl::subtexts; | 86 using AutofillPopupControllerImpl::subtexts; |
| 87 using AutofillPopupControllerImpl::identifiers; | 87 using AutofillPopupControllerImpl::identifiers; |
| 88 using AutofillPopupControllerImpl::selected_line; | 88 using AutofillPopupControllerImpl::preselected_line; |
| 89 using AutofillPopupControllerImpl::SetSelectedLine; | 89 using AutofillPopupControllerImpl::SetPreselectedLine; |
| 90 using AutofillPopupControllerImpl::SelectNextLine; | 90 using AutofillPopupControllerImpl::PreselectNextLine; |
| 91 using AutofillPopupControllerImpl::SelectPreviousLine; | 91 using AutofillPopupControllerImpl::PreselectPreviousLine; |
| 92 using AutofillPopupControllerImpl::RemoveSelectedLine; | 92 using AutofillPopupControllerImpl::RemovePreselectedLine; |
| 93 using AutofillPopupControllerImpl::popup_bounds; | 93 using AutofillPopupControllerImpl::popup_bounds; |
| 94 using AutofillPopupControllerImpl::element_bounds; | 94 using AutofillPopupControllerImpl::element_bounds; |
| 95 #if !defined(OS_ANDROID) | 95 #if !defined(OS_ANDROID) |
| 96 using AutofillPopupControllerImpl::GetNameFontForRow; | 96 using AutofillPopupControllerImpl::GetNameFontForRow; |
| 97 using AutofillPopupControllerImpl::subtext_font; | 97 using AutofillPopupControllerImpl::subtext_font; |
| 98 using AutofillPopupControllerImpl::RowWidthWithoutText; | 98 using AutofillPopupControllerImpl::RowWidthWithoutText; |
| 99 #endif | 99 #endif |
| 100 using AutofillPopupControllerImpl::SetValues; | 100 using AutofillPopupControllerImpl::SetValues; |
| 101 using AutofillPopupControllerImpl::GetDesiredPopupWidth; | 101 using AutofillPopupControllerImpl::GetDesiredPopupWidth; |
| 102 using AutofillPopupControllerImpl::GetDesiredPopupHeight; | 102 using AutofillPopupControllerImpl::GetDesiredPopupHeight; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 gfx::Rect popup_bounds(10, 10, 100, 100); | 174 gfx::Rect popup_bounds(10, 10, 100, 100); |
| 175 | 175 |
| 176 EXPECT_CALL(*autofill_popup_controller_, | 176 EXPECT_CALL(*autofill_popup_controller_, |
| 177 UpdateBoundsAndRedrawPopup()); | 177 UpdateBoundsAndRedrawPopup()); |
| 178 | 178 |
| 179 popup_controller()->SetPopupBounds(popup_bounds); | 179 popup_controller()->SetPopupBounds(popup_bounds); |
| 180 | 180 |
| 181 EXPECT_EQ(popup_bounds, popup_controller()->popup_bounds()); | 181 EXPECT_EQ(popup_bounds, popup_controller()->popup_bounds()); |
| 182 } | 182 } |
| 183 | 183 |
| 184 TEST_F(AutofillPopupControllerUnitTest, ChangeSelectedLine) { | 184 TEST_F(AutofillPopupControllerUnitTest, ChangePreselectedLine) { |
| 185 // Set up the popup. | 185 // Set up the popup. |
| 186 std::vector<string16> names(2, string16()); | 186 std::vector<string16> names(2, string16()); |
| 187 std::vector<int> autofill_ids(2, 0); | 187 std::vector<int> autofill_ids(2, 0); |
| 188 autofill_popup_controller_->Show(names, names, names, autofill_ids); | 188 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 189 | 189 |
| 190 EXPECT_LT(autofill_popup_controller_->selected_line(), 0); | 190 EXPECT_LT(autofill_popup_controller_->preselected_line(), 0); |
| 191 // Check that there are at least 2 values so that the first and last selection | 191 // Check that there are at least 2 values so that the first and last selection |
| 192 // are different. | 192 // are different. |
| 193 EXPECT_GE(2, | 193 EXPECT_GE(2, |
| 194 static_cast<int>(autofill_popup_controller_->subtexts().size())); | 194 static_cast<int>(autofill_popup_controller_->subtexts().size())); |
| 195 | 195 |
| 196 // Test wrapping before the front. | 196 // Test wrapping before the front. |
| 197 autofill_popup_controller_->SelectPreviousLine(); | 197 autofill_popup_controller_->PreselectPreviousLine(); |
| 198 EXPECT_EQ(static_cast<int>( | 198 EXPECT_EQ(static_cast<int>( |
| 199 autofill_popup_controller_->subtexts().size() - 1), | 199 autofill_popup_controller_->subtexts().size() - 1), |
| 200 autofill_popup_controller_->selected_line()); | 200 autofill_popup_controller_->preselected_line()); |
| 201 | 201 |
| 202 // Test wrapping after the end. | 202 // Test wrapping after the end. |
| 203 autofill_popup_controller_->SelectNextLine(); | 203 autofill_popup_controller_->PreselectNextLine(); |
| 204 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); | 204 EXPECT_EQ(0, autofill_popup_controller_->preselected_line()); |
| 205 } | 205 } |
| 206 | 206 |
| 207 TEST_F(AutofillPopupControllerUnitTest, RedrawSelectedLine) { | 207 TEST_F(AutofillPopupControllerUnitTest, RedrawPreselectedLine) { |
| 208 // Set up the popup. | 208 // Set up the popup. |
| 209 std::vector<string16> names(2, string16()); | 209 std::vector<string16> names(2, string16()); |
| 210 std::vector<int> autofill_ids(2, 0); | 210 std::vector<int> autofill_ids(2, 0); |
| 211 autofill_popup_controller_->Show(names, names, names, autofill_ids); | 211 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 212 | 212 |
| 213 // Make sure that when a new line is selected, it is invalidated so it can | 213 // Make sure that when a new line is preselected, it is invalidated so it can |
| 214 // be updated to show it is selected. | 214 // be updated to show it is preselected. |
| 215 int selected_line = 0; | 215 int preselected_line = 0; |
| 216 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); | 216 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(preselected_line)); |
| 217 autofill_popup_controller_->SetSelectedLine(selected_line); | 217 autofill_popup_controller_->SetPreselectedLine(preselected_line); |
| 218 | 218 |
| 219 // Ensure that the row isn't invalidated if it didn't change. | 219 // Ensure that the row isn't invalidated if it didn't change. |
| 220 EXPECT_CALL(*autofill_popup_controller_, | 220 EXPECT_CALL(*autofill_popup_controller_, |
| 221 InvalidateRow(selected_line)).Times(0); | 221 InvalidateRow(preselected_line)).Times(0); |
| 222 autofill_popup_controller_->SetSelectedLine(selected_line); | 222 autofill_popup_controller_->SetPreselectedLine(preselected_line); |
| 223 | 223 |
| 224 // Change back to no selection. | 224 // Change back to no preselection. |
| 225 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); | 225 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(preselected_line)); |
| 226 autofill_popup_controller_->SetSelectedLine(-1); | 226 autofill_popup_controller_->SetPreselectedLine(-1); |
| 227 } | 227 } |
| 228 | 228 |
| 229 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) { | 229 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) { |
| 230 // Set up the popup. | 230 // Set up the popup. |
| 231 std::vector<string16> names(3, string16()); | 231 std::vector<string16> names(3, string16()); |
| 232 std::vector<int> autofill_ids; | 232 std::vector<int> autofill_ids; |
| 233 autofill_ids.push_back(1); | 233 autofill_ids.push_back(1); |
| 234 autofill_ids.push_back(1); | 234 autofill_ids.push_back(1); |
| 235 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); | 235 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); |
| 236 autofill_popup_controller_->Show(names, names, names, autofill_ids); | 236 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 237 | 237 |
| 238 // Generate a popup, so it can be hidden later. It doesn't matter what the | 238 // Generate a popup, so it can be hidden later. It doesn't matter what the |
| 239 // external_delegate thinks is being shown in the process, since we are just | 239 // external_delegate thinks is being shown in the process, since we are just |
| 240 // testing the popup here. | 240 // testing the popup here. |
| 241 autofill::GenerateTestAutofillPopup(external_delegate_.get()); | 241 autofill::GenerateTestAutofillPopup(external_delegate_.get()); |
| 242 | 242 |
| 243 // No line is selected so the removal should fail. | 243 // No line is preselected so the removal should fail. |
| 244 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); | 244 EXPECT_FALSE(autofill_popup_controller_->RemovePreselectedLine()); |
| 245 | 245 |
| 246 // Try to remove the last entry and ensure it fails (it is an option). | 246 // Try to remove the last entry and ensure it fails (it is an option). |
| 247 autofill_popup_controller_->SetSelectedLine( | 247 autofill_popup_controller_->SetPreselectedLine( |
| 248 autofill_popup_controller_->subtexts().size() - 1); | 248 autofill_popup_controller_->subtexts().size() - 1); |
| 249 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); | 249 EXPECT_FALSE(autofill_popup_controller_->RemovePreselectedLine()); |
| 250 EXPECT_LE(0, autofill_popup_controller_->selected_line()); | 250 EXPECT_LE(0, autofill_popup_controller_->preselected_line()); |
| 251 | 251 |
| 252 // Remove the first entry. The popup should be redrawn since its size has | 252 // Remove the first entry. The popup should be redrawn since its size has |
| 253 // changed. | 253 // changed. |
| 254 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); | 254 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); |
| 255 autofill_popup_controller_->SetSelectedLine(0); | 255 autofill_popup_controller_->SetPreselectedLine(0); |
| 256 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 256 EXPECT_TRUE(autofill_popup_controller_->RemovePreselectedLine()); |
| 257 | 257 |
| 258 // Remove the last entry. The popup should then be hidden since there are | 258 // Remove the last entry. The popup should then be hidden since there are |
| 259 // no Autofill entries left. | 259 // no Autofill entries left. |
| 260 EXPECT_CALL(*autofill_popup_controller_, Hide()); | 260 EXPECT_CALL(*autofill_popup_controller_, Hide()); |
| 261 autofill_popup_controller_->SetSelectedLine(0); | 261 autofill_popup_controller_->SetPreselectedLine(0); |
| 262 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 262 EXPECT_TRUE(autofill_popup_controller_->RemovePreselectedLine()); |
| 263 } | 263 } |
| 264 | 264 |
| 265 TEST_F(AutofillPopupControllerUnitTest, RemoveOnlyLine) { | 265 TEST_F(AutofillPopupControllerUnitTest, RemoveOnlyLine) { |
| 266 // Set up the popup. | 266 // Set up the popup. |
| 267 std::vector<string16> names(1, string16()); | 267 std::vector<string16> names(1, string16()); |
| 268 std::vector<int> autofill_ids; | 268 std::vector<int> autofill_ids; |
| 269 autofill_ids.push_back(1); | 269 autofill_ids.push_back(1); |
| 270 autofill_popup_controller_->Show(names, names, names, autofill_ids); | 270 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 271 | 271 |
| 272 // Generate a popup. | 272 // Generate a popup. |
| 273 autofill::GenerateTestAutofillPopup(external_delegate_.get()); | 273 autofill::GenerateTestAutofillPopup(external_delegate_.get()); |
| 274 | 274 |
| 275 // Select the only line. | 275 // Preselect the only line. |
| 276 autofill_popup_controller_->SetSelectedLine(0); | 276 autofill_popup_controller_->SetPreselectedLine(0); |
| 277 | 277 |
| 278 // Remove the only line. There should be no row invalidation and the popup | 278 // Remove the only line. There should be no row invalidation and the popup |
| 279 // should then be hidden since there are no Autofill entries left. | 279 // should then be hidden since there are no Autofill entries left. |
| 280 EXPECT_CALL(*autofill_popup_controller_, Hide()); | 280 EXPECT_CALL(*autofill_popup_controller_, Hide()); |
| 281 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(_)).Times(0); | 281 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(_)).Times(0); |
| 282 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 282 EXPECT_TRUE(autofill_popup_controller_->RemovePreselectedLine()); |
| 283 } | 283 } |
| 284 | 284 |
| 285 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { | 285 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { |
| 286 // Set up the popup. | 286 // Set up the popup. |
| 287 std::vector<string16> names(3, string16()); | 287 std::vector<string16> names(3, string16()); |
| 288 std::vector<int> autofill_ids; | 288 std::vector<int> autofill_ids; |
| 289 autofill_ids.push_back(1); | 289 autofill_ids.push_back(1); |
| 290 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator); | 290 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator); |
| 291 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); | 291 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); |
| 292 autofill_popup_controller_->Show(names, names, names, autofill_ids); | 292 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 293 | 293 |
| 294 autofill_popup_controller_->SetSelectedLine(0); | 294 autofill_popup_controller_->SetPreselectedLine(0); |
| 295 | 295 |
| 296 // Make sure next skips the unselectable separator. | 296 // Make sure next skips the unselectable separator. |
| 297 autofill_popup_controller_->SelectNextLine(); | 297 autofill_popup_controller_->PreselectNextLine(); |
| 298 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); | 298 EXPECT_EQ(2, autofill_popup_controller_->preselected_line()); |
| 299 | 299 |
| 300 // Make sure previous skips the unselectable separator. | 300 // Make sure previous skips the unselectable separator. |
| 301 autofill_popup_controller_->SelectPreviousLine(); | 301 autofill_popup_controller_->PreselectPreviousLine(); |
| 302 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); | 302 EXPECT_EQ(0, autofill_popup_controller_->preselected_line()); |
| 303 } | 303 } |
| 304 | 304 |
| 305 TEST_F(AutofillPopupControllerUnitTest, RowWidthWithoutText) { | 305 TEST_F(AutofillPopupControllerUnitTest, RowWidthWithoutText) { |
| 306 std::vector<string16> names(4); | 306 std::vector<string16> names(4); |
| 307 std::vector<string16> subtexts(4); | 307 std::vector<string16> subtexts(4); |
| 308 std::vector<string16> icons(4); | 308 std::vector<string16> icons(4); |
| 309 std::vector<int> ids(4); | 309 std::vector<int> ids(4); |
| 310 | 310 |
| 311 // Set up some visible display so the text values are kept. | 311 // Set up some visible display so the text values are kept. |
| 312 gfx::Display display(0, gfx::Rect(0, 0, 100, 100)); | 312 gfx::Display display(0, gfx::Rect(0, 0, 100, 100)); |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 controller3->Hide(); | 469 controller3->Hide(); |
| 470 | 470 |
| 471 // Hide the test_controller to delete it. | 471 // Hide the test_controller to delete it. |
| 472 test_controller->DoHide(); | 472 test_controller->DoHide(); |
| 473 } | 473 } |
| 474 | 474 |
| 475 TEST_F(AutofillPopupControllerUnitTest, ProperlyResetController) { | 475 TEST_F(AutofillPopupControllerUnitTest, ProperlyResetController) { |
| 476 std::vector<string16> names(2); | 476 std::vector<string16> names(2); |
| 477 std::vector<int> ids(2); | 477 std::vector<int> ids(2); |
| 478 popup_controller()->SetValues(names, names, names, ids); | 478 popup_controller()->SetValues(names, names, names, ids); |
| 479 popup_controller()->SetSelectedLine(0); | 479 popup_controller()->SetPreselectedLine(0); |
| 480 | 480 |
| 481 // Now show a new popup with the same controller, but with fewer items. | 481 // Now show a new popup with the same controller, but with fewer items. |
| 482 WeakPtr<AutofillPopupControllerImpl> controller = | 482 WeakPtr<AutofillPopupControllerImpl> controller = |
| 483 AutofillPopupControllerImpl::GetOrCreate( | 483 AutofillPopupControllerImpl::GetOrCreate( |
| 484 popup_controller()->GetWeakPtr(), | 484 popup_controller()->GetWeakPtr(), |
| 485 delegate()->GetWeakPtr(), | 485 delegate()->GetWeakPtr(), |
| 486 NULL, | 486 NULL, |
| 487 gfx::Rect(), | 487 gfx::Rect(), |
| 488 base::i18n::UNKNOWN_DIRECTION); | 488 base::i18n::UNKNOWN_DIRECTION); |
| 489 EXPECT_NE(0, controller->selected_line()); | 489 EXPECT_NE(0, controller->preselected_line()); |
| 490 EXPECT_TRUE(controller->names().empty()); | 490 EXPECT_TRUE(controller->names().empty()); |
| 491 } | 491 } |
| 492 | 492 |
| 493 #if !defined(OS_ANDROID) | 493 #if !defined(OS_ANDROID) |
| 494 TEST_F(AutofillPopupControllerUnitTest, ElideText) { | 494 TEST_F(AutofillPopupControllerUnitTest, ElideText) { |
| 495 std::vector<string16> names; | 495 std::vector<string16> names; |
| 496 names.push_back(ASCIIToUTF16("Text that will need to be trimmed")); | 496 names.push_back(ASCIIToUTF16("Text that will need to be trimmed")); |
| 497 names.push_back(ASCIIToUTF16("Untrimmed")); | 497 names.push_back(ASCIIToUTF16("Untrimmed")); |
| 498 | 498 |
| 499 std::vector<string16> subtexts; | 499 std::vector<string16> subtexts; |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 EXPECT_EQ(expected_popup_bounds[i].ToString(), | 595 EXPECT_EQ(expected_popup_bounds[i].ToString(), |
| 596 autofill_popup_controller->popup_bounds().ToString()) << | 596 autofill_popup_controller->popup_bounds().ToString()) << |
| 597 "Popup bounds failed to match for test " << i; | 597 "Popup bounds failed to match for test " << i; |
| 598 | 598 |
| 599 // Hide the controller to delete it. | 599 // Hide the controller to delete it. |
| 600 autofill_popup_controller->DoHide(); | 600 autofill_popup_controller->DoHide(); |
| 601 } | 601 } |
| 602 } | 602 } |
| 603 | 603 |
| 604 } // namespace autofill | 604 } // namespace autofill |
| OLD | NEW |