OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/ui/views/autofill/autofill_popup_view_views.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/optional.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "build/build_config.h" |
| 11 #include "chrome/browser/ui/autofill/autofill_popup_controller.h" |
| 12 #include "chrome/browser/ui/autofill/autofill_popup_layout_model.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/browser_window.h" |
| 15 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 16 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "components/autofill/core/browser/suggestion.h" |
| 18 #include "testing/gmock/include/gmock/gmock.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" |
| 20 #include "ui/gfx/font_list.h" |
| 21 #include "ui/gfx/geometry/rect.h" |
| 22 #include "ui/gfx/native_widget_types.h" |
| 23 #include "ui/native_theme/native_theme.h" |
| 24 #include "ui/views/widget/widget.h" |
| 25 |
| 26 using ::testing::NiceMock; |
| 27 using ::testing::Return; |
| 28 |
| 29 namespace autofill { |
| 30 namespace { |
| 31 |
| 32 constexpr int kNumInitialSuggestions = 3; |
| 33 |
| 34 class MockAutofillPopupController : public AutofillPopupController { |
| 35 public: |
| 36 MockAutofillPopupController() { |
| 37 gfx::FontList::SetDefaultFontDescription("Arial, Times New Roman, 15px"); |
| 38 layout_model_.reset( |
| 39 new AutofillPopupLayoutModel(this, false /* is_credit_card_field */)); |
| 40 } |
| 41 |
| 42 // AutofillPopupViewDelegate |
| 43 MOCK_METHOD0(Hide, void()); |
| 44 MOCK_METHOD0(ViewDestroyed, void()); |
| 45 MOCK_METHOD1(SetSelectionAtPoint, void(const gfx::Point& point)); |
| 46 MOCK_METHOD0(AcceptSelectedLine, bool()); |
| 47 MOCK_METHOD0(SelectionCleared, void()); |
| 48 MOCK_CONST_METHOD0(popup_bounds, gfx::Rect()); |
| 49 MOCK_METHOD0(container_view, gfx::NativeView()); |
| 50 MOCK_CONST_METHOD0(element_bounds, const gfx::RectF&()); |
| 51 MOCK_CONST_METHOD0(IsRTL, bool()); |
| 52 const std::vector<autofill::Suggestion> GetSuggestions() override { |
| 53 std::vector<Suggestion> suggestions(GetLineCount(), |
| 54 Suggestion("", "", "", 0)); |
| 55 return suggestions; |
| 56 } |
| 57 #if !defined(OS_ANDROID) |
| 58 MOCK_METHOD1(GetElidedValueWidthForRow, int(size_t row)); |
| 59 MOCK_METHOD1(GetElidedLabelWidthForRow, int(size_t row)); |
| 60 #endif |
| 61 |
| 62 // AutofillPopupController |
| 63 MOCK_METHOD0(OnSuggestionsChanged, void()); |
| 64 MOCK_METHOD1(AcceptSuggestion, void(size_t index)); |
| 65 MOCK_CONST_METHOD0(GetLineCount, size_t()); |
| 66 const autofill::Suggestion& GetSuggestionAt(size_t row) const override { |
| 67 return suggestion_; |
| 68 } |
| 69 MOCK_CONST_METHOD1(GetElidedValueAt, const base::string16&(size_t row)); |
| 70 MOCK_CONST_METHOD1(GetElidedLabelAt, const base::string16&(size_t row)); |
| 71 MOCK_METHOD3(GetRemovalConfirmationText, |
| 72 bool(int index, base::string16* title, base::string16* body)); |
| 73 MOCK_METHOD1(RemoveSuggestion, bool(int index)); |
| 74 MOCK_CONST_METHOD1(GetBackgroundColorIDForRow, |
| 75 ui::NativeTheme::ColorId(int index)); |
| 76 MOCK_CONST_METHOD0(selected_line, base::Optional<size_t>()); |
| 77 const AutofillPopupLayoutModel& layout_model() const override { |
| 78 return *layout_model_; |
| 79 } |
| 80 |
| 81 private: |
| 82 std::unique_ptr<AutofillPopupLayoutModel> layout_model_; |
| 83 autofill::Suggestion suggestion_; |
| 84 }; |
| 85 |
| 86 class TestAutofillPopupViewViews : public AutofillPopupViewViews { |
| 87 public: |
| 88 TestAutofillPopupViewViews(AutofillPopupController* controller, |
| 89 views::Widget* parent_widget) |
| 90 : AutofillPopupViewViews(controller, parent_widget) {} |
| 91 ~TestAutofillPopupViewViews() override {} |
| 92 |
| 93 void DoUpdateBoundsAndRedrawPopup() override {} |
| 94 |
| 95 private: |
| 96 DISALLOW_COPY_AND_ASSIGN(TestAutofillPopupViewViews); |
| 97 }; |
| 98 |
| 99 } // namespace |
| 100 |
| 101 class AutofillPopupViewViewsTest : public InProcessBrowserTest { |
| 102 public: |
| 103 AutofillPopupViewViewsTest() {} |
| 104 ~AutofillPopupViewViewsTest() override {} |
| 105 |
| 106 void SetUpOnMainThread() override { |
| 107 gfx::NativeView native_view = |
| 108 browser()->tab_strip_model()->GetActiveWebContents()->GetNativeView(); |
| 109 EXPECT_CALL(autofill_popup_controller_, container_view()) |
| 110 .WillRepeatedly(Return(native_view)); |
| 111 EXPECT_CALL(autofill_popup_controller_, GetLineCount()) |
| 112 .WillRepeatedly(Return(kNumInitialSuggestions)); |
| 113 autofill_popup_view_views_ = new TestAutofillPopupViewViews( |
| 114 &autofill_popup_controller_, |
| 115 views::Widget::GetWidgetForNativeWindow( |
| 116 browser()->window()->GetNativeWindow())); |
| 117 } |
| 118 |
| 119 protected: |
| 120 NiceMock<MockAutofillPopupController> autofill_popup_controller_; |
| 121 // We intentionally do not destroy this view in the test because of |
| 122 // difficulty in mocking out 'RemoveObserver'. |
| 123 TestAutofillPopupViewViews* autofill_popup_view_views_; |
| 124 }; |
| 125 |
| 126 IN_PROC_BROWSER_TEST_F(AutofillPopupViewViewsTest, OnSelectedRowChanged) { |
| 127 for (int i = 0; i < kNumInitialSuggestions; ++i) { |
| 128 autofill_popup_view_views_->OnSelectedRowChanged(i - 1, i); |
| 129 } |
| 130 |
| 131 // Increase number of suggestions. |
| 132 EXPECT_CALL(autofill_popup_controller_, GetLineCount()) |
| 133 .WillRepeatedly(Return(kNumInitialSuggestions + 1)); |
| 134 |
| 135 autofill_popup_view_views_->OnSuggestionsChanged(); |
| 136 for (int i = 0; i < kNumInitialSuggestions + 1; ++i) { |
| 137 autofill_popup_view_views_->OnSelectedRowChanged( |
| 138 i ? i - 1 : kNumInitialSuggestions - 1, i); |
| 139 } |
| 140 |
| 141 // Decrease number of suggestions. |
| 142 EXPECT_CALL(autofill_popup_controller_, GetLineCount()) |
| 143 .WillRepeatedly(Return(kNumInitialSuggestions - 1)); |
| 144 |
| 145 autofill_popup_view_views_->OnSuggestionsChanged(); |
| 146 for (int i = 0; i < kNumInitialSuggestions - 1; ++i) { |
| 147 autofill_popup_view_views_->OnSelectedRowChanged( |
| 148 i ? kNumInitialSuggestions : i - 1, i); |
| 149 } |
| 150 } |
| 151 |
| 152 } // namespace autofill |
OLD | NEW |