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 "android_webview/native/permission/aw_permission_request.h"
6 #include "android_webview/native/permission/aw_permission_request_delegate.h"
7 #include "android_webview/native/permission/permission_request_handler.h"
8 #include "android_webview/native/permission/permission_request_handler_client.h"
10 #include "base/callback.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace android_webview
{
15 class TestAwPermissionRequestDelegate
: public AwPermissionRequestDelegate
{
17 TestAwPermissionRequestDelegate(
18 const GURL
& origin
, int64 resources
, base::Callback
<void(bool)> callback
)
20 resources_(resources
),
21 callback_(callback
) {}
23 // Get the origin which initiated the permission request.
24 const GURL
& GetOrigin() override
{ return origin_
; }
26 // Get the resources the origin wanted to access.
27 int64
GetResources() override
{ return resources_
; }
29 // Notify the permission request is allowed or not.
30 void NotifyRequestResult(bool allowed
) override
{ callback_
.Run(allowed
); }
35 base::Callback
<void(bool)> callback_
;
38 class TestPermissionRequestHandlerClient
:
39 public PermissionRequestHandlerClient
{
44 Permission(const GURL
& origin
, int64 resources
)
46 resources(resources
) {}
51 TestPermissionRequestHandlerClient()
54 void OnPermissionRequest(AwPermissionRequest
* request
) override
{
56 requested_permission_
=
57 Permission(request
->GetOrigin(), request
->GetResources());
60 void OnPermissionRequestCanceled(AwPermissionRequest
* request
) override
{
61 canceled_permission_
=
62 Permission(request
->GetOrigin(), request
->GetResources());
65 AwPermissionRequest
* request() {
69 const Permission
& requested_permission() {
70 return requested_permission_
;
73 const Permission
& canceled_permission() {
74 return canceled_permission_
;
78 request_
->OnAccept(NULL
, NULL
, true);
83 request_
->OnAccept(NULL
, NULL
, false);
89 requested_permission_
= Permission();
90 canceled_permission_
= Permission();
94 AwPermissionRequest
* request_
;
95 Permission requested_permission_
;
96 Permission canceled_permission_
;
99 class TestPermissionRequestHandler
: public PermissionRequestHandler
{
101 TestPermissionRequestHandler(PermissionRequestHandlerClient
* client
)
102 : PermissionRequestHandler(client
, NULL
) {
105 const std::vector
<base::WeakPtr
<AwPermissionRequest
> > requests() {
109 void PruneRequests() {
110 return PermissionRequestHandler::PruneRequests();
114 class PermissionRequestHandlerTest
: public testing::Test
{
116 PermissionRequestHandlerTest()
117 : handler_(&client_
),
120 void NotifyRequestResult(bool allowed
) {
125 void SetUp() override
{
126 testing::Test::SetUp();
127 origin_
= GURL("http://www.google.com");
129 AwPermissionRequest::VideoCapture
| AwPermissionRequest::AudioCapture
;
131 new TestAwPermissionRequestDelegate(
132 origin_
, resources_
, base::Bind(
133 &PermissionRequestHandlerTest::NotifyRequestResult
,
134 base::Unretained(this))));
137 const GURL
& origin() {
145 scoped_ptr
<AwPermissionRequestDelegate
> delegate() {
146 return delegate_
.Pass();
149 TestPermissionRequestHandler
* handler() {
153 TestPermissionRequestHandlerClient
* client() {
164 scoped_ptr
<AwPermissionRequestDelegate
> delegate_
;
165 TestPermissionRequestHandlerClient client_
;
166 TestPermissionRequestHandler handler_
;
170 TEST_F(PermissionRequestHandlerTest
, TestPermissionGranted
) {
171 handler()->SendRequest(delegate().Pass());
172 // Verify Handler store the request correctly.
173 ASSERT_EQ(1u, handler()->requests().size());
174 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
175 EXPECT_EQ(resources(), handler()->requests()[0]->GetResources());
177 // Verify client's onPermissionRequest was called
178 EXPECT_EQ(origin(), client()->request()->GetOrigin());
179 EXPECT_EQ(resources(), client()->request()->GetResources());
181 // Simulate the grant request.
183 // Verify the request is notified as granted
184 EXPECT_TRUE(allowed());
185 handler()->PruneRequests();
186 // Verify the weak reference in handler was removed.
187 EXPECT_TRUE(handler()->requests().empty());
190 TEST_F(PermissionRequestHandlerTest
, TestPermissionDenied
) {
191 handler()->SendRequest(delegate().Pass());
192 // Verify Handler store the request correctly.
193 ASSERT_EQ(1u, handler()->requests().size());
194 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
195 EXPECT_EQ(resources(), handler()->requests()[0]->GetResources());
197 // Verify client's onPermissionRequest was called
198 EXPECT_EQ(origin(), client()->request()->GetOrigin());
199 EXPECT_EQ(resources(), client()->request()->GetResources());
201 // Simulate the deny request.
203 // Verify the request is notified as granted
204 EXPECT_FALSE(allowed());
205 handler()->PruneRequests();
206 // Verify the weak reference in handler was removed.
207 EXPECT_TRUE(handler()->requests().empty());
210 TEST_F(PermissionRequestHandlerTest
, TestMultiplePermissionRequest
) {
211 GURL origin1
= GURL("http://a.google.com");
212 int64 resources1
= AwPermissionRequest::Geolocation
;
214 scoped_ptr
<AwPermissionRequestDelegate
> delegate1
;
215 delegate1
.reset(new TestAwPermissionRequestDelegate(
217 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
218 base::Unretained(this))));
221 handler()->SendRequest(delegate().Pass());
222 // Verify Handler store the request correctly.
223 ASSERT_EQ(1u, handler()->requests().size());
224 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
225 EXPECT_EQ(resources(), handler()->requests()[0]->GetResources());
226 // Verify client's onPermissionRequest was called
227 EXPECT_EQ(origin(), client()->request()->GetOrigin());
228 EXPECT_EQ(resources(), client()->request()->GetResources());
231 handler()->SendRequest(delegate1
.Pass());
232 // Verify Handler store the request correctly.
233 ASSERT_EQ(2u, handler()->requests().size());
234 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
235 EXPECT_EQ(resources(), handler()->requests()[0]->GetResources());
236 EXPECT_EQ(origin1
, handler()->requests()[1]->GetOrigin());
237 EXPECT_EQ(resources1
, handler()->requests()[1]->GetResources());
238 // Verify client's onPermissionRequest was called
239 EXPECT_EQ(origin1
, client()->request()->GetOrigin());
240 EXPECT_EQ(resources1
, client()->request()->GetResources());
242 // Send 3rd request which has same origin and resources as first one.
243 delegate1
.reset(new TestAwPermissionRequestDelegate(origin(), resources(),
244 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
245 base::Unretained(this))));
246 handler()->SendRequest(delegate1
.Pass());
247 // Verify Handler store the request correctly.
248 ASSERT_EQ(3u, handler()->requests().size());
249 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
250 EXPECT_EQ(resources(), handler()->requests()[0]->GetResources());
251 EXPECT_EQ(origin1
, handler()->requests()[1]->GetOrigin());
252 EXPECT_EQ(resources1
, handler()->requests()[1]->GetResources());
253 EXPECT_EQ(origin(), handler()->requests()[2]->GetOrigin());
254 EXPECT_EQ(resources(), handler()->requests()[2]->GetResources());
255 // Verify client's onPermissionRequest was called
256 EXPECT_EQ(origin(), client()->request()->GetOrigin());
257 EXPECT_EQ(resources(), client()->request()->GetResources());
259 // Cancel the request.
260 handler()->CancelRequest(origin(), resources());
261 // Verify client's OnPermissionRequestCancled() was called.
262 EXPECT_EQ(origin(), client()->canceled_permission().origin
);
263 EXPECT_EQ(resources(), client()->canceled_permission().resources
);
264 // Verify Handler store the request correctly, the 1st and 3rd were removed.
265 handler()->PruneRequests();
266 ASSERT_EQ(1u, handler()->requests().size());
267 EXPECT_EQ(origin1
, handler()->requests()[0]->GetOrigin());
268 EXPECT_EQ(resources1
, handler()->requests()[0]->GetResources());
271 TEST_F(PermissionRequestHandlerTest
, TestPreauthorizePermission
) {
272 handler()->PreauthorizePermission(origin(), resources());
274 // Permission should granted without asking PermissionRequestHandlerClient.
275 handler()->SendRequest(delegate().Pass());
276 EXPECT_TRUE(allowed());
277 EXPECT_EQ(NULL
, client()->request());
279 // Only ask one preauthorized resource, permission should granted
280 // without asking PermissionRequestHandlerClient.
281 scoped_ptr
<AwPermissionRequestDelegate
> delegate
;
282 delegate
.reset(new TestAwPermissionRequestDelegate(
283 origin(), AwPermissionRequest::AudioCapture
,
284 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
285 base::Unretained(this))));
287 handler()->SendRequest(delegate
.Pass());
288 EXPECT_TRUE(allowed());
289 EXPECT_EQ(NULL
, client()->request());
292 TEST_F(PermissionRequestHandlerTest
, TestOriginNotPreauthorized
) {
293 handler()->PreauthorizePermission(origin(), resources());
295 // Ask the origin which wasn't preauthorized.
296 GURL
origin ("http://a.google.com/a/b");
297 scoped_ptr
<AwPermissionRequestDelegate
> delegate
;
298 int64 requested_resources
= AwPermissionRequest::AudioCapture
;
299 delegate
.reset(new TestAwPermissionRequestDelegate(
300 origin
, requested_resources
,
301 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
302 base::Unretained(this))));
303 handler()->SendRequest(delegate
.Pass());
304 EXPECT_EQ(origin
, handler()->requests()[0]->GetOrigin());
305 EXPECT_EQ(requested_resources
, handler()->requests()[0]->GetResources());
307 EXPECT_TRUE(allowed());
310 TEST_F(PermissionRequestHandlerTest
, TestResourcesNotPreauthorized
) {
311 handler()->PreauthorizePermission(origin(), resources());
313 // Ask the resources which weren't preauthorized.
314 scoped_ptr
<AwPermissionRequestDelegate
> delegate
;
315 int64 requested_resources
= AwPermissionRequest::AudioCapture
316 | AwPermissionRequest::Geolocation
;
317 delegate
.reset(new TestAwPermissionRequestDelegate(
318 origin(), requested_resources
,
319 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
320 base::Unretained(this))));
322 handler()->SendRequest(delegate
.Pass());
323 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
324 EXPECT_EQ(requested_resources
, handler()->requests()[0]->GetResources());
326 EXPECT_FALSE(allowed());
329 TEST_F(PermissionRequestHandlerTest
, TestPreauthorizeMultiplePermission
) {
330 handler()->PreauthorizePermission(origin(), resources());
331 // Preauthorize another permission.
332 GURL
origin ("http://a.google.com/a/b");
333 handler()->PreauthorizePermission(origin
, AwPermissionRequest::Geolocation
);
334 GURL
origin_hostname ("http://a.google.com/");
335 scoped_ptr
<AwPermissionRequestDelegate
> delegate
;
336 delegate
.reset(new TestAwPermissionRequestDelegate(
337 origin_hostname
, AwPermissionRequest::Geolocation
,
338 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
339 base::Unretained(this))));
340 handler()->SendRequest(delegate
.Pass());
341 EXPECT_TRUE(allowed());
342 EXPECT_EQ(NULL
, client()->request());