diff --git a/build.gradle b/build.gradle index 36015f91..b5931a63 100644 --- a/build.gradle +++ b/build.gradle @@ -6,13 +6,13 @@ buildscript { } } dependencies { - classpath 'com.android.tools.build:gradle:2.3.2' - classpath 'com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.7.1' + classpath "com.android.tools.build:gradle:2.3.2" + classpath "com.getkeepsafe.dexcount:dexcount-gradle-plugin:0.7.1" classpath "com.github.dcendents:android-maven-gradle-plugin:1.5" - classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3' - classpath "org.jacoco:org.jacoco.core:0.7.8" - classpath "org.jacoco:org.jacoco.agent:0.7.8" - classpath "org.jacoco:org.jacoco.report:0.7.8" + classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3" + classpath "org.jacoco:org.jacoco.core:0.7.9" + classpath "org.jacoco:org.jacoco.agent:0.7.9" + classpath "org.jacoco:org.jacoco.report:0.7.9" } } @@ -46,7 +46,7 @@ subprojects { ext { GROUP = "com.isupatches" - VERSION_NAME = "1.0.10" + VERSION_NAME = "2.0.0-BETA" BINTRAY_REPO = "Maven" BINTRAY_NAME = "wisefy" diff --git a/wisefy/build.gradle b/wisefy/build.gradle index f6d6878b..a5678b22 100644 --- a/wisefy/build.gradle +++ b/wisefy/build.gradle @@ -26,13 +26,13 @@ android { unitTests.returnDefaultValues = true; } - compileSdkVersion 25 + compileSdkVersion 26 buildToolsVersion "25.0.3" defaultConfig { minSdkVersion 16 - targetSdkVersion 25 - versionCode 9 + targetSdkVersion 26 + versionCode 10 versionName version testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner' } @@ -55,6 +55,8 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:support-annotations:25.3.1' + androidTestCompile 'com.android.support:support-annotations:25.3.1' androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2' androidTestCompile 'org.mockito:mockito-android:2.8.47' @@ -62,7 +64,7 @@ dependencies { // For issue #55 / IllegalAccessError conflict test androidTestCompile 'com.google.android.gms:play-services-gcm:8.3.0' - testCompile 'org.mockito:mockito-core:2.7.19' + testCompile 'org.mockito:mockito-core:2.8.47' testCompile 'junit:junit:4.12' } diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/AddOpenNetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/AddOpenNetworkTests.java new file mode 100644 index 00000000..423e036f --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/AddOpenNetworkTests.java @@ -0,0 +1,186 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiInfo; +import com.isupatches.wisefy.WiseFy; +import com.isupatches.wisefy.callbacks.AddOpenNetworkCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.OPEN_NETWORK_SSID; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class AddOpenNetworkTests extends BaseTestClass { + + public AddOpenNetworkTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); + int result = mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID); + assertEquals(WiseFy.WIFI_MANAGER_FAILURE, result); + } + + @Test + public void noCallbacks_failure_nullSSID() { + int result = mWiseFy.addOpenNetwork(null); + assertEquals(WiseFyCodes.MISSING_PARAMETER, result); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + int result = mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID); + assertEquals(WiseFyCodes.NULL_MANAGER, result); + } + + @Test + public void noCallbacks_success() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); + int result = mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID); + assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); + } + + @Test + public void noCallbacks_success_alreadyConfigured() { + setManagers(); + + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = OPEN_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(OPEN_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + int result = mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID); + assertEquals(WiseFyCodes.NETWORK_ALREADY_CONFIGURED, result); + } + + @Test + public void callbacks_failure() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); + AddOpenNetworkCallbacks mockAddNetworkCallbacks = mock(AddOpenNetworkCallbacks.class); + mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(3000)).failureAddingOpenNetwork(WiseFy.WIFI_MANAGER_FAILURE); + } + + @Test + public void callbacks_failure_nullCallback() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); + AddOpenNetworkCallbacks mockAddNetworkCallbacks = mock(AddOpenNetworkCallbacks.class); + mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID, null); + verify(mockAddNetworkCallbacks, never()).failureAddingOpenNetwork(WiseFy.WIFI_MANAGER_FAILURE); + } + + @Test + public void callbacks_failure_nullSSID() { + setManagers(); + AddOpenNetworkCallbacks mockAddNetworkCallbacks = mock(AddOpenNetworkCallbacks.class); + mWiseFy.addOpenNetwork(null, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(3000)).addOpenNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullSSID_nullCallback() { + setManagers(); + AddOpenNetworkCallbacks mockAddNetworkCallbacks = mock(AddOpenNetworkCallbacks.class); + mWiseFy.addOpenNetwork(null, null); + verify(mockAddNetworkCallbacks, never()).addOpenNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + AddOpenNetworkCallbacks mockAddNetworkCallbacks = mock(AddOpenNetworkCallbacks.class); + mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(3000)).addOpenNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + AddOpenNetworkCallbacks mockAddNetworkCallbacks = mock(AddOpenNetworkCallbacks.class); + mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID, null); + verify(mockAddNetworkCallbacks, never()).addOpenNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); + AddOpenNetworkCallbacks mockAddNetworkCallbacks = mock(AddOpenNetworkCallbacks.class); + mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(3000)).openNetworkAdded(any(WifiConfiguration.class)); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); + AddOpenNetworkCallbacks mockAddNetworkCallbacks = mock(AddOpenNetworkCallbacks.class); + mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID, null); + verify(mockAddNetworkCallbacks, never()).openNetworkAdded(any(WifiConfiguration.class)); + } + + @Test + public void callbacks_success_alreadyConfigured() { + setManagers(); + + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = OPEN_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(OPEN_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + AddOpenNetworkCallbacks mockAddNetworkCallbacks = mock(AddOpenNetworkCallbacks.class); + mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(3000)).addOpenNetworkWiseFyFailure(WiseFyCodes.NETWORK_ALREADY_CONFIGURED); + } + + @Test + public void callbacks_success_alreadyConfigured_nullCallback() { + setManagers(); + + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = OPEN_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(OPEN_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + AddOpenNetworkCallbacks mockAddNetworkCallbacks = mock(AddOpenNetworkCallbacks.class); + mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID, null); + verify(mockAddNetworkCallbacks, never()).addOpenNetworkWiseFyFailure(WiseFyCodes.NETWORK_ALREADY_CONFIGURED); + } +} \ No newline at end of file diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/AddWEPNetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/AddWEPNetworkTests.java new file mode 100644 index 00000000..0886ef7b --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/AddWEPNetworkTests.java @@ -0,0 +1,202 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiInfo; +import com.isupatches.wisefy.WiseFy; +import com.isupatches.wisefy.callbacks.AddWEPNetworkCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.WEP_NETWORK_PASSWORD; +import static com.isupatches.wisefy.test.base.TestUtils.WEP_NETWORK_SSID; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class AddWEPNetworkTests extends BaseTestClass { + + public AddWEPNetworkTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); + int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD); + assertEquals(WiseFy.WIFI_MANAGER_FAILURE, result); + } + + @Test + public void noCallbacks_failure_nullPassword() { + int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, null); + assertEquals(WiseFyCodes.MISSING_PARAMETER, result); + } + + @Test + public void noCallbacks_failure_nullSSID() { + int result = mWiseFy.addWEPNetwork(null, WEP_NETWORK_PASSWORD); + assertEquals(WiseFyCodes.MISSING_PARAMETER, result); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD); + assertEquals(WiseFyCodes.NULL_MANAGER, result); + } + + @Test + public void noCallbacks_success() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); + int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD); + assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); + } + + @Test + public void noCallbacks_success_alreadyConfigured() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = WEP_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(WEP_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD); + assertEquals(WiseFyCodes.NETWORK_ALREADY_CONFIGURED, result); + } + + @Test + public void callbacks_failure() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(3000)).failureAddingWEPNetwork(WiseFy.WIFI_MANAGER_FAILURE); + } + + @Test + public void callbacks_failure_nullCallback() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD, null); + verify(mockAddNetworkCallbacks, never()).failureAddingWEPNetwork(WiseFy.WIFI_MANAGER_FAILURE); + } + + @Test + public void callbacks_failure_nullPassword() { + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, null, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).addWEPNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullPassword_nullCallback() { + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, null, null); + verify(mockAddNetworkCallbacks, never()).addWEPNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullSSID() { + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(null, WEP_NETWORK_PASSWORD, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).addWEPNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullSSID_nullCallback() { + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(null, WEP_NETWORK_PASSWORD, null); + verify(mockAddNetworkCallbacks, never()).addWEPNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).addWEPNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD, null); + verify(mockAddNetworkCallbacks, never()).addWEPNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).wepNetworkAdded(any(WifiConfiguration.class)); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD, null); + verify(mockAddNetworkCallbacks, never()).wepNetworkAdded(any(WifiConfiguration.class)); + } + + @Test + public void callbacks_success_alreadyConfigured() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = WEP_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(WEP_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).addWEPNetworkWiseFyFailure(WiseFyCodes.NETWORK_ALREADY_CONFIGURED); + } + + @Test + public void callbacks_success_alreadyConfigured_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = WEP_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(WEP_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + AddWEPNetworkCallbacks mockAddNetworkCallbacks = mock(AddWEPNetworkCallbacks.class); + mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD, null); + verify(mockAddNetworkCallbacks, never()).addWEPNetworkWiseFyFailure(WiseFyCodes.NETWORK_ALREADY_CONFIGURED); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/AddWPA2NetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/AddWPA2NetworkTests.java new file mode 100644 index 00000000..77c683f8 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/AddWPA2NetworkTests.java @@ -0,0 +1,205 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiInfo; +import com.isupatches.wisefy.WiseFy; +import com.isupatches.wisefy.callbacks.AddWPA2NetworkCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.WPA2_NETWORK_PASSWORD; +import static com.isupatches.wisefy.test.base.TestUtils.WPA2_NETWORK_SSID; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class AddWPA2NetworkTests extends BaseTestClass { + + public AddWPA2NetworkTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure() { + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); + int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD); + assertEquals(WiseFy.WIFI_MANAGER_FAILURE, result); + } + + @Test + public void noCallbacks_failure_nullPassword() { + int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, null); + assertEquals(WiseFyCodes.MISSING_PARAMETER, result); + } + + @Test + public void anoCallbacks_failure_nullSSID() { + int result = mWiseFy.addWPA2Network(null, WPA2_NETWORK_PASSWORD); + assertEquals(WiseFyCodes.MISSING_PARAMETER, result); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD); + assertEquals(WiseFyCodes.NULL_MANAGER, result); + } + + @Test + public void noCallbacks_success() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); + int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD); + assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); + } + + @Test + public void noCallbacks_success_alreadyConfigured() { + setManagers(); + + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = WPA2_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(WPA2_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD); + assertEquals(WiseFyCodes.NETWORK_ALREADY_CONFIGURED, result); + } + + @Test + public void callbacks_failure() { + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).failureAddingWPA2Network(WiseFy.WIFI_MANAGER_FAILURE); + } + + @Test + public void callbacks_failure_nullCallback() { + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD, null); + verify(mockAddNetworkCallbacks, never()).failureAddingWPA2Network(WiseFy.WIFI_MANAGER_FAILURE); + } + + @Test + public void callbacks_failure_nullPassword() { + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, null, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).addWPA2NetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullPassword_nullCallback() { + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, null, null); + verify(mockAddNetworkCallbacks, never()).addWPA2NetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullSSID() { + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(null, WPA2_NETWORK_PASSWORD, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).addWPA2NetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullSSID_nullCallback() { + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(null, WPA2_NETWORK_PASSWORD, null); + verify(mockAddNetworkCallbacks, never()).addWPA2NetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).addWPA2NetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD, null); + verify(mockAddNetworkCallbacks, never()).addWPA2NetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).wpa2NetworkAdded(any(WifiConfiguration.class)); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD, null); + verify(mockAddNetworkCallbacks, never()).wpa2NetworkAdded(any(WifiConfiguration.class)); + } + + @Test + public void callbacks_success_alreadyConfigured() { + setManagers(); + + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = WPA2_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(WPA2_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD, mockAddNetworkCallbacks); + verify(mockAddNetworkCallbacks, timeout(2000)).addWPA2NetworkWiseFyFailure(WiseFyCodes.NETWORK_ALREADY_CONFIGURED); + } + + @Test + public void callbacks_success_alreadyConfigured_nullCallback() { + setManagers(); + + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = WPA2_NETWORK_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(WPA2_NETWORK_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + AddWPA2NetworkCallbacks mockAddNetworkCallbacks = mock(AddWPA2NetworkCallbacks.class); + mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD, null); + verify(mockAddNetworkCallbacks,never()).addWPA2NetworkWiseFyFailure(WiseFyCodes.NETWORK_ALREADY_CONFIGURED); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/ConnectToNetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/ConnectToNetworkTests.java new file mode 100644 index 00000000..366c2a5e --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/ConnectToNetworkTests.java @@ -0,0 +1,593 @@ +package com.isupatches.wisefy.test; + + +import android.net.NetworkInfo; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiInfo; +import com.isupatches.wisefy.callbacks.ConnectToNetworkCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID2; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID3; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class ConnectToNetworkTests extends BaseTestClass { + + public ConnectToNetworkTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure_emptyList() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(new ArrayList()); + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_multipleNetworks() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration mWiFiConfiguration1 = new WifiConfiguration(); + mWiFiConfiguration1.SSID = TEST_SSID2; + wifiList.add(mWiFiConfiguration1); + + WifiConfiguration mWiFiConfiguration2 = new WifiConfiguration(); + mWiFiConfiguration2.SSID = TEST_SSID3; + wifiList.add(mWiFiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_notAvailable() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(false); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_notConnected() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(false); + + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_nullList() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(null); + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_nullSSIDParam() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(new ArrayList()); + boolean result = mWiseFy.connectToNetwork(null, 1); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_nullSSID() { + setManagers(); + + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = null; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_nullWifiConfiguration() { + setManagers(); + List wifiList = new ArrayList<>(); + wifiList.add(0, null); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(false, result); + } + + @Test + public void noCallbacks_success() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(true, result); + } + + @Test + public void noCallbacks_success_multipleNetworks() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration mWiFiConfiguration1 = new WifiConfiguration(); + mWiFiConfiguration1.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration1); + + WifiConfiguration mWiFiConfiguration2 = new WifiConfiguration(); + mWiFiConfiguration2.SSID = TEST_SSID2; + wifiList.add(mWiFiConfiguration2); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); + assertEquals(true, result); + } + + @Test + public void callbacks_failure_emptyList() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(new ArrayList()); + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks, timeout(2000)).networkNotFoundToConnectTo(); + } + + @Test + public void callbacks_failure_emptyList_nullCallback() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(new ArrayList()); + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, null); + verify(connectToNetworkCallbacks, never()).networkNotFoundToConnectTo(); + } + + @Test + public void callbacks_failure_multipleNetworks() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration mWiFiConfiguration1 = new WifiConfiguration(); + mWiFiConfiguration1.SSID = TEST_SSID2; + wifiList.add(mWiFiConfiguration1); + + WifiConfiguration mWiFiConfiguration2 = new WifiConfiguration(); + mWiFiConfiguration2.SSID = TEST_SSID3; + wifiList.add(mWiFiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks, timeout(2000)).networkNotFoundToConnectTo(); + } + + @Test + public void callbacks_failure_multipleNetworks_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration mWiFiConfiguration1 = new WifiConfiguration(); + mWiFiConfiguration1.SSID = TEST_SSID2; + wifiList.add(mWiFiConfiguration1); + + WifiConfiguration mWiFiConfiguration2 = new WifiConfiguration(); + mWiFiConfiguration2.SSID = TEST_SSID3; + wifiList.add(mWiFiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, null); + verify(connectToNetworkCallbacks, never()).networkNotFoundToConnectTo(); + } + + @Test + public void callbacks_failure_notAvailable() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(false); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks, timeout(2000)).failureConnectingToNetwork(); + } + + @Test + public void callbacks_failure_notAvailable_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(false); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, null); + verify(connectToNetworkCallbacks, never()).failureConnectingToNetwork(); + } + + @Test + public void callbacks_failure_notConnected() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(false); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks, timeout(2000)).failureConnectingToNetwork(); + } + + @Test + public void callbacks_failure_notConnected_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(false); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, null); + verify(connectToNetworkCallbacks, never()).failureConnectingToNetwork(); + } + + @Test + public void callbacks_failure_nullList() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(null); + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks, timeout(2000)).networkNotFoundToConnectTo(); + } + + @Test + public void callbacks_failure_nullList_nullCallback() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(null); + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, null); + verify(connectToNetworkCallbacks, never()).networkNotFoundToConnectTo(); + } + + @Test + public void callbacks_failure_nullSSIDParam() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(new ArrayList()); + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(null, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks, timeout(2000)).connectToNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullSSIDParam_nullCallback() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(new ArrayList()); + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(null, 1, null); + verify(connectToNetworkCallbacks, never()).connectToNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullSSID() { + setManagers(); + + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = null; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks, timeout(2000)).networkNotFoundToConnectTo(); + } + + @Test + public void callbacks_failure_nullSSID_nullCallback() { + setManagers(); + + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = null; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, null); + verify(connectToNetworkCallbacks,never()).networkNotFoundToConnectTo(); + } + + @Test + public void callbacks_failure_nullWifiConfiguration() { + setManagers(); + List wifiList = new ArrayList<>(); + wifiList.add(0, null); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks,timeout(2000)).networkNotFoundToConnectTo(); + } + + @Test + public void callbacks_failure_nullWifiConfiguration_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + wifiList.add(0, null); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, null); + verify(connectToNetworkCallbacks,never()).networkNotFoundToConnectTo(); + } + + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks, timeout(2000)).connectToNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, null); + verify(connectToNetworkCallbacks, never()).connectToNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks, timeout(2000)).connectedToNetwork(); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, null); + verify(connectToNetworkCallbacks, never()).connectedToNetwork(); + } + + @Test + public void callbacks_success_multipleNetworks() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration mWiFiConfiguration1 = new WifiConfiguration(); + mWiFiConfiguration1.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration1); + + WifiConfiguration mWiFiConfiguration2 = new WifiConfiguration(); + mWiFiConfiguration2.SSID = TEST_SSID2; + wifiList.add(mWiFiConfiguration2); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, connectToNetworkCallbacks); + verify(connectToNetworkCallbacks, timeout(2000)).connectedToNetwork(); + } + + @Test + public void callbacks_success_multipleNetworks_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration mWiFiConfiguration1 = new WifiConfiguration(); + mWiFiConfiguration1.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration1); + + WifiConfiguration mWiFiConfiguration2 = new WifiConfiguration(); + mWiFiConfiguration2.SSID = TEST_SSID2; + wifiList.add(mWiFiConfiguration2); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + ConnectToNetworkCallbacks connectToNetworkCallbacks = mock(ConnectToNetworkCallbacks.class); + mWiseFy.connectToNetwork(TEST_SSID, 1, null); + verify(connectToNetworkCallbacks, never()).connectedToNetwork(); + } +} \ No newline at end of file diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/DisableWifiTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/DisableWifiTests.java new file mode 100644 index 00000000..cb9cbf93 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/DisableWifiTests.java @@ -0,0 +1,106 @@ +package com.isupatches.wisefy.test; + + +import com.isupatches.wisefy.callbacks.DisableWifiCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class DisableWifiTests extends BaseTestClass { + + public DisableWifiTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(false); + boolean result = mWiseFy.disableWifi(); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(false); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + boolean result = mWiseFy.disableWifi(); + assertEquals(false, result); + } + + @Test + public void noCallbacks_success() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(true); + boolean result = mWiseFy.disableWifi(); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(false); + assertEquals(true, result); + } + + @Test + public void callbacks_failure() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(false); + DisableWifiCallbacks mockDisableWifiCallbacks = mock(DisableWifiCallbacks.class); + mWiseFy.disableWifi(mockDisableWifiCallbacks); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(false); + verify(mockDisableWifiCallbacks, timeout(2000)).failureDisablingWifi(); + } + + @Test + public void callbacks_failure_nullCallback() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(false); + DisableWifiCallbacks mockDisableWifiCallbacks = mock(DisableWifiCallbacks.class); + mWiseFy.disableWifi(null); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(false); + verify(mockDisableWifiCallbacks, never()).failureDisablingWifi(); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + DisableWifiCallbacks mockDisableWifiCallbacks = mock(DisableWifiCallbacks.class); + mWiseFy.disableWifi(mockDisableWifiCallbacks); + verify(mMockWiFiManager, never()).setWifiEnabled(false); + verify(mockDisableWifiCallbacks, timeout(2000)).disableWifiWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + DisableWifiCallbacks mockDisableWifiCallbacks = mock(DisableWifiCallbacks.class); + mWiseFy.disableWifi(null); + verify(mMockWiFiManager, never()).setWifiEnabled(false); + verify(mockDisableWifiCallbacks, never()).disableWifiWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(true); + DisableWifiCallbacks mockDisableWifiCallbacks = mock(DisableWifiCallbacks.class); + mWiseFy.disableWifi(mockDisableWifiCallbacks); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(false); + verify(mockDisableWifiCallbacks, timeout(2000)).wifiDisabled(); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(true); + DisableWifiCallbacks mockDisableWifiCallbacks = mock(DisableWifiCallbacks.class); + mWiseFy.disableWifi(null); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(false); + verify(mockDisableWifiCallbacks, never()).wifiDisabled(); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/DisconnectFromCurrentNetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/DisconnectFromCurrentNetworkTests.java new file mode 100644 index 00000000..647393df --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/DisconnectFromCurrentNetworkTests.java @@ -0,0 +1,105 @@ +package com.isupatches.wisefy.test; + + +import com.isupatches.wisefy.callbacks.DisconnectFromCurrentNetworkCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class DisconnectFromCurrentNetworkTests extends BaseTestClass { + + public DisconnectFromCurrentNetworkTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure() { + setManagers(); + when(mMockWiFiManager.disconnect()).thenReturn(false); + boolean result = mWiseFy.disconnectFromCurrentNetwork(); + verify(mMockWiFiManager, timeout(2000)).disconnect(); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + boolean result = mWiseFy.disconnectFromCurrentNetwork(); + assertEquals(false, result); + } + + @Test + public void noCallbacks_success() { + setManagers(); + when(mMockWiFiManager.disconnect()).thenReturn(true); + boolean result = mWiseFy.disconnectFromCurrentNetwork(); + verify(mMockWiFiManager, timeout(2000)).disconnect(); + assertEquals(true, result); + } + + @Test + public void callbacks_failure() { + setManagers(); + when(mMockWiFiManager.disconnect()).thenReturn(false); + DisconnectFromCurrentNetworkCallbacks disconnectFromCurrentNetworkCallbacks = mock(DisconnectFromCurrentNetworkCallbacks.class); + mWiseFy.disconnectFromCurrentNetwork(disconnectFromCurrentNetworkCallbacks); + verify(mMockWiFiManager, timeout(2000)).disconnect(); + verify(disconnectFromCurrentNetworkCallbacks, timeout(2000)).failureDisconnectingFromCurrentNetwork(); + } + + @Test + public void callbacks_failure_nullCallback() { + setManagers(); + when(mMockWiFiManager.disconnect()).thenReturn(false); + DisconnectFromCurrentNetworkCallbacks disconnectFromCurrentNetworkCallbacks = mock(DisconnectFromCurrentNetworkCallbacks.class); + mWiseFy.disconnectFromCurrentNetwork(null); + verify(mMockWiFiManager, timeout(2000)).disconnect(); + verify(disconnectFromCurrentNetworkCallbacks, never()).failureDisconnectingFromCurrentNetwork(); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + DisconnectFromCurrentNetworkCallbacks disconnectFromCurrentNetworkCallbacks = mock(DisconnectFromCurrentNetworkCallbacks.class); + mWiseFy.disconnectFromCurrentNetwork(disconnectFromCurrentNetworkCallbacks); + verify(mMockWiFiManager, never()).disconnect(); + verify(disconnectFromCurrentNetworkCallbacks, timeout(2000)).disconnectFromCurrentNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + DisconnectFromCurrentNetworkCallbacks disconnectFromCurrentNetworkCallbacks = mock(DisconnectFromCurrentNetworkCallbacks.class); + mWiseFy.disconnectFromCurrentNetwork(null); + verify(mMockWiFiManager, never()).disconnect(); + verify(disconnectFromCurrentNetworkCallbacks, never()).disconnectFromCurrentNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + when(mMockWiFiManager.disconnect()).thenReturn(true); + DisconnectFromCurrentNetworkCallbacks disconnectFromCurrentNetworkCallbacks = mock(DisconnectFromCurrentNetworkCallbacks.class); + mWiseFy.disconnectFromCurrentNetwork(disconnectFromCurrentNetworkCallbacks); + verify(mMockWiFiManager, timeout(2000)).disconnect(); + verify(disconnectFromCurrentNetworkCallbacks, timeout(2000)).disconnectedFromCurrentNetwork(); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + when(mMockWiFiManager.disconnect()).thenReturn(true); + DisconnectFromCurrentNetworkCallbacks disconnectFromCurrentNetworkCallbacks = mock(DisconnectFromCurrentNetworkCallbacks.class); + mWiseFy.disconnectFromCurrentNetwork(null); + verify(mMockWiFiManager, timeout(2000)).disconnect(); + verify(disconnectFromCurrentNetworkCallbacks, never()).disconnectedFromCurrentNetwork(); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/EnableWifiTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/EnableWifiTests.java new file mode 100644 index 00000000..ee6a7638 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/EnableWifiTests.java @@ -0,0 +1,106 @@ +package com.isupatches.wisefy.test; + + +import com.isupatches.wisefy.callbacks.EnableWifiCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class EnableWifiTests extends BaseTestClass { + + public EnableWifiTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(false); + boolean result = mWiseFy.enableWifi(); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(true); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + boolean result = mWiseFy.enableWifi(); + assertEquals(false, result); + } + + @Test + public void noCallbacks_success() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(true); + boolean result = mWiseFy.enableWifi(); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(true); + assertEquals(true, result); + } + + @Test + public void callbacks_failure() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(false); + EnableWifiCallbacks enableWifiCallbacks = mock(EnableWifiCallbacks.class); + mWiseFy.enableWifi(enableWifiCallbacks); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(true); + verify(enableWifiCallbacks, timeout(2000)).failureEnablingWifi(); + } + + @Test + public void callbacks_failure_nullCallback() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(false); + EnableWifiCallbacks enableWifiCallbacks = mock(EnableWifiCallbacks.class); + mWiseFy.enableWifi(null); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(true); + verify(enableWifiCallbacks, never()).failureEnablingWifi(); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + EnableWifiCallbacks enableWifiCallbacks = mock(EnableWifiCallbacks.class); + mWiseFy.enableWifi(enableWifiCallbacks); + verify(mMockWiFiManager, never()).setWifiEnabled(true); + verify(enableWifiCallbacks, timeout(2000)).enableWifiWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + EnableWifiCallbacks enableWifiCallbacks = mock(EnableWifiCallbacks.class); + mWiseFy.enableWifi(null); + verify(mMockWiFiManager, never()).setWifiEnabled(true); + verify(enableWifiCallbacks, never()).enableWifiWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(true); + EnableWifiCallbacks enableWifiCallbacks = mock(EnableWifiCallbacks.class); + mWiseFy.enableWifi(enableWifiCallbacks); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(true); + verify(enableWifiCallbacks, timeout(2000)).wifiEnabled(); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(true); + EnableWifiCallbacks enableWifiCallbacks = mock(EnableWifiCallbacks.class); + mWiseFy.enableWifi(null); + verify(mMockWiFiManager, timeout(2000)).setWifiEnabled(true); + verify(enableWifiCallbacks, never()).wifiEnabled(); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetCurrentNetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetCurrentNetworkTests.java new file mode 100644 index 00000000..a5e0fc7b --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetCurrentNetworkTests.java @@ -0,0 +1,80 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.WifiInfo; +import com.isupatches.wisefy.callbacks.GetCurrentNetworkCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class GetCurrentNetworkTests extends BaseTestClass { + + public GetCurrentNetworkTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + assertEquals(null, mWiseFy.getCurrentNetwork()); + } + + @Test + public void noCallbacks_success() { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getSSID()).thenReturn(TEST_SSID); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + assertEquals(TEST_SSID, mWiseFy.getCurrentNetwork().getSSID()); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + GetCurrentNetworkCallbacks getCurrentNetworkCallbacks = mock(GetCurrentNetworkCallbacks.class); + mWiseFy.getCurrentNetwork(getCurrentNetworkCallbacks); + verify(getCurrentNetworkCallbacks, timeout(2000)).getCurrentNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallbacks() { + setManagersToNull(); + GetCurrentNetworkCallbacks getCurrentNetworkCallbacks = mock(GetCurrentNetworkCallbacks.class); + mWiseFy.getCurrentNetwork(null); + verify(getCurrentNetworkCallbacks, never()).getCurrentNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getSSID()).thenReturn(TEST_SSID); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + GetCurrentNetworkCallbacks getCurrentNetworkCallbacks = mock(GetCurrentNetworkCallbacks.class); + mWiseFy.getCurrentNetwork(getCurrentNetworkCallbacks); + verify(getCurrentNetworkCallbacks, timeout(2000)).retrievedCurrentNetwork(mockWifiInfo); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getSSID()).thenReturn(TEST_SSID); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + GetCurrentNetworkCallbacks getCurrentNetworkCallbacks = mock(GetCurrentNetworkCallbacks.class); + mWiseFy.getCurrentNetwork(null); + verify(getCurrentNetworkCallbacks, never()).retrievedCurrentNetwork(mockWifiInfo); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetFrequencyTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetFrequencyTests.java new file mode 100644 index 00000000..29c1b7cc --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetFrequencyTests.java @@ -0,0 +1,161 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.WifiInfo; +import android.os.Build; +import com.isupatches.wisefy.callbacks.GetFrequencyCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_NETWORK_FREQUENCY_24GHZ; +import static junit.framework.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class GetFrequencyTests extends BaseTestClass { + + public GetFrequencyTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_getFrequency_currentNetwork_failure() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(null); + assertEquals(null, mWiseFy.getFrequency()); + } + } + + @Test + public void noCallbacks_getFrequency_currentNetwork_success() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_24GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + assertEquals(TEST_NETWORK_FREQUENCY_24GHZ, (int) mWiseFy.getFrequency()); + } + } + + @Test + public void callbacks_getFrequency_currentNetwork_failure() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(null); + GetFrequencyCallbacks mockGetFrequencyCallbacks = mock(GetFrequencyCallbacks.class); + mWiseFy.getFrequency(mockGetFrequencyCallbacks); + verify(mockGetFrequencyCallbacks, timeout(2000)).failureGettingFrequency(); + } + } + + @Test + public void callbacks_getFrequency_currentNetwork_failure_nullCallback() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(null); + GetFrequencyCallbacks mockGetFrequencyCallbacks = mock(GetFrequencyCallbacks.class); + mWiseFy.getFrequency((GetFrequencyCallbacks) null); + verify(mockGetFrequencyCallbacks, never()).failureGettingFrequency(); + } + } + + @Test + public void callbacks_getFrequency_currentNetwork_success() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_24GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + GetFrequencyCallbacks mockGetFrequencyCallbacks = mock(GetFrequencyCallbacks.class); + mWiseFy.getFrequency(mockGetFrequencyCallbacks); + verify(mockGetFrequencyCallbacks, timeout(2000)).retrievedFrequency(TEST_NETWORK_FREQUENCY_24GHZ); + } + } + + @Test + public void callbacks_getFrequency_currentNetwork_success_nullCallback() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_24GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + GetFrequencyCallbacks mockGetFrequencyCallbacks = mock(GetFrequencyCallbacks.class); + mWiseFy.getFrequency((GetFrequencyCallbacks) null); + verify(mockGetFrequencyCallbacks, never()).retrievedFrequency(TEST_NETWORK_FREQUENCY_24GHZ); + } + } + + @Test + public void noCallbacks_getFrequency_provideWifiInfo_failure() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(null); + assertEquals(null, mWiseFy.getFrequency((WifiInfo) null)); + } + } + + @Test + public void noCallbacks_getFrequency_provideWifiInfo_success() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_24GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + assertEquals(TEST_NETWORK_FREQUENCY_24GHZ, (int) mWiseFy.getFrequency(mockWifiInfo)); + } + } + + @Test + public void callbacks_getFrequency_provideWifiInfo_failure() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(null); + GetFrequencyCallbacks mockGetFrequencyCallbacks = mock(GetFrequencyCallbacks.class); + mWiseFy.getFrequency(null, mockGetFrequencyCallbacks); + verify(mockGetFrequencyCallbacks, timeout(2000)).getFrequencyWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + } + + @Test + public void callbacks_getFrequency_provideWifiInfo_failure_nullCallback() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(null); + GetFrequencyCallbacks mockGetFrequencyCallbacks = mock(GetFrequencyCallbacks.class); + mWiseFy.getFrequency(null, null); + verify(mockGetFrequencyCallbacks, never()).failureGettingFrequency(); + } + } + + @Test + public void callbacks_getFrequency_provideWifiInfo_success() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_24GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + GetFrequencyCallbacks mockGetFrequencyCallbacks = mock(GetFrequencyCallbacks.class); + mWiseFy.getFrequency(mockWifiInfo, mockGetFrequencyCallbacks); + verify(mockGetFrequencyCallbacks, timeout(2000)).retrievedFrequency(TEST_NETWORK_FREQUENCY_24GHZ); + } + } + + @Test + public void callbacks_getFrequency_provideWifiInfo_success_nullCallback() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_24GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + GetFrequencyCallbacks mockGetFrequencyCallbacks = mock(GetFrequencyCallbacks.class); + mWiseFy.getFrequency(mockWifiInfo, null); + verify(mockGetFrequencyCallbacks, never()).retrievedFrequency(TEST_NETWORK_FREQUENCY_24GHZ); + } + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetNearbyAccessPointsTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetNearbyAccessPointsTests.java new file mode 100644 index 00000000..1330a23e --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetNearbyAccessPointsTests.java @@ -0,0 +1,323 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.ScanResult; +import com.isupatches.wisefy.callbacks.GetNearbyAccessPointsCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID2; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class GetNearbyAccessPointsTests extends BaseTestClass { + + public GetNearbyAccessPointsTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_success_filterDuplicates_false() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID2; + scanResults.add(scanResult2); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + List results = mWiseFy.getNearbyAccessPoints(false); + + assertEquals(scanResults, results); + assertEquals(2, results.size()); + } + + @Test + public void noCallbacks_success_filterDuplicates_true_differentSSIDs() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID2; + scanResults.add(scanResult2); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + List results = mWiseFy.getNearbyAccessPoints(true); + + assertEquals(scanResults, results); + assertEquals(2, results.size()); + } + + @Test + public void noCallbacks_success_filterDuplicates_true_sameSSIDs_higherFrequency() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResult1.level = -35; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID; + scanResult2.level = -70; + scanResults.add(scanResult2); + + List expectedScanResults = new ArrayList<>(); + expectedScanResults.add(scanResult1); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + List results = mWiseFy.getNearbyAccessPoints(true); + + assertEquals(expectedScanResults, results); + assertEquals(1, results.size()); + } + + @Test + public void noCallbacks_filterDuplicates_true_sameSSIDs_lowerFrequency() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResult1.level = -70; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID; + scanResult2.level = -35; + scanResults.add(scanResult2); + + List expectedScanResults = new ArrayList<>(); + expectedScanResults.add(scanResult2); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + List results = mWiseFy.getNearbyAccessPoints(true); + + assertEquals(expectedScanResults, results); + assertEquals(1, results.size()); + } + + @Test + public void noCallbacks_failure_filterDuplicates_false_nullWifiManager() { + setManagersToNull(); + List results = mWiseFy.getNearbyAccessPoints(false); + assertEquals(null, results); + } + + @Test + public void noCallbacks_failure_filterDuplicates_true_nullWifiManager() { + setManagersToNull(); + List results = mWiseFy.getNearbyAccessPoints(true); + assertEquals(null, results); + } + + @Test + public void callbacks_success_filterDuplicates_false() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID2; + scanResults.add(scanResult2); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(false, getNearbyAccessPointsCallbacks); + verify(getNearbyAccessPointsCallbacks, timeout(2000)).retrievedNearbyAccessPoints(scanResults); + } + + @Test + public void callbacks_success_filterDuplicates_false_nullCallback() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID2; + scanResults.add(scanResult2); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(false, null); + verify(getNearbyAccessPointsCallbacks, never()).retrievedNearbyAccessPoints(scanResults); + } + + @Test + public void callbacks_success_filterDuplicates_true_differentSSIDs() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID2; + scanResults.add(scanResult2); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(true, getNearbyAccessPointsCallbacks); + verify(getNearbyAccessPointsCallbacks, timeout(2000)).retrievedNearbyAccessPoints(scanResults); + } + + @Test + public void callbacks_success_filterDuplicates_true_differentSSIDs_nullCallback() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID2; + scanResults.add(scanResult2); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(true, null); + verify(getNearbyAccessPointsCallbacks, never()).retrievedNearbyAccessPoints(scanResults); + } + + @Test + public void callbacks_success_filterDuplicates_true_sameSSIDs_higherFrequency() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResult1.level = -35; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID; + scanResult2.level = -70; + scanResults.add(scanResult2); + + List expectedScanResults = new ArrayList<>(); + expectedScanResults.add(scanResult1); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(true, getNearbyAccessPointsCallbacks); + verify(getNearbyAccessPointsCallbacks, timeout(2000)).retrievedNearbyAccessPoints(expectedScanResults); + } + + @Test + public void callbacks_success_filterDuplicates_true_sameSSIDs_higherFrequency_nullCallback() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResult1.level = -35; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID; + scanResult2.level = -70; + scanResults.add(scanResult2); + + List expectedScanResults = new ArrayList<>(); + expectedScanResults.add(scanResult1); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(true, null); + verify(getNearbyAccessPointsCallbacks, never()).retrievedNearbyAccessPoints(expectedScanResults); + } + + @Test + public void callbacks_filterDuplicates_true_sameSSIDs_lowerFrequency() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResult1.level = -70; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID; + scanResult2.level = -35; + scanResults.add(scanResult2); + + List expectedScanResults = new ArrayList<>(); + expectedScanResults.add(scanResult2); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(true, getNearbyAccessPointsCallbacks); + verify(getNearbyAccessPointsCallbacks, timeout(2000)).retrievedNearbyAccessPoints(expectedScanResults); + } + + @Test + public void callbacks_filterDuplicates_true_sameSSIDs_lowerFrequency_nullCallback() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult1 = mock(ScanResult.class); + scanResult1.SSID = TEST_SSID; + scanResult1.level = -70; + scanResults.add(scanResult1); + ScanResult scanResult2 = mock(ScanResult.class); + scanResult2.SSID = TEST_SSID; + scanResult2.level = -35; + scanResults.add(scanResult2); + + List expectedScanResults = new ArrayList<>(); + expectedScanResults.add(scanResult2); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(true, null); + verify(getNearbyAccessPointsCallbacks, never()).retrievedNearbyAccessPoints(expectedScanResults); + } + + @Test + public void callbacks_failure_filterDuplicates_false_nullWifiManager() { + setManagersToNull(); + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(false, getNearbyAccessPointsCallbacks); + verify(getNearbyAccessPointsCallbacks, timeout(2000)).getNearbyAccessPointsWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_filterDuplicates_false_nullWifiManager_nullCallback() { + setManagersToNull(); + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(false, null); + verify(getNearbyAccessPointsCallbacks, never()).getNearbyAccessPointsWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_filterDuplicates_true_nullWifiManager() { + setManagersToNull(); + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(true, getNearbyAccessPointsCallbacks); + verify(getNearbyAccessPointsCallbacks, timeout(2000)).getNearbyAccessPointsWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_filterDuplicates_true_nullWifiManager_nullCallback() { + setManagersToNull(); + GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks = mock(GetNearbyAccessPointsCallbacks.class); + mWiseFy.getNearbyAccessPoints(true, null); + verify(getNearbyAccessPointsCallbacks, never()).getNearbyAccessPointsWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetSavedNetworkTest.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetSavedNetworkTest.java new file mode 100644 index 00000000..c1e41091 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetSavedNetworkTest.java @@ -0,0 +1,307 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.WifiConfiguration; +import com.isupatches.wisefy.callbacks.GetSavedNetworkCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID2; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID3; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class GetSavedNetworkTest extends BaseTestClass { + + public GetSavedNetworkTest() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + assertEquals(null, mWiseFy.getSavedNetwork(TEST_SSID)); + } + + @Test + public void noCallbacks_failure_nullSSIDParam() { + setManagers(); + assertEquals(null, mWiseFy.getSavedNetwork(null)); + } + + @Test + public void noCallbacks_failure_differentSSID() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = TEST_SSID2; + wifiList.add(wifiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + assertEquals(null, mWiseFy.getSavedNetwork(TEST_SSID)); + } + + @Test + public void noCallbacks_failure_nullSSID() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = null; + wifiList.add(wifiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + assertEquals(null, mWiseFy.getSavedNetwork(TEST_SSID)); + } + + @Test + public void noCallbacks_failure_multipleNetworks() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration wifiConfiguration1 = new WifiConfiguration(); + wifiConfiguration1.SSID = TEST_SSID2; + wifiList.add(wifiConfiguration1); + + WifiConfiguration wifiConfiguration2 = new WifiConfiguration(); + wifiConfiguration2.SSID = TEST_SSID3; + wifiList.add(wifiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + assertEquals(null, mWiseFy.getSavedNetwork(TEST_SSID)); + } + + @Test + public void noCallbacks_success() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = TEST_SSID; + wifiList.add(wifiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + assertEquals(wifiConfiguration, mWiseFy.getSavedNetwork(TEST_SSID)); + } + + @Test + public void noCallbacks_success_multipleNetworks() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration wifiConfiguration1 = new WifiConfiguration(); + wifiConfiguration1.SSID = TEST_SSID; + wifiList.add(wifiConfiguration1); + + WifiConfiguration wifiConfiguration2 = new WifiConfiguration(); + wifiConfiguration2.SSID = TEST_SSID2; + wifiList.add(wifiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + assertEquals(wifiConfiguration1, mWiseFy.getSavedNetwork(TEST_SSID)); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, getSavedNetworkCallbacks); + verify(getSavedNetworkCallbacks, timeout(2000)).getSavedNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, null); + verify(getSavedNetworkCallbacks, never()).getSavedNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullSSIDParam() { + setManagers(); + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(null, getSavedNetworkCallbacks); + verify(getSavedNetworkCallbacks, timeout(2000)).getSavedNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullSSIDParam_nullCallback() { + setManagers(); + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(null, null); + verify(getSavedNetworkCallbacks, never()).getSavedNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_differentSSID() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = TEST_SSID2; + wifiList.add(wifiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, getSavedNetworkCallbacks); + verify(getSavedNetworkCallbacks, timeout(2000)).networkNotFound(); + } + + @Test + public void callbacks_failure_differentSSID_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = TEST_SSID2; + wifiList.add(wifiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, null); + verify(getSavedNetworkCallbacks, never()).networkNotFound(); + } + + @Test + public void callbacks_failure_nullSSID() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = null; + wifiList.add(wifiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, getSavedNetworkCallbacks); + verify(getSavedNetworkCallbacks, timeout(2000)).networkNotFound(); + } + + @Test + public void callbacks_failure_nullSSID_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = null; + wifiList.add(wifiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, null); + verify(getSavedNetworkCallbacks, never()).networkNotFound(); + } + + @Test + public void callbacks_failure_multipleNetworks() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration wifiConfiguration1 = new WifiConfiguration(); + wifiConfiguration1.SSID = TEST_SSID2; + wifiList.add(wifiConfiguration1); + + WifiConfiguration wifiConfiguration2 = new WifiConfiguration(); + wifiConfiguration2.SSID = TEST_SSID3; + wifiList.add(wifiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, getSavedNetworkCallbacks); + verify(getSavedNetworkCallbacks, timeout(2000)).networkNotFound(); + } + + @Test + public void callbacks_failure_multipleNetworks_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration wifiConfiguration1 = new WifiConfiguration(); + wifiConfiguration1.SSID = TEST_SSID2; + wifiList.add(wifiConfiguration1); + + WifiConfiguration wifiConfiguration2 = new WifiConfiguration(); + wifiConfiguration2.SSID = TEST_SSID3; + wifiList.add(wifiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, null); + verify(getSavedNetworkCallbacks, never()).networkNotFound(); + } + + @Test + public void callbacks_success() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = TEST_SSID; + wifiList.add(wifiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, getSavedNetworkCallbacks); + verify(getSavedNetworkCallbacks, timeout(2000)).retrievedSavedNetwork(wifiConfiguration); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = TEST_SSID; + wifiList.add(wifiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, null); + verify(getSavedNetworkCallbacks, never()).retrievedSavedNetwork(wifiConfiguration); + } + + @Test + public void callbacks_success_multipleNetworks() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration wifiConfiguration1 = new WifiConfiguration(); + wifiConfiguration1.SSID = TEST_SSID; + wifiList.add(wifiConfiguration1); + + WifiConfiguration wifiConfiguration2 = new WifiConfiguration(); + wifiConfiguration2.SSID = TEST_SSID2; + wifiList.add(wifiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, getSavedNetworkCallbacks); + verify(getSavedNetworkCallbacks, timeout(2000)).retrievedSavedNetwork(wifiConfiguration1); } + + @Test + public void callbacks_success_multipleNetworks_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + + WifiConfiguration wifiConfiguration1 = new WifiConfiguration(); + wifiConfiguration1.SSID = TEST_SSID; + wifiList.add(wifiConfiguration1); + + WifiConfiguration wifiConfiguration2 = new WifiConfiguration(); + wifiConfiguration2.SSID = TEST_SSID2; + wifiList.add(wifiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworkCallbacks getSavedNetworkCallbacks = mock(GetSavedNetworkCallbacks.class); + mWiseFy.getSavedNetwork(TEST_SSID, null); + verify(getSavedNetworkCallbacks, never()).retrievedSavedNetwork(wifiConfiguration1); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetSavedNetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetSavedNetworkTests.java new file mode 100644 index 00000000..14e0a794 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/GetSavedNetworkTests.java @@ -0,0 +1,88 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.WifiConfiguration; +import com.isupatches.wisefy.callbacks.GetSavedNetworksCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class GetSavedNetworkTests extends BaseTestClass { + + public GetSavedNetworkTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + assertEquals(null, mWiseFy.getSavedNetworks()); + } + + @Test + public void noCallbacks_success() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + assertEquals(wifiList, mWiseFy.getSavedNetworks()); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + GetSavedNetworksCallbacks getSavedNetworksCallbacks = mock(GetSavedNetworksCallbacks.class); + mWiseFy.getSavedNetworks(getSavedNetworksCallbacks); + verify(getSavedNetworksCallbacks, timeout(2000)).getSavedNetworksWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + GetSavedNetworksCallbacks getSavedNetworksCallbacks = mock(GetSavedNetworksCallbacks.class); + mWiseFy.getSavedNetworks(null); + verify(getSavedNetworksCallbacks, never()).getSavedNetworksWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworksCallbacks getSavedNetworksCallbacks = mock(GetSavedNetworksCallbacks.class); + mWiseFy.getSavedNetworks(getSavedNetworksCallbacks); + verify(getSavedNetworksCallbacks, timeout(2000)).retrievedSavedNetworks(wifiList); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + + GetSavedNetworksCallbacks getSavedNetworksCallbacks = mock(GetSavedNetworksCallbacks.class); + mWiseFy.getSavedNetworks(null); + verify(getSavedNetworksCallbacks, never()).retrievedSavedNetworks(wifiList); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToMobileNetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToMobileNetworkTests.java new file mode 100644 index 00000000..aea7a393 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToMobileNetworkTests.java @@ -0,0 +1,103 @@ +package com.isupatches.wisefy.test; + + +import android.net.NetworkInfo; +import com.isupatches.wisefy.constants.NetworkTypes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class IsDeviceConnectedToMobileNetworkTests extends BaseTestClass { + + public IsDeviceConnectedToMobileNetworkTests() { + super(TestActivity.class); + } + + @Test + public void failure_notAvailable() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(false); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_notConnected() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(false); + + boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_nullActiveNetworkInfo() { + setManagers(); + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); + boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_nullConfigurationManager() { + setManagers(); + setConnectivityManagerToNull(); + boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_nullTypeName() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(null); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + + boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_wrongType() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn("WRONG TYPE"); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); + assertEquals(false, result); + } + + @Test + public void success() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); + assertEquals(true, result); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToMobileOrWifiNetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToMobileOrWifiNetworkTests.java new file mode 100644 index 00000000..1aa08933 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToMobileOrWifiNetworkTests.java @@ -0,0 +1,118 @@ +package com.isupatches.wisefy.test; + + +import android.net.NetworkInfo; +import com.isupatches.wisefy.constants.NetworkTypes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class IsDeviceConnectedToMobileOrWifiNetworkTests extends BaseTestClass { + + public IsDeviceConnectedToMobileOrWifiNetworkTests() { + super(TestActivity.class); + } + + @Test + public void failure_mobile_notAvailable() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(false); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_mobile_notConnected() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(false); + + boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_nullActiveNetworkInfo() { + setManagers(); + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); + boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_nullConfigurationManager() { + setManagersToNull(); + boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_wifi_notAvailable() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(false); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_wifi_notConnected() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(false); + + boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void success_mobile() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); + assertEquals(true, result); + } + + @Test + public void success_wifi() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); + assertEquals(true, result); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToSSIDTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToSSIDTests.java new file mode 100644 index 00000000..927b3737 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToSSIDTests.java @@ -0,0 +1,206 @@ +package com.isupatches.wisefy.test; + + +import android.net.NetworkInfo; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiInfo; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID2; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class IsDeviceConnectedToSSIDTests extends BaseTestClass { + + public IsDeviceConnectedToSSIDTests() { + super(TestActivity.class); + } + + @Test + public void failure_differentSSID() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID2; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void failure_notAvailable() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(false); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void failure_notConnected() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(false); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void failure_nullActiveNetworkInfo() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void failure_nullConnectionInfo() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(null); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void failure_nullConnectionInfoSSID() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(null); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void failure_nullConnectivityManagerManager() { + setManagers(); + setConnectivityManagerToNull(); + + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void failure_nullSSIDParam() { + setManagers(); + boolean result = mWiseFy.isDeviceConnectedToSSID(null); + assertEquals(false, result); + } + + @Test + public void failure_nullWifiManager() { + setManagersToNull(); + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void success() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + + WifiInfo mockWiFiInfo = mock(WifiInfo.class); + when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); + + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); + assertEquals(true, result); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToWifiNetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToWifiNetworkTests.java new file mode 100644 index 00000000..4bc8049a --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsDeviceConnectedToWifiNetworkTests.java @@ -0,0 +1,101 @@ +package com.isupatches.wisefy.test; + + +import android.net.NetworkInfo; +import com.isupatches.wisefy.constants.NetworkTypes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class IsDeviceConnectedToWifiNetworkTests extends BaseTestClass { + + public IsDeviceConnectedToWifiNetworkTests() { + super(TestActivity.class); + } + + @Test + public void failure_notAvailable() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(false); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_notConnected() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(false); + + boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_nullActiveNetworkInfo() { + setManagers(); + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); + boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_nullConfigurationManager() { + setManagers(); + setConnectivityManagerToNull(); + boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_nullTypeName() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(null); + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void failure_wrongType() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn("WRONG TYPE"); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); + assertEquals(false, result); + } + + @Test + public void success() { + setManagers(); + NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); + when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); + + when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); + when(mockNetworkInfo.isAvailable()).thenReturn(true); + when(mockNetworkInfo.isConnected()).thenReturn(true); + + boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); + assertEquals(true, result); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsNetwork5gHzTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsNetwork5gHzTests.java new file mode 100644 index 00000000..4b2aa279 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsNetwork5gHzTests.java @@ -0,0 +1,94 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.WifiInfo; +import android.os.Build; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_NETWORK_FREQUENCY_5GHZ; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_NETWORK_FREQUENCY_ABOVE_5GHZ; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_NETWORK_FREQUENCY_BELOW_5GHZ; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + + +public class IsNetwork5gHzTests extends BaseTestClass { + + public IsNetwork5gHzTests() { + super(TestActivity.class); + } + + @Test + public void currentNetwork_failure_above5ghz() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_ABOVE_5GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + assertEquals(false, mWiseFy.isNetwork5gHz()); + } + } + + @Test + public void currentNetwork_failure_below5ghz() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_BELOW_5GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + assertEquals(false, mWiseFy.isNetwork5gHz()); + } + } + + @Test + public void currentNetwork_success() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_5GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + assertEquals(true, mWiseFy.isNetwork5gHz()); + } + } + + @Test + public void provideWifiInfo_failure_above5ghz() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_ABOVE_5GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + assertEquals(false, mWiseFy.isNetwork5gHz(mockWifiInfo)); + } + } + + @Test + public void provideWifiInfo_failure_below5ghz() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_BELOW_5GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + assertEquals(false, mWiseFy.isNetwork5gHz(mockWifiInfo)); + } + } + + @Test + public void provideWifiInfo_success() { + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setManagers(); + WifiInfo mockWifiInfo = mock(WifiInfo.class); + when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_5GHZ); + when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); + + assertEquals(true, mWiseFy.isNetwork5gHz(mockWifiInfo)); + } + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsNetworkInConfigurationListTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsNetworkInConfigurationListTests.java new file mode 100644 index 00000000..dff68958 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsNetworkInConfigurationListTests.java @@ -0,0 +1,109 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.WifiConfiguration; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID2; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID3; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + + +public class IsNetworkInConfigurationListTests extends BaseTestClass { + + public IsNetworkInConfigurationListTests() { + super(TestActivity.class); + } + + @Test + public void failure() { + setManagers(); + assertEquals(false, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); + } + + @Test + public void failure_multipleNetworks() { + setManagers(); + + List wifiList = new ArrayList<>(); + + WifiConfiguration wifiConfiguration1 = new WifiConfiguration(); + wifiConfiguration1.SSID = TEST_SSID2; + wifiList.add(wifiConfiguration1); + + WifiConfiguration wiFiConfiguration2 = new WifiConfiguration(); + wiFiConfiguration2.SSID = TEST_SSID3; + wifiList.add(wiFiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + assertEquals(false, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); + } + + @Test + public void failure_noNetworks() { + setManagers(); + List wifiList = new ArrayList<>(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + assertEquals(false, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); + } + + @Test + public void failure_nullSSID() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = null; + wifiList.add(wifiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + assertEquals(false, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); + } + + @Test + public void failure_nullWifiConfiguration() { + setManagers(); + List wifiList = new ArrayList<>(); + wifiList.add(0, null); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + assertEquals(false, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); + } + + @Test + public void failure_nullWifiManager() { + setManagersToNull(); + assertEquals(false, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); + } + + @Test + public void success() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + assertEquals(true, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); + } + + @Test + public void success_multipleNetworks() { + setManagers(); + + List wifiList = new ArrayList<>(); + + WifiConfiguration wifiConfiguration1 = new WifiConfiguration(); + wifiConfiguration1.SSID = TEST_SSID; + wifiList.add(wifiConfiguration1); + + WifiConfiguration wiFiConfiguration2 = new WifiConfiguration(); + wiFiConfiguration2.SSID = TEST_SSID2; + wifiList.add(wiFiConfiguration2); + + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + assertEquals(true, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsNetworkSecureTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsNetworkSecureTests.java new file mode 100644 index 00000000..9dcc7e32 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsNetworkSecureTests.java @@ -0,0 +1,57 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.ScanResult; +import com.isupatches.wisefy.constants.Capabilities; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; + + +public class IsNetworkSecureTests extends BaseTestClass { + + public IsNetworkSecureTests() { + super(TestActivity.class); + } + + @Test + public void failure_emptyCapabilities() { + ScanResult scanResult = mock(ScanResult.class); + scanResult.capabilities = ""; + assertEquals(false, mWiseFy.isNetworkSecure(scanResult)); + } + + @Test + public void failure_nullCapabilities() { + ScanResult scanResult = mock(ScanResult.class); + assertEquals(false, mWiseFy.isNetworkSecure(scanResult)); + } + + @Test + public void failure_nullScanResult() { + assertEquals(false, mWiseFy.isNetworkSecure(null)); + } + + @Test + public void success_withEAP() { + ScanResult scanResult = mock(ScanResult.class); + scanResult.capabilities = Capabilities.EAP; + assertEquals(true, mWiseFy.isNetworkSecure(scanResult)); + } + + @Test + public void success_withPSK() { + ScanResult scanResult = mock(ScanResult.class); + scanResult.capabilities = Capabilities.PSK; + assertEquals(true, mWiseFy.isNetworkSecure(scanResult)); + } + + @Test + public void success_withWEP() { + ScanResult scanResult = mock(ScanResult.class); + scanResult.capabilities = Capabilities.WEP; + assertEquals(true, mWiseFy.isNetworkSecure(scanResult)); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsWifiEnabledTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsWifiEnabledTests.java new file mode 100644 index 00000000..62aa4702 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/IsWifiEnabledTests.java @@ -0,0 +1,36 @@ +package com.isupatches.wisefy.test; + + +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; + + +public class IsWifiEnabledTests extends BaseTestClass { + + public IsWifiEnabledTests() { + super(TestActivity.class); + } + + @Test + public void failure() { + setManagers(); + when(mMockWiFiManager.isWifiEnabled()).thenReturn(false); + assertEquals(false, mWiseFy.isWifiEnabled()); + } + + @Test + public void failure_nullWifiManager() { + setManagersToNull(); + assertEquals(false, mWiseFy.isWifiEnabled()); + } + + @Test + public void success() { + setManagers(); + when(mMockWiFiManager.isWifiEnabled()).thenReturn(true); + assertEquals(true, mWiseFy.isWifiEnabled()); + } +} \ No newline at end of file diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/RemoveNetworkTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/RemoveNetworkTests.java new file mode 100644 index 00000000..6e2fde89 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/RemoveNetworkTests.java @@ -0,0 +1,185 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.WifiConfiguration; +import com.isupatches.wisefy.callbacks.RemoveNetworkCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID; +import static com.isupatches.wisefy.test.base.TestUtils.getTestWifiConfiguration; +import static org.junit.Assert.assertEquals; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class RemoveNetworkTests extends BaseTestClass { + + public RemoveNetworkTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(getTestWifiConfiguration()); + when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(false); + + boolean result = mWiseFy.removeNetwork(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_notInList() { + setManagers(); + List wifiList = new ArrayList<>(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(false); + + boolean result = mWiseFy.removeNetwork(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_nullSSIDParam() { + setManagersToNull(); + boolean result = mWiseFy.removeNetwork(null); + assertEquals(false, result); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + boolean result = mWiseFy.removeNetwork(TEST_SSID); + assertEquals(false, result); + } + + @Test + public void noCallbacks_success() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(true); + + boolean result = mWiseFy.removeNetwork(TEST_SSID); + assertEquals(true, result); + } + + @Test + public void callbacks_failure() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(getTestWifiConfiguration()); + when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(false); + + RemoveNetworkCallbacks removeNetworkCallbacks = mock(RemoveNetworkCallbacks.class); + mWiseFy.removeNetwork(TEST_SSID, removeNetworkCallbacks); + verify(removeNetworkCallbacks, timeout(2000)).failureRemovingNetwork(); + } + + @Test + public void callbacks_failure_nullCallback() { + setManagers(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(getTestWifiConfiguration()); + when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(false); + + RemoveNetworkCallbacks removeNetworkCallbacks = mock(RemoveNetworkCallbacks.class); + mWiseFy.removeNetwork(TEST_SSID, null); + verify(removeNetworkCallbacks, never()).failureRemovingNetwork(); + } + + @Test + public void callbacks_failure_notInList() { + setManagers(); + List wifiList = new ArrayList<>(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(false); + + RemoveNetworkCallbacks removeNetworkCallbacks = mock(RemoveNetworkCallbacks.class); + mWiseFy.removeNetwork(TEST_SSID, removeNetworkCallbacks); + verify(removeNetworkCallbacks, timeout(2000)).networkNotFoundToRemove(); + } + + @Test + public void callbacks_failure_notInList_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(false); + + RemoveNetworkCallbacks removeNetworkCallbacks = mock(RemoveNetworkCallbacks.class); + mWiseFy.removeNetwork(TEST_SSID, null); + verify(removeNetworkCallbacks, never()).networkNotFoundToRemove(); + } + + @Test + public void callbacks_failure_nullSSIDParam() { + setManagers(); + RemoveNetworkCallbacks removeNetworkCallbacks = mock(RemoveNetworkCallbacks.class); + mWiseFy.removeNetwork(null, removeNetworkCallbacks); + verify(removeNetworkCallbacks, timeout(2000)).removeNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullSSIDParam_nullCallback() { + setManagers(); + RemoveNetworkCallbacks removeNetworkCallbacks = mock(RemoveNetworkCallbacks.class); + mWiseFy.removeNetwork(null, null); + verify(removeNetworkCallbacks, never()).removeNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + RemoveNetworkCallbacks removeNetworkCallbacks = mock(RemoveNetworkCallbacks.class); + mWiseFy.removeNetwork(TEST_SSID, removeNetworkCallbacks); + verify(removeNetworkCallbacks, timeout(2000)).removeNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + RemoveNetworkCallbacks removeNetworkCallbacks = mock(RemoveNetworkCallbacks.class); + mWiseFy.removeNetwork(TEST_SSID, null); + verify(removeNetworkCallbacks, never()).removeNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(true); + + RemoveNetworkCallbacks removeNetworkCallbacks = mock(RemoveNetworkCallbacks.class); + mWiseFy.removeNetwork(TEST_SSID, removeNetworkCallbacks); + verify(removeNetworkCallbacks, timeout(2000)).networkRemoved(); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + List wifiList = new ArrayList<>(); + WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); + mWiFiConfiguration.SSID = TEST_SSID; + wifiList.add(mWiFiConfiguration); + when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); + when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(true); + + RemoveNetworkCallbacks removeNetworkCallbacks = mock(RemoveNetworkCallbacks.class); + mWiseFy.removeNetwork(TEST_SSID, null); + verify(removeNetworkCallbacks, never()).networkRemoved(); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/SearchForSSIDTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/SearchForSSIDTests.java new file mode 100644 index 00000000..995480f6 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/SearchForSSIDTests.java @@ -0,0 +1,253 @@ +package com.isupatches.wisefy.test; + + +import android.net.wifi.ScanResult; +import com.isupatches.wisefy.callbacks.SearchForSSIDCallbacks; +import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import java.util.ArrayList; +import java.util.List; +import static com.isupatches.wisefy.test.base.TestUtils.TEST_SSID; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.timeout; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class SearchForSSIDTests extends BaseTestClass { + + public SearchForSSIDTests() { + super(TestActivity.class); + } + + @Test + public void noCallbacks_failure() { + setManagers(); + assertEquals(null, mWiseFy.searchForSSID(TEST_SSID, 1)); + } + + @Test + public void noCallbacks_failure_nullSSID() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = null; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + assertEquals(null, mWiseFy.searchForSSID(TEST_SSID, 1)); + } + + @Test + public void noCallbacks_failure_nullSSIDParam() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = null; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + assertEquals(null, mWiseFy.searchForSSID(null, 1)); + } + + @Test + public void noCallbacks_failure_differentSSID() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = "Another SSID"; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + assertEquals(null, mWiseFy.searchForSSID(TEST_SSID, 1)); + } + + @Test + public void noCallbacks_failure_nullWifiManager() { + setManagersToNull(); + assertEquals(null, mWiseFy.searchForSSID(TEST_SSID, 1)); + } + + @Test + public void noCallbacks_success() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = TEST_SSID; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + assertEquals(TEST_SSID, mWiseFy.searchForSSID(TEST_SSID, 1)); + } + + @Test + public void callbacks_failure() { + setManagers(); + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(TEST_SSID, 1, mockSearchForSSIDCallbacks); + verify(mockSearchForSSIDCallbacks, timeout(2000)).ssidNotFound(); + } + + @Test + public void callbacks_failure_nullCallback() { + setManagers(); + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(TEST_SSID, 1, null); + verify(mockSearchForSSIDCallbacks, never()).ssidNotFound(); + } + + @Test + public void callbacks_failure_nullSSID() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = null; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(TEST_SSID, 1, mockSearchForSSIDCallbacks); + verify(mockSearchForSSIDCallbacks, timeout(2000)).ssidNotFound(); + } + + @Test + public void callbacks_failure_nullSSID_nullCallback() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = null; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(TEST_SSID, 1, null); + verify(mockSearchForSSIDCallbacks, never()).ssidNotFound(); + } + + @Test + public void callbacks_failure_nullSSIDParam() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = null; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(null, 1, mockSearchForSSIDCallbacks); + verify(mockSearchForSSIDCallbacks, timeout(2000)).searchForSSIDWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_nullSSIDParam_nullCallback() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = null; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(null, 1, null); + verify(mockSearchForSSIDCallbacks, never()).searchForSSIDWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + + @Test + public void callbacks_failure_differentSSID() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = "Another SSID"; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(TEST_SSID, 1, mockSearchForSSIDCallbacks); + verify(mockSearchForSSIDCallbacks, timeout(2000)).ssidNotFound(); + } + + @Test + public void callbacks_failure_differentSSID_nullCallback() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = "Another SSID"; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(TEST_SSID, 1, null); + verify(mockSearchForSSIDCallbacks, never()).ssidNotFound(); + } + + @Test + public void callbacks_failure_nullWifiManager() { + setManagersToNull(); + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(TEST_SSID, 1, mockSearchForSSIDCallbacks); + verify(mockSearchForSSIDCallbacks, timeout(2000)).searchForSSIDWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_failure_nullWifiManager_nullCallback() { + setManagersToNull(); + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(TEST_SSID, 1, null); + verify(mockSearchForSSIDCallbacks, never()).searchForSSIDWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + + @Test + public void callbacks_success() { + setManagers(); + + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = TEST_SSID; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(TEST_SSID, 1, mockSearchForSSIDCallbacks); + verify(mockSearchForSSIDCallbacks, timeout(2000)).ssidFound(TEST_SSID); + } + + @Test + public void callbacks_success_nullCallback() { + setManagers(); + List scanResults = new ArrayList<>(); + ScanResult scanResult = mock(ScanResult.class); + scanResult.SSID = TEST_SSID; + scanResults.add(scanResult); + + when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); + + SearchForSSIDCallbacks mockSearchForSSIDCallbacks = mock(SearchForSSIDCallbacks.class); + mWiseFy.searchForSSID(TEST_SSID, 1, null); + verify(mockSearchForSSIDCallbacks, never()).ssidFound(TEST_SSID); + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/WiseFyTest.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/WiseFyTest.java deleted file mode 100644 index 18b436e1..00000000 --- a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/WiseFyTest.java +++ /dev/null @@ -1,1278 +0,0 @@ -package com.isupatches.wisefy.test; - - -import android.content.Intent; -import android.net.ConnectivityManager; -import android.net.NetworkInfo; -import android.net.wifi.ScanResult; -import android.net.wifi.WifiConfiguration; -import android.net.wifi.WifiInfo; -import android.net.wifi.WifiManager; -import android.os.Build; -import android.support.test.rule.ActivityTestRule; -import com.google.android.gms.iid.InstanceID; -import com.isupatches.wisefy.WiseFy; -import com.isupatches.wisefy.constants.Capabilities; -import com.isupatches.wisefy.constants.NetworkTypes; -import com.isupatches.wisefy.constants.WiseFyCodes; -import com.isupatches.wisefy.test.base.BaseTestClass; -import com.isupatches.wisefy.test.base.TestActivity; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import java.util.ArrayList; -import java.util.List; -import static com.isupatches.wisefy.test.base.TestUtils.*; -import static org.junit.Assert.*; -import static org.mockito.Mockito.*; - - -public class WiseFyTest extends BaseTestClass { - - @Rule - public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(TestActivity.class); - - private ConnectivityManager mMockConnectivityManager; - - private WifiManager mMockWiFiManager; - - private WiseFy mWiseFy; - - public WiseFyTest() { - super(TestActivity.class); - } - - @Before - public void setUp() { - mMockWiFiManager = mock(WifiManager.class); - mMockConnectivityManager = mock(ConnectivityManager.class); - - mActivityTestRule.launchActivity(new Intent()); - - mWiseFy = new WiseFy.withContext(mActivityTestRule.getActivity()).logging(true).getSmarts(); - } - - @Test - public void addOpenNetwork_failure() { - setManagers(); - when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); - int result = mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID); - assertEquals(WiseFy.WIFI_MANAGER_FAILURE, result); - } - - @Test - public void addOpenNetwork_failure_nullSSID() { - int result = mWiseFy.addOpenNetwork(null); - assertEquals(WiseFyCodes.FAILURE, result); - } - - @Test - public void addOpenNetwork_failure_nullWifiManager() { - setManagersToNull(); - int result = mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID); - assertEquals(WiseFyCodes.FAILURE, result); - } - - @Test - public void addOpenNetwork_success() { - setManagers(); - when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); - int result = mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID); - assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); - } - - @Test - public void addOpenNetwork_success_alreadyConfigured() { - setManagers(); - - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = OPEN_NETWORK_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(OPEN_NETWORK_SSID); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - int result = mWiseFy.addOpenNetwork(OPEN_NETWORK_SSID); - assertEquals(WiseFyCodes.NETWORK_ALREADY_CONFIGURED, result); - } - - @Test - public void addWEPNetwork_failure() { - setManagers(); - when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); - int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD); - assertEquals(WiseFy.WIFI_MANAGER_FAILURE, result); - } - - @Test - public void addWEPNetwork_failure_nullPassword() { - int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, null); - assertEquals(WiseFyCodes.FAILURE, result); - } - - @Test - public void addWEPNetwork_failure_nullSSID() { - int result = mWiseFy.addWEPNetwork(null, WEP_NETWORK_PASSWORD); - assertEquals(WiseFyCodes.FAILURE, result); - } - - @Test - public void addWEPNetwork_failure_nullWifiManager() { - setManagersToNull(); - int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD); - assertEquals(WiseFyCodes.FAILURE, result); - } - - @Test - public void addWEPNetwork_success() { - setManagers(); - when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); - int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD); - assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); - } - - @Test - public void addWEPNetwork_success_alreadyConfigured() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = WEP_NETWORK_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(WEP_NETWORK_SSID); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - int result = mWiseFy.addWEPNetwork(WEP_NETWORK_SSID, WEP_NETWORK_PASSWORD); - assertEquals(WiseFyCodes.NETWORK_ALREADY_CONFIGURED, result); - } - - @Test - public void addWPA2Network_failure() { - when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(WiseFy.WIFI_MANAGER_FAILURE); - int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD); - assertEquals(WiseFy.WIFI_MANAGER_FAILURE, result); - } - - @Test - public void addWPA2Network_failure_nullPassword() { - int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, null); - assertEquals(WiseFyCodes.FAILURE, result); - } - - @Test - public void addWPA2Network_failure_nullSSID() { - int result = mWiseFy.addWPA2Network(null, WPA2_NETWORK_PASSWORD); - assertEquals(WiseFyCodes.FAILURE, result); - } - - @Test - public void addWPA2Network_failure_nullWifiManager() { - setManagersToNull(); - int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD); - assertEquals(WiseFyCodes.FAILURE, result); - } - - @Test - public void addWPA2Network_success() { - setManagers(); - when(mMockWiFiManager.addNetwork(any(WifiConfiguration.class))).thenReturn(0); - int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD); - assertNotEquals(WiseFy.WIFI_MANAGER_FAILURE, result); - } - - @Test - public void addWPA2Network_success_alreadyConfigured() { - setManagers(); - - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = WPA2_NETWORK_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(WPA2_NETWORK_SSID); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - int result = mWiseFy.addWPA2Network(WPA2_NETWORK_SSID, WPA2_NETWORK_PASSWORD); - assertEquals(WiseFyCodes.NETWORK_ALREADY_CONFIGURED, result); - } - - @Test - public void builder_loggingFalse() { - WiseFy wiseFy = new WiseFy.withContext(mActivityTestRule.getActivity()).logging(false).getSmarts(); - assertEquals(false, wiseFy.isLoggingEnabled()); - } - - @Test - public void builder_loggingTrue() { - WiseFy wiseFy = new WiseFy.withContext(mActivityTestRule.getActivity()).logging(true).getSmarts(); - assertEquals(true, wiseFy.isLoggingEnabled()); - } - - @Test - public void calculateBars() { - int result = mWiseFy.calculateBars(-35, 5); - assertEquals(4, result); - } - - @Test - public void compareSignalLevel() { - int result = mWiseFy.compareSignalLevel(-35, -70); - assertEquals(35, result); - } - - @Test - public void connectToNetwork_failure_emptyList() { - setManagers(); - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(new ArrayList()); - boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); - assertEquals(false, result); - } - - @Test - public void connectToNetwork_failure_notAvailable() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(false); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); - assertEquals(false, result); - } - - @Test - public void connectToNetwork_failure_notConnected() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); - - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(false); - - boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); - assertEquals(false, result); - } - - @Test - public void connectToNetwork_failure_nullList() { - setManagers(); - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(null); - boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); - assertEquals(false, result); - } - - @Test - public void connectToNetwork_failure_nullWifiManager() { - setManagersToNull(); - boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); - assertEquals(false, result); - } - - @Test - public void connectToNetwork_success() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.connectToNetwork(TEST_SSID, 1); - assertEquals(true, result); - } - - @Test - public void disableWifi_failure() { - setManagers(); - when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(true); - boolean result = mWiseFy.disableWifi(); - verify(mMockWiFiManager).setWifiEnabled(false); - assertEquals(true, result); - } - - @Test - public void disableWifi_failure_nullWifiManager() { - setManagersToNull(); - boolean result = mWiseFy.disableWifi(); - assertEquals(false, result); - } - - @Test - public void disableWifi_success() { - setManagers(); - when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(true); - boolean result = mWiseFy.disableWifi(); - verify(mMockWiFiManager).setWifiEnabled(false); - assertEquals(true, result); - } - - @Test - public void disconnectFromCurrentNetwork_failure() { - setManagers(); - when(mMockWiFiManager.disconnect()).thenReturn(false); - boolean result = mWiseFy.disconnectFromCurrentNetwork(); - verify(mMockWiFiManager).disconnect(); - assertEquals(false, result); - } - - @Test - public void disconnectFromCurrentNetwork_failure_nullWifiManager() { - setManagersToNull(); - boolean result = mWiseFy.disconnectFromCurrentNetwork(); - assertEquals(false, result); - } - - @Test - public void disconnectFromCurrentNetwork_success() { - setManagers(); - when(mMockWiFiManager.disconnect()).thenReturn(true); - boolean result = mWiseFy.disconnectFromCurrentNetwork(); - verify(mMockWiFiManager).disconnect(); - assertEquals(true, result); - } - - @Test - public void enableWiFi_failure() { - setManagers(); - when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(false); - boolean result = mWiseFy.enableWifi(); - verify(mMockWiFiManager).setWifiEnabled(true); - assertEquals(false, result); - } - - @Test - public void enableWiFi_failure_nullWifiManager() { - setManagersToNull(); - boolean result = mWiseFy.enableWifi(); - assertEquals(false, result); - } - - @Test - public void enableWiFi_success() { - setManagers(); - when(mMockWiFiManager.setWifiEnabled(anyBoolean())).thenReturn(true); - boolean result = mWiseFy.enableWifi(); - verify(mMockWiFiManager).setWifiEnabled(true); - assertEquals(true, result); - } - - @Test - public void olderGcm_IllegalAccessError_notThrown() { - InstanceID instanceID = InstanceID.getInstance(mActivityTestRule.getActivity()); - assertNotNull(instanceID); - } - - @Test - public void getCurrentNetwork_failure_nullWifiManager() { - setManagersToNull(); - assertEquals(null, mWiseFy.getCurrentNetwork()); - } - - @Test - public void getCurrentNetwork_success() { - setManagers(); - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getSSID()).thenReturn(TEST_SSID); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - - assertEquals(TEST_SSID, mWiseFy.getCurrentNetwork().getSSID()); - } - - @Test - public void getFrequency_currentNetwork_failure() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setManagers(); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(null); - - assertEquals(WiseFyCodes.FAILURE, mWiseFy.getFrequency()); - } - } - - @Test - public void getFrequency_currentNetwork_success() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setManagers(); - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_24GHZ); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - - assertEquals(TEST_NETWORK_FREQUENCY_24GHZ, mWiseFy.getFrequency()); - } - } - - @Test - public void getFrequency_provideWifiInfo_failure() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - assertEquals(WiseFyCodes.FAILURE, mWiseFy.getFrequency(null)); - } - } - - @Test - public void getFrequency_provideWifiInfo_success() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setManagers(); - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_24GHZ); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - - assertEquals(TEST_NETWORK_FREQUENCY_24GHZ, mWiseFy.getFrequency(mockWifiInfo)); - } - } - - @Test - public void getNearbyAccessPoints_success_filterDuplicates_false() { - setManagers(); - List scanResults = new ArrayList<>(); - ScanResult scanResult1 = mock(ScanResult.class); - scanResult1.SSID = TEST_SSID; - scanResults.add(scanResult1); - ScanResult scanResult2 = mock(ScanResult.class); - scanResult2.SSID = TEST_SSID2; - scanResults.add(scanResult2); - - when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); - - List results = mWiseFy.getNearbyAccessPoints(false); - - assertEquals(scanResults, results); - assertEquals(2, results.size()); - } - - @Test - public void getNearbyAccessPoints_success_filterDuplicates_true_differentSSIDs() { - setManagers(); - List scanResults = new ArrayList<>(); - ScanResult scanResult1 = mock(ScanResult.class); - scanResult1.SSID = TEST_SSID; - scanResults.add(scanResult1); - ScanResult scanResult2 = mock(ScanResult.class); - scanResult2.SSID = TEST_SSID2; - scanResults.add(scanResult2); - - when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); - - List results = mWiseFy.getNearbyAccessPoints(true); - - assertEquals(scanResults, results); - assertEquals(2, results.size()); - } - - @Test - public void getNearbyAccessPoints_success_filterDuplicates_true_sameSSIDs_higherFrequency() { - setManagers(); - List scanResults = new ArrayList<>(); - ScanResult scanResult1 = mock(ScanResult.class); - scanResult1.SSID = TEST_SSID; - scanResult1.level = -35; - scanResults.add(scanResult1); - ScanResult scanResult2 = mock(ScanResult.class); - scanResult2.SSID = TEST_SSID; - scanResult2.level = -70; - scanResults.add(scanResult2); - - List expectedScanResults = new ArrayList<>(); - expectedScanResults.add(scanResult1); - - when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); - - List results = mWiseFy.getNearbyAccessPoints(true); - - assertEquals(expectedScanResults, results); - assertEquals(1, results.size()); - } - - @Test - public void getNearbyAccessPoints_filterDuplicates_true_sameSSIDs_lowerFrequency() { - setManagers(); - List scanResults = new ArrayList<>(); - ScanResult scanResult1 = mock(ScanResult.class); - scanResult1.SSID = TEST_SSID; - scanResult1.level = -70; - scanResults.add(scanResult1); - ScanResult scanResult2 = mock(ScanResult.class); - scanResult2.SSID = TEST_SSID; - scanResult2.level = -35; - scanResults.add(scanResult2); - - List expectedScanResults = new ArrayList<>(); - expectedScanResults.add(scanResult2); - - when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); - - List results = mWiseFy.getNearbyAccessPoints(true); - - assertEquals(expectedScanResults, results); - assertEquals(1, results.size()); - } - - @Test - public void getNearbyAccessPoints_failure_filterDuplicates_false_nullWifiManager() { - setManagersToNull(); - List results = mWiseFy.getNearbyAccessPoints(false); - assertEquals(null, results); - } - - @Test - public void getNearbyAccessPoints_failure_filterDuplicates_true_nullWifiManager() { - setManagersToNull(); - List results = mWiseFy.getNearbyAccessPoints(true); - assertEquals(null, results); - } - - @Test - public void getSavedNetworks_failure_nullWifiManager() { - setManagersToNull(); - assertEquals(null, mWiseFy.getSavedNetworks()); - } - - @Test - public void getSavedNetworks_success() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - - assertEquals(wifiList, mWiseFy.getSavedNetworks()); - } - - @Test - public void isDeviceConnectedToMobileNetwork_failure_notAvailable() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(false); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileNetwork_failure_notConnected() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(false); - - boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileNetwork_failure_nullActiveNetworkInfo() { - setManagers(); - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); - boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileNetwork_failure_nullConfigurationManager() { - setManagers(); - setConnectivityManagerToNull(); - boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileNetwork_failure_nullTypeName() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(null); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - - boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileNetwork_failure_wrongType() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn("WRONG TYPE"); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileNetwork_success() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToMobileNetwork(); - assertEquals(true, result); - } - - @Test - public void isDeviceConnectedToMobileOrWifiNetwork_success_mobile() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); - assertEquals(true, result); - } - - @Test - public void isDeviceConnectedToMobileOrWifiNetwork_success_wifi() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); - assertEquals(true, result); - } - - @Test - public void isDeviceConnectedToMobileOrWifiNetwork_failure_mobile_notAvailable() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(false); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileOrWifiNetwork_failure_wifi_notAvailable() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(false); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileOrWifiNetwork_failure_mobile_notConnected() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.MOBILE); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(false); - - boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileOrWifiNetwork_failure_wifi_notConnected() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(false); - - boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileOrWifiNetwork_failure_nullActiveNetworkInfo() { - setManagers(); - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); - boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToMobileOrWifiNetwork_failure_nullConfigurationManager() { - setManagersToNull(); - boolean result = mWiseFy.isDeviceConnectedToMobileOrWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToSSID_failure_differentSSID() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID2; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID2); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToSSID_failure_notAvailable() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(false); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToSSID_failure_notConnected() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(false); - - boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToSSID_failure_nullActiveNetworkInfo() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); - - boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToSSID_failure_nullConnectionInfo() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(null); - - boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToSSID_failure_nullConnectionInfoSSID() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(null); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToSSID_failure_nullConnectivityManagerManager() { - setManagers(); - setConnectivityManagerToNull(); - - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToSSID_failure_nullWifiManager() { - setManagersToNull(); - boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToSSID_success() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - - WifiInfo mockWiFiInfo = mock(WifiInfo.class); - when(mockWiFiInfo.getSSID()).thenReturn(TEST_SSID); - - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWiFiInfo); - - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToSSID(TEST_SSID); - assertEquals(true, result); - } - - @Test - public void isDeviceConnectedToWifiNetwork_Failure_notAvailable() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(false); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToWifiNetwork_failure_notConnected() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(false); - - boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToWifiNetwork_failure_nullActiveNetworkInfo() { - setManagers(); - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(null); - boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToWifiNetwork_failure_nullConfigurationManager() { - setManagers(); - setConnectivityManagerToNull(); - boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToWifiNetwork_failure_nullTypeName() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(null); - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToWifiNetwork_failure_wrongType() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn("WRONG TYPE"); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); - assertEquals(false, result); - } - - @Test - public void isDeviceConnectedToWifiNetwork_success() { - setManagers(); - NetworkInfo mockNetworkInfo = mock(NetworkInfo.class); - when(mockNetworkInfo.getTypeName()).thenReturn(NetworkTypes.WIFI); - - when(mMockConnectivityManager.getActiveNetworkInfo()).thenReturn(mockNetworkInfo); - when(mockNetworkInfo.isAvailable()).thenReturn(true); - when(mockNetworkInfo.isConnected()).thenReturn(true); - - boolean result = mWiseFy.isDeviceConnectedToWifiNetwork(); - assertEquals(true, result); - } - - @Test - public void isNetwork5gHz_currentNetwork_failure_above5ghz() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setManagers(); - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_ABOVE_5GHZ); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - - assertEquals(false, mWiseFy.isNetwork5gHz()); - } - } - - @Test - public void isNetwork5gHz_currentNetwork_failure_below5ghz() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setManagers(); - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_BELOW_5GHZ); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - - assertEquals(false, mWiseFy.isNetwork5gHz()); - } - } - - @Test - public void isNetwork5gHz_currentNetwork_success() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setManagers(); - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_5GHZ); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - - assertEquals(true, mWiseFy.isNetwork5gHz()); - } - } - - @Test - public void isNetwork5gHz_provideWifiInfo_failure_above5ghz() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setManagers(); - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_ABOVE_5GHZ); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - - assertEquals(false, mWiseFy.isNetwork5gHz(mockWifiInfo)); - } - } - - @Test - public void isNetwork5gHz_provideWifiInfo_failure_below5ghz() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setManagers(); - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_BELOW_5GHZ); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - - assertEquals(false, mWiseFy.isNetwork5gHz(mockWifiInfo)); - } - } - - @Test - public void isNetwork5gHz_provideWifiInfo_success() { - if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setManagers(); - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getFrequency()).thenReturn(TEST_NETWORK_FREQUENCY_5GHZ); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - - assertEquals(true, mWiseFy.isNetwork5gHz(mockWifiInfo)); - } - } - - @Test - public void isNetworkInConfigurationList_failure() { - setManagers(); - assertEquals(false, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); - } - - @Test - public void isNetworkInConfigurationList_failure_noNetworks() { - setManagers(); - List wifiList = new ArrayList<>(); - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - assertEquals(false, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); - } - - @Test - public void isNetworkInConfigurationList_failure_nullWifiManager() { - setManagersToNull(); - assertEquals(false, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); - } - - @Test - public void isNetworkInConfigurationList_success() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - assertEquals(true, mWiseFy.isNetworkInConfigurationList(TEST_SSID)); - } - - @Test - public void isNetworkSecure_success_withWEP() { - ScanResult scanResult = mock(ScanResult.class); - scanResult.capabilities = Capabilities.WEP; - assertEquals(true, mWiseFy.isNetworkSecure(scanResult)); - } - - @Test - public void isNetworkSecure_success_withPSK() { - ScanResult scanResult = mock(ScanResult.class); - scanResult.capabilities = Capabilities.PSK; - assertEquals(true, mWiseFy.isNetworkSecure(scanResult)); - } - - @Test - public void isNetworkSecure_success_withEAP() { - ScanResult scanResult = mock(ScanResult.class); - scanResult.capabilities = Capabilities.EAP; - assertEquals(true, mWiseFy.isNetworkSecure(scanResult)); - } - - @Test - public void isNetworkSecure_failure_emptyCapabilities() { - ScanResult scanResult = mock(ScanResult.class); - scanResult.capabilities = ""; - assertEquals(false, mWiseFy.isNetworkSecure(scanResult)); - } - - @Test - public void isNetworkSecure_failure_nullCapabilities() { - ScanResult scanResult = mock(ScanResult.class); - assertEquals(false, mWiseFy.isNetworkSecure(scanResult)); - } - - @Test - public void isNetworkSecure_failure_nullScanResult() { - assertEquals(false, mWiseFy.isNetworkSecure(null)); - } - - @Test - public void isWifiEnabled_failure() { - setManagers(); - when(mMockWiFiManager.isWifiEnabled()).thenReturn(false); - assertEquals(false, mWiseFy.isWifiEnabled()); - } - - @Test - public void isWifiEnabled_success() { - setManagers(); - when(mMockWiFiManager.isWifiEnabled()).thenReturn(true); - assertEquals(true, mWiseFy.isWifiEnabled()); - } - - @Test - public void isWifiEnabled_failure_nullWifiManager() { - setManagersToNull(); - assertEquals(false, mWiseFy.isWifiEnabled()); - } - - @Test - public void removeNetwork_failure() { - setManagers(); - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(getTestWifiConfiguration()); - when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(false); - - boolean result = mWiseFy.removeNetwork(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void removeNetwork_failure_notInList() { - setManagers(); - List wifiList = new ArrayList<>(); - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(false); - - boolean result = mWiseFy.removeNetwork(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void removeNetwork_failure_nullWifiManager() { - setManagersToNull(); - boolean result = mWiseFy.removeNetwork(TEST_SSID); - assertEquals(false, result); - } - - @Test - public void removeNetwork_success() { - setManagers(); - List wifiList = new ArrayList<>(); - WifiConfiguration mWiFiConfiguration = new WifiConfiguration(); - mWiFiConfiguration.SSID = TEST_SSID; - wifiList.add(mWiFiConfiguration); - when(mMockWiFiManager.getConfiguredNetworks()).thenReturn(wifiList); - when(mMockWiFiManager.removeNetwork(anyInt())).thenReturn(true); - - boolean result = mWiseFy.removeNetwork(TEST_SSID); - assertEquals(true, result); - } - - @Test - public void searchSSID_failure() { - setManagers(); - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getSSID()).thenReturn(TEST_SSID); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - - assertEquals(null, mWiseFy.searchForSSID(TEST_SSID, 1)); - } - - @Test - public void searchSSID_failure_nullWifiManager() { - setManagersToNull(); - assertEquals(null, mWiseFy.searchForSSID(TEST_SSID, 1)); - } - - @Test - public void searchSSID_success() { - setManagers(); - List scanResults = new ArrayList<>(); - ScanResult scanResult = mock(ScanResult.class); - scanResult.SSID = TEST_SSID; - scanResults.add(scanResult); - - WifiInfo mockWifiInfo = mock(WifiInfo.class); - when(mockWifiInfo.getSSID()).thenReturn(TEST_SSID); - when(mMockWiFiManager.getConnectionInfo()).thenReturn(mockWifiInfo); - when(mMockWiFiManager.getScanResults()).thenReturn(scanResults); - - assertEquals(TEST_SSID, mWiseFy.searchForSSID(TEST_SSID, 1)); - } - - /** - * HELPERS - */ - - private void setManagers() { - mWiseFy.mWifiManager = mMockWiFiManager; - mWiseFy.mConnectivityManager = mMockConnectivityManager; - } - - private void setManagersToNull() { - mWiseFy.mWifiManager = null; - mWiseFy.mConnectivityManager = null; - } - - private void setConnectivityManagerToNull() { - mWiseFy.mConnectivityManager = null; - } -} \ No newline at end of file diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/WiseFyTests.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/WiseFyTests.java new file mode 100644 index 00000000..ab19caac --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/WiseFyTests.java @@ -0,0 +1,47 @@ +package com.isupatches.wisefy.test; + + +import com.google.android.gms.iid.InstanceID; +import com.isupatches.wisefy.WiseFy; +import com.isupatches.wisefy.test.base.BaseTestClass; +import com.isupatches.wisefy.test.base.TestActivity; +import org.junit.Test; +import static org.junit.Assert.*; + + +public class WiseFyTests extends BaseTestClass { + + public WiseFyTests() { + super(TestActivity.class); + } + + @Test + public void builder_loggingFalse() { + WiseFy wiseFy = new WiseFy.withContext(mActivityTestRule.getActivity()).logging(false).getSmarts(); + assertEquals(false, wiseFy.isLoggingEnabled()); + } + + @Test + public void builder_loggingTrue() { + WiseFy wiseFy = new WiseFy.withContext(mActivityTestRule.getActivity()).logging(true).getSmarts(); + assertEquals(true, wiseFy.isLoggingEnabled()); + } + + @Test + public void calculateBars() { + int result = mWiseFy.calculateBars(-35, 5); + assertEquals(4, result); + } + + @Test + public void compareSignalLevel() { + int result = mWiseFy.compareSignalLevel(-35, -70); + assertEquals(35, result); + } + + @Test + public void olderGcm_IllegalAccessError_notThrown() { + InstanceID instanceID = InstanceID.getInstance(mActivityTestRule.getActivity()); + assertNotNull(instanceID); + } +} \ No newline at end of file diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/BaseTestClass.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/BaseTestClass.java index 9912c4da..fa530fe8 100644 --- a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/BaseTestClass.java +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/BaseTestClass.java @@ -2,12 +2,60 @@ import android.app.Activity; +import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.wifi.WifiManager; import android.support.test.rule.ActivityTestRule; +import com.isupatches.wisefy.WiseFy; +import org.junit.Before; +import org.junit.Rule; + +import static org.mockito.Mockito.mock; public abstract class BaseTestClass extends ActivityTestRule { + @Rule + public ActivityTestRule mActivityTestRule = new ActivityTestRule<>(TestActivity.class); + + protected ConnectivityManager mMockConnectivityManager; + + protected WifiManager mMockWiFiManager; + + protected WiseFy mWiseFy; + public BaseTestClass(Class activityClass) { super(activityClass); } + + @Before + public void setUp() { + mMockWiFiManager = mock(WifiManager.class); + mMockConnectivityManager = mock(ConnectivityManager.class); + + mMockWiFiManager = mock(WifiManager.class); + mMockConnectivityManager = mock(ConnectivityManager.class); + + mActivityTestRule.launchActivity(new Intent()); + + mWiseFy = new WiseFy.withContext(mActivityTestRule.getActivity()).logging(true).getSmarts(); + } + + /** + * HELPERS + */ + + protected void setManagers() { + mWiseFy.mWifiManager = mMockWiFiManager; + mWiseFy.mConnectivityManager = mMockConnectivityManager; + } + + protected void setManagersToNull() { + mWiseFy.mWifiManager = null; + mWiseFy.mConnectivityManager = null; + } + + protected void setConnectivityManagerToNull() { + mWiseFy.mConnectivityManager = null; + } } \ No newline at end of file diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/TestActivity.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/TestActivity.java index 4299e90f..717249c2 100644 --- a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/TestActivity.java +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/TestActivity.java @@ -3,6 +3,19 @@ import android.app.Activity; +import com.isupatches.wisefy.callbacks.DisableWifiCallbacks; +import com.isupatches.wisefy.callbacks.EnableWifiCallbacks; +import com.isupatches.wisefy.callbacks.GetFrequencyCallbacks; +import com.isupatches.wisefy.callbacks.GetSavedNetworksCallbacks; +import com.isupatches.wisefy.callbacks.RemoveNetworkCallbacks; + public class TestActivity extends Activity { +// implements +// GetFrequencyCallbacks, +// GetSavedNetworksCallbacks, +// AddNetworkCallbacks, +// RemoveNetworkCallbacks, +// EnableWifiCallbacks, +// DisableWifiCallbacks { } diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/TestUtils.java b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/TestUtils.java index fe95df4b..65eab433 100644 --- a/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/TestUtils.java +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/test/base/TestUtils.java @@ -23,6 +23,8 @@ public class TestUtils { public static final String TEST_SSID2 = "Test Network 2"; + public static final String TEST_SSID3 = "Test Network 3"; + public static final int TEST_NETWORK_FREQUENCY_BELOW_5GHZ = WiseFy.MIN_FREQUENCY_5GHZ - 1; public static final int TEST_NETWORK_FREQUENCY_ABOVE_5GHZ = WiseFy.MAX_FREQUENCY_5GHZ + 1; diff --git a/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.java b/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.java index ae54b40d..e04e929b 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.java +++ b/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.java @@ -24,17 +24,40 @@ import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; +import android.os.Handler; +import android.os.Looper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.text.TextUtils; import android.util.Log; +import com.isupatches.wisefy.annotations.CallingThread; +import com.isupatches.wisefy.annotations.Async; +import com.isupatches.wisefy.annotations.Sync; +import com.isupatches.wisefy.annotations.WaitsForTimeout; +import com.isupatches.wisefy.annotations.WiseFyThread; +import com.isupatches.wisefy.callbacks.AddOpenNetworkCallbacks; +import com.isupatches.wisefy.callbacks.AddWEPNetworkCallbacks; +import com.isupatches.wisefy.callbacks.AddWPA2NetworkCallbacks; +import com.isupatches.wisefy.callbacks.ConnectToNetworkCallbacks; +import com.isupatches.wisefy.callbacks.DisableWifiCallbacks; +import com.isupatches.wisefy.callbacks.DisconnectFromCurrentNetworkCallbacks; +import com.isupatches.wisefy.callbacks.EnableWifiCallbacks; +import com.isupatches.wisefy.callbacks.GetCurrentNetworkCallbacks; +import com.isupatches.wisefy.callbacks.GetFrequencyCallbacks; +import com.isupatches.wisefy.callbacks.GetNearbyAccessPointsCallbacks; +import com.isupatches.wisefy.callbacks.GetSavedNetworkCallbacks; +import com.isupatches.wisefy.callbacks.GetSavedNetworksCallbacks; +import com.isupatches.wisefy.callbacks.RemoveNetworkCallbacks; +import com.isupatches.wisefy.callbacks.SearchForSSIDCallbacks; import com.isupatches.wisefy.constants.Capabilities; import com.isupatches.wisefy.constants.NetworkTypes; import com.isupatches.wisefy.constants.WiseFyCodes; +import com.isupatches.wisefy.threads.WiseFyHandlerThread; import com.isupatches.wisefy.util.ManagerUtil; import com.isupatches.wisefy.util.LogUtil; -import com.isupatches.wisefy.util.SSIDUtil; +import com.isupatches.wisefy.util.WifiConfigurationUtil; import java.util.ArrayList; import java.util.List; -import java.util.Locale; /** @@ -52,7 +75,7 @@ public class WiseFy { public static final int MAX_FREQUENCY_5GHZ = 5900; - private SSIDUtil mSSIDUtil = SSIDUtil.getInstance(); + private WiseFyHandlerThread mWiseFyHandlerThread; public ConnectivityManager mConnectivityManager; @@ -60,6 +83,8 @@ public class WiseFy { private boolean mLoggingEnabled; + private Handler mWiseFyHandler; + /** * Private constructor that accepts builder input */ @@ -137,41 +162,31 @@ interface GetSmarts { * * @param ssid - The ssid of the open network you want to add * - * {@link WiseFyCodes#FAILURE} - * {@link WiseFyCodes#NETWORK_ALREADY_CONFIGURED} + * Uses + * {@link #addNetwork(WifiConfiguration)} + * {@link #checkIfNetworkInConfigurationList(String)} + * {@link WifiConfigurationUtil#generateOpenNetworkConfiguration(String)} + * {@link WiseFyCodes * * @return int - The return code from WifiManager for network creation (-1 for failure) */ - public int addOpenNetwork(String ssid) { + @Sync + @CallingThread + public int addOpenNetwork(@Nullable String ssid) { if (TextUtils.isEmpty(ssid)) { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { Log.e(TAG, "Breaking due to empty SSID"); } - return WiseFyCodes.FAILURE; + return WiseFyCodes.MISSING_PARAMETER; } if (mWifiManager != null) { - boolean ssidAlreadyConfigured = checkIfNetworkInConfigurationList(ssid); - - if (!ssidAlreadyConfigured) { + if (!checkIfNetworkInConfigurationList(ssid)) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, String.format("Adding open network with SSID %s", ssid)); } - WifiConfiguration wifiConfig = new WifiConfiguration(); - - wifiConfig.SSID = mSSIDUtil.convertSSIDForConfig(ssid); - wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); - wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); - wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA); - wifiConfig.allowedAuthAlgorithms.clear(); - wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); - wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); - wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); - wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); - wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); - wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); - - return addNetwork(wifiConfig); + WifiConfiguration wifiConfiguration = WifiConfigurationUtil.getInstance().generateOpenNetworkConfiguration(ssid); + return addNetwork(wifiConfiguration); } else { return WiseFyCodes.NETWORK_ALREADY_CONFIGURED; } @@ -179,8 +194,70 @@ public int addOpenNetwork(String ssid) { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { Log.e(TAG, "No WifiManager to add open network"); } + return WiseFyCodes.NULL_MANAGER; } - return WiseFyCodes.FAILURE; + } + + /** + * To add an open network to the user's configured network list + * + * @param ssid - The ssid of the open network you want to add + * @param addOpenNetworkCallbacks - + * + * Uses + * {@link #addNetwork(WifiConfiguration)} + * {@link #checkIfNetworkInConfigurationList(String)} + * {@link WifiConfigurationUtil#generateOpenNetworkConfiguration(String)} + * {@link WiseFyCodes + */ + @Async + @WiseFyThread + public void addOpenNetwork(@Nullable final String ssid, @Nullable final AddOpenNetworkCallbacks addOpenNetworkCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (TextUtils.isEmpty(ssid)) { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "Breaking due to empty SSID"); + } + if (addOpenNetworkCallbacks != null) { + addOpenNetworkCallbacks.addOpenNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + return; + } + if (mWifiManager != null) { + if (!checkIfNetworkInConfigurationList(ssid)) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("Adding open network with SSID %s", ssid)); + } + + WifiConfiguration wifiConfiguration = WifiConfigurationUtil.getInstance().generateOpenNetworkConfiguration(ssid); + if (addOpenNetworkCallbacks != null) { + int result = addNetwork(wifiConfiguration); + if (result != WIFI_MANAGER_FAILURE) { + addOpenNetworkCallbacks.openNetworkAdded(wifiConfiguration); + } else { + addOpenNetworkCallbacks.failureAddingOpenNetwork(result); + } + } + } else { + if (addOpenNetworkCallbacks != null) { + addOpenNetworkCallbacks.addOpenNetworkWiseFyFailure(WiseFyCodes.NETWORK_ALREADY_CONFIGURED); + } + + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to add open network"); + } + if (addOpenNetworkCallbacks != null) { + addOpenNetworkCallbacks.addOpenNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); } /** @@ -189,40 +266,31 @@ public int addOpenNetwork(String ssid) { * @param ssid - The ssid of the WEP network you want to add * @param password - The password for the WEP network being added * - * {@link WiseFyCodes#FAILURE} - * {@link WiseFyCodes#NETWORK_ALREADY_CONFIGURED} + * Uses + * {@link #addNetwork(WifiConfiguration)} + * {@link #checkIfNetworkInConfigurationList(String)} + * {@link WifiConfigurationUtil#generateWEPNetworkConfiguration(String, String)} + * {@link WiseFyCodes * * @return int - The return code from WifiManager for network creation (-1 for failure) */ - public int addWEPNetwork(String ssid, String password) { + @Sync + @CallingThread + public int addWEPNetwork(@Nullable String ssid, @Nullable String password) { if (TextUtils.isEmpty(ssid) || TextUtils.isEmpty(password)) { if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { Log.w(TAG, String.format("Breaking due to missing ssid or password. ssid: %s, password: %s", ssid, password)); } - return WiseFyCodes.FAILURE; + return WiseFyCodes.MISSING_PARAMETER; } if (mWifiManager != null) { - boolean ssidAlreadyConfigured = checkIfNetworkInConfigurationList(ssid); - - if (!ssidAlreadyConfigured) { + if (!checkIfNetworkInConfigurationList(ssid)) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, String.format("Adding WEP network with SSID %s", ssid)); } - WifiConfiguration wifiConfig = new WifiConfiguration(); - wifiConfig.SSID = mSSIDUtil.convertSSIDForConfig(ssid); - wifiConfig.wepKeys[0] = "\"" + password + "\""; - wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); - wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); - wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA); - wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); - wifiConfig.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); - wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); - wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); - wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); - wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); - - return addNetwork(wifiConfig); + WifiConfiguration wifiConfiguration = WifiConfigurationUtil.getInstance().generateWEPNetworkConfiguration(ssid, password); + return addNetwork(wifiConfiguration); } else { return WiseFyCodes.NETWORK_ALREADY_CONFIGURED; } @@ -230,8 +298,70 @@ public int addWEPNetwork(String ssid, String password) { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { Log.e(TAG, "No WifiManager to add WEP network"); } + return WiseFyCodes.NULL_MANAGER; } - return WiseFyCodes.FAILURE; + } + + /** + * To add a WEP network to the user's configured network list + * + * @param ssid - The ssid of the WEP network you want to add + * @param password - The password for the WEP network being added + * @param addWEPNetworkCallbacks - + * + * Uses + * {@link #addNetwork(WifiConfiguration)} + * {@link #checkIfNetworkInConfigurationList(String)} + * {@link WifiConfigurationUtil#generateWEPNetworkConfiguration(String, String)} + * {@link WiseFyCodes + */ + @Async + @WiseFyThread + public void addWEPNetwork(@Nullable final String ssid, @Nullable final String password, @Nullable final AddWEPNetworkCallbacks addWEPNetworkCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (TextUtils.isEmpty(ssid) || TextUtils.isEmpty(password)) { + if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { + Log.w(TAG, String.format("Breaking due to missing ssid or password. ssid: %s, password: %s", ssid, password)); + } + if (addWEPNetworkCallbacks != null) { + addWEPNetworkCallbacks.addWEPNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + return; + } + if (mWifiManager != null) { + if (!checkIfNetworkInConfigurationList(ssid)) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("Adding WEP network with SSID %s", ssid)); + } + + WifiConfiguration wifiConfiguration = WifiConfigurationUtil.getInstance().generateWEPNetworkConfiguration(ssid, password); + if (addWEPNetworkCallbacks != null) { + int result = addNetwork(wifiConfiguration); + if (result != WIFI_MANAGER_FAILURE) { + addWEPNetworkCallbacks.wepNetworkAdded(wifiConfiguration); + } else { + addWEPNetworkCallbacks.failureAddingWEPNetwork(result); + } + } + } else { + if (addWEPNetworkCallbacks != null) { + addWEPNetworkCallbacks.addWEPNetworkWiseFyFailure(WiseFyCodes.NETWORK_ALREADY_CONFIGURED); + } + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to add WEP network"); + } + if (addWEPNetworkCallbacks != null) { + addWEPNetworkCallbacks.addWEPNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); } /** @@ -240,43 +370,31 @@ public int addWEPNetwork(String ssid, String password) { * @param ssid - The ssid of the WPA2 network you want to add * @param password - The password for the WPA2 network being added * - * {@link WiseFyCodes#FAILURE} - * {@link WiseFyCodes#NETWORK_ALREADY_CONFIGURED} + * Uses + * {@link #addNetwork(WifiConfiguration)} + * {@link #checkIfNetworkInConfigurationList(String)} + * {@link WifiConfigurationUtil#generateWPA2NetworkConfiguration(String, String)} + * {@link WiseFyCodes * * @return int - The return code from WifiManager for network creation (-1 for failure) */ - public int addWPA2Network(String ssid, String password) { + @Sync + @CallingThread + public int addWPA2Network(@Nullable String ssid, @Nullable String password) { if (TextUtils.isEmpty(ssid) || TextUtils.isEmpty(password)) { if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { Log.w(TAG, String.format("Breaking due to missing ssid or password. ssid: %s, password: %s", ssid, password)); } - return WiseFyCodes.FAILURE; + return WiseFyCodes.MISSING_PARAMETER; } if (mWifiManager != null) { - boolean ssidAlreadyConfigured = checkIfNetworkInConfigurationList(ssid); - - if (!ssidAlreadyConfigured) { + if (!checkIfNetworkInConfigurationList(ssid)) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, String.format("Adding WPA2 network with SSID %s", ssid)); } - WifiConfiguration wifiConfig = new WifiConfiguration(); - wifiConfig.SSID = mSSIDUtil.convertSSIDForConfig(ssid); - wifiConfig.preSharedKey = "\"" + password + "\""; - wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); - wifiConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); - wifiConfig.status = WifiConfiguration.Status.ENABLED; - wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); - wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); - wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); - wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); - wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.RSN); - wifiConfig.allowedProtocols.set(WifiConfiguration.Protocol.WPA); - - wifiConfig.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); - wifiConfig.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); - - return addNetwork(wifiConfig); + WifiConfiguration wifiConfiguration = WifiConfigurationUtil.getInstance().generateWPA2NetworkConfiguration(ssid, password); + return addNetwork(wifiConfiguration); } else { return WiseFyCodes.NETWORK_ALREADY_CONFIGURED; } @@ -284,8 +402,70 @@ public int addWPA2Network(String ssid, String password) { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { Log.e(TAG, "No WifiManager to add WPA2 network"); } + return WiseFyCodes.NULL_MANAGER; } - return WiseFyCodes.FAILURE; + } + + /** + * To add a WPA2 network to the user's configured network list + * + * @param ssid - The ssid of the WPA2 network you want to add + * @param password - The password for the WPA2 network being added + * @param addWPA2NetworkCallbacks - + * + * Uses + * {@link #addNetwork(WifiConfiguration)} + * {@link #checkIfNetworkInConfigurationList(String)} + * {@link WifiConfigurationUtil#generateWPA2NetworkConfiguration(String, String)} + * {@link WiseFyCodes + */ + @Async + @WiseFyThread + public void addWPA2Network(@Nullable final String ssid, @Nullable final String password, @Nullable final AddWPA2NetworkCallbacks addWPA2NetworkCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (TextUtils.isEmpty(ssid) || TextUtils.isEmpty(password)) { + if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { + Log.w(TAG, String.format("Breaking due to missing ssid or password. ssid: %s, password: %s", ssid, password)); + } + if (addWPA2NetworkCallbacks != null) { + addWPA2NetworkCallbacks.addWPA2NetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + return; + } + if (mWifiManager != null) { + if (!checkIfNetworkInConfigurationList(ssid)) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("Adding WPA2 network with SSID %s", ssid)); + } + + WifiConfiguration wifiConfiguration = WifiConfigurationUtil.getInstance().generateWPA2NetworkConfiguration(ssid, password); + if (addWPA2NetworkCallbacks != null) { + int result = addNetwork(wifiConfiguration); + if (result != WIFI_MANAGER_FAILURE) { + addWPA2NetworkCallbacks.wpa2NetworkAdded(wifiConfiguration); + } else { + addWPA2NetworkCallbacks.failureAddingWPA2Network(result); + } + } + } else { + if (addWPA2NetworkCallbacks != null) { + addWPA2NetworkCallbacks.addWPA2NetworkWiseFyFailure(WiseFyCodes.NETWORK_ALREADY_CONFIGURED); + } + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to add WPA2 network"); + } + if (addWPA2NetworkCallbacks != null) { + addWPA2NetworkCallbacks.addWPA2NetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); } /** @@ -296,6 +476,8 @@ public int addWPA2Network(String ssid, String password) { * * @return int - The number of bars for the given RSSI value */ + @Sync + @CallingThread public int calculateBars(int rssiLevel, int targetNumberOfBars) { return WifiManager.calculateSignalLevel(rssiLevel, targetNumberOfBars); } @@ -309,6 +491,8 @@ public int calculateBars(int rssiLevel, int targetNumberOfBars) { * @return int - Returns negative value if the first signal is weaker than the second signal, 0 if the two * signals have the same strength, and a positive value if the first signal is stronger than the second signal. */ + @Sync + @CallingThread public int compareSignalLevel(int rssi1, int rssi2) { return WifiManager.compareSignalLevel(rssi1, rssi2); } @@ -321,12 +505,21 @@ public int compareSignalLevel(int rssi1, int rssi2) { * @param ssidToConnectTo - The ssid to connect/reconnect to * @param timeoutInMillis - The number of milliseconds to continue waiting for the device to connect to the given SSID * + * Uses + * {@link #waitToConnectToSSID(String, int)} + * * @return boolean - If the network was successfully reconnected */ - public boolean connectToNetwork(String ssidToConnectTo, int timeoutInMillis) { + @Sync + @CallingThread + @WaitsForTimeout + public boolean connectToNetwork(@Nullable String ssidToConnectTo, int timeoutInMillis) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, String.format("Connecting to network: %s", ssidToConnectTo)); } + if (TextUtils.isEmpty(ssidToConnectTo)) { + return false; + } if (mWifiManager != null) { List list = mWifiManager.getConfiguredNetworks(); if (list != null) { @@ -361,11 +554,92 @@ public boolean connectToNetwork(String ssidToConnectTo, int timeoutInMillis) { return false; } + /** + * Used to connect to a network + * + * Gets a list of saved networks, connects/reconnects to the given ssid, and then calls waitToConnectToSSID to verify connectivity + * + * @param ssidToConnectTo - The ssid to connect/reconnect to + * @param timeoutInMillis - The number of milliseconds to continue waiting for the device to connect to the given SSID + * + * Uses + * {@link #waitToConnectToSSID(String, int)} + * {@link WiseFyCodes} + */ + @Async + @WiseFyThread + @WaitsForTimeout + public void connectToNetwork(@Nullable final String ssidToConnectTo, final int timeoutInMillis, @Nullable final ConnectToNetworkCallbacks connectToNetworkCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("Connecting to network: %s", ssidToConnectTo)); + } + if (TextUtils.isEmpty(ssidToConnectTo)) { + if (connectToNetworkCallbacks != null) { + connectToNetworkCallbacks.connectToNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + return; + } + if (mWifiManager != null) { + List list = mWifiManager.getConfiguredNetworks(); + if (list != null) { + for (int i = 0; i < list.size(); i++) { + WifiConfiguration wifiConfiguration = list.get(i); + if (wifiConfiguration != null && wifiConfiguration.SSID != null) { + String ssidInList = wifiConfiguration.SSID.replaceAll("\"", ""); + + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("Configured WiFi Network { index: %d, ssidInList: %s }", i, ssidInList)); + } + if (ssidInList.equals(ssidToConnectTo)) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("ssidToReconnectTo: %s matches ssidInList: %s", ssidToConnectTo, ssidInList)); + } + mWifiManager.disconnect(); + mWifiManager.enableNetwork(wifiConfiguration.networkId, true); + mWifiManager.reconnect(); + boolean connected = waitToConnectToSSID(ssidToConnectTo, timeoutInMillis); + if (connectToNetworkCallbacks != null) { + if (connected) { + connectToNetworkCallbacks.connectedToNetwork(); + } else { + connectToNetworkCallbacks.failureConnectingToNetwork(); + } + } + return; + } + } + } + } + if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { + Log.w(TAG, String.format("ssidToReconnectTo: %s was not found in list to connect to", ssidToConnectTo)); + } + if (connectToNetworkCallbacks != null) { + connectToNetworkCallbacks.networkNotFoundToConnectTo(); + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, String.format("No WifiManager to connect to network. SSID: %s", ssidToConnectTo)); + } + if (connectToNetworkCallbacks != null) { + connectToNetworkCallbacks.connectToNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); + } + /** * To disable Wifi on a user's device * * @return boolean - If the command succeeded in disabling wifi */ + @Sync + @CallingThread public boolean disableWifi() { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, "Disabling WiFi"); @@ -380,11 +654,51 @@ public boolean disableWifi() { return false; } + /** + * To disable Wifi on a user's device + * + * Uses + * {@link WiseFyCodes} + */ + @Async + @WiseFyThread + public void disableWifi(@Nullable final DisableWifiCallbacks disableWifiCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "Disabling WiFi"); + } + if (mWifiManager != null) { + boolean result = mWifiManager.setWifiEnabled(false); + if (disableWifiCallbacks != null) { + if (result) { + disableWifiCallbacks.wifiDisabled(); + } else { + disableWifiCallbacks.failureDisablingWifi(); + } + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to disable Wifi"); + } + if (disableWifiCallbacks != null) { + disableWifiCallbacks.disableWifiWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); + } + /** * To disconnect the user from their current network * * @return boolean - If the command succeeded in disconnecting the device from the current network */ + @Sync + @CallingThread public boolean disconnectFromCurrentNetwork() { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, "Disconnecting from current network"); @@ -399,11 +713,51 @@ public boolean disconnectFromCurrentNetwork() { return false; } + /** + * To disconnect the user from their current network + * + * Uses + * {@link WiseFyCodes} + */ + @Async + @WiseFyThread + public void disconnectFromCurrentNetwork(@Nullable final DisconnectFromCurrentNetworkCallbacks disconnectFromCurrentNetworkCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "Disconnecting from current network"); + } + if (mWifiManager != null) { + boolean result = mWifiManager.disconnect(); + if (disconnectFromCurrentNetworkCallbacks != null) { + if (result) { + disconnectFromCurrentNetworkCallbacks.disconnectedFromCurrentNetwork(); + } else { + disconnectFromCurrentNetworkCallbacks.failureDisconnectingFromCurrentNetwork(); + } + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to disconnect from current network"); + } + if (disconnectFromCurrentNetworkCallbacks != null) { + disconnectFromCurrentNetworkCallbacks.disconnectFromCurrentNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); + } + /** * To enable Wifi on a user's device * * @return boolean - If the command succeeded in enabling wifi */ + @Sync + @CallingThread public boolean enableWifi() { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, "Enabling WiFi"); @@ -418,11 +772,51 @@ public boolean enableWifi() { return false; } + /** + * To enable Wifi on a user's device + * + * Uses + * {@link WiseFyCodes} + */ + @Async + @WiseFyThread + public void enableWifi(@Nullable final EnableWifiCallbacks enableWifiCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "Enabling WiFi"); + } + if (mWifiManager != null) { + boolean result = mWifiManager.setWifiEnabled(true); + if (enableWifiCallbacks != null) { + if (result) { + enableWifiCallbacks.wifiEnabled(); + } else { + enableWifiCallbacks.failureEnablingWifi(); + } + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to enable wifi"); + } + if (enableWifiCallbacks != null) { + enableWifiCallbacks.enableWifiWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); + } + /** * To retrieve the user's current network * * @return WifiInfo|null - The user's current network information */ + @Sync + @CallingThread public WifiInfo getCurrentNetwork() { if (mWifiManager != null) { return mWifiManager.getConnectionInfo(); @@ -434,21 +828,89 @@ public WifiInfo getCurrentNetwork() { return null; } + /** + * To retrieve the user's current network + * + * Uses + * {@link WiseFyCodes} + */ + @Async + @WiseFyThread + public void getCurrentNetwork(@Nullable final GetCurrentNetworkCallbacks getCurrentNetworkCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (mWifiManager != null) { + if (getCurrentNetworkCallbacks != null) { + getCurrentNetworkCallbacks.retrievedCurrentNetwork(mWifiManager.getConnectionInfo()); + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to get current network"); + } + if (getCurrentNetworkCallbacks != null) { + getCurrentNetworkCallbacks.getCurrentNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); + } + /** * To retrieve the frequency of the device's current network * - * Used by isNetwork5gHz - * {@link #isNetwork5gHz()} + * Used by + * {@link #isNetwork5gHz()} + * + * Uses + * {@link #getCurrentNetwork()} * - * @return int - The frequency of the devices current network + * @return int|NULL - The frequency of the devices current network or null if no network */ + @Sync + @CallingThread @TargetApi(21) - public int getFrequency() { + public Integer getFrequency() { WifiInfo currentNetwork = getCurrentNetwork(); if (currentNetwork != null) { return currentNetwork.getFrequency(); } - return WiseFyCodes.FAILURE; + return null; + } + + /** + * To retrieve the frequency of the device's current network + * + * Used by + * {@link #isNetwork5gHz()} + * + * Uses + * {@link #getCurrentNetwork(GetCurrentNetworkCallbacks)} ()} + * {@link WiseFyCodes} + */ + @Async + @WiseFyThread + @TargetApi(21) + public void getFrequency(@Nullable final GetFrequencyCallbacks getFrequencyCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + WifiInfo currentNetwork = getCurrentNetwork(); + if (currentNetwork != null) { + if (getFrequencyCallbacks != null) { + getFrequencyCallbacks.retrievedFrequency(currentNetwork.getFrequency()); + } + } else { + if (getFrequencyCallbacks != null) { + getFrequencyCallbacks.failureGettingFrequency(); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); } /** @@ -456,17 +918,51 @@ public int getFrequency() { * * @param network - The network to return the frequency of * - * Used by isNetwork5gHz - * {@link #isNetwork5gHz(WifiInfo)} + * Used by + * {@link #isNetwork5gHz(WifiInfo)} * - * @return int - The frequency of the network + * @return int|NULL - The frequency of the devices current network or null if no network */ + @Sync + @CallingThread @TargetApi(21) - public int getFrequency(WifiInfo network) { + public Integer getFrequency(@Nullable WifiInfo network) { if (network != null) { return network.getFrequency(); } - return WiseFyCodes.FAILURE; + return null; + } + + /** + * To retrieve the frequency of a network + * + * @param network - The network to return the frequency of + * + * Used by isNetwork5gHz + * {@link #isNetwork5gHz(WifiInfo)} + * + * Uses + * {@link WiseFyCodes} + */ + @Async + @WiseFyThread + @TargetApi(21) + public void getFrequency(@Nullable final WifiInfo network, @Nullable final GetFrequencyCallbacks getFrequencyCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (network != null) { + if (getFrequencyCallbacks != null) { + getFrequencyCallbacks.retrievedFrequency(network.getFrequency()); + } + } + if (getFrequencyCallbacks != null) { + getFrequencyCallbacks.getFrequencyWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); } /** @@ -478,6 +974,8 @@ public int getFrequency(WifiInfo network) { * * @return List of ScanResults|null - List of nearby access points */ + @Sync + @CallingThread public List getNearbyAccessPoints(boolean filterDuplicates) { if (mWifiManager != null) { mWifiManager.startScan(); @@ -532,11 +1030,161 @@ public List getNearbyAccessPoints(boolean filterDuplicates) { return null; } + /** + * To retrieve a list of nearby access points + * + * *NOTE* Setting filterDuplicates to true will not return SSIDs with a weaker signal strength (will always take the highest) + * + * @param filterDuplicates - If you want to exclude SSIDs with that same name that have a weaker signal strength + */ + @Async + @WiseFyThread + public void getNearbyAccessPoints(final boolean filterDuplicates, @Nullable final GetNearbyAccessPointsCallbacks getNearbyAccessPointsCallbacks) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (mWifiManager != null) { + mWifiManager.startScan(); + if (!filterDuplicates) { + if (getNearbyAccessPointsCallbacks != null) { + getNearbyAccessPointsCallbacks.retrievedNearbyAccessPoints(mWifiManager.getScanResults()); + } + } else { + List scanResults = mWifiManager.getScanResults(); + List scanResultsToReturn = new ArrayList<>(); + + for (ScanResult newScanResult : scanResults) { + boolean found = false; + for (int i = 0; i < scanResultsToReturn.size(); i++) { + ScanResult scanResult = scanResultsToReturn.get(i); + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("SSID 1: %s, SSID 2: %s", newScanResult.SSID, scanResult.SSID)); + } + if (newScanResult.SSID.equalsIgnoreCase(scanResult.SSID)) { + found = true; + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "SSID did match"); + Log.d(TAG, String.format("Current level: %d", scanResult.level)); + Log.d(TAG, String.format("New level: %d", newScanResult.level)); + Log.d(TAG, String.format("comparison result: %d", WifiManager.compareSignalLevel(newScanResult.level, scanResult.level))); + } + if (WifiManager.compareSignalLevel(newScanResult.level, scanResult.level) > 0) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "New result has a higher signal strength, swapping"); + } + scanResultsToReturn.set(i, newScanResult); + } + } else { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "SSID did not match"); + } + } + } + + if (!found) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "Found new wifi network"); + } + scanResultsToReturn.add(newScanResult); + } + } + if (getNearbyAccessPointsCallbacks != null) { + getNearbyAccessPointsCallbacks.retrievedNearbyAccessPoints(scanResultsToReturn); + } + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to get nearby access points"); + } + if (getNearbyAccessPointsCallbacks != null) { + getNearbyAccessPointsCallbacks.getNearbyAccessPointsWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); + } + + /** + * To search for and return a saved WiFiConfiguration given an SSID + * + * @param ssid - The ssid to use while searching for saved configuration + * + * Uses + * {@link #findNetworkInConfigurationList(String)} + * + * @return WifiConfiguration|null - Saved network that matches the ssid + */ + @Sync + @CallingThread + public WifiConfiguration getSavedNetwork(@Nullable String ssid) { + if (mWifiManager != null) { + if (!TextUtils.isEmpty(ssid)) { + return findNetworkInConfigurationList(ssid); + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to get saved networks"); + } + } + return null; + } + + /** + * To search for and return a saved WiFiConfiguration given an SSID + * + * @param ssid - The ssid to use while searching for saved configuration + * @param getSavedNetworkCallback - The listener to return results to + * + * Uses + * {@link #execute(Runnable)} + * {@link #findNetworkInConfigurationList(String)} + * {@link GetSavedNetworkCallbacks} + * {@link WiseFyHandlerThread} + */ + @Async + @WiseFyThread + public void getSavedNetwork(@Nullable final String ssid, @Nullable final GetSavedNetworkCallbacks getSavedNetworkCallback) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (TextUtils.isEmpty(ssid)) { + if (getSavedNetworkCallback != null) { + getSavedNetworkCallback.getSavedNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + return; + } + if (mWifiManager != null) { + WifiConfiguration savedNetwork = findNetworkInConfigurationList(ssid); + if (getSavedNetworkCallback != null) { + if (savedNetwork != null) { + getSavedNetworkCallback.retrievedSavedNetwork(savedNetwork); + } else { + getSavedNetworkCallback.networkNotFound(); + } + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to get saved networks"); + } + if (getSavedNetworkCallback != null) { + getSavedNetworkCallback.getSavedNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); + } + /** * To retrieve a list of saved networks on a user's device * * @return List of WifiConfiguration|null - List of saved networks on a users device */ + @Sync + @CallingThread public List getSavedNetworks() { if (mWifiManager != null) { return mWifiManager.getConfiguredNetworks(); @@ -548,11 +1196,50 @@ public List getSavedNetworks() { return null; } + /** + * To retrieve a list of saved networks on a user's device + * + * @param getSavedNetworksCallback - The listener to return results to + * + * Uses + * {@link #execute(Runnable)} + * {@link GetSavedNetworksCallbacks} + * {@link WiseFyHandlerThread} + */ + @Async + @WiseFyThread + public void getSavedNetworks(@Nullable final GetSavedNetworksCallbacks getSavedNetworksCallback) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (mWifiManager != null) { + if (getSavedNetworksCallback != null) { + getSavedNetworksCallback.retrievedSavedNetworks(mWifiManager.getConfiguredNetworks()); + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, "No WifiManager to get saved networks"); + } + if (getSavedNetworksCallback != null) { + getSavedNetworksCallback.getSavedNetworksWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); + } + /** * To check if the device is connected to a mobile network * + * Uses + * {@link NetworkTypes} + * * @return bool - If the device is currently connected to a mobile network */ + @Sync + @CallingThread public boolean isDeviceConnectedToMobileNetwork() { if (mConnectivityManager != null) { NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo(); @@ -574,6 +1261,8 @@ public boolean isDeviceConnectedToMobileNetwork() { * * @return bool - If the device is currently connected to a mobile or wifi network */ + @Sync + @CallingThread public boolean isDeviceConnectedToMobileOrWifiNetwork() { if (mConnectivityManager != null) { NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo(); @@ -597,7 +1286,12 @@ public boolean isDeviceConnectedToMobileOrWifiNetwork() { * * @return bool - If the device is currently attached to the given SSID */ - public boolean isDeviceConnectedToSSID(String ssid) { + @Sync + @CallingThread + public boolean isDeviceConnectedToSSID(@Nullable String ssid) { + if (TextUtils.isEmpty(ssid)) { + return false; + } if (mWifiManager != null) { WifiInfo connectionInfo = mWifiManager.getConnectionInfo(); if (connectionInfo != null && connectionInfo.getSSID() != null) { @@ -632,8 +1326,13 @@ public boolean isDeviceConnectedToSSID(String ssid) { /** * To check if the device is connected to a wifi network * + * Uses + * {@link NetworkTypes} + * * @return bool - If the device is currently connected to a wifi network */ + @Sync + @CallingThread public boolean isDeviceConnectedToWifiNetwork() { if (mConnectivityManager != null) { NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo(); @@ -655,6 +1354,8 @@ public boolean isDeviceConnectedToWifiNetwork() { * * @return boolean - If logging is enabled for the WiseFy instance */ + @Sync + @CallingThread public boolean isLoggingEnabled() { return mLoggingEnabled; } @@ -662,10 +1363,13 @@ public boolean isLoggingEnabled() { /** * To check if the device's current network is 5gHz * - * {@link #getFrequency()} + * Uses + * {@link #getFrequency()} * * @return boolean - If the network is 5gHz */ + @Sync + @CallingThread public boolean isNetwork5gHz() { int frequency = getFrequency(); return frequency > MIN_FREQUENCY_5GHZ && frequency < MAX_FREQUENCY_5GHZ; @@ -676,11 +1380,14 @@ public boolean isNetwork5gHz() { * * @param network - The network to check if it's 5gHz * - * {@link #getFrequency(WifiInfo)} + * Uses + * {@link #getFrequency(WifiInfo)} * * @return boolean - If the network is 5gHz */ - public boolean isNetwork5gHz(WifiInfo network) { + @Sync + @CallingThread + public boolean isNetwork5gHz(@NonNull WifiInfo network) { int frequency = getFrequency(network); return frequency > MIN_FREQUENCY_5GHZ && frequency < MAX_FREQUENCY_5GHZ; } @@ -690,9 +1397,14 @@ public boolean isNetwork5gHz(WifiInfo network) { * * @param ssid - The SSID to check and see if it's in the list of configured networks * + * Uses + * {@link #checkIfNetworkInConfigurationList(String)} + * * @return boolean - If the SSID is in the list of configured networks */ - public boolean isNetworkInConfigurationList(String ssid) { + @Sync + @CallingThread + public boolean isNetworkInConfigurationList(@NonNull String ssid) { if (mWifiManager != null) { return checkIfNetworkInConfigurationList(ssid); } else { @@ -708,9 +1420,14 @@ public boolean isNetworkInConfigurationList(String ssid) { * * @param scanResult - The network to see if it is secure * + * Uses + * {@link Capabilities} + * * @return boolean - Whether the network is secure */ - public boolean isNetworkSecure(ScanResult scanResult) { + @Sync + @CallingThread + public boolean isNetworkSecure(@Nullable ScanResult scanResult) { boolean isSecure = false; if (scanResult != null && scanResult.capabilities != null) { if (scanResult.capabilities.contains(Capabilities.WEP) || scanResult.capabilities.contains(Capabilities.PSK) || scanResult.capabilities.contains(Capabilities.EAP)) { @@ -725,6 +1442,8 @@ public boolean isNetworkSecure(ScanResult scanResult) { * * @return boolean - if Wifi is enabled on device */ + @Sync + @CallingThread public boolean isWifiEnabled() { if (mWifiManager != null) { return mWifiManager.isWifiEnabled(); @@ -741,43 +1460,40 @@ public boolean isWifiEnabled() { * * @param ssidToRemove - The ssid of the network you want to remove from the configured network list * + * Uses + * {@link #findNetworkInConfigurationList(String)} + * * @return boolean - If the command succeeded in removing the network */ - public boolean removeNetwork(String ssidToRemove) { + @Sync + @CallingThread + public boolean removeNetwork(@Nullable String ssidToRemove) { + if (TextUtils.isEmpty(ssidToRemove)) { + return false; + } if (mWifiManager != null) { - List list = mWifiManager.getConfiguredNetworks(); - if (list != null) { - for (int i = 0; i < list.size(); i++) { - WifiConfiguration wifiConfiguration = list.get(i); - if (wifiConfiguration != null && wifiConfiguration.SSID != null) { - String ssidInList = wifiConfiguration.SSID.replaceAll("\"", ""); - if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, String.format("Configured WiFi Network { index: %d, ssidInList: %s }", i, ssidInList)); - } - if (ssidInList.equals(ssidToRemove)) { - if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, String.format("Removing network: %s", ssidToRemove)); - } - mWifiManager.disconnect(); - boolean result = mWifiManager.removeNetwork(wifiConfiguration.networkId); - if (result) { - if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Successfully removed network"); - } - mWifiManager.saveConfiguration(); - } else { - if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { - Log.d(TAG, "Failed to remove network"); - } - } - mWifiManager.reconnect(); - return result; - } + WifiConfiguration wifiConfiguration = findNetworkInConfigurationList(ssidToRemove); + if (wifiConfiguration != null) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("Removing network: %s", ssidToRemove)); + } + mWifiManager.disconnect(); + boolean result = mWifiManager.removeNetwork(wifiConfiguration.networkId); + if (result) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "Successfully removed network"); + } + } else { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "Failed to remove network"); } } - } - if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { - Log.w(TAG, String.format("SSID to remove: %s was not found in list to remove network", ssidToRemove)); + mWifiManager.reconnect(); + return result; + } else { + if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { + Log.w(TAG, String.format("SSID to remove: %s was not found in list to remove network", ssidToRemove)); + } } } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { @@ -788,14 +1504,92 @@ public boolean removeNetwork(String ssidToRemove) { } /** - * To search local networks and return the first one that contains a given ssid (non-case sensitive) + * To remove a configured network + * + * @param ssidToRemove - The ssid of the network you want to remove from the configured network list + * @param removeNetworkCallback - The listener to return results to + * + * Uses + * {@link #execute(Runnable)} + * {@link #findNetworkInConfigurationList(String)} + * {@link RemoveNetworkCallbacks} + * {@link WiseFyHandlerThread} + */ + @Async + @WiseFyThread + public void removeNetwork(@Nullable final String ssidToRemove, @Nullable final RemoveNetworkCallbacks removeNetworkCallback) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (TextUtils.isEmpty(ssidToRemove)) { + if (removeNetworkCallback != null) { + removeNetworkCallback.removeNetworkWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + return; + } + if (mWifiManager != null) { + WifiConfiguration wifiConfiguration = findNetworkInConfigurationList(ssidToRemove); + if (wifiConfiguration != null) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("Removing network: %s", ssidToRemove)); + } + mWifiManager.disconnect(); + boolean result = mWifiManager.removeNetwork(wifiConfiguration.networkId); + if (result) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "Successfully removed network"); + } + if (removeNetworkCallback != null) { + removeNetworkCallback.networkRemoved(); + } + } else { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "Failed to remove network"); + } + if (removeNetworkCallback != null) { + removeNetworkCallback.failureRemovingNetwork(); + } + } + mWifiManager.reconnect(); + } else { + if (LogUtil.isLoggable(TAG, Log.WARN, mLoggingEnabled)) { + Log.w(TAG, String.format("SSID to remove: %s was not found in list to remove network", ssidToRemove)); + } + if (removeNetworkCallback != null) { + removeNetworkCallback.networkNotFoundToRemove(); + } + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, String.format("No WifiManager to remove network. SSID: %s", ssidToRemove)); + } + if (removeNetworkCallback != null) { + removeNetworkCallback.removeNetworkWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); + } + + /** + * To search local networks and return the first one that contains a given ssid * - * @param ssidToSearchFor - The ssid to search for + * *NOTE* Case insensitive + * + * @param regexForSSID - The regex to be used to search for the ssid * @param timeoutInMillis - The number of milliseconds to keep searching for the SSID * * @return String|null - The first SSID that contains the search ssid (if any, else null) */ - public String searchForSSID(String ssidToSearchFor, int timeoutInMillis) { + @Sync + @CallingThread + @WaitsForTimeout + public String searchForSSID(@Nullable String regexForSSID, int timeoutInMillis) { + if (TextUtils.isEmpty(regexForSSID)) { + return null; + } if (mWifiManager != null) { int scanPass = 1; long currentTime; @@ -813,7 +1607,7 @@ public String searchForSSID(String ssidToSearchFor, int timeoutInMillis) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, String.format("scanResult.SSID: %s", scanResult.SSID)); } - if (scanResult.SSID != null && (scanResult.SSID.toUpperCase(Locale.getDefault()).contains(ssidToSearchFor.toUpperCase(Locale.getDefault())))) { + if (scanResult.SSID != null && (scanResult.SSID).matches(regexForSSID)) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, String.format("Found match, SSID: %s", scanResult.SSID)); } @@ -834,34 +1628,120 @@ public String searchForSSID(String ssidToSearchFor, int timeoutInMillis) { } while (currentTime < endTime); } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { - Log.e(TAG, String.format("No WifiManager to search for network. SSID: %s", ssidToSearchFor)); + Log.e(TAG, String.format("No WifiManager to search for network. SSID: %s", regexForSSID)); } } return null; } + /** + * To search local networks and return the first one that contains a given ssid + * + * *NOTE* Case insensitive + * + * @param regexForSSID - The regex to be used to search for the ssid + * @param timeoutInMillis - The number of milliseconds to keep searching for the SSID + * @param searchForSSIDCallback - The listener to return results to + * + * Uses + * {@link #execute(Runnable)} + * {@link SearchForSSIDCallbacks} + * {@link WiseFyHandlerThread} + */ + @Async + @WiseFyThread + @WaitsForTimeout + public void searchForSSID(@Nullable final String regexForSSID, final int timeoutInMillis, @Nullable final SearchForSSIDCallbacks searchForSSIDCallback) { + Runnable runnable = new Runnable() { + @Override + public void run() { + if (TextUtils.isEmpty(regexForSSID)) { + if (searchForSSIDCallback != null) { + searchForSSIDCallback.searchForSSIDWiseFyFailure(WiseFyCodes.MISSING_PARAMETER); + } + return; + } + if (mWifiManager != null) { + int scanPass = 1; + long currentTime; + long endTime = System.currentTimeMillis() + timeoutInMillis; + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("End time (searchForSSID): %d", endTime)); + } + do { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("Scanning SSIDs, pass %d", scanPass)); + } + mWifiManager.startScan(); + List networks = mWifiManager.getScanResults(); + for (ScanResult scanResult : networks) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("scanResult.SSID: %s", scanResult.SSID)); + } + if (scanResult.SSID != null && (scanResult.SSID).matches(regexForSSID)) { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("Found match, SSID: %s", scanResult.SSID)); + } + if (searchForSSIDCallback != null) { + searchForSSIDCallback.ssidFound(scanResult.SSID); + } + return; + } + } + + try { + Thread.sleep(1000); + } catch (InterruptedException ie) { + // Do nothing + } + currentTime = System.currentTimeMillis(); + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, String.format("Current time (searchForSSID): %d", endTime)); + } + scanPass++; + } while (currentTime < endTime); + if (searchForSSIDCallback != null) { + searchForSSIDCallback.ssidNotFound(); + } + } else { + if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { + Log.e(TAG, String.format("No WifiManager to search for network. SSID: %s", regexForSSID)); + } + if (searchForSSIDCallback != null) { + searchForSSIDCallback.searchForSSIDWiseFyFailure(WiseFyCodes.NULL_MANAGER); + } + } + mWiseFyHandlerThread.quitSafely(); + } + }; + execute(runnable); + } + /* * HELPERS */ /** - * Used internally by addOpenNetwork, addWEPNetwork, and addWPA2Network to add and save a new wifi configuration + * Used internally to add and save a new wifi configuration * - * @param wifiConfig - The network to configuration to add + * @param wifiConfiguration - The network to configuration to add * - * {@link #addOpenNetwork(String)} - * {@link #addWEPNetwork(String, String)} - * {@link #addWPA2Network(String, String)} + * Used by + * {@link #addOpenNetwork(String)} + * {@link #addOpenNetwork(String, AddOpenNetworkCallbacks)} + * {@link #addWEPNetwork(String, String)} + * {@link #addWEPNetwork(String, String, AddWEPNetworkCallbacks)} + * {@link #addWPA2Network(String, String)} + * {@link #addWPA2Network(String, String, AddWPA2NetworkCallbacks)} * * @return int - The return code from WifiManager for network creation (-1 for failure) */ - private int addNetwork(WifiConfiguration wifiConfig) { - int result = mWifiManager.addNetwork(wifiConfig); + private int addNetwork(@NonNull WifiConfiguration wifiConfiguration) { + int result = mWifiManager.addNetwork(wifiConfiguration); if (result != WIFI_MANAGER_FAILURE) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, "Successfully added network"); } - mWifiManager.saveConfiguration(); } else { if (LogUtil.isLoggable(TAG, Log.ERROR, mLoggingEnabled)) { Log.e(TAG, "Failed to add network"); @@ -871,19 +1751,74 @@ private int addNetwork(WifiConfiguration wifiConfig) { } /** - * Used internally by addOpenNetwork, addWEPNetwork, addWPA2Network, and - * isNetworkInConfigurationList to see if an ssid is already saved (CASE SENSITIVE!) + * Used internally to see if an ssid is already saved + * + * *NOTE* CASE SENSITIVE! * * @param ssid - The ssid to check for in the configured network list * - * {@link #addOpenNetwork(String)} - * {@link #addWEPNetwork(String, String)} - * {@link #addWPA2Network(String, String)} - * {@link #isNetworkInConfigurationList(String)} + * Used by + * {@link #addOpenNetwork(String)} + * {@link #addOpenNetwork(String, AddOpenNetworkCallbacks)} + * {@link #addWEPNetwork(String, String)} + * {@link #addWEPNetwork(String, String, AddWEPNetworkCallbacks)} + * {@link #addWPA2Network(String, String)} + * {@link #addWPA2Network(String, String, AddWPA2NetworkCallbacks)} + * {@link #isNetworkInConfigurationList(String)} * * @return boolean - If the ssid was found in the configuration list */ - private boolean checkIfNetworkInConfigurationList(String ssid) { + private boolean checkIfNetworkInConfigurationList(@NonNull String ssid) { + WifiConfiguration wifiConfiguration = findNetworkInConfigurationList(ssid); + return wifiConfiguration != null; + } + + /** + * A method to execute logic on a background thread + * + * Used by + * {@link #addOpenNetwork(String, AddOpenNetworkCallbacks)} + * {@link #addWEPNetwork(String, String, AddWEPNetworkCallbacks)} + * {@link #addWPA2Network(String, String, AddWPA2NetworkCallbacks)} + * {@link #connectToNetwork(String, int, ConnectToNetworkCallbacks)} + * {@link #disableWifi(DisableWifiCallbacks)} + * {@link #disconnectFromCurrentNetwork(DisconnectFromCurrentNetworkCallbacks)} + * {@link #enableWifi(EnableWifiCallbacks)} + * {@link #getCurrentNetwork(GetCurrentNetworkCallbacks)} + * {@link #getFrequency(GetFrequencyCallbacks)} + * {@link #getFrequency(WifiInfo, GetFrequencyCallbacks)} + * {@link #getNearbyAccessPoints(boolean, GetNearbyAccessPointsCallbacks)} + * {@link #getSavedNetwork(String, GetSavedNetworkCallbacks)} + * {@link #getSavedNetworks(GetSavedNetworksCallbacks)} + * {@link #removeNetwork(String, RemoveNetworkCallbacks)} + * {@link #searchForSSID(String, int, SearchForSSIDCallbacks)} + * + * @param runnable - The block of code to execute in the background + */ + private void execute(Runnable runnable) { + if (mWiseFyHandler == null) { + setupWiseFyThread(); + } + mWiseFyHandler.post(runnable); + } + + /** + * Used internally to see if an ssid is already saved + * + * *NOTE* CASE SENSITIVE! + * + * @param ssid - The ssid to find in the configured network list + * + * Used by + * {@link #checkIfNetworkInConfigurationList(String)} + * {@link #getSavedNetwork(String)} + * {@link #getSavedNetwork(String, GetSavedNetworkCallbacks)} + * {@link #removeNetwork(String)} + * {@link #removeNetwork(String, RemoveNetworkCallbacks)} + * + * @return WiFiConfiguration|null - A matching configured network in the list or null if no matching ones found + */ + private WifiConfiguration findNetworkInConfigurationList(@NonNull String ssid) { List list = mWifiManager.getConfiguredNetworks(); if (list != null && list.size() > 0) { for (int i = 0; i < list.size(); i++) { @@ -897,7 +1832,7 @@ private boolean checkIfNetworkInConfigurationList(String ssid) { if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { Log.d(TAG, "Found SSID in list"); } - return true; + return wifiConfiguration; } } } @@ -906,19 +1841,37 @@ private boolean checkIfNetworkInConfigurationList(String ssid) { Log.w(TAG, "Found 0 configured networks"); } } - return false; + return null; } /** - * Used internally by connectToNetwork to check if the device connects to - * a given SSID within a specified time (timeout is in millis) + * Used internally to setup a WiseFyThread to run background operations + * + * Used by + * {@link #execute(Runnable)} + */ + private void setupWiseFyThread() { + mWiseFyHandlerThread = new WiseFyHandlerThread(WiseFyHandlerThread.TAG, mLoggingEnabled); + mWiseFyHandlerThread.start(); + Looper looper = mWiseFyHandlerThread.getLooper(); + mWiseFyHandler = new Handler(looper); + } + + /** + * Used internally to check if the device connects to a given SSID + * within a specified time (timeout is in millis) * * @param ssid - The ssid to wait for the device to connect to * @param timeoutInMillis - The number of milliseconds to wait * + * Used by + * {@link #connectToNetwork(String, int)} + * {@link #connectToNetwork(String, int, ConnectToNetworkCallbacks)} + * * @return boolean - If the device is connected to the ssid in the given time */ - private boolean waitToConnectToSSID(String ssid, int timeoutInMillis) { + @WaitsForTimeout + private boolean waitToConnectToSSID(@NonNull String ssid, int timeoutInMillis) { long currentTime; long endTime = System.currentTimeMillis() + timeoutInMillis; if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { diff --git a/wisefy/src/main/java/com/isupatches/wisefy/annotations/Async.java b/wisefy/src/main/java/com/isupatches/wisefy/annotations/Async.java new file mode 100644 index 00000000..39b2b939 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/annotations/Async.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.annotations; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Documented +@Target(ElementType.METHOD) +@Inherited +@Retention(RetentionPolicy.RUNTIME) +public @interface Async { + +} + diff --git a/wisefy/src/main/java/com/isupatches/wisefy/annotations/CallingThread.java b/wisefy/src/main/java/com/isupatches/wisefy/annotations/CallingThread.java new file mode 100644 index 00000000..fac6eaa4 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/annotations/CallingThread.java @@ -0,0 +1,33 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.annotations; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Documented +@Target(ElementType.METHOD) +@Inherited +@Retention(RetentionPolicy.RUNTIME) +public @interface CallingThread { + +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/annotations/Sync.java b/wisefy/src/main/java/com/isupatches/wisefy/annotations/Sync.java new file mode 100644 index 00000000..2b5bb033 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/annotations/Sync.java @@ -0,0 +1,33 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.annotations; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Documented +@Target(ElementType.METHOD) +@Inherited +@Retention(RetentionPolicy.RUNTIME) +public @interface Sync { + +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/annotations/WaitsForTimeout.java b/wisefy/src/main/java/com/isupatches/wisefy/annotations/WaitsForTimeout.java new file mode 100644 index 00000000..90d71a8b --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/annotations/WaitsForTimeout.java @@ -0,0 +1,34 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.annotations; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Documented +@Target(ElementType.METHOD) +@Inherited +@Retention(RetentionPolicy.RUNTIME) +public @interface WaitsForTimeout { + +} + diff --git a/wisefy/src/main/java/com/isupatches/wisefy/annotations/WiseFyThread.java b/wisefy/src/main/java/com/isupatches/wisefy/annotations/WiseFyThread.java new file mode 100644 index 00000000..e5b6d2b0 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/annotations/WiseFyThread.java @@ -0,0 +1,33 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.annotations; + + +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +@Documented +@Target(ElementType.METHOD) +@Inherited +@Retention(RetentionPolicy.RUNTIME) +public @interface WiseFyThread { + +} \ No newline at end of file diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/AddOpenNetworkCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/AddOpenNetworkCallbacks.java new file mode 100644 index 00000000..28d721df --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/AddOpenNetworkCallbacks.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +import android.net.wifi.WifiConfiguration; + + +public interface AddOpenNetworkCallbacks { + void addOpenNetworkWiseFyFailure(Integer wisefyReturnCode); + void failureAddingOpenNetwork(Integer wifiManagerReturnCode); + void openNetworkAdded(WifiConfiguration wifiConfiguration); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/AddWEPNetworkCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/AddWEPNetworkCallbacks.java new file mode 100644 index 00000000..b9fabc55 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/AddWEPNetworkCallbacks.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +import android.net.wifi.WifiConfiguration; + + +public interface AddWEPNetworkCallbacks { + void addWEPNetworkWiseFyFailure(Integer wisefyReturnCode); + void failureAddingWEPNetwork(Integer wifiManagerReturnCode); + void wepNetworkAdded(WifiConfiguration wifiConfiguration); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/AddWPA2NetworkCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/AddWPA2NetworkCallbacks.java new file mode 100644 index 00000000..9236e77a --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/AddWPA2NetworkCallbacks.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +import android.net.wifi.WifiConfiguration; + + +public interface AddWPA2NetworkCallbacks { + void addWPA2NetworkWiseFyFailure(Integer wisefyReturnCode); + void failureAddingWPA2Network(Integer wifiManagerReturnCode); + void wpa2NetworkAdded(WifiConfiguration wifiConfiguration); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/ConnectToNetworkCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/ConnectToNetworkCallbacks.java new file mode 100644 index 00000000..51ad390a --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/ConnectToNetworkCallbacks.java @@ -0,0 +1,24 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +public interface ConnectToNetworkCallbacks { + void connectedToNetwork(); + void connectToNetworkWiseFyFailure(Integer wisefyReturnCode); + void failureConnectingToNetwork(); + void networkNotFoundToConnectTo(); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/DisableWifiCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/DisableWifiCallbacks.java new file mode 100644 index 00000000..643fbc48 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/DisableWifiCallbacks.java @@ -0,0 +1,23 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +public interface DisableWifiCallbacks { + void disableWifiWiseFyFailure(Integer wisefyReturnCode); + void failureDisablingWifi(); + void wifiDisabled(); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/DisconnectFromCurrentNetworkCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/DisconnectFromCurrentNetworkCallbacks.java new file mode 100644 index 00000000..24cbc4e7 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/DisconnectFromCurrentNetworkCallbacks.java @@ -0,0 +1,23 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +public interface DisconnectFromCurrentNetworkCallbacks { + void disconnectedFromCurrentNetwork(); + void disconnectFromCurrentNetworkWiseFyFailure(Integer wisefyReturnCode); + void failureDisconnectingFromCurrentNetwork(); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/EnableWifiCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/EnableWifiCallbacks.java new file mode 100644 index 00000000..3f7876c6 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/EnableWifiCallbacks.java @@ -0,0 +1,23 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +public interface EnableWifiCallbacks { + void enableWifiWiseFyFailure(Integer wisefyReturnCode); + void failureEnablingWifi(); + void wifiEnabled(); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetCurrentNetworkCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetCurrentNetworkCallbacks.java new file mode 100644 index 00000000..24129b37 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetCurrentNetworkCallbacks.java @@ -0,0 +1,25 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +import android.net.wifi.WifiInfo; + + +public interface GetCurrentNetworkCallbacks { + void getCurrentNetworkWiseFyFailure(Integer wisefyReturnCode); + void retrievedCurrentNetwork(WifiInfo currentNetwork); +} \ No newline at end of file diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetFrequencyCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetFrequencyCallbacks.java new file mode 100644 index 00000000..ec10a774 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetFrequencyCallbacks.java @@ -0,0 +1,23 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +public interface GetFrequencyCallbacks { + void failureGettingFrequency(); + void getFrequencyWiseFyFailure(Integer wisefyReturnCode); + void retrievedFrequency(int frequency); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetNearbyAccessPointsCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetNearbyAccessPointsCallbacks.java new file mode 100644 index 00000000..c0181ec9 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetNearbyAccessPointsCallbacks.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +import android.net.wifi.ScanResult; +import java.util.List; + + +public interface GetNearbyAccessPointsCallbacks { + void getNearbyAccessPointsWiseFyFailure(Integer wisefyReturnCode); + void retrievedNearbyAccessPoints(List nearbyAccessPoints); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetSavedNetworkCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetSavedNetworkCallbacks.java new file mode 100644 index 00000000..e4eceb1f --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetSavedNetworkCallbacks.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +import android.net.wifi.WifiConfiguration; + + +public interface GetSavedNetworkCallbacks { + void getSavedNetworkWiseFyFailure(Integer wisefyReturnCode); + void networkNotFound(); + void retrievedSavedNetwork(WifiConfiguration savedNetwork); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetSavedNetworksCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetSavedNetworksCallbacks.java new file mode 100644 index 00000000..79d3dfbb --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/GetSavedNetworksCallbacks.java @@ -0,0 +1,26 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +import android.net.wifi.WifiConfiguration; +import java.util.List; + + +public interface GetSavedNetworksCallbacks { + void getSavedNetworksWiseFyFailure(Integer wisefyReturnCode); + void retrievedSavedNetworks(List savedNetworks); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/RemoveNetworkCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/RemoveNetworkCallbacks.java new file mode 100644 index 00000000..f366d6b4 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/RemoveNetworkCallbacks.java @@ -0,0 +1,24 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +public interface RemoveNetworkCallbacks { + void failureRemovingNetwork(); + void networkNotFoundToRemove(); + void networkRemoved(); + void removeNetworkWiseFyFailure(Integer wisefyReturnCode); +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/callbacks/SearchForSSIDCallbacks.java b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/SearchForSSIDCallbacks.java new file mode 100644 index 00000000..656e0775 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/callbacks/SearchForSSIDCallbacks.java @@ -0,0 +1,23 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.callbacks; + + +public interface SearchForSSIDCallbacks { + void searchForSSIDWiseFyFailure(Integer wisefyReturnCode); + void ssidFound(String ssid); + void ssidNotFound(); +} \ No newline at end of file diff --git a/wisefy/src/main/java/com/isupatches/wisefy/constants/WiseFyCodes.java b/wisefy/src/main/java/com/isupatches/wisefy/constants/WiseFyCodes.java index a6d356cc..c5a42981 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/constants/WiseFyCodes.java +++ b/wisefy/src/main/java/com/isupatches/wisefy/constants/WiseFyCodes.java @@ -17,6 +17,7 @@ public class WiseFyCodes { - public static final int FAILURE = -1000; + public static final int MISSING_PARAMETER = -1000; public static final int NETWORK_ALREADY_CONFIGURED = -1001; + public static final int NULL_MANAGER = -1002; } diff --git a/wisefy/src/main/java/com/isupatches/wisefy/threads/WiseFyHandlerThread.java b/wisefy/src/main/java/com/isupatches/wisefy/threads/WiseFyHandlerThread.java new file mode 100644 index 00000000..2e9d9dfe --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/threads/WiseFyHandlerThread.java @@ -0,0 +1,35 @@ +package com.isupatches.wisefy.threads; + + +import android.os.HandlerThread; +import android.util.Log; +import com.isupatches.wisefy.util.LogUtil; + + +public class WiseFyHandlerThread extends HandlerThread { + + public static final String TAG = WiseFyHandlerThread.class.getSimpleName(); + + private boolean mLoggingEnabled; + + public WiseFyHandlerThread(String name, boolean loggingEnabled) { + super(name); + this.mLoggingEnabled = loggingEnabled; + } + + @Override + public synchronized void start() { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "WiseFyHandlerThread starting"); + } + super.start(); + } + + @Override + public boolean quitSafely() { + if (LogUtil.isLoggable(TAG, Log.DEBUG, mLoggingEnabled)) { + Log.d(TAG, "quitSafely called on WiseFyHandlerThread"); + } + return super.quitSafely(); + } +} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/util/LogUtil.java b/wisefy/src/main/java/com/isupatches/wisefy/util/LogUtil.java index ed1b37aa..6226f8e7 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/util/LogUtil.java +++ b/wisefy/src/main/java/com/isupatches/wisefy/util/LogUtil.java @@ -28,9 +28,11 @@ public class LogUtil { * Checks to see given a TAG, log level, and if logging is enabled if logging should occur * * @param tag - The tag to be used for the log - * @param level - The level of logging (i.e Log.DEBUG, Log.WARN, Log.ERROR, etc) {@link Log} + * @param level - The level of logging (i.e Log.DEBUG, Log.WARN, Log.ERROR, etc) * @param loggingEnabled - If the instances of WiseFy has logging set to always be enabled * + * {@link Log} + * * @return boolean - if logging is enabled */ public static boolean isLoggable(String tag, int level, boolean loggingEnabled) { diff --git a/wisefy/src/main/java/com/isupatches/wisefy/util/SSIDUtil.java b/wisefy/src/main/java/com/isupatches/wisefy/util/SSIDUtil.java index 11137a3b..17d77c4c 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/util/SSIDUtil.java +++ b/wisefy/src/main/java/com/isupatches/wisefy/util/SSIDUtil.java @@ -32,11 +32,11 @@ private SSIDUtil() { /** * @return instance of SSIDUtil */ - public static SSIDUtil getInstance() { + static SSIDUtil getInstance() { return SSID_UTIL; } - public String convertSSIDForConfig(String ssid) { + String convertSSIDForConfig(String ssid) { // On devices with version Kitkat and below, We need to send SSID name // with double quotes. On devices with version Lollipop, We need to send // SSID name without double quotes diff --git a/wisefy/src/main/java/com/isupatches/wisefy/util/WifiConfigurationUtil.java b/wisefy/src/main/java/com/isupatches/wisefy/util/WifiConfigurationUtil.java new file mode 100644 index 00000000..bed24325 --- /dev/null +++ b/wisefy/src/main/java/com/isupatches/wisefy/util/WifiConfigurationUtil.java @@ -0,0 +1,91 @@ +/* + * Copyright 2017 Patches Klinefelter + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.isupatches.wisefy.util; + + +import android.net.wifi.WifiConfiguration; + + +public class WifiConfigurationUtil { + + private static final WifiConfigurationUtil WIFI_CONFIGURATION_UTIL = new WifiConfigurationUtil(); + + private SSIDUtil mSSIDUtil = SSIDUtil.getInstance(); + + /** + * Private constructor with no setup + */ + private WifiConfigurationUtil() { + } + + /** + * @return instance of WifiConfigurationUtil + */ + public static WifiConfigurationUtil getInstance() { + return WIFI_CONFIGURATION_UTIL; + } + + public WifiConfiguration generateOpenNetworkConfiguration(String ssid) { + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = mSSIDUtil.convertSSIDForConfig(ssid); + wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); + wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN); + wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA); + wifiConfiguration.allowedAuthAlgorithms.clear(); + wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); + wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); + wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); + wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); + wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); + wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); + return wifiConfiguration; + } + + public WifiConfiguration generateWEPNetworkConfiguration(String ssid, String password) { + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = mSSIDUtil.convertSSIDForConfig(ssid); + wifiConfiguration.wepKeys[0] = "\"" + password + "\""; + wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); + wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN); + wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA); + wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); + wifiConfiguration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); + wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); + wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); + wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); + wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); + return wifiConfiguration; + } + + public WifiConfiguration generateWPA2NetworkConfiguration(String ssid, String password) { + WifiConfiguration wifiConfiguration = new WifiConfiguration(); + wifiConfiguration.SSID = mSSIDUtil.convertSSIDForConfig(ssid); + wifiConfiguration.preSharedKey = "\"" + password + "\""; + wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN); + wifiConfiguration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); + wifiConfiguration.status = WifiConfiguration.Status.ENABLED; + wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); + wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); + wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); + wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); + wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.RSN); + wifiConfiguration.allowedProtocols.set(WifiConfiguration.Protocol.WPA); + + wifiConfiguration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); + wifiConfiguration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); + return wifiConfiguration; + } +}