|
| 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.emulation; |
| 19 | + |
| 20 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 21 | +import static org.openqa.selenium.testing.drivers.Browser.FIREFOX; |
| 22 | + |
| 23 | +import java.time.ZoneId; |
| 24 | +import java.time.ZonedDateTime; |
| 25 | +import java.util.List; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.openqa.selenium.JavascriptExecutor; |
| 28 | +import org.openqa.selenium.WebDriver; |
| 29 | +import org.openqa.selenium.WindowType; |
| 30 | +import org.openqa.selenium.bidi.browsingcontext.BrowsingContext; |
| 31 | +import org.openqa.selenium.bidi.browsingcontext.CreateContextParameters; |
| 32 | +import org.openqa.selenium.bidi.browsingcontext.ReadinessState; |
| 33 | +import org.openqa.selenium.bidi.module.Browser; |
| 34 | +import org.openqa.selenium.testing.Ignore; |
| 35 | +import org.openqa.selenium.testing.JupiterTestBase; |
| 36 | +import org.openqa.selenium.testing.NeedsFreshDriver; |
| 37 | + |
| 38 | +public class SetTimezoneOverrideTest extends JupiterTestBase { |
| 39 | + |
| 40 | + int getExpectedTimezoneOffset(String timezoneId) { |
| 41 | + ZoneId zone = ZoneId.of(timezoneId); |
| 42 | + ZonedDateTime now = ZonedDateTime.now(zone); |
| 43 | + return now.getOffset().getTotalSeconds() |
| 44 | + / 60 |
| 45 | + * -1; // Negate to match JavaScript getTimezoneOffset behavior |
| 46 | + } |
| 47 | + |
| 48 | + String getTimezoneString(WebDriver driver, String context) { |
| 49 | + JavascriptExecutor executor = (JavascriptExecutor) driver; |
| 50 | + |
| 51 | + driver.switchTo().window(context); |
| 52 | + return (String) |
| 53 | + executor.executeScript("return Intl.DateTimeFormat().resolvedOptions().timeZone;"); |
| 54 | + } |
| 55 | + |
| 56 | + Number getTimezoneOffset(WebDriver driver, String context) { |
| 57 | + JavascriptExecutor executor = (JavascriptExecutor) driver; |
| 58 | + |
| 59 | + driver.switchTo().window(context); |
| 60 | + return (Number) executor.executeScript("return new Date().getTimezoneOffset()"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @NeedsFreshDriver |
| 65 | + void canSetTimezoneOverrideInContext() { |
| 66 | + BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); |
| 67 | + String contextId = context.getId(); |
| 68 | + |
| 69 | + String url = appServer.whereIs("blank.html"); |
| 70 | + context.navigate(url, ReadinessState.COMPLETE); |
| 71 | + |
| 72 | + Emulation emul = new Emulation(driver); |
| 73 | + String timezone = "America/Los_Angeles"; |
| 74 | + String tzOrg = getTimezoneString(driver, contextId); |
| 75 | + emul.setTimezoneOverride( |
| 76 | + new SetTimezoneOverrideParameters(timezone).contexts(List.of(contextId))); |
| 77 | + |
| 78 | + String tzString = getTimezoneString(driver, contextId); |
| 79 | + Number tzOffset = getTimezoneOffset(driver, contextId); |
| 80 | + |
| 81 | + int expectedOffset = getExpectedTimezoneOffset(timezone); |
| 82 | + |
| 83 | + assertThat(tzString).isEqualTo(timezone); |
| 84 | + assertThat(tzOffset.intValue()).isEqualTo(expectedOffset); |
| 85 | + |
| 86 | + emul.setTimezoneOverride(new SetTimezoneOverrideParameters(null).contexts(List.of(contextId))); |
| 87 | + String TzNew = getTimezoneString(driver, contextId); |
| 88 | + assertThat(TzNew).isEqualTo(tzOrg); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + @NeedsFreshDriver |
| 93 | + void canSetTimeZoneOverrideInUserContext() { |
| 94 | + Browser browser = new Browser(driver); |
| 95 | + String userContext = browser.createUserContext(); |
| 96 | + |
| 97 | + BrowsingContext context = |
| 98 | + new BrowsingContext( |
| 99 | + driver, new CreateContextParameters(WindowType.TAB).userContext(userContext)); |
| 100 | + String contextId = context.getId(); |
| 101 | + |
| 102 | + String url = appServer.whereIs("blank.html"); |
| 103 | + context.navigate(url, ReadinessState.COMPLETE); |
| 104 | + |
| 105 | + Emulation emul = new Emulation(driver); |
| 106 | + String timezone = "Europe/London"; |
| 107 | + String tzOrg = getTimezoneString(driver, contextId); |
| 108 | + emul.setTimezoneOverride( |
| 109 | + new SetTimezoneOverrideParameters(timezone).userContexts(List.of(userContext))); |
| 110 | + |
| 111 | + String tzString = getTimezoneString(driver, contextId); |
| 112 | + Number tzOffset = getTimezoneOffset(driver, contextId); |
| 113 | + |
| 114 | + int expectedOffset = getExpectedTimezoneOffset(timezone); |
| 115 | + |
| 116 | + assertThat(tzString).isEqualTo(timezone); |
| 117 | + assertThat(tzOffset.intValue()).isEqualTo(expectedOffset); |
| 118 | + |
| 119 | + emul.setTimezoneOverride( |
| 120 | + new SetTimezoneOverrideParameters(null).userContexts(List.of(userContext))); |
| 121 | + String TzNew = getTimezoneString(driver, contextId); |
| 122 | + assertThat(TzNew).isEqualTo(tzOrg); |
| 123 | + |
| 124 | + context.close(); |
| 125 | + browser.removeUserContext(userContext); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + @NeedsFreshDriver |
| 130 | + @Ignore(FIREFOX) |
| 131 | + void canSetTimezoneOverrideUsingOffset() { |
| 132 | + BrowsingContext context = new BrowsingContext(driver, driver.getWindowHandle()); |
| 133 | + String contextId = context.getId(); |
| 134 | + |
| 135 | + String url = appServer.whereIs("blank.html"); |
| 136 | + context.navigate(url, ReadinessState.COMPLETE); |
| 137 | + |
| 138 | + Emulation emul = new Emulation(driver); |
| 139 | + String timezone = "+05:30"; |
| 140 | + String tzOrg = getTimezoneString(driver, contextId); |
| 141 | + |
| 142 | + emul.setTimezoneOverride( |
| 143 | + new SetTimezoneOverrideParameters(timezone).contexts(List.of(contextId))); |
| 144 | + |
| 145 | + String tzString = getTimezoneString(driver, contextId); |
| 146 | + Number tzOffset = getTimezoneOffset(driver, contextId); |
| 147 | + |
| 148 | + assertThat(tzOffset.intValue()).isEqualTo(-330); |
| 149 | + assertThat(tzString).isEqualTo("+05:30"); |
| 150 | + |
| 151 | + emul.setTimezoneOverride(new SetTimezoneOverrideParameters(null).contexts(List.of(contextId))); |
| 152 | + String tzNew = getTimezoneString(driver, contextId); |
| 153 | + assertThat(tzNew).isEqualTo(tzOrg); |
| 154 | + } |
| 155 | +} |
0 commit comments