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(
55 base::android::ScopedJavaLocalRef
<jobject
> j_request
,
56 AwPermissionRequest
* request
) override
{
59 java_request_
= j_request
;
60 requested_permission_
=
61 Permission(request
->GetOrigin(), request
->GetResources());
64 void OnPermissionRequestCanceled(AwPermissionRequest
* request
) override
{
65 canceled_permission_
=
66 Permission(request
->GetOrigin(), request
->GetResources());
69 AwPermissionRequest
* request() {
73 const Permission
& requested_permission() {
74 return requested_permission_
;
77 const Permission
& canceled_permission() {
78 return canceled_permission_
;
82 request_
->OnAccept(NULL
, NULL
, true);
83 request_
->Destroy(NULL
, NULL
);
88 request_
->OnAccept(NULL
, NULL
, false);
89 request_
->Destroy(NULL
, NULL
);
95 requested_permission_
= Permission();
96 canceled_permission_
= Permission();
100 base::android::ScopedJavaLocalRef
<jobject
> java_request_
;
101 AwPermissionRequest
* request_
;
102 Permission requested_permission_
;
103 Permission canceled_permission_
;
106 class TestPermissionRequestHandler
: public PermissionRequestHandler
{
108 TestPermissionRequestHandler(PermissionRequestHandlerClient
* client
)
109 : PermissionRequestHandler(client
, NULL
) {
112 const std::vector
<base::WeakPtr
<AwPermissionRequest
> > requests() {
116 void PruneRequests() {
117 return PermissionRequestHandler::PruneRequests();
121 class PermissionRequestHandlerTest
: public testing::Test
{
123 PermissionRequestHandlerTest()
124 : handler_(&client_
),
127 void NotifyRequestResult(bool allowed
) {
132 void SetUp() override
{
133 testing::Test::SetUp();
134 origin_
= GURL("http://www.google.com");
136 AwPermissionRequest::VideoCapture
| AwPermissionRequest::AudioCapture
;
138 new TestAwPermissionRequestDelegate(
139 origin_
, resources_
, base::Bind(
140 &PermissionRequestHandlerTest::NotifyRequestResult
,
141 base::Unretained(this))));
144 const GURL
& origin() {
152 scoped_ptr
<AwPermissionRequestDelegate
> delegate() {
153 return delegate_
.Pass();
156 TestPermissionRequestHandler
* handler() {
160 TestPermissionRequestHandlerClient
* client() {
171 scoped_ptr
<AwPermissionRequestDelegate
> delegate_
;
172 TestPermissionRequestHandlerClient client_
;
173 TestPermissionRequestHandler handler_
;
177 TEST_F(PermissionRequestHandlerTest
, TestPermissionGranted
) {
178 handler()->SendRequest(delegate().Pass());
179 // Verify Handler store the request correctly.
180 ASSERT_EQ(1u, handler()->requests().size());
181 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
182 EXPECT_EQ(resources(), handler()->requests()[0]->GetResources());
184 // Verify client's onPermissionRequest was called
185 EXPECT_EQ(origin(), client()->request()->GetOrigin());
186 EXPECT_EQ(resources(), client()->request()->GetResources());
188 // Simulate the grant request.
190 // Verify the request is notified as granted
191 EXPECT_TRUE(allowed());
192 handler()->PruneRequests();
193 // Verify the weak reference in handler was removed.
194 EXPECT_TRUE(handler()->requests().empty());
197 TEST_F(PermissionRequestHandlerTest
, TestPermissionDenied
) {
198 handler()->SendRequest(delegate().Pass());
199 // Verify Handler store the request correctly.
200 ASSERT_EQ(1u, handler()->requests().size());
201 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
202 EXPECT_EQ(resources(), handler()->requests()[0]->GetResources());
204 // Verify client's onPermissionRequest was called
205 EXPECT_EQ(origin(), client()->request()->GetOrigin());
206 EXPECT_EQ(resources(), client()->request()->GetResources());
208 // Simulate the deny request.
210 // Verify the request is notified as granted
211 EXPECT_FALSE(allowed());
212 handler()->PruneRequests();
213 // Verify the weak reference in handler was removed.
214 EXPECT_TRUE(handler()->requests().empty());
217 TEST_F(PermissionRequestHandlerTest
, TestMultiplePermissionRequest
) {
218 GURL origin1
= GURL("http://a.google.com");
219 int64 resources1
= AwPermissionRequest::Geolocation
;
221 scoped_ptr
<AwPermissionRequestDelegate
> delegate1
;
222 delegate1
.reset(new TestAwPermissionRequestDelegate(
224 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
225 base::Unretained(this))));
228 handler()->SendRequest(delegate().Pass());
229 // Verify Handler store the request correctly.
230 ASSERT_EQ(1u, handler()->requests().size());
231 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
232 EXPECT_EQ(resources(), handler()->requests()[0]->GetResources());
233 // Verify client's onPermissionRequest was called
234 EXPECT_EQ(origin(), client()->request()->GetOrigin());
235 EXPECT_EQ(resources(), client()->request()->GetResources());
238 handler()->SendRequest(delegate1
.Pass());
239 // Verify Handler store the request correctly.
240 ASSERT_EQ(2u, handler()->requests().size());
241 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
242 EXPECT_EQ(resources(), handler()->requests()[0]->GetResources());
243 EXPECT_EQ(origin1
, handler()->requests()[1]->GetOrigin());
244 EXPECT_EQ(resources1
, handler()->requests()[1]->GetResources());
245 // Verify client's onPermissionRequest was called
246 EXPECT_EQ(origin1
, client()->request()->GetOrigin());
247 EXPECT_EQ(resources1
, client()->request()->GetResources());
249 // Send 3rd request which has same origin and resources as first one.
250 delegate1
.reset(new TestAwPermissionRequestDelegate(origin(), resources(),
251 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
252 base::Unretained(this))));
253 handler()->SendRequest(delegate1
.Pass());
254 // Verify Handler store the request correctly.
255 ASSERT_EQ(3u, handler()->requests().size());
256 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
257 EXPECT_EQ(resources(), handler()->requests()[0]->GetResources());
258 EXPECT_EQ(origin1
, handler()->requests()[1]->GetOrigin());
259 EXPECT_EQ(resources1
, handler()->requests()[1]->GetResources());
260 EXPECT_EQ(origin(), handler()->requests()[2]->GetOrigin());
261 EXPECT_EQ(resources(), handler()->requests()[2]->GetResources());
262 // Verify client's onPermissionRequest was called
263 EXPECT_EQ(origin(), client()->request()->GetOrigin());
264 EXPECT_EQ(resources(), client()->request()->GetResources());
266 // Cancel the request.
267 handler()->CancelRequest(origin(), resources());
268 // Verify client's OnPermissionRequestCancled() was called.
269 EXPECT_EQ(origin(), client()->canceled_permission().origin
);
270 EXPECT_EQ(resources(), client()->canceled_permission().resources
);
271 // Verify Handler store the request correctly, the 1st and 3rd were removed.
272 handler()->PruneRequests();
273 ASSERT_EQ(1u, handler()->requests().size());
274 EXPECT_EQ(origin1
, handler()->requests()[0]->GetOrigin());
275 EXPECT_EQ(resources1
, handler()->requests()[0]->GetResources());
278 TEST_F(PermissionRequestHandlerTest
, TestPreauthorizePermission
) {
279 handler()->PreauthorizePermission(origin(), resources());
281 // Permission should granted without asking PermissionRequestHandlerClient.
282 handler()->SendRequest(delegate().Pass());
283 EXPECT_TRUE(allowed());
284 EXPECT_EQ(NULL
, client()->request());
286 // Only ask one preauthorized resource, permission should granted
287 // without asking PermissionRequestHandlerClient.
288 scoped_ptr
<AwPermissionRequestDelegate
> delegate
;
289 delegate
.reset(new TestAwPermissionRequestDelegate(
290 origin(), AwPermissionRequest::AudioCapture
,
291 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
292 base::Unretained(this))));
294 handler()->SendRequest(delegate
.Pass());
295 EXPECT_TRUE(allowed());
296 EXPECT_EQ(NULL
, client()->request());
299 TEST_F(PermissionRequestHandlerTest
, TestOriginNotPreauthorized
) {
300 handler()->PreauthorizePermission(origin(), resources());
302 // Ask the origin which wasn't preauthorized.
303 GURL
origin ("http://a.google.com/a/b");
304 scoped_ptr
<AwPermissionRequestDelegate
> delegate
;
305 int64 requested_resources
= AwPermissionRequest::AudioCapture
;
306 delegate
.reset(new TestAwPermissionRequestDelegate(
307 origin
, requested_resources
,
308 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
309 base::Unretained(this))));
310 handler()->SendRequest(delegate
.Pass());
311 EXPECT_EQ(origin
, handler()->requests()[0]->GetOrigin());
312 EXPECT_EQ(requested_resources
, handler()->requests()[0]->GetResources());
314 EXPECT_TRUE(allowed());
317 TEST_F(PermissionRequestHandlerTest
, TestResourcesNotPreauthorized
) {
318 handler()->PreauthorizePermission(origin(), resources());
320 // Ask the resources which weren't preauthorized.
321 scoped_ptr
<AwPermissionRequestDelegate
> delegate
;
322 int64 requested_resources
= AwPermissionRequest::AudioCapture
323 | AwPermissionRequest::Geolocation
;
324 delegate
.reset(new TestAwPermissionRequestDelegate(
325 origin(), requested_resources
,
326 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
327 base::Unretained(this))));
329 handler()->SendRequest(delegate
.Pass());
330 EXPECT_EQ(origin(), handler()->requests()[0]->GetOrigin());
331 EXPECT_EQ(requested_resources
, handler()->requests()[0]->GetResources());
333 EXPECT_FALSE(allowed());
336 TEST_F(PermissionRequestHandlerTest
, TestPreauthorizeMultiplePermission
) {
337 handler()->PreauthorizePermission(origin(), resources());
338 // Preauthorize another permission.
339 GURL
origin ("http://a.google.com/a/b");
340 handler()->PreauthorizePermission(origin
, AwPermissionRequest::Geolocation
);
341 GURL
origin_hostname ("http://a.google.com/");
342 scoped_ptr
<AwPermissionRequestDelegate
> delegate
;
343 delegate
.reset(new TestAwPermissionRequestDelegate(
344 origin_hostname
, AwPermissionRequest::Geolocation
,
345 base::Bind(&PermissionRequestHandlerTest::NotifyRequestResult
,
346 base::Unretained(this))));
347 handler()->SendRequest(delegate
.Pass());
348 EXPECT_TRUE(allowed());
349 EXPECT_EQ(NULL
, client()->request());