Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / chrome / browser / password_manager / chrome_password_manager_client_unittest.cc
blob802bdcef1797161c2ab75ee008efb0b534f841b8
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/command_line.h"
7 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
9 #include "chrome/common/chrome_version_info.h"
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/autofill/content/common/autofill_messages.h"
13 #include "components/password_manager/content/browser/password_manager_internals_service_factory.h"
14 #include "components/password_manager/core/browser/log_receiver.h"
15 #include "components/password_manager/core/browser/password_manager_internals_service.h"
16 #include "components/password_manager/core/common/password_manager_switches.h"
17 #include "content/public/browser/browser_context.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/test/mock_render_process_host.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 using content::BrowserContext;
24 using content::WebContents;
26 namespace {
28 const char kTestText[] = "abcd1234";
30 class MockLogReceiver : public password_manager::LogReceiver {
31 public:
32 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&));
35 class TestChromePasswordManagerClient : public ChromePasswordManagerClient {
36 public:
37 explicit TestChromePasswordManagerClient(content::WebContents* web_contents)
38 : ChromePasswordManagerClient(web_contents, NULL),
39 is_sync_account_credential_(false) {}
40 virtual ~TestChromePasswordManagerClient() {}
42 virtual bool IsSyncAccountCredential(
43 const std::string& username,
44 const std::string& origin) const OVERRIDE {
45 return is_sync_account_credential_;
48 void set_is_sync_account_credential(bool is_sync_account_credential) {
49 is_sync_account_credential_ = is_sync_account_credential;
52 private:
53 bool is_sync_account_credential_;
55 DISALLOW_COPY_AND_ASSIGN(TestChromePasswordManagerClient);
58 } // namespace
60 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness {
61 public:
62 ChromePasswordManagerClientTest();
64 virtual void SetUp() OVERRIDE;
66 protected:
67 ChromePasswordManagerClient* GetClient();
69 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then
70 // copies its argument into |activation_flag| and returns true. Otherwise
71 // returns false.
72 bool WasLoggingActivationMessageSent(bool* activation_flag);
74 password_manager::PasswordManagerInternalsService* service_;
76 testing::StrictMock<MockLogReceiver> receiver_;
79 ChromePasswordManagerClientTest::ChromePasswordManagerClientTest()
80 : service_(NULL) {
83 void ChromePasswordManagerClientTest::SetUp() {
84 ChromeRenderViewHostTestHarness::SetUp();
85 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
86 web_contents(), NULL);
87 service_ = password_manager::PasswordManagerInternalsServiceFactory::
88 GetForBrowserContext(profile());
89 ASSERT_TRUE(service_);
92 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() {
93 return ChromePasswordManagerClient::FromWebContents(web_contents());
96 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent(
97 bool* activation_flag) {
98 const uint32 kMsgID = AutofillMsg_SetLoggingState::ID;
99 const IPC::Message* message =
100 process()->sink().GetFirstMessageMatching(kMsgID);
101 if (!message)
102 return false;
103 Tuple1<bool> param;
104 AutofillMsg_SetLoggingState::Read(message, &param);
105 *activation_flag = param.a;
106 process()->sink().ClearMessages();
107 return true;
110 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoReceiver) {
111 ChromePasswordManagerClient* client = GetClient();
113 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0);
114 // Before attaching the receiver, no text should be passed.
115 client->LogSavePasswordProgress(kTestText);
116 EXPECT_FALSE(client->IsLoggingActive());
119 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachReceiver) {
120 ChromePasswordManagerClient* client = GetClient();
121 EXPECT_FALSE(client->IsLoggingActive());
123 // After attaching the logger, text should be passed.
124 service_->RegisterReceiver(&receiver_);
125 EXPECT_TRUE(client->IsLoggingActive());
126 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1);
127 client->LogSavePasswordProgress(kTestText);
128 service_->UnregisterReceiver(&receiver_);
129 EXPECT_FALSE(client->IsLoggingActive());
132 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressDetachReceiver) {
133 ChromePasswordManagerClient* client = GetClient();
135 service_->RegisterReceiver(&receiver_);
136 EXPECT_TRUE(client->IsLoggingActive());
137 service_->UnregisterReceiver(&receiver_);
138 EXPECT_FALSE(client->IsLoggingActive());
140 // After detaching the logger, no text should be passed.
141 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0);
142 client->LogSavePasswordProgress(kTestText);
145 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) {
146 ChromePasswordManagerClient* client = GetClient();
147 bool logging_active = false;
149 // Initially, the logging should be off, so no IPC messages.
150 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active));
152 service_->RegisterReceiver(&receiver_);
153 EXPECT_TRUE(client->IsLoggingActive());
154 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
155 EXPECT_TRUE(logging_active);
157 service_->UnregisterReceiver(&receiver_);
158 EXPECT_FALSE(client->IsLoggingActive());
159 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
160 EXPECT_FALSE(logging_active);
163 TEST_F(ChromePasswordManagerClientTest, AnswerToPingsAboutLoggingState_Active) {
164 service_->RegisterReceiver(&receiver_);
166 process()->sink().ClearMessages();
168 // Ping the client for logging activity update.
169 AutofillHostMsg_PasswordAutofillAgentConstructed msg(0);
170 static_cast<IPC::Listener*>(GetClient())->OnMessageReceived(msg);
172 bool logging_active = false;
173 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
174 EXPECT_TRUE(logging_active);
176 service_->UnregisterReceiver(&receiver_);
179 TEST_F(ChromePasswordManagerClientTest,
180 AnswerToPingsAboutLoggingState_Inactive) {
181 process()->sink().ClearMessages();
183 // Ping the client for logging activity update.
184 AutofillHostMsg_PasswordAutofillAgentConstructed msg(0);
185 static_cast<IPC::Listener*>(GetClient())->OnMessageReceived(msg);
187 bool logging_active = true;
188 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
189 EXPECT_FALSE(logging_active);
192 TEST_F(ChromePasswordManagerClientTest,
193 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) {
194 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
197 TEST_F(ChromePasswordManagerClientTest,
198 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) {
199 CommandLine::ForCurrentProcess()->AppendSwitch(
200 password_manager::switches::kEnableAutomaticPasswordSaving);
201 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_UNKNOWN)
202 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled());
203 else
204 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
207 TEST_F(ChromePasswordManagerClientTest, LogToAReceiver) {
208 ChromePasswordManagerClient* client = GetClient();
209 service_->RegisterReceiver(&receiver_);
210 EXPECT_TRUE(client->IsLoggingActive());
212 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1);
213 client->LogSavePasswordProgress(kTestText);
215 service_->UnregisterReceiver(&receiver_);
216 EXPECT_FALSE(client->IsLoggingActive());
219 TEST_F(ChromePasswordManagerClientTest, ShouldFilterAutofillResult_Reauth) {
220 // Make client disallow only reauth requests.
221 CommandLine* command_line = CommandLine::ForCurrentProcess();
222 command_line->AppendSwitch(
223 password_manager::switches::kDisallowAutofillSyncCredentialForReauth);
224 scoped_ptr<TestChromePasswordManagerClient> client(
225 new TestChromePasswordManagerClient(web_contents()));
226 autofill::PasswordForm form;
228 client->set_is_sync_account_credential(false);
229 NavigateAndCommit(
230 GURL("https://accounts.google.com/login?rart=123&continue=blah"));
231 EXPECT_FALSE(client->ShouldFilterAutofillResult(form));
233 client->set_is_sync_account_credential(true);
234 NavigateAndCommit(
235 GURL("https://accounts.google.com/login?rart=123&continue=blah"));
236 EXPECT_TRUE(client->ShouldFilterAutofillResult(form));
238 // This counts as a reauth url, though a valid URL should have a value for
239 // "rart"
240 NavigateAndCommit(GURL("https://accounts.google.com/addlogin?rart"));
241 EXPECT_TRUE(client->ShouldFilterAutofillResult(form));
243 NavigateAndCommit(GURL("https://accounts.google.com/login?param=123"));
244 EXPECT_FALSE(client->ShouldFilterAutofillResult(form));
246 NavigateAndCommit(GURL("https://site.com/login?rart=678"));
247 EXPECT_FALSE(client->ShouldFilterAutofillResult(form));
250 TEST_F(ChromePasswordManagerClientTest, ShouldFilterAutofillResult) {
251 // Normally the client should allow any credentials through, even if they
252 // are the sync credential.
253 scoped_ptr<TestChromePasswordManagerClient> client(
254 new TestChromePasswordManagerClient(web_contents()));
255 autofill::PasswordForm form;
256 client->set_is_sync_account_credential(true);
257 NavigateAndCommit(GURL("https://accounts.google.com/Login"));
258 EXPECT_FALSE(client->ShouldFilterAutofillResult(form));
260 // Adding disallow switch should cause sync credential to be filtered.
261 CommandLine* command_line = CommandLine::ForCurrentProcess();
262 command_line->AppendSwitch(
263 password_manager::switches::kDisallowAutofillSyncCredential);
264 client.reset(new TestChromePasswordManagerClient(web_contents()));
265 client->set_is_sync_account_credential(true);
266 NavigateAndCommit(GURL("https://accounts.google.com/Login"));
267 EXPECT_TRUE(client->ShouldFilterAutofillResult(form));