Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: chrome/browser/ui/autofill/autofill_popup_controller_unittest.cc

Issue 2727233003: Uses child views in Autofill Popup so we can trigger (Closed)
Patch Set: Removes NotifyAccessibilityEventForRow and InvalidateRow wrapper methods. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 ~MockAutofillClient() override {} 62 ~MockAutofillClient() override {}
63 63
64 PrefService* GetPrefs() override { return prefs_.get(); } 64 PrefService* GetPrefs() override { return prefs_.get(); }
65 65
66 private: 66 private:
67 std::unique_ptr<PrefService> prefs_; 67 std::unique_ptr<PrefService> prefs_;
68 68
69 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient); 69 DISALLOW_COPY_AND_ASSIGN(MockAutofillClient);
70 }; 70 };
71 71
72 class MockAutofillPopupView : public AutofillPopupView {
73 public:
74 MockAutofillPopupView() {}
75
76 MOCK_METHOD0(Show, void());
77 MOCK_METHOD0(Hide, void());
78 MOCK_METHOD2(InvalidateRow, void(size_t row, bool is_selected));
79 MOCK_METHOD0(OnSuggestionsChanged, void());
80
81 private:
82 DISALLOW_COPY_AND_ASSIGN(MockAutofillPopupView);
83 };
84
72 class TestAutofillPopupController : public AutofillPopupControllerImpl { 85 class TestAutofillPopupController : public AutofillPopupControllerImpl {
73 public: 86 public:
74 TestAutofillPopupController( 87 TestAutofillPopupController(
75 base::WeakPtr<AutofillExternalDelegate> external_delegate, 88 base::WeakPtr<AutofillExternalDelegate> external_delegate,
76 const gfx::RectF& element_bounds) 89 const gfx::RectF& element_bounds)
77 : AutofillPopupControllerImpl(external_delegate, 90 : AutofillPopupControllerImpl(external_delegate,
78 NULL, 91 NULL,
79 NULL, 92 NULL,
80 element_bounds, 93 element_bounds,
81 base::i18n::UNKNOWN_DIRECTION) {} 94 base::i18n::UNKNOWN_DIRECTION) {}
82 ~TestAutofillPopupController() override {} 95 ~TestAutofillPopupController() override {}
83 96
84 // Making protected functions public for testing 97 // Making protected functions public for testing
85 using AutofillPopupControllerImpl::GetLineCount; 98 using AutofillPopupControllerImpl::GetLineCount;
86 using AutofillPopupControllerImpl::GetSuggestionAt; 99 using AutofillPopupControllerImpl::GetSuggestionAt;
87 using AutofillPopupControllerImpl::GetElidedValueAt; 100 using AutofillPopupControllerImpl::GetElidedValueAt;
88 using AutofillPopupControllerImpl::GetElidedLabelAt; 101 using AutofillPopupControllerImpl::GetElidedLabelAt;
89 using AutofillPopupControllerImpl::selected_line; 102 using AutofillPopupControllerImpl::selected_line;
90 using AutofillPopupControllerImpl::SetSelectedLine; 103 using AutofillPopupControllerImpl::SetSelectedLine;
91 using AutofillPopupControllerImpl::SelectNextLine; 104 using AutofillPopupControllerImpl::SelectNextLine;
92 using AutofillPopupControllerImpl::SelectPreviousLine; 105 using AutofillPopupControllerImpl::SelectPreviousLine;
93 using AutofillPopupControllerImpl::RemoveSelectedLine; 106 using AutofillPopupControllerImpl::RemoveSelectedLine;
94 using AutofillPopupControllerImpl::popup_bounds; 107 using AutofillPopupControllerImpl::popup_bounds;
95 using AutofillPopupControllerImpl::element_bounds; 108 using AutofillPopupControllerImpl::element_bounds;
96 using AutofillPopupControllerImpl::SetValues; 109 using AutofillPopupControllerImpl::SetValues;
97 using AutofillPopupControllerImpl::GetWeakPtr; 110 using AutofillPopupControllerImpl::GetWeakPtr;
98 MOCK_METHOD1(InvalidateRow, void(size_t)); 111 MOCK_METHOD0(OnSuggestionsChanged, void());
99 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void());
100 MOCK_METHOD0(Hide, void()); 112 MOCK_METHOD0(Hide, void());
101 113
102 void DoHide() { 114 void DoHide() {
103 AutofillPopupControllerImpl::Hide(); 115 AutofillPopupControllerImpl::Hide();
104 } 116 }
105
106 private:
107 void ShowView() override {}
108 }; 117 };
109 118
110 } // namespace 119 } // namespace
111 120
112 class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness { 121 class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness {
113 public: 122 public:
114 AutofillPopupControllerUnitTest() 123 AutofillPopupControllerUnitTest()
115 : autofill_client_(new MockAutofillClient()), 124 : autofill_client_(new MockAutofillClient()),
116 autofill_popup_controller_(NULL) {} 125 autofill_popup_controller_(NULL) {}
117 ~AutofillPopupControllerUnitTest() override {} 126 ~AutofillPopupControllerUnitTest() override {}
118 127
119 void SetUp() override { 128 void SetUp() override {
120 ChromeRenderViewHostTestHarness::SetUp(); 129 ChromeRenderViewHostTestHarness::SetUp();
121 130
122 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate( 131 ContentAutofillDriverFactory::CreateForWebContentsAndDelegate(
123 web_contents(), autofill_client_.get(), "en-US", 132 web_contents(), autofill_client_.get(), "en-US",
124 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER); 133 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER);
125 // Make sure RenderFrame is created. 134 // Make sure RenderFrame is created.
126 NavigateAndCommit(GURL("about:blank")); 135 NavigateAndCommit(GURL("about:blank"));
127 ContentAutofillDriverFactory* factory = 136 ContentAutofillDriverFactory* factory =
128 ContentAutofillDriverFactory::FromWebContents(web_contents()); 137 ContentAutofillDriverFactory::FromWebContents(web_contents());
129 ContentAutofillDriver* driver = 138 ContentAutofillDriver* driver =
130 factory->DriverForFrame(web_contents()->GetMainFrame()); 139 factory->DriverForFrame(web_contents()->GetMainFrame());
131 external_delegate_.reset( 140 external_delegate_.reset(
132 new NiceMock<MockAutofillExternalDelegate>( 141 new NiceMock<MockAutofillExternalDelegate>(
133 driver->autofill_manager(), 142 driver->autofill_manager(),
134 driver)); 143 driver));
135 144 autofill_popup_view_.reset(new NiceMock<MockAutofillPopupView>());
136 autofill_popup_controller_ = 145 autofill_popup_controller_ = new NiceMock<TestAutofillPopupController>(
137 new testing::NiceMock<TestAutofillPopupController>( 146 external_delegate_->GetWeakPtr(), gfx::RectF());
138 external_delegate_->GetWeakPtr(), gfx::RectF()); 147 autofill_popup_controller_->SetViewForTesting(autofill_popup_view());
139 } 148 }
140 149
141 void TearDown() override { 150 void TearDown() override {
142 // This will make sure the controller and the view (if any) are both 151 // This will make sure the controller and the view (if any) are both
143 // cleaned up. 152 // cleaned up.
144 if (autofill_popup_controller_) 153 if (autofill_popup_controller_)
145 autofill_popup_controller_->DoHide(); 154 autofill_popup_controller_->DoHide();
146 155
147 external_delegate_.reset(); 156 external_delegate_.reset();
148 ChromeRenderViewHostTestHarness::TearDown(); 157 ChromeRenderViewHostTestHarness::TearDown();
149 } 158 }
150 159
151 TestAutofillPopupController* popup_controller() { 160 TestAutofillPopupController* popup_controller() {
152 return autofill_popup_controller_; 161 return autofill_popup_controller_;
153 } 162 }
154 163
155 MockAutofillExternalDelegate* delegate() { 164 MockAutofillExternalDelegate* delegate() {
156 return external_delegate_.get(); 165 return external_delegate_.get();
157 } 166 }
158 167
168 MockAutofillPopupView* autofill_popup_view() {
169 return autofill_popup_view_.get();
170 }
171
159 protected: 172 protected:
160 std::unique_ptr<MockAutofillClient> autofill_client_; 173 std::unique_ptr<MockAutofillClient> autofill_client_;
161 std::unique_ptr<NiceMock<MockAutofillExternalDelegate>> external_delegate_; 174 std::unique_ptr<NiceMock<MockAutofillExternalDelegate>> external_delegate_;
162 testing::NiceMock<TestAutofillPopupController>* autofill_popup_controller_; 175 std::unique_ptr<NiceMock<MockAutofillPopupView>> autofill_popup_view_;
176 NiceMock<TestAutofillPopupController>* autofill_popup_controller_;
163 }; 177 };
164 178
165 TEST_F(AutofillPopupControllerUnitTest, ChangeSelectedLine) { 179 TEST_F(AutofillPopupControllerUnitTest, ChangeSelectedLine) {
166 // Set up the popup. 180 // Set up the popup.
167 std::vector<Suggestion> suggestions; 181 std::vector<Suggestion> suggestions;
168 suggestions.push_back(Suggestion("", "", "", 0)); 182 suggestions.push_back(Suggestion("", "", "", 0));
169 suggestions.push_back(Suggestion("", "", "", 0)); 183 suggestions.push_back(Suggestion("", "", "", 0));
170 autofill_popup_controller_->Show(suggestions); 184 autofill_popup_controller_->Show(suggestions);
171 185
172 EXPECT_LT(autofill_popup_controller_->selected_line(), 0); 186 EXPECT_LT(autofill_popup_controller_->selected_line(), 0);
(...skipping 16 matching lines...) Expand all
189 TEST_F(AutofillPopupControllerUnitTest, RedrawSelectedLine) { 203 TEST_F(AutofillPopupControllerUnitTest, RedrawSelectedLine) {
190 // Set up the popup. 204 // Set up the popup.
191 std::vector<Suggestion> suggestions; 205 std::vector<Suggestion> suggestions;
192 suggestions.push_back(Suggestion("", "", "", 0)); 206 suggestions.push_back(Suggestion("", "", "", 0));
193 suggestions.push_back(Suggestion("", "", "", 0)); 207 suggestions.push_back(Suggestion("", "", "", 0));
194 autofill_popup_controller_->Show(suggestions); 208 autofill_popup_controller_->Show(suggestions);
195 209
196 // Make sure that when a new line is selected, it is invalidated so it can 210 // Make sure that when a new line is selected, it is invalidated so it can
197 // be updated to show it is selected. 211 // be updated to show it is selected.
198 int selected_line = 0; 212 int selected_line = 0;
199 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); 213 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line, true));
214
200 autofill_popup_controller_->SetSelectedLine(selected_line); 215 autofill_popup_controller_->SetSelectedLine(selected_line);
201 216
202 // Ensure that the row isn't invalidated if it didn't change. 217 // Ensure that the row isn't invalidated if it didn't change.
203 EXPECT_CALL(*autofill_popup_controller_, 218 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line, _)).Times(0);
204 InvalidateRow(selected_line)).Times(0);
205 autofill_popup_controller_->SetSelectedLine(selected_line); 219 autofill_popup_controller_->SetSelectedLine(selected_line);
206 220
207 // Change back to no selection. 221 // Change back to no selection.
208 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line)); 222 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(selected_line, false));
209 autofill_popup_controller_->SetSelectedLine(-1); 223 autofill_popup_controller_->SetSelectedLine(-1);
210 } 224 }
211 225
212 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) { 226 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) {
213 // Set up the popup. 227 // Set up the popup.
214 std::vector<Suggestion> suggestions; 228 std::vector<Suggestion> suggestions;
215 suggestions.push_back(Suggestion("", "", "", 1)); 229 suggestions.push_back(Suggestion("", "", "", 1));
216 suggestions.push_back(Suggestion("", "", "", 1)); 230 suggestions.push_back(Suggestion("", "", "", 1));
217 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_AUTOFILL_OPTIONS)); 231 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_AUTOFILL_OPTIONS));
218 autofill_popup_controller_->Show(suggestions); 232 autofill_popup_controller_->Show(suggestions);
219 233
220 // Generate a popup, so it can be hidden later. It doesn't matter what the 234 // 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 235 // external_delegate thinks is being shown in the process, since we are just
222 // testing the popup here. 236 // testing the popup here.
223 autofill::GenerateTestAutofillPopup(external_delegate_.get()); 237 autofill::GenerateTestAutofillPopup(external_delegate_.get());
224 238
225 // No line is selected so the removal should fail. 239 // No line is selected so the removal should fail.
226 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine()); 240 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine());
227 241
228 // Remove the first entry. The popup should be redrawn since its size has 242 // Remove the first entry. The popup should be redrawn since its size has
229 // changed. 243 // changed.
230 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); 244 EXPECT_CALL(*autofill_popup_controller_, OnSuggestionsChanged());
231 autofill_popup_controller_->SetSelectedLine(0); 245 autofill_popup_controller_->SetSelectedLine(0);
232 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); 246 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine());
233 247
234 // Remove the last entry. The popup should then be hidden since there are 248 // Remove the last entry. The popup should then be hidden since there are
235 // no Autofill entries left. 249 // no Autofill entries left.
236 EXPECT_CALL(*autofill_popup_controller_, Hide()); 250 EXPECT_CALL(*autofill_popup_controller_, Hide());
237 autofill_popup_controller_->SetSelectedLine(0); 251 autofill_popup_controller_->SetSelectedLine(0);
238 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); 252 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine());
239 } 253 }
240 254
241 TEST_F(AutofillPopupControllerUnitTest, RemoveOnlyLine) { 255 TEST_F(AutofillPopupControllerUnitTest, RemoveOnlyLine) {
242 // Set up the popup. 256 // Set up the popup.
243 std::vector<Suggestion> suggestions; 257 std::vector<Suggestion> suggestions;
244 suggestions.push_back(Suggestion("", "", "", 1)); 258 suggestions.push_back(Suggestion("", "", "", 1));
245 autofill_popup_controller_->Show(suggestions); 259 autofill_popup_controller_->Show(suggestions);
246 260
247 // Generate a popup. 261 // Generate a popup.
248 autofill::GenerateTestAutofillPopup(external_delegate_.get()); 262 autofill::GenerateTestAutofillPopup(external_delegate_.get());
249 263
250 // Select the only line. 264 // Select the only line.
251 autofill_popup_controller_->SetSelectedLine(0); 265 autofill_popup_controller_->SetSelectedLine(0);
252 266
253 // Remove the only line. There should be no row invalidation and the popup 267 // Remove the only line. There should be no row invalidation and the popup
254 // should then be hidden since there are no Autofill entries left. 268 // should then be hidden since there are no Autofill entries left.
255 EXPECT_CALL(*autofill_popup_controller_, Hide()); 269 EXPECT_CALL(*autofill_popup_controller_, Hide());
256 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(_)).Times(0); 270 EXPECT_CALL(*autofill_popup_view_, InvalidateRow(_, _)).Times(0);
257 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); 271 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine());
258 } 272 }
259 273
260 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { 274 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) {
261 // Set up the popup. 275 // Set up the popup.
262 std::vector<Suggestion> suggestions; 276 std::vector<Suggestion> suggestions;
263 suggestions.push_back(Suggestion("", "", "", 1)); 277 suggestions.push_back(Suggestion("", "", "", 1));
264 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_SEPARATOR)); 278 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_SEPARATOR));
265 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_AUTOFILL_OPTIONS)); 279 suggestions.push_back(Suggestion("", "", "", POPUP_ITEM_ID_AUTOFILL_OPTIONS));
266 autofill_popup_controller_->Show(suggestions); 280 autofill_popup_controller_->Show(suggestions);
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 gfx::RectF(), base::i18n::UNKNOWN_DIRECTION); 393 gfx::RectF(), base::i18n::UNKNOWN_DIRECTION);
380 EXPECT_TRUE(controller.get()); 394 EXPECT_TRUE(controller.get());
381 395
382 WeakPtr<AutofillPopupControllerImpl> controller2 = 396 WeakPtr<AutofillPopupControllerImpl> controller2 =
383 AutofillPopupControllerImpl::GetOrCreate( 397 AutofillPopupControllerImpl::GetOrCreate(
384 controller, delegate.GetWeakPtr(), NULL, NULL, gfx::RectF(), 398 controller, delegate.GetWeakPtr(), NULL, NULL, gfx::RectF(),
385 base::i18n::UNKNOWN_DIRECTION); 399 base::i18n::UNKNOWN_DIRECTION);
386 EXPECT_EQ(controller.get(), controller2.get()); 400 EXPECT_EQ(controller.get(), controller2.get());
387 controller->Hide(); 401 controller->Hide();
388 402
389 testing::NiceMock<TestAutofillPopupController>* test_controller = 403 NiceMock<TestAutofillPopupController>* test_controller =
390 new testing::NiceMock<TestAutofillPopupController>(delegate.GetWeakPtr(), 404 new NiceMock<TestAutofillPopupController>(delegate.GetWeakPtr(),
391 gfx::RectF()); 405 gfx::RectF());
392 EXPECT_CALL(*test_controller, Hide()); 406 EXPECT_CALL(*test_controller, Hide());
393 407
394 gfx::RectF bounds(0.f, 0.f, 1.f, 2.f); 408 gfx::RectF bounds(0.f, 0.f, 1.f, 2.f);
395 base::WeakPtr<AutofillPopupControllerImpl> controller3 = 409 base::WeakPtr<AutofillPopupControllerImpl> controller3 =
396 AutofillPopupControllerImpl::GetOrCreate( 410 AutofillPopupControllerImpl::GetOrCreate(
397 test_controller->GetWeakPtr(), 411 test_controller->GetWeakPtr(),
398 delegate.GetWeakPtr(), 412 delegate.GetWeakPtr(),
399 NULL, 413 NULL,
400 NULL, 414 NULL,
401 bounds, 415 bounds,
402 base::i18n::UNKNOWN_DIRECTION); 416 base::i18n::UNKNOWN_DIRECTION);
403 EXPECT_EQ( 417 EXPECT_EQ(
404 bounds, 418 bounds,
405 static_cast<AutofillPopupController*>(controller3.get())-> 419 static_cast<AutofillPopupController*>(controller3.get())->
406 element_bounds()); 420 element_bounds());
407 controller3->Hide(); 421 controller3->Hide();
408 422
409 // Hide the test_controller to delete it. 423 // Hide the test_controller to delete it.
410 test_controller->DoHide(); 424 test_controller->DoHide();
411 } 425 }
412 426
413 TEST_F(AutofillPopupControllerUnitTest, ProperlyResetController) { 427 TEST_F(AutofillPopupControllerUnitTest, ProperlyResetController) {
414 std::vector<Suggestion> suggestions; 428 std::vector<Suggestion> suggestions;
415 suggestions.push_back(Suggestion("", "", "", 0)); 429 suggestions.push_back(Suggestion("", "", "", 0));
416 suggestions.push_back(Suggestion("", "", "", 0)); 430 suggestions.push_back(Suggestion("", "", "", 0));
417 popup_controller()->SetValues(suggestions); 431 popup_controller()->Show(suggestions);
418 popup_controller()->SetSelectedLine(0); 432 popup_controller()->SetSelectedLine(0);
419 433
420 // Now show a new popup with the same controller, but with fewer items. 434 // Now show a new popup with the same controller, but with fewer items.
421 WeakPtr<AutofillPopupControllerImpl> controller = 435 WeakPtr<AutofillPopupControllerImpl> controller =
422 AutofillPopupControllerImpl::GetOrCreate( 436 AutofillPopupControllerImpl::GetOrCreate(
423 popup_controller()->GetWeakPtr(), delegate()->GetWeakPtr(), NULL, 437 popup_controller()->GetWeakPtr(), delegate()->GetWeakPtr(), NULL,
424 NULL, gfx::RectF(), base::i18n::UNKNOWN_DIRECTION); 438 NULL, gfx::RectF(), base::i18n::UNKNOWN_DIRECTION);
425 EXPECT_NE(0, controller->selected_line()); 439 EXPECT_NE(0, controller->selected_line());
426 EXPECT_EQ(0u, controller->GetLineCount()); 440 EXPECT_EQ(0u, controller->GetLineCount());
427 } 441 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 475
462 // The second element was shorter so it should be unchanged. 476 // The second element was shorter so it should be unchanged.
463 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).value, 477 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).value,
464 autofill_popup_controller_->GetElidedValueAt(1)); 478 autofill_popup_controller_->GetElidedValueAt(1));
465 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).label, 479 EXPECT_EQ(autofill_popup_controller_->GetSuggestionAt(1).label,
466 autofill_popup_controller_->GetElidedLabelAt(1)); 480 autofill_popup_controller_->GetElidedLabelAt(1));
467 } 481 }
468 #endif 482 #endif
469 483
470 } // namespace autofill 484 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698