Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / password_manager / chrome_password_manager_client_unittest.cc
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.
4
5 #include "base/command_line.h"
6
7 #include "chrome/browser/password_manager/chrome_password_manager_client.h"
8
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"
22
23 using content::BrowserContext;
24 using content::WebContents;
25
26 namespace {
27
28 const char kTestText[] = "abcd1234";
29
30 class MockLogReceiver : public password_manager::LogReceiver {
31  public:
32   MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&));
33 };
34
35 }  // namespace
36
37 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness {
38  public:
39   ChromePasswordManagerClientTest();
40
41   virtual void SetUp() OVERRIDE;
42
43  protected:
44   ChromePasswordManagerClient* GetClient();
45
46   // If the test IPC sink contains an AutofillMsg_SetLoggingState message, then
47   // copies its argument into |activation_flag| and returns true. Otherwise
48   // returns false.
49   bool WasLoggingActivationMessageSent(bool* activation_flag);
50
51   password_manager::PasswordManagerInternalsService* service_;
52
53   testing::StrictMock<MockLogReceiver> receiver_;
54 };
55
56 ChromePasswordManagerClientTest::ChromePasswordManagerClientTest()
57     : service_(NULL) {
58 }
59
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_);
67 }
68
69 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() {
70   return ChromePasswordManagerClient::FromWebContents(web_contents());
71 }
72
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);
78   if (!message)
79     return false;
80   Tuple1<bool> param;
81   AutofillMsg_SetLoggingState::Read(message, &param);
82   *activation_flag = param.a;
83   process()->sink().ClearMessages();
84   return true;
85 }
86
87 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoReceiver) {
88   ChromePasswordManagerClient* client = GetClient();
89
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());
94 }
95
96 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachReceiver) {
97   ChromePasswordManagerClient* client = GetClient();
98   EXPECT_FALSE(client->IsLoggingActive());
99
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());
107 }
108
109 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressDetachReceiver) {
110   ChromePasswordManagerClient* client = GetClient();
111
112   service_->RegisterReceiver(&receiver_);
113   EXPECT_TRUE(client->IsLoggingActive());
114   service_->UnregisterReceiver(&receiver_);
115   EXPECT_FALSE(client->IsLoggingActive());
116
117   // After detaching the logger, no text should be passed.
118   EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0);
119   client->LogSavePasswordProgress(kTestText);
120 }
121
122 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) {
123   ChromePasswordManagerClient* client = GetClient();
124   bool logging_active = false;
125
126   // Initially, the logging should be off, so no IPC messages.
127   EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active));
128
129   service_->RegisterReceiver(&receiver_);
130   EXPECT_TRUE(client->IsLoggingActive());
131   EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
132   EXPECT_TRUE(logging_active);
133
134   service_->UnregisterReceiver(&receiver_);
135   EXPECT_FALSE(client->IsLoggingActive());
136   EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
137   EXPECT_FALSE(logging_active);
138 }
139
140 TEST_F(ChromePasswordManagerClientTest, AnswerToPingsAboutLoggingState_Active) {
141   service_->RegisterReceiver(&receiver_);
142
143   process()->sink().ClearMessages();
144
145   // Ping the client for logging activity update.
146   AutofillHostMsg_PasswordAutofillAgentConstructed msg(0);
147   static_cast<IPC::Listener*>(GetClient())->OnMessageReceived(msg);
148
149   bool logging_active = false;
150   EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
151   EXPECT_TRUE(logging_active);
152
153   service_->UnregisterReceiver(&receiver_);
154 }
155
156 TEST_F(ChromePasswordManagerClientTest,
157        AnswerToPingsAboutLoggingState_Inactive) {
158   process()->sink().ClearMessages();
159
160   // Ping the client for logging activity update.
161   AutofillHostMsg_PasswordAutofillAgentConstructed msg(0);
162   static_cast<IPC::Listener*>(GetClient())->OnMessageReceived(msg);
163
164   bool logging_active = true;
165   EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active));
166   EXPECT_FALSE(logging_active);
167 }
168
169 TEST_F(ChromePasswordManagerClientTest,
170        IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) {
171   EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
172 }
173
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());
180   else
181     EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled());
182 }
183
184 TEST_F(ChromePasswordManagerClientTest, LogToAReceiver) {
185   ChromePasswordManagerClient* client = GetClient();
186   service_->RegisterReceiver(&receiver_);
187   EXPECT_TRUE(client->IsLoggingActive());
188
189   EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1);
190   client->LogSavePasswordProgress(kTestText);
191
192   service_->UnregisterReceiver(&receiver_);
193   EXPECT_FALSE(client->IsLoggingActive());
194 }