Skip to content

Commit bd925b7

Browse files
committed
Fix test and add static imports from TestUtils
1 parent 960ed56 commit bd925b7

File tree

1 file changed

+39
-36
lines changed

1 file changed

+39
-36
lines changed

firebase-appdistribution/src/test/java/com/google/firebase/appdistribution/impl/FirebaseAppDistributionServiceImplTest.java

Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
import static com.google.firebase.appdistribution.impl.ErrorMessages.NETWORK_ERROR;
2727
import static com.google.firebase.appdistribution.impl.ErrorMessages.RELEASE_NOT_FOUND_ERROR;
2828
import static com.google.firebase.appdistribution.impl.ErrorMessages.UPDATE_CANCELED;
29+
import static com.google.firebase.appdistribution.impl.TestUtils.awaitAsyncOperations;
30+
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTask;
31+
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTaskFailure;
32+
import static com.google.firebase.appdistribution.impl.TestUtils.awaitTermination;
33+
import static com.google.firebase.appdistribution.impl.TestUtils.mockForegroundActivity;
2934
import static org.junit.Assert.assertEquals;
3035
import static org.junit.Assert.assertFalse;
3136
import static org.junit.Assert.assertNull;
@@ -181,7 +186,7 @@ public void setup() throws FirebaseAppDistributionException {
181186
shadowPackageManager.installPackage(packageInfo);
182187

183188
activity = spy(Robolectric.buildActivity(TestActivity.class).create().get());
184-
TestUtils.mockForegroundActivity(mockLifecycleNotifier, activity);
189+
mockForegroundActivity(mockLifecycleNotifier, activity);
185190
}
186191

187192
@Test
@@ -194,7 +199,7 @@ public void checkForNewRelease_whenCheckForNewReleaseFails_throwsError() {
194199

195200
Task<AppDistributionRelease> task = firebaseAppDistribution.checkForNewRelease();
196201

197-
TestUtils.awaitTaskFailure(task, NETWORK_FAILURE, JSON_PARSING_ERROR);
202+
awaitTaskFailure(task, NETWORK_FAILURE, JSON_PARSING_ERROR);
198203
}
199204

200205
@Test
@@ -203,7 +208,7 @@ public void checkForNewRelease_testerIsNotSignedIn_taskFails() {
203208

204209
Task<AppDistributionRelease> task = firebaseAppDistribution.checkForNewRelease();
205210

206-
TestUtils.awaitTaskFailure(task, AUTHENTICATION_FAILURE, "Tester is not signed in");
211+
awaitTaskFailure(task, AUTHENTICATION_FAILURE, "Tester is not signed in");
207212
}
208213

209214
@Test
@@ -213,7 +218,7 @@ public void checkForNewRelease_whenCheckForNewReleaseSucceeds_returnsRelease()
213218
when(mockNewReleaseFetcher.checkForNewRelease()).thenReturn(Tasks.forResult(internalRelease));
214219

215220
Task<AppDistributionRelease> task = firebaseAppDistribution.checkForNewRelease();
216-
TestUtils.awaitTask(task);
221+
awaitTask(task);
217222

218223
assertEquals(TEST_RELEASE_NEWER_AAB, task.getResult());
219224
assertEquals(internalRelease, firebaseAppDistribution.getCachedNewRelease().getSnapshot());
@@ -228,8 +233,8 @@ public void checkForNewRelease_authenticationFailure_signOutTester() throws Inte
228233

229234
Task<AppDistributionRelease> task = firebaseAppDistribution.checkForNewRelease();
230235

231-
TestUtils.awaitTaskFailure(task, AUTHENTICATION_FAILURE, "Test");
232-
TestUtils.awaitTermination(testExecutor);
236+
awaitTaskFailure(task, AUTHENTICATION_FAILURE, "Test");
237+
awaitTermination(testExecutor);
233238
verify(mockSignInStorage, times(1)).setSignInStatus(false);
234239
}
235240

@@ -239,7 +244,7 @@ public void updateApp_whenNotSignedIn_throwsError() {
239244

240245
UpdateTask updateTask = firebaseAppDistribution.updateApp();
241246

242-
TestUtils.awaitTaskFailure(updateTask, AUTHENTICATION_FAILURE, "Tester is not signed in");
247+
awaitTaskFailure(updateTask, AUTHENTICATION_FAILURE, "Tester is not signed in");
243248
}
244249

245250
@Test
@@ -249,7 +254,7 @@ public void updateIfNewReleaseAvailable_whenNewAabReleaseAvailable_showsUpdateDi
249254
.thenReturn(Tasks.forResult((TEST_RELEASE_NEWER_AAB_INTERNAL)));
250255

