-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
EspressoIdlingResourceTest.java
181 lines (156 loc) · 4.98 KB
/
EspressoIdlingResourceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package org.robolectric.integrationtests.axt;
import static androidx.test.espresso.Espresso.onIdle;
import static com.google.common.truth.Truth.assertThat;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import androidx.test.espresso.IdlingRegistry;
import androidx.test.espresso.IdlingResource;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
/** Test Espresso IdlingResource support. */
@RunWith(AndroidJUnit4.class)
public final class EspressoIdlingResourceTest {
private final IdlingRegistry idlingRegistry = IdlingRegistry.getInstance();
private ExecutorService executor;
@Before
public void setup() {
executor = Executors.newSingleThreadExecutor();
}
@After
public void teardown() {
for (IdlingResource resource : idlingRegistry.getResources()) {
idlingRegistry.unregister(resource);
}
for (Looper looper : idlingRegistry.getLoopers()) {
idlingRegistry.unregisterLooperAsIdlingResource(looper);
}
executor.shutdown();
}
@Test
public void onIdle_idlingResourceIsIdle_doesntBlock() {
AtomicBoolean didCheckIdle = new AtomicBoolean();
idlingRegistry.register(
new NamedIdleResource("Test", /* isIdle= */ true) {
@Override
public boolean isIdleNow() {
didCheckIdle.set(true);
return super.isIdleNow();
}
});
onIdle();
assertThat(didCheckIdle.get()).isTrue();
}
@SuppressWarnings("FutureReturnValueIgnored")
@Test
public void onIdle_postToMainThread() {
idlingRegistry.register(
new NamedIdleResource("Test", /* isIdle= */ false) {
boolean submitted;
@Override
public boolean isIdleNow() {
if (!submitted) {
submitted = true;
executor.submit(this::postToMainLooper);
}
return super.isIdleNow();
}
void postToMainLooper() {
new Handler(Looper.getMainLooper()).post(() -> setIdle(true));
}
});
onIdle();
}
@SuppressWarnings("FutureReturnValueIgnored")
@Test
public void onIdle_cooperativeResources() {
NamedIdleResource a = new NamedIdleResource("A", /* isIdle= */ true);
NamedIdleResource b = new NamedIdleResource("B", /* isIdle= */ false);
NamedIdleResource c = new NamedIdleResource("C", /* isIdle= */ false);
idlingRegistry.register(a, b, c);
executor.submit(
() -> {
a.setIdle(false);
b.setIdle(true);
c.setIdle(false);
executor.submit(
() -> {
a.setIdle(true);
b.setIdle(false);
c.setIdle(false);
executor.submit(
() -> {
a.setIdle(true);
b.setIdle(true);
c.setIdle(true);
});
});
});
onIdle();
assertThat(a.isIdleNow()).isTrue();
assertThat(b.isIdleNow()).isTrue();
assertThat(c.isIdleNow()).isTrue();
}
@SuppressWarnings("FutureReturnValueIgnored")
@Test
public void onIdle_looperIsIdle() throws Exception {
HandlerThread handlerThread = new HandlerThread("Test");
try {
handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
CountDownLatch handlerStarted = new CountDownLatch(1);
CountDownLatch releaseHandler = new CountDownLatch(1);
handler.post(
() -> {
handlerStarted.countDown();
try {
releaseHandler.await();
} catch (InterruptedException e) {
// ignore
}
});
handlerStarted.await();
idlingRegistry.registerLooperAsIdlingResource(handlerThread.getLooper());
executor.submit(releaseHandler::countDown);
onIdle();
// onIdle should have blocked on the looper waiting on the release latch
assertThat(releaseHandler.getCount()).isEqualTo(0);
} finally {
handlerThread.quit();
}
}
private static class NamedIdleResource implements IdlingResource {
final String name;
final AtomicBoolean isIdle;
ResourceCallback callback;
NamedIdleResource(String name, boolean isIdle) {
this.name = name;
this.isIdle = new AtomicBoolean(isIdle);
}
@Override
public String getName() {
return name;
}
@Override
public boolean isIdleNow() {
return isIdle.get();
}
void setIdle(boolean isIdle) {
this.isIdle.set(isIdle);
if (isIdle && callback != null) {
callback.onTransitionToIdle();
}
}
@Override
public void registerIdleTransitionCallback(ResourceCallback callback) {
this.callback = callback;
}
}
}