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.
5 #include "components/password_manager/content/common/credential_manager_messages.h"
6 #include "components/password_manager/content/renderer/credential_manager_client.h"
7 #include "components/password_manager/content/renderer/test_credential_manager_client.h"
8 #include "content/public/test/mock_render_thread.h"
9 #include "content/test/blink_test_environment.h"
10 #include "ipc/ipc_test_sink.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/WebKit/public/platform/WebCredential.h"
13 #include "third_party/WebKit/public/platform/WebCredentialManagerClient.h"
14 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h"
16 namespace password_manager
{
20 class CredentialManagerClientTest
: public testing::Test
{
22 CredentialManagerClientTest()
23 : callback_errored_(false),
24 callback_succeeded_(false) {
25 blink::WebString string
= blink::WebString::fromUTF8("");
26 GURL
url("https://example.com/image");
27 credential_
.reset(new blink::WebCredential(string
, string
, url
));
29 virtual ~CredentialManagerClientTest() {}
31 static void SetUpTestCase() { content::SetUpBlinkTestEnvironment(); }
33 static void TearDownTestCase() { content::TearDownBlinkTestEnvironment(); }
35 IPC::TestSink
& sink() { return render_thread_
.sink(); }
37 blink::WebCredential
* credential() { return credential_
.get(); }
39 // The browser's response to any of the messages the client sends must contain
40 // a request ID so that the client knows which request is being serviced. This
41 // method grabs the ID from an outgoing |message_id| message, and sets the
42 // |request_id| param to its value. If no request ID can be found, the method
43 // returns false, and the |request_id| is set to -1.
45 // Clears any pending messages upon return.
46 bool ExtractRequestId(uint32 message_id
, int& request_id
) {
48 const IPC::Message
* message
= sink().GetFirstMessageMatching(message_id
);
53 case CredentialManagerHostMsg_NotifyFailedSignIn::ID
: {
54 Tuple2
<int, CredentialInfo
> param
;
55 CredentialManagerHostMsg_NotifyFailedSignIn::Read(message
, ¶m
);
60 case CredentialManagerHostMsg_NotifySignedIn::ID
: {
61 Tuple2
<int, CredentialInfo
> param
;
62 CredentialManagerHostMsg_NotifySignedIn::Read(message
, ¶m
);
67 case CredentialManagerHostMsg_NotifySignedOut::ID
: {
69 CredentialManagerHostMsg_NotifySignedOut::Read(message
, ¶m
);
74 case CredentialManagerHostMsg_RequestCredential::ID
: {
75 Tuple3
<int, bool, std::vector
<GURL
> > param
;
76 CredentialManagerHostMsg_RequestCredential::Read(message
, ¶m
);
84 sink().ClearMessages();
85 return request_id
!= -1;
88 bool callback_errored() const { return callback_errored_
; }
89 void set_callback_errored(bool state
) { callback_errored_
= state
; }
90 bool callback_succeeded() const { return callback_succeeded_
; }
91 void set_callback_succeeded(bool state
) { callback_succeeded_
= state
; }
94 content::MockRenderThread render_thread_
;
95 TestCredentialManagerClient client_
;
97 // True if a message's callback's 'onSuccess'/'onError' methods were called,
98 // false otherwise. We put these on the test object rather than on the
99 // Test*Callbacks objects because ownership of those objects passes into the
100 // client, which destroys the callbacks after calling them to resolve the
101 // pending Blink-side Promise.
102 bool callback_errored_
;
103 bool callback_succeeded_
;
105 scoped_ptr
<blink::WebCredential
> credential_
;
108 class TestNotificationCallbacks
109 : public blink::WebCredentialManagerClient::NotificationCallbacks
{
111 explicit TestNotificationCallbacks(CredentialManagerClientTest
* test
)
115 virtual ~TestNotificationCallbacks() {}
117 virtual void onSuccess() OVERRIDE
{ test_
->set_callback_succeeded(true); }
119 virtual void onError(blink::WebCredentialManagerError
* reason
) OVERRIDE
{
120 test_
->set_callback_errored(true);
124 CredentialManagerClientTest
* test_
;
127 class TestRequestCallbacks
128 : public blink::WebCredentialManagerClient::RequestCallbacks
{
130 explicit TestRequestCallbacks(CredentialManagerClientTest
* test
)
134 virtual ~TestRequestCallbacks() {}
136 virtual void onSuccess(blink::WebCredential
*) OVERRIDE
{
137 test_
->set_callback_succeeded(true);
140 virtual void onError(blink::WebCredentialManagerError
* reason
) OVERRIDE
{
141 test_
->set_callback_errored(true);
145 CredentialManagerClientTest
* test_
;
150 TEST_F(CredentialManagerClientTest
, SendNotifyFailedSignIn
) {
152 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID
,
155 scoped_ptr
<TestNotificationCallbacks
> callbacks(
156 new TestNotificationCallbacks(this));
157 client_
.dispatchFailedSignIn(*credential(), callbacks
.release());
159 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID
,
162 client_
.OnAcknowledgeFailedSignIn(request_id
);
163 EXPECT_TRUE(callback_succeeded());
164 EXPECT_FALSE(callback_errored());
167 TEST_F(CredentialManagerClientTest
, SendNotifySignedIn
) {
169 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID
,
172 scoped_ptr
<TestNotificationCallbacks
> callbacks(
173 new TestNotificationCallbacks(this));
174 client_
.dispatchSignedIn(*credential(), callbacks
.release());
176 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID
,
179 client_
.OnAcknowledgeSignedIn(request_id
);
180 EXPECT_TRUE(callback_succeeded());
181 EXPECT_FALSE(callback_errored());
184 TEST_F(CredentialManagerClientTest
, SendNotifySignedOut
) {
186 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID
,
189 scoped_ptr
<TestNotificationCallbacks
> callbacks(
190 new TestNotificationCallbacks(this));
191 client_
.dispatchSignedOut(callbacks
.release());
193 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID
,
196 client_
.OnAcknowledgeSignedOut(request_id
);
197 EXPECT_TRUE(callback_succeeded());
198 EXPECT_FALSE(callback_errored());
201 TEST_F(CredentialManagerClientTest
, SendRequestCredential
) {
203 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID
,
206 scoped_ptr
<TestRequestCallbacks
> callbacks(new TestRequestCallbacks(this));
207 std::vector
<GURL
> federations
;
208 client_
.dispatchRequest(false, federations
, callbacks
.release());
210 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID
,
214 client_
.OnSendCredential(request_id
, info
);
215 EXPECT_TRUE(callback_succeeded());
216 EXPECT_FALSE(callback_errored());
219 } // namespace password_manager