With more information while sending logs (#2222)

* With more information while sending logs

* With java docs and unit tests

* Changes based on code review
This commit is contained in:
Vivek Maskara 2019-01-02 20:42:08 +05:30 committed by Josephine Lim
parent 5317063689
commit 2ea6bd7f65
6 changed files with 305 additions and 14 deletions

View file

@ -4,11 +4,19 @@ import android.app.Application;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import fr.free.nrw.commons.utils.model.NetworkConnectionType;
import static android.telephony.TelephonyManager.NETWORK_TYPE_EDGE;
import static android.telephony.TelephonyManager.NETWORK_TYPE_HSPA;
import static android.telephony.TelephonyManager.NETWORK_TYPE_LTE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -68,4 +76,104 @@ public class NetworkUtilsTest {
boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(mockContext);
assertFalse(internetConnectionEstablished);
}
@Test
public void testWifiNetwork() {
Context mockContext = mock(Context.class);
Application mockApplication = mock(Application.class);
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
when(mockNetworkInfo.getType())
.thenReturn(ConnectivityManager.TYPE_WIFI);
when(mockConnectivityManager.getActiveNetworkInfo())
.thenReturn(mockNetworkInfo);
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(mockConnectivityManager);
when(mockApplication.getSystemService(Context.TELEPHONY_SERVICE))
.thenReturn(mock(TelephonyManager.class));
when(mockContext.getApplicationContext()).thenReturn(mockApplication);
NetworkConnectionType networkType = NetworkUtils.getNetworkType(mockContext);
assertEquals(networkType, NetworkConnectionType.WIFI);
}
@Test
public void testCellular2GNetwork() {
Context mockContext = mock(Context.class);
Application mockApplication = mock(Application.class);
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
when(mockNetworkInfo.getType())
.thenReturn(ConnectivityManager.TYPE_MOBILE);
when(mockConnectivityManager.getActiveNetworkInfo())
.thenReturn(mockNetworkInfo);
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(mockConnectivityManager);
TelephonyManager mockTelephonyManager = mock(TelephonyManager.class);
when(mockTelephonyManager.getNetworkType())
.thenReturn(NETWORK_TYPE_EDGE);
when(mockApplication.getSystemService(Context.TELEPHONY_SERVICE))
.thenReturn(mockTelephonyManager);
when(mockContext.getApplicationContext()).thenReturn(mockApplication);
NetworkConnectionType networkType = NetworkUtils.getNetworkType(mockContext);
assertEquals(networkType, NetworkConnectionType.TWO_G);
}
@Test
public void testCellular3GNetwork() {
Context mockContext = mock(Context.class);
Application mockApplication = mock(Application.class);
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
when(mockNetworkInfo.getType())
.thenReturn(ConnectivityManager.TYPE_MOBILE);
when(mockConnectivityManager.getActiveNetworkInfo())
.thenReturn(mockNetworkInfo);
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(mockConnectivityManager);
TelephonyManager mockTelephonyManager = mock(TelephonyManager.class);
when(mockTelephonyManager.getNetworkType())
.thenReturn(NETWORK_TYPE_HSPA);
when(mockApplication.getSystemService(Context.TELEPHONY_SERVICE))
.thenReturn(mockTelephonyManager);
when(mockContext.getApplicationContext()).thenReturn(mockApplication);
NetworkConnectionType networkType = NetworkUtils.getNetworkType(mockContext);
assertEquals(networkType, NetworkConnectionType.THREE_G);
}
@Test
public void testCellular4GNetwork() {
Context mockContext = mock(Context.class);
Application mockApplication = mock(Application.class);
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
when(mockNetworkInfo.getType())
.thenReturn(ConnectivityManager.TYPE_MOBILE);
when(mockConnectivityManager.getActiveNetworkInfo())
.thenReturn(mockNetworkInfo);
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE))
.thenReturn(mockConnectivityManager);
TelephonyManager mockTelephonyManager = mock(TelephonyManager.class);
when(mockTelephonyManager.getNetworkType())
.thenReturn(NETWORK_TYPE_LTE);
when(mockApplication.getSystemService(Context.TELEPHONY_SERVICE))
.thenReturn(mockTelephonyManager);
when(mockContext.getApplicationContext()).thenReturn(mockApplication);
NetworkConnectionType networkType = NetworkUtils.getNetworkType(mockContext);
assertEquals(networkType, NetworkConnectionType.FOUR_G);
}
}