251256
firebaseAppDistribution.updateIfNewReleaseAvailable();
252-
TestUtils.awaitAsyncOperations(testExecutor);
257+
awaitAsyncOperations(testExecutor);
253258

254259
AlertDialog dialog = assertAlertDialogShown();
255260
assertEquals(
@@ -269,7 +274,7 @@ public void updateIfNewReleaseAvailable_fromABackgroundThread_showsUpdateDialog(
269274

270275
ExecutorService executorService = Executors.newSingleThreadExecutor();
271276
executorService.submit(() -> firebaseAppDistribution.updateIfNewReleaseAvailable());
272-
TestUtils.awaitAsyncOperations(executorService);
277+
awaitAsyncOperations(executorService);
273278

274279
assertAlertDialogShown();
275280
}
@@ -283,7 +288,7 @@ public void updateIfNewReleaseAvailable_whenReleaseNotesEmpty_doesNotShowRelease
283288
(TEST_RELEASE_NEWER_AAB_INTERNAL.toBuilder().setReleaseNotes("").build())));
284289

285290
firebaseAppDistribution.updateIfNewReleaseAvailable();
286-
TestUtils.awaitAsyncOperations(testExecutor);
291+
awaitAsyncOperations(testExecutor);
287292

288293
AlertDialog dialog = assertAlertDialogShown();
289294
assertEquals(
@@ -302,7 +307,7 @@ public void updateIfNewReleaseAvailable_whenNoReleaseAvailable_updateDialogNotSh
302307

303308
List<UpdateProgress> progressEvents = new ArrayList<>();
304309
task.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents::add);
305-
TestUtils.awaitTermination(testExecutor);
310+
awaitTermination(testExecutor);
306311

307312
assertEquals(1, progressEvents.size());
308313
assertEquals(UpdateStatus.NEW_RELEASE_NOT_AVAILABLE, progressEvents.get(0).getUpdateStatus());
@@ -318,7 +323,7 @@ public void updateIfNewReleaseAvailable_whenActivityBackgrounded_updateDialogNot
318323

319324
List<UpdateProgress> progressEvents = new ArrayList<>();
320325
task.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents::add);
321-
TestUtils.awaitTermination(testExecutor);
326+
awaitTermination(testExecutor);
322327

323328
assertEquals(1, progressEvents.size());
324329
assertEquals(UpdateStatus.NEW_RELEASE_NOT_AVAILABLE, progressEvents.get(0).getUpdateStatus());
@@ -339,8 +344,7 @@ public void updateIfNewReleaseAvailable_whenSignInCancelled_checkForUpdateNotCal
339344
AlertDialog signInDialog = assertAlertDialogShown();
340345
signInDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
341346

342-
TestUtils.awaitTaskFailure(
343-
updateTask, AUTHENTICATION_CANCELED, ErrorMessages.AUTHENTICATION_CANCELED);
347+
awaitTaskFailure(updateTask, AUTHENTICATION_CANCELED, ErrorMessages.AUTHENTICATION_CANCELED);
344348
verify(mockTesterSignInManager, times(1)).signInTester();
345349
verify(mockNewReleaseFetcher, never()).checkForNewRelease();
346350
}
@@ -359,7 +363,7 @@ public void updateIfNewReleaseAvailable_whenSignInFailed_checkForUpdateNotCalled
359363
AlertDialog signInDialog = assertAlertDialogShown();
360364
signInDialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
361365

362-
TestUtils.awaitTaskFailure(updateTask, AUTHENTICATION_FAILURE, AUTHENTICATION_ERROR);
366+
awaitTaskFailure(updateTask, AUTHENTICATION_FAILURE, AUTHENTICATION_ERROR);
363367
}
364368

365369
@Test
@@ -369,12 +373,12 @@ public void updateIfNewReleaseAvailable_whenDialogDismissed_taskFails()
369373
.thenReturn(Tasks.forResult(TEST_RELEASE_NEWER_AAB_INTERNAL));
370374

371375
UpdateTask updateTask = firebaseAppDistribution.updateIfNewReleaseAvailable();
372-
TestUtils.awaitAsyncOperations(testExecutor);
376+
awaitAsyncOperations(testExecutor);
373377

374378
AlertDialog updateDialog = assertAlertDialogShown();
375379
updateDialog.getButton(AlertDialog.BUTTON_NEGATIVE).performClick(); // dismiss dialog
376380

