Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: components/password_manager/content/renderer/credential_manager_client_unittest.cc

Issue 464883002: Credential Manager: Renderer-side implementation. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Feedback3. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 virtual ~CredentialManagerClientTest() {}
26
27 static void SetUpTestCase() { content::SetUpBlinkTestEnvironment(); }
28
29 static void TearDownTestCase() { content::TearDownBlinkTestEnvironment(); }
30
31 IPC::TestSink& sink() { return render_thread_.sink(); }
32
33 // The browser's response to any of the messages the client sends must contain
34 // a request ID so that the client knows which request is being serviced. This
35 // method grabs the ID from an outgoing |message_id| message, and sets the
36 // |request_id| param to its value. If no request ID can be found, the method
37 // returns false, and the |request_id| is set to -1.
38 //
39 // Clears any pending messages upon return.
40 bool ExtractRequestId(uint32 message_id, int& request_id) {
41 request_id = -1;
42 const IPC::Message* message = sink().GetFirstMessageMatching(message_id);
43 if (!message)
44 return false;
45
46 switch (message_id) {
47 case CredentialManagerHostMsg_NotifyFailedSignIn::ID: {
48 Tuple2<int, CredentialInfo> param;
49 CredentialManagerHostMsg_NotifyFailedSignIn::Read(message, &param);
50 request_id = param.a;
51 break;
52 }
53
54 case CredentialManagerHostMsg_NotifySignedIn::ID: {
55 Tuple2<int, CredentialInfo> param;
56 CredentialManagerHostMsg_NotifySignedIn::Read(message, &param);
57 request_id = param.a;
58 break;
59 }
60
61 case CredentialManagerHostMsg_NotifySignedOut::ID: {
62 Tuple1<int> param;
63 CredentialManagerHostMsg_NotifySignedOut::Read(message, &param);
64 request_id = param.a;
65 break;
66 }
67
68 case CredentialManagerHostMsg_RequestCredential::ID: {
69 Tuple3<int, bool, std::vector<GURL> > param;
70 CredentialManagerHostMsg_RequestCredential::Read(message, &param);
71 request_id = param.a;
72 break;
73 }
74
75 default:
76 break;
77 }
78 sink().ClearMessages();
79 return request_id != -1;
80 }
81
82 bool callback_errored() const { return callback_errored_; }
83 void set_callback_errored(bool state) { callback_errored_ = state; }
84 bool callback_succeeded() const { return callback_succeeded_; }
85 void set_callback_succeeded(bool state) { callback_succeeded_ = state; }
86
87 protected:
88 content::MockRenderThread render_thread_;
89 TestCredentialManagerClient client_;
90
91 // True if a message's callback's 'onSuccess'/'onError' methods were called,
92 // false otherwise. We put these on the test object rather than on the
93 // Test*Callbacks objects because ownership of those objects passes into the
94 // client, which destroys the callbacks after calling them to resolve the
95 // pending Blink-side Promise.
96 bool callback_errored_;
97 bool callback_succeeded_;
98 };
99
100 class TestNotificationCallbacks
101 : public blink::WebCredentialManagerClient::NotificationCallbacks {
102 public:
103 explicit TestNotificationCallbacks(CredentialManagerClientTest* test)
104 : test_(test) {
105 }
106
107 virtual ~TestNotificationCallbacks() {}
108
109 virtual void onSuccess() OVERRIDE { test_->set_callback_succeeded(true); }
110
111 virtual void onError(blink::WebCredentialManagerError* reason) OVERRIDE {
112 test_->set_callback_errored(true);
113 }
114
115 private:
116 CredentialManagerClientTest* test_;
117 };
118
119 class TestRequestCallbacks
120 : public blink::WebCredentialManagerClient::RequestCallbacks {
121 public:
122 explicit TestRequestCallbacks(CredentialManagerClientTest* test)
123 : test_(test) {
124 }
125
126 virtual ~TestRequestCallbacks() {}
127
128 virtual void onSuccess(blink::WebCredential*) OVERRIDE {
129 test_->set_callback_succeeded(true);
130 }
131
132 virtual void onError(blink::WebCredentialManagerError* reason) OVERRIDE {
133 test_->set_callback_errored(true);
134 }
135
136 private:
137 CredentialManagerClientTest* test_;
138 };
139
140 } // namespace
141
142 TEST_F(CredentialManagerClientTest, SendNotifyFailedSignIn) {
143 int request_id;
144 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID,
145 request_id));
146
147 blink::WebCredential credential(
148 "id", "user", GURL("https://example.com/img.png"));
149 scoped_ptr<TestNotificationCallbacks> callbacks(
150 new TestNotificationCallbacks(this));
151 client_.dispatchFailedSignIn(credential, callbacks.release());
152
153 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID,
154 request_id));
155
156 client_.OnAcknowledgeFailedSignIn(request_id);
157 EXPECT_TRUE(callback_succeeded());
158 EXPECT_FALSE(callback_errored());
159 }
160
161 TEST_F(CredentialManagerClientTest, SendNotifySignedIn) {
162 int request_id;
163 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID,
164 request_id));
165
166 blink::WebCredential credential(
167 "id", "user", GURL("https://example.com/img.png"));
168 scoped_ptr<TestNotificationCallbacks> callbacks(
169 new TestNotificationCallbacks(this));
170 client_.dispatchSignedIn(credential, callbacks.release());
171
172 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID,
173 request_id));
174
175 client_.OnAcknowledgeSignedIn(request_id);
176 EXPECT_TRUE(callback_succeeded());
177 EXPECT_FALSE(callback_errored());
178 }
179
180 TEST_F(CredentialManagerClientTest, SendNotifySignedOut) {
181 int request_id;
182 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID,
183 request_id));
184
185 scoped_ptr<TestNotificationCallbacks> callbacks(
186 new TestNotificationCallbacks(this));
187 client_.dispatchSignedOut(callbacks.release());
188
189 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID,
190 request_id));
191
192 client_.OnAcknowledgeSignedOut(request_id);
193 EXPECT_TRUE(callback_succeeded());
194 EXPECT_FALSE(callback_errored());
195 }
196
197 TEST_F(CredentialManagerClientTest, SendRequestCredential) {
198 int request_id;
199 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
200 request_id));
201
202 scoped_ptr<TestRequestCallbacks> callbacks(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());
213 }
214
215 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698