Skip to content

Commit 94f6e23

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Use SystemClock.uptimeMillis instead of System.currentTimeMillis in RootViewPicker.
The former is resilient to clock updates, and is better suited for Robolectric that uses a controlled SystemClock. This should fix slow inRoot calls on Robolectric for non existent roots. PiperOrigin-RevId: 602436682
1 parent 91ce263 commit 94f6e23

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

espresso/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ The following artifacts were released:
1616

1717
**Bug Fixes**
1818

19+
* Fix slow inRoot operations in Robolectric
20+
1921
**New Features**
2022

2123
**Breaking Changes**

espresso/core/java/androidx/test/espresso/base/RootViewPicker.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
2020
import static androidx.test.internal.util.Checks.checkState;
2121
import static java.util.Collections.unmodifiableList;
22+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
23+
import static java.util.concurrent.TimeUnit.SECONDS;
2224

2325
import android.app.Activity;
2426
import android.content.Context;
2527
import android.os.Looper;
28+
import android.os.SystemClock;
2629
import android.util.Log;
2730
import android.view.View;
2831
import androidx.test.espresso.EspressoException;
@@ -103,9 +106,9 @@ public View get() {
103106
* root view is not being requested and the root view has window focus or is focusable.
104107
*/
105108
private Root waitForRootToBeReady(Root pickedRoot) {
106-
long timeout = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(10) /* 10 seconds */;
109+
long timeout = SystemClock.uptimeMillis() + SECONDS.toMillis(10) /* 10 seconds */;
107110
BackOff rootReadyBackoff = new RootReadyBackoff();
108-
while (System.currentTimeMillis() <= timeout) {
111+
while (SystemClock.uptimeMillis() <= timeout) {
109112
if (pickedRoot.isReady()) {
110113
return pickedRoot;
111114
} else {
@@ -125,11 +128,11 @@ private Root waitForRootToBeReady(Root pickedRoot) {
125128
}
126129

127130
private Root pickARoot() {
128-
long timeout = System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(60) /* 60 seconds */;
131+
long timeout = SystemClock.uptimeMillis() + SECONDS.toMillis(60) /* 60 seconds */;
129132
RootResults rootResults = rootResultFetcher.fetch();
130133
BackOff noActiveRootsBackoff = new NoActiveRootsBackoff();
131134
BackOff noMatchingRootBackoff = new NoMatchingRootBackoff();
132-
while (System.currentTimeMillis() <= timeout) {
135+
while (SystemClock.uptimeMillis() <= timeout) {
133136
switch (rootResults.getState()) {
134137
case ROOTS_PICKED:
135138
return rootResults.getPickedRoot();
@@ -341,7 +344,7 @@ private static final class NoActiveRootsBackoff extends BackOff {
341344
unmodifiableList(Arrays.asList(10, 10, 20, 30, 50, 80, 130, 210, 340));
342345

343346
public NoActiveRootsBackoff() {
344-
super(NO_ACTIVE_ROOTS_BACKOFF, TimeUnit.MILLISECONDS);
347+
super(NO_ACTIVE_ROOTS_BACKOFF, MILLISECONDS);
345348
}
346349

347350
@Override
@@ -358,7 +361,7 @@ private static final class NoMatchingRootBackoff extends BackOff {
358361
unmodifiableList(Arrays.asList(10, 20, 200, 400, 1000, 2000 /* 2sec */));
359362

360363
public NoMatchingRootBackoff() {
361-
super(NO_MATCHING_ROOT_BACKOFF, TimeUnit.MILLISECONDS);
364+
super(NO_MATCHING_ROOT_BACKOFF, MILLISECONDS);
362365
}
363366

364367
@Override
@@ -379,7 +382,7 @@ private static final class RootReadyBackoff extends BackOff {
379382
unmodifiableList(Arrays.asList(10, 25, 50, 100, 200, 400, 800, 1000 /* 1sec */));
380383

381384
public RootReadyBackoff() {
382-
super(ROOT_READY_BACKOFF, TimeUnit.MILLISECONDS);
385+
super(ROOT_READY_BACKOFF, MILLISECONDS);
383386
}
384387

385388
@Override

0 commit comments

Comments
 (0)