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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" | 13 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
14 #include "chrome/browser/ui/autofill/autofill_popup_view.h" | 14 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
15 #include "chrome/browser/ui/autofill/popup_constants.h" | |
16 #include "chrome/browser/ui/autofill/test_popup_controller_common.h" | |
17 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
18 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
19 #include "components/autofill/content/browser/content_autofill_driver.h" | 17 #include "components/autofill/content/browser/content_autofill_driver.h" |
20 #include "components/autofill/content/browser/content_autofill_driver_factory.h" | 18 #include "components/autofill/content/browser/content_autofill_driver_factory.h" |
21 #include "components/autofill/core/browser/autofill_external_delegate.h" | 19 #include "components/autofill/core/browser/autofill_external_delegate.h" |
22 #include "components/autofill/core/browser/autofill_manager.h" | 20 #include "components/autofill/core/browser/autofill_manager.h" |
23 #include "components/autofill/core/browser/autofill_test_utils.h" | 21 #include "components/autofill/core/browser/autofill_test_utils.h" |
24 #include "components/autofill/core/browser/popup_item_ids.h" | 22 #include "components/autofill/core/browser/popup_item_ids.h" |
25 #include "components/autofill/core/browser/test_autofill_client.h" | 23 #include "components/autofill/core/browser/test_autofill_client.h" |
26 #include "components/autofill/core/browser/test_autofill_external_delegate.h" | 24 #include "components/autofill/core/browser/test_autofill_external_delegate.h" |
27 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
28 #include "grit/components_scaled_resources.h" | |
29 #include "testing/gmock/include/gmock/gmock.h" | 26 #include "testing/gmock/include/gmock/gmock.h" |
30 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
31 #include "ui/base/resource/resource_bundle.h" | |
32 #include "ui/gfx/display.h" | 28 #include "ui/gfx/display.h" |
33 #include "ui/gfx/geometry/rect.h" | 29 #include "ui/gfx/geometry/rect.h" |
34 #include "ui/gfx/text_utils.h" | 30 #include "ui/gfx/text_utils.h" |
35 | 31 |
36 using ::testing::_; | 32 using ::testing::_; |
37 using ::testing::AtLeast; | 33 using ::testing::AtLeast; |
38 using ::testing::NiceMock; | 34 using ::testing::NiceMock; |
39 using base::ASCIIToUTF16; | 35 using base::ASCIIToUTF16; |
40 using base::WeakPtr; | 36 using base::WeakPtr; |
41 | 37 |
(...skipping 26 matching lines...) Expand all Loading... |
68 PrefService* GetPrefs() override { return prefs_.get(); } | 64 PrefService* GetPrefs() override { return prefs_.get(); } |
69 | 65 |
70 private: | 66 private: |
71 scoped_ptr<PrefService> prefs_; | 67 scoped_ptr<PrefService> prefs_; |
72 | 68 |
73 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient); | 69 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient); |
74 }; | 70 }; |
75 | 71 |
76 class TestAutofillPopupController : public AutofillPopupControllerImpl { | 72 class TestAutofillPopupController : public AutofillPopupControllerImpl { |
77 public: | 73 public: |
78 explicit TestAutofillPopupController( | 74 TestAutofillPopupController( |
79 base::WeakPtr<AutofillExternalDelegate> external_delegate, | 75 base::WeakPtr<AutofillExternalDelegate> external_delegate, |
80 const gfx::RectF& element_bounds) | 76 const gfx::RectF& element_bounds) |
81 : AutofillPopupControllerImpl(external_delegate, | 77 : AutofillPopupControllerImpl(external_delegate, |
82 NULL, | 78 NULL, |
83 NULL, | 79 NULL, |
84 element_bounds, | 80 element_bounds, |
85 base::i18n::UNKNOWN_DIRECTION), | 81 base::i18n::UNKNOWN_DIRECTION) {} |
86 test_controller_common_( | |
87 new TestPopupControllerCommon(element_bounds, | |
88 base::i18n::LEFT_TO_RIGHT)) { | |
89 controller_common_.reset(test_controller_common_); | |
90 } | |
91 ~TestAutofillPopupController() override {} | 82 ~TestAutofillPopupController() override {} |
92 | 83 |
93 void set_display(const gfx::Display& display) { | |
94 test_controller_common_->set_display(display); | |
95 } | |
96 | |
97 // Making protected functions public for testing | 84 // Making protected functions public for testing |
98 using AutofillPopupControllerImpl::GetLineCount; | 85 using AutofillPopupControllerImpl::GetLineCount; |
99 using AutofillPopupControllerImpl::GetSuggestionAt; | 86 using AutofillPopupControllerImpl::GetSuggestionAt; |
100 using AutofillPopupControllerImpl::GetElidedValueAt; | 87 using AutofillPopupControllerImpl::GetElidedValueAt; |
101 using AutofillPopupControllerImpl::GetElidedLabelAt; | 88 using AutofillPopupControllerImpl::GetElidedLabelAt; |
102 using AutofillPopupControllerImpl::selected_line; | 89 using AutofillPopupControllerImpl::selected_line; |
103 using AutofillPopupControllerImpl::SetSelectedLine; | 90 using AutofillPopupControllerImpl::SetSelectedLine; |
104 using AutofillPopupControllerImpl::SelectNextLine; | 91 using AutofillPopupControllerImpl::SelectNextLine; |
105 using AutofillPopupControllerImpl::SelectPreviousLine; | 92 using AutofillPopupControllerImpl::SelectPreviousLine; |
106 using AutofillPopupControllerImpl::RemoveSelectedLine; | 93 using AutofillPopupControllerImpl::RemoveSelectedLine; |
107 using AutofillPopupControllerImpl::popup_bounds; | 94 using AutofillPopupControllerImpl::popup_bounds; |
108 using AutofillPopupControllerImpl::element_bounds; | 95 using AutofillPopupControllerImpl::element_bounds; |
109 #if !defined(OS_ANDROID) | 96 #if !defined(OS_ANDROID) |
110 using AutofillPopupControllerImpl::GetValueFontListForRow; | 97 using AutofillPopupControllerImpl::GetValueFontListForRow; |
111 using AutofillPopupControllerImpl::GetLabelFontList; | 98 using AutofillPopupControllerImpl::GetLabelFontList; |
112 using AutofillPopupControllerImpl::RowWidthWithoutText; | |
113 #endif | 99 #endif |
114 using AutofillPopupControllerImpl::SetValues; | 100 using AutofillPopupControllerImpl::SetValues; |
115 using AutofillPopupControllerImpl::GetDesiredPopupWidth; | |
116 using AutofillPopupControllerImpl::GetDesiredPopupHeight; | |
117 using AutofillPopupControllerImpl::GetWeakPtr; | 101 using AutofillPopupControllerImpl::GetWeakPtr; |
118 MOCK_METHOD1(InvalidateRow, void(size_t)); | 102 MOCK_METHOD1(InvalidateRow, void(size_t)); |
119 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void()); | 103 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void()); |
120 MOCK_METHOD0(Hide, void()); | 104 MOCK_METHOD0(Hide, void()); |
121 | 105 |
122 void DoHide() { | 106 void DoHide() { |
123 AutofillPopupControllerImpl::Hide(); | 107 AutofillPopupControllerImpl::Hide(); |
124 } | 108 } |
125 | 109 |
126 private: | 110 private: |
127 void ShowView() override {} | 111 void ShowView() override {} |
128 | |
129 TestPopupControllerCommon* test_controller_common_; | |
130 }; | 112 }; |
131 | 113 |
132 } // namespace | 114 } // namespace |
133 | 115 |
134 class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness { | 116 class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness { |
135 public: | 117 public: |
136 AutofillPopupControllerUnitTest() | 118 AutofillPopupControllerUnitTest() |
137 : autofill_client_(new MockAutofillClient()), | 119 : autofill_client_(new MockAutofillClient()), |
138 autofill_popup_controller_(NULL) {} | 120 autofill_popup_controller_(NULL) {} |
139 ~AutofillPopupControllerUnitTest() override {} | 121 ~AutofillPopupControllerUnitTest() override {} |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 | 273 |
292 // Make sure next skips the unselectable separator. | 274 // Make sure next skips the unselectable separator. |
293 autofill_popup_controller_->SelectNextLine(); | 275 autofill_popup_controller_->SelectNextLine(); |
294 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); | 276 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); |
295 | 277 |
296 // Make sure previous skips the unselectable separator. | 278 // Make sure previous skips the unselectable separator. |
297 autofill_popup_controller_->SelectPreviousLine(); | 279 autofill_popup_controller_->SelectPreviousLine(); |
298 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); | 280 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); |
299 } | 281 } |
300 | 282 |
301 TEST_F(AutofillPopupControllerUnitTest, RowWidthWithoutText) { | |
302 // Give elements 1 and 3 subtexts and elements 2 and 3 icons, to ensure | |
303 // all combinations of subtexts and icons. | |
304 std::vector<Suggestion> suggestions; | |
305 suggestions.push_back(Suggestion("", "", "", 0)); | |
306 suggestions.push_back(Suggestion("", "x", "", 0)); | |
307 suggestions.push_back(Suggestion("", "", "americanExpressCC", 0)); | |
308 suggestions.push_back(Suggestion("", "x", "genericCC", 0)); | |
309 | |
310 // Set up some visible display so the text values are kept. | |
311 gfx::Display display(0, gfx::Rect(0, 0, 100, 100)); | |
312 autofill_popup_controller_->set_display(display); | |
313 | |
314 autofill_popup_controller_->Show(suggestions); | |
315 | |
316 int base_size = | |
317 AutofillPopupView::kEndPadding * 2 + | |
318 kPopupBorderThickness * 2; | |
319 int subtext_increase = AutofillPopupView::kNamePadding; | |
320 | |
321 EXPECT_EQ(base_size, autofill_popup_controller_->RowWidthWithoutText(0)); | |
322 EXPECT_EQ(base_size + subtext_increase, | |
323 autofill_popup_controller_->RowWidthWithoutText(1)); | |
324 EXPECT_EQ(base_size + AutofillPopupView::kIconPadding + | |
325 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
326 IDR_AUTOFILL_CC_AMEX).Width(), | |
327 autofill_popup_controller_->RowWidthWithoutText(2)); | |
328 EXPECT_EQ(base_size + subtext_increase + AutofillPopupView::kIconPadding + | |
329 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
330 IDR_AUTOFILL_CC_GENERIC).Width(), | |
331 autofill_popup_controller_->RowWidthWithoutText(3)); | |
332 } | |
333 | |
334 TEST_F(AutofillPopupControllerUnitTest, UpdateDataListValues) { | 283 TEST_F(AutofillPopupControllerUnitTest, UpdateDataListValues) { |
335 std::vector<Suggestion> suggestions; | 284 std::vector<Suggestion> suggestions; |
336 suggestions.push_back(Suggestion("", "", "", 1)); | 285 suggestions.push_back(Suggestion("", "", "", 1)); |
337 autofill_popup_controller_->Show(suggestions); | 286 autofill_popup_controller_->Show(suggestions); |
338 | 287 |
339 // Add one data list entry. | 288 // Add one data list entry. |
340 base::string16 value1 = ASCIIToUTF16("data list value 1"); | 289 base::string16 value1 = ASCIIToUTF16("data list value 1"); |
341 std::vector<base::string16> data_list_values; | 290 std::vector<base::string16> data_list_values; |
342 data_list_values.push_back(value1); | 291 data_list_values.push_back(value1); |
343 | 292 |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 | 432 |
484 #if !defined(OS_ANDROID) | 433 #if !defined(OS_ANDROID) |
485 TEST_F(AutofillPopupControllerUnitTest, ElideText) { | 434 TEST_F(AutofillPopupControllerUnitTest, ElideText) { |
486 std::vector<Suggestion> suggestions; | 435 std::vector<Suggestion> suggestions; |
487 suggestions.push_back( | 436 suggestions.push_back( |
488 Suggestion("Text that will need to be trimmed", | 437 Suggestion("Text that will need to be trimmed", |
489 "Label that will be trimmed", "genericCC", 0)); | 438 "Label that will be trimmed", "genericCC", 0)); |
490 suggestions.push_back( | 439 suggestions.push_back( |
491 Suggestion("untrimmed", "Untrimmed", "genericCC", 0)); | 440 Suggestion("untrimmed", "Untrimmed", "genericCC", 0)); |
492 | 441 |
493 // Show the popup once so we can easily generate the size it needs. | 442 autofill_popup_controller_->SetValues(suggestions); |
494 autofill_popup_controller_->Show(suggestions); | |
495 | 443 |
496 // Ensure the popup will be too small to display all of the first row. | 444 // Ensure the popup will be too small to display all of the first row. |
497 int popup_max_width = | 445 int popup_max_width = |
498 gfx::GetStringWidth( | 446 gfx::GetStringWidth( |
499 suggestions[0].value, | 447 suggestions[0].value, |
500 autofill_popup_controller_->GetValueFontListForRow(0)) + | 448 autofill_popup_controller_->GetValueFontListForRow(0)) + |
501 gfx::GetStringWidth( | 449 gfx::GetStringWidth( |
502 suggestions[0].label, | 450 suggestions[0].label, |
503 autofill_popup_controller_->GetLabelFontList()) - 25; | 451 autofill_popup_controller_->GetLabelFontList()) - 25; |
504 gfx::Rect popup_bounds = gfx::Rect(0, 0, popup_max_width, 0); | |
505 autofill_popup_controller_->set_display(gfx::Display(0, popup_bounds)); | |
506 | 452 |
507 autofill_popup_controller_->Show(suggestions); | 453 autofill_popup_controller_->ElideValueAndLabelForRow(0, popup_max_width); |
508 | 454 |
509 // The first element was long so it should have been trimmed. | 455 // The first element was long so it should have been trimmed. |
510 EXPECT_NE(autofill_popup_controller_->GetSuggestionAt(0).value, | 456 EXPECT_NE(autofill_popup_controller_->GetSuggestionAt(0).value, |
511 autofill_popup_controller_->GetElidedValueAt(0)); | 457 autofill_popup_controller_->GetElidedValueAt(0)); |
512 EXPECT_NE(autofill_popup_controller_->GetSuggestionAt(0).label, | 458 EXPECT_NE(autofill_popup_controller_->GetSuggestionAt(0).label, |
513 autofill_popup_controller_->GetElidedLabelAt(0)); | 459 autofill_popup_controller_->GetElidedLabelAt(0)); |
514 | 460 |
| 461 autofill_popup_controller_->ElideValueAndLabelForRow(1, popup_max_width); |
| 462 |
515 // The second element was shorter so it should be unchanged. | 463 // The second element was shorter so it should be unchanged. |
516 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).value, | 464 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).value, |
517 autofill_popup_controller_->GetElidedValueAt(1)); | 465 autofill_popup_controller_->GetElidedValueAt(1)); |
518 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).label, | 466 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).label, |
519 autofill_popup_controller_->GetElidedLabelAt(1)); | 467 autofill_popup_controller_->GetElidedLabelAt(1)); |
520 } | 468 } |
521 #endif | 469 #endif |
522 | 470 |
523 } // namespace autofill | 471 } // namespace autofill |
OLD | NEW |