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 PretendCredentialsWaiting() {
49 model_
->set_state(password_manager::ui::CREDENTIAL_REQUEST_STATE
);
50 model_
->OnBubbleShown(ManagePasswordsBubble::AUTOMATIC
);
53 void PretendAutoSigningIn() {
54 model_
->set_state(password_manager::ui::AUTO_SIGNIN_STATE
);
55 model_
->OnBubbleShown(ManagePasswordsBubble::AUTOMATIC
);
58 void PretendManagingPasswords() {
59 model_
->set_state(password_manager::ui::MANAGE_STATE
);
60 model_
->OnBubbleShown(ManagePasswordsBubble::USER_ACTION
);
63 void PretendBlacklisted() {
64 model_
->set_state(password_manager::ui::BLACKLIST_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 scoped_ptr
<base::HistogramSamples
> samples(
102 histogram_tester
.GetHistogramSamplesSinceCreation(
103 kUIDismissalReasonMetric
));
104 EXPECT_FALSE(samples
);
107 TEST_F(ManagePasswordsBubbleModelTest
, CloseWithoutInteraction
) {
108 base::HistogramTester histogram_tester
;
109 PretendPasswordWaiting();
110 model_
->OnBubbleHidden();
111 EXPECT_EQ(model_
->dismissal_reason(),
112 password_manager::metrics_util::NO_DIRECT_INTERACTION
);
113 EXPECT_EQ(password_manager::ui::PENDING_PASSWORD_STATE
,
115 EXPECT_FALSE(controller()->saved_password());
116 EXPECT_FALSE(controller()->never_saved_password());
118 histogram_tester
.ExpectUniqueSample(
119 kUIDismissalReasonMetric
,
120 password_manager::metrics_util::NO_DIRECT_INTERACTION
,
124 TEST_F(ManagePasswordsBubbleModelTest
, ClickSave
) {
125 base::HistogramTester histogram_tester
;
126 PretendPasswordWaiting();
127 model_
->OnSaveClicked();
128 model_
->OnBubbleHidden();
129 EXPECT_EQ(model_
->dismissal_reason(),
130 password_manager::metrics_util::CLICKED_SAVE
);
131 EXPECT_EQ(password_manager::ui::MANAGE_STATE
, model_
->state());
132 EXPECT_TRUE(controller()->saved_password());
133 EXPECT_FALSE(controller()->never_saved_password());
135 histogram_tester
.ExpectUniqueSample(
136 kUIDismissalReasonMetric
,
137 password_manager::metrics_util::CLICKED_SAVE
,
141 TEST_F(ManagePasswordsBubbleModelTest
, ClickNope
) {
142 base::HistogramTester histogram_tester
;
143 PretendPasswordWaiting();
144 model_
->OnNopeClicked();
145 model_
->OnBubbleHidden();
146 EXPECT_EQ(model_
->dismissal_reason(),
147 password_manager::metrics_util::CLICKED_NOPE
);
148 EXPECT_EQ(password_manager::ui::PENDING_PASSWORD_STATE
, model_
->state());
149 EXPECT_FALSE(controller()->saved_password());
150 EXPECT_FALSE(controller()->never_saved_password());
152 histogram_tester
.ExpectUniqueSample(
153 kUIDismissalReasonMetric
,
154 password_manager::metrics_util::CLICKED_NOPE
,
158 TEST_F(ManagePasswordsBubbleModelTest
, ClickNever
) {
159 base::HistogramTester histogram_tester
;
160 PretendPasswordWaiting();
161 model_
->OnNeverForThisSiteClicked();
162 model_
->OnBubbleHidden();
163 EXPECT_EQ(model_
->dismissal_reason(),
164 password_manager::metrics_util::CLICKED_NEVER
);
165 EXPECT_EQ(password_manager::ui::BLACKLIST_STATE
, model_
->state());
166 EXPECT_FALSE(controller()->saved_password());
167 EXPECT_TRUE(controller()->never_saved_password());
169 histogram_tester
.ExpectUniqueSample(
170 kUIDismissalReasonMetric
,
171 password_manager::metrics_util::CLICKED_NEVER
,
175 TEST_F(ManagePasswordsBubbleModelTest
, ClickManage
) {
176 base::HistogramTester histogram_tester
;
177 PretendManagingPasswords();
178 model_
->OnManageLinkClicked();
179 model_
->OnBubbleHidden();
180 EXPECT_EQ(model_
->dismissal_reason(),
181 password_manager::metrics_util::CLICKED_MANAGE
);
182 EXPECT_EQ(password_manager::ui::MANAGE_STATE
, model_
->state());
183 EXPECT_FALSE(controller()->saved_password());
184 EXPECT_FALSE(controller()->never_saved_password());
186 histogram_tester
.ExpectUniqueSample(
187 kUIDismissalReasonMetric
,
188 password_manager::metrics_util::CLICKED_MANAGE
,
192 TEST_F(ManagePasswordsBubbleModelTest
, ClickDone
) {
193 base::HistogramTester histogram_tester
;
194 PretendManagingPasswords();
195 model_
->OnDoneClicked();
196 model_
->OnBubbleHidden();
197 EXPECT_EQ(model_
->dismissal_reason(),
198 password_manager::metrics_util::CLICKED_DONE
);
199 EXPECT_EQ(password_manager::ui::MANAGE_STATE
, model_
->state());
200 EXPECT_FALSE(controller()->saved_password());
201 EXPECT_FALSE(controller()->never_saved_password());
203 histogram_tester
.ExpectUniqueSample(
204 kUIDismissalReasonMetric
,
205 password_manager::metrics_util::CLICKED_DONE
,
209 TEST_F(ManagePasswordsBubbleModelTest
, ClickUnblacklist
) {
210 base::HistogramTester histogram_tester
;
211 PretendBlacklisted();
212 model_
->OnUnblacklistClicked();
213 model_
->OnBubbleHidden();
214 EXPECT_EQ(model_
->dismissal_reason(),
215 password_manager::metrics_util::CLICKED_UNBLACKLIST
);
216 EXPECT_EQ(password_manager::ui::MANAGE_STATE
, model_
->state());
217 EXPECT_FALSE(controller()->saved_password());
218 EXPECT_FALSE(controller()->never_saved_password());
219 EXPECT_TRUE(controller()->unblacklist_site());
221 histogram_tester
.ExpectUniqueSample(
222 kUIDismissalReasonMetric
,
223 password_manager::metrics_util::CLICKED_UNBLACKLIST
,
227 TEST_F(ManagePasswordsBubbleModelTest
, ClickCredential
) {
228 base::HistogramTester histogram_tester
;
229 PretendCredentialsWaiting();
230 EXPECT_FALSE(controller()->choose_credential());
231 autofill::PasswordForm form
;
232 model_
->OnChooseCredentials(
233 form
, password_manager::CredentialType::CREDENTIAL_TYPE_LOCAL
);
234 model_
->OnBubbleHidden();
235 EXPECT_EQ(model_
->dismissal_reason(),
236 password_manager::metrics_util::CLICKED_CREDENTIAL
);
237 EXPECT_FALSE(controller()->saved_password());
238 EXPECT_FALSE(controller()->never_saved_password());
239 EXPECT_TRUE(controller()->choose_credential());
241 histogram_tester
.ExpectUniqueSample(
242 kUIDismissalReasonMetric
,
243 password_manager::metrics_util::CLICKED_CREDENTIAL
,
247 TEST_F(ManagePasswordsBubbleModelTest
, ClickCancelCredential
) {
248 base::HistogramTester histogram_tester
;
249 PretendCredentialsWaiting();
250 EXPECT_FALSE(controller()->choose_credential());
251 model_
->OnNopeClicked();
252 model_
->OnBubbleHidden();
253 EXPECT_EQ(model_
->dismissal_reason(),
254 password_manager::metrics_util::CLICKED_NOPE
);
255 EXPECT_FALSE(controller()->saved_password());
256 EXPECT_FALSE(controller()->never_saved_password());
257 EXPECT_FALSE(controller()->choose_credential());
259 histogram_tester
.ExpectUniqueSample(
260 kUIDismissalReasonMetric
,
261 password_manager::metrics_util::CLICKED_NOPE
,
265 TEST_F(ManagePasswordsBubbleModelTest
, DismissCredential
) {
266 base::HistogramTester histogram_tester
;
267 PretendCredentialsWaiting();
268 EXPECT_FALSE(controller()->choose_credential());
269 model_
->OnBubbleHidden();
270 EXPECT_EQ(model_
->dismissal_reason(),
271 password_manager::metrics_util::NO_DIRECT_INTERACTION
);
272 EXPECT_FALSE(controller()->saved_password());
273 EXPECT_FALSE(controller()->never_saved_password());
274 EXPECT_FALSE(controller()->choose_credential());
276 histogram_tester
.ExpectUniqueSample(
277 kUIDismissalReasonMetric
,
278 password_manager::metrics_util::NO_DIRECT_INTERACTION
,
282 TEST_F(ManagePasswordsBubbleModelTest
, PopupAutoSigninToast
) {
283 base::HistogramTester histogram_tester
;
284 PretendAutoSigningIn();
285 model_
->OnAutoSignInToastTimeout();
286 model_
->OnBubbleHidden();
287 EXPECT_EQ(model_
->dismissal_reason(),
288 password_manager::metrics_util::AUTO_SIGNIN_TOAST_TIMEOUT
);
290 histogram_tester
.ExpectUniqueSample(
291 kUIDismissalReasonMetric
,
292 password_manager::metrics_util::AUTO_SIGNIN_TOAST_TIMEOUT
,
296 TEST_F(ManagePasswordsBubbleModelTest
, PopupAutoSigninAndManagedBubble
) {
297 base::HistogramTester histogram_tester
;
298 PretendAutoSigningIn();
299 model_
->OnAutoSignInToastTimeout();
300 model_
->OnAutoSignInClicked();
301 EXPECT_EQ(model_
->dismissal_reason(),
302 password_manager::metrics_util::AUTO_SIGNIN_TOAST_CLICKED
);
303 model_
->OnBubbleHidden();
305 EXPECT_TRUE(controller()->manage_accounts());
307 histogram_tester
.ExpectUniqueSample(
308 kUIDismissalReasonMetric
,
309 password_manager::metrics_util::AUTO_SIGNIN_TOAST_CLICKED
,