377-
TestUtils.awaitTaskFailure(updateTask, INSTALLATION_CANCELED, UPDATE_CANCELED);
381+
awaitTaskFailure(updateTask, INSTALLATION_CANCELED, UPDATE_CANCELED);
378382
assertFalse(updateDialog.isShowing());
379383
}
380384

@@ -387,15 +391,15 @@ public void updateIfNewReleaseAvailable_whenDialogCanceled_taskFails()
387391
UpdateTask updateTask = firebaseAppDistribution.updateIfNewReleaseAvailable();
388392

389393
// Task callbacks happen on the executor
390-
TestUtils.awaitTermination(testExecutor);
394+
awaitTermination(testExecutor);
391395

392396
// Show update confirmation dialog happens on the UI thread
393397
shadowOf(getMainLooper()).idle();
394398

395399
AlertDialog updateDialog = assertAlertDialogShown();
396400
updateDialog.onBackPressed(); // cancels the dialog
397401

398-
TestUtils.awaitTaskFailure(updateTask, INSTALLATION_CANCELED, UPDATE_CANCELED);
402+
awaitTaskFailure(updateTask, INSTALLATION_CANCELED, UPDATE_CANCELED);
399403
assertFalse(updateDialog.isShowing());
400404
}
401405

@@ -412,7 +416,7 @@ public void updateIfNewReleaseAvailable_whenCheckForUpdateFails_updateAppNotCall
412416
List<UpdateProgress> progressEvents = new ArrayList<>();
413417
updateTask.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents::add);
414418

415-
TestUtils.awaitTaskFailure(updateTask, NETWORK_FAILURE, NETWORK_ERROR);
419+
awaitTaskFailure(updateTask, NETWORK_FAILURE, NETWORK_ERROR);
416420
assertEquals(1, progressEvents.size());
417421
assertEquals(UpdateStatus.NEW_RELEASE_CHECK_FAILED, progressEvents.get(0).getUpdateStatus());
418422
verify(firebaseAppDistribution, never()).updateApp();
@@ -425,7 +429,7 @@ public void updateIfNewReleaseAvailable_whenTesterIsSignedIn_doesNotOpenDialog()
425429
when(mockSignInStorage.getSignInStatus()).thenReturn(true);
426430

427431
UpdateTask task = firebaseAppDistribution.updateIfNewReleaseAvailable();
428-
TestUtils.awaitTask(task);
432+
awaitTask(task);
429433

430434
assertNull(ShadowAlertDialog.getLatestAlertDialog());
431435
}
@@ -438,8 +442,7 @@ public void signInTester_whenDialogDismissed_taskFails() {
438442
AlertDialog dialog = assertAlertDialogShown();
439443
dialog.getButton(AlertDialog.BUTTON_NEGATIVE).performClick(); // dismiss dialog
440444

441-
TestUtils.awaitTaskFailure(
442-
updateTask, AUTHENTICATION_CANCELED, ErrorMessages.AUTHENTICATION_CANCELED);
445+
awaitTaskFailure(updateTask, AUTHENTICATION_CANCELED, ErrorMessages.AUTHENTICATION_CANCELED);
443446
}
444447

445448
@Test
@@ -450,8 +453,7 @@ public void updateIfNewReleaseAvailable_whenSignInDialogCanceled_taskFails() {
450453
AlertDialog dialog = assertAlertDialogShown();
451454
dialog.onBackPressed(); // cancel dialog
452455

453-
TestUtils.awaitTaskFailure(
454-
signInTask, AUTHENTICATION_CANCELED, ErrorMessages.AUTHENTICATION_CANCELED);
456+
awaitTaskFailure(signInTask, AUTHENTICATION_CANCELED, ErrorMessages.AUTHENTICATION_CANCELED);
455457
}
456458

