Skip to content

Commit d729870

Browse files
committed
use duration for readability
1 parent 4d157b9 commit d729870

37 files changed

+110
-100
lines changed

src/test/java/org/htmlunit/BrowserVersion2Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void acceptHeaderWindowOpen() throws Exception {
7575
final WebDriver driver = loadPage2(html);
7676
driver.findElement(By.id("clickme")).click();
7777
// because real browsers are doing the open async, we have to be a bit patient
78-
Thread.sleep(DEFAULT_WAIT_TIME);
78+
Thread.sleep(DEFAULT_WAIT_TIME.toMillis());
7979

8080
assertEquals(getExpectedAlerts()[0], Integer.toString(getMockWebConnection().getRequestCount()));
8181
assertEquals(getExpectedAlerts()[1], acceptHeaderString());

src/test/java/org/htmlunit/CookieManagerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public void setCookieTimeout() throws Exception {
323323
getMockWebConnection().setResponse(URL_FIRST, html, 200, "OK", MimeType.TEXT_HTML, responseHeader1);
324324

325325
loadPage2(URL_FIRST, StandardCharsets.ISO_8859_1);
326-
verifyTitle2(DEFAULT_WAIT_TIME * 4, getWebDriver(), getExpectedAlerts());
326+
verifyTitle2(DEFAULT_WAIT_TIME.multipliedBy(4), getWebDriver(), getExpectedAlerts());
327327
}
328328

329329
/**
@@ -555,7 +555,7 @@ public void cookieExpiresAfterBeingSet() throws Exception {
555555
+ "</body></html>";
556556

557557
loadPage2(html);
558-
verifyTitle2(4 * DEFAULT_WAIT_TIME, getWebDriver(), getExpectedAlerts());
558+
verifyTitle2(DEFAULT_WAIT_TIME.multipliedBy(4), getWebDriver(), getExpectedAlerts());
559559
}
560560

561561
/**
@@ -676,7 +676,7 @@ public void setCookieDuring302() throws Exception {
676676
getMockWebConnection().setResponse(firstUrl, "", 302, "Moved", MimeType.TEXT_HTML, responseHeader1);
677677

678678
loadPage2(firstUrl, StandardCharsets.ISO_8859_1);
679-
verifyTitle2(DEFAULT_WAIT_TIME * 4, getWebDriver(), getExpectedAlerts());
679+
verifyTitle2(DEFAULT_WAIT_TIME.multipliedBy(4), getWebDriver(), getExpectedAlerts());
680680
}
681681

682682
/**

src/test/java/org/htmlunit/HttpWebConnection3Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public void locationQueryUTF8Encoded() throws Exception {
260260
final WebDriver driver = getWebDriver();
261261

262262
driver.get(url);
263-
Thread.sleep(DEFAULT_WAIT_TIME);
263+
Thread.sleep(DEFAULT_WAIT_TIME.toMillis());
264264
assertEquals(getExpectedAlerts()[0], driver.getCurrentUrl());
265265
assertEquals(2, primitiveWebServer.getRequests().size());
266266
assertTrue(driver.getPageSource(), driver.getPageSource().contains("Hi"));

src/test/java/org/htmlunit/SimpleWebTestCase.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.htmlunit;
1616

1717
import java.net.URL;
18+
import java.time.Duration;
1819
import java.util.ArrayList;
1920
import java.util.HashMap;
2021
import java.util.List;
@@ -188,7 +189,7 @@ protected final WebClient getWebClientWithMockWebConnection() {
188189
* @throws Exception if something goes wrong
189190
*/
190191
protected final HtmlPage loadPageWithAlerts(final String html) throws Exception {
191-
return loadPageWithAlerts(html, URL_FIRST, -1);
192+
return loadPageWithAlerts(html, URL_FIRST, null);
192193
}
193194

194195
/**
@@ -201,7 +202,7 @@ protected final HtmlPage loadPageWithAlerts(final String html) throws Exception
201202
* @return the new page
202203
* @throws Exception if something goes wrong
203204
*/
204-
protected final HtmlPage loadPageWithAlerts(final String html, final URL url, final int waitForJS)
205+
protected final HtmlPage loadPageWithAlerts(final String html, final URL url, final Duration waitForJS)
205206
throws Exception {
206207
if (getExpectedAlerts() == null) {
207208
throw new IllegalStateException("You must annotate the test class with '@RunWith(BrowserRunner.class)'");
@@ -218,8 +219,8 @@ protected final HtmlPage loadPageWithAlerts(final String html, final URL url, fi
218219
webConnection.setResponse(url, html);
219220

220221
final HtmlPage page = client.getPage(url);
221-
if (waitForJS > 0) {
222-
assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(waitForJS));
222+
if (waitForJS != null) {
223+
assertEquals(0, client.waitForBackgroundJavaScriptStartingBefore(waitForJS.toMillis()));
223224
}
224225
assertEquals(getExpectedAlerts(), collectedAlerts);
225226
return page;

src/test/java/org/htmlunit/WebClient2Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public void makeSureTheCurrentJobHasEndedBeforeReplaceWindowPage() throws Except
345345
// Immediately load page 2. Timeout function was triggered already
346346
final HtmlPage page2 = client.getPage(URL_SECOND);
347347
verify(() -> page1.getEnclosingWindow().getName(),
348-
getExpectedAlerts()[0] + getExpectedAlerts()[1], DEFAULT_WAIT_TIME * 4);
348+
getExpectedAlerts()[0] + getExpectedAlerts()[1], DEFAULT_WAIT_TIME.multipliedBy(4));
349349

350350
// Fails: return 98 (about) instead of 1
351351
// assertEquals(1, page.querySelectorAll("p").size());

src/test/java/org/htmlunit/WebDriverTestCase.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.URISyntaxException;
2424
import java.net.URL;
2525
import java.nio.charset.Charset;
26+
import java.time.Duration;
2627
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.Collections;
@@ -1094,7 +1095,7 @@ protected final WebDriver loadPageVerifyTitle2(final String html, final String..
10941095
return verifyTitle2(driver, expectedAlerts);
10951096
}
10961097

1097-
protected final WebDriver verifyTitle2(final long maxWaitTime, final WebDriver driver,
1098+
protected final WebDriver verifyTitle2(final Duration maxWaitTime, final WebDriver driver,
10981099
final String... expectedAlerts) throws Exception {
10991100

11001101
final StringBuilder expected = new StringBuilder();
@@ -1103,7 +1104,7 @@ protected final WebDriver verifyTitle2(final long maxWaitTime, final WebDriver d
11031104
}
11041105
final String expectedTitle = expected.toString();
11051106

1106-
final long maxWait = System.currentTimeMillis() + maxWaitTime;
1107+
final long maxWait = System.currentTimeMillis() + maxWaitTime.toMillis();
11071108

11081109
while (System.currentTimeMillis() < maxWait) {
11091110
try {
@@ -1200,9 +1201,9 @@ protected final WebDriver verifyJsVariable(final WebDriver driver, final String
12001201
return driver;
12011202
}
12021203

1203-
protected final WebDriver verifyWindowName2(final long maxWaitTime, final WebDriver driver,
1204+
protected final WebDriver verifyWindowName2(final Duration maxWaitTime, final WebDriver driver,
12041205
final String... expectedAlerts) throws Exception {
1205-
final long maxWait = System.currentTimeMillis() + maxWaitTime;
1206+
final long maxWait = System.currentTimeMillis() + maxWaitTime.toMillis();
12061207

12071208
while (System.currentTimeMillis() < maxWait) {
12081209
try {
@@ -1245,7 +1246,7 @@ protected final WebDriver verifySessionStorage2(final WebDriver driver,
12451246
* @return the web driver
12461247
* @throws Exception if something goes wrong
12471248
*/
1248-
protected final WebDriver loadPageWithAlerts2(final String html, final long maxWaitTime) throws Exception {
1249+
protected final WebDriver loadPageWithAlerts2(final String html, final Duration maxWaitTime) throws Exception {
12491250
return loadPageWithAlerts2(html, URL_FIRST, maxWaitTime);
12501251
}
12511252

@@ -1268,7 +1269,7 @@ protected final WebDriver loadPageWithAlerts2(final String html, final URL url)
12681269
* @return the web driver
12691270
* @throws Exception if something goes wrong
12701271
*/
1271-
protected final WebDriver loadPageWithAlerts2(final String html, final URL url, final long maxWaitTime)
1272+
protected final WebDriver loadPageWithAlerts2(final String html, final URL url, final Duration maxWaitTime)
12721273
throws Exception {
12731274
final WebDriver driver = loadPage2(html, url);
12741275

@@ -1294,7 +1295,7 @@ protected void verifyAlerts(final WebDriver driver, final String... expectedAler
12941295
* @param expected the expected alerts
12951296
* @throws Exception in case of failure
12961297
*/
1297-
protected void verifyAlerts(final long maxWaitTime, final WebDriver driver, final String... expected)
1298+
protected void verifyAlerts(final Duration maxWaitTime, final WebDriver driver, final String... expected)
12981299
throws Exception {
12991300
final List<String> actualAlerts = getCollectedAlerts(maxWaitTime, driver, expected.length);
13001301

@@ -1342,7 +1343,7 @@ protected final WebDriver loadPageWithAlerts2(final String html,
13421343
* @return the web driver
13431344
* @throws Exception if something goes wrong
13441345
*/
1345-
protected final WebDriver loadPageWithAlerts2(final String html, final URL url, final long maxWaitTime,
1346+
protected final WebDriver loadPageWithAlerts2(final String html, final URL url, final Duration maxWaitTime,
13461347
final Map<String, Class<? extends Servlet>> servlets) throws Exception {
13471348

13481349
expandExpectedAlertsVariables(URL_FIRST);
@@ -1371,7 +1372,7 @@ protected final WebDriver loadPageWithAlerts2(final URL url) throws Exception {
13711372
* @return the web driver
13721373
* @throws Exception if something goes wrong
13731374
*/
1374-
protected final WebDriver loadPageWithAlerts2(final URL url, final long maxWaitTime) throws Exception {
1375+
protected final WebDriver loadPageWithAlerts2(final URL url, final Duration maxWaitTime) throws Exception {
13751376
startWebServer(getMockWebConnection(), null);
13761377

13771378
final WebDriver driver = getWebDriver();
@@ -1413,11 +1414,11 @@ protected List<String> getCollectedAlerts(final WebDriver driver, final int aler
14131414
* @return the collected alerts
14141415
* @throws Exception in case of problem
14151416
*/
1416-
protected List<String> getCollectedAlerts(final long maxWaitTime, final WebDriver driver, final int alertsLength)
1417-
throws Exception {
1417+
protected List<String> getCollectedAlerts(final Duration maxWaitTime,
1418+
final WebDriver driver, final int alertsLength) throws Exception {
14181419
final List<String> collectedAlerts = new ArrayList<>();
14191420

1420-
long maxWait = System.currentTimeMillis() + maxWaitTime;
1421+
long maxWait = System.currentTimeMillis() + maxWaitTime.toMillis();
14211422

14221423
while (collectedAlerts.size() < alertsLength && System.currentTimeMillis() < maxWait) {
14231424
try {
@@ -1567,7 +1568,7 @@ public void beforeTest() throws InterruptedException {
15671568
* @throws Exception in case of failure
15681569
*/
15691570
protected void assertTitle(final WebDriver webdriver, final String expected) throws Exception {
1570-
final long maxWait = System.currentTimeMillis() + DEFAULT_WAIT_TIME;
1571+
final long maxWait = System.currentTimeMillis() + DEFAULT_WAIT_TIME.toMillis();
15711572

15721573
while (true) {
15731574
final String title = webdriver.getTitle();

src/test/java/org/htmlunit/WebServerTestCase.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.net.BindException;
2121
import java.net.URL;
2222
import java.nio.charset.Charset;
23+
import java.time.Duration;
2324
import java.util.List;
2425
import java.util.Map;
2526

@@ -256,7 +257,7 @@ public void tearDown() throws Exception {
256257
*/
257258
protected final HtmlPage loadPageWithAlerts(final String html, final URL url)
258259
throws Exception {
259-
return loadPageWithAlerts(html, url, 0);
260+
return loadPageWithAlerts(html, url, Duration.ofSeconds(0));
260261
}
261262

262263
/**
@@ -267,7 +268,7 @@ protected final HtmlPage loadPageWithAlerts(final String html, final URL url)
267268
* @return the page
268269
* @throws Exception if something goes wrong
269270
*/
270-
protected final HtmlPage loadPageWithAlerts(final String html, final URL url, final long maxWaitTime)
271+
protected final HtmlPage loadPageWithAlerts(final String html, final URL url, final Duration maxWaitTime)
271272
throws Exception {
272273
alertHandler_.clear();
273274
expandExpectedAlertsVariables(URL_FIRST);
@@ -276,7 +277,7 @@ protected final HtmlPage loadPageWithAlerts(final String html, final URL url, fi
276277
final HtmlPage page = loadPage(html, url);
277278

278279
List<String> actualAlerts = getCollectedAlerts(page);
279-
final long maxWait = System.currentTimeMillis() + maxWaitTime;
280+
final long maxWait = System.currentTimeMillis() + maxWaitTime.toMillis();
280281
while (actualAlerts.size() < expectedAlerts.length && System.currentTimeMillis() < maxWait) {
281282
Thread.sleep(30);
282283
actualAlerts = getCollectedAlerts(page);

src/test/java/org/htmlunit/WebTestCase.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.URL;
2828
import java.nio.file.Files;
2929
import java.nio.file.Paths;
30+
import java.time.Duration;
3031
import java.util.ArrayList;
3132
import java.util.Arrays;
3233
import java.util.Base64;
@@ -115,7 +116,7 @@ public abstract class WebTestCase {
115116
public static final String SOCKS_PROXY_HOST = System.getProperty("htmlunit.test.socksproxy.host", "localhost");
116117

117118
/** The default time used to wait for the expected alerts. */
118-
protected static final long DEFAULT_WAIT_TIME = 1000;
119+
protected static final Duration DEFAULT_WAIT_TIME = Duration.ofSeconds(1);
119120

120121
/** Constant for the URL which is used in the tests. */
121122
public static final URL URL_FIRST;
@@ -485,8 +486,8 @@ protected void verify(final Supplier<String> func, final String expected) throws
485486
* @throws Exception in case of failure
486487
*/
487488
protected void verify(final Supplier<String> func, final String expected,
488-
final long maxWaitTime) throws Exception {
489-
final long maxWait = System.currentTimeMillis() + maxWaitTime;
489+
final Duration maxWaitTime) throws Exception {
490+
final long maxWait = System.currentTimeMillis() + maxWaitTime.toMillis();
490491

491492
String actual = null;
492493
while (System.currentTimeMillis() < maxWait) {

src/test/java/org/htmlunit/html/HtmlAnchorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,7 @@ public void ctrlClick() throws Exception {
745745
.keyUp(Keys.CONTROL)
746746
.perform();
747747

748-
Thread.sleep(DEFAULT_WAIT_TIME);
748+
Thread.sleep(DEFAULT_WAIT_TIME.toMillis());
749749
assertEquals("Should have opened a new window",
750750
windowsSize + Integer.parseInt(getExpectedAlerts()[0]), driver.getWindowHandles().size());
751751
assertEquals("Should not have navigated away", getExpectedAlerts()[1], driver.getTitle());
@@ -872,7 +872,7 @@ public void controlClick_refererHeader() throws Exception {
872872
.keyUp(Keys.CONTROL)
873873
.build().perform();
874874

875-
Thread.sleep(DEFAULT_WAIT_TIME / 10);
875+
Thread.sleep(DEFAULT_WAIT_TIME.toMillis() / 10);
876876

877877
assertEquals(2, getMockWebConnection().getRequestCount());
878878

src/test/java/org/htmlunit/html/HtmlFileInput3Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ private void contentType(final String extension) throws Exception {
352352
assertTrue(tmpFile.delete());
353353
}
354354

355-
final long maxWait = System.currentTimeMillis() + DEFAULT_WAIT_TIME;
355+
final long maxWait = System.currentTimeMillis() + DEFAULT_WAIT_TIME.toMillis();
356356

357357
while (System.currentTimeMillis() < maxWait) {
358358
try {

0 commit comments

Comments
 (0)