// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "base/command_line.h"
+
#include "chrome/browser/password_manager/chrome_password_manager_client.h"
+#include "chrome/common/chrome_version_info.h"
#include "chrome/test/base/chrome_render_view_host_test_harness.h"
+#include "components/autofill/content/common/autofill_messages.h"
#include "components/password_manager/core/browser/password_manager_logger.h"
+#include "components/password_manager/core/common/password_manager_switches.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/web_contents.h"
+#include "content/public/test/mock_render_process_host.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
-class MockPasswordManagerLogger : public PasswordManagerLogger {
+const char kTestText[] = "abcd1234";
+
+class MockPasswordManagerLogger
+ : public password_manager::PasswordManagerLogger {
public:
MockPasswordManagerLogger() {}
protected:
ChromePasswordManagerClient* GetClient();
+
+ // If the test IPC sink contains an AutofillMsg_ChangeLoggingState message,
+ // then copies its argument into |activation_flag| and returns true. Otherwise
+ // returns false.
+ bool WasLoggingActivationMessageSent(bool* activation_flag);
+
+ testing::StrictMock<MockPasswordManagerLogger> logger_;
};
void ChromePasswordManagerClientTest::SetUp() {
ChromeRenderViewHostTestHarness::SetUp();
- ChromePasswordManagerClient::CreateForWebContents(web_contents());
+ ChromePasswordManagerClient::CreateForWebContentsWithAutofillManagerDelegate(
+ web_contents(), NULL);
}
ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() {
return ChromePasswordManagerClient::FromWebContents(web_contents());
}
-TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgress) {
+bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent(
+ bool* activation_flag) {
+ const uint32 kMsgID = AutofillMsg_ChangeLoggingState::ID;
+ const IPC::Message* message =
+ process()->sink().GetFirstMessageMatching(kMsgID);
+ if (!message)
+ return false;
+ Tuple1<bool> param;
+ AutofillMsg_ChangeLoggingState::Read(message, ¶m);
+ *activation_flag = param.a;
+ process()->sink().ClearMessages();
+ return true;
+}
+
+TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoLogger) {
ChromePasswordManagerClient* client = GetClient();
- testing::StrictMock<MockPasswordManagerLogger> logger;
- const std::string text("abcd1234");
+ EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(0);
// Before attaching the logger, no text should be passed.
- client->LogSavePasswordProgress(text);
+ client->LogSavePasswordProgress(kTestText);
+ EXPECT_FALSE(client->IsLoggingActive());
+}
+
+TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachLogger) {
+ ChromePasswordManagerClient* client = GetClient();
// After attaching the logger, text should be passed.
- client->SetLogger(&logger);
- EXPECT_CALL(logger, LogSavePasswordProgress(text)).Times(1);
- client->LogSavePasswordProgress(text);
+ client->SetLogger(&logger_);
+ EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(1);
+ client->LogSavePasswordProgress(kTestText);
+ EXPECT_TRUE(client->IsLoggingActive());
+}
+
+TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressDetachLogger) {
+ ChromePasswordManagerClient* client = GetClient();
- // After detaching the logger, no text should be passed again.
+ client->SetLogger(&logger_);
+ // After detaching the logger, no text should be passed.
client->SetLogger(NULL);
- client->LogSavePasswordProgress(text);
+ EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(0);
+ client->LogSavePasswordProgress(kTestText);
+ EXPECT_FALSE(client->IsLoggingActive());
+}
+
+TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) {
+ ChromePasswordManagerClient* client = GetClient();
+ bool logging_active = false;
+
+ // Initially, the logging should be off, so no IPC messages.
+ EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active));
+
+ client->SetLogger(&logger_);
+ EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
+ EXPECT_TRUE(logging_active);
+
+ client->SetLogger(NULL);
+ EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
+ EXPECT_FALSE(logging_active);
+}
+
+TEST_F(ChromePasswordManagerClientTest,
+ IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) {
+ EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
+}
+
+TEST_F(ChromePasswordManagerClientTest,
+ IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) {
+ CommandLine::ForCurrentProcess()->AppendSwitch(
+ password_manager::switches::kEnableAutomaticPasswordSaving);
+ if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_UNKNOWN)
+ EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled());
+ else
+ EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
}