|
| 1 | +// Licensed to the Software Freedom Conservancy (SFC) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The SFC licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.openqa.selenium.bidi.permissions; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Optional; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.openqa.selenium.WindowType; |
| 27 | +import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; |
| 28 | +import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters; |
| 29 | +import org.openqa.selenium.bidi.browsingcontext.ReadinessState; |
| 30 | +import org.openqa.selenium.bidi.module.Browser; |
| 31 | +import org.openqa.selenium.bidi.module.Permission; |
| 32 | +import org.openqa.selenium.bidi.module.Script; |
| 33 | +import org.openqa.selenium.bidi.script.EvaluateResult; |
| 34 | +import org.openqa.selenium.bidi.script.EvaluateResultSuccess; |
| 35 | +import org.openqa.selenium.testing.JupiterTestBase; |
| 36 | +import org.openqa.selenium.testing.NeedsFreshDriver; |
| 37 | +import org.openqa.selenium.testing.Pages; |
| 38 | + |
| 39 | +public class PermissionsTest extends JupiterTestBase { |
| 40 | + private Permission permission; |
| 41 | + private Script script; |
| 42 | + |
| 43 | + private static final String GET_GEOLOCATION_PERMISSION = |
| 44 | + "() => {return navigator.permissions.query({ name: 'geolocation' })" |
| 45 | + + ".then(val => val.state, err => err.message)}"; |
| 46 | + private static final String GET_ORIGIN = "() => {return window.location.origin;}"; |
| 47 | + |
| 48 | + @BeforeEach |
| 49 | + public void setUp() { |
| 50 | + permission = new Permission(driver); |
| 51 | + script = new Script(driver); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + @NeedsFreshDriver |
| 56 | + public void canSetPermission() { |
| 57 | + BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); |
| 58 | + |
| 59 | + context.navigate(new Pages(appServer).blankPage, ReadinessState.COMPLETE); |
| 60 | + |
| 61 | + String contextId = context.getId(); |
| 62 | + |
| 63 | + EvaluateResult origin = |
| 64 | + script.callFunctionInBrowsingContext( |
| 65 | + contextId, GET_ORIGIN, true, Optional.empty(), Optional.empty(), Optional.empty()); |
| 66 | + |
| 67 | + String originValue = (String) ((EvaluateResultSuccess) origin).getResult().getValue().get(); |
| 68 | + |
| 69 | + permission.setPermission(Map.of("name", "geolocation"), PermissionState.GRANTED, originValue); |
| 70 | + |
| 71 | + EvaluateResult result = |
| 72 | + script.callFunctionInBrowsingContext( |
| 73 | + contextId, |
| 74 | + GET_GEOLOCATION_PERMISSION, |
| 75 | + true, |
| 76 | + Optional.empty(), |
| 77 | + Optional.empty(), |
| 78 | + Optional.empty()); |
| 79 | + |
| 80 | + String resultValue = (String) ((EvaluateResultSuccess) result).getResult().getValue().get(); |
| 81 | + assertThat(resultValue).isEqualTo("granted"); |
| 82 | + |
| 83 | + permission.setPermission(Map.of("name", "geolocation"), PermissionState.DENIED, originValue); |
| 84 | + |
| 85 | + result = |
| 86 | + script.callFunctionInBrowsingContext( |
| 87 | + contextId, |
| 88 | + GET_GEOLOCATION_PERMISSION, |
| 89 | + true, |
| 90 | + Optional.empty(), |
| 91 | + Optional.empty(), |
| 92 | + Optional.empty()); |
| 93 | + |
| 94 | + resultValue = (String) ((EvaluateResultSuccess) result).getResult().getValue().get(); |
| 95 | + assertThat(resultValue).isEqualTo("denied"); |
| 96 | + |
| 97 | + permission.setPermission(Map.of("name", "geolocation"), PermissionState.PROMPT, originValue); |
| 98 | + |
| 99 | + result = |
| 100 | + script.callFunctionInBrowsingContext( |
| 101 | + contextId, |
| 102 | + GET_GEOLOCATION_PERMISSION, |
| 103 | + true, |
| 104 | + Optional.empty(), |
| 105 | + Optional.empty(), |
| 106 | + Optional.empty()); |
| 107 | + |
| 108 | + resultValue = (String) ((EvaluateResultSuccess) result).getResult().getValue().get(); |
| 109 | + assertThat(resultValue).isEqualTo("prompt"); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @NeedsFreshDriver |
| 114 | + public void canSetPermissionForAUserContext() { |
| 115 | + Browser browser = new Browser(driver); |
| 116 | + |
| 117 | + String url = new Pages(appServer).blankPage; |
| 118 | + |
| 119 | + String userContext = browser.createUserContext(); |
| 120 | + |
| 121 | + String originalTab = driver.getWindowHandle(); |
| 122 | + |
| 123 | + BrowsingContext context1 = new BrowsingContext(this.driver, driver.getWindowHandle()); |
| 124 | + BrowsingContext context2 = |
| 125 | + new BrowsingContext( |
| 126 | + this.driver, new CreateContextParameters(WindowType.TAB).userContext(userContext)); |
| 127 | + |
| 128 | + String newTab = context2.getId(); |
| 129 | + |
| 130 | + context1.navigate(url, ReadinessState.COMPLETE); |
| 131 | + context2.navigate(url, ReadinessState.COMPLETE); |
| 132 | + |
| 133 | + EvaluateResult origin = |
| 134 | + script.callFunctionInBrowsingContext( |
| 135 | + originalTab, GET_ORIGIN, true, Optional.empty(), Optional.empty(), Optional.empty()); |
| 136 | + |
| 137 | + String originValue = (String) ((EvaluateResultSuccess) origin).getResult().getValue().get(); |
| 138 | + |
| 139 | + permission.setPermission( |
| 140 | + Map.of("name", "geolocation"), PermissionState.GRANTED, originValue, userContext); |
| 141 | + |
| 142 | + EvaluateResult newTabUpdatedPermission = |
| 143 | + script.callFunctionInBrowsingContext( |
| 144 | + newTab, |
| 145 | + GET_GEOLOCATION_PERMISSION, |
| 146 | + true, |
| 147 | + Optional.empty(), |
| 148 | + Optional.empty(), |
| 149 | + Optional.empty()); |
| 150 | + |
| 151 | + String newTabUpdatedPermissionValue = |
| 152 | + (String) ((EvaluateResultSuccess) newTabUpdatedPermission).getResult().getValue().get(); |
| 153 | + assertThat(newTabUpdatedPermissionValue).isEqualTo("granted"); |
| 154 | + |
| 155 | + BrowsingContext context3 = |
| 156 | + new BrowsingContext( |
| 157 | + this.driver, new CreateContextParameters(WindowType.TAB).userContext(userContext)); |
| 158 | + |
| 159 | + context3.navigate(url, ReadinessState.COMPLETE); |
| 160 | + |
| 161 | + newTabUpdatedPermission = |
| 162 | + script.callFunctionInBrowsingContext( |
| 163 | + context3.getId(), |
| 164 | + GET_GEOLOCATION_PERMISSION, |
| 165 | + true, |
| 166 | + Optional.empty(), |
| 167 | + Optional.empty(), |
| 168 | + Optional.empty()); |
| 169 | + |
| 170 | + newTabUpdatedPermissionValue = |
| 171 | + (String) ((EvaluateResultSuccess) newTabUpdatedPermission).getResult().getValue().get(); |
| 172 | + assertThat(newTabUpdatedPermissionValue).isEqualTo("granted"); |
| 173 | + } |
| 174 | +} |
0 commit comments