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 : AutofillPopupControllerImpl(external_delegate, NULL, gfx::Rect()) { |
40 ON_CALL(*this, GetDisplayNearestPoint(_)).WillByDefault( | |
41 testing::Return(gfx::Display())); | |
42 | |
43 ON_CALL(*this, element_bounds()).WillByDefault( | |
44 testing::ReturnRef(element_bounds_)); | |
Ilya Sherman
2013/01/15 22:29:55
I think I recall Scott mentioning that gMock shoul
csharp
2013/01/16 20:56:58
Removed the two new mocks I had added.
| |
45 } | |
38 virtual ~TestAutofillPopupController() {} | 46 virtual ~TestAutofillPopupController() {} |
39 | 47 |
48 void set_element_bounds(const gfx::Rect& element_bounds) { | |
49 element_bounds_ = element_bounds; | |
50 } | |
51 | |
40 // Making protected functions public for testing | 52 // Making protected functions public for testing |
53 const std::vector<string16>& names() const { | |
54 return AutofillPopupControllerImpl::names(); | |
55 } | |
41 const std::vector<string16>& subtexts() const { | 56 const std::vector<string16>& subtexts() const { |
42 return AutofillPopupControllerImpl::subtexts(); | 57 return AutofillPopupControllerImpl::subtexts(); |
43 } | 58 } |
44 int selected_line() const { | 59 int selected_line() const { |
45 return AutofillPopupControllerImpl::selected_line(); | 60 return AutofillPopupControllerImpl::selected_line(); |
46 } | 61 } |
47 void SetSelectedLine(size_t selected_line) { | 62 void SetSelectedLine(size_t selected_line) { |
48 AutofillPopupControllerImpl::SetSelectedLine(selected_line); | 63 AutofillPopupControllerImpl::SetSelectedLine(selected_line); |
49 } | 64 } |
50 void SelectNextLine() { | 65 void SelectNextLine() { |
51 AutofillPopupControllerImpl::SelectNextLine(); | 66 AutofillPopupControllerImpl::SelectNextLine(); |
52 } | 67 } |
53 void SelectPreviousLine() { | 68 void SelectPreviousLine() { |
54 AutofillPopupControllerImpl::SelectPreviousLine(); | 69 AutofillPopupControllerImpl::SelectPreviousLine(); |
55 } | 70 } |
56 bool RemoveSelectedLine() { | 71 bool RemoveSelectedLine() { |
57 return AutofillPopupControllerImpl::RemoveSelectedLine(); | 72 return AutofillPopupControllerImpl::RemoveSelectedLine(); |
58 } | 73 } |
59 void DoHide() { | 74 void DoHide() { |
60 AutofillPopupControllerImpl::Hide(); | 75 AutofillPopupControllerImpl::Hide(); |
61 } | 76 } |
77 const gfx::Rect& popup_bounds() const { | |
78 return AutofillPopupControllerImpl::popup_bounds(); | |
79 } | |
80 #if !defined(OS_ANDROID) | |
81 const gfx::Font& name_font() const { | |
82 return AutofillPopupControllerImpl::name_font(); | |
83 } | |
84 const gfx::Font& subtext_font() const { | |
85 return AutofillPopupControllerImpl::subtext_font(); | |
86 } | |
87 #endif | |
88 int GetDesiredPopupWidth() const { | |
89 return AutofillPopupControllerImpl::GetDesiredPopupWidth(); | |
90 } | |
91 int GetDesiredPopupHeight() const { | |
92 return AutofillPopupControllerImpl::GetDesiredPopupHeight(); | |
93 } | |
62 | 94 |
95 | |
Ilya Sherman
2013/01/15 22:29:55
nit: Extra newline.
csharp
2013/01/16 20:56:58
Done.
| |
96 MOCK_CONST_METHOD0(element_bounds, const gfx::Rect&()); | |
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()); |
100 MOCK_CONST_METHOD1(GetDisplayNearestPoint, gfx::Display(const gfx::Point&)); | |
66 | 101 |
67 private: | 102 private: |
68 virtual void ShowView() OVERRIDE {} | 103 virtual void ShowView() OVERRIDE {} |
104 | |
105 gfx::Rect element_bounds_; | |
69 }; | 106 }; |
70 | 107 |
71 } // namespace | 108 } // namespace |
72 | 109 |
73 class AutofillPopupControllerUnitTest : public ::testing::Test { | 110 class AutofillPopupControllerUnitTest : public ::testing::Test { |
74 public: | 111 public: |
75 AutofillPopupControllerUnitTest() | 112 AutofillPopupControllerUnitTest() |
76 : autofill_popup_controller_( | 113 : autofill_popup_controller_( |
77 new testing::NiceMock<TestAutofillPopupController>( | 114 new testing::NiceMock<TestAutofillPopupController>( |
78 &external_delegate_)) {} | 115 &external_delegate_)) {} |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 EXPECT_TRUE(controller); | 269 EXPECT_TRUE(controller); |
233 AutofillPopupControllerImpl* controller2 = | 270 AutofillPopupControllerImpl* controller2 = |
234 AutofillPopupControllerImpl::GetOrCreate( | 271 AutofillPopupControllerImpl::GetOrCreate( |
235 controller, | 272 controller, |
236 &delegate, | 273 &delegate, |
237 NULL, | 274 NULL, |
238 gfx::Rect()); | 275 gfx::Rect()); |
239 EXPECT_EQ(controller, controller2); | 276 EXPECT_EQ(controller, controller2); |
240 controller->Hide(); | 277 controller->Hide(); |
241 | 278 |
242 TestAutofillPopupController* test_controller = | 279 testing::NiceMock<TestAutofillPopupController>* test_controller = |
243 new TestAutofillPopupController(&delegate); | 280 new testing::NiceMock<TestAutofillPopupController>(&delegate); |
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 gfx::Display display(0, popup_bounds); | |
319 EXPECT_CALL(*autofill_popup_controller_, GetDisplayNearestPoint(_) | |
320 ).WillRepeatedly(testing::Return(display)); | |
Ilya Sherman
2013/01/15 22:29:55
nit: Closing paren should be on the previous line;
csharp
2013/01/16 20:56:58
Fixed.
| |
321 | |
322 autofill_popup_controller_->Show(names, subtexts, icons, autofill_ids); | |
323 | |
324 // The first element was long so it should have been trimmed. | |
325 EXPECT_NE(names[0], autofill_popup_controller_->names()[0]); | |
326 EXPECT_NE(subtexts[0], autofill_popup_controller_->subtexts()[0]); | |
327 | |
328 // The second element was shorter so it should be unchanged. | |
329 EXPECT_EQ(names[1], autofill_popup_controller_->names()[1]); | |
330 EXPECT_EQ(subtexts[1], autofill_popup_controller_->subtexts()[1]); | |
331 } | |
332 #endif | |
333 | |
334 TEST_F(AutofillPopupControllerUnitTest, GrowPopupInSpace) { | |
335 std::vector<string16> names(1); | |
336 std::vector<int> autofill_ids(1, 1); | |
337 | |
338 // Call Show so that GetDesired...() will be able to provide valid values. | |
339 autofill_popup_controller_->Show(names, names, names, autofill_ids); | |
340 int desired_width = autofill_popup_controller_->GetDesiredPopupWidth(); | |
341 int desired_height = autofill_popup_controller_->GetDesiredPopupHeight(); | |
342 | |
343 // Setup the visible screen space. | |
344 gfx::Display display(0, gfx::Rect(0, 0, | |
345 desired_width * 2, desired_height * 2)); | |
346 EXPECT_CALL(*autofill_popup_controller_, GetDisplayNearestPoint(_) | |
347 ).WillRepeatedly(testing::Return(display)); | |
348 | |
349 // The popup grows down and to the right. | |
350 autofill_popup_controller_->set_element_bounds(gfx::Rect(0, 0, 0, 0)); | |
351 autofill_popup_controller_->Show(names, names, names, autofill_ids); | |
352 EXPECT_EQ(gfx::Rect(0, 0, desired_width, desired_height).ToString(), | |
353 autofill_popup_controller_->popup_bounds().ToString()); | |
354 | |
355 // The popup grows down and to the left. | |
356 autofill_popup_controller_->set_element_bounds( | |
357 gfx::Rect(2 * desired_width, 0, 0, 0)); | |
Ilya Sherman
2013/01/15 22:29:55
Hmm, this puts the element completely off screen,
csharp
2013/01/16 20:56:58
Added two tests for the popup only going partially
| |
358 autofill_popup_controller_->Show(names, names, names, autofill_ids); | |
359 EXPECT_EQ(gfx::Rect(desired_width, 0, | |
360 desired_width, desired_height).ToString(), | |
361 autofill_popup_controller_->popup_bounds().ToString()); | |
362 | |
363 // The popup grows up and to the right. | |
364 autofill_popup_controller_->set_element_bounds( | |
365 gfx::Rect(0, 2 * desired_height, 0, 0)); | |
366 autofill_popup_controller_->Show(names, names, names, autofill_ids); | |
367 EXPECT_EQ(gfx::Rect(0, desired_height, | |
368 desired_width, desired_height).ToString(), | |
369 autofill_popup_controller_->popup_bounds().ToString()); | |
370 | |
371 // The popup grows up and to the left. | |
372 autofill_popup_controller_->set_element_bounds( | |
373 gfx::Rect(2 * desired_width, 2 * desired_height, 0, 0)); | |
374 autofill_popup_controller_->Show(names, names, names, autofill_ids); | |
375 EXPECT_EQ(gfx::Rect(desired_width, desired_height, | |
376 desired_width, desired_height).ToString(), | |
377 autofill_popup_controller_->popup_bounds().ToString()); | |
378 } | |
OLD | NEW |