Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / password_manager / content / renderer / credential_manager_client_browsertest.cc
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 "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/WebLocalCredential.h"
14
15 namespace password_manager {
16
17 namespace {
18
19 class CredentialManagerClientTest : public content::RenderViewTest {
20  public:
21   CredentialManagerClientTest()
22       : callback_errored_(false), callback_succeeded_(false) {}
23   ~CredentialManagerClientTest() override {}
24
25   void SetUp() override {
26     content::RenderViewTest::SetUp();
27     credential_.reset(new blink::WebLocalCredential("", "", GURL(), ""));
28     client_.reset(new CredentialManagerClient(view_));
29   }
30
31   void TearDown() override {
32     credential_.reset();
33     content::RenderViewTest::TearDown();
34   }
35
36   IPC::TestSink& sink() { return render_thread_->sink(); }
37
38   blink::WebCredential* credential() { return credential_.get(); }
39
40   // The browser's response to any of the messages the client sends must contain
41   // a request ID so that the client knows which request is being serviced. This
42   // method grabs the ID from an outgoing |message_id| message, and sets the
43   // |request_id| param to its value. If no request ID can be found, the method
44   // returns false, and the |request_id| is set to -1.
45   //
46   // Clears any pending messages upon return.
47   bool ExtractRequestId(uint32 message_id, int& request_id) {
48     request_id = -1;
49     const IPC::Message* message = sink().GetFirstMessageMatching(message_id);
50     if (!message)
51       return false;
52
53     switch (message_id) {
54       case CredentialManagerHostMsg_NotifyFailedSignIn::ID: {
55         Tuple2<int, CredentialInfo> param;
56         CredentialManagerHostMsg_NotifyFailedSignIn::Read(message, &param);
57         request_id = param.a;
58         break;
59       }
60
61       case CredentialManagerHostMsg_NotifySignedIn::ID: {
62         Tuple2<int, CredentialInfo> param;
63         CredentialManagerHostMsg_NotifySignedIn::Read(message, &param);
64         request_id = param.a;
65         break;
66       }
67
68       case CredentialManagerHostMsg_NotifySignedOut::ID: {
69         Tuple1<int> param;
70         CredentialManagerHostMsg_NotifySignedOut::Read(message, &param);
71         request_id = param.a;
72         break;
73       }
74
75       case CredentialManagerHostMsg_RequestCredential::ID: {
76         Tuple3<int, bool, std::vector<GURL> > param;
77         CredentialManagerHostMsg_RequestCredential::Read(message, &param);
78         request_id = param.a;
79         break;
80       }
81
82       default:
83         break;
84     }
85     sink().ClearMessages();
86     return request_id != -1;
87   }
88
89   bool callback_errored() const { return callback_errored_; }
90   void set_callback_errored(bool state) { callback_errored_ = state; }
91   bool callback_succeeded() const { return callback_succeeded_; }
92   void set_callback_succeeded(bool state) { callback_succeeded_ = state; }
93
94  protected:
95   scoped_ptr<CredentialManagerClient> 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::WebLocalCredential> credential_;
106 };
107
108 class TestNotificationCallbacks
109     : public blink::WebCredentialManagerClient::NotificationCallbacks {
110  public:
111   explicit TestNotificationCallbacks(CredentialManagerClientTest* test)
112       : test_(test) {}
113
114   virtual ~TestNotificationCallbacks() {}
115
116   virtual void onSuccess() { test_->set_callback_succeeded(true); }
117
118   virtual void onError(blink::WebCredentialManagerError* reason) {
119     test_->set_callback_errored(true);
120   }
121
122  private:
123   CredentialManagerClientTest* test_;
124 };
125
126 class TestRequestCallbacks
127     : public blink::WebCredentialManagerClient::RequestCallbacks {
128  public:
129   explicit TestRequestCallbacks(CredentialManagerClientTest* test)
130       : test_(test) {}
131
132   virtual ~TestRequestCallbacks() {}
133
134   virtual void onSuccess(blink::WebCredential*) {
135     test_->set_callback_succeeded(true);
136   }
137
138   virtual void onError(blink::WebCredentialManagerError* reason) {
139     test_->set_callback_errored(true);
140   }
141
142  private:
143   CredentialManagerClientTest* test_;
144 };
145
146 }  // namespace
147
148 TEST_F(CredentialManagerClientTest, SendNotifyFailedSignIn) {
149   int request_id;
150   EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID,
151                                 request_id));
152
153   scoped_ptr<TestNotificationCallbacks> callbacks(
154       new TestNotificationCallbacks(this));
155   client_->dispatchFailedSignIn(*credential(), callbacks.release());
156
157   EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifyFailedSignIn::ID,
158                                request_id));
159
160   client_->OnAcknowledgeFailedSignIn(request_id);
161   EXPECT_TRUE(callback_succeeded());
162   EXPECT_FALSE(callback_errored());
163 }
164
165 TEST_F(CredentialManagerClientTest, SendNotifySignedIn) {
166   int request_id;
167   EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID,
168                                 request_id));
169
170   scoped_ptr<TestNotificationCallbacks> callbacks(
171       new TestNotificationCallbacks(this));
172   client_->dispatchSignedIn(*credential(), callbacks.release());
173
174   EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedIn::ID,
175                                request_id));
176
177   client_->OnAcknowledgeSignedIn(request_id);
178   EXPECT_TRUE(callback_succeeded());
179   EXPECT_FALSE(callback_errored());
180 }
181
182 TEST_F(CredentialManagerClientTest, SendNotifySignedOut) {
183   int request_id;
184   EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID,
185                                 request_id));
186
187   scoped_ptr<TestNotificationCallbacks> callbacks(
188       new TestNotificationCallbacks(this));
189   client_->dispatchSignedOut(callbacks.release());
190
191   EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_NotifySignedOut::ID,
192                                request_id));
193
194   client_->OnAcknowledgeSignedOut(request_id);
195   EXPECT_TRUE(callback_succeeded());
196   EXPECT_FALSE(callback_errored());
197 }
198
199 TEST_F(CredentialManagerClientTest, SendRequestCredential) {
200   int request_id;
201   EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
202                                 request_id));
203
204   scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this));
205   std::vector<GURL> federations;
206   client_->dispatchRequest(false, federations, callbacks.release());
207
208   EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
209                                request_id));
210
211   CredentialInfo info;
212   info.type = CREDENTIAL_TYPE_LOCAL;
213   client_->OnSendCredential(request_id, info);
214   EXPECT_TRUE(callback_succeeded());
215   EXPECT_FALSE(callback_errored());
216 }
217
218 TEST_F(CredentialManagerClientTest, SendRequestCredentialEmpty) {
219   int request_id;
220   EXPECT_FALSE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
221                                 request_id));
222
223   scoped_ptr<TestRequestCallbacks> callbacks(new TestRequestCallbacks(this));
224   std::vector<GURL> federations;
225   client_->dispatchRequest(false, federations, callbacks.release());
226
227   EXPECT_TRUE(ExtractRequestId(CredentialManagerHostMsg_RequestCredential::ID,
228                                request_id));
229
230   CredentialInfo info; // Send an empty credential in response.
231   client_->OnSendCredential(request_id, info);
232   EXPECT_TRUE(callback_succeeded());
233   EXPECT_FALSE(callback_errored());
234 }
235
236 }  // namespace password_manager