18
18
import static com .google .firebase .app .distribution .FirebaseAppDistributionException .Status .AUTHENTICATION_FAILURE ;
19
19
import static com .google .firebase .app .distribution .FirebaseAppDistributionException .Status .INSTALLATION_CANCELED ;
20
20
import static org .junit .Assert .assertEquals ;
21
+ import static org .junit .Assert .assertFalse ;
21
22
import static org .junit .Assert .assertNotNull ;
22
23
import static org .junit .Assert .assertNull ;
23
24
import static org .junit .Assert .assertTrue ;
25
+ import static org .mockito .Mockito .doReturn ;
24
26
import static org .mockito .Mockito .never ;
25
27
import static org .mockito .Mockito .spy ;
26
28
import static org .mockito .Mockito .times ;
48
50
import com .google .firebase .installations .InstallationTokenResult ;
49
51
import java .util .ArrayList ;
50
52
import java .util .List ;
53
+ import java .util .concurrent .Executor ;
54
+ import java .util .concurrent .Executors ;
51
55
import org .junit .Before ;
52
56
import org .junit .Test ;
53
57
import org .junit .runner .RunWith ;
54
58
import org .mockito .Mock ;
59
+ import org .mockito .Mockito ;
55
60
import org .mockito .MockitoAnnotations ;
56
61
import org .robolectric .Robolectric ;
57
62
import org .robolectric .RobolectricTestRunner ;
@@ -66,6 +71,7 @@ public class FirebaseAppDistributionTest {
66
71
private static final String TEST_AUTH_TOKEN = "fad.auth.token" ;
67
72
private static final String TEST_IAS_ARTIFACT_ID = "ias-artifact-id" ;
68
73
private static final String IAS_ARTIFACT_ID_KEY = "com.android.vending.internal.apk.id" ;
74
+ private static final String TEST_URL = "https://test-url" ;
69
75
private static final long INSTALLED_VERSION_CODE = 2 ;
70
76
71
77
private static final AppDistributionReleaseInternal .Builder TEST_RELEASE_NEWER_AAB_INTERNAL =
@@ -74,7 +80,7 @@ public class FirebaseAppDistributionTest {
74
80
.setDisplayVersion ("3.0" )
75
81
.setReleaseNotes ("Newer version." )
76
82
.setBinaryType (BinaryType .AAB )
77
- .setDownloadUrl ("https://test-url" );
83
+ .setDownloadUrl (TEST_URL );
78
84
79
85
private static final AppDistributionRelease TEST_RELEASE_NEWER_AAB =
80
86
AppDistributionRelease .builder ()
@@ -84,16 +90,28 @@ public class FirebaseAppDistributionTest {
84
90
.setBinaryType (BinaryType .AAB )
85
91
.build ();
86
92
93
+ private static final AppDistributionReleaseInternal .Builder TEST_RELEASE_NEWER_APK_INTERNAL =
94
+ AppDistributionReleaseInternal .builder ()
95
+ .setBuildVersion ("3" )
96
+ .setDisplayVersion ("3.0" )
97
+ .setReleaseNotes ("Newer version." )
98
+ .setBinaryType (BinaryType .APK )
99
+ .setDownloadUrl (TEST_URL );
100
+
87
101
private FirebaseAppDistribution firebaseAppDistribution ;
88
102
private TestActivity activity ;
89
103
90
104
@ Mock private InstallationTokenResult mockInstallationTokenResult ;
91
105
@ Mock private TesterSignInClient mockTesterSignInClient ;
92
106
@ Mock private CheckForNewReleaseClient mockCheckForNewReleaseClient ;
93
- @ Mock private UpdateAppClient mockUpdateAppClient ;
107
+ @ Mock private InstallApkClient mockInstallApkClient ;
108
+ private UpdateApkClient mockUpdateApkClient ;
109
+ @ Mock private UpdateAabClient mockUpdateAabClient ;
94
110
@ Mock private SignInStorage mockSignInStorage ;
95
111
@ Mock private FirebaseAppDistributionLifecycleNotifier mockLifecycleNotifier ;
96
112
113
+ Executor testExecutor = Executors .newSingleThreadExecutor ();
114
+
97
115
static class TestActivity extends Activity {}
98
116
99
117
@ Before
@@ -112,13 +130,17 @@ public void setup() {
112
130
.setApiKey (TEST_API_KEY )
113
131
.build ());
114
132
133
+ this .mockUpdateApkClient =
134
+ Mockito .spy (new UpdateApkClient (testExecutor , firebaseApp , mockInstallApkClient ));
135
+
115
136
firebaseAppDistribution =
116
137
spy (
117
138
new FirebaseAppDistribution (
118
139
firebaseApp ,
119
140
mockTesterSignInClient ,
120
141
mockCheckForNewReleaseClient ,
121
- mockUpdateAppClient ,
142
+ mockUpdateApkClient ,
143
+ mockUpdateAabClient ,
122
144
mockSignInStorage ,
123
145
mockLifecycleNotifier ));
124
146
@@ -220,7 +242,7 @@ public void updateToNewRelease_whenNewAabReleaseAvailable_showsUpdateDialog() {
220
242
AppDistributionReleaseInternal newRelease = TEST_RELEASE_NEWER_AAB_INTERNAL .build ();
221
243
when (mockCheckForNewReleaseClient .checkForNewRelease ()).thenReturn (Tasks .forResult (newRelease ));
222
244
firebaseAppDistribution .setCachedNewRelease (newRelease );
223
- when ( mockUpdateAppClient . updateApp ( newRelease , true )).thenReturn ( new UpdateTaskImpl () );
245
+ doReturn ( new UpdateTaskImpl ( )).when ( mockUpdateAabClient ). updateAab ( newRelease );
224
246
225
247
firebaseAppDistribution .updateIfNewReleaseAvailable ();
226
248
@@ -374,9 +396,8 @@ public void updateToNewRelease_receiveProgressUpdateFromUpdateApp() {
374
396
AppDistributionReleaseInternal newRelease = TEST_RELEASE_NEWER_AAB_INTERNAL .build ();
375
397
when (mockCheckForNewReleaseClient .checkForNewRelease ()).thenReturn (Tasks .forResult (newRelease ));
376
398
firebaseAppDistribution .setCachedNewRelease (newRelease );
377
-
378
399
UpdateTaskImpl mockTask = new UpdateTaskImpl ();
379
- when (mockUpdateAppClient . updateApp (newRelease , true )).thenReturn (mockTask );
400
+ when (mockUpdateAabClient . updateAab (newRelease )).thenReturn (mockTask );
380
401
mockTask .updateProgress (
381
402
UpdateProgress .builder ()
382
403
.setApkFileTotalBytes (1 )
@@ -415,4 +436,45 @@ public void taskCancelledOnScreenRotation() {
415
436
assertEquals ("Update canceled" , e .getMessage ());
416
437
assertEquals (INSTALLATION_CANCELED , e .getErrorCode ());
417
438
}
439
+
440
+ @ Test
441
+ public void updateAppTask_whenNoReleaseAvailable_throwsError () {
442
+ firebaseAppDistribution .setCachedNewRelease (null );
443
+ when (mockSignInStorage .getSignInStatus ()).thenReturn (true );
444
+
445
+ UpdateTask updateTask = firebaseAppDistribution .updateApp ();
446
+
447
+ assertFalse (updateTask .isSuccessful ());
448
+ assertTrue (updateTask .getException () instanceof FirebaseAppDistributionException );
449
+ FirebaseAppDistributionException ex =
450
+ (FirebaseAppDistributionException ) updateTask .getException ();
451
+ assertEquals (FirebaseAppDistributionException .Status .UPDATE_NOT_AVAILABLE , ex .getErrorCode ());
452
+ assertEquals (Constants .ErrorMessages .NOT_FOUND_ERROR , ex .getMessage ());
453
+ }
454
+
455
+ @ Test
456
+ public void updateApp_withAabReleaseAvailable_returnsSameAabTask () {
457
+ AppDistributionReleaseInternal release = TEST_RELEASE_NEWER_AAB_INTERNAL .build ();
458
+ firebaseAppDistribution .setCachedNewRelease (release );
459
+ UpdateTaskImpl updateTaskToReturn = new UpdateTaskImpl ();
460
+ doReturn (updateTaskToReturn ).when (mockUpdateAabClient ).updateAab (release );
461
+ when (mockSignInStorage .getSignInStatus ()).thenReturn (true );
462
+
463
+ UpdateTask updateTask = firebaseAppDistribution .updateApp ();
464
+
465
+ assertEquals (updateTask , updateTaskToReturn );
466
+ }
467
+
468
+ @ Test
469
+ public void updateApp_withApkReleaseAvailable_returnsSameApkTask () {
470
+ when (mockSignInStorage .getSignInStatus ()).thenReturn (true );
471
+ AppDistributionReleaseInternal release = TEST_RELEASE_NEWER_APK_INTERNAL .build ();
472
+ firebaseAppDistribution .setCachedNewRelease (release );
473
+ UpdateTaskImpl updateTaskToReturn = new UpdateTaskImpl ();
474
+ doReturn (updateTaskToReturn ).when (mockUpdateApkClient ).updateApk (release , false );
475
+
476
+ UpdateTask updateTask = firebaseAppDistribution .updateApp ();
477
+
478
+ assertEquals (updateTask , updateTaskToReturn );
479
+ }
418
480
}
0 commit comments