Browse Source

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

test: added PowerMocked unit tests for the ConnectivityUtils class.
Andy Scherzinger 6 years ago
parent
commit
8353ef42bb
2 changed files with 104 additions and 16 deletions
  1. 4 0
      build.gradle
  2. 100 16
      src/test/java/com/owncloud/android/utils/ConnectivityUtilsTest.java

+ 4 - 0
build.gradle

@@ -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

+ 100 - 16
src/test/java/com/owncloud/android/utils/ConnectivityUtilsTest.java

@@ -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 connectivityManager;
+
+    @Mock
+    private NetworkInfo networkInfo;
+
+    @Mock
+    private Account account;
 
     @Mock
-    private ConnectivityManager mConnectivityManager;
+    private OwnCloudAccount ocAccount;
 
     @Mock
-    private NetworkInfo mNetworkInfo;
+    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(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("Falsely indicated connection on WiFi", ConnectivityUtils.isOnlineWithWifi(mContext));
+        assertFalse("internet was falsely claimed to be walled",
+                    ConnectivityUtils.isInternetWalled(context));
     }
 }