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