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 "content/public/test/render_view_test.h"
8 #include "ipc/ipc_test_sink.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/WebKit/public/platform/WebCredential.h"
11 #include "third_party/WebKit/public/platform/WebCredentialManagerClient.h"
12 #include "third_party/WebKit/public/platform/WebCredentialManagerError.h"
13 #include "third_party/WebKit/public/platform/WebLocalCredential.h"
15 namespace password_manager {
19 class CredentialManagerClientTest : public content::RenderViewTest {
21 CredentialManagerClientTest()
22 : callback_errored_(false), callback_succeeded_(false) {}
23 ~CredentialManagerClientTest() override {}
25 void SetUp() override {
26 content::RenderViewTest::SetUp();
27 credential_.reset(new blink::WebLocalCredential("", "", GURL(), ""));
28 client_.reset(new CredentialManagerClient(view_));
31 void TearDown() override {
33 content::RenderViewTest::TearDown();
36 IPC::TestSink& sink() { return render_thread_->sink(); }
38 blink::WebCredential* credential() { return credential_.get(); }
40 // The browser's response to any of the messages the client sends must contain
41 // a request ID so that the client knows which request is being serviced. This
42 // method grabs the ID from an outgoing |message_id| message, and sets the
43 // |request_id| param to its value. If no request ID can be found, the method
44 // returns false, and the |request_id| is set to -1.
46 // Clears any pending messages upon return.
47 bool ExtractRequestId(uint32 message_id, int& request_id) {
49 const IPC::Message* message = sink().GetFirstMessageMatching(message_id);
54 case CredentialManagerHostMsg_NotifyFailedSignIn::ID: {
55 Tuple2<int, CredentialInfo> param;
56 CredentialManagerHostMsg_NotifyFailedSignIn::Read(message, ¶m);
61 case CredentialManagerHostMsg_NotifySignedIn::ID: {
62 Tuple2<int, CredentialInfo> param;
63 CredentialManagerHostMsg_NotifySignedIn::Read(message, ¶m);
68 case CredentialManagerHostMsg_NotifySignedOut::ID: {
70 CredentialManagerHostMsg_NotifySignedOut::Read(message, ¶m);
75 case CredentialManagerHostMsg_RequestCredential::ID: {
76 Tuple3<int, bool, std::vector<GURL> > param;
77 CredentialManagerHostMsg_RequestCredential::Read(message, ¶m);
85 sink().ClearMessages();
86 return request_id != -1;
89 bool callback_errored() const { return callback_errored_; }
90 void set_callback_errored(bool state) { callback_errored_ = state; }
91 bool callback_succeeded() const { return callback_succeeded_; }
92 void set_callback_succeeded(bool state) { callback_succeeded_ = state; }
95 scoped_ptr<CredentialManagerClient> 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::WebLocalCredential> credential_;
108 class TestNotificationCallbacks
109 : public blink::WebCredentialManagerClient::NotificationCallbacks {
111 explicit TestNotificationCallbacks(CredentialManagerClientTest* test)
114 virtual ~TestNotificationCallbacks() {}
116 virtual void onSuccess() { test_->set_callback_succeeded(true); }
118 virtual void onError(blink::WebCredentialManagerError* reason) {
119 test_->set_callback_errored(true);
123 CredentialManagerClientTest* test_;
126 class TestRequestCallbacks
127 : public blink::WebCredentialManagerClient::RequestCallbacks {
129 explicit TestRequestCallbacks(CredentialManagerClientTest* test)
132 virtual ~TestRequestCallbacks() {}
134 virtual void onSuccess(blink::WebCredential*) {
135 test_->set_callback_succeeded(true);
138 virtual void onError(blink::WebCredentialManagerError* reason) {
139 test_->set_callback_errored(true);
143 CredentialManagerClientTest* test_;
148 TEST_F(CredentialManagerClientTest, SendNotifyFailedSignIn) {
150 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID,
153 scoped_ptr<TestNotificationCallbacks> callbacks(
154 new TestNotificationCallbacks(this));
155 client_->dispatchFailedSignIn(*credential(), callbacks.release());
157 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID,
160 client_->OnAcknowledgeFailedSignIn(request_id);
161 EXPECT_TRUE(callback_succeeded());
162 EXPECT_FALSE(callback_errored());
165 TEST_F(CredentialManagerClientTest, SendNotifySignedIn) {
167 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID,
170 scoped_ptr<TestNotificationCallbacks> callbacks(
171 new TestNotificationCallbacks(this));
172 client_->dispatchSignedIn(*credential(), callbacks.release());
174 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID,
177 client_->OnAcknowledgeSignedIn(request_id);
178 EXPECT_TRUE(callback_succeeded());
179 EXPECT_FALSE(callback_errored());
182 TEST_F(CredentialManagerClientTest, SendNotifySignedOut) {
184 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID,
187 scoped_ptr<TestNotificationCallbacks> callbacks(
188 new TestNotificationCallbacks(this));
189 client_->dispatchSignedOut(callbacks.release());
191 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID,
194 client_->OnAcknowledgeSignedOut(request_id);
195 EXPECT_TRUE(callback_succeeded());
196 EXPECT_FALSE(callback_errored());
199 TEST_F(CredentialManagerClientTest, SendRequestCredential) {
201 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
204 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this));
205 std::vector<GURL> federations;
206 client_->dispatchRequest(false, federations, callbacks.release());
208 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
212 info.type = CREDENTIAL_TYPE_LOCAL;
213 client_->OnSendCredential(request_id, info);
214 EXPECT_TRUE(callback_succeeded());
215 EXPECT_FALSE(callback_errored());
218 TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) {
220 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
223 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this));
224 std::vector<GURL> federations;
225 client_->dispatchRequest(false, federations, callbacks.release());
227 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
230 CredentialInfo info; // Send an empty credential in response.
231 client_->OnSendCredential(request_id, info);
232 EXPECT_TRUE(callback_succeeded());
233 EXPECT_FALSE(callback_errored());
236 } // namespace password_manager