Index: chrome/browser/ui/views/autofill/autofill_popup_view_views_browsertest.cc |
diff --git a/chrome/browser/ui/views/autofill/autofill_popup_view_views_browsertest.cc b/chrome/browser/ui/views/autofill/autofill_popup_view_views_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..5ea9725415cba6d34793578f2a2c744cc00741af |
--- /dev/null |
+++ b/chrome/browser/ui/views/autofill/autofill_popup_view_views_browsertest.cc |
@@ -0,0 +1,148 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/views/autofill/autofill_popup_view_views.h" |
+ |
+#include "base/macros.h" |
+#include "base/strings/utf_string_conversions.h" |
+#include "build/build_config.h" |
+#include "chrome/browser/ui/autofill/autofill_popup_controller.h" |
+#include "chrome/browser/ui/autofill/autofill_popup_layout_model.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/browser/ui/browser_window.h" |
+#include "chrome/browser/ui/tabs/tab_strip_model.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "components/autofill/core/browser/suggestion.h" |
+#include "testing/gmock/include/gmock/gmock.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "ui/gfx/font_list.h" |
+#include "ui/gfx/geometry/rect.h" |
+#include "ui/gfx/native_widget_types.h" |
+#include "ui/native_theme/native_theme.h" |
+#include "ui/views/widget/widget.h" |
+ |
+using ::testing::NiceMock; |
+using ::testing::Return; |
+ |
+namespace autofill { |
+namespace { |
+ |
+constexpr int kNumInitialSuggestions = 3; |
+ |
+class MockAutofillPopupController : public AutofillPopupController { |
+ public: |
+ MockAutofillPopupController() { |
+ gfx::FontList::SetDefaultFontDescription("Arial, Times New Roman, 15px"); |
+ layout_model_.reset( |
+ new AutofillPopupLayoutModel(this, false /* is_credit_card_field */)); |
+ } |
+ |
+ // AutofillPopupViewDelegate |
+ MOCK_METHOD0(Hide, void()); |
+ MOCK_METHOD0(ViewDestroyed, void()); |
+ MOCK_METHOD1(SetSelectionAtPoint, void(const gfx::Point& point)); |
+ MOCK_METHOD0(AcceptSelectedLine, bool()); |
+ MOCK_METHOD0(SelectionCleared, void()); |
+ MOCK_CONST_METHOD0(popup_bounds, gfx::Rect()); |
+ MOCK_METHOD0(container_view, gfx::NativeView()); |
+ MOCK_CONST_METHOD0(element_bounds, const gfx::RectF&()); |
+ MOCK_CONST_METHOD0(IsRTL, bool()); |
+ const std::vector<autofill::Suggestion> GetSuggestions() override { |
+ std::vector<Suggestion> suggestions(GetLineCount(), |
+ Suggestion("", "", "", 0)); |
+ return suggestions; |
+ } |
+#if !defined(OS_ANDROID) |
+ MOCK_METHOD1(GetElidedValueWidthForRow, int(size_t row)); |
+ MOCK_METHOD1(GetElidedLabelWidthForRow, int(size_t row)); |
+#endif |
+ |
+ // AutofillPopupController |
+ MOCK_METHOD0(OnSuggestionsChanged, void()); |
+ MOCK_METHOD1(AcceptSuggestion, void(size_t index)); |
+ MOCK_CONST_METHOD0(GetLineCount, size_t()); |
+ MOCK_CONST_METHOD1(GetSuggestionAt, const autofill::Suggestion&(size_t row)); |
+ MOCK_CONST_METHOD1(GetElidedValueAt, const base::string16&(size_t row)); |
+ MOCK_CONST_METHOD1(GetElidedLabelAt, const base::string16&(size_t row)); |
+ MOCK_METHOD3(GetRemovalConfirmationText, |
+ bool(int index, base::string16* title, base::string16* body)); |
+ MOCK_METHOD1(RemoveSuggestion, bool(int index)); |
+ MOCK_CONST_METHOD1(GetBackgroundColorIDForRow, |
+ ui::NativeTheme::ColorId(int index)); |
+ MOCK_CONST_METHOD0(selected_line, int()); |
+ const AutofillPopupLayoutModel& layout_model() const override { |
+ return *layout_model_; |
+ } |
+ |
+ private: |
+ std::unique_ptr<AutofillPopupLayoutModel> layout_model_; |
+}; |
+ |
+class TestAutofillPopupViewViews : public AutofillPopupViewViews { |
+ public: |
+ TestAutofillPopupViewViews(AutofillPopupController* controller, |
+ views::Widget* parent_widget) |
+ : AutofillPopupViewViews(controller, parent_widget) {} |
+ ~TestAutofillPopupViewViews() override {} |
+ |
+ void DoUpdateBoundsAndRedrawPopup() override {} |
+ |
+ private: |
+ DISALLOW_COPY_AND_ASSIGN(TestAutofillPopupViewViews); |
+}; |
+ |
+} // namespace |
+ |
+class AutofillPopupViewViewsTest : public InProcessBrowserTest { |
+ public: |
+ AutofillPopupViewViewsTest() {} |
+ ~AutofillPopupViewViewsTest() override {} |
+ |
+ void SetUpOnMainThread() override { |
+ gfx::NativeView native_view = |
+ browser()->tab_strip_model()->GetActiveWebContents()->GetNativeView(); |
+ EXPECT_CALL(autofill_popup_controller_, container_view()) |
+ .WillRepeatedly(Return(native_view)); |
+ EXPECT_CALL(autofill_popup_controller_, GetLineCount()) |
+ .WillRepeatedly(Return(kNumInitialSuggestions)); |
+ autofill_popup_view_views_ = new TestAutofillPopupViewViews( |
+ &autofill_popup_controller_, |
+ views::Widget::GetWidgetForNativeWindow( |
+ browser()->window()->GetNativeWindow())); |
+ } |
+ |
+ protected: |
+ NiceMock<MockAutofillPopupController> autofill_popup_controller_; |
+ // We intentionally do not destroy this view in the test because of |
+ // difficulty in mocking out 'RemoveObserver'. |
+ TestAutofillPopupViewViews* autofill_popup_view_views_; |
+}; |
+ |
+IN_PROC_BROWSER_TEST_F(AutofillPopupViewViewsTest, OnSelectedRowChanged) { |
+ for (int i = 0; i < kNumInitialSuggestions; ++i) { |
+ autofill_popup_view_views_->OnSelectedRowChanged(i - 1, i); |
+ } |
+ |
+ // Increase number of suggestions. |
+ EXPECT_CALL(autofill_popup_controller_, GetLineCount()) |
+ .WillRepeatedly(Return(kNumInitialSuggestions + 1)); |
+ |
+ autofill_popup_view_views_->OnSuggestionsChanged(); |
+ for (int i = 0; i < kNumInitialSuggestions + 1; ++i) { |
+ autofill_popup_view_views_->OnSelectedRowChanged( |
+ i ? i - 1 : kNumInitialSuggestions - 1, i); |
+ } |
+ |
+ // Decrease number of suggestions. |
+ EXPECT_CALL(autofill_popup_controller_, GetLineCount()) |
+ .WillRepeatedly(Return(kNumInitialSuggestions - 1)); |
+ |
+ autofill_popup_view_views_->OnSuggestionsChanged(); |
+ for (int i = 0; i < kNumInitialSuggestions - 1; ++i) { |
+ autofill_popup_view_views_->OnSelectedRowChanged( |
+ i ? kNumInitialSuggestions : i - 1, i); |
+ } |
+} |
+ |
+} // namespace autofill |