| 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 "chrome/browser/autofill/autofill_popup_view.h" | 6 #include "chrome/browser/autofill/autofill_popup_view.h" |
| 7 #include "chrome/browser/autofill/test_autofill_external_delegate.h" | 7 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 | 10 |
| 11 using ::testing::_; |
| 12 using ::testing::AtLeast; |
| 13 |
| 11 namespace { | 14 namespace { |
| 12 | 15 |
| 13 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { | 16 class MockAutofillExternalDelegate : public TestAutofillExternalDelegate { |
| 14 public: | 17 public: |
| 15 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; | 18 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; |
| 16 virtual ~MockAutofillExternalDelegate() {}; | 19 virtual ~MockAutofillExternalDelegate() {}; |
| 17 | 20 |
| 18 virtual void SelectAutofillSuggestionAtIndex(int unique_id, int list_index) | 21 virtual void SelectAutofillSuggestionAtIndex(int unique_id) |
| 19 OVERRIDE {} | 22 OVERRIDE {} |
| 23 |
| 24 virtual void ClearPreviewedForm() OVERRIDE {} |
| 20 }; | 25 }; |
| 21 | 26 |
| 22 class TestAutofillPopupView : public AutofillPopupView { | 27 class TestAutofillPopupView : public AutofillPopupView { |
| 23 public: | 28 public: |
| 24 explicit TestAutofillPopupView(AutofillExternalDelegate* external_delegate) : | 29 explicit TestAutofillPopupView(AutofillExternalDelegate* external_delegate) : |
| 25 AutofillPopupView(NULL, external_delegate) { | 30 AutofillPopupView(NULL, external_delegate) {} |
| 26 std::vector<string16> autofill_values; | |
| 27 autofill_values.push_back(string16()); | |
| 28 autofill_values.push_back(string16()); | |
| 29 | |
| 30 std::vector<int> autofill_ids; | |
| 31 autofill_ids.push_back(0); | |
| 32 autofill_ids.push_back(1); | |
| 33 | |
| 34 Show(autofill_values, autofill_values, autofill_values, autofill_ids, 0); | |
| 35 } | |
| 36 virtual ~TestAutofillPopupView() {} | 31 virtual ~TestAutofillPopupView() {} |
| 37 | 32 |
| 38 // Making protected functions public for testing | 33 // Making protected functions public for testing |
| 39 const std::vector<string16>& autofill_values() const { | 34 const std::vector<string16>& autofill_values() const { |
| 40 return AutofillPopupView::autofill_values(); | 35 return AutofillPopupView::autofill_values(); |
| 41 } | 36 } |
| 42 int selected_line() const { | 37 int selected_line() const { |
| 43 return AutofillPopupView::selected_line(); | 38 return AutofillPopupView::selected_line(); |
| 44 } | 39 } |
| 45 void SetSelectedLine(size_t selected_line) { | 40 void SetSelectedLine(size_t selected_line) { |
| 46 AutofillPopupView::SetSelectedLine(selected_line); | 41 AutofillPopupView::SetSelectedLine(selected_line); |
| 47 } | 42 } |
| 48 void SelectNextLine() { | 43 void SelectNextLine() { |
| 49 AutofillPopupView::SelectNextLine(); | 44 AutofillPopupView::SelectNextLine(); |
| 50 } | 45 } |
| 51 void SelectPreviousLine() { | 46 void SelectPreviousLine() { |
| 52 AutofillPopupView::SelectPreviousLine(); | 47 AutofillPopupView::SelectPreviousLine(); |
| 53 } | 48 } |
| 49 bool RemoveSelectedLine() { |
| 50 return AutofillPopupView::RemoveSelectedLine(); |
| 51 } |
| 54 | 52 |
| 55 MOCK_METHOD1(InvalidateRow, void(size_t)); | 53 MOCK_METHOD1(InvalidateRow, void(size_t)); |
| 54 MOCK_METHOD0(HideInternal, void()); |
| 56 | 55 |
| 57 private: | 56 private: |
| 58 virtual void ShowInternal() OVERRIDE {} | 57 virtual void ShowInternal() OVERRIDE {} |
| 59 virtual void HideInternal() OVERRIDE {} | 58 |
| 59 virtual void ResizePopup() OVERRIDE {} |
| 60 }; | 60 }; |
| 61 | 61 |
| 62 } // namespace | 62 } // namespace |
| 63 | 63 |
| 64 class AutofillPopupViewUnitTest : public ::testing::Test { | 64 class AutofillPopupViewUnitTest : public ::testing::Test { |
| 65 public: | 65 public: |
| 66 AutofillPopupViewUnitTest() { | 66 AutofillPopupViewUnitTest() { |
| 67 autofill_popup_view_.reset(new TestAutofillPopupView(&external_delegate_)); | 67 autofill_popup_view_.reset(new TestAutofillPopupView(&external_delegate_)); |
| 68 } | 68 } |
| 69 virtual ~AutofillPopupViewUnitTest() {} | 69 virtual ~AutofillPopupViewUnitTest() {} |
| 70 | 70 |
| 71 scoped_ptr<TestAutofillPopupView> autofill_popup_view_; | 71 scoped_ptr<TestAutofillPopupView> autofill_popup_view_; |
| 72 | 72 |
| 73 private: | 73 private: |
| 74 MockAutofillExternalDelegate external_delegate_; | 74 MockAutofillExternalDelegate external_delegate_; |
| 75 }; | 75 }; |
| 76 | 76 |
| 77 TEST_F(AutofillPopupViewUnitTest, ChangeSelectedLine) { | 77 TEST_F(AutofillPopupViewUnitTest, ChangeSelectedLine) { |
| 78 // Set up the popup. |
| 79 std::vector<string16> autofill_values(2, string16()); |
| 80 std::vector<int> autofill_ids(2, 0); |
| 81 autofill_popup_view_->Show(autofill_values, autofill_values, autofill_values, |
| 82 autofill_ids, 1); |
| 83 |
| 84 // To remove warnings. |
| 85 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(_)).Times(AtLeast(0)); |
| 86 |
| 78 EXPECT_LT(autofill_popup_view_->selected_line(), 0); | 87 EXPECT_LT(autofill_popup_view_->selected_line(), 0); |
| 79 // Check that there are at least 2 values so that the first and last selection | 88 // Check that there are at least 2 values so that the first and last selection |
| 80 // are different. | 89 // are different. |
| 81 EXPECT_GE(2, | 90 EXPECT_GE(2, |
| 82 static_cast<int>(autofill_popup_view_->autofill_values().size())); | 91 static_cast<int>(autofill_popup_view_->autofill_values().size())); |
| 83 | 92 |
| 84 // Test wrapping before the front. | 93 // Test wrapping before the front. |
| 85 autofill_popup_view_->SelectPreviousLine(); | 94 autofill_popup_view_->SelectPreviousLine(); |
| 86 EXPECT_EQ( | 95 EXPECT_EQ( |
| 87 static_cast<int>(autofill_popup_view_->autofill_values().size() - 1), | 96 static_cast<int>(autofill_popup_view_->autofill_values().size() - 1), |
| 88 autofill_popup_view_->selected_line()); | 97 autofill_popup_view_->selected_line()); |
| 89 | 98 |
| 90 // Test wrapping after the end. | 99 // Test wrapping after the end. |
| 91 autofill_popup_view_->SelectNextLine(); | 100 autofill_popup_view_->SelectNextLine(); |
| 92 EXPECT_EQ(0, autofill_popup_view_->selected_line()); | 101 EXPECT_EQ(0, autofill_popup_view_->selected_line()); |
| 93 } | 102 } |
| 94 | 103 |
| 95 TEST_F(AutofillPopupViewUnitTest, RedrawSelectedLine) { | 104 TEST_F(AutofillPopupViewUnitTest, RedrawSelectedLine) { |
| 105 // Set up the popup. |
| 106 std::vector<string16> autofill_values(2, string16()); |
| 107 std::vector<int> autofill_ids(2, 0); |
| 108 autofill_popup_view_->Show(autofill_values, autofill_values, autofill_values, |
| 109 autofill_ids, 1); |
| 110 |
| 96 // Make sure that when a new line is selected, it is invalidated so it can | 111 // Make sure that when a new line is selected, it is invalidated so it can |
| 97 // be updated to show it is selected. | 112 // be updated to show it is selected. |
| 98 int selected_line = 0; | 113 int selected_line = 0; |
| 99 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)); | 114 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)); |
| 100 autofill_popup_view_->SetSelectedLine(selected_line); | 115 autofill_popup_view_->SetSelectedLine(selected_line); |
| 101 | 116 |
| 102 // Ensure that the row isn't invalidated if it didn't change. | 117 // Ensure that the row isn't invalidated if it didn't change. |
| 103 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)).Times(0); | 118 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)).Times(0); |
| 104 autofill_popup_view_->SetSelectedLine(selected_line); | 119 autofill_popup_view_->SetSelectedLine(selected_line); |
| 105 | 120 |
| 106 // Change back to no selection. | 121 // Change back to no selection. |
| 107 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)); | 122 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line)); |
| 108 autofill_popup_view_->SetSelectedLine(-1); | 123 autofill_popup_view_->SetSelectedLine(-1); |
| 109 } | 124 } |
| 125 |
| 126 TEST_F(AutofillPopupViewUnitTest, RemoveLine) { |
| 127 // Set up the popup. |
| 128 std::vector<string16> autofill_values(2, string16()); |
| 129 std::vector<int> autofill_ids(2, 0); |
| 130 autofill_popup_view_->Show(autofill_values, autofill_values, autofill_values, |
| 131 autofill_ids, 1); |
| 132 |
| 133 // To remove warnings. |
| 134 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(_)).Times(AtLeast(0)); |
| 135 |
| 136 // No line is selected so the removal should fail. |
| 137 EXPECT_FALSE(autofill_popup_view_->RemoveSelectedLine()); |
| 138 |
| 139 // Try to remove the last entry and ensure it fails (it is an option). |
| 140 autofill_popup_view_->SetSelectedLine( |
| 141 autofill_popup_view_->autofill_values().size() - 1); |
| 142 EXPECT_FALSE(autofill_popup_view_->RemoveSelectedLine()); |
| 143 EXPECT_LE(0, autofill_popup_view_->selected_line()); |
| 144 |
| 145 // Remove the first (and only) entry. The popup should then be hidden since |
| 146 // there are no Autofill entries left. |
| 147 EXPECT_CALL(*autofill_popup_view_, HideInternal()); |
| 148 autofill_popup_view_->SetSelectedLine(0); |
| 149 EXPECT_TRUE(autofill_popup_view_->RemoveSelectedLine()); |
| 150 } |
| OLD | NEW |