| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/ui/cocoa/passwords/base_passwords_controller_test.h" | 5 #include "chrome/browser/ui/cocoa/passwords/base_passwords_controller_test.h" |
| 6 | 6 |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/password_manager/password_store_factory.h" | 8 #include "chrome/browser/password_manager/password_store_factory.h" |
| 9 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller_mock.h" | |
| 10 #include "components/password_manager/core/browser/mock_password_store.h" | 9 #include "components/password_manager/core/browser/mock_password_store.h" |
| 11 #include "components/password_manager/core/browser/password_manager_test_utils.h
" | 10 #include "components/password_manager/core/browser/password_manager_test_utils.h
" |
| 11 #include "content/public/browser/web_contents.h" |
| 12 #include "content/public/test/web_contents_tester.h" | 12 #include "content/public/test/web_contents_tester.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 constexpr char kSiteOrigin[] = "http://example.com/login"; | 15 constexpr char kSiteOrigin[] = "http://example.com/login"; |
| 16 } | 16 } |
| 17 | 17 |
| 18 using testing::Return; | 18 using testing::Return; |
| 19 using testing::ReturnRef; | 19 using testing::ReturnRef; |
| 20 | 20 |
| 21 ManagePasswordsControllerTest:: | 21 ManagePasswordsControllerTest:: |
| 22 ManagePasswordsControllerTest() { | 22 ManagePasswordsControllerTest() { |
| 23 } | 23 } |
| 24 | 24 |
| 25 ManagePasswordsControllerTest:: | 25 ManagePasswordsControllerTest:: |
| 26 ~ManagePasswordsControllerTest() { | 26 ~ManagePasswordsControllerTest() { |
| 27 } | 27 } |
| 28 | 28 |
| 29 void ManagePasswordsControllerTest::SetUp() { | 29 void ManagePasswordsControllerTest::SetUp() { |
| 30 CocoaProfileTest::SetUp(); | 30 CocoaProfileTest::SetUp(); |
| 31 // Create the test UIController here so that it's bound to and owned by | 31 |
| 32 // |test_web_contents_| and therefore accessible to the model. | |
| 33 test_web_contents_.reset( | 32 test_web_contents_.reset( |
| 34 content::WebContentsTester::CreateTestWebContents(profile(), NULL)); | 33 content::WebContentsTester::CreateTestWebContents(profile(), nullptr)); |
| 35 ui_controller_ = new testing::NiceMock<ManagePasswordsUIControllerMock>( | 34 ui_controller_.reset(new testing::NiceMock<PasswordsModelDelegateMock>); |
| 36 test_web_contents_.get()); | 35 ON_CALL(*ui_controller_, GetWebContents()) |
| 36 .WillByDefault(Return(test_web_contents_.get())); |
| 37 PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse( | 37 PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse( |
| 38 profile(), password_manager::BuildPasswordStore< | 38 profile(), password_manager::BuildPasswordStore< |
| 39 content::BrowserContext, | 39 content::BrowserContext, |
| 40 testing::NiceMock<password_manager::MockPasswordStore>>); | 40 testing::NiceMock<password_manager::MockPasswordStore>>); |
| 41 delegate_.reset([[ContentViewDelegateMock alloc] init]); | 41 delegate_.reset([[ContentViewDelegateMock alloc] init]); |
| 42 } | 42 } |
| 43 | 43 |
| 44 ManagePasswordsBubbleModel* | 44 ManagePasswordsBubbleModel* |
| 45 ManagePasswordsControllerTest::GetModelAndCreateIfNull() { | 45 ManagePasswordsControllerTest::GetModelAndCreateIfNull() { |
| 46 if (!model_) { | 46 if (!model_) { |
| 47 model_.reset(new ManagePasswordsBubbleModel(test_web_contents_.get(), | 47 model_.reset(new ManagePasswordsBubbleModel(ui_controller_->AsWeakPtr(), |
| 48 GetDisplayReason())); | 48 GetDisplayReason())); |
| 49 [delegate() setModel:model_.get()]; | 49 [delegate() setModel:model_.get()]; |
| 50 } | 50 } |
| 51 return model_.get(); | 51 return model_.get(); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void ManagePasswordsControllerTest::SetUpSavePendingState(bool empty_username) { | 54 void ManagePasswordsControllerTest::SetUpSavePendingState(bool empty_username) { |
| 55 autofill::PasswordForm form; | 55 autofill::PasswordForm form; |
| 56 if (!empty_username) { | 56 if (!empty_username) { |
| 57 form.username_value = base::ASCIIToUTF16("username"); | 57 form.username_value = base::ASCIIToUTF16("username"); |
| 58 } | 58 } |
| 59 EXPECT_CALL(*ui_controller_, GetPendingPassword()).WillOnce(ReturnRef(form)); | 59 EXPECT_CALL(*ui_controller_, GetPendingPassword()).WillOnce(ReturnRef(form)); |
| 60 std::vector<const autofill::PasswordForm*> forms; | 60 std::vector<const autofill::PasswordForm*> forms; |
| 61 EXPECT_CALL(*ui_controller_, GetCurrentForms()).WillOnce(ReturnRef(forms)); | 61 EXPECT_CALL(*ui_controller_, GetCurrentForms()).WillOnce(ReturnRef(forms)); |
| 62 GURL origin(kSiteOrigin); | 62 GURL origin(kSiteOrigin); |
| 63 EXPECT_CALL(*ui_controller_, GetOrigin()).WillOnce(ReturnRef(origin)); | 63 EXPECT_CALL(*ui_controller_, GetOrigin()).WillOnce(ReturnRef(origin)); |
| 64 EXPECT_CALL(*ui_controller_, GetState()) | 64 EXPECT_CALL(*ui_controller_, GetState()) |
| 65 .WillOnce(Return(password_manager::ui::PENDING_PASSWORD_STATE)); | 65 .WillOnce(Return(password_manager::ui::PENDING_PASSWORD_STATE)); |
| 66 GetModelAndCreateIfNull(); | 66 GetModelAndCreateIfNull(); |
| 67 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(ui_controller_)); | 67 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(ui_controller_.get())); |
| 68 } | 68 } |
| 69 | 69 |
| 70 void ManagePasswordsControllerTest::SetUpUpdatePendingState( | 70 void ManagePasswordsControllerTest::SetUpUpdatePendingState( |
| 71 bool multiple_forms) { | 71 bool multiple_forms) { |
| 72 autofill::PasswordForm form; | 72 autofill::PasswordForm form; |
| 73 EXPECT_CALL(*ui_controller_, GetPendingPassword()).WillOnce(ReturnRef(form)); | 73 EXPECT_CALL(*ui_controller_, GetPendingPassword()).WillOnce(ReturnRef(form)); |
| 74 std::vector<const autofill::PasswordForm*> forms; | 74 std::vector<const autofill::PasswordForm*> forms; |
| 75 forms.push_back(&form); | 75 forms.push_back(&form); |
| 76 if (multiple_forms) { | 76 if (multiple_forms) { |
| 77 forms.push_back(&form); | 77 forms.push_back(&form); |
| 78 } | 78 } |
| 79 EXPECT_CALL(*ui_controller_, GetCurrentForms()).WillOnce(ReturnRef(forms)); | 79 EXPECT_CALL(*ui_controller_, GetCurrentForms()).WillOnce(ReturnRef(forms)); |
| 80 GURL origin(kSiteOrigin); | 80 GURL origin(kSiteOrigin); |
| 81 EXPECT_CALL(*ui_controller_, GetOrigin()).WillOnce(ReturnRef(origin)); | 81 EXPECT_CALL(*ui_controller_, GetOrigin()).WillOnce(ReturnRef(origin)); |
| 82 EXPECT_CALL(*ui_controller_, GetState()) | 82 EXPECT_CALL(*ui_controller_, GetState()) |
| 83 .WillOnce(Return(password_manager::ui::PENDING_PASSWORD_UPDATE_STATE)); | 83 .WillOnce(Return(password_manager::ui::PENDING_PASSWORD_UPDATE_STATE)); |
| 84 GetModelAndCreateIfNull(); | 84 GetModelAndCreateIfNull(); |
| 85 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(ui_controller_)); | 85 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(ui_controller_.get())); |
| 86 } | 86 } |
| 87 | 87 |
| 88 void ManagePasswordsControllerTest::SetUpConfirmationState() { | 88 void ManagePasswordsControllerTest::SetUpConfirmationState() { |
| 89 GURL origin(kSiteOrigin); | 89 GURL origin(kSiteOrigin); |
| 90 EXPECT_CALL(*ui_controller_, GetOrigin()).WillOnce(ReturnRef(origin)); | 90 EXPECT_CALL(*ui_controller_, GetOrigin()).WillOnce(ReturnRef(origin)); |
| 91 EXPECT_CALL(*ui_controller_, GetState()) | 91 EXPECT_CALL(*ui_controller_, GetState()) |
| 92 .WillOnce(Return(password_manager::ui::CONFIRMATION_STATE)); | 92 .WillOnce(Return(password_manager::ui::CONFIRMATION_STATE)); |
| 93 GetModelAndCreateIfNull(); | 93 GetModelAndCreateIfNull(); |
| 94 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(ui_controller_)); | 94 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(ui_controller_.get())); |
| 95 } | 95 } |
| 96 | 96 |
| 97 void ManagePasswordsControllerTest::SetUpManageState( | 97 void ManagePasswordsControllerTest::SetUpManageState( |
| 98 const VectorConstFormPtr& forms) { | 98 const VectorConstFormPtr& forms) { |
| 99 EXPECT_CALL(*ui_controller_, GetCurrentForms()).WillOnce(ReturnRef(forms)); | 99 EXPECT_CALL(*ui_controller_, GetCurrentForms()).WillOnce(ReturnRef(forms)); |
| 100 GURL origin(kSiteOrigin); | 100 GURL origin(kSiteOrigin); |
| 101 EXPECT_CALL(*ui_controller_, GetOrigin()).WillOnce(ReturnRef(origin)); | 101 EXPECT_CALL(*ui_controller_, GetOrigin()).WillOnce(ReturnRef(origin)); |
| 102 EXPECT_CALL(*ui_controller_, GetState()) | 102 EXPECT_CALL(*ui_controller_, GetState()) |
| 103 .WillOnce(Return(password_manager::ui::MANAGE_STATE)); | 103 .WillOnce(Return(password_manager::ui::MANAGE_STATE)); |
| 104 GetModelAndCreateIfNull(); | 104 GetModelAndCreateIfNull(); |
| 105 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(ui_controller_)); | 105 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(ui_controller_.get())); |
| 106 } | 106 } |
| 107 | 107 |
| 108 ManagePasswordsBubbleModel::DisplayReason | 108 ManagePasswordsBubbleModel::DisplayReason |
| 109 ManagePasswordsControllerTest::GetDisplayReason() const { | 109 ManagePasswordsControllerTest::GetDisplayReason() const { |
| 110 return ManagePasswordsBubbleModel::AUTOMATIC; | 110 return ManagePasswordsBubbleModel::AUTOMATIC; |
| 111 } | 111 } |
| 112 | 112 |
| 113 @implementation ContentViewDelegateMock | 113 @implementation ContentViewDelegateMock |
| 114 | 114 |
| 115 @synthesize model = _model; | 115 @synthesize model = _model; |
| 116 @synthesize dismissed = _dismissed; | 116 @synthesize dismissed = _dismissed; |
| 117 | 117 |
| 118 - (void)viewShouldDismiss { | 118 - (void)viewShouldDismiss { |
| 119 _dismissed = YES; | 119 _dismissed = YES; |
| 120 } | 120 } |
| 121 | 121 |
| 122 - (void)refreshBubble { | 122 - (void)refreshBubble { |
| 123 } | 123 } |
| 124 | 124 |
| 125 @end | 125 @end |
| OLD | NEW |