15
15
16
16
import static com .google .common .truth .Truth .assertThat ;
17
17
import static com .google .firebase .appdistribution .TestUtils .assertTaskFailure ;
18
+ import static com .google .firebase .appdistribution .TestUtils .awaitAsyncOperations ;
18
19
import static org .junit .Assert .assertEquals ;
19
20
import static org .mockito .Mockito .when ;
20
21
import static org .robolectric .Shadows .shadowOf ;
22
+ import static org .robolectric .annotation .LooperMode .Mode .PAUSED ;
21
23
22
24
import android .app .Activity ;
23
25
import android .net .Uri ;
31
33
import java .util .List ;
32
34
import java .util .concurrent .ExecutorService ;
33
35
import java .util .concurrent .Executors ;
34
- import java .util .concurrent .TimeUnit ;
35
36
import javax .net .ssl .HttpsURLConnection ;
36
37
import org .junit .Before ;
37
38
import org .junit .Test ;
41
42
import org .mockito .MockitoAnnotations ;
42
43
import org .robolectric .Robolectric ;
43
44
import org .robolectric .RobolectricTestRunner ;
45
+ import org .robolectric .annotation .LooperMode ;
44
46
import org .robolectric .shadows .ShadowActivity ;
45
47
46
48
@ RunWith (RobolectricTestRunner .class )
49
+ @ LooperMode (PAUSED )
47
50
public class AabUpdaterTest {
48
51
private static final String TEST_URL = "https://test-url" ;
49
52
private static final String REDIRECT_TO_PLAY = "https://redirect-to-play-url" ;
@@ -95,7 +98,7 @@ public void updateAppTask_whenOpenConnectionFails_setsNetworkFailure()
95
98
when (mockHttpsUrlConnectionFactory .openConnection (TEST_URL )).thenThrow (caughtException );
96
99
97
100
UpdateTask updateTask = aabUpdater .updateAab (TEST_RELEASE_NEWER_AAB_INTERNAL );
98
- testExecutor . awaitTermination ( 100 , TimeUnit . MILLISECONDS );
101
+ awaitAsyncOperations ( testExecutor );
99
102
100
103
assertTaskFailure (
101
104
updateTask , Status .NETWORK_FAILURE , "Failed to open connection" , caughtException );
@@ -107,7 +110,7 @@ public void updateAppTask_isNotRedirectResponse_setsDownloadFailure()
107
110
when (mockHttpsUrlConnection .getResponseCode ()).thenReturn (200 );
108
111
109
112
UpdateTask updateTask = aabUpdater .updateAab (TEST_RELEASE_NEWER_AAB_INTERNAL );
110
- testExecutor . awaitTermination ( 100 , TimeUnit . MILLISECONDS );
113
+ awaitAsyncOperations ( testExecutor );
111
114
112
115
assertTaskFailure (updateTask , Status .DOWNLOAD_FAILURE , "Expected redirect" );
113
116
}
@@ -118,7 +121,7 @@ public void updateAppTask_missingLocationHeader_setsDownloadFailure()
118
121
when (mockHttpsUrlConnection .getHeaderField ("Location" )).thenReturn (null );
119
122
120
123
UpdateTask updateTask = aabUpdater .updateAab (TEST_RELEASE_NEWER_AAB_INTERNAL );
121
- testExecutor . awaitTermination ( 100 , TimeUnit . MILLISECONDS );
124
+ awaitAsyncOperations ( testExecutor );
122
125
123
126
assertTaskFailure (updateTask , Status .DOWNLOAD_FAILURE , "No Location header" );
124
127
}
@@ -128,7 +131,7 @@ public void updateAppTask_emptyLocationHeader_setsDownloadFailure() throws Inter
128
131
when (mockHttpsUrlConnection .getHeaderField ("Location" )).thenReturn ("" );
129
132
130
133
UpdateTask updateTask = aabUpdater .updateAab (TEST_RELEASE_NEWER_AAB_INTERNAL );
131
- testExecutor . awaitTermination ( 100 , TimeUnit . MILLISECONDS );
134
+ awaitAsyncOperations ( testExecutor );
132
135
133
136
assertTaskFailure (updateTask , Status .DOWNLOAD_FAILURE , "Empty Location header" );
134
137
}
@@ -139,7 +142,7 @@ public void updateAppTask_whenAabReleaseAvailable_redirectsToPlay() throws Excep
139
142
140
143
UpdateTask updateTask = aabUpdater .updateAab (TEST_RELEASE_NEWER_AAB_INTERNAL );
141
144
updateTask .addOnProgressListener (testExecutor , progressEvents ::add );
142
- testExecutor . awaitTermination ( 100 , TimeUnit . MILLISECONDS );
145
+ awaitAsyncOperations ( testExecutor );
143
146
144
147
assertThat (shadowActivity .getNextStartedActivity ().getData ())
145
148
.isEqualTo (Uri .parse (REDIRECT_TO_PLAY ));
@@ -156,12 +159,11 @@ public void updateAppTask_whenAabReleaseAvailable_redirectsToPlay() throws Excep
156
159
@ Test
157
160
public void updateAppTask_onAppResume_setsUpdateCancelled () throws InterruptedException {
158
161
UpdateTask updateTask = aabUpdater .updateAab (TEST_RELEASE_NEWER_AAB_INTERNAL );
159
- testExecutor . awaitTermination ( 100 , TimeUnit . MILLISECONDS );
162
+ awaitAsyncOperations ( testExecutor );
160
163
aabUpdater .onActivityStarted (activity );
161
164
162
165
FirebaseAppDistributionException exception =
163
166
assertTaskFailure (updateTask , Status .INSTALLATION_CANCELED , ErrorMessages .UPDATE_CANCELED );
164
-
165
167
assertEquals (
166
168
ReleaseUtils .convertToAppDistributionRelease (TEST_RELEASE_NEWER_AAB_INTERNAL ),
167
169
exception .getRelease ());
0 commit comments