diff --git a/wisefy/addnetwork/build.gradle.kts b/wisefy/addnetwork/build.gradle.kts index 22dbcb3f..6dd32cea 100644 --- a/wisefy/addnetwork/build.gradle.kts +++ b/wisefy/addnetwork/build.gradle.kts @@ -1,6 +1,7 @@ import com.isupatches.android.wisefy.build.BuildVersions import com.isupatches.android.wisefy.build.Dependencies import com.isupatches.android.wisefy.build.PublishingConstants +import com.isupatches.android.wisefy.build.TestDependencies plugins { id("com.android.library") @@ -26,16 +27,16 @@ dependencies { // Unit Tests testImplementation(project(":testsupport")) - testImplementation(com.isupatches.android.wisefy.build.TestDependencies.JUNIT) - testImplementation(com.isupatches.android.wisefy.build.TestDependencies.Mockito.CORE) - testImplementation(com.isupatches.android.wisefy.build.TestDependencies.Kotlin.Coroutines.TEST) + testImplementation(TestDependencies.JUNIT) + testImplementation(TestDependencies.Mockito.CORE) + testImplementation(TestDependencies.Kotlin.Coroutines.TEST) // Instrumentation Tests androidTestImplementation(project(":testsupport")) - androidTestImplementation(com.isupatches.android.wisefy.build.TestDependencies.JUNIT) - androidTestImplementation(com.isupatches.android.wisefy.build.TestDependencies.AndroidX.TEST_RUNNER) - androidTestImplementation(com.isupatches.android.wisefy.build.TestDependencies.AndroidX.TEST_RULES) - androidTestImplementation(com.isupatches.android.wisefy.build.TestDependencies.Mockito.CORE) - androidTestImplementation(com.isupatches.android.wisefy.build.TestDependencies.Mockito.ANDROID) - androidTestImplementation(com.isupatches.android.wisefy.build.TestDependencies.Kotlin.Coroutines.TEST) + androidTestImplementation(TestDependencies.JUNIT) + androidTestImplementation(TestDependencies.AndroidX.TEST_RUNNER) + androidTestImplementation(TestDependencies.AndroidX.TEST_RULES) + androidTestImplementation(TestDependencies.Mockito.CORE) + androidTestImplementation(TestDependencies.Mockito.ANDROID) + androidTestImplementation(TestDependencies.Kotlin.Coroutines.TEST) } diff --git a/wisefy/addnetwork/src/test/java/com/isupatches/android/wisefy/addnetwork/WisefyAddNetworkDelegateAddNetworkSyncTest.kt b/wisefy/addnetwork/src/test/java/com/isupatches/android/wisefy/addnetwork/WisefyAddNetworkDelegateAddNetworkSyncTest.kt index e7e8d197..ddb076aa 100644 --- a/wisefy/addnetwork/src/test/java/com/isupatches/android/wisefy/addnetwork/WisefyAddNetworkDelegateAddNetworkSyncTest.kt +++ b/wisefy/addnetwork/src/test/java/com/isupatches/android/wisefy/addnetwork/WisefyAddNetworkDelegateAddNetworkSyncTest.kt @@ -22,13 +22,9 @@ import com.isupatches.android.wisefy.core.assertions.WisefyAssertions import com.isupatches.android.wisefy.core.logging.DefaultWisefyLogger import com.isupatches.android.wisefy.core.util.SdkUtilImpl import com.isupatches.android.wisefy.testsupport.TestCoroutineDispatchProvider -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.sync.Mutex -import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestScope -import kotlinx.coroutines.test.resetMain -import kotlinx.coroutines.test.setMain import org.junit.After import org.junit.Assert.assertEquals import org.junit.Before @@ -57,20 +53,16 @@ internal class WisefyAddNetworkDelegateAddNetworkSyncTest( private var closable: AutoCloseable? = null - private lateinit var testScope: TestScope - @Before fun setUp() { - Dispatchers.setMain(StandardTestDispatcher()) closable = MockitoAnnotations.openMocks(this) - testScope = TestScope() delegate = WisefyAddNetworkDelegate( wifiManager = mockWifiManager, logger = DefaultWisefyLogger(), assertions = WisefyAssertions(false), sdkUtil = SdkUtilImpl(), coroutineDispatcherProvider = TestCoroutineDispatchProvider(), - scope = testScope, + scope = TestScope(), savedNetworkMutex = Mutex(), adapter = mockAdapter ) @@ -78,7 +70,6 @@ internal class WisefyAddNetworkDelegateAddNetworkSyncTest( @After fun tearDown() { - Dispatchers.resetMain() closable?.close() } diff --git a/wisefy/wifi/build.gradle.kts b/wisefy/wifi/build.gradle.kts index e5cf054a..cf567186 100644 --- a/wisefy/wifi/build.gradle.kts +++ b/wisefy/wifi/build.gradle.kts @@ -1,6 +1,7 @@ import com.isupatches.android.wisefy.build.BuildVersions import com.isupatches.android.wisefy.build.Dependencies import com.isupatches.android.wisefy.build.PublishingConstants +import com.isupatches.android.wisefy.build.TestDependencies plugins { id("com.android.library") @@ -23,4 +24,19 @@ dependencies { // Kotlin implementation(Dependencies.Kotlin.COROUTINES) + + // Unit Tests + testImplementation(project(":testsupport")) + testImplementation(TestDependencies.JUNIT) + testImplementation(TestDependencies.Mockito.CORE) + testImplementation(TestDependencies.Kotlin.Coroutines.TEST) + + // Instrumentation Tests + androidTestImplementation(project(":testsupport")) + androidTestImplementation(TestDependencies.JUNIT) + androidTestImplementation(TestDependencies.AndroidX.TEST_RUNNER) + androidTestImplementation(TestDependencies.AndroidX.TEST_RULES) + androidTestImplementation(TestDependencies.Mockito.CORE) + androidTestImplementation(TestDependencies.Mockito.ANDROID) + androidTestImplementation(TestDependencies.Kotlin.Coroutines.TEST) } diff --git a/wisefy/wifi/src/androidTest/java/com/isupatches/android/wisefy/wifi/os/impls/Android29WifiImplTest.kt b/wisefy/wifi/src/androidTest/java/com/isupatches/android/wisefy/wifi/os/impls/Android29WifiImplTest.kt new file mode 100644 index 00000000..48258660 --- /dev/null +++ b/wisefy/wifi/src/androidTest/java/com/isupatches/android/wisefy/wifi/os/impls/Android29WifiImplTest.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2023 Patches Barrett + * + * 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.android.wisefy.wifi.os.impls + +internal class Android29WifiImplTest diff --git a/wisefy/wifi/src/androidTest/java/com/isupatches/android/wisefy/wifi/os/impls/DefaultWifiApiImplTest.kt b/wisefy/wifi/src/androidTest/java/com/isupatches/android/wisefy/wifi/os/impls/DefaultWifiApiImplTest.kt new file mode 100644 index 00000000..94c84da0 --- /dev/null +++ b/wisefy/wifi/src/androidTest/java/com/isupatches/android/wisefy/wifi/os/impls/DefaultWifiApiImplTest.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2023 Patches Barrett + * + * 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.android.wisefy.wifi.os.impls + +internal class DefaultWifiApiImplTest diff --git a/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateDisableWifiAsyncTest.kt b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateDisableWifiAsyncTest.kt new file mode 100644 index 00000000..a092bebf --- /dev/null +++ b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateDisableWifiAsyncTest.kt @@ -0,0 +1,228 @@ +/* + * Copyright 2023 Patches Barrett + * + * 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.android.wisefy.wifi + +import android.content.Context +import android.net.wifi.WifiManager +import com.isupatches.android.wisefy.core.assertions.WisefyAssertions +import com.isupatches.android.wisefy.core.logging.DefaultWisefyLogger +import com.isupatches.android.wisefy.core.util.SdkUtilImpl +import com.isupatches.android.wisefy.testsupport.TestCoroutineDispatchProvider +import com.isupatches.android.wisefy.testsupport.anyNonNull +import com.isupatches.android.wisefy.wifi.callbacks.DisableWifiCallbacks +import com.isupatches.android.wisefy.wifi.entities.DisableWifiRequest +import com.isupatches.android.wisefy.wifi.entities.DisableWifiResult +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.test.setMain +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import org.mockito.BDDMockito.given +import org.mockito.Mock +import org.mockito.Mockito.mock +import org.mockito.Mockito.never +import org.mockito.Mockito.times +import org.mockito.Mockito.verify +import org.mockito.MockitoAnnotations + +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(Parameterized::class) +internal class WisefyWifiDelegateDisableWifiAsyncTest( + private val params: DisableWifiParams +) { + + @Mock + private lateinit var mockWifiManager: WifiManager + + @Mock + private lateinit var mockAdapter: WifiApi + + private lateinit var delegate: WisefyWifiDelegate + + private var closable: AutoCloseable? = null + + private lateinit var testScope: TestScope + + @Before + fun setUp() { + Dispatchers.setMain(StandardTestDispatcher()) + closable = MockitoAnnotations.openMocks(this) + testScope = TestScope() + delegate = WisefyWifiDelegate( + wifiManager = mockWifiManager, + logger = DefaultWisefyLogger(), + assertions = WisefyAssertions(false), + sdkUtil = SdkUtilImpl(), + coroutineDispatcherProvider = TestCoroutineDispatchProvider(), + scope = testScope, + wifiMutex = Mutex(), + adapter = mockAdapter + ) + } + + @After + fun tearDown() { + Dispatchers.resetMain() + closable?.close() + } + + @Test + fun disableWifi() = runTest { + // Given + given(mockAdapter.disableWifi(params.request)).willReturn(params.result) + + // Then + delegate.disableWifi(params.request, params.mockCallbacks) + advanceUntilIdle() + + // When + verify(mockAdapter, times(1)).disableWifi(params.request) + when { + params.mockCallbacks != null && params.expectedFailureCallbacks > 0 -> { + verify(params.mockCallbacks, times(params.expectedFailureCallbacks)).onFailureDisablingWifi( + params.result as DisableWifiResult.Failure + ) + } + params.mockCallbacks != null -> { + verify(params.mockCallbacks, never()).onFailureDisablingWifi(anyNonNull()) + } + } + when { + params.mockCallbacks != null && params.expectedSuccessCallbacks > 0 -> { + verify(params.mockCallbacks, times(params.expectedSuccessCallbacks)).onSuccessDisablingWifi( + params.result as DisableWifiResult.Success + ) + } + params.mockCallbacks != null -> { + verify(params.mockCallbacks, never()).onSuccessDisablingWifi(anyNonNull()) + } + } + } + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{index}: {0}") + fun params(): List { + return listOf( + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Success.Disabled, + mockCallbacks = mock(DisableWifiCallbacks::class.java), + expectedSuccessCallbacks = 1 + ), + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Success.WifiSettingScreenOpened, + mockCallbacks = mock(DisableWifiCallbacks::class.java), + expectedSuccessCallbacks = 1 + ), + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Success.Disabled, + mockCallbacks = null + ), + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Success.WifiSettingScreenOpened, + mockCallbacks = null + ), + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Failure.UnableToDisable, + mockCallbacks = mock(DisableWifiCallbacks::class.java), + expectedFailureCallbacks = 1 + ), + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Failure.Assertion("Test"), + mockCallbacks = mock(DisableWifiCallbacks::class.java), + expectedFailureCallbacks = 1 + ), + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Failure.UnableToDisable, + mockCallbacks = null + ), + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Failure.Assertion("Test"), + mockCallbacks = null + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Success.Disabled, + mockCallbacks = mock(DisableWifiCallbacks::class.java), + expectedSuccessCallbacks = 1 + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Success.WifiSettingScreenOpened, + mockCallbacks = mock(DisableWifiCallbacks::class.java), + expectedSuccessCallbacks = 1 + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Success.Disabled, + mockCallbacks = null + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Success.WifiSettingScreenOpened, + mockCallbacks = null + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Failure.UnableToDisable, + mockCallbacks = mock(DisableWifiCallbacks::class.java), + expectedFailureCallbacks = 1 + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Failure.Assertion("Test"), + mockCallbacks = mock(DisableWifiCallbacks::class.java), + expectedFailureCallbacks = 1 + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Failure.UnableToDisable, + mockCallbacks = null + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Failure.Assertion("Test"), + mockCallbacks = null + ) + ) + } + + data class DisableWifiParams( + val request: DisableWifiRequest, + val result: DisableWifiResult, + val mockCallbacks: DisableWifiCallbacks?, + val expectedFailureCallbacks: Int = 0, + val expectedSuccessCallbacks: Int = 0 + ) + } +} diff --git a/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateDisableWifiSyncTest.kt b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateDisableWifiSyncTest.kt new file mode 100644 index 00000000..3e32fe8c --- /dev/null +++ b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateDisableWifiSyncTest.kt @@ -0,0 +1,136 @@ +/* + * Copyright 2023 Patches Barrett + * + * 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.android.wisefy.wifi + +import android.content.Context +import android.net.wifi.WifiManager +import com.isupatches.android.wisefy.core.assertions.WisefyAssertions +import com.isupatches.android.wisefy.core.logging.DefaultWisefyLogger +import com.isupatches.android.wisefy.core.util.SdkUtilImpl +import com.isupatches.android.wisefy.testsupport.TestCoroutineDispatchProvider +import com.isupatches.android.wisefy.wifi.entities.DisableWifiRequest +import com.isupatches.android.wisefy.wifi.entities.DisableWifiResult +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.test.TestScope +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import org.mockito.BDDMockito.given +import org.mockito.Mock +import org.mockito.Mockito.mock +import org.mockito.Mockito.times +import org.mockito.Mockito.verify +import org.mockito.MockitoAnnotations + +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(Parameterized::class) +internal class WisefyWifiDelegateDisableWifiSyncTest( + private val params: DisableWifiParams +) { + + @Mock + private lateinit var mockWifiManager: WifiManager + + @Mock + private lateinit var mockAdapter: WifiApi + + private lateinit var delegate: WisefyWifiDelegate + + private var closable: AutoCloseable? = null + + @Before + fun setUp() { + closable = MockitoAnnotations.openMocks(this) + delegate = WisefyWifiDelegate( + wifiManager = mockWifiManager, + logger = DefaultWisefyLogger(), + assertions = WisefyAssertions(false), + sdkUtil = SdkUtilImpl(), + coroutineDispatcherProvider = TestCoroutineDispatchProvider(), + scope = TestScope(), + wifiMutex = Mutex(), + adapter = mockAdapter + ) + } + + @After + fun tearDown() { + closable?.close() + } + + @Test + fun disableWifi() { + // Given + given(mockAdapter.disableWifi(params.request)).willReturn(params.result) + + // Then + val result = delegate.disableWifi(params.request) + + // When + verify(mockAdapter, times(1)).disableWifi(params.request) + assertEquals(params.result, result) + } + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{index}: {0}") + fun params(): List { + return listOf( + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Success.Disabled + ), + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Success.WifiSettingScreenOpened + ), + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Failure.UnableToDisable + ), + DisableWifiParams( + request = DisableWifiRequest.Default, + result = DisableWifiResult.Failure.Assertion("Test") + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Success.Disabled + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Success.WifiSettingScreenOpened + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Failure.UnableToDisable + ), + DisableWifiParams( + request = DisableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = DisableWifiResult.Failure.Assertion("Test") + ) + ) + } + + data class DisableWifiParams( + val request: DisableWifiRequest, + val result: DisableWifiResult + ) + } +} diff --git a/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateEnableWifiAsyncTest.kt b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateEnableWifiAsyncTest.kt new file mode 100644 index 00000000..9ca23bb0 --- /dev/null +++ b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateEnableWifiAsyncTest.kt @@ -0,0 +1,228 @@ +/* + * Copyright 2023 Patches Barrett + * + * 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.android.wisefy.wifi + +import android.content.Context +import android.net.wifi.WifiManager +import com.isupatches.android.wisefy.core.assertions.WisefyAssertions +import com.isupatches.android.wisefy.core.logging.DefaultWisefyLogger +import com.isupatches.android.wisefy.core.util.SdkUtilImpl +import com.isupatches.android.wisefy.testsupport.TestCoroutineDispatchProvider +import com.isupatches.android.wisefy.testsupport.anyNonNull +import com.isupatches.android.wisefy.wifi.callbacks.EnableWifiCallbacks +import com.isupatches.android.wisefy.wifi.entities.EnableWifiRequest +import com.isupatches.android.wisefy.wifi.entities.EnableWifiResult +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.test.setMain +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import org.mockito.BDDMockito.given +import org.mockito.Mock +import org.mockito.Mockito.mock +import org.mockito.Mockito.never +import org.mockito.Mockito.times +import org.mockito.Mockito.verify +import org.mockito.MockitoAnnotations + +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(Parameterized::class) +internal class WisefyWifiDelegateEnableWifiAsyncTest( + private val params: EnableWifiParams +) { + + @Mock + private lateinit var mockWifiManager: WifiManager + + @Mock + private lateinit var mockAdapter: WifiApi + + private lateinit var delegate: WisefyWifiDelegate + + private var closable: AutoCloseable? = null + + private lateinit var testScope: TestScope + + @Before + fun setUp() { + Dispatchers.setMain(StandardTestDispatcher()) + closable = MockitoAnnotations.openMocks(this) + testScope = TestScope() + delegate = WisefyWifiDelegate( + wifiManager = mockWifiManager, + logger = DefaultWisefyLogger(), + assertions = WisefyAssertions(false), + sdkUtil = SdkUtilImpl(), + coroutineDispatcherProvider = TestCoroutineDispatchProvider(), + scope = testScope, + wifiMutex = Mutex(), + adapter = mockAdapter + ) + } + + @After + fun tearDown() { + Dispatchers.resetMain() + closable?.close() + } + + @Test + fun enableWifi() = runTest { + // Given + given(mockAdapter.enableWifi(params.request)).willReturn(params.result) + + // Then + delegate.enableWifi(params.request, params.mockCallbacks) + advanceUntilIdle() + + // When + verify(mockAdapter, times(1)).enableWifi(params.request) + when { + params.mockCallbacks != null && params.expectedFailureCallbacks > 0 -> { + verify(params.mockCallbacks, times(params.expectedFailureCallbacks)).onFailureEnablingWifi( + params.result as EnableWifiResult.Failure + ) + } + params.mockCallbacks != null -> { + verify(params.mockCallbacks, never()).onFailureEnablingWifi(anyNonNull()) + } + } + when { + params.mockCallbacks != null && params.expectedSuccessCallbacks > 0 -> { + verify(params.mockCallbacks, times(params.expectedSuccessCallbacks)).onSuccessEnablingWifi( + params.result as EnableWifiResult.Success + ) + } + params.mockCallbacks != null -> { + verify(params.mockCallbacks, never()).onSuccessEnablingWifi(anyNonNull()) + } + } + } + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{index}: {0}") + fun params(): List { + return listOf( + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Success.Enabled, + mockCallbacks = mock(EnableWifiCallbacks::class.java), + expectedSuccessCallbacks = 1 + ), + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Success.WifiSettingScreenOpened, + mockCallbacks = mock(EnableWifiCallbacks::class.java), + expectedSuccessCallbacks = 1 + ), + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Success.Enabled, + mockCallbacks = null + ), + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Success.WifiSettingScreenOpened, + mockCallbacks = null + ), + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Failure.UnableToEnable, + mockCallbacks = mock(EnableWifiCallbacks::class.java), + expectedFailureCallbacks = 1 + ), + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Failure.Assertion("Test"), + mockCallbacks = mock(EnableWifiCallbacks::class.java), + expectedFailureCallbacks = 1 + ), + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Failure.UnableToEnable, + mockCallbacks = null + ), + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Failure.Assertion("Test"), + mockCallbacks = null + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = EnableWifiResult.Success.Enabled, + mockCallbacks = mock(EnableWifiCallbacks::class.java), + expectedSuccessCallbacks = 1 + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = EnableWifiResult.Success.WifiSettingScreenOpened, + mockCallbacks = mock(EnableWifiCallbacks::class.java), + expectedSuccessCallbacks = 1 + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = EnableWifiResult.Success.Enabled, + mockCallbacks = null + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = EnableWifiResult.Success.WifiSettingScreenOpened, + mockCallbacks = null + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = EnableWifiResult.Failure.UnableToEnable, + mockCallbacks = mock(EnableWifiCallbacks::class.java), + expectedFailureCallbacks = 1 + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = EnableWifiResult.Failure.Assertion("Test"), + mockCallbacks = mock(EnableWifiCallbacks::class.java), + expectedFailureCallbacks = 1 + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = EnableWifiResult.Failure.UnableToEnable, + mockCallbacks = null + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(mock(Context::class.java)), + result = EnableWifiResult.Failure.Assertion("Test"), + mockCallbacks = null + ) + ) + } + + data class EnableWifiParams( + val request: EnableWifiRequest, + val result: EnableWifiResult, + val mockCallbacks: EnableWifiCallbacks?, + val expectedFailureCallbacks: Int = 0, + val expectedSuccessCallbacks: Int = 0 + ) + } +} diff --git a/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateEnableWifiSyncTest.kt b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateEnableWifiSyncTest.kt new file mode 100644 index 00000000..0e66ddf0 --- /dev/null +++ b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateEnableWifiSyncTest.kt @@ -0,0 +1,136 @@ +/* + * Copyright 2023 Patches Barrett + * + * 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.android.wisefy.wifi + +import android.content.Context +import android.net.wifi.WifiManager +import com.isupatches.android.wisefy.core.assertions.WisefyAssertions +import com.isupatches.android.wisefy.core.logging.DefaultWisefyLogger +import com.isupatches.android.wisefy.core.util.SdkUtilImpl +import com.isupatches.android.wisefy.testsupport.TestCoroutineDispatchProvider +import com.isupatches.android.wisefy.wifi.entities.EnableWifiRequest +import com.isupatches.android.wisefy.wifi.entities.EnableWifiResult +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.test.TestScope +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import org.mockito.BDDMockito.given +import org.mockito.Mock +import org.mockito.Mockito +import org.mockito.Mockito.times +import org.mockito.Mockito.verify +import org.mockito.MockitoAnnotations + +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(Parameterized::class) +internal class WisefyWifiDelegateEnableWifiSyncTest( + private val params: EnableWifiParams +) { + + @Mock + private lateinit var mockWifiManager: WifiManager + + @Mock + private lateinit var mockAdapter: WifiApi + + private lateinit var delegate: WisefyWifiDelegate + + private var closable: AutoCloseable? = null + + @Before + fun setUp() { + closable = MockitoAnnotations.openMocks(this) + delegate = WisefyWifiDelegate( + wifiManager = mockWifiManager, + logger = DefaultWisefyLogger(), + assertions = WisefyAssertions(false), + sdkUtil = SdkUtilImpl(), + coroutineDispatcherProvider = TestCoroutineDispatchProvider(), + scope = TestScope(), + wifiMutex = Mutex(), + adapter = mockAdapter + ) + } + + @After + fun tearDown() { + closable?.close() + } + + @Test + fun enableWifi() { + // Given + given(mockAdapter.enableWifi(params.request)).willReturn(params.result) + + // Then + val result = delegate.enableWifi(params.request) + + // When + verify(mockAdapter, times(1)).enableWifi(params.request) + assertEquals(params.result, result) + } + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{index}: {0}") + fun params(): List { + return listOf( + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Success.Enabled + ), + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Success.WifiSettingScreenOpened + ), + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Failure.UnableToEnable + ), + EnableWifiParams( + request = EnableWifiRequest.Default, + result = EnableWifiResult.Failure.Assertion("Test") + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(Mockito.mock(Context::class.java)), + result = EnableWifiResult.Success.Enabled + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(Mockito.mock(Context::class.java)), + result = EnableWifiResult.Success.WifiSettingScreenOpened + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(Mockito.mock(Context::class.java)), + result = EnableWifiResult.Failure.UnableToEnable + ), + EnableWifiParams( + request = EnableWifiRequest.Android29OrAbove(Mockito.mock(Context::class.java)), + result = EnableWifiResult.Failure.Assertion("Test") + ) + ) + } + + data class EnableWifiParams( + val request: EnableWifiRequest, + val result: EnableWifiResult + ) + } +} diff --git a/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateIsWifiEnabledAsyncTest.kt b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateIsWifiEnabledAsyncTest.kt new file mode 100644 index 00000000..8c8f7887 --- /dev/null +++ b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateIsWifiEnabledAsyncTest.kt @@ -0,0 +1,156 @@ +/* + * Copyright 2023 Patches Barrett + * + * 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.android.wisefy.wifi + +import android.net.wifi.WifiManager +import com.isupatches.android.wisefy.core.assertions.WisefyAssertions +import com.isupatches.android.wisefy.core.logging.DefaultWisefyLogger +import com.isupatches.android.wisefy.core.util.SdkUtilImpl +import com.isupatches.android.wisefy.testsupport.TestCoroutineDispatchProvider +import com.isupatches.android.wisefy.wifi.callbacks.IsWifiEnabledCallbacks +import com.isupatches.android.wisefy.wifi.entities.IsWifiEnabledQuery +import com.isupatches.android.wisefy.wifi.entities.IsWifiEnabledResult +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.advanceUntilIdle +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.test.setMain +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import org.mockito.BDDMockito.given +import org.mockito.Mock +import org.mockito.Mockito.mock +import org.mockito.Mockito.never +import org.mockito.Mockito.times +import org.mockito.Mockito.verify +import org.mockito.MockitoAnnotations + +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(Parameterized::class) +internal class WisefyWifiDelegateIsWifiEnabledAsyncTest( + private val params: IsWifiEnabledParams +) { + + @Mock + private lateinit var mockWifiManager: WifiManager + + @Mock + private lateinit var mockAdapter: WifiApi + + private lateinit var delegate: WisefyWifiDelegate + + private var closable: AutoCloseable? = null + + private lateinit var testScope: TestScope + + @Before + fun setUp() { + Dispatchers.setMain(StandardTestDispatcher()) + closable = MockitoAnnotations.openMocks(this) + testScope = TestScope() + delegate = WisefyWifiDelegate( + wifiManager = mockWifiManager, + logger = DefaultWisefyLogger(), + assertions = WisefyAssertions(false), + sdkUtil = SdkUtilImpl(), + coroutineDispatcherProvider = TestCoroutineDispatchProvider(), + scope = testScope, + wifiMutex = Mutex(), + adapter = mockAdapter + ) + } + + @After + fun tearDown() { + Dispatchers.resetMain() + closable?.close() + } + + @Test + fun isWifiEnabled() = runTest { + // Given + given(mockAdapter.isWifiEnabled(params.query)).willReturn(params.result) + + // Then + delegate.isWifiEnabled(params.query, params.mockCallbacks) + advanceUntilIdle() + + // When + verify(mockAdapter, times(1)).isWifiEnabled(params.query) + when { + params.mockCallbacks != null && params.expectedWifiDisabledCallbacks > 0 -> { + verify(params.mockCallbacks, times(params.expectedWifiDisabledCallbacks)).onWifiIsDisabled() + } + params.mockCallbacks != null -> { + verify(params.mockCallbacks, never()).onWifiIsDisabled() + } + } + when { + params.mockCallbacks != null && params.expectedWifiEnabledCallbacks > 0 -> { + verify(params.mockCallbacks, times(params.expectedWifiEnabledCallbacks)).onWifiIsEnabled() + } + params.mockCallbacks != null -> { + verify(params.mockCallbacks, never()).onWifiIsEnabled() + } + } + } + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{index}: {0}") + fun params(): List { + return listOf( + IsWifiEnabledParams( + query = IsWifiEnabledQuery(), + result = IsWifiEnabledResult.True, + mockCallbacks = mock(IsWifiEnabledCallbacks::class.java), + expectedWifiEnabledCallbacks = 1 + ), + IsWifiEnabledParams( + query = IsWifiEnabledQuery(), + result = IsWifiEnabledResult.False, + mockCallbacks = mock(IsWifiEnabledCallbacks::class.java), + expectedWifiDisabledCallbacks = 1 + ), + IsWifiEnabledParams( + query = IsWifiEnabledQuery(), + result = IsWifiEnabledResult.True, + mockCallbacks = null + ), + IsWifiEnabledParams( + query = IsWifiEnabledQuery(), + result = IsWifiEnabledResult.False, + mockCallbacks = null + ) + ) + } + + data class IsWifiEnabledParams( + val query: IsWifiEnabledQuery, + val result: IsWifiEnabledResult, + val mockCallbacks: IsWifiEnabledCallbacks?, + val expectedWifiEnabledCallbacks: Int = 0, + val expectedWifiDisabledCallbacks: Int = 0 + ) + } +} diff --git a/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateIsWifiEnabledSyncTest.kt b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateIsWifiEnabledSyncTest.kt new file mode 100644 index 00000000..86a1f971 --- /dev/null +++ b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/WisefyWifiDelegateIsWifiEnabledSyncTest.kt @@ -0,0 +1,110 @@ +/* + * Copyright 2023 Patches Barrett + * + * 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.android.wisefy.wifi + +import android.net.wifi.WifiManager +import com.isupatches.android.wisefy.core.assertions.WisefyAssertions +import com.isupatches.android.wisefy.core.logging.DefaultWisefyLogger +import com.isupatches.android.wisefy.core.util.SdkUtilImpl +import com.isupatches.android.wisefy.testsupport.TestCoroutineDispatchProvider +import com.isupatches.android.wisefy.wifi.entities.IsWifiEnabledQuery +import com.isupatches.android.wisefy.wifi.entities.IsWifiEnabledResult +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.test.TestScope +import org.junit.After +import org.junit.Assert.assertEquals +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.junit.runners.Parameterized +import org.mockito.BDDMockito.given +import org.mockito.Mock +import org.mockito.Mockito.times +import org.mockito.Mockito.verify +import org.mockito.MockitoAnnotations + +@OptIn(ExperimentalCoroutinesApi::class) +@RunWith(Parameterized::class) +internal class WisefyWifiDelegateIsWifiEnabledSyncTest( + private val params: IsWifiEnabledParams +) { + + @Mock + private lateinit var mockWifiManager: WifiManager + + @Mock + private lateinit var mockAdapter: WifiApi + + private lateinit var delegate: WisefyWifiDelegate + + private var closable: AutoCloseable? = null + + @Before + fun setUp() { + closable = MockitoAnnotations.openMocks(this) + delegate = WisefyWifiDelegate( + wifiManager = mockWifiManager, + logger = DefaultWisefyLogger(), + assertions = WisefyAssertions(false), + sdkUtil = SdkUtilImpl(), + coroutineDispatcherProvider = TestCoroutineDispatchProvider(), + scope = TestScope(), + wifiMutex = Mutex(), + adapter = mockAdapter + ) + } + + @After + fun tearDown() { + closable?.close() + } + + @Test + fun isWifiEnabled() { + // Given + given(mockAdapter.isWifiEnabled(params.query)).willReturn(params.result) + + // Then + val result = delegate.isWifiEnabled(params.query) + + // When + verify(mockAdapter, times(1)).isWifiEnabled(params.query) + assertEquals(params.result, result) + } + + companion object { + @JvmStatic + @Parameterized.Parameters(name = "{index}: {0}") + fun params(): List { + return listOf( + IsWifiEnabledParams( + query = IsWifiEnabledQuery(), + result = IsWifiEnabledResult.True + ), + IsWifiEnabledParams( + query = IsWifiEnabledQuery(), + result = IsWifiEnabledResult.False + ) + ) + } + + data class IsWifiEnabledParams( + val query: IsWifiEnabledQuery, + val result: IsWifiEnabledResult + ) + } +} diff --git a/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/os/adapters/Android29WifiAdapterTest.kt b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/os/adapters/Android29WifiAdapterTest.kt new file mode 100644 index 00000000..1509e1f5 --- /dev/null +++ b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/os/adapters/Android29WifiAdapterTest.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2023 Patches Barrett + * + * 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.android.wisefy.wifi.os.adapters + +internal class Android29WifiAdapterTest diff --git a/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/os/adapters/DefaultWifiAdapterTest.kt b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/os/adapters/DefaultWifiAdapterTest.kt new file mode 100644 index 00000000..819dc969 --- /dev/null +++ b/wisefy/wifi/src/test/java/com/isupatches/android/wisefy/wifi/os/adapters/DefaultWifiAdapterTest.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2023 Patches Barrett + * + * 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.android.wisefy.wifi.os.adapters + +internal class DefaultWifiAdapterTest