OLD | NEW |
| (Empty) |
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. | |
4 | |
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_c
lient.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" | |
15 | |
16 namespace password_manager { | |
17 | |
18 namespace { | |
19 | |
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)); | |
28 } | |
29 virtual ~CredentialManagerClientTest() {} | |
30 | |
31 static void SetUpTestCase() { content::SetUpBlinkTestEnvironment(); } | |
32 | |
33 static void TearDownTestCase() { content::TearDownBlinkTestEnvironment(); } | |
34 | |
35 IPC::TestSink& sink() { return render_thread_.sink(); } | |
36 | |
37 blink::WebCredential* credential() { return credential_.get(); } | |
38 | |
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. | |
44 // | |
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; | |
51 | |
52 switch (message_id) { | |
53 case CredentialManagerHostMsg_NotifyFailedSignIn::ID: { | |
54 Tuple2<int, CredentialInfo> param; | |
55 CredentialManagerHostMsg_NotifyFailedSignIn::Read(message, ¶m); | |
56 request_id = param.a; | |
57 break; | |
58 } | |
59 | |
60 case CredentialManagerHostMsg_NotifySignedIn::ID: { | |
61 Tuple2<int, CredentialInfo> param; | |
62 CredentialManagerHostMsg_NotifySignedIn::Read(message, ¶m); | |
63 request_id = param.a; | |
64 break; | |
65 } | |
66 | |
67 case CredentialManagerHostMsg_NotifySignedOut::ID: { | |
68 Tuple1<int> param; | |
69 CredentialManagerHostMsg_NotifySignedOut::Read(message, ¶m); | |
70 request_id = param.a; | |
71 break; | |
72 } | |
73 | |
74 case CredentialManagerHostMsg_RequestCredential::ID: { | |
75 Tuple3<int, bool, std::vector<GURL> > param; | |
76 CredentialManagerHostMsg_RequestCredential::Read(message, ¶m); | |
77 request_id = param.a; | |
78 break; | |
79 } | |
80 | |
81 default: | |
82 break; | |
83 } | |
84 sink().ClearMessages(); | |
85 return request_id != -1; | |
86 } | |
87 | |
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; } | |
92 | |
93 protected: | |
94 content::MockRenderThread render_thread_; | |
95 TestCredentialManagerClient client_; | |
96 | |
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_; | |
104 | |
105 scoped_ptr<blink::WebCredential> credential_; | |
106 }; | |
107 | |
108 class TestNotificationCallbacks | |
109 : public blink::WebCredentialManagerClient::NotificationCallbacks { | |
110 public: | |
111 explicit TestNotificationCallbacks(CredentialManagerClientTest* test) | |
112 : test_(test) { | |
113 } | |
114 | |
115 virtual ~TestNotificationCallbacks() {} | |
116 | |
117 virtual void onSuccess() OVERRIDE { test_->set_callback_succeeded(true); } | |
118 | |
119 virtual void onError(blink::WebCredentialManagerError* reason) OVERRIDE { | |
120 test_->set_callback_errored(true); | |
121 } | |
122 | |
123 private: | |
124 CredentialManagerClientTest* test_; | |
125 }; | |
126 | |
127 class TestRequestCallbacks | |
128 : public blink::WebCredentialManagerClient::RequestCallbacks { | |
129 public: | |
130 explicit TestRequestCallbacks(CredentialManagerClientTest* test) | |
131 : test_(test) { | |
132 } | |
133 | |
134 virtual ~TestRequestCallbacks() {} | |
135 | |
136 virtual void onSuccess(blink::WebCredential*) OVERRIDE { | |
137 test_->set_callback_succeeded(true); | |
138 } | |
139 | |
140 virtual void onError(blink::WebCredentialManagerError* reason) OVERRIDE { | |
141 test_->set_callback_errored(true); | |
142 } | |
143 | |
144 private: | |
145 CredentialManagerClientTest* test_; | |
146 }; | |
147 | |
148 } // namespace | |
149 | |
150 TEST_F(CredentialManagerClientTest, SendNotifyFailedSignIn) { | |
151 int request_id; | |
152 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID, | |
153 request_id)); | |
154 | |
155 scoped_ptr<TestNotificationCallbacks> callbacks( | |
156 new TestNotificationCallbacks(this)); | |
157 client_.dispatchFailedSignIn(*credential(), callbacks.release()); | |
158 | |
159 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID, | |
160 request_id)); | |
161 | |
162 client_.OnAcknowledgeFailedSignIn(request_id); | |
163 EXPECT_TRUE(callback_succeeded()); | |
164 EXPECT_FALSE(callback_errored()); | |
165 } | |
166 | |
167 TEST_F(CredentialManagerClientTest, SendNotifySignedIn) { | |
168 int request_id; | |
169 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID, | |
170 request_id)); | |
171 | |
172 scoped_ptr<TestNotificationCallbacks> callbacks( | |
173 new TestNotificationCallbacks(this)); | |
174 client_.dispatchSignedIn(*credential(), callbacks.release()); | |
175 | |
176 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID, | |
177 request_id)); | |
178 | |
179 client_.OnAcknowledgeSignedIn(request_id); | |
180 EXPECT_TRUE(callback_succeeded()); | |
181 EXPECT_FALSE(callback_errored()); | |
182 } | |
183 | |
184 TEST_F(CredentialManagerClientTest, SendNotifySignedOut) { | |
185 int request_id; | |
186 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID, | |
187 request_id)); | |
188 | |
189 scoped_ptr<TestNotificationCallbacks> callbacks( | |
190 new TestNotificationCallbacks(this)); | |
191 client_.dispatchSignedOut(callbacks.release()); | |
192 | |
193 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID, | |
194 request_id)); | |
195 | |
196 client_.OnAcknowledgeSignedOut(request_id); | |
197 EXPECT_TRUE(callback_succeeded()); | |
198 EXPECT_FALSE(callback_errored()); | |
199 } | |
200 | |
201 TEST_F(CredentialManagerClientTest, SendRequestCredential) { | |
202 int request_id; | |
203 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, | |
204 request_id)); | |
205 | |
206 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this)); | |
207 std::vector<GURL> federations; | |
208 client_.dispatchRequest(false, federations, callbacks.release()); | |
209 | |
210 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, | |
211 request_id)); | |
212 | |
213 CredentialInfo info; | |
214 client_.OnSendCredential(request_id, info); | |
215 EXPECT_TRUE(callback_succeeded()); | |
216 EXPECT_FALSE(callback_errored()); | |
217 } | |
218 | |
219 } // namespace password_manager | |
OLD | NEW |