| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/password_manager/account_chooser_dialog_android.h" |
| 6 |
| 7 #include "base/macros.h" |
| 8 #include "base/test/histogram_tester.h" |
| 9 #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 12 #include "components/autofill/core/common/password_form.h" |
| 13 #include "components/password_manager/core/browser/password_manager_metrics_util
.h" |
| 14 #include "components/password_manager/core/browser/password_manager_test_utils.h
" |
| 15 #include "components/password_manager/core/common/password_manager_pref_names.h" |
| 16 #include "content/public/browser/web_contents.h" |
| 17 #include "testing/gmock/include/gmock/gmock.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 password_manager::PasswordFormData kFormData = { |
| 23 autofill::PasswordForm::SCHEME_HTML, |
| 24 "http://example.com/", |
| 25 "http://example.com/origin", |
| 26 "http://example.com/action", |
| 27 L"submit_element", |
| 28 L"username_element", |
| 29 L"password_element", |
| 30 L"", |
| 31 L"", |
| 32 true, |
| 33 false, |
| 34 1, |
| 35 }; |
| 36 |
| 37 } // namespace |
| 38 |
| 39 class AccountChooserDialogAndroidTest : public ChromeRenderViewHostTestHarness { |
| 40 public: |
| 41 AccountChooserDialogAndroidTest() {} |
| 42 ~AccountChooserDialogAndroidTest() override {} |
| 43 |
| 44 void SetUp() override; |
| 45 |
| 46 MOCK_METHOD1(OnChooseCredential, void(const autofill::PasswordForm*)); |
| 47 |
| 48 protected: |
| 49 AccountChooserDialogAndroid* CreateDialogOneAccount(); |
| 50 AccountChooserDialogAndroid* CreateDialogManyAccounts(); |
| 51 |
| 52 AccountChooserDialogAndroid* CreateDialog( |
| 53 ScopedVector<autofill::PasswordForm> credentials); |
| 54 |
| 55 private: |
| 56 DISALLOW_COPY_AND_ASSIGN(AccountChooserDialogAndroidTest); |
| 57 }; |
| 58 |
| 59 void AccountChooserDialogAndroidTest::SetUp() { |
| 60 ChromeRenderViewHostTestHarness::SetUp(); |
| 61 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient( |
| 62 web_contents(), nullptr); |
| 63 } |
| 64 |
| 65 AccountChooserDialogAndroid* AccountChooserDialogAndroidTest::CreateDialog( |
| 66 ScopedVector<autofill::PasswordForm> credentials) { |
| 67 ScopedVector<autofill::PasswordForm> deprecated_federated; |
| 68 return new AccountChooserDialogAndroid( |
| 69 web_contents(), std::move(credentials), std::move(deprecated_federated), |
| 70 GURL("https://example.com"), |
| 71 base::Bind(&AccountChooserDialogAndroidTest::OnChooseCredential, |
| 72 base::Unretained(this))); |
| 73 } |
| 74 |
| 75 AccountChooserDialogAndroid* |
| 76 AccountChooserDialogAndroidTest::CreateDialogOneAccount() { |
| 77 ScopedVector<autofill::PasswordForm> credentials; |
| 78 credentials.push_back(CreatePasswordFormFromDataForTesting(kFormData)); |
| 79 return CreateDialog(std::move(credentials)); |
| 80 } |
| 81 |
| 82 AccountChooserDialogAndroid* |
| 83 AccountChooserDialogAndroidTest::CreateDialogManyAccounts() { |
| 84 ScopedVector<autofill::PasswordForm> credentials; |
| 85 credentials.push_back(CreatePasswordFormFromDataForTesting(kFormData)); |
| 86 credentials.push_back(CreatePasswordFormFromDataForTesting(kFormData)); |
| 87 return CreateDialog(std::move(credentials)); |
| 88 } |
| 89 |
| 90 TEST_F(AccountChooserDialogAndroidTest, |
| 91 CheckHistogramsReportingOnceAccountViaOnAccountClick) { |
| 92 base::HistogramTester histogram_tester; |
| 93 AccountChooserDialogAndroid* dialog = CreateDialogOneAccount(); |
| 94 dialog->OnCredentialClicked( |
| 95 base::android::AttachCurrentThread(), nullptr /* obj */, |
| 96 0 /* credential_item */, |
| 97 static_cast<int>( |
| 98 password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD), |
| 99 false /* signin_button_clicked */); |
| 100 dialog->Destroy(base::android::AttachCurrentThread(), nullptr); |
| 101 |
| 102 histogram_tester.ExpectUniqueSample( |
| 103 "PasswordManager.AccountChooserDialog", |
| 104 password_manager::metrics_util::ACCOUNT_CHOOSER_CREDENTIAL_CHOSEN, 1); |
| 105 histogram_tester.ExpectUniqueSample( |
| 106 "PasswordManager.AccountChooserDialogOneAccount", |
| 107 password_manager::metrics_util::ACCOUNT_CHOOSER_CREDENTIAL_CHOSEN, 1); |
| 108 histogram_tester.ExpectTotalCount( |
| 109 "PasswordManager.AccountChooserDialogMultipleAccounts", 0); |
| 110 } |
| 111 |
| 112 TEST_F(AccountChooserDialogAndroidTest, |
| 113 CheckHistogramsReportingOneAccountChoosenViaSigninButton) { |
| 114 base::HistogramTester histogram_tester; |
| 115 AccountChooserDialogAndroid* dialog = CreateDialogOneAccount(); |
| 116 dialog->OnCredentialClicked( |
| 117 base::android::AttachCurrentThread(), nullptr /* obj */, |
| 118 0 /* credential_item */, |
| 119 static_cast<int>( |
| 120 password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD), |
| 121 true /* signin_button_clicked */); |
| 122 dialog->Destroy(base::android::AttachCurrentThread(), nullptr); |
| 123 histogram_tester.ExpectUniqueSample( |
| 124 "PasswordManager.AccountChooserDialog", |
| 125 password_manager::metrics_util::ACCOUNT_CHOOSER_SIGN_IN, 1); |
| 126 histogram_tester.ExpectUniqueSample( |
| 127 "PasswordManager.AccountChooserDialogOneAccount", |
| 128 password_manager::metrics_util::ACCOUNT_CHOOSER_SIGN_IN, 1); |
| 129 histogram_tester.ExpectTotalCount( |
| 130 "PasswordManager.AccountChooserDialogMultipleAccounts", 0); |
| 131 } |
| 132 |
| 133 TEST_F(AccountChooserDialogAndroidTest, CheckHistogramsReportingManyAccounts) { |
| 134 base::HistogramTester histogram_tester; |
| 135 AccountChooserDialogAndroid* dialog = CreateDialogManyAccounts(); |
| 136 dialog->OnCredentialClicked( |
| 137 base::android::AttachCurrentThread(), nullptr /* obj */, |
| 138 0 /* credential_item */, |
| 139 static_cast<int>( |
| 140 password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD), |
| 141 false /* signin_button_clicked */); |
| 142 dialog->Destroy(base::android::AttachCurrentThread(), nullptr); |
| 143 |
| 144 histogram_tester.ExpectUniqueSample( |
| 145 "PasswordManager.AccountChooserDialog", |
| 146 password_manager::metrics_util::ACCOUNT_CHOOSER_CREDENTIAL_CHOSEN, 1); |
| 147 histogram_tester.ExpectUniqueSample( |
| 148 "PasswordManager.AccountChooserDialogMultipleAccounts", |
| 149 password_manager::metrics_util::ACCOUNT_CHOOSER_CREDENTIAL_CHOSEN, 1); |
| 150 histogram_tester.ExpectTotalCount( |
| 151 "PasswordManager.AccountChooserDialogOneAccount", 0); |
| 152 } |
| OLD | NEW |