17
17
import static android .os .Looper .getMainLooper ;
18
18
import static com .google .common .truth .Truth .assertThat ;
19
19
import static com .google .firebase .appdistribution .BinaryType .APK ;
20
+ import static com .google .firebase .appdistribution .impl .TestUtils .awaitTask ;
21
+ import static com .google .firebase .appdistribution .impl .TestUtils .awaitTaskFailure ;
20
22
import static org .junit .Assert .assertEquals ;
21
23
import static org .mockito .Mockito .spy ;
24
+ import static org .mockito .Mockito .times ;
25
+ import static org .mockito .Mockito .verify ;
22
26
import static org .mockito .Mockito .when ;
23
27
import static org .robolectric .Shadows .shadowOf ;
24
28
28
32
import com .google .android .gms .tasks .Task ;
29
33
import com .google .android .gms .tasks .TaskCompletionSource ;
30
34
import com .google .android .gms .tasks .Tasks ;
35
+ import com .google .firebase .annotations .concurrent .Lightweight ;
31
36
import com .google .firebase .appdistribution .BinaryType ;
32
37
import com .google .firebase .appdistribution .FirebaseAppDistributionException ;
33
38
import com .google .firebase .appdistribution .FirebaseAppDistributionException .Status ;
39
+ import com .google .firebase .concurrent .TestOnlyExecutors ;
40
+ import java .util .concurrent .ExecutionException ;
34
41
import java .util .concurrent .Executor ;
35
- import java .util .concurrent .Executors ;
36
42
import org .junit .Before ;
37
43
import org .junit .Test ;
38
44
import org .junit .runner .RunWith ;
@@ -58,7 +64,7 @@ public class NewReleaseFetcherTest {
58
64
@ Mock private FirebaseAppDistributionTesterApiClient mockFirebaseAppDistributionTesterApiClient ;
59
65
@ Mock private ReleaseIdentifier mockReleaseIdentifier ;
60
66
61
- Executor testExecutor = Executors . newSingleThreadExecutor ();
67
+ @ Lightweight Executor lightweightExecutor = TestOnlyExecutors . lite ();
62
68
63
69
@ Before
64
70
public void setup () throws FirebaseAppDistributionException {
@@ -84,11 +90,13 @@ public void setup() throws FirebaseAppDistributionException {
84
90
new NewReleaseFetcher (
85
91
ApplicationProvider .getApplicationContext (),
86
92
mockFirebaseAppDistributionTesterApiClient ,
87
- mockReleaseIdentifier ));
93
+ mockReleaseIdentifier ,
94
+ lightweightExecutor ));
88
95
}
89
96
90
97
@ Test
91
- public void checkForNewRelease_whenCalledMultipleTimes_returnsTheSameTask () {
98
+ public void checkForNewRelease_whenCalledMultipleTimes_onlyFetchesReleasesOnce ()
99
+ throws FirebaseAppDistributionException , ExecutionException , InterruptedException {
92
100
// Start a new task completion source and do not complete it, to mimic an in progress task
93
101
TaskCompletionSource <AppDistributionReleaseInternal > task = new TaskCompletionSource <>();
94
102
when (mockFirebaseAppDistributionTesterApiClient .fetchNewRelease ()).thenReturn (task .getTask ());
@@ -98,7 +106,13 @@ public void checkForNewRelease_whenCalledMultipleTimes_returnsTheSameTask() {
98
106
Task <AppDistributionReleaseInternal > checkForNewReleaseTask2 =
99
107
newReleaseFetcher .checkForNewRelease ();
100
108
101
- assertEquals (checkForNewReleaseTask1 , checkForNewReleaseTask2 );
109
+ // Don't set the result until after calling twice, to make sure that the task from the first
110
+ // call is still ongoing.
111
+ task .setResult (null );
112
+ awaitTask (checkForNewReleaseTask1 );
113
+ awaitTask (checkForNewReleaseTask2 );
114
+
115
+ verify (mockFirebaseAppDistributionTesterApiClient , times (1 )).fetchNewRelease ();
102
116
}
103
117
104
118
@ Test
@@ -107,7 +121,7 @@ public void checkForNewRelease_newApkReleaseIsAvailable_returnsRelease() throws
107
121
.thenReturn (Tasks .forResult (getTestNewRelease ().build ()));
108
122
109
123
Task <AppDistributionReleaseInternal > task = newReleaseFetcher .checkForNewRelease ();
110
- AppDistributionReleaseInternal appDistributionReleaseInternal = TestUtils . awaitTask (task );
124
+ AppDistributionReleaseInternal appDistributionReleaseInternal = awaitTask (task );
111
125
112
126
assertEquals (getTestNewRelease ().build (), appDistributionReleaseInternal );
113
127
}
@@ -123,23 +137,23 @@ public void checkForNewRelease_apiClientFailure() {
123
137
Task <AppDistributionReleaseInternal > task = newReleaseFetcher .checkForNewRelease ();
124
138
shadowOf (getMainLooper ()).idle ();
125
139
126
- assertThat (task .isSuccessful ()).isFalse ();
127
- assertThat (task .getException ()).isEqualTo (expectedException );
140
+ awaitTaskFailure (task , FirebaseAppDistributionException .Status .UNKNOWN , "test" );
128
141
}
129
142
130
143
@ Test
131
- public void checkForNewRelease_whenNewReleaseIsSameRelease_returnsNull () {
144
+ public void checkForNewRelease_whenNewReleaseIsSameRelease_returnsNull ()
145
+ throws FirebaseAppDistributionException , ExecutionException , InterruptedException {
132
146
when (mockFirebaseAppDistributionTesterApiClient .fetchNewRelease ())
133
147
.thenReturn (Tasks .forResult (getTestInstalledRelease ().build ()));
134
148
135
149
Task <AppDistributionReleaseInternal > releaseTask = newReleaseFetcher .checkForNewRelease ();
136
- shadowOf (getMainLooper ()).idle ();
137
150
138
- assertThat (releaseTask . getResult ( )).isNull ();
151
+ assertThat (awaitTask ( releaseTask )).isNull ();
139
152
}
140
153
141
154
@ Test
142
- public void checkForNewRelease_whenNewReleaseIsLowerVersionCode_returnsNull () {
155
+ public void checkForNewRelease_whenNewReleaseIsLowerVersionCode_returnsNull ()
156
+ throws FirebaseAppDistributionException , ExecutionException , InterruptedException {
143
157
when (mockFirebaseAppDistributionTesterApiClient .fetchNewRelease ())
144
158
.thenReturn (
145
159
Tasks .forResult (
@@ -148,14 +162,13 @@ public void checkForNewRelease_whenNewReleaseIsLowerVersionCode_returnsNull() {
148
162
.build ()));
149
163
150
164
Task <AppDistributionReleaseInternal > releaseTask = newReleaseFetcher .checkForNewRelease ();
151
- shadowOf (getMainLooper ()).idle ();
152
165
153
- assertThat (releaseTask .isSuccessful ()).isTrue ();
154
- assertThat (releaseTask .getResult ()).isNull ();
166
+ assertThat (awaitTask (releaseTask )).isNull ();
155
167
}
156
168
157
169
@ Test
158
- public void checkForNewRelease_whenNewAabIsAvailable_returnsRelease () {
170
+ public void checkForNewRelease_whenNewAabIsAvailable_returnsRelease ()
171
+ throws FirebaseAppDistributionException , ExecutionException , InterruptedException {
159
172
AppDistributionReleaseInternal expectedRelease =
160
173
getTestNewRelease ()
161
174
.setDownloadUrl ("http://fake-download-url" )
@@ -166,14 +179,13 @@ public void checkForNewRelease_whenNewAabIsAvailable_returnsRelease() {
166
179
.thenReturn (Tasks .forResult (expectedRelease ));
167
180
168
181
Task <AppDistributionReleaseInternal > result = newReleaseFetcher .checkForNewRelease ();
169
- shadowOf (getMainLooper ()).idle ();
170
182
171
- assertThat (result .isSuccessful ()).isTrue ();
172
- assertThat (result .getResult ()).isEqualTo (expectedRelease );
183
+ assertThat (awaitTask (result )).isEqualTo (expectedRelease );
173
184
}
174
185
175
186
@ Test
176
- public void checkForNewRelease_differentVersionNameThanInstalled_returnsRelease () {
187
+ public void checkForNewRelease_differentVersionNameThanInstalled_returnsRelease ()
188
+ throws FirebaseAppDistributionException , ExecutionException , InterruptedException {
177
189
AppDistributionReleaseInternal expectedRelease =
178
190
getTestNewRelease ()
179
191
.setDownloadUrl ("http://fake-download-url" )
@@ -185,14 +197,13 @@ public void checkForNewRelease_differentVersionNameThanInstalled_returnsRelease(
185
197
.thenReturn (Tasks .forResult (expectedRelease ));
186
198
187
199
Task <AppDistributionReleaseInternal > result = newReleaseFetcher .checkForNewRelease ();
188
- shadowOf (getMainLooper ()).idle ();
189
200
190
- assertThat (result .isSuccessful ()).isTrue ();
191
- assertThat (result .getResult ()).isEqualTo (expectedRelease );
201
+ assertThat (awaitTask (result )).isEqualTo (expectedRelease );
192
202
}
193
203
194
204
@ Test
195
- public void checkForNewRelease_whenNewReleaseIsSameAsInstalledAab_returnsNull () {
205
+ public void checkForNewRelease_whenNewReleaseIsSameAsInstalledAab_returnsNull ()
206
+ throws FirebaseAppDistributionException , ExecutionException , InterruptedException {
196
207
when (mockFirebaseAppDistributionTesterApiClient .fetchNewRelease ())
197
208
.thenReturn (
198
209
Tasks .forResult (
@@ -203,10 +214,8 @@ public void checkForNewRelease_whenNewReleaseIsSameAsInstalledAab_returnsNull()
203
214
.build ()));
204
215
205
216
Task <AppDistributionReleaseInternal > result = newReleaseFetcher .checkForNewRelease ();
206
- shadowOf (getMainLooper ()).idle ();
207
217
208
- assertThat (result .isSuccessful ()).isTrue ();
209
- assertThat (result .getResult ()).isNull ();
218
+ assertThat (awaitTask (result )).isNull ();
210
219
}
211
220
212
221
@ Test
@@ -216,7 +225,7 @@ public void checkForNewRelease_onlyDifferenceIsMissingApkHash_throwsError() {
216
225
217
226
Task <AppDistributionReleaseInternal > task = newReleaseFetcher .checkForNewRelease ();
218
227
219
- TestUtils . awaitTaskFailure (task , Status .UNKNOWN , "Missing APK hash" );
228
+ awaitTaskFailure (task , Status .UNKNOWN , "Missing APK hash" );
220
229
}
221
230
222
231
private AppDistributionReleaseInternal .Builder getTestNewRelease () {
0 commit comments