| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * Copyright 2017 Google Inc. All rights reserved. | 
					
						
							| 
									
										
										
										
											2019-12-10 13:21:51 -08:00
										 |  |  |  * Modifications copyright (c) Microsoft Corporation. | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  |  * you may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * You may obtain a copy of the License at | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *     http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions and | 
					
						
							|  |  |  |  * limitations under the License. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-10 13:20:13 -08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @type {PageTestSuite} | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  | module.exports.describe = function({testRunner, expect, WEBKIT, FFOX}) { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |   const {describe, xdescribe, fdescribe} = testRunner; | 
					
						
							| 
									
										
										
										
											2019-12-19 15:47:35 -08:00
										 |  |  |   const {it, fit, xit, dit} = testRunner; | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |   const {beforeAll, beforeEach, afterAll, afterEach} = testRunner; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Permissions API is not implemented in WebKit (see https://developer.mozilla.org/en-US/docs/Web/API/Permissions_API)
 | 
					
						
							| 
									
										
										
										
											2020-03-02 15:13:15 -08:00
										 |  |  |   describe.skip(WEBKIT)('Permissions', function() { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |     function getPermission(page, name) { | 
					
						
							|  |  |  |       return page.evaluate(name => navigator.permissions.query({name}).then(result => result.state), name); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-12 16:13:48 -08:00
										 |  |  |     it('should be prompt by default', async({page, server, context}) => { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       expect(await getPermission(page, 'geolocation')).toBe('prompt'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-12 16:13:48 -08:00
										 |  |  |     it('should deny permission when not listed', async({page, server, context}) => { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |       await context.grantPermissions([], { origin: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       expect(await getPermission(page, 'geolocation')).toBe('denied'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-12 16:13:48 -08:00
										 |  |  |     it('should fail when bad permission is given', async({page, server, context}) => { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       let error = {}; | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |       await context.grantPermissions(['foo'], { origin: server.EMPTY_PAGE }).catch(e => error = e); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       expect(error.message).toBe('Unknown permission: foo'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |     it('should grant geolocation permission when listed', async({page, server, context}) => { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |       await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       expect(await getPermission(page, 'geolocation')).toBe('granted'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-25 11:52:50 -07:00
										 |  |  |     it('should grant notifications permission when listed', async({page, server, context}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       await context.grantPermissions(['notifications'], { origin: server.EMPTY_PAGE }); | 
					
						
							|  |  |  |       expect(await getPermission(page, 'notifications')).toBe('granted'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-25 11:52:50 -07:00
										 |  |  |     it('should accumulate when adding', async({page, server, context}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       await context.grantPermissions(['geolocation']); | 
					
						
							|  |  |  |       await context.grantPermissions(['notifications']); | 
					
						
							|  |  |  |       expect(await getPermission(page, 'geolocation')).toBe('granted'); | 
					
						
							|  |  |  |       expect(await getPermission(page, 'notifications')).toBe('granted'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-25 11:52:50 -07:00
										 |  |  |     it('should clear permissions', async({page, server, context}) => { | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       await context.grantPermissions(['geolocation']); | 
					
						
							|  |  |  |       await context.clearPermissions(); | 
					
						
							|  |  |  |       await context.grantPermissions(['notifications']); | 
					
						
							|  |  |  |       expect(await getPermission(page, 'geolocation')).not.toBe('granted'); | 
					
						
							|  |  |  |       expect(await getPermission(page, 'notifications')).toBe('granted'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     it('should grant permission when listed for all domains', async({page, server, context}) => { | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       await context.grantPermissions(['geolocation']); | 
					
						
							|  |  |  |       expect(await getPermission(page, 'geolocation')).toBe('granted'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     it('should grant permission when creating context', async({server, browser}) => { | 
					
						
							|  |  |  |       const context = await browser.newContext({ permissions: ['geolocation'] }); | 
					
						
							|  |  |  |       const page = await context.newPage(); | 
					
						
							|  |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       expect(await getPermission(page, 'geolocation')).toBe('granted'); | 
					
						
							|  |  |  |       await context.close(); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-12 16:13:48 -08:00
										 |  |  |     it('should reset permissions', async({page, server, context}) => { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |       await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       expect(await getPermission(page, 'geolocation')).toBe('granted'); | 
					
						
							| 
									
										
										
										
											2019-12-20 13:07:14 -08:00
										 |  |  |       await context.clearPermissions(); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       expect(await getPermission(page, 'geolocation')).toBe('prompt'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-12 16:13:48 -08:00
										 |  |  |     it('should trigger permission onchange', async({page, server, context}) => { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       await page.evaluate(() => { | 
					
						
							|  |  |  |         window['events'] = []; | 
					
						
							|  |  |  |         return navigator.permissions.query({name: 'geolocation'}).then(function(result) { | 
					
						
							|  |  |  |           window['events'].push(result.state); | 
					
						
							|  |  |  |           result.onchange = function() { | 
					
						
							|  |  |  |             window['events'].push(result.state); | 
					
						
							|  |  |  |           }; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       expect(await page.evaluate(() => window['events'])).toEqual(['prompt']); | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |       await context.grantPermissions([], { origin: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied']); | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |       await context.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied', 'granted']); | 
					
						
							| 
									
										
										
										
											2019-12-20 13:07:14 -08:00
										 |  |  |       await context.clearPermissions(); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       expect(await page.evaluate(() => window['events'])).toEqual(['prompt', 'denied', 'granted', 'prompt']); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |     it('should isolate permissions between browser contexs', async({page, server, context, browser}) => { | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       await page.goto(server.EMPTY_PAGE); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       const otherContext = await browser.newContext(); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       const otherPage = await otherContext.newPage(); | 
					
						
							|  |  |  |       await otherPage.goto(server.EMPTY_PAGE); | 
					
						
							|  |  |  |       expect(await getPermission(page, 'geolocation')).toBe('prompt'); | 
					
						
							|  |  |  |       expect(await getPermission(otherPage, 'geolocation')).toBe('prompt'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 15:32:50 -07:00
										 |  |  |       await context.grantPermissions([], { origin: server.EMPTY_PAGE }); | 
					
						
							|  |  |  |       await otherContext.grantPermissions(['geolocation'], { origin: server.EMPTY_PAGE }); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       expect(await getPermission(page, 'geolocation')).toBe('denied'); | 
					
						
							|  |  |  |       expect(await getPermission(otherPage, 'geolocation')).toBe('granted'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-20 13:07:14 -08:00
										 |  |  |       await context.clearPermissions(); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |       expect(await getPermission(page, 'geolocation')).toBe('prompt'); | 
					
						
							|  |  |  |       expect(await getPermission(otherPage, 'geolocation')).toBe('granted'); | 
					
						
							| 
									
										
										
										
											2020-02-20 15:15:48 -08:00
										 |  |  |       await otherContext.close(); | 
					
						
							| 
									
										
										
										
											2019-11-20 09:40:26 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }; |