Merge pull request #3864 from nextcloud/connectivityutils-powermock-test

test: added PowerMocked unit tests for the ConnectivityUtils class.
This commit is contained in:
Andy Scherzinger 2019-04-11 14:34:38 +02:00 committed by GitHub
commit 8353ef42bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 104 additions and 16 deletions

View File

@ -287,6 +287,10 @@ dependencies {
testImplementation 'junit:junit:4.12'
testImplementation 'org.mockito:mockito-core:2.27.0'
testImplementation 'androidx.test:core:1.1.0'
testImplementation 'org.powermock:powermock-core:2.0.0'
testImplementation 'org.powermock:powermock-module-junit4:2.0.0'
testImplementation 'org.powermock:powermock-api-mockito2:2.0.0'
testImplementation 'org.json:json:20180813'
// dependencies for instrumented tests
// JUnit4 Rules

View File

@ -20,65 +20,149 @@
*/
package com.owncloud.android.utils;
import android.accounts.Account;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.owncloud.android.authentication.AccountUtils;
import com.owncloud.android.lib.common.OwnCloudAccount;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.OwnCloudClientFactory;
import com.owncloud.android.lib.resources.status.OwnCloudVersion;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.contains;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"org.slf4j.*"})
@PrepareForTest({AccountUtils.class, OwnCloudClientFactory.class, ConnectivityUtils.class})
public class ConnectivityUtilsTest {
@Mock
private Context mContext;
private Context context;
@Mock
private ConnectivityManager mConnectivityManager;
private ConnectivityManager connectivityManager;
@Mock
private NetworkInfo mNetworkInfo;
private NetworkInfo networkInfo;
@Mock
private Account account;
@Mock
private OwnCloudAccount ocAccount;
@Mock
private OwnCloudClient client;
@Mock
private GetMethod getMethod;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mConnectivityManager);
when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(mNetworkInfo);
when(context.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(connectivityManager);
when(connectivityManager.getActiveNetworkInfo()).thenReturn(networkInfo);
mockStatic(AccountUtils.class);
mockStatic(OwnCloudClientFactory.class);
}
@Test
public void isOnlineWithWifi_assertTrueWhenOnWifi() {
when(mNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
assertTrue("Falsely indicated connection not on WiFi", ConnectivityUtils.isOnlineWithWifi(mContext));
assertTrue("Falsely indicated connection not on WiFi", ConnectivityUtils.isOnlineWithWifi(context));
}
@Test
public void isOnlineWithWifi_assertTrueWhenOnVPNWithAdditionalWiFiConnection() {
when(mNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_VPN);
when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_VPN);
NetworkInfo[] networkInfoList = new NetworkInfo[1];
NetworkInfo wifiNetworkInfo = mock(NetworkInfo.class);
when(wifiNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
when(wifiNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
networkInfoList[0] = wifiNetworkInfo;
when(mConnectivityManager.getAllNetworkInfo()).thenReturn(networkInfoList);
when(connectivityManager.getAllNetworkInfo()).thenReturn(networkInfoList);
assertTrue("Falsely indicated connection not on WiFi", ConnectivityUtils.isOnlineWithWifi(mContext));
assertTrue("Falsely indicated connection not on WiFi", ConnectivityUtils.isOnlineWithWifi(context));
}
@Test
public void isOnlineWithWifi_assertFalseWhenNotOnWifi() {
when(mNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE);
when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_MOBILE);
assertFalse("Falsely indicated connection on WiFi", ConnectivityUtils.isOnlineWithWifi(mContext));
assertFalse("Falsely indicated connection on WiFi", ConnectivityUtils.isOnlineWithWifi(context));
}
@Test
public void isInternetWalled_assertFalseWhenOnOlderNC() throws Exception {
// Ensure we are on WiFi
when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
PowerMockito.when(AccountUtils.getCurrentOwnCloudAccount(eq(context))).thenReturn(account);
PowerMockito.whenNew(OwnCloudAccount.class).withAnyArguments().thenReturn(ocAccount);
PowerMockito.when(AccountUtils.getServerVersion(eq(account))).thenReturn(OwnCloudVersion.nextcloud_13);
PowerMockito.when(OwnCloudClientFactory.createOwnCloudClient(eq(account), eq(context))).thenReturn(client);
PowerMockito.whenNew(GetMethod.class).withAnyArguments().thenReturn(getMethod);
// Return SC_OK
when(client.executeMethod(getMethod)).thenReturn(HttpStatus.SC_OK);
// Content length should be > 0.
when(getMethod.getResponseContentLength()).thenReturn(1024L);
JSONObject jsonObj = new JSONObject();
jsonObj.put("maintenance", false);
when(getMethod.getResponseBodyAsString()).thenReturn(jsonObj.toString());
assertFalse("internet was falsely claimed to be walled",
ConnectivityUtils.isInternetWalled(context));
}
@Test
public void isInternetWalled_assertFalseWhenOnNewerNC() throws Exception {
// Ensure we are on WiFi
when(networkInfo.isConnectedOrConnecting()).thenReturn(true);
when(networkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
PowerMockito.when(AccountUtils.getCurrentOwnCloudAccount(eq(context))).thenReturn(account);
PowerMockito.whenNew(OwnCloudAccount.class).withAnyArguments().thenReturn(ocAccount);
PowerMockito.when(AccountUtils.getServerVersion(eq(account))).thenReturn(OwnCloudVersion.nextcloud_14);
PowerMockito.when(OwnCloudClientFactory.createOwnCloudClient(eq(account), eq(context))).thenReturn(client);
PowerMockito.whenNew(GetMethod.class).withAnyArguments().thenReturn(getMethod);
// Return SC_NO_CONTENT
when(client.executeMethod(getMethod)).thenReturn(HttpStatus.SC_NO_CONTENT);
// Content length should be 0.
when(getMethod.getResponseContentLength()).thenReturn(0L);
assertFalse("internet was falsely claimed to be walled",
ConnectivityUtils.isInternetWalled(context));
}
}