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 "chrome/browser/password_manager/chrome_password_manager_client.h" | 5 #include "chrome/browser/password_manager/chrome_password_manager_client.h" |
6 | 6 |
7 #include "chrome/test/base/chrome_render_view_host_test_harness.h" | 7 #include "chrome/test/base/chrome_render_view_host_test_harness.h" |
8 #include "components/autofill/content/common/autofill_messages.h" | |
8 #include "components/password_manager/core/browser/password_manager_logger.h" | 9 #include "components/password_manager/core/browser/password_manager_logger.h" |
9 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
10 #include "content/public/browser/web_contents.h" | 11 #include "content/public/browser/web_contents.h" |
12 #include "content/public/test/mock_render_process_host.h" | |
11 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
13 | 15 |
14 using content::BrowserContext; | 16 using content::BrowserContext; |
15 using content::WebContents; | 17 using content::WebContents; |
16 | 18 |
17 namespace { | 19 namespace { |
18 | 20 |
19 const char kTestText[] = "abcd1234"; | 21 const char kTestText[] = "abcd1234"; |
20 | 22 |
21 class MockPasswordManagerLogger | 23 class MockPasswordManagerLogger |
22 : public password_manager::PasswordManagerLogger { | 24 : public password_manager::PasswordManagerLogger { |
23 public: | 25 public: |
24 MockPasswordManagerLogger() {} | 26 MockPasswordManagerLogger() {} |
25 | 27 |
26 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); | 28 MOCK_METHOD1(LogSavePasswordProgress, void(const std::string&)); |
27 }; | 29 }; |
28 | 30 |
29 } // namespace | 31 } // namespace |
30 | 32 |
31 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { | 33 class ChromePasswordManagerClientTest : public ChromeRenderViewHostTestHarness { |
32 public: | 34 public: |
33 virtual void SetUp() OVERRIDE; | 35 virtual void SetUp() OVERRIDE; |
34 | 36 |
35 protected: | 37 protected: |
36 ChromePasswordManagerClient* GetClient(); | 38 ChromePasswordManagerClient* GetClient(); |
37 | 39 |
38 testing::StrictMock<MockPasswordManagerLogger> logger; | 40 // If the test IPC sink contains an AutofillMsg_ChangeLoggingState message, |
41 // then copies its argument into |activation_flag| and returns true. Otherwise | |
42 // returns false. | |
43 bool WasLoggingActivationMessageSent(bool* activation_flag); | |
44 | |
45 testing::StrictMock<MockPasswordManagerLogger> logger_; | |
39 }; | 46 }; |
40 | 47 |
41 void ChromePasswordManagerClientTest::SetUp() { | 48 void ChromePasswordManagerClientTest::SetUp() { |
42 ChromeRenderViewHostTestHarness::SetUp(); | 49 ChromeRenderViewHostTestHarness::SetUp(); |
43 ChromePasswordManagerClient::CreateForWebContents(web_contents()); | 50 ChromePasswordManagerClient::CreateForWebContents(web_contents()); |
44 } | 51 } |
45 | 52 |
46 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { | 53 ChromePasswordManagerClient* ChromePasswordManagerClientTest::GetClient() { |
47 return ChromePasswordManagerClient::FromWebContents(web_contents()); | 54 return ChromePasswordManagerClient::FromWebContents(web_contents()); |
48 } | 55 } |
49 | 56 |
57 bool ChromePasswordManagerClientTest::WasLoggingActivationMessageSent( | |
58 bool* activation_flag) { | |
59 const uint32 kMsgID = AutofillMsg_ChangeLoggingState::ID; | |
Ilya Sherman
2014/04/23 20:21:44
nit: De-indent this line and all of the other line
vabr (Chromium)
2014/04/24 10:59:27
Done.
| |
60 const IPC::Message* message = | |
61 process()->sink().GetFirstMessageMatching(kMsgID); | |
62 if (!message) | |
63 return false; | |
64 Tuple1<bool> param; | |
65 AutofillMsg_ChangeLoggingState::Read(message, ¶m); | |
66 *activation_flag = param.a; | |
67 process()->sink().ClearMessages(); | |
68 return true; | |
69 } | |
70 | |
50 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoLogger) { | 71 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNoLogger) { |
51 ChromePasswordManagerClient* client = GetClient(); | 72 ChromePasswordManagerClient* client = GetClient(); |
52 | 73 |
53 EXPECT_CALL(logger, LogSavePasswordProgress(kTestText)).Times(0); | 74 EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(0); |
54 // Before attaching the logger, no text should be passed. | 75 // Before attaching the logger, no text should be passed. |
55 client->LogSavePasswordProgress(kTestText); | 76 client->LogSavePasswordProgress(kTestText); |
56 EXPECT_FALSE(client->IsLoggingActive()); | 77 EXPECT_FALSE(client->IsLoggingActive()); |
57 } | 78 } |
58 | 79 |
59 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachLogger) { | 80 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressAttachLogger) { |
60 ChromePasswordManagerClient* client = GetClient(); | 81 ChromePasswordManagerClient* client = GetClient(); |
61 | 82 |
62 // After attaching the logger, text should be passed. | 83 // After attaching the logger, text should be passed. |
63 client->SetLogger(&logger); | 84 client->SetLogger(&logger_); |
64 EXPECT_CALL(logger, LogSavePasswordProgress(kTestText)).Times(1); | 85 EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(1); |
65 client->LogSavePasswordProgress(kTestText); | 86 client->LogSavePasswordProgress(kTestText); |
66 EXPECT_TRUE(client->IsLoggingActive()); | 87 EXPECT_TRUE(client->IsLoggingActive()); |
67 } | 88 } |
68 | 89 |
69 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressDetachLogger) { | 90 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressDetachLogger) { |
70 ChromePasswordManagerClient* client = GetClient(); | 91 ChromePasswordManagerClient* client = GetClient(); |
71 | 92 |
72 client->SetLogger(&logger); | 93 client->SetLogger(&logger_); |
73 // After detaching the logger, no text should be passed. | 94 // After detaching the logger, no text should be passed. |
74 client->SetLogger(NULL); | 95 client->SetLogger(NULL); |
75 EXPECT_CALL(logger, LogSavePasswordProgress(kTestText)).Times(0); | 96 EXPECT_CALL(logger_, LogSavePasswordProgress(kTestText)).Times(0); |
76 client->LogSavePasswordProgress(kTestText); | 97 client->LogSavePasswordProgress(kTestText); |
77 EXPECT_FALSE(client->IsLoggingActive()); | 98 EXPECT_FALSE(client->IsLoggingActive()); |
78 } | 99 } |
100 | |
101 TEST_F(ChromePasswordManagerClientTest, LogSavePasswordProgressNotifyRenderer) { | |
102 ChromePasswordManagerClient* client = GetClient(); | |
103 bool logging_active = false; | |
104 | |
105 // Initially, the logging should be off, so no IPC messages. | |
106 EXPECT_FALSE(WasLoggingActivationMessageSent(&logging_active)); | |
107 | |
108 client->SetLogger(&logger_); | |
109 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); | |
110 EXPECT_TRUE(logging_active); | |
111 | |
112 client->SetLogger(NULL); | |
113 EXPECT_TRUE(WasLoggingActivationMessageSent(&logging_active)); | |
114 EXPECT_FALSE(logging_active); | |
115 } | |
OLD | NEW |