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 class CredentialManagerClientTest : public testing::Test { | |
19 public: | |
20 CredentialManagerClientTest() { } | |
21 virtual ~CredentialManagerClientTest() { } | |
22 | |
23 virtual void SetUp() { | |
24 callback_errored_ = false; | |
25 callback_succeeded_ = false; | |
Ilya Sherman
2014/08/21 17:58:15
nit: Perhaps initialize these in the constructor's
Mike West
2014/08/22 09:03:15
Done.
| |
26 } | |
27 | |
28 static void SetUpTestCase() { content::SetUpBlinkTestEnvironment(); } | |
29 | |
30 static void TearDownTestCase() { content::TearDownBlinkTestEnvironment(); } | |
31 | |
32 IPC::TestSink& sink() { return render_thread_.sink(); } | |
33 | |
34 // The browser's response to any of the messages the client sends must contain | |
35 // a request ID so that the client knows which request is being serviced. This | |
36 // method grabs the ID from an outgoing |message_id| message, and sets the | |
37 // |request_id| param to its value. If no request ID can be found, the method | |
38 // returns false, and the |request_id| is set to -1. | |
39 // | |
40 // Clears any pending messages upon return. | |
41 bool ExtractRequestId(uint32 message_id, int& request_id) { | |
42 request_id = -1; | |
43 const IPC::Message* message = sink().GetFirstMessageMatching(message_id); | |
44 if (!message) | |
45 return false; | |
46 | |
47 switch (message_id) { | |
48 case CredentialManagerHostMsg_NotifyFailedSignIn::ID: { | |
Ilya Sherman
2014/08/21 17:58:15
nit: The case stmts should be indented by two spac
Mike West
2014/08/22 09:03:15
Done.
| |
49 Tuple2<int, CredentialInfo> param; | |
50 CredentialManagerHostMsg_NotifyFailedSignIn::Read(message, ¶m); | |
51 request_id = param.a; | |
52 break; | |
53 } | |
54 | |
55 case CredentialManagerHostMsg_NotifySignedIn::ID: { | |
56 Tuple2<int, CredentialInfo> param; | |
57 CredentialManagerHostMsg_NotifySignedIn::Read(message, ¶m); | |
58 request_id = param.a; | |
59 break; | |
60 } | |
61 | |
62 case CredentialManagerHostMsg_NotifySignedOut::ID: { | |
63 Tuple1<int> param; | |
64 CredentialManagerHostMsg_NotifySignedOut::Read(message, ¶m); | |
65 request_id = param.a; | |
66 break; | |
67 } | |
68 | |
69 case CredentialManagerHostMsg_RequestCredential::ID: { | |
70 Tuple3<int, bool, std::vector<GURL> > param; | |
71 CredentialManagerHostMsg_RequestCredential::Read(message, ¶m); | |
72 request_id = param.a; | |
73 break; | |
74 } | |
75 | |
76 default: | |
77 break; | |
78 } | |
79 sink().ClearMessages(); | |
80 return request_id != -1; | |
81 } | |
82 | |
83 bool callback_errored() const { return callback_errored_; } | |
84 void set_callback_errored(bool state) { callback_errored_ = state; } | |
85 bool callback_succeeded() const { return callback_succeeded_; } | |
86 void set_callback_succeeded(bool state) { callback_succeeded_ = state; } | |
87 | |
88 protected: | |
89 content::MockRenderThread render_thread_; | |
90 TestCredentialManagerClient client_; | |
91 | |
92 bool callback_succeeded_; | |
93 bool callback_errored_; | |
Ilya Sherman
2014/08/21 17:58:15
nit: Docs, plz
Mike West
2014/08/22 09:03:15
Done.
| |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(CredentialManagerClientTest); | |
Ilya Sherman
2014/08/21 17:58:15
nit: DISALLOW_COPY_AND_ASSIGN is often omitted for
Mike West
2014/08/22 09:03:15
Done.
| |
96 }; | |
97 | |
98 class TestNotificationCallbacks | |
99 : public blink::WebCredentialManagerClient::NotificationCallbacks { | |
100 public: | |
101 TestNotificationCallbacks(CredentialManagerClientTest* test) | |
Ilya Sherman
2014/08/21 17:58:15
nit: explicit
Mike West
2014/08/22 09:03:15
Done.
| |
102 : test_(test) { } | |
103 | |
104 virtual ~TestNotificationCallbacks() { } | |
105 | |
106 virtual void onSuccess() OVERRIDE | |
107 { | |
108 test_->set_callback_succeeded(true); | |
109 } | |
110 | |
111 virtual void onError(blink::WebCredentialManagerError* reason) OVERRIDE | |
112 { | |
113 test_->set_callback_errored(true); | |
114 } | |
115 | |
116 private: | |
117 CredentialManagerClientTest* test_; | |
118 }; | |
119 | |
120 class TestRequestCallbacks | |
121 : public blink::WebCredentialManagerClient::RequestCallbacks { | |
122 public: | |
123 TestRequestCallbacks(CredentialManagerClientTest* test) : test_(test) { } | |
Ilya Sherman
2014/08/21 17:58:15
nit: explicit
Mike West
2014/08/22 09:03:15
Done.
| |
124 | |
125 virtual ~TestRequestCallbacks() { } | |
126 | |
127 virtual void onSuccess(blink::WebCredential*) OVERRIDE | |
128 { | |
129 test_->set_callback_succeeded(true); | |
130 } | |
131 | |
132 virtual void onError(blink::WebCredentialManagerError* reason) OVERRIDE | |
133 { | |
134 test_->set_callback_errored(true); | |
135 } | |
136 | |
137 private: | |
138 CredentialManagerClientTest* test_; | |
139 }; | |
Ilya Sherman
2014/08/21 17:58:15
nit: Please tuck these into an anonymous namespace
Mike West
2014/08/22 09:03:15
Done.
| |
140 | |
141 TEST_F(CredentialManagerClientTest, SendNotifyFailedSignIn) { | |
142 int request_id; | |
143 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID, | |
144 request_id)); | |
145 | |
146 blink::WebCredential credential( | |
147 "id", "user", GURL("https://example.com/img.png")); | |
148 scoped_ptr<TestNotificationCallbacks> callbacks( | |
149 new TestNotificationCallbacks(this)); | |
150 client_.dispatchFailedSignIn(credential, callbacks.release()); | |
Ilya Sherman
2014/08/21 17:58:15
Oof, I'm just noticing that the Blink API has the
Mike West
2014/08/22 09:03:15
For better or worse, this is the WebCallbacks inte
| |
151 | |
152 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID, | |
153 request_id)); | |
154 | |
155 client_.OnAcknowledgeFailedSignIn(request_id); | |
156 EXPECT_TRUE(callback_succeeded()); | |
157 EXPECT_FALSE(callback_errored()); | |
158 } | |
159 | |
160 TEST_F(CredentialManagerClientTest, SendNotifySignedIn) { | |
161 int request_id; | |
162 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID, | |
163 request_id)); | |
164 | |
165 blink::WebCredential credential( | |
166 "id", "user", GURL("https://example.com/img.png")); | |
167 scoped_ptr<TestNotificationCallbacks> callbacks( | |
168 new TestNotificationCallbacks(this)); | |
169 client_.dispatchSignedIn(credential, callbacks.release()); | |
170 | |
171 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID, | |
172 request_id)); | |
173 | |
174 client_.OnAcknowledgeSignedIn(request_id); | |
175 EXPECT_TRUE(callback_succeeded()); | |
176 EXPECT_FALSE(callback_errored()); | |
177 } | |
178 | |
179 TEST_F(CredentialManagerClientTest, SendNotifySignedOut) { | |
180 int request_id; | |
181 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID, | |
182 request_id)); | |
183 | |
184 scoped_ptr<TestNotificationCallbacks> callbacks( | |
185 new TestNotificationCallbacks(this)); | |
186 client_.dispatchSignedOut(callbacks.release()); | |
187 | |
188 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID, | |
189 request_id)); | |
190 | |
191 client_.OnAcknowledgeSignedOut(request_id); | |
192 EXPECT_TRUE(callback_succeeded()); | |
193 EXPECT_FALSE(callback_errored()); | |
194 } | |
195 | |
196 TEST_F(CredentialManagerClientTest, SendRequestCredential) { | |
197 int request_id; | |
198 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, | |
199 request_id)); | |
200 | |
201 scoped_ptr<TestRequestCallbacks> callbacks( | |
202 new TestRequestCallbacks(this)); | |
203 std::vector<GURL> federations; | |
204 client_.dispatchRequest(false, federations, callbacks.release()); | |
205 | |
206 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID, | |
207 request_id)); | |
208 | |
209 CredentialInfo info; | |
210 client_.OnSendCredential(request_id, info); | |
211 EXPECT_TRUE(callback_succeeded()); | |
212 EXPECT_FALSE(callback_errored()); | |
Ilya Sherman
2014/08/21 17:58:15
Can you also add test coverage for where callback_
| |
213 } | |
214 | |
215 } // namespace password_manager | |
OLD | NEW |