Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / password_manager / content / renderer / credential_manager_client_browsertest.cc
blob70802c792416bca38f178c11a88d474ff7cde481
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/WebPasswordCredential.h"
15 namespace password_manager {
17 namespace {
19 class CredentialManagerClientTest : public content::RenderViewTest {
20 public:
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::WebPasswordCredential("", "", "", GURL()));
28 client_.reset(new CredentialManagerClient(view_));
31 void TearDown() override {
32 credential_.reset();
33 client_.reset();
34 content::RenderViewTest::TearDown();
37 IPC::TestSink& sink() { return render_thread_->sink(); }
39 blink::WebCredential* credential() { return credential_.get(); }
41 // The browser's response to any of the messages the client sends must contain
42 // a request ID so that the client knows which request is being serviced. This
43 // method grabs the ID from an outgoing |message_id| message, and sets the
44 // |request_id| param to its value. If no request ID can be found, the method
45 // returns false, and the |request_id| is set to -1.
47 // Clears any pending messages upon return.
48 bool ExtractRequestId(uint32 message_id, int& request_id) {
49 request_id = -1;
50 const IPC::Message* message = sink().GetFirstMessageMatching(message_id);
51 if (!message)
52 return false;
54 switch (message_id) {
55 case CredentialManagerHostMsg_Store::ID: {
56 base::Tuple<int, CredentialInfo> param;
57 CredentialManagerHostMsg_Store::Read(message, &param);
58 request_id = base::get<0>(param);
59 break;
62 case CredentialManagerHostMsg_RequireUserMediation::ID: {
63 base::Tuple<int> param;
64 CredentialManagerHostMsg_RequireUserMediation::Read(message, &param);
65 request_id = base::get<0>(param);
66 break;
69 case CredentialManagerHostMsg_RequestCredential::ID: {
70 base::Tuple<int, bool, std::vector<GURL>> param;
71 CredentialManagerHostMsg_RequestCredential::Read(message, &param);
72 request_id = base::get<0>(param);
73 break;
76 default:
77 break;
79 sink().ClearMessages();
80 return request_id != -1;
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; }
88 protected:
89 scoped_ptr<CredentialManagerClient> client_;
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_;
99 scoped_ptr<blink::WebPasswordCredential> credential_;
102 class TestNotificationCallbacks
103 : public blink::WebCredentialManagerClient::NotificationCallbacks {
104 public:
105 explicit TestNotificationCallbacks(CredentialManagerClientTest* test)
106 : test_(test) {}
108 virtual ~TestNotificationCallbacks() {}
110 virtual void onSuccess() { test_->set_callback_succeeded(true); }
112 virtual void onError(blink::WebCredentialManagerError* reason) {
113 test_->set_callback_errored(true);
116 private:
117 CredentialManagerClientTest* test_;
120 class TestRequestCallbacks
121 : public blink::WebCredentialManagerClient::RequestCallbacks {
122 public:
123 explicit TestRequestCallbacks(CredentialManagerClientTest* test)
124 : test_(test) {}
126 virtual ~TestRequestCallbacks() {}
128 virtual void onSuccess(blink::WebCredential*) {
129 test_->set_callback_succeeded(true);
132 virtual void onError(blink::WebCredentialManagerError* reason) {
133 test_->set_callback_errored(true);
136 private:
137 CredentialManagerClientTest* test_;
140 } // namespace
142 TEST_F(CredentialManagerClientTest, SendStore) {
143 int request_id;
144 EXPECT_FALSE(
145 ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id));
147 scoped_ptr<TestNotificationCallbacks> callbacks(
148 new TestNotificationCallbacks(this));
149 client_->dispatchStore(*credential(), callbacks.release());
151 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_Store::ID, request_id));
153 client_->OnAcknowledgeStore(request_id);
154 EXPECT_TRUE(callback_succeeded());
155 EXPECT_FALSE(callback_errored());
158 TEST_F(CredentialManagerClientTest, SendRequestUserMediation) {
159 int request_id;
160 EXPECT_FALSE(ExtractRequestId(
161 CredentialManagerHostMsg_RequireUserMediation::ID, request_id));
163 scoped_ptr<TestNotificationCallbacks> callbacks(
164 new TestNotificationCallbacks(this));
165 client_->dispatchRequireUserMediation(callbacks.release());
167 EXPECT_TRUE(ExtractRequestId(
168 CredentialManagerHostMsg_RequireUserMediation::ID, request_id));
170 client_->OnAcknowledgeRequireUserMediation(request_id);
171 EXPECT_TRUE(callback_succeeded());
172 EXPECT_FALSE(callback_errored());
175 TEST_F(CredentialManagerClientTest, SendRequestCredential) {
176 int request_id;
177 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
178 request_id));
180 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this));
181 std::vector<GURL> federations;
182 client_->dispatchGet(false, federations, callbacks.release());
184 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
185 request_id));
187 CredentialInfo info;
188 info.type = CredentialType::CREDENTIAL_TYPE_PASSWORD;
189 client_->OnSendCredential(request_id, info);
190 EXPECT_TRUE(callback_succeeded());
191 EXPECT_FALSE(callback_errored());
194 TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) {
195 int request_id;
196 EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
197 request_id));
199 scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this));
200 std::vector<GURL> federations;
201 client_->dispatchGet(false, federations, callbacks.release());
203 EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
204 request_id));
206 CredentialInfo info; // Send an empty credential in response.
207 client_->OnSendCredential(request_id, info);
208 EXPECT_TRUE(callback_succeeded());
209 EXPECT_FALSE(callback_errored());
212 } // namespace password_manager