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
3 // found in the LICENSE file.
5 #include "base/metrics/histogram_samples.h"
6 #include "base/prefs/pref_service.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "base/test/histogram_tester.h"
9 #include "chrome/browser/ui/passwords/manage_passwords_bubble.h"
10 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
11 #include "chrome/browser/ui/passwords/manage_passwords_ui_controller_mock.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/password_manager/core/browser/password_manager_metrics_util.h"
14 #include "components/password_manager/core/common/credential_manager_types.h"
15 #include "components/password_manager/core/common/password_manager_pref_names.h"
16 #include "components/password_manager/core/common/password_manager_ui.h"
17 #include "content/public/test/test_browser_thread_bundle.h"
18 #include "content/public/test/web_contents_tester.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 const char kUIDismissalReasonMetric
[] = "PasswordManager.UIDismissalReason";
23 class ManagePasswordsBubbleModelTest
: public testing::Test
{
25 ManagePasswordsBubbleModelTest()
26 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
),
28 content::WebContentsTester::CreateTestWebContents(&profile_
,
31 void SetUp() override
{
32 // Create the test UIController here so that it's bound to
33 // |test_web_contents_| and therefore accessible to the model.
34 new ManagePasswordsUIControllerMock(test_web_contents_
.get());
36 model_
.reset(new ManagePasswordsBubbleModel(test_web_contents_
.get()));
39 void TearDown() override
{ model_
.reset(); }
41 PrefService
* prefs() { return profile_
.GetPrefs(); }
43 void PretendPasswordWaiting() {
44 model_
->set_state(password_manager::ui::PENDING_PASSWORD_STATE
);
45 model_
->OnBubbleShown(ManagePasswordsBubble::AUTOMATIC
);
48 void PretendUpdatePasswordWaiting() {
49 model_
->set_state(password_manager::ui::PENDING_PASSWORD_UPDATE_STATE
);
50 model_
->OnBubbleShown(ManagePasswordsBubble::AUTOMATIC
);
53 void PretendCredentialsWaiting() {
54 model_
->set_state(password_manager::ui::CREDENTIAL_REQUEST_STATE
);
55 model_
->OnBubbleShown(ManagePasswordsBubble::AUTOMATIC
);
58 void PretendAutoSigningIn() {
59 model_
->set_state(password_manager::ui::AUTO_SIGNIN_STATE
);
60 model_
->OnBubbleShown(ManagePasswordsBubble::AUTOMATIC
);
63 void PretendManagingPasswords() {
64 model_
->set_state(password_manager::ui::MANAGE_STATE
);
65 model_
->OnBubbleShown(ManagePasswordsBubble::USER_ACTION
);
68 ManagePasswordsUIControllerMock
* controller() {
69 return static_cast<ManagePasswordsUIControllerMock
*>(
70 ManagePasswordsUIController::FromWebContents(
71 test_web_contents_
.get()));
75 scoped_ptr
<ManagePasswordsBubbleModel
> model_
;
76 autofill::PasswordForm test_form_
;
79 content::TestBrowserThreadBundle thread_bundle_
;
80 TestingProfile profile_
;
81 scoped_ptr
<content::WebContents
> test_web_contents_
;
84 TEST_F(ManagePasswordsBubbleModelTest
, DefaultValues
) {
85 EXPECT_EQ(model_
->display_disposition(),
86 password_manager::metrics_util::AUTOMATIC_WITH_PASSWORD_PENDING
);
87 EXPECT_EQ(model_
->dismissal_reason(),
88 password_manager::metrics_util::NOT_DISPLAYED
);
89 EXPECT_FALSE(controller()->saved_password());
90 EXPECT_FALSE(controller()->never_saved_password());
93 TEST_F(ManagePasswordsBubbleModelTest
, CloseWithoutLogging
) {
94 base::HistogramTester histogram_tester
;
95 model_
->OnBubbleHidden();
96 EXPECT_EQ(model_
->dismissal_reason(),
97 password_manager::metrics_util::NOT_DISPLAYED
);
98 EXPECT_FALSE(controller()->saved_password());
99 EXPECT_FALSE(controller()->never_saved_password());
101 histogram_tester
.ExpectTotalCount(kUIDismissalReasonMetric
, 0);
104 TEST_F(ManagePasswordsBubbleModelTest
, CloseWithoutInteraction
) {
105 base::HistogramTester histogram_tester
;
106 PretendPasswordWaiting();
107 model_
->OnBubbleHidden();
108 EXPECT_EQ(model_
->dismissal_reason(),
109 password_manager::metrics_util::NO_DIRECT_INTERACTION
);
110 EXPECT_EQ(password_manager::ui::PENDING_PASSWORD_STATE
,
112 EXPECT_FALSE(controller()->saved_password());
113 EXPECT_FALSE(controller()->never_saved_password());
115 histogram_tester
.ExpectUniqueSample(
116 kUIDismissalReasonMetric
,
117 password_manager::metrics_util::NO_DIRECT_INTERACTION
,
121 TEST_F(ManagePasswordsBubbleModelTest
, ClickSave
) {
122 base::HistogramTester histogram_tester
;
123 PretendPasswordWaiting();
124 model_
->OnSaveClicked();
125 model_
->OnBubbleHidden();
126 EXPECT_EQ(model_
->dismissal_reason(),
127 password_manager::metrics_util::CLICKED_SAVE
);
128 EXPECT_TRUE(controller()->saved_password());
129 EXPECT_FALSE(controller()->never_saved_password());
131 histogram_tester
.ExpectUniqueSample(
132 kUIDismissalReasonMetric
,
133 password_manager::metrics_util::CLICKED_SAVE
,
137 TEST_F(ManagePasswordsBubbleModelTest
, ClickNever
) {
138 base::HistogramTester histogram_tester
;
139 PretendPasswordWaiting();
140 model_
->OnNeverForThisSiteClicked();
141 model_
->OnBubbleHidden();
142 EXPECT_EQ(model_
->dismissal_reason(),
143 password_manager::metrics_util::CLICKED_NEVER
);
144 EXPECT_EQ(password_manager::ui::PENDING_PASSWORD_STATE
, model_
->state());
145 EXPECT_FALSE(controller()->saved_password());
146 EXPECT_TRUE(controller()->never_saved_password());
148 histogram_tester
.ExpectUniqueSample(
149 kUIDismissalReasonMetric
,
150 password_manager::metrics_util::CLICKED_NEVER
,
154 TEST_F(ManagePasswordsBubbleModelTest
, ClickManage
) {
155 base::HistogramTester histogram_tester
;
156 PretendManagingPasswords();
157 model_
->OnManageLinkClicked();
158 model_
->OnBubbleHidden();
159 EXPECT_EQ(model_
->dismissal_reason(),
160 password_manager::metrics_util::CLICKED_MANAGE
);
161 EXPECT_EQ(password_manager::ui::MANAGE_STATE
, model_
->state());
162 EXPECT_FALSE(controller()->saved_password());
163 EXPECT_FALSE(controller()->never_saved_password());
165 histogram_tester
.ExpectUniqueSample(
166 kUIDismissalReasonMetric
,
167 password_manager::metrics_util::CLICKED_MANAGE
,
171 TEST_F(ManagePasswordsBubbleModelTest
, ClickDone
) {
172 base::HistogramTester histogram_tester
;
173 PretendManagingPasswords();
174 model_
->OnDoneClicked();
175 model_
->OnBubbleHidden();
176 EXPECT_EQ(model_
->dismissal_reason(),
177 password_manager::metrics_util::CLICKED_DONE
);
178 EXPECT_EQ(password_manager::ui::MANAGE_STATE
, model_
->state());
179 EXPECT_FALSE(controller()->saved_password());
180 EXPECT_FALSE(controller()->never_saved_password());
182 histogram_tester
.ExpectUniqueSample(
183 kUIDismissalReasonMetric
,
184 password_manager::metrics_util::CLICKED_DONE
,
188 TEST_F(ManagePasswordsBubbleModelTest
, ClickCredential
) {
189 base::HistogramTester histogram_tester
;
190 PretendCredentialsWaiting();
191 EXPECT_FALSE(controller()->choose_credential());
192 autofill::PasswordForm form
;
193 model_
->OnChooseCredentials(
194 form
, password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD
);
195 model_
->OnBubbleHidden();
196 EXPECT_EQ(model_
->dismissal_reason(),
197 password_manager::metrics_util::CLICKED_CREDENTIAL
);
198 EXPECT_FALSE(controller()->saved_password());
199 EXPECT_FALSE(controller()->never_saved_password());
200 EXPECT_TRUE(controller()->choose_credential());
202 histogram_tester
.ExpectUniqueSample(
203 kUIDismissalReasonMetric
,
204 password_manager::metrics_util::CLICKED_CREDENTIAL
,
208 TEST_F(ManagePasswordsBubbleModelTest
, ClickCancelCredential
) {
209 base::HistogramTester histogram_tester
;
210 PretendCredentialsWaiting();
211 EXPECT_FALSE(controller()->choose_credential());
212 model_
->OnCancelClicked();
213 model_
->OnBubbleHidden();
214 EXPECT_EQ(model_
->dismissal_reason(),
215 password_manager::metrics_util::CLICKED_CANCEL
);
216 EXPECT_FALSE(controller()->saved_password());
217 EXPECT_FALSE(controller()->never_saved_password());
218 EXPECT_FALSE(controller()->choose_credential());
220 histogram_tester
.ExpectUniqueSample(
221 kUIDismissalReasonMetric
, password_manager::metrics_util::CLICKED_CANCEL
,
225 TEST_F(ManagePasswordsBubbleModelTest
, DismissCredential
) {
226 base::HistogramTester histogram_tester
;
227 PretendCredentialsWaiting();
228 EXPECT_FALSE(controller()->choose_credential());
229 model_
->OnBubbleHidden();
230 EXPECT_EQ(model_
->dismissal_reason(),
231 password_manager::metrics_util::NO_DIRECT_INTERACTION
);
232 EXPECT_FALSE(controller()->saved_password());
233 EXPECT_FALSE(controller()->never_saved_password());
234 EXPECT_FALSE(controller()->choose_credential());
236 histogram_tester
.ExpectUniqueSample(
237 kUIDismissalReasonMetric
,
238 password_manager::metrics_util::NO_DIRECT_INTERACTION
,
242 TEST_F(ManagePasswordsBubbleModelTest
, PopupAutoSigninToast
) {
243 base::HistogramTester histogram_tester
;
244 PretendAutoSigningIn();
245 model_
->OnAutoSignInToastTimeout();
246 model_
->OnBubbleHidden();
247 EXPECT_EQ(model_
->dismissal_reason(),
248 password_manager::metrics_util::AUTO_SIGNIN_TOAST_TIMEOUT
);
250 histogram_tester
.ExpectUniqueSample(
251 kUIDismissalReasonMetric
,
252 password_manager::metrics_util::AUTO_SIGNIN_TOAST_TIMEOUT
,
256 TEST_F(ManagePasswordsBubbleModelTest
, PopupAutoSigninAndManagedBubble
) {
257 base::HistogramTester histogram_tester
;
258 PretendAutoSigningIn();
259 model_
->OnAutoSignInToastTimeout();
260 model_
->OnAutoSignInClicked();
261 EXPECT_EQ(model_
->dismissal_reason(),
262 password_manager::metrics_util::AUTO_SIGNIN_TOAST_CLICKED
);
263 model_
->OnBubbleHidden();
265 EXPECT_TRUE(controller()->manage_accounts());
267 histogram_tester
.ExpectUniqueSample(
268 kUIDismissalReasonMetric
,
269 password_manager::metrics_util::AUTO_SIGNIN_TOAST_CLICKED
,
273 TEST_F(ManagePasswordsBubbleModelTest
, ClickUpdate
) {
274 PretendUpdatePasswordWaiting();
275 model_
->OnUpdateClicked(autofill::PasswordForm());
276 model_
->OnBubbleHidden();
277 EXPECT_TRUE(controller()->updated_password());
278 EXPECT_FALSE(controller()->never_saved_password());