1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/passwords/password_dialog_controller_impl.h"
#include <utility>
#include <vector>
#include "base/memory/scoped_ptr.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/passwords/manage_passwords_ui_controller_mock.h"
#include "chrome/browser/ui/passwords/password_dialog_prompts.h"
#include "chrome/test/base/testing_profile.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/browser/password_bubble_experiment.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/prefs/pref_service.h"
#include "content/public/test/test_browser_thread_bundle.h"
#include "content/public/test/web_contents_tester.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
using testing::ElementsAre;
using testing::Pointee;
using testing::StrictMock;
const char kUsername[] = "user1";
class MockPasswordPrompt : public AccountChooserPrompt,
public AutoSigninFirstRunPrompt {
public:
MockPasswordPrompt() = default;
MOCK_METHOD0(ShowAccountChooser, void());
MOCK_METHOD0(ShowAutoSigninPrompt, void());
MOCK_METHOD0(ControllerGone, void());
private:
DISALLOW_COPY_AND_ASSIGN(MockPasswordPrompt);
};
autofill::PasswordForm GetLocalForm() {
autofill::PasswordForm form;
form.username_value = base::ASCIIToUTF16(kUsername);
form.origin = GURL("http://example.com");
return form;
}
autofill::PasswordForm GetFederationProviderForm() {
autofill::PasswordForm form;
form.username_value = base::ASCIIToUTF16(kUsername);
form.origin = GURL("http://idp.com");
return form;
}
class PasswordDialogControllerTest : public testing::Test {
public:
PasswordDialogControllerTest()
: test_web_contents_(content::WebContentsTester::CreateTestWebContents(
&profile_, nullptr)),
ui_controller_mock_(new StrictMock<ManagePasswordsUIControllerMock>(
test_web_contents_.get())),
controller_(&profile_, ui_controller_mock_) {
}
ManagePasswordsUIControllerMock& ui_controller_mock() {
return *ui_controller_mock_;
}
PasswordDialogControllerImpl& controller() { return controller_; }
PrefService* prefs() { return profile_.GetPrefs(); }
private:
content::TestBrowserThreadBundle thread_bundle_;
TestingProfile profile_;
scoped_ptr<content::WebContents> test_web_contents_;
// Owned by |test_web_contents_|.
ManagePasswordsUIControllerMock* ui_controller_mock_;
PasswordDialogControllerImpl controller_;
};
TEST_F(PasswordDialogControllerTest, ShowAccountChooser) {
StrictMock<MockPasswordPrompt> prompt;
autofill::PasswordForm local_form = GetLocalForm();
autofill::PasswordForm idp_form = GetFederationProviderForm();
std::vector<scoped_ptr<autofill::PasswordForm>> locals;
locals.push_back(make_scoped_ptr(new autofill::PasswordForm(local_form)));
autofill::PasswordForm* local_form_ptr = locals[0].get();
std::vector<scoped_ptr<autofill::PasswordForm>> federations;
federations.push_back(make_scoped_ptr(new autofill::PasswordForm(idp_form)));
EXPECT_CALL(prompt, ShowAccountChooser());
controller().ShowAccountChooser(&prompt,
std::move(locals), std::move(federations));
EXPECT_THAT(controller().GetLocalForms(), ElementsAre(Pointee(local_form)));
EXPECT_THAT(controller().GetFederationsForms(),
ElementsAre(Pointee(idp_form)));
// Close the dialog.
EXPECT_CALL(prompt, ControllerGone());
EXPECT_CALL(ui_controller_mock(), ChooseCredential(
local_form,
password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD));
controller().OnChooseCredentials(
*local_form_ptr,
password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD);
}
TEST_F(PasswordDialogControllerTest, AccountChooserClosed) {
StrictMock<MockPasswordPrompt> prompt;
EXPECT_CALL(prompt, ShowAccountChooser());
controller().ShowAccountChooser(&prompt,
PasswordDialogController::FormsVector(),
PasswordDialogController::FormsVector());
EXPECT_CALL(ui_controller_mock(), OnDialogHidden());
controller().OnCloseDialog();
}
TEST_F(PasswordDialogControllerTest, AutoSigninPromo) {
StrictMock<MockPasswordPrompt> prompt;
EXPECT_CALL(prompt, ShowAutoSigninPrompt());
controller().ShowAutosigninPrompt(&prompt);
prefs()->SetBoolean(
password_manager::prefs::kWasAutoSignInFirstRunExperienceShown, false);
EXPECT_CALL(ui_controller_mock(), OnDialogHidden());
controller().OnCloseDialog();
EXPECT_TRUE(
password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
prefs()));
}
TEST_F(PasswordDialogControllerTest, AutoSigninPromoOkGotIt) {
StrictMock<MockPasswordPrompt> prompt;
EXPECT_CALL(prompt, ShowAutoSigninPrompt());
controller().ShowAutosigninPrompt(&prompt);
prefs()->SetBoolean(
password_manager::prefs::kWasAutoSignInFirstRunExperienceShown, false);
prefs()->SetBoolean(password_manager::prefs::kCredentialsEnableAutosignin,
true);
EXPECT_CALL(prompt, ControllerGone());
EXPECT_CALL(ui_controller_mock(), OnDialogHidden());
controller().OnAutoSigninOK();
EXPECT_FALSE(
password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
prefs()));
EXPECT_TRUE(prefs()->GetBoolean(
password_manager::prefs::kCredentialsEnableAutosignin));
}
TEST_F(PasswordDialogControllerTest, AutoSigninPromoTurnOff) {
StrictMock<MockPasswordPrompt> prompt;
EXPECT_CALL(prompt, ShowAutoSigninPrompt());
controller().ShowAutosigninPrompt(&prompt);
prefs()->SetBoolean(
password_manager::prefs::kWasAutoSignInFirstRunExperienceShown, false);
prefs()->SetBoolean(password_manager::prefs::kCredentialsEnableAutosignin,
true);
EXPECT_CALL(prompt, ControllerGone());
EXPECT_CALL(ui_controller_mock(), OnDialogHidden());
controller().OnAutoSigninTurnOff();
EXPECT_FALSE(
password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
prefs()));
EXPECT_FALSE(prefs()->GetBoolean(
password_manager::prefs::kCredentialsEnableAutosignin));
}
TEST_F(PasswordDialogControllerTest, OnBrandLinkClicked) {
EXPECT_CALL(ui_controller_mock(), NavigateToSmartLockHelpPage());
controller().OnSmartLockLinkClicked();
}
} // namespace
|