| 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/passwords/manage_passwords_bubble_model.h" | 5 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/metrics/field_trial.h" | 11 #include "base/metrics/field_trial.h" |
| 12 #include "base/metrics/histogram_samples.h" | 12 #include "base/metrics/histogram_samples.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "base/test/histogram_tester.h" | 14 #include "base/test/histogram_tester.h" |
| 15 #include "base/test/simple_test_clock.h" | 15 #include "base/test/simple_test_clock.h" |
| 16 #include "chrome/browser/password_manager/password_store_factory.h" | 16 #include "chrome/browser/password_manager/password_store_factory.h" |
| 17 #include "chrome/browser/sync/profile_sync_service_factory.h" | 17 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 18 #include "chrome/browser/sync/profile_sync_test_util.h" | 18 #include "chrome/browser/sync/profile_sync_test_util.h" |
| 19 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller_mock.h" | 19 #include "chrome/browser/ui/passwords/passwords_model_delegate_mock.h" |
| 20 #include "chrome/test/base/testing_profile.h" | 20 #include "chrome/test/base/testing_profile.h" |
| 21 #include "components/browser_sync/browser/profile_sync_service_mock.h" | 21 #include "components/browser_sync/browser/profile_sync_service_mock.h" |
| 22 #include "components/password_manager/core/browser/mock_password_store.h" | 22 #include "components/password_manager/core/browser/mock_password_store.h" |
| 23 #include "components/password_manager/core/browser/password_bubble_experiment.h" | 23 #include "components/password_manager/core/browser/password_bubble_experiment.h" |
| 24 #include "components/password_manager/core/browser/password_manager_metrics_util
.h" | 24 #include "components/password_manager/core/browser/password_manager_metrics_util
.h" |
| 25 #include "components/password_manager/core/browser/password_manager_test_utils.h
" | 25 #include "components/password_manager/core/browser/password_manager_test_utils.h
" |
| 26 #include "components/password_manager/core/browser/statistics_table.h" | 26 #include "components/password_manager/core/browser/statistics_table.h" |
| 27 #include "components/password_manager/core/common/credential_manager_types.h" | 27 #include "components/password_manager/core/common/credential_manager_types.h" |
| 28 #include "components/password_manager/core/common/password_manager_pref_names.h" | 28 #include "components/password_manager/core/common/password_manager_pref_names.h" |
| 29 #include "components/password_manager/core/common/password_manager_ui.h" | 29 #include "components/password_manager/core/common/password_manager_ui.h" |
| 30 #include "components/prefs/pref_service.h" | 30 #include "components/prefs/pref_service.h" |
| 31 #include "components/variations/variations_associated_data.h" | 31 #include "components/variations/variations_associated_data.h" |
| 32 #include "content/public/browser/web_contents.h" |
| 32 #include "content/public/test/test_browser_thread_bundle.h" | 33 #include "content/public/test/test_browser_thread_bundle.h" |
| 33 #include "content/public/test/web_contents_tester.h" | 34 #include "content/public/test/web_contents_tester.h" |
| 34 #include "testing/gmock/include/gmock/gmock.h" | 35 #include "testing/gmock/include/gmock/gmock.h" |
| 35 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 36 | 37 |
| 37 using password_bubble_experiment::kBrandingExperimentName; | 38 using password_bubble_experiment::kBrandingExperimentName; |
| 38 using password_bubble_experiment::kChromeSignInPasswordPromoExperimentName; | 39 using password_bubble_experiment::kChromeSignInPasswordPromoExperimentName; |
| 39 using password_bubble_experiment::kChromeSignInPasswordPromoThresholdParam; | 40 using password_bubble_experiment::kChromeSignInPasswordPromoThresholdParam; |
| 40 using password_bubble_experiment::kSmartLockBrandingGroupName; | 41 using password_bubble_experiment::kSmartLockBrandingGroupName; |
| 41 using password_bubble_experiment::kSmartLockBrandingSavePromptOnlyGroupName; | 42 using password_bubble_experiment::kSmartLockBrandingSavePromptOnlyGroupName; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 89 } // namespace | 90 } // namespace |
| 90 | 91 |
| 91 class ManagePasswordsBubbleModelTest : public ::testing::Test { | 92 class ManagePasswordsBubbleModelTest : public ::testing::Test { |
| 92 public: | 93 public: |
| 93 ManagePasswordsBubbleModelTest() : field_trials_(nullptr) {} | 94 ManagePasswordsBubbleModelTest() : field_trials_(nullptr) {} |
| 94 ~ManagePasswordsBubbleModelTest() override = default; | 95 ~ManagePasswordsBubbleModelTest() override = default; |
| 95 | 96 |
| 96 void SetUp() override { | 97 void SetUp() override { |
| 97 test_web_contents_.reset( | 98 test_web_contents_.reset( |
| 98 content::WebContentsTester::CreateTestWebContents(&profile_, nullptr)); | 99 content::WebContentsTester::CreateTestWebContents(&profile_, nullptr)); |
| 99 // Create the test UIController here so that it's bound to | 100 mock_delegate_.reset(new testing::StrictMock<PasswordsModelDelegateMock>); |
| 100 // |test_web_contents_| and therefore accessible to the model. | |
| 101 new testing::StrictMock<ManagePasswordsUIControllerMock>( | |
| 102 test_web_contents_.get()); | |
| 103 PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse( | 101 PasswordStoreFactory::GetInstance()->SetTestingFactoryAndUse( |
| 104 profile(), | 102 profile(), |
| 105 password_manager::BuildPasswordStore< | 103 password_manager::BuildPasswordStore< |
| 106 content::BrowserContext, | 104 content::BrowserContext, |
| 107 testing::StrictMock<password_manager::MockPasswordStore>>); | 105 testing::StrictMock<password_manager::MockPasswordStore>>); |
| 108 } | 106 } |
| 109 | 107 |
| 110 void TearDown() override { | 108 void TearDown() override { |
| 111 // Reset WebContents first. It can happen if the user closes the tab. | 109 // Reset the delegate first. It can happen if the user closes the tab. |
| 112 test_web_contents_.reset(); | 110 mock_delegate_.reset(); |
| 113 model_.reset(); | 111 model_.reset(); |
| 114 variations::testing::ClearAllVariationIDs(); | 112 variations::testing::ClearAllVariationIDs(); |
| 115 variations::testing::ClearAllVariationParams(); | 113 variations::testing::ClearAllVariationParams(); |
| 116 } | 114 } |
| 117 | 115 |
| 118 PrefService* prefs() { return profile_.GetPrefs(); } | 116 PrefService* prefs() { return profile_.GetPrefs(); } |
| 119 | 117 |
| 120 TestingProfile* profile() { return &profile_; } | 118 TestingProfile* profile() { return &profile_; } |
| 121 | 119 |
| 122 password_manager::MockPasswordStore* GetStore() { | 120 password_manager::MockPasswordStore* GetStore() { |
| 123 return static_cast<password_manager::MockPasswordStore*>( | 121 return static_cast<password_manager::MockPasswordStore*>( |
| 124 PasswordStoreFactory::GetInstance() | 122 PasswordStoreFactory::GetInstance() |
| 125 ->GetForProfile(profile(), ServiceAccessType::EXPLICIT_ACCESS) | 123 ->GetForProfile(profile(), ServiceAccessType::EXPLICIT_ACCESS) |
| 126 .get()); | 124 .get()); |
| 127 } | 125 } |
| 128 | 126 |
| 129 ManagePasswordsUIControllerMock* controller() { | 127 PasswordsModelDelegateMock* controller() { |
| 130 return static_cast<ManagePasswordsUIControllerMock*>( | 128 return mock_delegate_.get(); |
| 131 PasswordsModelDelegateFromWebContents(test_web_contents_.get())); | |
| 132 } | 129 } |
| 133 | 130 |
| 134 content::WebContents* test_web_contents() { return test_web_contents_.get(); } | |
| 135 | |
| 136 ManagePasswordsBubbleModel* model() { return model_.get(); } | 131 ManagePasswordsBubbleModel* model() { return model_.get(); } |
| 137 | 132 |
| 138 void SetUpWithState(password_manager::ui::State state, | 133 void SetUpWithState(password_manager::ui::State state, |
| 139 ManagePasswordsBubbleModel::DisplayReason reason); | 134 ManagePasswordsBubbleModel::DisplayReason reason); |
| 140 void PretendPasswordWaiting(); | 135 void PretendPasswordWaiting(); |
| 141 void PretendUpdatePasswordWaiting(); | 136 void PretendUpdatePasswordWaiting(); |
| 142 void PretendAutoSigningIn(); | 137 void PretendAutoSigningIn(); |
| 143 void PretendManagingPasswords(); | 138 void PretendManagingPasswords(); |
| 144 | 139 |
| 145 void DestroyModel(); | 140 void DestroyModel(); |
| 146 void DestroyModelExpectReason( | 141 void DestroyModelExpectReason( |
| 147 password_manager::metrics_util::UIDismissalReason dismissal_reason); | 142 password_manager::metrics_util::UIDismissalReason dismissal_reason); |
| 148 | 143 |
| 149 static password_manager::InteractionsStats GetTestStats(); | 144 static password_manager::InteractionsStats GetTestStats(); |
| 150 static autofill::PasswordForm GetPendingPassword(); | 145 static autofill::PasswordForm GetPendingPassword(); |
| 151 | 146 |
| 152 private: | 147 private: |
| 153 content::TestBrowserThreadBundle thread_bundle_; | 148 content::TestBrowserThreadBundle thread_bundle_; |
| 154 TestingProfile profile_; | 149 TestingProfile profile_; |
| 155 std::unique_ptr<content::WebContents> test_web_contents_; | 150 std::unique_ptr<content::WebContents> test_web_contents_; |
| 156 base::FieldTrialList field_trials_; | 151 base::FieldTrialList field_trials_; |
| 157 std::unique_ptr<ManagePasswordsBubbleModel> model_; | 152 std::unique_ptr<ManagePasswordsBubbleModel> model_; |
| 153 std::unique_ptr<PasswordsModelDelegateMock> mock_delegate_; |
| 158 }; | 154 }; |
| 159 | 155 |
| 160 void ManagePasswordsBubbleModelTest::SetUpWithState( | 156 void ManagePasswordsBubbleModelTest::SetUpWithState( |
| 161 password_manager::ui::State state, | 157 password_manager::ui::State state, |
| 162 ManagePasswordsBubbleModel::DisplayReason reason) { | 158 ManagePasswordsBubbleModel::DisplayReason reason) { |
| 163 ManagePasswordsUIControllerMock* mock = controller(); | |
| 164 GURL origin(kSiteOrigin); | 159 GURL origin(kSiteOrigin); |
| 165 EXPECT_CALL(*mock, GetOrigin()).WillOnce(ReturnRef(origin)); | 160 EXPECT_CALL(*controller(), GetOrigin()).WillOnce(ReturnRef(origin)); |
| 166 EXPECT_CALL(*mock, GetState()).WillOnce(Return(state)); | 161 EXPECT_CALL(*controller(), GetState()).WillOnce(Return(state)); |
| 167 EXPECT_CALL(*mock, OnBubbleShown()); | 162 EXPECT_CALL(*controller(), OnBubbleShown()); |
| 163 EXPECT_CALL(*controller(), GetWebContents()).WillRepeatedly( |
| 164 Return(test_web_contents_.get())); |
| 168 model_.reset( | 165 model_.reset( |
| 169 new ManagePasswordsBubbleModel(test_web_contents_.get(), reason)); | 166 new ManagePasswordsBubbleModel(mock_delegate_->AsWeakPtr(), reason)); |
| 170 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(mock)); | 167 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(controller())); |
| 168 EXPECT_CALL(*controller(), GetWebContents()).WillRepeatedly( |
| 169 Return(test_web_contents_.get())); |
| 171 } | 170 } |
| 172 | 171 |
| 173 void ManagePasswordsBubbleModelTest::PretendPasswordWaiting() { | 172 void ManagePasswordsBubbleModelTest::PretendPasswordWaiting() { |
| 174 autofill::PasswordForm form = GetPendingPassword(); | 173 autofill::PasswordForm form = GetPendingPassword(); |
| 175 EXPECT_CALL(*controller(), GetPendingPassword()).WillOnce(ReturnRef(form)); | 174 EXPECT_CALL(*controller(), GetPendingPassword()).WillOnce(ReturnRef(form)); |
| 176 std::vector<const autofill::PasswordForm*> forms; | 175 std::vector<const autofill::PasswordForm*> forms; |
| 177 EXPECT_CALL(*controller(), GetCurrentForms()).WillOnce(ReturnRef(forms)); | 176 EXPECT_CALL(*controller(), GetCurrentForms()).WillOnce(ReturnRef(forms)); |
| 178 password_manager::InteractionsStats stats = GetTestStats(); | 177 password_manager::InteractionsStats stats = GetTestStats(); |
| 179 EXPECT_CALL(*controller(), GetCurrentInteractionStats()) | 178 EXPECT_CALL(*controller(), GetCurrentInteractionStats()) |
| 180 .WillOnce(Return(&stats)); | 179 .WillOnce(Return(&stats)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 201 | 200 |
| 202 void ManagePasswordsBubbleModelTest::PretendManagingPasswords() { | 201 void ManagePasswordsBubbleModelTest::PretendManagingPasswords() { |
| 203 autofill::PasswordForm form = GetPendingPassword(); | 202 autofill::PasswordForm form = GetPendingPassword(); |
| 204 std::vector<const autofill::PasswordForm*> forms(1, &form); | 203 std::vector<const autofill::PasswordForm*> forms(1, &form); |
| 205 EXPECT_CALL(*controller(), GetCurrentForms()).WillOnce(ReturnRef(forms)); | 204 EXPECT_CALL(*controller(), GetCurrentForms()).WillOnce(ReturnRef(forms)); |
| 206 SetUpWithState(password_manager::ui::MANAGE_STATE, | 205 SetUpWithState(password_manager::ui::MANAGE_STATE, |
| 207 ManagePasswordsBubbleModel::USER_ACTION); | 206 ManagePasswordsBubbleModel::USER_ACTION); |
| 208 } | 207 } |
| 209 | 208 |
| 210 void ManagePasswordsBubbleModelTest::DestroyModel() { | 209 void ManagePasswordsBubbleModelTest::DestroyModel() { |
| 211 ManagePasswordsUIControllerMock* mock = | 210 EXPECT_CALL(*controller(), OnBubbleHidden()); |
| 212 test_web_contents_ ? controller() : nullptr; | |
| 213 if (mock) | |
| 214 EXPECT_CALL(*mock, OnBubbleHidden()); | |
| 215 model_.reset(); | 211 model_.reset(); |
| 216 if (mock) | 212 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(controller())); |
| 217 ASSERT_TRUE(testing::Mock::VerifyAndClearExpectations(mock)); | |
| 218 } | 213 } |
| 219 | 214 |
| 220 void ManagePasswordsBubbleModelTest::DestroyModelExpectReason( | 215 void ManagePasswordsBubbleModelTest::DestroyModelExpectReason( |
| 221 password_manager::metrics_util::UIDismissalReason dismissal_reason) { | 216 password_manager::metrics_util::UIDismissalReason dismissal_reason) { |
| 222 base::HistogramTester histogram_tester; | 217 base::HistogramTester histogram_tester; |
| 223 DestroyModel(); | 218 DestroyModel(); |
| 224 histogram_tester.ExpectUniqueSample(kUIDismissalReasonMetric, | 219 histogram_tester.ExpectUniqueSample(kUIDismissalReasonMetric, |
| 225 dismissal_reason, 1); | 220 dismissal_reason, 1); |
| 226 } | 221 } |
| 227 | 222 |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 568 ManageLinkTarget::SETTINGS_PAGE}, | 563 ManageLinkTarget::SETTINGS_PAGE}, |
| 569 {nullptr, SmartLockStatus::DISABLE, ManageLinkTarget::SETTINGS_PAGE}, | 564 {nullptr, SmartLockStatus::DISABLE, ManageLinkTarget::SETTINGS_PAGE}, |
| 570 {"Default", SmartLockStatus::DISABLE, ManageLinkTarget::SETTINGS_PAGE}, | 565 {"Default", SmartLockStatus::DISABLE, ManageLinkTarget::SETTINGS_PAGE}, |
| 571 }; | 566 }; |
| 572 | 567 |
| 573 } // namespace | 568 } // namespace |
| 574 | 569 |
| 575 INSTANTIATE_TEST_CASE_P(Default, | 570 INSTANTIATE_TEST_CASE_P(Default, |
| 576 ManagePasswordsBubbleModelManageLinkTest, | 571 ManagePasswordsBubbleModelManageLinkTest, |
| 577 ::testing::ValuesIn(kManageLinkTestCases)); | 572 ::testing::ValuesIn(kManageLinkTestCases)); |
| OLD | NEW |