Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / password_manager / content / renderer / credential_manager_client_unittest.cc
blob31d5c7300f2097c8e684e7a6693467178ec39a78
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 {
18 namespace {
20 class CredentialManagerClientTest : public testing::Test {
21 public:
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) {
47 request_id = -1;
48 const IPC::Message* message = sink().GetFirstMessageMatching(message_id);
49 if (!message)
50 return false;
52 switch (message_id) {
53 case CredentialManagerHostMsg_NotifyFailedSignIn::ID: {
54 Tuple2<int, CredentialInfo> param;
55 CredentialManagerHostMsg_NotifyFailedSignIn::Read(message, &param);
56 request_id = param.a;
57 break;
60 case CredentialManagerHostMsg_NotifySignedIn::ID: {
61 Tuple2<int, CredentialInfo> param;
62 CredentialManagerHostMsg_NotifySignedIn::Read(message, &param);
63 request_id = param.a;
64 break;
67 case CredentialManagerHostMsg_NotifySignedOut::ID: {
68 Tuple1<int> param;
69 CredentialManagerHostMsg_NotifySignedOut::Read(message, &param);
70 request_id = param.a;
71 break;
74 case CredentialManagerHostMsg_RequestCredential::ID: {
75 Tuple3<int, bool, std::vector<GURL> > param;
76 CredentialManagerHostMsg_RequestCredential::Read(message, &param);
77 request_id = param.a;
78 break;
81 default:
82 break;
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; }
93 protected:
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 {
110 public:
111 explicit TestNotificationCallbacks(CredentialManagerClientTest* test)
112 : test_(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);
123 private:
124 CredentialManagerClientTest* test_;
127 class TestRequestCallbacks
128 : public blink::WebCredentialManagerClient::RequestCallbacks {
129 public:
130 explicit TestRequestCallbacks(CredentialManagerClientTest* test)
131 : test_(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);
144 private:
145 CredentialManagerClientTest* test_;
148 } // namespace
150 TEST_F(CredentialManagerClientTest, SendNotifyFailedSignIn) {
151 int request_id;
152 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID,
153 request_id));
155 scoped_ptr<TestNotificationCallbacks> callbacks(
156 new TestNotificationCallbacks(this));
157 client_.dispatchFailedSignIn(*credential(), callbacks.release());
159 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID,
160 request_id));
162 client_.OnAcknowledgeFailedSignIn(request_id);
163 EXPECT_TRUE(callback_succeeded());
164 EXPECT_FALSE(callback_errored());
167 TEST_F(CredentialManagerClientTest, SendNotifySignedIn) {
168 int request_id;
169 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID,
170 request_id));
172 scoped_ptr<TestNotificationCallbacks> callbacks(
173 new TestNotificationCallbacks(this));
174 client_.dispatchSignedIn(*credential(), callbacks.release());
176 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID,
177 request_id));
179 client_.OnAcknowledgeSignedIn(request_id);
180 EXPECT_TRUE(callback_succeeded());
181 EXPECT_FALSE(callback_errored());
184 TEST_F(CredentialManagerClientTest, SendNotifySignedOut) {
185 int request_id;
186 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID,
187 request_id));
189 scoped_ptr<TestNotificationCallbacks> callbacks(
190 new TestNotificationCallbacks(this));
191 client_.dispatchSignedOut(callbacks.release());
193 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID,
194 request_id));
196 client_.OnAcknowledgeSignedOut(request_id);
197 EXPECT_TRUE(callback_succeeded());
198 EXPECT_FALSE(callback_errored());
201 TEST_F(CredentialManagerClientTest, SendRequestCredential) {
202 int request_id;
203 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
204 request_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,
211 request_id));
213 CredentialInfo info;
214 client_.OnSendCredential(request_id, info);
215 EXPECT_TRUE(callback_succeeded());
216 EXPECT_FALSE(callback_errored());
219 } // namespace password_manager