Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / autofill / autofill_popup_controller_unittest.cc
blobafd2640884d3e7113f2b82b9dff40830fce7b855
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
3 // found in the LICENSE file.
5 #include "base/memory/scoped_ptr.h"
6 #include "base/memory/weak_ptr.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
10 #include "chrome/browser/ui/autofill/autofill_popup_view.h"
11 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/autofill/content/browser/autofill_driver_impl.h"
14 #include "components/autofill/core/browser/autofill_external_delegate.h"
15 #include "components/autofill/core/browser/autofill_manager.h"
16 #include "components/autofill/core/browser/autofill_test_utils.h"
17 #include "components/autofill/core/browser/test_autofill_external_delegate.h"
18 #include "components/autofill/core/browser/test_autofill_manager_delegate.h"
19 #include "grit/webkit_resources.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/WebKit/public/web/WebAutofillClient.h"
23 #include "ui/base/resource/resource_bundle.h"
24 #include "ui/gfx/display.h"
25 #include "ui/gfx/rect.h"
26 #include "ui/gfx/text_utils.h"
28 using ::testing::_;
29 using ::testing::AtLeast;
30 using ::testing::NiceMock;
31 using base::ASCIIToUTF16;
32 using base::WeakPtr;
33 using blink::WebAutofillClient;
35 namespace autofill {
36 namespace {
38 class MockAutofillExternalDelegate : public AutofillExternalDelegate {
39 public:
40 MockAutofillExternalDelegate(AutofillManager* autofill_manager,
41 AutofillDriver* autofill_driver)
42 : AutofillExternalDelegate(autofill_manager, autofill_driver) {}
43 virtual ~MockAutofillExternalDelegate() {}
45 virtual void DidSelectSuggestion(int identifier) OVERRIDE {}
46 virtual void RemoveSuggestion(const base::string16& value,
47 int identifier) OVERRIDE {}
48 virtual void ClearPreviewedForm() OVERRIDE {}
49 base::WeakPtr<AutofillExternalDelegate> GetWeakPtr() {
50 return AutofillExternalDelegate::GetWeakPtr();
54 class MockAutofillManagerDelegate
55 : public autofill::TestAutofillManagerDelegate {
56 public:
57 MockAutofillManagerDelegate()
58 : prefs_(autofill::test::PrefServiceForTesting()) {
60 virtual ~MockAutofillManagerDelegate() {}
62 virtual PrefService* GetPrefs() OVERRIDE { return prefs_.get(); }
64 private:
65 scoped_ptr<PrefService> prefs_;
67 DISALLOW_COPY_AND_ASSIGN(MockAutofillManagerDelegate);
70 class TestAutofillPopupController : public AutofillPopupControllerImpl {
71 public:
72 explicit TestAutofillPopupController(
73 base::WeakPtr<AutofillExternalDelegate> external_delegate,
74 const gfx::RectF& element_bounds)
75 : AutofillPopupControllerImpl(
76 external_delegate, NULL, NULL, element_bounds,
77 base::i18n::UNKNOWN_DIRECTION) {}
78 virtual ~TestAutofillPopupController() {}
80 void set_display(const gfx::Display display) {
81 display_ = display;
83 virtual gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const
84 OVERRIDE {
85 return display_;
88 // Making protected functions public for testing
89 using AutofillPopupControllerImpl::SetPopupBounds;
90 using AutofillPopupControllerImpl::names;
91 using AutofillPopupControllerImpl::subtexts;
92 using AutofillPopupControllerImpl::identifiers;
93 using AutofillPopupControllerImpl::selected_line;
94 using AutofillPopupControllerImpl::SetSelectedLine;
95 using AutofillPopupControllerImpl::SelectNextLine;
96 using AutofillPopupControllerImpl::SelectPreviousLine;
97 using AutofillPopupControllerImpl::RemoveSelectedLine;
98 using AutofillPopupControllerImpl::popup_bounds;
99 using AutofillPopupControllerImpl::element_bounds;
100 #if !defined(OS_ANDROID)
101 using AutofillPopupControllerImpl::GetNameFontListForRow;
102 using AutofillPopupControllerImpl::subtext_font_list;
103 using AutofillPopupControllerImpl::RowWidthWithoutText;
104 #endif
105 using AutofillPopupControllerImpl::SetValues;
106 using AutofillPopupControllerImpl::GetDesiredPopupWidth;
107 using AutofillPopupControllerImpl::GetDesiredPopupHeight;
108 using AutofillPopupControllerImpl::GetWeakPtr;
109 MOCK_METHOD1(InvalidateRow, void(size_t));
110 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void());
111 MOCK_METHOD0(Hide, void());
113 void DoHide() {
114 AutofillPopupControllerImpl::Hide();
117 private:
118 virtual void ShowView() OVERRIDE {}
120 gfx::Display display_;
123 } // namespace
125 class AutofillPopupControllerUnitTest : public ChromeRenderViewHostTestHarness {
126 public:
127 AutofillPopupControllerUnitTest()
128 : manager_delegate_(new MockAutofillManagerDelegate()),
129 autofill_popup_controller_(NULL) {}
130 virtual ~AutofillPopupControllerUnitTest() {}
132 virtual void SetUp() OVERRIDE {
133 ChromeRenderViewHostTestHarness::SetUp();
135 AutofillDriverImpl::CreateForWebContentsAndDelegate(
136 web_contents(),
137 manager_delegate_.get(),
138 "en-US",
139 AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER);
140 AutofillDriverImpl* driver =
141 AutofillDriverImpl::FromWebContents(web_contents());
142 external_delegate_.reset(
143 new NiceMock<MockAutofillExternalDelegate>(
144 driver->autofill_manager(),
145 driver));
147 autofill_popup_controller_ =
148 new testing::NiceMock<TestAutofillPopupController>(
149 external_delegate_->GetWeakPtr(),gfx::Rect());
152 virtual void TearDown() OVERRIDE {
153 // This will make sure the controller and the view (if any) are both
154 // cleaned up.
155 if (autofill_popup_controller_)
156 autofill_popup_controller_->DoHide();
158 external_delegate_.reset();
159 ChromeRenderViewHostTestHarness::TearDown();
162 TestAutofillPopupController* popup_controller() {
163 return autofill_popup_controller_;
166 MockAutofillExternalDelegate* delegate() {
167 return external_delegate_.get();
170 protected:
171 scoped_ptr<MockAutofillManagerDelegate> manager_delegate_;
172 scoped_ptr<NiceMock<MockAutofillExternalDelegate> > external_delegate_;
173 testing::NiceMock<TestAutofillPopupController>* autofill_popup_controller_;
176 TEST_F(AutofillPopupControllerUnitTest, SetBounds) {
177 // Ensure the popup size can be set and causes a redraw.
178 gfx::Rect popup_bounds(10, 10, 100, 100);
180 EXPECT_CALL(*autofill_popup_controller_,
181 UpdateBoundsAndRedrawPopup());
183 popup_controller()->SetPopupBounds(popup_bounds);
185 EXPECT_EQ(popup_bounds, popup_controller()->popup_bounds());
188 TEST_F(AutofillPopupControllerUnitTest, ChangeSelectedLine) {
189 // Set up the popup.
190 std::vector<base::string16> names(2, base::string16());
191 std::vector<int> autofill_ids(2, 0);
192 autofill_popup_controller_->Show(names, names, names, autofill_ids);
194 EXPECT_LT(autofill_popup_controller_->selected_line(), 0);
195 // Check that there are at least 2 values so that the first and last selection
196 // are different.
197 EXPECT_GE(2,
198 static_cast<int>(autofill_popup_controller_->subtexts().size()));
200 // Test wrapping before the front.
201 autofill_popup_controller_->SelectPreviousLine();
202 EXPECT_EQ(static_cast<int>(
203 autofill_popup_controller_->subtexts().size() - 1),
204 autofill_popup_controller_->selected_line());
206 // Test wrapping after the end.
207 autofill_popup_controller_->SelectNextLine();
208 EXPECT_EQ(0, autofill_popup_controller_->selected_line());
211 TEST_F(AutofillPopupControllerUnitTest, RedrawSelectedLine) {
212 // Set up the popup.
213 std::vector<base::string16> names(2, base::string16());
214 std::vector<int> autofill_ids(2, 0);
215 autofill_popup_controller_->Show(names, names, names, autofill_ids);
217 // Make sure that when a new line is selected, it is invalidated so it can
218 // be updated to show it is selected.
219 int selected_line = 0;
220 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line));
221 autofill_popup_controller_->SetSelectedLine(selected_line);
223 // Ensure that the row isn't invalidated if it didn't change.
224 EXPECT_CALL(*autofill_popup_controller_,
225 InvalidateRow(selected_line)).Times(0);
226 autofill_popup_controller_->SetSelectedLine(selected_line);
228 // Change back to no selection.
229 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(selected_line));
230 autofill_popup_controller_->SetSelectedLine(-1);
233 TEST_F(AutofillPopupControllerUnitTest, RemoveLine) {
234 // Set up the popup.
235 std::vector<base::string16> names(3, base::string16());
236 std::vector<int> autofill_ids;
237 autofill_ids.push_back(1);
238 autofill_ids.push_back(1);
239 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions);
240 autofill_popup_controller_->Show(names, names, names, autofill_ids);
242 // Generate a popup, so it can be hidden later. It doesn't matter what the
243 // external_delegate thinks is being shown in the process, since we are just
244 // testing the popup here.
245 autofill::GenerateTestAutofillPopup(external_delegate_.get());
247 // No line is selected so the removal should fail.
248 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine());
250 // Try to remove the last entry and ensure it fails (it is an option).
251 autofill_popup_controller_->SetSelectedLine(
252 autofill_popup_controller_->subtexts().size() - 1);
253 EXPECT_FALSE(autofill_popup_controller_->RemoveSelectedLine());
254 EXPECT_LE(0, autofill_popup_controller_->selected_line());
256 // Remove the first entry. The popup should be redrawn since its size has
257 // changed.
258 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup());
259 autofill_popup_controller_->SetSelectedLine(0);
260 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine());
262 // Remove the last entry. The popup should then be hidden since there are
263 // no Autofill entries left.
264 EXPECT_CALL(*autofill_popup_controller_, Hide());
265 autofill_popup_controller_->SetSelectedLine(0);
266 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine());
269 TEST_F(AutofillPopupControllerUnitTest, RemoveOnlyLine) {
270 // Set up the popup.
271 std::vector<base::string16> names(1, base::string16());
272 std::vector<int> autofill_ids;
273 autofill_ids.push_back(1);
274 autofill_popup_controller_->Show(names, names, names, autofill_ids);
276 // Generate a popup.
277 autofill::GenerateTestAutofillPopup(external_delegate_.get());
279 // Select the only line.
280 autofill_popup_controller_->SetSelectedLine(0);
282 // Remove the only line. There should be no row invalidation and the popup
283 // should then be hidden since there are no Autofill entries left.
284 EXPECT_CALL(*autofill_popup_controller_, Hide());
285 EXPECT_CALL(*autofill_popup_controller_, InvalidateRow(_)).Times(0);
286 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine());
289 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) {
290 // Set up the popup.
291 std::vector<base::string16> names(3, base::string16());
292 std::vector<int> autofill_ids;
293 autofill_ids.push_back(1);
294 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator);
295 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions);
296 autofill_popup_controller_->Show(names, names, names, autofill_ids);
298 autofill_popup_controller_->SetSelectedLine(0);
300 // Make sure next skips the unselectable separator.
301 autofill_popup_controller_->SelectNextLine();
302 EXPECT_EQ(2, autofill_popup_controller_->selected_line());
304 // Make sure previous skips the unselectable separator.
305 autofill_popup_controller_->SelectPreviousLine();
306 EXPECT_EQ(0, autofill_popup_controller_->selected_line());
309 TEST_F(AutofillPopupControllerUnitTest, RowWidthWithoutText) {
310 std::vector<base::string16> names(4);
311 std::vector<base::string16> subtexts(4);
312 std::vector<base::string16> icons(4);
313 std::vector<int> ids(4);
315 // Set up some visible display so the text values are kept.
316 gfx::Display display(0, gfx::Rect(0, 0, 100, 100));
317 autofill_popup_controller_->set_display(display);
319 // Give elements 1 and 3 subtexts and elements 2 and 3 icons, to ensure
320 // all combinations of subtexts and icons.
321 subtexts[1] = ASCIIToUTF16("x");
322 subtexts[3] = ASCIIToUTF16("x");
323 icons[2] = ASCIIToUTF16("americanExpressCC");
324 icons[3] = ASCIIToUTF16("genericCC");
325 autofill_popup_controller_->Show(names, subtexts, icons, ids);
327 int base_size =
328 AutofillPopupView::kEndPadding * 2 +
329 AutofillPopupView::kBorderThickness * 2;
330 int subtext_increase = AutofillPopupView::kNamePadding;
332 EXPECT_EQ(base_size, autofill_popup_controller_->RowWidthWithoutText(0));
333 EXPECT_EQ(base_size + subtext_increase,
334 autofill_popup_controller_->RowWidthWithoutText(1));
335 EXPECT_EQ(base_size + AutofillPopupView::kIconPadding +
336 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
337 IDR_AUTOFILL_CC_AMEX).Width(),
338 autofill_popup_controller_->RowWidthWithoutText(2));
339 EXPECT_EQ(base_size + subtext_increase + AutofillPopupView::kIconPadding +
340 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
341 IDR_AUTOFILL_CC_GENERIC).Width(),
342 autofill_popup_controller_->RowWidthWithoutText(3));
345 TEST_F(AutofillPopupControllerUnitTest, UpdateDataListValues) {
346 std::vector<base::string16> items;
347 items.push_back(base::string16());
348 std::vector<int> ids;
349 ids.push_back(1);
351 autofill_popup_controller_->Show(items, items, items, ids);
353 EXPECT_EQ(items, autofill_popup_controller_->names());
354 EXPECT_EQ(ids, autofill_popup_controller_->identifiers());
356 // Add one data list entry.
357 std::vector<base::string16> data_list_values;
358 data_list_values.push_back(ASCIIToUTF16("data list value 1"));
360 autofill_popup_controller_->UpdateDataListValues(data_list_values,
361 data_list_values);
363 // Update the expected values.
364 items.insert(items.begin(), data_list_values[0]);
365 items.insert(items.begin() + 1, base::string16());
366 ids.insert(ids.begin(), WebAutofillClient::MenuItemIDDataListEntry);
367 ids.insert(ids.begin() + 1, WebAutofillClient::MenuItemIDSeparator);
369 EXPECT_EQ(items, autofill_popup_controller_->names());
370 EXPECT_EQ(ids, autofill_popup_controller_->identifiers());
372 // Add two data list entries (which should replace the current one).
373 data_list_values.push_back(ASCIIToUTF16("data list value 2"));
375 autofill_popup_controller_->UpdateDataListValues(data_list_values,
376 data_list_values);
378 // Update the expected values.
379 items.insert(items.begin() + 1, data_list_values[1]);
380 ids.insert(ids.begin(), WebAutofillClient::MenuItemIDDataListEntry);
382 EXPECT_EQ(items, autofill_popup_controller_->names());
383 EXPECT_EQ(ids, autofill_popup_controller_->identifiers());
385 // Clear all data list values.
386 data_list_values.clear();
388 autofill_popup_controller_->UpdateDataListValues(data_list_values,
389 data_list_values);
391 items.clear();
392 items.push_back(base::string16());
393 ids.clear();
394 ids.push_back(1);
396 EXPECT_EQ(items, autofill_popup_controller_->names());
397 EXPECT_EQ(ids, autofill_popup_controller_->identifiers());
400 TEST_F(AutofillPopupControllerUnitTest, PopupsWithOnlyDataLists) {
401 // Create the popup with a single datalist element.
402 std::vector<base::string16> items;
403 items.push_back(base::string16());
404 std::vector<int> ids;
405 ids.push_back(WebAutofillClient::MenuItemIDDataListEntry);
407 autofill_popup_controller_->Show(items, items, items, ids);
409 EXPECT_EQ(items, autofill_popup_controller_->names());
410 EXPECT_EQ(ids, autofill_popup_controller_->identifiers());
412 // Replace the datalist element with a new one.
413 std::vector<base::string16> data_list_values;
414 data_list_values.push_back(ASCIIToUTF16("data list value 1"));
416 autofill_popup_controller_->UpdateDataListValues(data_list_values,
417 data_list_values);
419 EXPECT_EQ(data_list_values, autofill_popup_controller_->names());
420 // The id value should stay the same.
421 EXPECT_EQ(ids, autofill_popup_controller_->identifiers());
423 // Clear datalist values and check that the popup becomes hidden.
424 EXPECT_CALL(*autofill_popup_controller_, Hide());
425 data_list_values.clear();
426 autofill_popup_controller_->UpdateDataListValues(data_list_values,
427 data_list_values);
430 TEST_F(AutofillPopupControllerUnitTest, GetOrCreate) {
431 AutofillDriverImpl* driver =
432 AutofillDriverImpl::FromWebContents(web_contents());
433 MockAutofillExternalDelegate delegate(driver->autofill_manager(), driver);
435 WeakPtr<AutofillPopupControllerImpl> controller =
436 AutofillPopupControllerImpl::GetOrCreate(
437 WeakPtr<AutofillPopupControllerImpl>(), delegate.GetWeakPtr(),
438 NULL, NULL, gfx::Rect(), base::i18n::UNKNOWN_DIRECTION);
439 EXPECT_TRUE(controller.get());
441 controller->Hide();
443 controller = AutofillPopupControllerImpl::GetOrCreate(
444 WeakPtr<AutofillPopupControllerImpl>(), delegate.GetWeakPtr(),
445 NULL, NULL, gfx::Rect(), base::i18n::UNKNOWN_DIRECTION);
446 EXPECT_TRUE(controller.get());
448 WeakPtr<AutofillPopupControllerImpl> controller2 =
449 AutofillPopupControllerImpl::GetOrCreate(controller,
450 delegate.GetWeakPtr(),
451 NULL,
452 NULL,
453 gfx::Rect(),
454 base::i18n::UNKNOWN_DIRECTION);
455 EXPECT_EQ(controller.get(), controller2.get());
456 controller->Hide();
458 testing::NiceMock<TestAutofillPopupController>* test_controller =
459 new testing::NiceMock<TestAutofillPopupController>(delegate.GetWeakPtr(),
460 gfx::Rect());
461 EXPECT_CALL(*test_controller, Hide());
463 gfx::RectF bounds(0.f, 0.f, 1.f, 2.f);
464 base::WeakPtr<AutofillPopupControllerImpl> controller3 =
465 AutofillPopupControllerImpl::GetOrCreate(
466 test_controller->GetWeakPtr(),
467 delegate.GetWeakPtr(),
468 NULL,
469 NULL,
470 bounds,
471 base::i18n::UNKNOWN_DIRECTION);
472 EXPECT_EQ(
473 bounds,
474 static_cast<AutofillPopupController*>(controller3.get())->
475 element_bounds());
476 controller3->Hide();
478 // Hide the test_controller to delete it.
479 test_controller->DoHide();
482 TEST_F(AutofillPopupControllerUnitTest, ProperlyResetController) {
483 std::vector<base::string16> names(2);
484 std::vector<int> ids(2);
485 popup_controller()->SetValues(names, names, names, ids);
486 popup_controller()->SetSelectedLine(0);
488 // Now show a new popup with the same controller, but with fewer items.
489 WeakPtr<AutofillPopupControllerImpl> controller =
490 AutofillPopupControllerImpl::GetOrCreate(
491 popup_controller()->GetWeakPtr(),
492 delegate()->GetWeakPtr(),
493 NULL,
494 NULL,
495 gfx::Rect(),
496 base::i18n::UNKNOWN_DIRECTION);
497 EXPECT_NE(0, controller->selected_line());
498 EXPECT_TRUE(controller->names().empty());
501 #if !defined(OS_ANDROID)
502 TEST_F(AutofillPopupControllerUnitTest, ElideText) {
503 std::vector<base::string16> names;
504 names.push_back(ASCIIToUTF16("Text that will need to be trimmed"));
505 names.push_back(ASCIIToUTF16("Untrimmed"));
507 std::vector<base::string16> subtexts;
508 subtexts.push_back(ASCIIToUTF16("Label that will be trimmed"));
509 subtexts.push_back(ASCIIToUTF16("Untrimmed"));
511 std::vector<base::string16> icons(2, ASCIIToUTF16("genericCC"));
512 std::vector<int> autofill_ids(2, 0);
514 // Show the popup once so we can easily generate the size it needs.
515 autofill_popup_controller_->Show(names, subtexts, icons, autofill_ids);
517 // Ensure the popup will be too small to display all of the first row.
518 int popup_max_width =
519 gfx::GetStringWidth(
520 names[0], autofill_popup_controller_->GetNameFontListForRow(0)) +
521 gfx::GetStringWidth(
522 subtexts[0], autofill_popup_controller_->subtext_font_list()) - 25;
523 gfx::Rect popup_bounds = gfx::Rect(0, 0, popup_max_width, 0);
524 autofill_popup_controller_->set_display(gfx::Display(0, popup_bounds));
526 autofill_popup_controller_->Show(names, subtexts, icons, autofill_ids);
528 // The first element was long so it should have been trimmed.
529 EXPECT_NE(names[0], autofill_popup_controller_->names()[0]);
530 EXPECT_NE(subtexts[0], autofill_popup_controller_->subtexts()[0]);
532 // The second element was shorter so it should be unchanged.
533 EXPECT_EQ(names[1], autofill_popup_controller_->names()[1]);
534 EXPECT_EQ(subtexts[1], autofill_popup_controller_->subtexts()[1]);
536 #endif
538 TEST_F(AutofillPopupControllerUnitTest, GrowPopupInSpace) {
539 std::vector<base::string16> names(1);
540 std::vector<int> autofill_ids(1, 1);
542 // Call Show so that GetDesired...() will be able to provide valid values.
543 autofill_popup_controller_->Show(names, names, names, autofill_ids);
544 int desired_width = autofill_popup_controller_->GetDesiredPopupWidth();
545 int desired_height = autofill_popup_controller_->GetDesiredPopupHeight();
547 // Set up the visible screen space.
548 gfx::Display display(0,
549 gfx::Rect(0, 0, desired_width * 2, desired_height * 2));
551 // Store the possible element bounds and the popup bounds they should result
552 // in.
553 std::vector<gfx::RectF> element_bounds;
554 std::vector<gfx::Rect> expected_popup_bounds;
556 // The popup grows down and to the right.
557 element_bounds.push_back(gfx::RectF(0, 0, 0, 0));
558 expected_popup_bounds.push_back(
559 gfx::Rect(0, 0, desired_width, desired_height));
561 // The popup grows down and to the left.
562 element_bounds.push_back(gfx::RectF(2 * desired_width, 0, 0, 0));
563 expected_popup_bounds.push_back(
564 gfx::Rect(desired_width, 0, desired_width, desired_height));
566 // The popup grows up and to the right.
567 element_bounds.push_back(gfx::RectF(0, 2 * desired_height, 0, 0));
568 expected_popup_bounds.push_back(
569 gfx::Rect(0, desired_height, desired_width, desired_height));
571 // The popup grows up and to the left.
572 element_bounds.push_back(
573 gfx::RectF(2 * desired_width, 2 * desired_height, 0, 0));
574 expected_popup_bounds.push_back(
575 gfx::Rect(desired_width, desired_height, desired_width, desired_height));
577 // The popup would be partial off the top and left side of the screen.
578 element_bounds.push_back(
579 gfx::RectF(-desired_width / 2, -desired_height / 2, 0, 0));
580 expected_popup_bounds.push_back(
581 gfx::Rect(0, 0, desired_width, desired_height));
583 // The popup would be partially off the bottom and the right side of
584 // the screen.
585 element_bounds.push_back(
586 gfx::RectF(desired_width * 1.5, desired_height * 1.5, 0, 0));
587 expected_popup_bounds.push_back(
588 gfx::Rect((desired_width + 1) / 2, (desired_height + 1) / 2,
589 desired_width, desired_height));
591 for (size_t i = 0; i < element_bounds.size(); ++i) {
592 AutofillDriverImpl* driver =
593 AutofillDriverImpl::FromWebContents(web_contents());
594 NiceMock<MockAutofillExternalDelegate> external_delegate(
595 driver->autofill_manager(), driver);
596 TestAutofillPopupController* autofill_popup_controller =
597 new TestAutofillPopupController(external_delegate.GetWeakPtr(),
598 element_bounds[i]);
600 autofill_popup_controller->set_display(display);
601 autofill_popup_controller->Show(names, names, names, autofill_ids);
603 EXPECT_EQ(expected_popup_bounds[i].ToString(),
604 autofill_popup_controller->popup_bounds().ToString()) <<
605 "Popup bounds failed to match for test " << i;
607 // Hide the controller to delete it.
608 autofill_popup_controller->DoHide();
612 } // namespace autofill