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;
28 const char kTestText[] = "abcd1234";
30 class MockLogReceiver : public password_manager::LogReceiver {
32 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&));
37 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness {
39 ChromePasswordManagerClientTest();
41 virtual void SetUp() OVERRIDE;
44 ChromePasswordManagerClient* GetClient();
46 // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then
47 // copies its argument into |activation_flag| and returns true. Otherwise
49 bool WasLoggingActivationMessageSent(bool* activation_flag);
51 password_manager::PasswordManagerInternalsService* service_;
53 testing::StrictMock<MockLogReceiver> receiver_;
56 ChromePasswordManagerClientTest::ChromePasswordManagerClientTest()
60 void ChromePasswordManagerClientTest::SetUp() {
61 ChromeRenderViewHostTestHarness::SetUp();
62 ChromePasswordManagerClient::CreateForWebContentsWithAutofillClient(
63 web_contents(), NULL);
64 service_ = password_manager::PasswordManagerInternalsServiceFactory::
65 GetForBrowserContext(profile());
66 ASSERT_TRUE(service_);
69 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() {
70 return ChromePasswordManagerClient::FromWebContents(web_contents());
73 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent(
74 bool* activation_flag) {
75 const uint32 kMsgID = AutofillMsg_SetLoggingState::ID;
76 const IPC::Message* message =
77 process()->sink().GetFirstMessageMatching(kMsgID);
81 AutofillMsg_SetLoggingState::Read(message, ¶m);
82 *activation_flag = param.a;
83 process()->sink().ClearMessages();
87 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoReceiver) {
88 ChromePasswordManagerClient* client = GetClient();
90 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0);
91 // Before attaching the receiver, no text should be passed.
92 client->LogSavePasswordProgress(kTestText);
93 EXPECT_FALSE(client->IsLoggingActive());
96 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachReceiver) {
97 ChromePasswordManagerClient* client = GetClient();
98 EXPECT_FALSE(client->IsLoggingActive());
100 // After attaching the logger, text should be passed.
101 service_->RegisterReceiver(&receiver_);
102 EXPECT_TRUE(client->IsLoggingActive());
103 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1);
104 client->LogSavePasswordProgress(kTestText);
105 service_->UnregisterReceiver(&receiver_);
106 EXPECT_FALSE(client->IsLoggingActive());
109 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressDetachReceiver) {
110 ChromePasswordManagerClient* client = GetClient();
112 service_->RegisterReceiver(&receiver_);
113 EXPECT_TRUE(client->IsLoggingActive());
114 service_->UnregisterReceiver(&receiver_);
115 EXPECT_FALSE(client->IsLoggingActive());
117 // After detaching the logger, no text should be passed.
118 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0);
119 client->LogSavePasswordProgress(kTestText);
122 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) {
123 ChromePasswordManagerClient* client = GetClient();
124 bool logging_active = false;
126 // Initially, the logging should be off, so no IPC messages.
127 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active));
129 service_->RegisterReceiver(&receiver_);
130 EXPECT_TRUE(client->IsLoggingActive());
131 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
132 EXPECT_TRUE(logging_active);
134 service_->UnregisterReceiver(&receiver_);
135 EXPECT_FALSE(client->IsLoggingActive());
136 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
137 EXPECT_FALSE(logging_active);
140 TEST_F(ChromePasswordManagerClientTest, AnswerToPingsAboutLoggingState_Active) {
141 service_->RegisterReceiver(&receiver_);
143 process()->sink().ClearMessages();
145 // Ping the client for logging activity update.
146 AutofillHostMsg_PasswordAutofillAgentConstructed msg(0);
147 static_cast<IPC::Listener*>(GetClient())->OnMessageReceived(msg);
149 bool logging_active = false;
150 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
151 EXPECT_TRUE(logging_active);
153 service_->UnregisterReceiver(&receiver_);
156 TEST_F(ChromePasswordManagerClientTest,
157 AnswerToPingsAboutLoggingState_Inactive) {
158 process()->sink().ClearMessages();
160 // Ping the client for logging activity update.
161 AutofillHostMsg_PasswordAutofillAgentConstructed msg(0);
162 static_cast<IPC::Listener*>(GetClient())->OnMessageReceived(msg);
164 bool logging_active = true;
165 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
166 EXPECT_FALSE(logging_active);
169 TEST_F(ChromePasswordManagerClientTest,
170 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) {
171 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
174 TEST_F(ChromePasswordManagerClientTest,
175 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) {
176 CommandLine::ForCurrentProcess()->AppendSwitch(
177 password_manager::switches::kEnableAutomaticPasswordSaving);
178 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_UNKNOWN)
179 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled());
181 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
184 TEST_F(ChromePasswordManagerClientTest, LogToAReceiver) {
185 ChromePasswordManagerClient* client = GetClient();
186 service_->RegisterReceiver(&receiver_);
187 EXPECT_TRUE(client->IsLoggingActive());
189 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1);
190 client->LogSavePasswordProgress(kTestText);
192 service_->UnregisterReceiver(&receiver_);
193 EXPECT_FALSE(client->IsLoggingActive());