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 "chrome/browser/ui/passwords/password_bubble_experiment.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/prefs/pref_service.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_profile.h"
13 #include "components/variations/entropy_provider.h"
14 #include "components/variations/variations_associated_data.h"
15 #include "testing/gtest/include/gtest/gtest.h"
19 const char kGroupName
[] = "SomeGroupName";
20 const int kNopeThreshold
= 10;
22 void SetupExperiment() {
23 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
24 password_bubble_experiment::kExperimentName
,
26 std::map
<std::string
, std::string
> params
;
27 params
[password_bubble_experiment::kParamNopeThreshold
] =
28 base::IntToString(kNopeThreshold
);
29 ASSERT_TRUE(variations::AssociateVariationParams(
30 password_bubble_experiment::kExperimentName
,
37 class PasswordBubbleExperimentTest
: public testing::Test
{
39 void SetUp() override
{
40 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
41 profile_
.reset(new TestingProfile(temp_dir_
.path()));
43 field_trial_list_
.reset(new base::FieldTrialList(
44 new metrics::SHA1EntropyProvider("foo")));
47 void TearDown() override
{
48 variations::testing::ClearAllVariationParams();
51 PrefService
* prefs() { return profile_
->GetPrefs(); }
54 base::ScopedTempDir temp_dir_
;
55 scoped_ptr
<TestingProfile
> profile_
;
56 scoped_ptr
<base::FieldTrialList
> field_trial_list_
;
59 TEST_F(PasswordBubbleExperimentTest
, NoExperiment
) {
61 password_bubble_experiment::ShouldShowNeverForThisSiteDefault(prefs()));
62 for (int i
= 0; i
<= kNopeThreshold
; ++i
) {
63 password_bubble_experiment::RecordBubbleClosed(
64 prefs(), password_manager::metrics_util::CLICKED_NOPE
);
66 password_bubble_experiment::ShouldShowNeverForThisSiteDefault(prefs()));
70 TEST_F(PasswordBubbleExperimentTest
, WithExperiment
) {
73 // Repeatedly click "Never". It shouldn't affect the state.
74 for (int i
= 0; i
< kNopeThreshold
; ++i
) {
75 password_manager::metrics_util::UIDismissalReason reason
=
76 password_manager::metrics_util::CLICKED_NEVER
;
77 password_bubble_experiment::RecordBubbleClosed(prefs(), reason
);
80 password_bubble_experiment::ShouldShowNeverForThisSiteDefault(prefs()));
81 // Repeatedly refuse to save password, for |kNopeThreshold|-1 times.
82 for (int i
= 0; i
< kNopeThreshold
- 1; ++i
) {
83 password_manager::metrics_util::UIDismissalReason reason
=
84 password_manager::metrics_util::CLICKED_NOPE
;
85 password_bubble_experiment::RecordBubbleClosed(prefs(), reason
);
88 password_bubble_experiment::ShouldShowNeverForThisSiteDefault(prefs()));
90 // Refuse to save once more to make Never the default button.
91 password_bubble_experiment::RecordBubbleClosed(
92 prefs(), password_manager::metrics_util::CLICKED_NOPE
);
94 password_bubble_experiment::ShouldShowNeverForThisSiteDefault(prefs()));
95 password_bubble_experiment::RecordBubbleClosed(
96 prefs(), password_manager::metrics_util::CLICKED_SAVE
);
98 password_bubble_experiment::ShouldShowNeverForThisSiteDefault(prefs()));
100 // Repeatedly refuse to save password, for |kNopeThreshold| times.
101 for (int i
= 0; i
< kNopeThreshold
; ++i
) {
102 password_manager::metrics_util::UIDismissalReason reason
=
103 password_manager::metrics_util::CLICKED_NOPE
;
104 password_bubble_experiment::RecordBubbleClosed(prefs(), reason
);
107 password_bubble_experiment::ShouldShowNeverForThisSiteDefault(prefs()));
108 // Repeatedly click "Never". It shouldn't affect the state.
109 for (int i
= 0; i
< kNopeThreshold
; ++i
) {
110 password_manager::metrics_util::UIDismissalReason reason
=
111 password_manager::metrics_util::CLICKED_NEVER
;
112 password_bubble_experiment::RecordBubbleClosed(prefs(), reason
);
115 password_bubble_experiment::ShouldShowNeverForThisSiteDefault(prefs()));