|
23 | 23 | import static com.github.tomakehurst.wiremock.client.WireMock.verify;
|
24 | 24 | import static com.google.android.datatransport.cct.CctTransportBackend.getTzOffset;
|
25 | 25 | import static com.google.android.datatransport.cct.ProtoMatchers.protoMatcher;
|
| 26 | +import static com.google.common.truth.Truth.assertThat; |
26 | 27 | import static org.junit.Assert.assertEquals;
|
27 | 28 |
|
28 | 29 | import android.content.Context;
|
|
47 | 48 | import org.junit.runner.RunWith;
|
48 | 49 | import org.robolectric.RobolectricTestRunner;
|
49 | 50 | import org.robolectric.RuntimeEnvironment;
|
| 51 | +import org.robolectric.annotation.Config; |
| 52 | +import org.robolectric.annotation.Implementation; |
| 53 | +import org.robolectric.annotation.Implements; |
50 | 54 |
|
51 | 55 | @RunWith(RobolectricTestRunner.class)
|
52 | 56 | public class CctTransportBackendTest {
|
@@ -205,4 +209,59 @@ public void send_whenBackendResponseTimesOut_shouldReturnTransientError() {
|
205 | 209 |
|
206 | 210 | assertEquals(response, BackendResponse.transientError());
|
207 | 211 | }
|
| 212 | + |
| 213 | + @Test |
| 214 | + public void decorate_whenOnline_shouldProperlyPopulateNetworkInfo() { |
| 215 | + CctTransportBackend backend = |
| 216 | + new CctTransportBackend( |
| 217 | + RuntimeEnvironment.application, TEST_ENDPOINT, wallClock, uptimeClock, 300); |
| 218 | + |
| 219 | + EventInternal result = |
| 220 | + backend.decorate( |
| 221 | + EventInternal.builder() |
| 222 | + .setEventMillis(INITIAL_WALL_TIME) |
| 223 | + .setUptimeMillis(INITIAL_UPTIME) |
| 224 | + .setTransportName("3") |
| 225 | + .setPayload(PAYLOAD.toByteArray()) |
| 226 | + .build()); |
| 227 | + |
| 228 | + assertThat(result.get(CctTransportBackend.KEY_NETWORK_TYPE)) |
| 229 | + .isEqualTo(String.valueOf(NetworkConnectionInfo.NetworkType.MOBILE_VALUE)); |
| 230 | + assertThat(result.get(CctTransportBackend.KEY_MOBILE_SUBTYPE)) |
| 231 | + .isEqualTo(String.valueOf(NetworkConnectionInfo.MobileSubtype.EDGE_VALUE)); |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + @Config(shadows = {OfflineConnectivityManagerShadow.class}) |
| 236 | + public void decorate_whenOffline_shouldProperlyPopulateNetworkInfo() { |
| 237 | + CctTransportBackend backend = |
| 238 | + new CctTransportBackend( |
| 239 | + RuntimeEnvironment.application, TEST_ENDPOINT, wallClock, uptimeClock, 300); |
| 240 | + |
| 241 | + EventInternal result = |
| 242 | + backend.decorate( |
| 243 | + EventInternal.builder() |
| 244 | + .setEventMillis(INITIAL_WALL_TIME) |
| 245 | + .setUptimeMillis(INITIAL_UPTIME) |
| 246 | + .setTransportName("3") |
| 247 | + .setPayload(PAYLOAD.toByteArray()) |
| 248 | + .build()); |
| 249 | + |
| 250 | + assertThat(result.get(CctTransportBackend.KEY_NETWORK_TYPE)) |
| 251 | + .isEqualTo(String.valueOf(NetworkConnectionInfo.NetworkType.NONE_VALUE)); |
| 252 | + assertThat(result.get(CctTransportBackend.KEY_MOBILE_SUBTYPE)) |
| 253 | + .isEqualTo( |
| 254 | + String.valueOf(NetworkConnectionInfo.MobileSubtype.UNKNOWN_MOBILE_SUBTYPE_VALUE)); |
| 255 | + } |
| 256 | + |
| 257 | + // When there is no active network, the ConnectivityManager returns null when |
| 258 | + // getActiveNetworkInfo() is called. |
| 259 | + @Implements(ConnectivityManager.class) |
| 260 | + public static class OfflineConnectivityManagerShadow { |
| 261 | + |
| 262 | + @Implementation |
| 263 | + public NetworkInfo getActiveNetworkInfo() { |
| 264 | + return null; |
| 265 | + } |
| 266 | + } |
208 | 267 | }
|
0 commit comments