Skip to content

Commit 4b58f16

Browse files
maskaravivekdomdomegg
authored andcommitted
Fix null context error in network utils (#2184)
1 parent b9274c0 commit 4b58f16

File tree

2 files changed

+88
-4
lines changed

2 files changed

+88
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
package fr.free.nrw.commons.utils;
22

33

4+
import android.annotation.SuppressLint;
45
import android.content.Context;
56
import android.net.ConnectivityManager;
67
import android.net.NetworkInfo;
78

9+
import javax.annotation.Nullable;
10+
811
public class NetworkUtils {
912

10-
public static boolean isInternetConnectionEstablished(Context context) {
13+
/**
14+
* https://developer.android.com/training/monitoring-device-state/connectivity-monitoring#java
15+
* Check if internet connection is established.
16+
* @param context context passed to this method could be null.
17+
* @return Returns current internet connection status. Returns false if null context was passed.
18+
*/
19+
@SuppressLint("MissingPermission")
20+
public static boolean isInternetConnectionEstablished(@Nullable Context context) {
21+
if (context == null) {
22+
return false;
23+
}
24+
1125
ConnectivityManager cm =
12-
(ConnectivityManager)context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
26+
(ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
1327

1428
if (cm == null) {
1529
return false;
1630
}
1731
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
18-
return activeNetwork != null &&
19-
activeNetwork.isConnectedOrConnecting();
32+
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
2033
}
2134
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package fr.free.nrw.commons.utils;
2+
3+
import android.app.Application;
4+
import android.content.Context;
5+
import android.net.ConnectivityManager;
6+
import android.net.NetworkInfo;
7+
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import static org.junit.Assert.*;
12+
import static org.mockito.Mockito.mock;
13+
import static org.mockito.Mockito.when;
14+
15+
public class NetworkUtilsTest {
16+
17+
@Before
18+
public void setUp() throws Exception {
19+
}
20+
21+
@Test
22+
public void testInternetConnectionEstablished() {
23+
Context mockContext = mock(Context.class);
24+
Application mockApplication = mock(Application.class);
25+
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
26+
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
27+
when(mockNetworkInfo.isConnectedOrConnecting())
28+
.thenReturn(true);
29+
when(mockConnectivityManager.getActiveNetworkInfo())
30+
.thenReturn(mockNetworkInfo);
31+
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE))
32+
.thenReturn(mockConnectivityManager);
33+
when(mockContext.getApplicationContext()).thenReturn(mockApplication);
34+
boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(mockContext);
35+
assertTrue(internetConnectionEstablished);
36+
}
37+
38+
@Test
39+
public void testInternetConnectionNotEstablished() {
40+
Context mockContext = mock(Context.class);
41+
Application mockApplication = mock(Application.class);
42+
ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class);
43+
NetworkInfo mockNetworkInfo = mock(NetworkInfo.class);
44+
when(mockNetworkInfo.isConnectedOrConnecting())
45+
.thenReturn(false);
46+
when(mockConnectivityManager.getActiveNetworkInfo())
47+
.thenReturn(mockNetworkInfo);
48+
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE))
49+
.thenReturn(mockConnectivityManager);
50+
when(mockContext.getApplicationContext()).thenReturn(mockApplication);
51+
boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(mockContext);
52+
assertFalse(internetConnectionEstablished);
53+
}
54+
55+
@Test
56+
public void testInternetConnectionStatusForNullContext() {
57+
boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(null);
58+
assertFalse(internetConnectionEstablished);
59+
}
60+
61+
@Test
62+
public void testInternetConnectionForNullConnectivityManager() {
63+
Context mockContext = mock(Context.class);
64+
Application mockApplication = mock(Application.class);
65+
when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE))
66+
.thenReturn(null);
67+
when(mockContext.getApplicationContext()).thenReturn(mockApplication);
68+
boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(mockContext);
69+
assertFalse(internetConnectionEstablished);
70+
}
71+
}

0 commit comments

Comments
 (0)