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