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

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

Powered by Google App Engine
This is Rietveld 408576698