18
18
import static com .google .firebase .appdistribution .TestUtils .applyToForegroundActivityTaskAnswer ;
19
19
import static org .junit .Assert .assertThrows ;
20
20
import static org .mockito .ArgumentMatchers .any ;
21
+ import static org .mockito .ArgumentMatchers .anyBoolean ;
22
+ import static org .mockito .ArgumentMatchers .anyLong ;
23
+ import static org .mockito .Mockito .doNothing ;
21
24
import static org .mockito .Mockito .doReturn ;
22
25
import static org .mockito .Mockito .verify ;
23
26
import static org .mockito .Mockito .verifyNoInteractions ;
31
34
import com .google .firebase .FirebaseApp ;
32
35
import com .google .firebase .FirebaseOptions ;
33
36
import com .google .firebase .appdistribution .FirebaseAppDistributionException .Status ;
37
+ import java .io .ByteArrayInputStream ;
34
38
import java .io .File ;
35
39
import java .io .IOException ;
36
40
import java .util .ArrayList ;
37
41
import java .util .List ;
42
+ import java .util .concurrent .ExecutionException ;
38
43
import java .util .concurrent .Executor ;
39
44
import java .util .concurrent .Executors ;
40
45
import javax .net .ssl .HttpsURLConnection ;
@@ -55,6 +60,7 @@ public class ApkUpdaterTest {
55
60
private static final String TEST_PROJECT_ID = "777777777777" ;
56
61
private static final String TEST_URL = "https://test-url" ;
57
62
private static final String TEST_CODE_HASH = "abcdefghijklmnopqrstuvwxyz" ;
63
+ private static final String TEST_FILE = "test_file" ;
58
64
private static final long TEST_FILE_LENGTH = 1000 ;
59
65
private TestActivity activity ;
60
66
@@ -150,24 +156,59 @@ public void updateApk_whenCannotReadInputStream_setsDownloadFailure() throws Exc
150
156
IOException caughtException = new IOException ("error" );
151
157
when (mockHttpsUrlConnection .getInputStream ()).thenThrow (caughtException );
152
158
153
- UpdateTaskImpl updateTask = apkUpdater .updateApk (TEST_RELEASE , false );
159
+ UpdateTaskImpl updateTask = apkUpdater .updateApk (TEST_RELEASE , true );
154
160
updateTask .addOnCompleteListener (testExecutor , onCompleteListener );
155
161
FirebaseAppDistributionException e =
156
162
assertThrows (FirebaseAppDistributionException .class , () -> onCompleteListener .await ());
157
163
158
164
assertThat (e .getErrorCode ()).isEqualTo (Status .DOWNLOAD_FAILURE );
159
165
assertThat (e ).hasMessageThat ().contains ("Failed to download APK" );
160
166
assertThat (e ).hasCauseThat ().isEqualTo (caughtException );
167
+ verify (mockNotificationsManager ).updateNotification (0 , 0 , R .string .download_failed );
161
168
}
162
169
163
170
@ Test
164
- public void updateApk_whenInstallSuccessful_setsResult () throws Exception {
165
- doReturn (Tasks .forResult (mockFile )).when (apkUpdater ).downloadApk (TEST_RELEASE , false );
171
+ public void updateApk_whenSuccessfullyUpdated_notificationsSetCorrectly ()
172
+ throws FirebaseAppDistributionException , ExecutionException , InterruptedException ,
173
+ IOException {
174
+ doReturn (new ByteArrayInputStream (TEST_FILE .getBytes ()))
175
+ .when (mockHttpsUrlConnection )
176
+ .getInputStream ();
177
+ doNothing ().when (apkUpdater ).validateJarFile (any (), anyLong (), anyBoolean (), anyLong ());
166
178
when (mockApkInstaller .installApk (any (), any ())).thenReturn (Tasks .forResult (null ));
167
- UpdateTaskImpl updateTask = apkUpdater .updateApk (TEST_RELEASE , false );
179
+
180
+ UpdateTask updateTask = apkUpdater .updateApk (TEST_RELEASE , true );
168
181
updateTask .addOnCompleteListener (testExecutor , onCompleteListener );
182
+ List <UpdateProgress > events = new ArrayList <>();
183
+ updateTask .addOnProgressListener (testExecutor , events ::add );
169
184
onCompleteListener .await ();
170
- assertThat (updateTask .isSuccessful ()).isTrue ();
185
+
186
+ assertThat (events ).hasSize (3 );
187
+ assertThat (events .get (0 ).getUpdateStatus ()).isEqualTo (UpdateStatus .PENDING );
188
+ assertThat (events .get (1 ).getUpdateStatus ()).isEqualTo (UpdateStatus .DOWNLOADING );
189
+ assertThat (events .get (1 ).getApkBytesDownloaded ()).isEqualTo (TEST_FILE .length ());
190
+ assertThat (events .get (2 ).getUpdateStatus ()).isEqualTo (UpdateStatus .DOWNLOADED );
191
+ }
192
+
193
+ @ Test
194
+ public void updateApk_invalidJarFile_throwsException () throws IOException {
195
+ doReturn (new ByteArrayInputStream (TEST_FILE .getBytes ()))
196
+ .when (mockHttpsUrlConnection )
197
+ .getInputStream ();
198
+ when (mockApkInstaller .installApk (any (), any ())).thenReturn (Tasks .forResult (null ));
199
+
200
+ // If validateJarFile is not mocked it will be called with an invalid jar file.
201
+ UpdateTask updateTask = apkUpdater .updateApk (TEST_RELEASE , true );
202
+ updateTask .addOnCompleteListener (testExecutor , onCompleteListener );
203
+ FirebaseAppDistributionException e =
204
+ assertThrows (FirebaseAppDistributionException .class , () -> onCompleteListener .await ());
205
+
206
+ assertThat (e .getErrorCode ()).isEqualTo (Status .DOWNLOAD_FAILURE );
207
+ assertThat (updateTask .isSuccessful ()).isFalse ();
208
+
209
+ // Verify that the notification in validateJarFile is set.
210
+ verify (mockNotificationsManager )
211
+ .updateNotification (0 , TEST_FILE .length (), R .string .download_failed );
171
212
}
172
213
173
214
@ Test
0 commit comments