457459
private AlertDialog assertAlertDialogShown() {
@@ -463,8 +465,9 @@ private AlertDialog assertAlertDialogShown() {
463465
}
464466

465467
@Test
466-
public void signOutTester_setsSignInStatusFalse() {
468+
public void signOutTester_setsSignInStatusFalse() throws InterruptedException {
467469
firebaseAppDistribution.signOutTester();
470+
awaitTermination(testExecutor);
468471
verify(mockSignInStorage).setSignInStatus(false);
469472
}
470473

@@ -486,12 +489,12 @@ public void updateIfNewReleaseAvailable_receiveProgressUpdateFromUpdateApp()
486489

487490
List<UpdateProgress> progressEvents = new ArrayList<>();
488491
updateTask.addOnProgressListener(FirebaseExecutors.directExecutor(), progressEvents::add);
489-
TestUtils.awaitAsyncOperations(testExecutor);
492+
awaitAsyncOperations(testExecutor);
490493

491494
// Clicking the update button.
492495
AlertDialog updateDialog = (AlertDialog) ShadowAlertDialog.getLatestDialog();
493496
updateDialog.getButton(Dialog.BUTTON_POSITIVE).performClick();
494-
TestUtils.awaitAsyncOperations(testExecutor);
497+
awaitAsyncOperations(testExecutor);
495498

496499
// Update flow
497500
assertEquals(1, progressEvents.size());
@@ -506,7 +509,7 @@ public void updateIfNewReleaseAvailable_fromABackgroundThread_showsSignInDialog(
506509
ExecutorService executorService = Executors.newSingleThreadExecutor();
507510
Future<UpdateTask> future =
508511
executorService.submit(() -> firebaseAppDistribution.updateIfNewReleaseAvailable());
509-
TestUtils.awaitAsyncOperations(executorService);
512+
awaitAsyncOperations(executorService);
510513

511514
assertAlertDialogShown();
512515
assertFalse(((UpdateTask) future.get()).isComplete());
@@ -536,7 +539,7 @@ public void updateIfNewReleaseAvailable_whenScreenRotates_updateDialogReappears(
536539
when(activity.isChangingConfigurations()).thenReturn(true);
537540

538541
UpdateTask updateTask = firebaseAppDistribution.updateIfNewReleaseAvailable();
539-
TestUtils.awaitAsyncOperations(testExecutor);
542+
awaitAsyncOperations(testExecutor);
540543

541544
// Mimic activity recreation due to a configuration change
542545
firebaseAppDistribution.onActivityDestroyed(activity);
@@ -558,7 +561,7 @@ public void updateIfNewReleaseAvailable_whenScreenRotates_updateDialogReappears(
558561
firebaseAppDistribution.onActivityPaused(activity);
559562
firebaseAppDistribution.onActivityResumed(testActivity2);
560563

561-
TestUtils.awaitTaskFailure(
564+
awaitTaskFailure(
562565
updateTask, HOST_ACTIVITY_INTERRUPTED, ErrorMessages.HOST_ACTIVITY_INTERRUPTED);
563566
}
564567

@@ -571,13 +574,13 @@ public void updateIfNewReleaseAvailable_whenScreenRotates_updateDialogReappears(
571574
when(mockNewReleaseFetcher.checkForNewRelease()).thenReturn(Tasks.forResult(newRelease));
572575

573576
UpdateTask updateTask = firebaseAppDistribution.updateIfNewReleaseAvailable();
574-
TestUtils.awaitAsyncOperations(testExecutor);
577+
awaitAsyncOperations(testExecutor);
575578

576579
// Mimic different activity getting resumed
577580
firebaseAppDistribution.onActivityPaused(activity);
578581
firebaseAppDistribution.onActivityResumed(testActivity2);
579582

580-
TestUtils.awaitTaskFailure(
583+
awaitTaskFailure(
581584
updateTask, HOST_ACTIVITY_INTERRUPTED, ErrorMessages.HOST_ACTIVITY_INTERRUPTED);
582585
}
583586

@@ -588,7 +591,7 @@ public void updateAppTask_whenNoReleaseAvailable_throwsError() {
588591

589592
UpdateTask updateTask = firebaseAppDistribution.updateApp();
590593

591-
TestUtils.awaitTaskFailure(updateTask, UPDATE_NOT_AVAILABLE, RELEASE_NOT_FOUND_ERROR);
594+
awaitTaskFailure(updateTask, UPDATE_NOT_AVAILABLE, RELEASE_NOT_FOUND_ERROR);
592595
}
593596

594597
@Test
@@ -605,7 +608,7 @@ public void updateApp_withAabReleaseAvailable_returnsSameAabTask()
605608

606609
// Complete original task
607610
updateTaskToReturn.setResult();
608-
TestUtils.awaitTask(updateTask);
611+
awaitTask(updateTask);
609612

610613
// Returned task is complete
611614
assertTrue(updateTask.isSuccessful());
@@ -625,7 +628,7 @@ public void updateApp_withApkReleaseAvailable_returnsSameApkTask()
625628

626629
// Complete original task
627630
updateTaskToReturn.setResult();
628-
TestUtils.awaitTask(updateTask);
631+
awaitTask(updateTask);
629632

630633
// Returned task is complete
631634
assertTrue(updateTask.isSuccessful());

0 commit comments

Comments
 (0)