Skip to content

Commit

Permalink
Deprecate our Clock class in favour of the one in java.date
Browse files Browse the repository at this point in the history
Also deprecate all constructors and methods that take our Clock.
  • Loading branch information
shs96c committed Jul 13, 2018
1 parent 2cbcd08 commit 4c93795
Show file tree
Hide file tree
Showing 14 changed files with 288 additions and 105 deletions.
19 changes: 15 additions & 4 deletions java/client/src/org/openqa/selenium/lift/WebDriverTestContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.openqa.selenium.support.ui.Clock;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Sleeper;
import org.openqa.selenium.support.ui.SystemClock;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

Expand All @@ -40,14 +39,22 @@
public class WebDriverTestContext implements TestContext {

private WebDriver driver;
private final Clock clock;
private final java.time.Clock clock;
private final Sleeper sleeper;

public WebDriverTestContext(WebDriver driver) {
this(driver, new SystemClock(), Sleeper.SYSTEM_SLEEPER);
this(driver, java.time.Clock.systemDefaultZone(), Sleeper.SYSTEM_SLEEPER);
}

/**
* @deprecated Use {@link #WebDriverTestContext(WebDriver, java.time.Clock, Sleeper)}.
*/
@Deprecated
WebDriverTestContext(WebDriver driver, Clock clock, Sleeper sleeper) {
this(driver, clock.asJreClock(), sleeper);
}

WebDriverTestContext(WebDriver driver, java.time.Clock clock, Sleeper sleeper) {
this.driver = driver;
this.clock = clock;
this.sleeper = sleeper;
Expand Down Expand Up @@ -132,7 +139,11 @@ public void waitFor(final Finder<WebElement, WebDriver> finder, final long timeo
? defaultSleepTimeoutMillis : timeoutMillis / 2;

Wait<WebDriver> wait =
new WebDriverWait(driver, clock, sleeper, millisToSeconds(timeoutMillis),
new WebDriverWait(
driver,
clock,
sleeper,
millisToSeconds(timeoutMillis),
sleepTimeout) {
@Override
protected RuntimeException timeoutException(String message, Throwable lastException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Clock;
import org.openqa.selenium.support.ui.SlowLoadableComponent;
import org.openqa.selenium.support.ui.SystemClock;

import java.lang.reflect.Field;
import java.time.Clock;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -48,12 +47,30 @@ public class AjaxElementLocator extends DefaultElementLocator {
* @param timeOutInSeconds How long to wait for the element to appear. Measured in seconds.
* @param annotations AbstractAnnotations class implementation
*/
public AjaxElementLocator(SearchContext context, int timeOutInSeconds, AbstractAnnotations annotations) {
this(new SystemClock(), context, timeOutInSeconds, annotations);
public AjaxElementLocator(
SearchContext context,
int timeOutInSeconds,
AbstractAnnotations annotations) {
this(Clock.systemDefaultZone(), context, timeOutInSeconds, annotations);
}

public AjaxElementLocator(Clock clock, SearchContext context, int timeOutInSeconds,
AbstractAnnotations annotations) {
/**
* @deprecated Use {@link #AjaxElementLocator(Clock, SearchContext, int, AbstractAnnotations)}.
*/
@Deprecated
public AjaxElementLocator(
org.openqa.selenium.support.ui.Clock clock,
SearchContext context,
int timeOutInSeconds,
AbstractAnnotations annotations) {
this(clock.asJreClock(), context, timeOutInSeconds, annotations);
}

public AjaxElementLocator(
Clock clock,
SearchContext context,
int timeOutInSeconds,
AbstractAnnotations annotations) {
super(context, annotations);
this.timeOutInSeconds = timeOutInSeconds;
this.clock = clock;
Expand All @@ -67,10 +84,26 @@ public AjaxElementLocator(Clock clock, SearchContext context, int timeOutInSecon
* @param timeOutInSeconds How long to wait for the element to appear. Measured in seconds.
*/
public AjaxElementLocator(SearchContext searchContext, Field field, int timeOutInSeconds) {
this(new SystemClock(), searchContext, field, timeOutInSeconds);
this(Clock.systemDefaultZone(), searchContext, field, timeOutInSeconds);
}

public AjaxElementLocator(Clock clock, SearchContext searchContext, Field field, int timeOutInSeconds) {
/**
* @deprecated Use {@link #AjaxElementLocator(Clock, SearchContext, int, AbstractAnnotations)}.
*/
@Deprecated
public AjaxElementLocator(
org.openqa.selenium.support.ui.Clock clock,
SearchContext searchContext,
Field field,
int timeOutInSeconds) {
this(clock.asJreClock(), searchContext, timeOutInSeconds, new Annotations(field));
}

public AjaxElementLocator(
Clock clock,
SearchContext searchContext,
Field field,
int timeOutInSeconds) {
this(clock, searchContext, timeOutInSeconds, new Annotations(field));
}

Expand Down Expand Up @@ -136,6 +169,11 @@ private class SlowLoadingElement extends SlowLoadableComponent<SlowLoadingElemen
private NoSuchElementException lastException;
private WebElement element;

@Deprecated
public SlowLoadingElement(org.openqa.selenium.support.ui.Clock clock, int timeOutInSeconds) {
super(clock, timeOutInSeconds);
}

public SlowLoadingElement(Clock clock, int timeOutInSeconds) {
super(clock, timeOutInSeconds);
}
Expand Down Expand Up @@ -177,6 +215,11 @@ private class SlowLoadingElementList extends SlowLoadableComponent<SlowLoadingEl
private NoSuchElementException lastException;
private List<WebElement> elements;

@Deprecated
public SlowLoadingElementList(org.openqa.selenium.support.ui.Clock clock, int timeOutInSeconds) {
this(clock.asJreClock(), timeOutInSeconds);
}

public SlowLoadingElementList(Clock clock, int timeOutInSeconds) {
super(clock, timeOutInSeconds);
}
Expand Down
31 changes: 30 additions & 1 deletion java/client/src/org/openqa/selenium/support/ui/Clock.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@

package org.openqa.selenium.support.ui;

import java.time.Instant;
import java.time.ZoneId;

/**
* A simple encapsulation to allowing timing
* A simple encapsulation to allowing timing.
*
* @deprecated Use {@link java.time.Clock}.
*/
@Deprecated
public interface Clock {

/**
Expand All @@ -45,4 +51,27 @@ public interface Clock {
*/
boolean isNowBefore(long endInMillis);

default java.time.Clock asJreClock() {
class JreClock extends java.time.Clock {

@Override
public ZoneId getZone() {
return ZoneId.systemDefault();
}

@Override
public java.time.Clock withZone(ZoneId zone) {
throw new UnsupportedOperationException(
"Please use a complete instance of java.time.Clock");
}

@Override
public Instant instant() {
return Instant.ofEpochMilli(now());
}
}

return new JreClock();
}

}
26 changes: 19 additions & 7 deletions java/client/src/org/openqa/selenium/support/ui/FluentWait.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.openqa.selenium.support.ui;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.TimeUnit.MILLISECONDS;

import com.google.common.base.Throwables;
Expand All @@ -26,6 +26,7 @@
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriverException;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -79,7 +80,7 @@ public class FluentWait<T> implements Wait<T> {
private static final java.time.Duration DEFAULT_WAIT_DURATION = java.time.Duration.ofMillis(DEFAULT_SLEEP_TIMEOUT);

private final T input;
private final Clock clock;
private final java.time.Clock clock;
private final Sleeper sleeper;

private java.time.Duration timeout = DEFAULT_WAIT_DURATION;
Expand All @@ -100,10 +101,20 @@ public FluentWait(T input) {
* @param clock The clock to use when measuring the timeout.
* @param sleeper Used to put the thread to sleep between evaluation loops.
*/
@Deprecated
public FluentWait(T input, Clock clock, Sleeper sleeper) {
this.input = checkNotNull(input);
this.clock = checkNotNull(clock);
this.sleeper = checkNotNull(sleeper);
this(input, clock.asJreClock(), sleeper);
}

/**
* @param input The input value to pass to the evaluated conditions.
* @param clock The clock to use when measuring the timeout.
* @param sleeper Used to put the thread to sleep between evaluation loops.
*/
public FluentWait(T input, java.time.Clock clock, Sleeper sleeper) {
this.input = requireNonNull(input);
this.clock = requireNonNull(clock);
this.sleeper = requireNonNull(sleeper);
}

/**
Expand Down Expand Up @@ -241,7 +252,8 @@ public FluentWait<T> ignoring(Class<? extends Throwable> firstType,
*/
@Override
public <V> V until(Function<? super T, V> isTrue) {
long end = clock.laterBy(timeout.toMillis());
Instant end = clock.instant().plus(timeout);

Throwable lastException;
while (true) {
try {
Expand All @@ -260,7 +272,7 @@ public <V> V until(Function<? super T, V> isTrue) {

// Check the timeout after evaluating the function to ensure conditions
// with a zero timeout can succeed.
if (!clock.isNowBefore(end)) {
if (end.isBefore(clock.instant())) {
String message = messageSupplier != null ?
messageSupplier.get() : null;

Expand Down
33 changes: 33 additions & 0 deletions java/client/src/org/openqa/selenium/support/ui/JreBackedClock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.openqa.selenium.support.ui;

import org.openqa.selenium.Beta;

import java.time.Clock;
import java.time.Instant;
import java.time.ZoneId;
import java.util.Objects;

@Beta
class JreBackedClock extends java.time.Clock {

private final Clock delegate;

public JreBackedClock(Clock delegate) {
this.delegate = Objects.requireNonNull(delegate);
}

@Override
public ZoneId getZone() {
return null;
}

@Override
public Clock withZone(ZoneId zone) {
return null;
}

@Override
public Instant instant() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

package org.openqa.selenium.support.ui;

import static java.util.concurrent.TimeUnit.SECONDS;
import java.time.Duration;
import java.time.Instant;


/**
Expand All @@ -33,12 +34,20 @@
*/
public abstract class SlowLoadableComponent<T extends LoadableComponent<T>>
extends LoadableComponent<T> {
private final Clock clock;
private final long timeOutInSeconds;
private final java.time.Clock clock;
private final Duration timeOutInSeconds;

/**
* @deprecated Use {@link #SlowLoadableComponent(java.time.Clock, int)}.
*/
@Deprecated
public SlowLoadableComponent(Clock clock, int timeOutInSeconds) {
this(clock.asJreClock(), timeOutInSeconds);
}

public SlowLoadableComponent(java.time.Clock clock, int timeOutInSeconds) {
this.clock = clock;
this.timeOutInSeconds = timeOutInSeconds;
this.timeOutInSeconds = Duration.ofSeconds(timeOutInSeconds);
}

@Override
Expand All @@ -51,9 +60,9 @@ public T get() {
load();
}

long end = clock.laterBy(SECONDS.toMillis(timeOutInSeconds));
Instant end = clock.instant().plus(timeOutInSeconds);

while (clock.isNowBefore(end)) {
while (clock.instant().isBefore(end)) {
try {
isLoaded();
return (T) this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

package org.openqa.selenium.support.ui;

/**
* @deprecated Use {@link java.time.Clock#systemDefaultZone()}.
*/
@Deprecated
public class SystemClock implements Clock {

public long laterBy(long durationInMillis) {
Expand Down
Loading

0 comments on commit 4c93795

Please sign in to comment.