diff --git a/android/guava-testlib/src/com/google/common/testing/FakeTicker.java b/android/guava-testlib/src/com/google/common/testing/FakeTicker.java index 8c0e0026e0ce..3fe3f96c6ab2 100644 --- a/android/guava-testlib/src/com/google/common/testing/FakeTicker.java +++ b/android/guava-testlib/src/com/google/common/testing/FakeTicker.java @@ -18,9 +18,13 @@ import static com.google.common.base.Preconditions.checkArgument; +import com.google.common.annotations.Beta; import com.google.common.annotations.GwtCompatible; +import com.google.common.annotations.GwtIncompatible; +import com.google.common.annotations.J2ktIncompatible; import com.google.common.base.Ticker; import com.google.errorprone.annotations.CanIgnoreReturnValue; +import java.time.Duration; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; @@ -57,6 +61,22 @@ public FakeTicker advance(long nanoseconds) { return this; } + /** + * Advances the ticker value by {@code duration}. + * + * @since NEXT (but since 28.0 in the JRE flavor) + */ + @GwtIncompatible + @J2ktIncompatible + @CanIgnoreReturnValue + @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now. + @IgnoreJRERequirement // TODO: b/288085449 - Remove this once we use library-desugaring scents. + @Beta // TODO: b/288085449 - Remove @Beta after we're sure that Java 8 APIs are safe for Android + public FakeTicker advance(Duration duration) { + return advance(duration.toNanos()); + } + /** * Sets the increment applied to the ticker whenever it is queried. * @@ -71,6 +91,25 @@ public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit return this; } + /** + * Sets the increment applied to the ticker whenever it is queried. + * + *
The default behavior is to auto increment by zero. i.e: The ticker is left unchanged when + * queried. + * + * @since NEXT (but since 28.0 in the JRE flavor) + */ + @GwtIncompatible + @J2ktIncompatible + @CanIgnoreReturnValue + @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now. + @IgnoreJRERequirement // TODO: b/288085449 - Remove this once we use library-desugaring scents. + @Beta // TODO: b/288085449 - Remove @Beta after we're sure that Java 8 APIs are safe for Android + public FakeTicker setAutoIncrementStep(Duration autoIncrementStep) { + return setAutoIncrementStep(autoIncrementStep.toNanos(), TimeUnit.NANOSECONDS); + } + @Override public long read() { return nanos.getAndAdd(autoIncrementStepNanos); diff --git a/android/guava-testlib/test/com/google/common/testing/FakeTickerTest.java b/android/guava-testlib/test/com/google/common/testing/FakeTickerTest.java index 5810524d071f..366dcb4ccead 100644 --- a/android/guava-testlib/test/com/google/common/testing/FakeTickerTest.java +++ b/android/guava-testlib/test/com/google/common/testing/FakeTickerTest.java @@ -18,6 +18,7 @@ import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; +import java.time.Duration; import java.util.EnumSet; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; @@ -42,6 +43,9 @@ public void testNullPointerExceptions() { tester.testAllPublicInstanceMethods(new FakeTicker()); } + @GwtIncompatible // java.time.Duration + @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now. + @IgnoreJRERequirement // TODO: b/288085449 - Remove this once we use library-desugaring scents. public void testAdvance() { FakeTicker ticker = new FakeTicker(); assertEquals(0, ticker.read()); @@ -49,6 +53,8 @@ public void testAdvance() { assertEquals(10, ticker.read()); ticker.advance(1, TimeUnit.MILLISECONDS); assertEquals(1000010L, ticker.read()); + ticker.advance(Duration.ofMillis(1)); + assertEquals(2000010L, ticker.read()); } public void testAutoIncrementStep_returnsSameInstance() { @@ -77,6 +83,16 @@ public void testAutoIncrementStep_seconds() { assertEquals(6000000000L, ticker.read()); } + @GwtIncompatible // java.time.Duration + @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now. + @IgnoreJRERequirement // TODO: b/288085449 - Remove this once we use library-desugaring scents. + public void testAutoIncrementStep_duration() { + FakeTicker ticker = new FakeTicker().setAutoIncrementStep(Duration.ofMillis(1)); + assertEquals(0, ticker.read()); + assertEquals(1000000, ticker.read()); + assertEquals(2000000, ticker.read()); + } + public void testAutoIncrementStep_resetToZero() { FakeTicker ticker = new FakeTicker().setAutoIncrementStep(10, TimeUnit.NANOSECONDS); assertEquals(0, ticker.read()); diff --git a/guava-testlib/src/com/google/common/testing/FakeTicker.java b/guava-testlib/src/com/google/common/testing/FakeTicker.java index 2c3e798ff83c..5849057da2ca 100644 --- a/guava-testlib/src/com/google/common/testing/FakeTicker.java +++ b/guava-testlib/src/com/google/common/testing/FakeTicker.java @@ -23,6 +23,7 @@ import com.google.common.annotations.J2ktIncompatible; import com.google.common.base.Ticker; import com.google.errorprone.annotations.CanIgnoreReturnValue; +import java.time.Duration; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; @@ -67,7 +68,8 @@ public FakeTicker advance(long nanoseconds) { @GwtIncompatible @J2ktIncompatible @CanIgnoreReturnValue - public FakeTicker advance(java.time.Duration duration) { + @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now. + public FakeTicker advance(Duration duration) { return advance(duration.toNanos()); } @@ -96,7 +98,8 @@ public FakeTicker setAutoIncrementStep(long autoIncrementStep, TimeUnit timeUnit @GwtIncompatible @J2ktIncompatible @CanIgnoreReturnValue - public FakeTicker setAutoIncrementStep(java.time.Duration autoIncrementStep) { + @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now. + public FakeTicker setAutoIncrementStep(Duration autoIncrementStep) { return setAutoIncrementStep(autoIncrementStep.toNanos(), TimeUnit.NANOSECONDS); } diff --git a/guava-testlib/test/com/google/common/testing/FakeTickerTest.java b/guava-testlib/test/com/google/common/testing/FakeTickerTest.java index 5120119db1bc..2d0da3098aa2 100644 --- a/guava-testlib/test/com/google/common/testing/FakeTickerTest.java +++ b/guava-testlib/test/com/google/common/testing/FakeTickerTest.java @@ -18,6 +18,7 @@ import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; +import java.time.Duration; import java.util.EnumSet; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; @@ -43,6 +44,7 @@ public void testNullPointerExceptions() { } @GwtIncompatible // java.time.Duration + @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now. public void testAdvance() { FakeTicker ticker = new FakeTicker(); assertEquals(0, ticker.read()); @@ -50,7 +52,7 @@ public void testAdvance() { assertEquals(10, ticker.read()); ticker.advance(1, TimeUnit.MILLISECONDS); assertEquals(1000010L, ticker.read()); - ticker.advance(java.time.Duration.ofMillis(1)); + ticker.advance(Duration.ofMillis(1)); assertEquals(2000010L, ticker.read()); } @@ -81,8 +83,9 @@ public void testAutoIncrementStep_seconds() { } @GwtIncompatible // java.time.Duration + @SuppressWarnings("Java7ApiChecker") // guava-android can rely on library desugaring now. public void testAutoIncrementStep_duration() { - FakeTicker ticker = new FakeTicker().setAutoIncrementStep(java.time.Duration.ofMillis(1)); + FakeTicker ticker = new FakeTicker().setAutoIncrementStep(Duration.ofMillis(1)); assertEquals(0, ticker.read()); assertEquals(1000000, ticker.read()); assertEquals(2000000, ticker.read());