소스 검색

test: added PowerMocked unit tests for the ConnectivityUtils class.

Added necessary PowerMock dependencies in addition to a JSON dependency which is required in order to unit test with JSON objects. The default JSON library is part of AOSP and would need to be mocked which is not what we want.

Signed-off-by: ardevd <edvard.holst@gmail.com>
ardevd 6 년 전
부모
커밋
e17e5f6bd7
2개의 변경된 파일66개의 추가작업 그리고 1개의 파일을 삭제
  1. 4 0
      build.gradle
  2. 62 1
      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.26.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

+ 62 - 1
src/test/java/com/owncloud/android/utils/ConnectivityUtilsTest.java

@@ -20,20 +20,39 @@
  */
 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
@@ -45,9 +64,22 @@ public class ConnectivityUtilsTest {
     @Mock
     private NetworkInfo mNetworkInfo;
 
+    @Mock
+    private Account mAccount;
+
+    @Mock
+    private OwnCloudAccount mOcAccount;
+
+    @Mock
+    private OwnCloudClient mClient;
+
+    @Mock
+    private GetMethod mGetMethod;
+
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
+
         when(mContext.getSystemService(Context.CONNECTIVITY_SERVICE)).thenReturn(mConnectivityManager);
         when(mConnectivityManager.getActiveNetworkInfo()).thenReturn(mNetworkInfo);
     }
@@ -81,4 +113,33 @@ public class ConnectivityUtilsTest {
 
         assertFalse("Falsely indicated connection on WiFi", ConnectivityUtils.isOnlineWithWifi(mContext));
     }
+
+    @Test
+    public void isInternetWalled_assertFalseWhenAllIsGood() throws Exception {
+        mockStatic(AccountUtils.class);
+        mockStatic(OwnCloudClientFactory.class);
+        // Ensure we are on WiFi
+        when(mNetworkInfo.isConnectedOrConnecting()).thenReturn(true);
+        when(mNetworkInfo.getType()).thenReturn(ConnectivityManager.TYPE_WIFI);
+
+        PowerMockito.when(AccountUtils.getCurrentOwnCloudAccount(eq(mContext))).thenReturn(mAccount);
+        PowerMockito.whenNew(OwnCloudAccount.class).withAnyArguments().thenReturn(mOcAccount);
+        PowerMockito.when(AccountUtils.getServerVersion(eq(mAccount))).thenReturn(OwnCloudVersion.nextcloud_14);
+        PowerMockito.when(OwnCloudClientFactory.createOwnCloudClient(eq(mAccount), eq(mContext))).thenReturn(mClient);
+        PowerMockito.whenNew(GetMethod.class).withAnyArguments().thenReturn(mGetMethod);
+
+        // Return SC_OK
+        when(mClient.executeMethod(mGetMethod)).thenReturn(HttpStatus.SC_OK);
+
+        // Content length should be > 0.
+        when(mGetMethod.getResponseContentLength()).thenReturn(1024L);
+
+        JSONObject jsonObj = new JSONObject();
+        jsonObj.put("maintenance", false);
+
+        when(mGetMethod.getResponseBodyAsString()).thenReturn(jsonObj.toString());
+
+        assertFalse("internet was falsely claimed to be walled",
+                    ConnectivityUtils.isInternetWalled(mContext));
+    }
 }