mirror of
				https://github.com/commons-app/apps-android-commons.git
				synced 2025-10-31 14:53:59 +01:00 
			
		
		
		
	Fix null context error in network utils (#2184)
This commit is contained in:
		
							parent
							
								
									b9274c0335
								
							
						
					
					
						commit
						4b58f16557
					
				
					 2 changed files with 88 additions and 4 deletions
				
			
		|  | @ -0,0 +1,71 @@ | |||
| package fr.free.nrw.commons.utils; | ||||
| 
 | ||||
| import android.app.Application; | ||||
| import android.content.Context; | ||||
| import android.net.ConnectivityManager; | ||||
| import android.net.NetworkInfo; | ||||
| 
 | ||||
| import org.junit.Before; | ||||
| import org.junit.Test; | ||||
| 
 | ||||
| import static org.junit.Assert.*; | ||||
| import static org.mockito.Mockito.mock; | ||||
| import static org.mockito.Mockito.when; | ||||
| 
 | ||||
| public class NetworkUtilsTest { | ||||
| 
 | ||||
|     @Before | ||||
|     public void setUp() throws Exception { | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testInternetConnectionEstablished() { | ||||
|         Context mockContext = mock(Context.class); | ||||
|         Application mockApplication = mock(Application.class); | ||||
|         ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class); | ||||
|         NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); | ||||
|         when(mockNetworkInfo.isConnectedOrConnecting()) | ||||
|                 .thenReturn(true); | ||||
|         when(mockConnectivityManager.getActiveNetworkInfo()) | ||||
|                 .thenReturn(mockNetworkInfo); | ||||
|         when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE)) | ||||
|                 .thenReturn(mockConnectivityManager); | ||||
|         when(mockContext.getApplicationContext()).thenReturn(mockApplication); | ||||
|         boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(mockContext); | ||||
|         assertTrue(internetConnectionEstablished); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testInternetConnectionNotEstablished() { | ||||
|         Context mockContext = mock(Context.class); | ||||
|         Application mockApplication = mock(Application.class); | ||||
|         ConnectivityManager mockConnectivityManager = mock(ConnectivityManager.class); | ||||
|         NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); | ||||
|         when(mockNetworkInfo.isConnectedOrConnecting()) | ||||
|                 .thenReturn(false); | ||||
|         when(mockConnectivityManager.getActiveNetworkInfo()) | ||||
|                 .thenReturn(mockNetworkInfo); | ||||
|         when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE)) | ||||
|                 .thenReturn(mockConnectivityManager); | ||||
|         when(mockContext.getApplicationContext()).thenReturn(mockApplication); | ||||
|         boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(mockContext); | ||||
|         assertFalse(internetConnectionEstablished); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testInternetConnectionStatusForNullContext() { | ||||
|         boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(null); | ||||
|         assertFalse(internetConnectionEstablished); | ||||
|     } | ||||
| 
 | ||||
|     @Test | ||||
|     public void testInternetConnectionForNullConnectivityManager() { | ||||
|         Context mockContext = mock(Context.class); | ||||
|         Application mockApplication = mock(Application.class); | ||||
|         when(mockApplication.getSystemService(Context.CONNECTIVITY_SERVICE)) | ||||
|                 .thenReturn(null); | ||||
|         when(mockContext.getApplicationContext()).thenReturn(mockApplication); | ||||
|         boolean internetConnectionEstablished = NetworkUtils.isInternetConnectionEstablished(mockContext); | ||||
|         assertFalse(internetConnectionEstablished); | ||||
|     } | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vivek Maskara
						Vivek Maskara