Chromium Code Reviews| 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() {} | 32 MockLogReceiver() {} |
|
Ilya Sherman
2014/05/13 04:32:18
nit: Can this line be omitted?
vabr (Chromium)
2014/05/13 09:27:11
Done.
| |
| 31 | 33 |
| 32 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); | 34 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); |
| 33 }; | 35 }; |
| 34 | 36 |
| 35 } // namespace | 37 } // namespace |
| 36 | 38 |
| 37 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { | 39 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { |
| 38 public: | 40 public: |
| 41 ChromePasswordManagerClientTest(); | |
| 42 | |
| 39 virtual void SetUp() OVERRIDE; | 43 virtual void SetUp() OVERRIDE; |
| 40 | 44 |
| 41 protected: | 45 protected: |
| 42 ChromePasswordManagerClient* GetClient(); | 46 ChromePasswordManagerClient* GetClient(); |
| 43 | 47 |
| 44 // If the test IPC sink contains an AutofillMsg_ChangeLoggingState message, | 48 // If the test IPC sink contains an AutofillMsg_ChangeLoggingState message, |
| 45 // then copies its argument into |activation_flag| and returns true. Otherwise | 49 // then copies its argument into |activation_flag| and returns true. Otherwise |
| 46 // returns false. | 50 // returns false. |
| 47 bool WasLoggingActivationMessageSent(bool* activation_flag); | 51 bool WasLoggingActivationMessageSent(bool* activation_flag); |
| 48 | 52 |
| 49 testing::StrictMock<MockPasswordManagerLogger> logger_; | 53 password_manager::PasswordManagerInternalsService* service_; |
| 54 | |
| 55 testing::StrictMock<MockLogReceiver> receiver_; | |
| 50 }; | 56 }; |
| 51 | 57 |
| 58 ChromePasswordManagerClientTest::ChromePasswordManagerClientTest() | |
| 59 : service_(NULL) { | |
| 60 } | |
| 61 | |
| 52 void ChromePasswordManagerClientTest::SetUp() { | 62 void ChromePasswordManagerClientTest::SetUp() { |
| 53 ChromeRenderViewHostTestHarness::SetUp(); | 63 ChromeRenderViewHostTestHarness::SetUp(); |
| 54 ChromePasswordManagerClient::CreateForWebContentsWithAutofillManagerDelegate( | 64 ChromePasswordManagerClient::CreateForWebContentsWithAutofillManagerDelegate( |
| 55 web_contents(), NULL); | 65 web_contents(), NULL); |
| 66 service_ = password_manager::PasswordManagerInternalsServiceFactory:: | |
| 67 GetForBrowserContext(profile()); | |
| 68 ASSERT_TRUE(service_); | |
| 56 } | 69 } |
|
Ilya Sherman
2014/05/13 04:32:18
Optional nit: It's fine to inline these implementa
vabr (Chromium)
2014/05/13 09:27:11
I actually prefer them separated: the declarations
| |
| 57 | 70 |
| 58 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { | 71 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { |
| 59 return ChromePasswordManagerClient::FromWebContents(web_contents()); | 72 return ChromePasswordManagerClient::FromWebContents(web_contents()); |
| 60 } | 73 } |
| 61 | 74 |
| 62 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( | 75 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( |
| 63 bool* activation_flag) { | 76 bool* activation_flag) { |
| 64 const uint32 kMsgID = AutofillMsg_ChangeLoggingState::ID; | 77 const uint32 kMsgID = AutofillMsg_ChangeLoggingState::ID; |
| 65 const IPC::Message* message = | 78 const IPC::Message* message = |
| 66 process()->sink().GetFirstMessageMatching(kMsgID); | 79 process()->sink().GetFirstMessageMatching(kMsgID); |
| 67 if (!message) | 80 if (!message) |
| 68 return false; | 81 return false; |
| 69 Tuple1<bool> param; | 82 Tuple1<bool> param; |
| 70 AutofillMsg_ChangeLoggingState::Read(message, ¶m); | 83 AutofillMsg_ChangeLoggingState::Read(message, ¶m); |
| 71 *activation_flag = param.a; | 84 *activation_flag = param.a; |
| 72 process()->sink().ClearMessages(); | 85 process()->sink().ClearMessages(); |
| 73 return true; | 86 return true; |
| 74 } | 87 } |
| 75 | 88 |
| 76 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoLogger) { | 89 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoReceiver) { |
| 77 ChromePasswordManagerClient* client = GetClient(); | 90 ChromePasswordManagerClient* client = GetClient(); |
| 78 | 91 |
| 79 EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(0); | 92 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0); |
| 80 // Before attaching the logger, no text should be passed. | 93 // Before attaching the receiver, no text should be passed. |
| 81 client->LogSavePasswordProgress(kTestText); | 94 client->LogSavePasswordProgress(kTestText); |
| 82 EXPECT_FALSE(client->IsLoggingActive()); | 95 EXPECT_FALSE(client->IsLoggingActive()); |
| 83 } | 96 } |
| 84 | 97 |
| 85 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachLogger) { | 98 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachReceiver) { |
| 86 ChromePasswordManagerClient* client = GetClient(); | 99 ChromePasswordManagerClient* client = GetClient(); |
| 100 EXPECT_FALSE(client->IsLoggingActive()); | |
| 87 | 101 |
| 88 // After attaching the logger, text should be passed. | 102 // After attaching the logger, text should be passed. |
| 89 client->SetLogger(&logger_); | 103 EXPECT_EQ(std::string(), service_->RegisterReceiver(&receiver_)); |
| 90 EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(1); | 104 EXPECT_TRUE(client->IsLoggingActive()); |
| 105 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1); | |
| 91 client->LogSavePasswordProgress(kTestText); | 106 client->LogSavePasswordProgress(kTestText); |
| 92 EXPECT_TRUE(client->IsLoggingActive()); | 107 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()); | 108 EXPECT_FALSE(client->IsLoggingActive()); |
| 104 } | 109 } |
| 105 | 110 |
| 111 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressDetachReceiver) { | |
| 112 ChromePasswordManagerClient* client = GetClient(); | |
| 113 | |
| 114 EXPECT_EQ(std::string(), service_->RegisterReceiver(&receiver_)); | |
| 115 EXPECT_TRUE(client->IsLoggingActive()); | |
| 116 service_->UnregisterReceiver(&receiver_); | |
| 117 EXPECT_FALSE(client->IsLoggingActive()); | |
| 118 | |
| 119 // After detaching the logger, no text should be passed. | |
| 120 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(0); | |
| 121 client->LogSavePasswordProgress(kTestText); | |
| 122 } | |
| 123 | |
| 106 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { | 124 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { |
| 107 ChromePasswordManagerClient* client = GetClient(); | 125 ChromePasswordManagerClient* client = GetClient(); |
| 108 bool logging_active = false; | 126 bool logging_active = false; |
| 109 | 127 |
| 110 // Initially, the logging should be off, so no IPC messages. | 128 // Initially, the logging should be off, so no IPC messages. |
| 111 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active)); | 129 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active)); |
| 112 | 130 |
| 113 client->SetLogger(&logger_); | 131 EXPECT_EQ(std::string(), service_->RegisterReceiver(&receiver_)); |
| 132 EXPECT_TRUE(client->IsLoggingActive()); | |
| 114 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); | 133 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); |
| 115 EXPECT_TRUE(logging_active); | 134 EXPECT_TRUE(logging_active); |
| 116 | 135 |
| 117 client->SetLogger(NULL); | 136 service_->UnregisterReceiver(&receiver_); |
| 137 EXPECT_FALSE(client->IsLoggingActive()); | |
| 118 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); | 138 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); |
| 119 EXPECT_FALSE(logging_active); | 139 EXPECT_FALSE(logging_active); |
| 120 } | 140 } |
| 121 | 141 |
| 122 TEST_F(ChromePasswordManagerClientTest, | 142 TEST_F(ChromePasswordManagerClientTest, |
| 123 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) { | 143 IsAutomaticPasswordSavingEnabledDefaultBehaviourTest) { |
| 124 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); | 144 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); |
| 125 } | 145 } |
| 126 | 146 |
| 127 TEST_F(ChromePasswordManagerClientTest, | 147 TEST_F(ChromePasswordManagerClientTest, |
| 128 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) { | 148 IsAutomaticPasswordSavingEnabledWhenFlagIsSetTest) { |
| 129 CommandLine::ForCurrentProcess()->AppendSwitch( | 149 CommandLine::ForCurrentProcess()->AppendSwitch( |
| 130 password_manager::switches::kEnableAutomaticPasswordSaving); | 150 password_manager::switches::kEnableAutomaticPasswordSaving); |
| 131 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_UNKNOWN) | 151 if (chrome::VersionInfo::GetChannel() == chrome::VersionInfo::CHANNEL_UNKNOWN) |
| 132 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled()); | 152 EXPECT_TRUE(GetClient()->IsAutomaticPasswordSavingEnabled()); |
| 133 else | 153 else |
| 134 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); | 154 EXPECT_FALSE(GetClient()->IsAutomaticPasswordSavingEnabled()); |
| 135 } | 155 } |
| 156 | |
| 157 TEST_F(ChromePasswordManagerClientTest, LogToAReceiver) { | |
| 158 ChromePasswordManagerClient* client = GetClient(); | |
| 159 EXPECT_EQ(std::string(), service_->RegisterReceiver(&receiver_)); | |
|
Ilya Sherman
2014/05/13 04:32:18
I'm noticing that all of the tests in this file ex
vabr (Chromium)
2014/05/13 09:27:11
That's a fair point. Coverage for non-empty string
| |
| 160 EXPECT_TRUE(client->IsLoggingActive()); | |
| 161 | |
| 162 EXPECT_CALL(receiver_, LogSavePasswordProgress(kTestText)).Times(1); | |
| 163 client->LogSavePasswordProgress(kTestText); | |
| 164 | |
| 165 service_->UnregisterReceiver(&receiver_); | |
| 166 EXPECT_FALSE(client->IsLoggingActive()); | |
| 167 } | |
| OLD | NEW |