OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/command_line.h" | 5 #include "base/command_line.h" |
6 | 6 |
7 #include "chrome/browser/password_manager/chrome_password_manager_client.h" | 7 #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
8 | 8 |
9 #include "chrome/common/chrome_version_info.h" | 9 #include "chrome/common/chrome_version_info.h" |
10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 10 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
| 11 #include "chrome/test/base/testing_profile.h" |
11 #include "components/autofill/content/common/autofill_messages.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/password_manager_internals_se
rvice.h" |
12 #include "components/password_manager/core/browser/password_manager_logger.h" | 15 #include "components/password_manager/core/browser/password_manager_logger.h" |
13 #include "components/password_manager/core/common/password_manager_switches.h" | 16 #include "components/password_manager/core/common/password_manager_switches.h" |
14 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
15 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
16 #include "content/public/test/mock_render_process_host.h" | 19 #include "content/public/test/mock_render_process_host.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
19 | 22 |
20 using content::BrowserContext; | 23 using content::BrowserContext; |
21 using content::WebContents; | 24 using content::WebContents; |
22 | 25 |
23 namespace { | 26 namespace { |
24 | 27 |
25 const char kTestText[] = "abcd1234"; | 28 const char kTestText[] = "abcd1234"; |
26 | 29 |
27 class MockPasswordManagerLogger | 30 class MockLogReceiver : public password_manager::PasswordManagerLogger { |
28 : public password_manager::PasswordManagerLogger { | |
29 public: | 31 public: |
30 MockPasswordManagerLogger() {} | |
31 | |
32 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); | 32 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); |
33 }; | 33 }; |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { | 37 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { |
38 public: | 38 public: |
| 39 ChromePasswordManagerClientTest(); |
| 40 |
39 virtual void SetUp() OVERRIDE; | 41 virtual void SetUp() OVERRIDE; |
40 | 42 |
41 protected: | 43 protected: |
42 ChromePasswordManagerClient* GetClient(); | 44 ChromePasswordManagerClient* GetClient(); |
43 | 45 |
44 // If the test IPC sink contains an AutofillMsg_ChangeLoggingState message, | 46 // If the test IPC sink contains an AutofillMsg_ChangeLoggingState message, |
45 // then copies its argument into |activation_flag| and returns true. Otherwise | 47 // then copies its argument into |activation_flag| and returns true. Otherwise |
46 // returns false. | 48 // returns false. |
47 bool WasLoggingActivationMessageSent(bool* activation_flag); | 49 bool WasLoggingActivationMessageSent(bool* activation_flag); |
48 | 50 |
49 testing::StrictMock<MockPasswordManagerLogger> logger_; | 51 password_manager::PasswordManagerInternalsService* service_; |
| 52 |
| 53 testing::StrictMock<MockLogReceiver> receiver_; |
50 }; | 54 }; |
51 | 55 |
| 56 ChromePasswordManagerClientTest::ChromePasswordManagerClientTest() |
| 57 : service_(NULL) { |
| 58 } |
| 59 |
52 void ChromePasswordManagerClientTest::SetUp() { | 60 void ChromePasswordManagerClientTest::SetUp() { |
53 ChromeRenderViewHostTestHarness::SetUp(); | 61 ChromeRenderViewHostTestHarness::SetUp(); |
54 ChromePasswordManagerClient::CreateForWebContentsWithAutofillManagerDelegate( | 62 ChromePasswordManagerClient::CreateForWebContentsWithAutofillManagerDelegate( |
55 web_contents(), NULL); | 63 web_contents(), NULL); |
| 64 service_ = password_manager::PasswordManagerInternalsServiceFactory:: |
| 65 GetForBrowserContext(profile()); |
| 66 ASSERT_TRUE(service_); |
56 } | 67 } |
57 | 68 |
58 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { | 69 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { |
59 return ChromePasswordManagerClient::FromWebContents(web_contents()); | 70 return ChromePasswordManagerClient::FromWebContents(web_contents()); |
60 } | 71 } |
61 | 72 |
62 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( | 73 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( |
63 bool* activation_flag) { | 74 bool* activation_flag) { |
64 const uint32 kMsgID = AutofillMsg_ChangeLoggingState::ID; | 75 const uint32 kMsgID = AutofillMsg_ChangeLoggingState::ID; |
65 const IPC::Message* message = | 76 const IPC::Message* message = |
66 process()->sink().GetFirstMessageMatching(kMsgID); | 77 process()->sink().GetFirstMessageMatching(kMsgID); |
67 if (!message) | 78 if (!message) |
68 return false; | 79 return false; |
69 Tuple1<bool> param; | 80 Tuple1<bool> param; |
70 AutofillMsg_ChangeLoggingState::Read(message, ¶m); | 81 AutofillMsg_ChangeLoggingState::Read(message, ¶m); |
71 *activation_flag = param.a; | 82 *activation_flag = param.a; |
72 process()->sink().ClearMessages(); | 83 process()->sink().ClearMessages(); |
73 return true; | 84 return true; |
74 } | 85 } |
75 | 86 |
76 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoLogger) { | 87 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoReceiver) { |
77 ChromePasswordManagerClient* client = GetClient(); | 88 ChromePasswordManagerClient* client = GetClient(); |
78 | 89 |
79 EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(0); | 90 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0); |
80 // Before attaching the logger, no text should be passed. | 91 // Before attaching the receiver, no text should be passed. |
81 client->LogSavePasswordProgress(kTestText); | 92 client->LogSavePasswordProgress(kTestText); |
82 EXPECT_FALSE(client->IsLoggingActive()); | 93 EXPECT_FALSE(client->IsLoggingActive()); |
83 } | 94 } |
84 | 95 |
85 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachLogger) { | 96 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachReceiver) { |
86 ChromePasswordManagerClient* client = GetClient(); | 97 ChromePasswordManagerClient* client = GetClient(); |
| 98 EXPECT_FALSE(client->IsLoggingActive()); |
87 | 99 |
88 // After attaching the logger, text should be passed. | 100 // After attaching the logger, text should be passed. |
89 client->SetLogger(&logger_); | 101 service_->RegisterReceiver(&receiver_); |
90 EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(1); | 102 EXPECT_TRUE(client->IsLoggingActive()); |
| 103 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1); |
91 client->LogSavePasswordProgress(kTestText); | 104 client->LogSavePasswordProgress(kTestText); |
92 EXPECT_TRUE(client->IsLoggingActive()); | 105 service_->UnregisterReceiver(&receiver_); |
93 } | |
94 | |
95 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressDetachLogger) { | |
96 ChromePasswordManagerClient* client = GetClient(); | |
97 | |
98 client->SetLogger(&logger_); | |
99 // After detaching the logger, no text should be passed. | |
100 client->SetLogger(NULL); | |
101 EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(0); | |
102 client->LogSavePasswordProgress(kTestText); | |
103 EXPECT_FALSE(client->IsLoggingActive()); | 106 EXPECT_FALSE(client->IsLoggingActive()); |
104 } | 107 } |
105 | 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 |
106 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { | 122 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { |
107 ChromePasswordManagerClient* client = GetClient(); | 123 ChromePasswordManagerClient* client = GetClient(); |
108 bool logging_active = false; | 124 bool logging_active = false; |
109 | 125 |
110 // Initially, the logging should be off, so no IPC messages. | 126 // Initially, the logging should be off, so no IPC messages. |
111 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active)); | 127 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active)); |
112 | 128 |
113 client->SetLogger(&logger_); | 129 service_->RegisterReceiver(&receiver_); |
| 130 EXPECT_TRUE(client->IsLoggingActive()); |
114 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); | 131 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); |
115 EXPECT_TRUE(logging_active); | 132 EXPECT_TRUE(logging_active); |
116 | 133 |
117 client->SetLogger(NULL); | 134 service_->UnregisterReceiver(&receiver_); |
| 135 EXPECT_FALSE(client->IsLoggingActive()); |
118 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); | 136 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); |
119 EXPECT_FALSE(logging_active); | 137 EXPECT_FALSE(logging_active); |
120 } | 138 } |
121 | 139 |
122 TEST_F(ChromePasswordManagerClientTest, | 140 TEST_F(ChromePasswordManagerClientTest, |
123 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) { | 141 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) { |
124 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); | 142 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); |
125 } | 143 } |
126 | 144 |
127 TEST_F(ChromePasswordManagerClientTest, | 145 TEST_F(ChromePasswordManagerClientTest, |
128 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) { | 146 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) { |
129 CommandLine::ForCurrentProcess()->AppendSwitch( | 147 CommandLine::ForCurrentProcess()->AppendSwitch( |
130 password_manager::switches::kEnableAutomaticPasswordSaving); | 148 password_manager::switches::kEnableAutomaticPasswordSaving); |
131 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_UNKNOWN) | 149 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_UNKNOWN) |
132 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled()); | 150 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled()); |
133 else | 151 else |
134 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); | 152 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); |
135 } | 153 } |
| 154 |
| 155 TEST_F(ChromePasswordManagerClientTest, LogToAReceiver) { |
| 156 ChromePasswordManagerClient* client = GetClient(); |
| 157 service_->RegisterReceiver(&receiver_); |
| 158 EXPECT_TRUE(client->IsLoggingActive()); |
| 159 |
| 160 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1); |
| 161 client->LogSavePasswordProgress(kTestText); |
| 162 |
| 163 service_->UnregisterReceiver(&receiver_); |
| 164 EXPECT_FALSE(client->IsLoggingActive()); |
| 165 } |
OLD | NEW |