OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/utf_string_conversions.h" |
6 #include "chrome/browser/autofill/test_autofill_external_delegate.h" | 7 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
7 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" | 8 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
8 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
| 12 #include "ui/gfx/display.h" |
11 #include "ui/gfx/rect.h" | 13 #include "ui/gfx/rect.h" |
12 | 14 |
13 using ::testing::_; | 15 using ::testing::_; |
14 using ::testing::AtLeast; | 16 using ::testing::AtLeast; |
15 using WebKit::WebAutofillClient; | 17 using WebKit::WebAutofillClient; |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 class MockAutofillExternalDelegate : | 21 class MockAutofillExternalDelegate : |
20 public autofill::TestAutofillExternalDelegate { | 22 public autofill::TestAutofillExternalDelegate { |
21 public: | 23 public: |
22 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; | 24 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; |
23 virtual ~MockAutofillExternalDelegate() {}; | 25 virtual ~MockAutofillExternalDelegate() {}; |
24 | 26 |
25 virtual void DidSelectSuggestion(int identifier) OVERRIDE {} | 27 virtual void DidSelectSuggestion(int identifier) OVERRIDE {} |
26 virtual void RemoveSuggestion(const string16& value, int identifier) OVERRIDE | 28 virtual void RemoveSuggestion(const string16& value, int identifier) OVERRIDE |
27 {} | 29 {} |
28 virtual void ClearPreviewedForm() OVERRIDE {} | 30 virtual void ClearPreviewedForm() OVERRIDE {} |
29 | 31 |
30 MOCK_METHOD0(ControllerDestroyed, void()); | 32 MOCK_METHOD0(ControllerDestroyed, void()); |
31 }; | 33 }; |
32 | 34 |
33 class TestAutofillPopupController : public AutofillPopupControllerImpl { | 35 class TestAutofillPopupController : public AutofillPopupControllerImpl { |
34 public: | 36 public: |
35 explicit TestAutofillPopupController( | 37 explicit TestAutofillPopupController( |
36 AutofillExternalDelegate* external_delegate) | 38 AutofillExternalDelegate* external_delegate, |
37 : AutofillPopupControllerImpl(external_delegate, NULL, gfx::Rect()) {} | 39 const gfx::Rect& element_bounds) |
| 40 : AutofillPopupControllerImpl(external_delegate, NULL, element_bounds) {} |
38 virtual ~TestAutofillPopupController() {} | 41 virtual ~TestAutofillPopupController() {} |
39 | 42 |
| 43 void set_display(const gfx::Display display) { |
| 44 display_ = display; |
| 45 } |
| 46 virtual gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const |
| 47 OVERRIDE { |
| 48 return display_; |
| 49 } |
| 50 |
40 // Making protected functions public for testing | 51 // Making protected functions public for testing |
| 52 const std::vector<string16>& names() const { |
| 53 return AutofillPopupControllerImpl::names(); |
| 54 } |
41 const std::vector<string16>& subtexts() const { | 55 const std::vector<string16>& subtexts() const { |
42 return AutofillPopupControllerImpl::subtexts(); | 56 return AutofillPopupControllerImpl::subtexts(); |
43 } | 57 } |
44 int selected_line() const { | 58 int selected_line() const { |
45 return AutofillPopupControllerImpl::selected_line(); | 59 return AutofillPopupControllerImpl::selected_line(); |
46 } | 60 } |
47 void SetSelectedLine(size_t selected_line) { | 61 void SetSelectedLine(size_t selected_line) { |
48 AutofillPopupControllerImpl::SetSelectedLine(selected_line); | 62 AutofillPopupControllerImpl::SetSelectedLine(selected_line); |
49 } | 63 } |
50 void SelectNextLine() { | 64 void SelectNextLine() { |
51 AutofillPopupControllerImpl::SelectNextLine(); | 65 AutofillPopupControllerImpl::SelectNextLine(); |
52 } | 66 } |
53 void SelectPreviousLine() { | 67 void SelectPreviousLine() { |
54 AutofillPopupControllerImpl::SelectPreviousLine(); | 68 AutofillPopupControllerImpl::SelectPreviousLine(); |
55 } | 69 } |
56 bool RemoveSelectedLine() { | 70 bool RemoveSelectedLine() { |
57 return AutofillPopupControllerImpl::RemoveSelectedLine(); | 71 return AutofillPopupControllerImpl::RemoveSelectedLine(); |
58 } | 72 } |
59 void DoHide() { | 73 void DoHide() { |
60 AutofillPopupControllerImpl::Hide(); | 74 AutofillPopupControllerImpl::Hide(); |
61 } | 75 } |
| 76 const gfx::Rect& popup_bounds() const { |
| 77 return AutofillPopupControllerImpl::popup_bounds(); |
| 78 } |
| 79 const gfx::Rect& element_bounds() const { |
| 80 return AutofillPopupControllerImpl::element_bounds(); |
| 81 } |
| 82 #if !defined(OS_ANDROID) |
| 83 const gfx::Font& name_font() const { |
| 84 return AutofillPopupControllerImpl::name_font(); |
| 85 } |
| 86 const gfx::Font& subtext_font() const { |
| 87 return AutofillPopupControllerImpl::subtext_font(); |
| 88 } |
| 89 #endif |
| 90 int GetDesiredPopupWidth() const { |
| 91 return AutofillPopupControllerImpl::GetDesiredPopupWidth(); |
| 92 } |
| 93 int GetDesiredPopupHeight() const { |
| 94 return AutofillPopupControllerImpl::GetDesiredPopupHeight(); |
| 95 } |
62 | 96 |
63 MOCK_METHOD1(InvalidateRow, void(size_t)); | 97 MOCK_METHOD1(InvalidateRow, void(size_t)); |
64 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void()); | 98 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void()); |
65 MOCK_METHOD0(Hide, void()); | 99 MOCK_METHOD0(Hide, void()); |
66 | 100 |
67 private: | 101 private: |
68 virtual void ShowView() OVERRIDE {} | 102 virtual void ShowView() OVERRIDE {} |
| 103 |
| 104 gfx::Display display_; |
69 }; | 105 }; |
70 | 106 |
71 } // namespace | 107 } // namespace |
72 | 108 |
73 class AutofillPopupControllerUnitTest : public ::testing::Test { | 109 class AutofillPopupControllerUnitTest : public ::testing::Test { |
74 public: | 110 public: |
75 AutofillPopupControllerUnitTest() | 111 AutofillPopupControllerUnitTest() |
76 : autofill_popup_controller_( | 112 : autofill_popup_controller_( |
77 new testing::NiceMock<TestAutofillPopupController>( | 113 new testing::NiceMock<TestAutofillPopupController>( |
78 &external_delegate_)) {} | 114 &external_delegate_, gfx::Rect())) {} |
79 virtual ~AutofillPopupControllerUnitTest() { | 115 virtual ~AutofillPopupControllerUnitTest() { |
80 // This will make sure the controller and the view (if any) are both | 116 // This will make sure the controller and the view (if any) are both |
81 // cleaned up. | 117 // cleaned up. |
82 if (autofill_popup_controller_) | 118 if (autofill_popup_controller_) |
83 autofill_popup_controller_->DoHide(); | 119 autofill_popup_controller_->DoHide(); |
84 } | 120 } |
85 | 121 |
86 AutofillPopupController* popup_controller() { | 122 AutofillPopupController* popup_controller() { |
87 return autofill_popup_controller_; | 123 return autofill_popup_controller_; |
88 } | 124 } |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 EXPECT_TRUE(controller); | 268 EXPECT_TRUE(controller); |
233 AutofillPopupControllerImpl* controller2 = | 269 AutofillPopupControllerImpl* controller2 = |
234 AutofillPopupControllerImpl::GetOrCreate( | 270 AutofillPopupControllerImpl::GetOrCreate( |
235 controller, | 271 controller, |
236 &delegate, | 272 &delegate, |
237 NULL, | 273 NULL, |
238 gfx::Rect()); | 274 gfx::Rect()); |
239 EXPECT_EQ(controller, controller2); | 275 EXPECT_EQ(controller, controller2); |
240 controller->Hide(); | 276 controller->Hide(); |
241 | 277 |
242 TestAutofillPopupController* test_controller = | 278 testing::NiceMock<TestAutofillPopupController>* test_controller = |
243 new TestAutofillPopupController(&delegate); | 279 new testing::NiceMock<TestAutofillPopupController>(&delegate, |
| 280 gfx::Rect()); |
244 EXPECT_CALL(*test_controller, Hide()); | 281 EXPECT_CALL(*test_controller, Hide()); |
245 | 282 |
246 gfx::Rect bounds(0, 0, 1, 2); | 283 gfx::Rect bounds(0, 0, 1, 2); |
247 AutofillPopupControllerImpl* controller3 = | 284 AutofillPopupControllerImpl* controller3 = |
248 AutofillPopupControllerImpl::GetOrCreate( | 285 AutofillPopupControllerImpl::GetOrCreate( |
249 test_controller, | 286 test_controller, |
250 &delegate, | 287 &delegate, |
251 NULL, | 288 NULL, |
252 bounds); | 289 bounds); |
253 EXPECT_EQ( | 290 EXPECT_EQ( |
254 bounds, | 291 bounds, |
255 static_cast<AutofillPopupController*>(controller3)->element_bounds()); | 292 static_cast<AutofillPopupController*>(controller3)->element_bounds()); |
256 controller3->Hide(); | 293 controller3->Hide(); |
257 | 294 |
258 EXPECT_CALL(delegate, ControllerDestroyed()); | 295 EXPECT_CALL(delegate, ControllerDestroyed()); |
259 delete test_controller; | 296 delete test_controller; |
260 } | 297 } |
| 298 |
| 299 #if !defined(OS_ANDROID) |
| 300 TEST_F(AutofillPopupControllerUnitTest, ElideText) { |
| 301 std::vector<string16> names; |
| 302 names.push_back(ASCIIToUTF16("Text that will need to be trimmed")); |
| 303 names.push_back(ASCIIToUTF16("Untrimmed")); |
| 304 |
| 305 std::vector<string16> subtexts; |
| 306 subtexts.push_back(ASCIIToUTF16("Label that will be trimmed")); |
| 307 subtexts.push_back(ASCIIToUTF16("Untrimmed")); |
| 308 |
| 309 std::vector<string16> icons(2, string16()); |
| 310 std::vector<int> autofill_ids(2, 0); |
| 311 |
| 312 // Ensure the popup will be too small to display all of the first row. |
| 313 int popup_max_width = |
| 314 autofill_popup_controller_->name_font().GetStringWidth(names[0]) + |
| 315 autofill_popup_controller_->subtext_font().GetStringWidth(subtexts[0]) - |
| 316 25; |
| 317 gfx::Rect popup_bounds = gfx::Rect(0, 0, popup_max_width, 0); |
| 318 autofill_popup_controller_->set_display(gfx::Display(0, popup_bounds)); |
| 319 |
| 320 autofill_popup_controller_->Show(names, subtexts, icons, autofill_ids); |
| 321 |
| 322 // The first element was long so it should have been trimmed. |
| 323 EXPECT_NE(names[0], autofill_popup_controller_->names()[0]); |
| 324 EXPECT_NE(subtexts[0], autofill_popup_controller_->subtexts()[0]); |
| 325 |
| 326 // The second element was shorter so it should be unchanged. |
| 327 EXPECT_EQ(names[1], autofill_popup_controller_->names()[1]); |
| 328 EXPECT_EQ(subtexts[1], autofill_popup_controller_->subtexts()[1]); |
| 329 } |
| 330 #endif |
| 331 |
| 332 TEST_F(AutofillPopupControllerUnitTest, GrowPopupInSpace) { |
| 333 std::vector<string16> names(1); |
| 334 std::vector<int> autofill_ids(1, 1); |
| 335 |
| 336 // Call Show so that GetDesired...() will be able to provide valid values. |
| 337 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 338 int desired_width = autofill_popup_controller_->GetDesiredPopupWidth(); |
| 339 int desired_height = autofill_popup_controller_->GetDesiredPopupHeight(); |
| 340 |
| 341 // Setup the visible screen space. |
| 342 gfx::Display display(0, gfx::Rect(0, 0, |
| 343 desired_width * 2, desired_height * 2)); |
| 344 |
| 345 // Store the possible element bounds and the popup bounds they should result |
| 346 // in. |
| 347 std::vector<gfx::Rect> element_bounds; |
| 348 std::vector<gfx::Rect> expected_popup_bounds; |
| 349 |
| 350 // The popup grows down and to the right. |
| 351 element_bounds.push_back(gfx::Rect(0, 0, 0, 0)); |
| 352 expected_popup_bounds.push_back( |
| 353 gfx::Rect(0, 0, desired_width, desired_height)); |
| 354 |
| 355 // The popup grows down and to the left. |
| 356 element_bounds.push_back(gfx::Rect(2 * desired_width, 0, 0, 0)); |
| 357 expected_popup_bounds.push_back( |
| 358 gfx::Rect(desired_width, 0, desired_width, desired_height)); |
| 359 |
| 360 // The popup grows up and to the right. |
| 361 element_bounds.push_back(gfx::Rect(0, 2 * desired_height, 0, 0)); |
| 362 expected_popup_bounds.push_back( |
| 363 gfx::Rect(0, desired_height, desired_width, desired_height)); |
| 364 |
| 365 // The popup grows up and to the left. |
| 366 element_bounds.push_back( |
| 367 gfx::Rect(2 * desired_width, 2 * desired_height, 0, 0)); |
| 368 expected_popup_bounds.push_back( |
| 369 gfx::Rect(desired_width, desired_height, desired_width, desired_height)); |
| 370 |
| 371 // The popup would be partial off the top and left side of the screen. |
| 372 element_bounds.push_back( |
| 373 gfx::Rect(-desired_width / 2, -desired_height / 2, 0, 0)); |
| 374 expected_popup_bounds.push_back( |
| 375 gfx::Rect(0, 0, desired_width, desired_height)); |
| 376 |
| 377 // The popup would be partially off the bottom and the right side of |
| 378 // the screen. |
| 379 element_bounds.push_back( |
| 380 gfx::Rect(desired_width * 1.5, desired_height * 1.5, 0, 0)); |
| 381 expected_popup_bounds.push_back(gfx::Rect( |
| 382 desired_width / 2, desired_height /2, desired_width, desired_height)); |
| 383 |
| 384 for (size_t i = 0; i < element_bounds.size(); ++i) { |
| 385 autofill_popup_controller_ = |
| 386 new testing::NiceMock<TestAutofillPopupController>(&external_delegate_, |
| 387 element_bounds[i]); |
| 388 autofill_popup_controller_->set_display(display); |
| 389 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 390 |
| 391 EXPECT_EQ(expected_popup_bounds[i].ToString(), |
| 392 autofill_popup_controller_->popup_bounds().ToString()) << |
| 393 "Popup bounds failed to match for test " << i; |
| 394 } |
| 395 } |
OLD | NEW |