| 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/test_autofill_external_delegate.h" | 6 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
| 7 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" | 7 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
| 8 #include "testing/gmock/include/gmock/gmock.h" | 8 #include "testing/gmock/include/gmock/gmock.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
| 11 #include "ui/gfx/rect.h" | 11 #include "ui/gfx/rect.h" |
| 12 | 12 |
| 13 using ::testing::_; | 13 using ::testing::_; |
| 14 using ::testing::AtLeast; | 14 using ::testing::AtLeast; |
| 15 using WebKit::WebAutofillClient; | 15 using WebKit::WebAutofillClient; |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 class MockAutofillExternalDelegate : | 19 class MockAutofillExternalDelegate : |
| 20 public autofill::TestAutofillExternalDelegate { | 20 public autofill::TestAutofillExternalDelegate { |
| 21 public: | 21 public: |
| 22 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; | 22 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; |
| 23 virtual ~MockAutofillExternalDelegate() {}; | 23 virtual ~MockAutofillExternalDelegate() {}; |
| 24 | 24 |
| 25 virtual void SelectAutofillSuggestion(int unique_id) | 25 virtual void DidSelectSuggestion(int identifier) OVERRIDE {} |
| 26 OVERRIDE {} | 26 virtual void RemoveSuggestion(const string16& value, int identifier) OVERRIDE |
| 27 virtual void RemoveAutocompleteEntry(const string16& value) OVERRIDE {} | 27 {} |
| 28 virtual void RemoveAutofillProfileOrCreditCard(int unique_id) OVERRIDE {} | |
| 29 virtual void ClearPreviewedForm() OVERRIDE {} | 28 virtual void ClearPreviewedForm() OVERRIDE {} |
| 30 | 29 |
| 31 MOCK_METHOD0(ControllerDestroyed, void()); | 30 MOCK_METHOD0(ControllerDestroyed, void()); |
| 32 }; | 31 }; |
| 33 | 32 |
| 34 class TestAutofillPopupController : public AutofillPopupControllerImpl { | 33 class TestAutofillPopupController : public AutofillPopupControllerImpl { |
| 35 public: | 34 public: |
| 36 explicit TestAutofillPopupController( | 35 explicit TestAutofillPopupController( |
| 37 AutofillExternalDelegate* external_delegate) | 36 AutofillExternalDelegate* external_delegate) |
| 38 : AutofillPopupControllerImpl(external_delegate, NULL, gfx::Rect()) {} | 37 : AutofillPopupControllerImpl(external_delegate, NULL, gfx::Rect()) {} |
| 39 virtual ~TestAutofillPopupController() {} | 38 virtual ~TestAutofillPopupController() {} |
| 40 | 39 |
| 41 // Making protected functions public for testing | 40 // Making protected functions public for testing |
| 42 const std::vector<string16>& autofill_values() const { | 41 const std::vector<string16>& subtexts() const { |
| 43 return AutofillPopupControllerImpl::autofill_values(); | 42 return AutofillPopupControllerImpl::subtexts(); |
| 44 } | 43 } |
| 45 int selected_line() const { | 44 int selected_line() const { |
| 46 return AutofillPopupControllerImpl::selected_line(); | 45 return AutofillPopupControllerImpl::selected_line(); |
| 47 } | 46 } |
| 48 void SetSelectedLine(size_t selected_line) { | 47 void SetSelectedLine(size_t selected_line) { |
| 49 AutofillPopupControllerImpl::SetSelectedLine(selected_line); | 48 AutofillPopupControllerImpl::SetSelectedLine(selected_line); |
| 50 } | 49 } |
| 51 void SelectNextLine() { | 50 void SelectNextLine() { |
| 52 AutofillPopupControllerImpl::SelectNextLine(); | 51 AutofillPopupControllerImpl::SelectNextLine(); |
| 53 } | 52 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 EXPECT_CALL(*autofill_popup_controller_, | 99 EXPECT_CALL(*autofill_popup_controller_, |
| 101 UpdateBoundsAndRedrawPopup()); | 100 UpdateBoundsAndRedrawPopup()); |
| 102 | 101 |
| 103 popup_controller()->SetPopupBounds(popup_bounds); | 102 popup_controller()->SetPopupBounds(popup_bounds); |
| 104 | 103 |
| 105 EXPECT_EQ(popup_bounds, popup_controller()->popup_bounds()); | 104 EXPECT_EQ(popup_bounds, popup_controller()->popup_bounds()); |
| 106 } | 105 } |
| 107 | 106 |
| 108 TEST_F(AutofillPopupControllerUnitTest, ChangeSelectedLine) { | 107 TEST_F(AutofillPopupControllerUnitTest, ChangeSelectedLine) { |
| 109 // Set up the popup. | 108 // Set up the popup. |
| 110 std::vector<string16> autofill_values(2, string16()); | 109 std::vector<string16> names(2, string16()); |
| 111 std::vector<int> autofill_ids(2, 0); | 110 std::vector<int> autofill_ids(2, 0); |
| 112 autofill_popup_controller_->Show( | 111 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 113 autofill_values, autofill_values, autofill_values, autofill_ids); | |
| 114 | 112 |
| 115 EXPECT_LT(autofill_popup_controller_->selected_line(), 0); | 113 EXPECT_LT(autofill_popup_controller_->selected_line(), 0); |
| 116 // Check that there are at least 2 values so that the first and last selection | 114 // Check that there are at least 2 values so that the first and last selection |
| 117 // are different. | 115 // are different. |
| 118 EXPECT_GE(2, | 116 EXPECT_GE(2, |
| 119 static_cast<int>(autofill_popup_controller_->autofill_values().size())); | 117 static_cast<int>(autofill_popup_controller_->subtexts().size())); |
| 120 | 118 |
| 121 // Test wrapping before the front. | 119 // Test wrapping before the front. |
| 122 autofill_popup_controller_->SelectPreviousLine(); | 120 autofill_popup_controller_->SelectPreviousLine(); |
| 123 EXPECT_EQ(static_cast<int>( | 121 EXPECT_EQ(static_cast<int>( |
| 124 autofill_popup_controller_->autofill_values().size() - 1), | 122 autofill_popup_controller_->subtexts().size() - 1), |
| 125 autofill_popup_controller_->selected_line()); | 123 autofill_popup_controller_->selected_line()); |
| 126 | 124 |
| 127 // Test wrapping after the end. | 125 // Test wrapping after the end. |
| 128 autofill_popup_controller_->SelectNextLine(); | 126 autofill_popup_controller_->SelectNextLine(); |
| 129 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); | 127 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); |
| 130 } | 128 } |
| 131 | 129 |
| 132 TEST_F(AutofillPopupControllerUnitTest, RedrawSelectedLine) { | 130 TEST_F(AutofillPopupControllerUnitTest, RedrawSelectedLine) { |
| 133 // Set up the popup. | 131 // Set up the popup. |
| 134 std::vector<string16> autofill_values(2, string16()); | 132 std::vector<string16> names(2, string16()); |
| 135 std::vector<int> autofill_ids(2, 0); | 133 std::vector<int> autofill_ids(2, 0); |
| 136 autofill_popup_controller_->Show( | 134 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 137 autofill_values, autofill_values, autofill_values, | |
| 138 autofill_ids); | |
| 139 | 135 |
| 140 // Make sure that when a new line is selected, it is invalidated so it can | 136 // Make sure that when a new line is selected, it is invalidated so it can |
| 141 // be updated to show it is selected. | 137 // be updated to show it is selected. |
| 142 int selected_line = 0; | 138 int selected_line = 0; |
| 143 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); | 139 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); |
| 144 autofill_popup_controller_->SetSelectedLine(selected_line); | 140 autofill_popup_controller_->SetSelectedLine(selected_line); |
| 145 | 141 |
| 146 // Ensure that the row isn't invalidated if it didn't change. | 142 // Ensure that the row isn't invalidated if it didn't change. |
| 147 EXPECT_CALL(*autofill_popup_controller_, | 143 EXPECT_CALL(*autofill_popup_controller_, |
| 148 InvalidateRow(selected_line)).Times(0); | 144 InvalidateRow(selected_line)).Times(0); |
| 149 autofill_popup_controller_->SetSelectedLine(selected_line); | 145 autofill_popup_controller_->SetSelectedLine(selected_line); |
| 150 | 146 |
| 151 // Change back to no selection. | 147 // Change back to no selection. |
| 152 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); | 148 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); |
| 153 autofill_popup_controller_->SetSelectedLine(-1); | 149 autofill_popup_controller_->SetSelectedLine(-1); |
| 154 } | 150 } |
| 155 | 151 |
| 156 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) { | 152 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) { |
| 157 // Set up the popup. | 153 // Set up the popup. |
| 158 std::vector<string16> autofill_values(3, string16()); | 154 std::vector<string16> names(3, string16()); |
| 159 std::vector<int> autofill_ids; | 155 std::vector<int> autofill_ids; |
| 160 autofill_ids.push_back(1); | 156 autofill_ids.push_back(1); |
| 161 autofill_ids.push_back(1); | 157 autofill_ids.push_back(1); |
| 162 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); | 158 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); |
| 163 autofill_popup_controller_->Show( | 159 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 164 autofill_values, autofill_values, autofill_values, autofill_ids); | |
| 165 | 160 |
| 166 // Generate a popup, so it can be hidden later. It doesn't matter what the | 161 // Generate a popup, so it can be hidden later. It doesn't matter what the |
| 167 // external_delegate thinks is being shown in the process, since we are just | 162 // external_delegate thinks is being shown in the process, since we are just |
| 168 // testing the popup here. | 163 // testing the popup here. |
| 169 autofill::GenerateTestAutofillPopup(&external_delegate_); | 164 autofill::GenerateTestAutofillPopup(&external_delegate_); |
| 170 | 165 |
| 171 // No line is selected so the removal should fail. | 166 // No line is selected so the removal should fail. |
| 172 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); | 167 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); |
| 173 | 168 |
| 174 // Try to remove the last entry and ensure it fails (it is an option). | 169 // Try to remove the last entry and ensure it fails (it is an option). |
| 175 autofill_popup_controller_->SetSelectedLine( | 170 autofill_popup_controller_->SetSelectedLine( |
| 176 autofill_popup_controller_->autofill_values().size() - 1); | 171 autofill_popup_controller_->subtexts().size() - 1); |
| 177 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); | 172 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); |
| 178 EXPECT_LE(0, autofill_popup_controller_->selected_line()); | 173 EXPECT_LE(0, autofill_popup_controller_->selected_line()); |
| 179 | 174 |
| 180 // Remove the first entry. The popup should be redrawn since its size has | 175 // Remove the first entry. The popup should be redrawn since its size has |
| 181 // changed. | 176 // changed. |
| 182 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); | 177 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); |
| 183 autofill_popup_controller_->SetSelectedLine(0); | 178 autofill_popup_controller_->SetSelectedLine(0); |
| 184 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 179 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); |
| 185 | 180 |
| 186 // Remove the last entry. The popup should then be hidden since there are | 181 // Remove the last entry. The popup should then be hidden since there are |
| 187 // no Autofill entries left. | 182 // no Autofill entries left. |
| 188 EXPECT_CALL(external_delegate_, ControllerDestroyed()); | 183 EXPECT_CALL(external_delegate_, ControllerDestroyed()); |
| 189 | 184 |
| 190 autofill_popup_controller_->SetSelectedLine(0); | 185 autofill_popup_controller_->SetSelectedLine(0); |
| 191 // The controller self-deletes here, don't double delete. | 186 // The controller self-deletes here, don't double delete. |
| 192 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 187 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); |
| 193 autofill_popup_controller_ = NULL; | 188 autofill_popup_controller_ = NULL; |
| 194 } | 189 } |
| 195 | 190 |
| 196 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { | 191 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { |
| 197 // Set up the popup. | 192 // Set up the popup. |
| 198 std::vector<string16> autofill_values(3, string16()); | 193 std::vector<string16> names(3, string16()); |
| 199 std::vector<int> autofill_ids; | 194 std::vector<int> autofill_ids; |
| 200 autofill_ids.push_back(1); | 195 autofill_ids.push_back(1); |
| 201 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator); | 196 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator); |
| 202 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); | 197 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); |
| 203 autofill_popup_controller_->Show( | 198 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 204 autofill_values, autofill_values, autofill_values, autofill_ids); | |
| 205 | 199 |
| 206 autofill_popup_controller_->SetSelectedLine(0); | 200 autofill_popup_controller_->SetSelectedLine(0); |
| 207 | 201 |
| 208 // Make sure next skips the unselectable separator. | 202 // Make sure next skips the unselectable separator. |
| 209 autofill_popup_controller_->SelectNextLine(); | 203 autofill_popup_controller_->SelectNextLine(); |
| 210 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); | 204 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); |
| 211 | 205 |
| 212 // Make sure previous skips the unselectable separator. | 206 // Make sure previous skips the unselectable separator. |
| 213 autofill_popup_controller_->SelectPreviousLine(); | 207 autofill_popup_controller_->SelectPreviousLine(); |
| 214 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); | 208 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 NULL, | 251 NULL, |
| 258 bounds); | 252 bounds); |
| 259 EXPECT_EQ( | 253 EXPECT_EQ( |
| 260 bounds, | 254 bounds, |
| 261 static_cast<AutofillPopupController*>(controller3)->element_bounds()); | 255 static_cast<AutofillPopupController*>(controller3)->element_bounds()); |
| 262 controller3->Hide(); | 256 controller3->Hide(); |
| 263 | 257 |
| 264 EXPECT_CALL(delegate, ControllerDestroyed()); | 258 EXPECT_CALL(delegate, ControllerDestroyed()); |
| 265 delete test_controller; | 259 delete test_controller; |
| 266 } | 260 } |
| OLD | NEW |