| 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/optional.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 13 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" | 14 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
| 14 #include "chrome/browser/ui/autofill/autofill_popup_view.h" | 15 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
| 15 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 16 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 16 #include "chrome/test/base/testing_profile.h" | 17 #include "chrome/test/base/testing_profile.h" |
| 17 #include "components/autofill/content/browser/content_autofill_driver.h" | 18 #include "components/autofill/content/browser/content_autofill_driver.h" |
| 18 #include "components/autofill/content/browser/content_autofill_driver_factory.h" | 19 #include "components/autofill/content/browser/content_autofill_driver_factory.h" |
| 19 #include "components/autofill/core/browser/autofill_external_delegate.h" | 20 #include "components/autofill/core/browser/autofill_external_delegate.h" |
| 20 #include "components/autofill/core/browser/autofill_manager.h" | 21 #include "components/autofill/core/browser/autofill_manager.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 ~MockAutofillClient() override {} | 63 ~MockAutofillClient() override {} |
| 63 | 64 |
| 64 PrefService* GetPrefs() override { return prefs_.get(); } | 65 PrefService* GetPrefs() override { return prefs_.get(); } |
| 65 | 66 |
| 66 private: | 67 private: |
| 67 std::unique_ptr<PrefService> prefs_; | 68 std::unique_ptr<PrefService> prefs_; |
| 68 | 69 |
| 69 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient); | 70 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient); |
| 70 }; | 71 }; |
| 71 | 72 |
| 73 class MockAutofillPopupView : public AutofillPopupView { |
| 74 public: |
| 75 MockAutofillPopupView() {} |
| 76 |
| 77 MOCK_METHOD0(Show, void()); |
| 78 MOCK_METHOD0(Hide, void()); |
| 79 MOCK_METHOD2(OnSelectedRowChanged, |
| 80 void(base::Optional<int> previous_row_selection, |
| 81 base::Optional<int> current_row_selection)); |
| 82 MOCK_METHOD0(OnSuggestionsChanged, void()); |
| 83 |
| 84 private: |
| 85 DISALLOW_COPY_AND_ASSIGN(MockAutofillPopupView); |
| 86 }; |
| 87 |
| 72 class TestAutofillPopupController : public AutofillPopupControllerImpl { | 88 class TestAutofillPopupController : public AutofillPopupControllerImpl { |
| 73 public: | 89 public: |
| 74 TestAutofillPopupController( | 90 TestAutofillPopupController( |
| 75 base::WeakPtr<AutofillExternalDelegate> external_delegate, | 91 base::WeakPtr<AutofillExternalDelegate> external_delegate, |
| 76 const gfx::RectF& element_bounds) | 92 const gfx::RectF& element_bounds) |
| 77 : AutofillPopupControllerImpl(external_delegate, | 93 : AutofillPopupControllerImpl(external_delegate, |
| 78 NULL, | 94 NULL, |
| 79 NULL, | 95 NULL, |
| 80 element_bounds, | 96 element_bounds, |
| 81 base::i18n::UNKNOWN_DIRECTION) {} | 97 base::i18n::UNKNOWN_DIRECTION) {} |
| 82 ~TestAutofillPopupController() override {} | 98 ~TestAutofillPopupController() override {} |
| 83 | 99 |
| 84 // Making protected functions public for testing | 100 // Making protected functions public for testing |
| 85 using AutofillPopupControllerImpl::GetLineCount; | 101 using AutofillPopupControllerImpl::GetLineCount; |
| 86 using AutofillPopupControllerImpl::GetSuggestionAt; | 102 using AutofillPopupControllerImpl::GetSuggestionAt; |
| 87 using AutofillPopupControllerImpl::GetElidedValueAt; | 103 using AutofillPopupControllerImpl::GetElidedValueAt; |
| 88 using AutofillPopupControllerImpl::GetElidedLabelAt; | 104 using AutofillPopupControllerImpl::GetElidedLabelAt; |
| 89 using AutofillPopupControllerImpl::selected_line; | 105 using AutofillPopupControllerImpl::selected_line; |
| 90 using AutofillPopupControllerImpl::SetSelectedLine; | 106 using AutofillPopupControllerImpl::SetSelectedLine; |
| 91 using AutofillPopupControllerImpl::SelectNextLine; | 107 using AutofillPopupControllerImpl::SelectNextLine; |
| 92 using AutofillPopupControllerImpl::SelectPreviousLine; | 108 using AutofillPopupControllerImpl::SelectPreviousLine; |
| 93 using AutofillPopupControllerImpl::RemoveSelectedLine; | 109 using AutofillPopupControllerImpl::RemoveSelectedLine; |
| 94 using AutofillPopupControllerImpl::popup_bounds; | 110 using AutofillPopupControllerImpl::popup_bounds; |
| 95 using AutofillPopupControllerImpl::element_bounds; | 111 using AutofillPopupControllerImpl::element_bounds; |
| 96 using AutofillPopupControllerImpl::SetValues; | 112 using AutofillPopupControllerImpl::SetValues; |
| 97 using AutofillPopupControllerImpl::GetWeakPtr; | 113 using AutofillPopupControllerImpl::GetWeakPtr; |
| 98 MOCK_METHOD1(InvalidateRow, void(size_t)); | 114 MOCK_METHOD0(OnSuggestionsChanged, void()); |
| 99 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void()); | |
| 100 MOCK_METHOD0(Hide, void()); | 115 MOCK_METHOD0(Hide, void()); |
| 101 | 116 |
| 102 void DoHide() { | 117 void DoHide() { |
| 103 AutofillPopupControllerImpl::Hide(); | 118 AutofillPopupControllerImpl::Hide(); |
| 104 } | 119 } |
| 120 }; |
| 105 | 121 |
| 106 private: | 122 static constexpr base::Optional<int> kNoSelection; |
| 107 void ShowView() override {} | |
| 108 }; | |
| 109 | 123 |
| 110 } // namespace | 124 } // namespace |
| 111 | 125 |
| 112 class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness { | 126 class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness { |
| 113 public: | 127 public: |
| 114 AutofillPopupControllerUnitTest() | 128 AutofillPopupControllerUnitTest() |
| 115 : autofill_client_(new MockAutofillClient()), | 129 : autofill_client_(new MockAutofillClient()), |
| 116 autofill_popup_controller_(NULL) {} | 130 autofill_popup_controller_(NULL) {} |
| 117 ~AutofillPopupControllerUnitTest() override {} | 131 ~AutofillPopupControllerUnitTest() override {} |
| 118 | 132 |
| 119 void SetUp() override { | 133 void SetUp() override { |
| 120 ChromeRenderViewHostTestHarness::SetUp(); | 134 ChromeRenderViewHostTestHarness::SetUp(); |
| 121 | 135 |
| 122 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate( | 136 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate( |
| 123 web_contents(), autofill_client_.get(), "en-US", | 137 web_contents(), autofill_client_.get(), "en-US", |
| 124 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER); | 138 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER); |
| 125 // Make sure RenderFrame is created. | 139 // Make sure RenderFrame is created. |
| 126 NavigateAndCommit(GURL("about:blank")); | 140 NavigateAndCommit(GURL("about:blank")); |
| 127 ContentAutofillDriverFactory* factory = | 141 ContentAutofillDriverFactory* factory = |
| 128 ContentAutofillDriverFactory::FromWebContents(web_contents()); | 142 ContentAutofillDriverFactory::FromWebContents(web_contents()); |
| 129 ContentAutofillDriver* driver = | 143 ContentAutofillDriver* driver = |
| 130 factory->DriverForFrame(web_contents()->GetMainFrame()); | 144 factory->DriverForFrame(web_contents()->GetMainFrame()); |
| 131 external_delegate_.reset( | 145 external_delegate_.reset( |
| 132 new NiceMock<MockAutofillExternalDelegate>( | 146 new NiceMock<MockAutofillExternalDelegate>( |
| 133 driver->autofill_manager(), | 147 driver->autofill_manager(), |
| 134 driver)); | 148 driver)); |
| 135 | 149 autofill_popup_view_.reset(new NiceMock<MockAutofillPopupView>()); |
| 136 autofill_popup_controller_ = | 150 autofill_popup_controller_ = new NiceMock<TestAutofillPopupController>( |
| 137 new testing::NiceMock<TestAutofillPopupController>( | 151 external_delegate_->GetWeakPtr(), gfx::RectF()); |
| 138 external_delegate_->GetWeakPtr(), gfx::RectF()); | 152 autofill_popup_controller_->SetViewForTesting(autofill_popup_view()); |
| 139 } | 153 } |
| 140 | 154 |
| 141 void TearDown() override { | 155 void TearDown() override { |
| 142 // This will make sure the controller and the view (if any) are both | 156 // This will make sure the controller and the view (if any) are both |
| 143 // cleaned up. | 157 // cleaned up. |
| 144 if (autofill_popup_controller_) | 158 if (autofill_popup_controller_) |
| 145 autofill_popup_controller_->DoHide(); | 159 autofill_popup_controller_->DoHide(); |
| 146 | 160 |
| 147 external_delegate_.reset(); | 161 external_delegate_.reset(); |
| 148 ChromeRenderViewHostTestHarness::TearDown(); | 162 ChromeRenderViewHostTestHarness::TearDown(); |
| 149 } | 163 } |
| 150 | 164 |
| 151 TestAutofillPopupController* popup_controller() { | 165 TestAutofillPopupController* popup_controller() { |
| 152 return autofill_popup_controller_; | 166 return autofill_popup_controller_; |
| 153 } | 167 } |
| 154 | 168 |
| 155 MockAutofillExternalDelegate* delegate() { | 169 MockAutofillExternalDelegate* delegate() { |
| 156 return external_delegate_.get(); | 170 return external_delegate_.get(); |
| 157 } | 171 } |
| 158 | 172 |
| 173 MockAutofillPopupView* autofill_popup_view() { |
| 174 return autofill_popup_view_.get(); |
| 175 } |
| 176 |
| 159 protected: | 177 protected: |
| 160 std::unique_ptr<MockAutofillClient> autofill_client_; | 178 std::unique_ptr<MockAutofillClient> autofill_client_; |
| 161 std::unique_ptr<NiceMock<MockAutofillExternalDelegate>> external_delegate_; | 179 std::unique_ptr<NiceMock<MockAutofillExternalDelegate>> external_delegate_; |
| 162 testing::NiceMock<TestAutofillPopupController>* autofill_popup_controller_; | 180 std::unique_ptr<NiceMock<MockAutofillPopupView>> autofill_popup_view_; |
| 181 NiceMock<TestAutofillPopupController>* autofill_popup_controller_; |
| 163 }; | 182 }; |
| 164 | 183 |
| 165 TEST_F(AutofillPopupControllerUnitTest, ChangeSelectedLine) { | 184 TEST_F(AutofillPopupControllerUnitTest, ChangeSelectedLine) { |
| 166 // Set up the popup. | 185 // Set up the popup. |
| 167 std::vector<Suggestion> suggestions; | 186 std::vector<Suggestion> suggestions; |
| 168 suggestions.push_back(Suggestion("", "", "", 0)); | 187 suggestions.push_back(Suggestion("", "", "", 0)); |
| 169 suggestions.push_back(Suggestion("", "", "", 0)); | 188 suggestions.push_back(Suggestion("", "", "", 0)); |
| 170 autofill_popup_controller_->Show(suggestions); | 189 autofill_popup_controller_->Show(suggestions); |
| 171 | 190 |
| 172 EXPECT_LT(autofill_popup_controller_->selected_line(), 0); | 191 EXPECT_FALSE(autofill_popup_controller_->selected_line()); |
| 173 // Check that there are at least 2 values so that the first and last selection | 192 // Check that there are at least 2 values so that the first and last selection |
| 174 // are different. | 193 // are different. |
| 175 EXPECT_GE(2, | 194 EXPECT_GE(2, |
| 176 static_cast<int>(autofill_popup_controller_->GetLineCount())); | 195 static_cast<int>(autofill_popup_controller_->GetLineCount())); |
| 177 | 196 |
| 178 // Test wrapping before the front. | 197 // Test wrapping before the front. |
| 179 autofill_popup_controller_->SelectPreviousLine(); | 198 autofill_popup_controller_->SelectPreviousLine(); |
| 180 EXPECT_EQ(static_cast<int>( | 199 EXPECT_EQ(autofill_popup_controller_->GetLineCount() - 1, |
| 181 autofill_popup_controller_->GetLineCount() - 1), | 200 autofill_popup_controller_->selected_line().value()); |
| 182 autofill_popup_controller_->selected_line()); | |
| 183 | 201 |
| 184 // Test wrapping after the end. | 202 // Test wrapping after the end. |
| 185 autofill_popup_controller_->SelectNextLine(); | 203 autofill_popup_controller_->SelectNextLine(); |
| 186 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); | 204 EXPECT_EQ(0, *autofill_popup_controller_->selected_line()); |
| 187 } | 205 } |
| 188 | 206 |
| 189 TEST_F(AutofillPopupControllerUnitTest, RedrawSelectedLine) { | 207 TEST_F(AutofillPopupControllerUnitTest, RedrawSelectedLine) { |
| 190 // Set up the popup. | 208 // Set up the popup. |
| 191 std::vector<Suggestion> suggestions; | 209 std::vector<Suggestion> suggestions; |
| 192 suggestions.push_back(Suggestion("", "", "", 0)); | 210 suggestions.push_back(Suggestion("", "", "", 0)); |
| 193 suggestions.push_back(Suggestion("", "", "", 0)); | 211 suggestions.push_back(Suggestion("", "", "", 0)); |
| 194 autofill_popup_controller_->Show(suggestions); | 212 autofill_popup_controller_->Show(suggestions); |
| 195 | 213 |
| 196 // Make sure that when a new line is selected, it is invalidated so it can | 214 // Make sure that when a new line is selected, it is invalidated so it can |
| 197 // be updated to show it is selected. | 215 // be updated to show it is selected. |
| 198 int selected_line = 0; | 216 base::Optional<int> selected_line = 0; |
| 199 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); | 217 EXPECT_CALL(*autofill_popup_view_, |
| 218 OnSelectedRowChanged(kNoSelection, selected_line)); |
| 219 |
| 200 autofill_popup_controller_->SetSelectedLine(selected_line); | 220 autofill_popup_controller_->SetSelectedLine(selected_line); |
| 201 | 221 |
| 202 // Ensure that the row isn't invalidated if it didn't change. | 222 // Ensure that the row isn't invalidated if it didn't change. |
| 203 EXPECT_CALL(*autofill_popup_controller_, | 223 EXPECT_CALL(*autofill_popup_view_, OnSelectedRowChanged(_, _)).Times(0); |
| 204 InvalidateRow(selected_line)).Times(0); | |
| 205 autofill_popup_controller_->SetSelectedLine(selected_line); | 224 autofill_popup_controller_->SetSelectedLine(selected_line); |
| 206 | 225 |
| 207 // Change back to no selection. | 226 // Change back to no selection. |
| 208 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); | 227 EXPECT_CALL(*autofill_popup_view_, |
| 209 autofill_popup_controller_->SetSelectedLine(-1); | 228 OnSelectedRowChanged(selected_line, kNoSelection)); |
| 229 |
| 230 autofill_popup_controller_->SetSelectedLine(kNoSelection); |
| 210 } | 231 } |
| 211 | 232 |
| 212 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) { | 233 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) { |
| 213 // Set up the popup. | 234 // Set up the popup. |
| 214 std::vector<Suggestion> suggestions; | 235 std::vector<Suggestion> suggestions; |
| 215 suggestions.push_back(Suggestion("", "", "", 1)); | 236 suggestions.push_back(Suggestion("", "", "", 1)); |
| 216 suggestions.push_back(Suggestion("", "", "", 1)); | 237 suggestions.push_back(Suggestion("", "", "", 1)); |
| 217 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_AUTOFILL_OPTIONS)); | 238 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_AUTOFILL_OPTIONS)); |
| 218 autofill_popup_controller_->Show(suggestions); | 239 autofill_popup_controller_->Show(suggestions); |
| 219 | 240 |
| 220 // Generate a popup, so it can be hidden later. It doesn't matter what the | 241 // Generate a popup, so it can be hidden later. It doesn't matter what the |
| 221 // external_delegate thinks is being shown in the process, since we are just | 242 // external_delegate thinks is being shown in the process, since we are just |
| 222 // testing the popup here. | 243 // testing the popup here. |
| 223 autofill::GenerateTestAutofillPopup(external_delegate_.get()); | 244 autofill::GenerateTestAutofillPopup(external_delegate_.get()); |
| 224 | 245 |
| 225 // No line is selected so the removal should fail. | 246 // No line is selected so the removal should fail. |
| 226 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); | 247 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); |
| 227 | 248 |
| 228 // Remove the first entry. The popup should be redrawn since its size has | 249 // Remove the first entry. The popup should be redrawn since its size has |
| 229 // changed. | 250 // changed. |
| 230 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); | 251 EXPECT_CALL(*autofill_popup_controller_, OnSuggestionsChanged()); |
| 231 autofill_popup_controller_->SetSelectedLine(0); | 252 autofill_popup_controller_->SetSelectedLine(0); |
| 232 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 253 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); |
| 233 | 254 |
| 234 // Remove the last entry. The popup should then be hidden since there are | 255 // Remove the last entry. The popup should then be hidden since there are |
| 235 // no Autofill entries left. | 256 // no Autofill entries left. |
| 236 EXPECT_CALL(*autofill_popup_controller_, Hide()); | 257 EXPECT_CALL(*autofill_popup_controller_, Hide()); |
| 237 autofill_popup_controller_->SetSelectedLine(0); | 258 autofill_popup_controller_->SetSelectedLine(0); |
| 238 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 259 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); |
| 239 } | 260 } |
| 240 | 261 |
| 241 TEST_F(AutofillPopupControllerUnitTest, RemoveOnlyLine) { | 262 TEST_F(AutofillPopupControllerUnitTest, RemoveOnlyLine) { |
| 242 // Set up the popup. | 263 // Set up the popup. |
| 243 std::vector<Suggestion> suggestions; | 264 std::vector<Suggestion> suggestions; |
| 244 suggestions.push_back(Suggestion("", "", "", 1)); | 265 suggestions.push_back(Suggestion("", "", "", 1)); |
| 245 autofill_popup_controller_->Show(suggestions); | 266 autofill_popup_controller_->Show(suggestions); |
| 246 | 267 |
| 247 // Generate a popup. | 268 // Generate a popup. |
| 248 autofill::GenerateTestAutofillPopup(external_delegate_.get()); | 269 autofill::GenerateTestAutofillPopup(external_delegate_.get()); |
| 249 | 270 |
| 250 // Select the only line. | 271 // Select the only line. |
| 251 autofill_popup_controller_->SetSelectedLine(0); | 272 base::Optional<int> selected_line(0); |
| 273 autofill_popup_controller_->SetSelectedLine(selected_line); |
| 274 EXPECT_CALL(*autofill_popup_view_, |
| 275 OnSelectedRowChanged(kNoSelection, selected_line)) |
| 276 .Times(0); |
| 252 | 277 |
| 253 // Remove the only line. There should be no row invalidation and the popup | 278 // Remove the only line. The popup should then be hidden since there are no |
| 254 // should then be hidden since there are no Autofill entries left. | 279 // Autofill entries left. |
| 255 EXPECT_CALL(*autofill_popup_controller_, Hide()); | 280 EXPECT_CALL(*autofill_popup_controller_, Hide()); |
| 256 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(_)).Times(0); | 281 EXPECT_CALL(*autofill_popup_view_, |
| 282 OnSelectedRowChanged(selected_line, kNoSelection)); |
| 257 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 283 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); |
| 258 } | 284 } |
| 259 | 285 |
| 260 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { | 286 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { |
| 261 // Set up the popup. | 287 // Set up the popup. |
| 262 std::vector<Suggestion> suggestions; | 288 std::vector<Suggestion> suggestions; |
| 263 suggestions.push_back(Suggestion("", "", "", 1)); | 289 suggestions.push_back(Suggestion("", "", "", 1)); |
| 264 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_SEPARATOR)); | 290 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_SEPARATOR)); |
| 265 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_AUTOFILL_OPTIONS)); | 291 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_AUTOFILL_OPTIONS)); |
| 266 autofill_popup_controller_->Show(suggestions); | 292 autofill_popup_controller_->Show(suggestions); |
| 267 | 293 |
| 268 autofill_popup_controller_->SetSelectedLine(0); | 294 autofill_popup_controller_->SetSelectedLine(0); |
| 269 | 295 |
| 270 // Make sure next skips the unselectable separator. | 296 // Make sure next skips the unselectable separator. |
| 271 autofill_popup_controller_->SelectNextLine(); | 297 autofill_popup_controller_->SelectNextLine(); |
| 272 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); | 298 EXPECT_EQ(2, *autofill_popup_controller_->selected_line()); |
| 273 | 299 |
| 274 // Make sure previous skips the unselectable separator. | 300 // Make sure previous skips the unselectable separator. |
| 275 autofill_popup_controller_->SelectPreviousLine(); | 301 autofill_popup_controller_->SelectPreviousLine(); |
| 276 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); | 302 EXPECT_EQ(0, *autofill_popup_controller_->selected_line()); |
| 303 } |
| 304 |
| 305 TEST_F(AutofillPopupControllerUnitTest, SkipInsecureFormWarning) { |
| 306 std::vector<Suggestion> suggestions; |
| 307 suggestions.push_back(Suggestion("", "", "", 1)); |
| 308 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_SEPARATOR)); |
| 309 suggestions.push_back(Suggestion( |
| 310 "", "", "", POPUP_ITEM_ID_INSECURE_CONTEXT_PAYMENT_DISABLED_MESSAGE)); |
| 311 autofill_popup_controller_->Show(suggestions); |
| 312 |
| 313 // Make sure previous skips the unselectable form warning when there is no |
| 314 // selection. |
| 315 autofill_popup_controller_->SelectPreviousLine(); |
| 316 EXPECT_FALSE(autofill_popup_controller_->selected_line()); |
| 317 |
| 318 autofill_popup_controller_->SetSelectedLine(0); |
| 319 EXPECT_EQ(0, *autofill_popup_controller_->selected_line()); |
| 320 |
| 321 // Make sure previous skips the unselectable form warning when there is a |
| 322 // selection. |
| 323 autofill_popup_controller_->SelectPreviousLine(); |
| 324 EXPECT_FALSE(autofill_popup_controller_->selected_line()); |
| 277 } | 325 } |
| 278 | 326 |
| 279 TEST_F(AutofillPopupControllerUnitTest, UpdateDataListValues) { | 327 TEST_F(AutofillPopupControllerUnitTest, UpdateDataListValues) { |
| 280 std::vector<Suggestion> suggestions; | 328 std::vector<Suggestion> suggestions; |
| 281 suggestions.push_back(Suggestion("", "", "", 1)); | 329 suggestions.push_back(Suggestion("", "", "", 1)); |
| 282 autofill_popup_controller_->Show(suggestions); | 330 autofill_popup_controller_->Show(suggestions); |
| 283 | 331 |
| 284 // Add one data list entry. | 332 // Add one data list entry. |
| 285 base::string16 value1 = ASCIIToUTF16("data list value 1"); | 333 base::string16 value1 = ASCIIToUTF16("data list value 1"); |
| 286 std::vector<base::string16> data_list_values; | 334 std::vector<base::string16> data_list_values; |
| 287 data_list_values.push_back(value1); | 335 data_list_values.push_back(value1); |
| 288 | 336 |
| 289 autofill_popup_controller_->UpdateDataListValues(data_list_values, | 337 autofill_popup_controller_->UpdateDataListValues(data_list_values, |
| 290 data_list_values); | 338 data_list_values); |
| 291 | 339 |
| 292 ASSERT_EQ(3u, autofill_popup_controller_->GetLineCount()); | 340 ASSERT_EQ(3, autofill_popup_controller_->GetLineCount()); |
| 293 | 341 |
| 294 Suggestion result0 = autofill_popup_controller_->GetSuggestionAt(0); | 342 Suggestion result0 = autofill_popup_controller_->GetSuggestionAt(0); |
| 295 EXPECT_EQ(value1, result0.value); | 343 EXPECT_EQ(value1, result0.value); |
| 296 EXPECT_EQ(value1, autofill_popup_controller_->GetElidedValueAt(0)); | 344 EXPECT_EQ(value1, autofill_popup_controller_->GetElidedValueAt(0)); |
| 297 EXPECT_EQ(value1, result0.label); | 345 EXPECT_EQ(value1, result0.label); |
| 298 EXPECT_EQ(value1, autofill_popup_controller_->GetElidedLabelAt(0)); | 346 EXPECT_EQ(value1, autofill_popup_controller_->GetElidedLabelAt(0)); |
| 299 EXPECT_EQ(POPUP_ITEM_ID_DATALIST_ENTRY, result0.frontend_id); | 347 EXPECT_EQ(POPUP_ITEM_ID_DATALIST_ENTRY, result0.frontend_id); |
| 300 | 348 |
| 301 Suggestion result1 = autofill_popup_controller_->GetSuggestionAt(1); | 349 Suggestion result1 = autofill_popup_controller_->GetSuggestionAt(1); |
| 302 EXPECT_EQ(base::string16(), result1.value); | 350 EXPECT_EQ(base::string16(), result1.value); |
| 303 EXPECT_EQ(base::string16(), result1.label); | 351 EXPECT_EQ(base::string16(), result1.label); |
| 304 EXPECT_EQ(POPUP_ITEM_ID_SEPARATOR, result1.frontend_id); | 352 EXPECT_EQ(POPUP_ITEM_ID_SEPARATOR, result1.frontend_id); |
| 305 | 353 |
| 306 Suggestion result2 = autofill_popup_controller_->GetSuggestionAt(2); | 354 Suggestion result2 = autofill_popup_controller_->GetSuggestionAt(2); |
| 307 EXPECT_EQ(base::string16(), result2.value); | 355 EXPECT_EQ(base::string16(), result2.value); |
| 308 EXPECT_EQ(base::string16(), result2.label); | 356 EXPECT_EQ(base::string16(), result2.label); |
| 309 EXPECT_EQ(1, result2.frontend_id); | 357 EXPECT_EQ(1, result2.frontend_id); |
| 310 | 358 |
| 311 // Add two data list entries (which should replace the current one). | 359 // Add two data list entries (which should replace the current one). |
| 312 base::string16 value2 = ASCIIToUTF16("data list value 2"); | 360 base::string16 value2 = ASCIIToUTF16("data list value 2"); |
| 313 data_list_values.push_back(value2); | 361 data_list_values.push_back(value2); |
| 314 | 362 |
| 315 autofill_popup_controller_->UpdateDataListValues(data_list_values, | 363 autofill_popup_controller_->UpdateDataListValues(data_list_values, |
| 316 data_list_values); | 364 data_list_values); |
| 317 ASSERT_EQ(4u, autofill_popup_controller_->GetLineCount()); | 365 ASSERT_EQ(4, autofill_popup_controller_->GetLineCount()); |
| 318 | 366 |
| 319 // Original one first, followed by new one, then separator. | 367 // Original one first, followed by new one, then separator. |
| 320 EXPECT_EQ(value1, autofill_popup_controller_->GetSuggestionAt(0).value); | 368 EXPECT_EQ(value1, autofill_popup_controller_->GetSuggestionAt(0).value); |
| 321 EXPECT_EQ(value1, autofill_popup_controller_->GetElidedValueAt(0)); | 369 EXPECT_EQ(value1, autofill_popup_controller_->GetElidedValueAt(0)); |
| 322 EXPECT_EQ(value2, autofill_popup_controller_->GetSuggestionAt(1).value); | 370 EXPECT_EQ(value2, autofill_popup_controller_->GetSuggestionAt(1).value); |
| 323 EXPECT_EQ(value2, autofill_popup_controller_->GetElidedValueAt(1)); | 371 EXPECT_EQ(value2, autofill_popup_controller_->GetElidedValueAt(1)); |
| 324 EXPECT_EQ(POPUP_ITEM_ID_SEPARATOR, | 372 EXPECT_EQ(POPUP_ITEM_ID_SEPARATOR, |
| 325 autofill_popup_controller_->GetSuggestionAt(2).frontend_id); | 373 autofill_popup_controller_->GetSuggestionAt(2).frontend_id); |
| 326 | 374 |
| 327 // Clear all data list values. | 375 // Clear all data list values. |
| 328 data_list_values.clear(); | 376 data_list_values.clear(); |
| 329 autofill_popup_controller_->UpdateDataListValues(data_list_values, | 377 autofill_popup_controller_->UpdateDataListValues(data_list_values, |
| 330 data_list_values); | 378 data_list_values); |
| 331 | 379 |
| 332 ASSERT_EQ(1u, autofill_popup_controller_->GetLineCount()); | 380 ASSERT_EQ(1, autofill_popup_controller_->GetLineCount()); |
| 333 EXPECT_EQ(1, autofill_popup_controller_->GetSuggestionAt(0).frontend_id); | 381 EXPECT_EQ(1, autofill_popup_controller_->GetSuggestionAt(0).frontend_id); |
| 334 } | 382 } |
| 335 | 383 |
| 336 TEST_F(AutofillPopupControllerUnitTest, PopupsWithOnlyDataLists) { | 384 TEST_F(AutofillPopupControllerUnitTest, PopupsWithOnlyDataLists) { |
| 337 // Create the popup with a single datalist element. | 385 // Create the popup with a single datalist element. |
| 338 std::vector<Suggestion> suggestions; | 386 std::vector<Suggestion> suggestions; |
| 339 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_DATALIST_ENTRY)); | 387 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_DATALIST_ENTRY)); |
| 340 autofill_popup_controller_->Show(suggestions); | 388 autofill_popup_controller_->Show(suggestions); |
| 341 | 389 |
| 342 // Replace the datalist element with a new one. | 390 // Replace the datalist element with a new one. |
| 343 base::string16 value1 = ASCIIToUTF16("data list value 1"); | 391 base::string16 value1 = ASCIIToUTF16("data list value 1"); |
| 344 std::vector<base::string16> data_list_values; | 392 std::vector<base::string16> data_list_values; |
| 345 data_list_values.push_back(value1); | 393 data_list_values.push_back(value1); |
| 346 | 394 |
| 347 autofill_popup_controller_->UpdateDataListValues(data_list_values, | 395 autofill_popup_controller_->UpdateDataListValues(data_list_values, |
| 348 data_list_values); | 396 data_list_values); |
| 349 | 397 |
| 350 ASSERT_EQ(1u, autofill_popup_controller_->GetLineCount()); | 398 ASSERT_EQ(1, autofill_popup_controller_->GetLineCount()); |
| 351 EXPECT_EQ(value1, autofill_popup_controller_->GetSuggestionAt(0).value); | 399 EXPECT_EQ(value1, autofill_popup_controller_->GetSuggestionAt(0).value); |
| 352 EXPECT_EQ(POPUP_ITEM_ID_DATALIST_ENTRY, | 400 EXPECT_EQ(POPUP_ITEM_ID_DATALIST_ENTRY, |
| 353 autofill_popup_controller_->GetSuggestionAt(0).frontend_id); | 401 autofill_popup_controller_->GetSuggestionAt(0).frontend_id); |
| 354 | 402 |
| 355 // Clear datalist values and check that the popup becomes hidden. | 403 // Clear datalist values and check that the popup becomes hidden. |
| 356 EXPECT_CALL(*autofill_popup_controller_, Hide()); | 404 EXPECT_CALL(*autofill_popup_controller_, Hide()); |
| 357 data_list_values.clear(); | 405 data_list_values.clear(); |
| 358 autofill_popup_controller_->UpdateDataListValues(data_list_values, | 406 autofill_popup_controller_->UpdateDataListValues(data_list_values, |
| 359 data_list_values); | 407 data_list_values); |
| 360 } | 408 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 379 gfx::RectF(), base::i18n::UNKNOWN_DIRECTION); | 427 gfx::RectF(), base::i18n::UNKNOWN_DIRECTION); |
| 380 EXPECT_TRUE(controller.get()); | 428 EXPECT_TRUE(controller.get()); |
| 381 | 429 |
| 382 WeakPtr<AutofillPopupControllerImpl> controller2 = | 430 WeakPtr<AutofillPopupControllerImpl> controller2 = |
| 383 AutofillPopupControllerImpl::GetOrCreate( | 431 AutofillPopupControllerImpl::GetOrCreate( |
| 384 controller, delegate.GetWeakPtr(), NULL, NULL, gfx::RectF(), | 432 controller, delegate.GetWeakPtr(), NULL, NULL, gfx::RectF(), |
| 385 base::i18n::UNKNOWN_DIRECTION); | 433 base::i18n::UNKNOWN_DIRECTION); |
| 386 EXPECT_EQ(controller.get(), controller2.get()); | 434 EXPECT_EQ(controller.get(), controller2.get()); |
| 387 controller->Hide(); | 435 controller->Hide(); |
| 388 | 436 |
| 389 testing::NiceMock<TestAutofillPopupController>* test_controller = | 437 NiceMock<TestAutofillPopupController>* test_controller = |
| 390 new testing::NiceMock<TestAutofillPopupController>(delegate.GetWeakPtr(), | 438 new NiceMock<TestAutofillPopupController>(delegate.GetWeakPtr(), |
| 391 gfx::RectF()); | 439 gfx::RectF()); |
| 392 EXPECT_CALL(*test_controller, Hide()); | 440 EXPECT_CALL(*test_controller, Hide()); |
| 393 | 441 |
| 394 gfx::RectF bounds(0.f, 0.f, 1.f, 2.f); | 442 gfx::RectF bounds(0.f, 0.f, 1.f, 2.f); |
| 395 base::WeakPtr<AutofillPopupControllerImpl> controller3 = | 443 base::WeakPtr<AutofillPopupControllerImpl> controller3 = |
| 396 AutofillPopupControllerImpl::GetOrCreate( | 444 AutofillPopupControllerImpl::GetOrCreate( |
| 397 test_controller->GetWeakPtr(), | 445 test_controller->GetWeakPtr(), |
| 398 delegate.GetWeakPtr(), | 446 delegate.GetWeakPtr(), |
| 399 NULL, | 447 NULL, |
| 400 NULL, | 448 NULL, |
| 401 bounds, | 449 bounds, |
| 402 base::i18n::UNKNOWN_DIRECTION); | 450 base::i18n::UNKNOWN_DIRECTION); |
| 403 EXPECT_EQ( | 451 EXPECT_EQ( |
| 404 bounds, | 452 bounds, |
| 405 static_cast<AutofillPopupController*>(controller3.get())-> | 453 static_cast<AutofillPopupController*>(controller3.get())-> |
| 406 element_bounds()); | 454 element_bounds()); |
| 407 controller3->Hide(); | 455 controller3->Hide(); |
| 408 | 456 |
| 409 // Hide the test_controller to delete it. | 457 // Hide the test_controller to delete it. |
| 410 test_controller->DoHide(); | 458 test_controller->DoHide(); |
| 411 } | 459 } |
| 412 | 460 |
| 413 TEST_F(AutofillPopupControllerUnitTest, ProperlyResetController) { | 461 TEST_F(AutofillPopupControllerUnitTest, ProperlyResetController) { |
| 414 std::vector<Suggestion> suggestions; | 462 std::vector<Suggestion> suggestions; |
| 415 suggestions.push_back(Suggestion("", "", "", 0)); | 463 suggestions.push_back(Suggestion("", "", "", 0)); |
| 416 suggestions.push_back(Suggestion("", "", "", 0)); | 464 suggestions.push_back(Suggestion("", "", "", 0)); |
| 417 popup_controller()->SetValues(suggestions); | 465 popup_controller()->Show(suggestions); |
| 418 popup_controller()->SetSelectedLine(0); | 466 popup_controller()->SetSelectedLine(0); |
| 419 | 467 |
| 420 // Now show a new popup with the same controller, but with fewer items. | 468 // Now show a new popup with the same controller, but with fewer items. |
| 421 WeakPtr<AutofillPopupControllerImpl> controller = | 469 WeakPtr<AutofillPopupControllerImpl> controller = |
| 422 AutofillPopupControllerImpl::GetOrCreate( | 470 AutofillPopupControllerImpl::GetOrCreate( |
| 423 popup_controller()->GetWeakPtr(), delegate()->GetWeakPtr(), NULL, | 471 popup_controller()->GetWeakPtr(), delegate()->GetWeakPtr(), NULL, |
| 424 NULL, gfx::RectF(), base::i18n::UNKNOWN_DIRECTION); | 472 NULL, gfx::RectF(), base::i18n::UNKNOWN_DIRECTION); |
| 425 EXPECT_NE(0, controller->selected_line()); | 473 EXPECT_FALSE(controller->selected_line()); |
| 426 EXPECT_EQ(0u, controller->GetLineCount()); | 474 EXPECT_EQ(0, controller->GetLineCount()); |
| 427 } | 475 } |
| 428 | 476 |
| 429 #if !defined(OS_ANDROID) | 477 #if !defined(OS_ANDROID) |
| 430 TEST_F(AutofillPopupControllerUnitTest, ElideText) { | 478 TEST_F(AutofillPopupControllerUnitTest, ElideText) { |
| 431 std::vector<Suggestion> suggestions; | 479 std::vector<Suggestion> suggestions; |
| 432 suggestions.push_back( | 480 suggestions.push_back( |
| 433 Suggestion("Text that will need to be trimmed", | 481 Suggestion("Text that will need to be trimmed", |
| 434 "Label that will be trimmed", "genericCC", 0)); | 482 "Label that will be trimmed", "genericCC", 0)); |
| 435 suggestions.push_back( | 483 suggestions.push_back( |
| 436 Suggestion("untrimmed", "Untrimmed", "genericCC", 0)); | 484 Suggestion("untrimmed", "Untrimmed", "genericCC", 0)); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 461 | 509 |
| 462 // The second element was shorter so it should be unchanged. | 510 // The second element was shorter so it should be unchanged. |
| 463 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).value, | 511 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).value, |
| 464 autofill_popup_controller_->GetElidedValueAt(1)); | 512 autofill_popup_controller_->GetElidedValueAt(1)); |
| 465 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).label, | 513 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).label, |
| 466 autofill_popup_controller_->GetElidedLabelAt(1)); | 514 autofill_popup_controller_->GetElidedLabelAt(1)); |
| 467 } | 515 } |
| 468 #endif | 516 #endif |
| 469 | 517 |
| 470 } // namespace autofill | 518 } // namespace autofill |
| OLD | NEW |