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