| 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/weak_ptr.h" |
| 6 #include "base/utf_string_conversions.h" | 6 #include "base/utf_string_conversions.h" |
| 7 #include "chrome/browser/autofill/test_autofill_external_delegate.h" | 7 #include "chrome/browser/autofill/test_autofill_external_delegate.h" |
| 8 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" | 8 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
| 9 #include "testing/gmock/include/gmock/gmock.h" | 9 #include "testing/gmock/include/gmock/gmock.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #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" | 12 #include "ui/gfx/display.h" |
| 13 #include "ui/gfx/rect.h" | 13 #include "ui/gfx/rect.h" |
| 14 | 14 |
| 15 using ::testing::_; | 15 using ::testing::_; |
| 16 using ::testing::AtLeast; | 16 using ::testing::AtLeast; |
| 17 using base::WeakPtr; |
| 17 using WebKit::WebAutofillClient; | 18 using WebKit::WebAutofillClient; |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 class MockAutofillExternalDelegate : | 22 class MockAutofillExternalDelegate : |
| 22 public autofill::TestAutofillExternalDelegate { | 23 public autofill::TestAutofillExternalDelegate { |
| 23 public: | 24 public: |
| 24 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; | 25 MockAutofillExternalDelegate() : TestAutofillExternalDelegate(NULL, NULL) {}; |
| 25 virtual ~MockAutofillExternalDelegate() {}; | 26 virtual ~MockAutofillExternalDelegate() {}; |
| 26 | 27 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 return AutofillPopupControllerImpl::subtext_font(); | 88 return AutofillPopupControllerImpl::subtext_font(); |
| 88 } | 89 } |
| 89 #endif | 90 #endif |
| 90 int GetDesiredPopupWidth() const { | 91 int GetDesiredPopupWidth() const { |
| 91 return AutofillPopupControllerImpl::GetDesiredPopupWidth(); | 92 return AutofillPopupControllerImpl::GetDesiredPopupWidth(); |
| 92 } | 93 } |
| 93 int GetDesiredPopupHeight() const { | 94 int GetDesiredPopupHeight() const { |
| 94 return AutofillPopupControllerImpl::GetDesiredPopupHeight(); | 95 return AutofillPopupControllerImpl::GetDesiredPopupHeight(); |
| 95 } | 96 } |
| 96 | 97 |
| 98 WeakPtr<AutofillPopupControllerImpl> GetWeakPtr() { |
| 99 return AutofillPopupControllerImpl::GetWeakPtr(); |
| 100 } |
| 101 |
| 97 MOCK_METHOD1(InvalidateRow, void(size_t)); | 102 MOCK_METHOD1(InvalidateRow, void(size_t)); |
| 98 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void()); | 103 MOCK_METHOD0(UpdateBoundsAndRedrawPopup, void()); |
| 99 MOCK_METHOD0(Hide, void()); | 104 MOCK_METHOD0(Hide, void()); |
| 100 | 105 |
| 101 private: | 106 private: |
| 102 virtual void ShowView() OVERRIDE {} | 107 virtual void ShowView() OVERRIDE {} |
| 103 | 108 |
| 104 gfx::Display display_; | 109 gfx::Display display_; |
| 105 }; | 110 }; |
| 106 | 111 |
| 107 } // namespace | 112 } // namespace |
| 108 | 113 |
| 109 class AutofillPopupControllerUnitTest : public ::testing::Test { | 114 class AutofillPopupControllerUnitTest : public ::testing::Test { |
| 110 public: | 115 public: |
| 111 AutofillPopupControllerUnitTest() | 116 AutofillPopupControllerUnitTest() |
| 112 : autofill_popup_controller_( | 117 : autofill_popup_controller_( |
| 113 new testing::NiceMock<TestAutofillPopupController>( | 118 new testing::NiceMock<TestAutofillPopupController>( |
| 114 &external_delegate_, gfx::Rect())) {} | 119 &external_delegate_, gfx::Rect())) {} |
| 120 |
| 115 virtual ~AutofillPopupControllerUnitTest() { | 121 virtual ~AutofillPopupControllerUnitTest() { |
| 116 // This will make sure the controller and the view (if any) are both | 122 // This will make sure the controller and the view (if any) are both |
| 117 // cleaned up. | 123 // cleaned up. |
| 118 if (autofill_popup_controller_) | 124 if (autofill_popup_controller_) |
| 119 autofill_popup_controller_->DoHide(); | 125 autofill_popup_controller_->DoHide(); |
| 120 } | 126 } |
| 121 | 127 |
| 122 AutofillPopupController* popup_controller() { | 128 AutofillPopupController* popup_controller() { |
| 123 return autofill_popup_controller_; | 129 return autofill_popup_controller_; |
| 124 } | 130 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 EXPECT_LE(0, autofill_popup_controller_->selected_line()); | 215 EXPECT_LE(0, autofill_popup_controller_->selected_line()); |
| 210 | 216 |
| 211 // Remove the first entry. The popup should be redrawn since its size has | 217 // Remove the first entry. The popup should be redrawn since its size has |
| 212 // changed. | 218 // changed. |
| 213 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); | 219 EXPECT_CALL(*autofill_popup_controller_, UpdateBoundsAndRedrawPopup()); |
| 214 autofill_popup_controller_->SetSelectedLine(0); | 220 autofill_popup_controller_->SetSelectedLine(0); |
| 215 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 221 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); |
| 216 | 222 |
| 217 // Remove the last entry. The popup should then be hidden since there are | 223 // Remove the last entry. The popup should then be hidden since there are |
| 218 // no Autofill entries left. | 224 // no Autofill entries left. |
| 219 EXPECT_CALL(external_delegate_, ControllerDestroyed()); | |
| 220 | |
| 221 autofill_popup_controller_->SetSelectedLine(0); | 225 autofill_popup_controller_->SetSelectedLine(0); |
| 222 // The controller self-deletes here, don't double delete. | 226 // The controller self-deletes here, don't double delete. |
| 223 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); | 227 EXPECT_TRUE(autofill_popup_controller_->RemoveSelectedLine()); |
| 224 autofill_popup_controller_ = NULL; | 228 autofill_popup_controller_ = NULL; |
| 225 } | 229 } |
| 226 | 230 |
| 227 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { | 231 TEST_F(AutofillPopupControllerUnitTest, SkipSeparator) { |
| 228 // Set up the popup. | 232 // Set up the popup. |
| 229 std::vector<string16> names(3, string16()); | 233 std::vector<string16> names(3, string16()); |
| 230 std::vector<int> autofill_ids; | 234 std::vector<int> autofill_ids; |
| 231 autofill_ids.push_back(1); | 235 autofill_ids.push_back(1); |
| 232 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator); | 236 autofill_ids.push_back(WebAutofillClient::MenuItemIDSeparator); |
| 233 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); | 237 autofill_ids.push_back(WebAutofillClient::MenuItemIDAutofillOptions); |
| 234 autofill_popup_controller_->Show(names, names, names, autofill_ids); | 238 autofill_popup_controller_->Show(names, names, names, autofill_ids); |
| 235 | 239 |
| 236 autofill_popup_controller_->SetSelectedLine(0); | 240 autofill_popup_controller_->SetSelectedLine(0); |
| 237 | 241 |
| 238 // Make sure next skips the unselectable separator. | 242 // Make sure next skips the unselectable separator. |
| 239 autofill_popup_controller_->SelectNextLine(); | 243 autofill_popup_controller_->SelectNextLine(); |
| 240 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); | 244 EXPECT_EQ(2, autofill_popup_controller_->selected_line()); |
| 241 | 245 |
| 242 // Make sure previous skips the unselectable separator. | 246 // Make sure previous skips the unselectable separator. |
| 243 autofill_popup_controller_->SelectPreviousLine(); | 247 autofill_popup_controller_->SelectPreviousLine(); |
| 244 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); | 248 EXPECT_EQ(0, autofill_popup_controller_->selected_line()); |
| 245 } | 249 } |
| 246 | 250 |
| 247 TEST_F(AutofillPopupControllerUnitTest, GetOrCreate) { | 251 TEST_F(AutofillPopupControllerUnitTest, GetOrCreate) { |
| 248 MockAutofillExternalDelegate delegate; | 252 MockAutofillExternalDelegate delegate; |
| 249 | 253 |
| 250 AutofillPopupControllerImpl* controller = | 254 WeakPtr<AutofillPopupControllerImpl> controller = |
| 251 AutofillPopupControllerImpl::GetOrCreate( | 255 AutofillPopupControllerImpl::GetOrCreate( |
| 252 NULL, | 256 WeakPtr<AutofillPopupControllerImpl>(), &delegate, NULL, gfx::Rect()); |
| 253 &delegate, | |
| 254 NULL, | |
| 255 gfx::Rect()); | |
| 256 EXPECT_TRUE(controller); | 257 EXPECT_TRUE(controller); |
| 257 | 258 |
| 258 // This should not inform |delegate| of its destruction. | |
| 259 EXPECT_CALL(delegate, ControllerDestroyed()).Times(0); | |
| 260 controller->Hide(); | 259 controller->Hide(); |
| 261 | 260 |
| 262 controller = | 261 controller = AutofillPopupControllerImpl::GetOrCreate( |
| 263 AutofillPopupControllerImpl::GetOrCreate( | 262 WeakPtr<AutofillPopupControllerImpl>(), &delegate, NULL, gfx::Rect()); |
| 264 NULL, | |
| 265 &delegate, | |
| 266 NULL, | |
| 267 gfx::Rect()); | |
| 268 EXPECT_TRUE(controller); | 263 EXPECT_TRUE(controller); |
| 269 AutofillPopupControllerImpl* controller2 = | 264 |
| 270 AutofillPopupControllerImpl::GetOrCreate( | 265 WeakPtr<AutofillPopupControllerImpl> controller2 = |
| 271 controller, | 266 AutofillPopupControllerImpl::GetOrCreate(controller, &delegate, NULL, |
| 272 &delegate, | 267 gfx::Rect()); |
| 273 NULL, | 268 EXPECT_EQ(controller.get(), controller2.get()); |
| 274 gfx::Rect()); | |
| 275 EXPECT_EQ(controller, controller2); | |
| 276 controller->Hide(); | 269 controller->Hide(); |
| 277 | 270 |
| 278 testing::NiceMock<TestAutofillPopupController>* test_controller = | 271 testing::NiceMock<TestAutofillPopupController>* test_controller = |
| 279 new testing::NiceMock<TestAutofillPopupController>(&delegate, | 272 new testing::NiceMock<TestAutofillPopupController>(&delegate, |
| 280 gfx::Rect()); | 273 gfx::Rect()); |
| 281 EXPECT_CALL(*test_controller, Hide()); | 274 EXPECT_CALL(*test_controller, Hide()); |
| 282 | 275 |
| 283 gfx::RectF bounds(0.f, 0.f, 1.f, 2.f); | 276 gfx::RectF bounds(0.f, 0.f, 1.f, 2.f); |
| 284 AutofillPopupControllerImpl* controller3 = | 277 AutofillPopupControllerImpl* controller3 = |
| 285 AutofillPopupControllerImpl::GetOrCreate( | 278 AutofillPopupControllerImpl::GetOrCreate( |
| 286 test_controller, | 279 test_controller->GetWeakPtr(), |
| 287 &delegate, | 280 &delegate, |
| 288 NULL, | 281 NULL, |
| 289 bounds); | 282 bounds); |
| 290 EXPECT_EQ( | 283 EXPECT_EQ( |
| 291 bounds, | 284 bounds, |
| 292 static_cast<AutofillPopupController*>(controller3)->element_bounds()); | 285 static_cast<AutofillPopupController*>(controller3)->element_bounds()); |
| 293 controller3->Hide(); | 286 controller3->Hide(); |
| 294 | 287 |
| 295 EXPECT_CALL(delegate, ControllerDestroyed()); | |
| 296 delete test_controller; | 288 delete test_controller; |
| 297 } | 289 } |
| 298 | 290 |
| 299 #if !defined(OS_ANDROID) | 291 #if !defined(OS_ANDROID) |
| 300 TEST_F(AutofillPopupControllerUnitTest, ElideText) { | 292 TEST_F(AutofillPopupControllerUnitTest, ElideText) { |
| 301 std::vector<string16> names; | 293 std::vector<string16> names; |
| 302 names.push_back(ASCIIToUTF16("Text that will need to be trimmed")); | 294 names.push_back(ASCIIToUTF16("Text that will need to be trimmed")); |
| 303 names.push_back(ASCIIToUTF16("Untrimmed")); | 295 names.push_back(ASCIIToUTF16("Untrimmed")); |
| 304 | 296 |
| 305 std::vector<string16> subtexts; | 297 std::vector<string16> subtexts; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 385 TestAutofillPopupController autofill_popup_controller(&external_delegate_, | 377 TestAutofillPopupController autofill_popup_controller(&external_delegate_, |
| 386 element_bounds[i]); | 378 element_bounds[i]); |
| 387 autofill_popup_controller.set_display(display); | 379 autofill_popup_controller.set_display(display); |
| 388 autofill_popup_controller.Show(names, names, names, autofill_ids); | 380 autofill_popup_controller.Show(names, names, names, autofill_ids); |
| 389 | 381 |
| 390 EXPECT_EQ(expected_popup_bounds[i].ToString(), | 382 EXPECT_EQ(expected_popup_bounds[i].ToString(), |
| 391 autofill_popup_controller.popup_bounds().ToString()) << | 383 autofill_popup_controller.popup_bounds().ToString()) << |
| 392 "Popup bounds failed to match for test " << i; | 384 "Popup bounds failed to match for test " << i; |
| 393 } | 385 } |
| 394 } | 386 } |
| OLD | NEW |