From 177854865dc1f5886f14cfde84dd059c5c0ad8b8 Mon Sep 17 00:00:00 2001 From: Aitor Viana Date: Tue, 15 Oct 2024 12:51:14 +0100 Subject: [PATCH 1/3] Fix Disconnect/re-connect from notification when both VPN and AppTP are enabled --- .../mobile/android/vpn/service/VpnActionReceiver.kt | 9 +++++++++ .../AppTPAndNetPEnabledNotificationContentPlugin.kt | 2 ++ .../AppTpEnabledNotificationContentPlugin.kt | 1 + 3 files changed, 12 insertions(+) diff --git a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/VpnActionReceiver.kt b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/VpnActionReceiver.kt index 656ad37455be..2035550e26b7 100644 --- a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/VpnActionReceiver.kt +++ b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/VpnActionReceiver.kt @@ -25,8 +25,10 @@ import android.os.SystemClock import com.duckduckgo.anvil.annotations.InjectWith import com.duckduckgo.common.utils.DispatcherProvider import com.duckduckgo.di.scopes.ReceiverScope +import com.duckduckgo.mobile.android.app.tracking.AppTrackingProtection import com.duckduckgo.mobile.android.vpn.Vpn import com.duckduckgo.mobile.android.vpn.pixels.DeviceShieldPixels +import com.duckduckgo.networkprotection.api.NetworkProtectionState import dagger.android.AndroidInjection import java.util.concurrent.TimeUnit import javax.inject.Inject @@ -38,6 +40,10 @@ import logcat.logcat class VpnActionReceiver : BroadcastReceiver() { @Inject lateinit var vpn: Vpn + @Inject lateinit var networkProtectionState: NetworkProtectionState + + @Inject lateinit var appTrackingProtection: AppTrackingProtection + @Inject lateinit var dispatcherProvider: DispatcherProvider @Inject lateinit var context: Context @@ -65,7 +71,10 @@ class VpnActionReceiver : BroadcastReceiver() { ACTION_VPN_DISABLE -> { logcat { "Entire VPN will disabled because the user asked it" } goAsync(pendingResult) { + // instead of stopping the VPN, we stop the VPN features individually vpn.stop() + appTrackingProtection.stop() + networkProtectionState.stop() } } diff --git a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTPAndNetPEnabledNotificationContentPlugin.kt b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTPAndNetPEnabledNotificationContentPlugin.kt index 1b86a102aec6..a1dadd88d421 100644 --- a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTPAndNetPEnabledNotificationContentPlugin.kt +++ b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTPAndNetPEnabledNotificationContentPlugin.kt @@ -33,6 +33,7 @@ import com.squareup.anvil.annotations.ContributesMultibinding import dagger.SingleInstanceIn import javax.inject.Inject import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.map import kotlinx.coroutines.runBlocking @@ -77,6 +78,7 @@ class AppTPAndNetPEnabledNotificationContentPlugin @Inject constructor( } return repository.getVpnTrackers({ dateOfLastHour() }) + .filter { isActive() } // make sure we only emit when this plugin is active .map { trackersBlocked -> val trackingApps = trackersBlocked.trackingApps() val location = networkProtectionState.serverLocation() diff --git a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTpEnabledNotificationContentPlugin.kt b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTpEnabledNotificationContentPlugin.kt index c950113120b7..281e5bfcba8d 100644 --- a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTpEnabledNotificationContentPlugin.kt +++ b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTpEnabledNotificationContentPlugin.kt @@ -79,6 +79,7 @@ class AppTpEnabledNotificationContentPlugin @Inject constructor( } return repository.getVpnTrackers({ dateOfLastHour() }) + .filter { isActive() } // make sure we only emit when this plugin is active .map { trackersBlocked -> val trackingApps = trackersBlocked.trackingApps() val isEnabled = appTrackingProtection.isEnabled() From a99ebfe1818f85ffe931dcc261ab8c9ae9085cd2 Mon Sep 17 00:00:00 2001 From: Aitor Viana Date: Mon, 21 Oct 2024 18:34:15 +0100 Subject: [PATCH 2/3] Fix tests --- ...etPEnabledNotificationContentPluginTest.kt | 16 ++++++++ ...pTpEnabledNotificationContentPluginTest.kt | 38 ++++++------------- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTPAndNetPEnabledNotificationContentPluginTest.kt b/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTPAndNetPEnabledNotificationContentPluginTest.kt index f2f4c9d1b171..55b31eab4180 100644 --- a/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTPAndNetPEnabledNotificationContentPluginTest.kt +++ b/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTPAndNetPEnabledNotificationContentPluginTest.kt @@ -137,6 +137,8 @@ class AppTPAndNetPEnabledNotificationContentPluginTest { @Test fun getUpdateContentThenReturnsCorrectInitialUpdatedNotificationContent() = runTest { whenever(networkProtectionState.serverLocation()).thenReturn("Stockholm, SE") + whenever(appTrackingProtection.isEnabled()).thenReturn(true) + whenever(networkProtectionState.isEnabled()).thenReturn(true) plugin.getUpdatedContent().test { val item = awaitItem() @@ -151,6 +153,8 @@ class AppTPAndNetPEnabledNotificationContentPluginTest { @Test fun getUpdateContentOneCompanyThenReturnsCorrectUpdatedNotificationContent() = runTest { whenever(networkProtectionState.serverLocation()).thenReturn("Stockholm, SE") + whenever(appTrackingProtection.isEnabled()).thenReturn(true) + whenever(networkProtectionState.isEnabled()).thenReturn(true) plugin.getUpdatedContent().test { val trackers = listOf(aTrackerAndCompany()) @@ -169,6 +173,8 @@ class AppTPAndNetPEnabledNotificationContentPluginTest { @Test fun getUpdateContentMultipleDifferentAppsThenReturnsCorrectUpdatedNotificationContent() = runTest { whenever(networkProtectionState.serverLocation()).thenReturn("Stockholm, SE") + whenever(appTrackingProtection.isEnabled()).thenReturn(true) + whenever(networkProtectionState.isEnabled()).thenReturn(true) plugin.getUpdatedContent().test { val trackers = listOf( @@ -193,6 +199,8 @@ class AppTPAndNetPEnabledNotificationContentPluginTest { @Test fun getUpdateContentMultipleDifferentAppsNoLocationThenReturnsCorrectUpdatedNotificationContent() = runTest { whenever(networkProtectionState.serverLocation()).thenReturn(null) + whenever(appTrackingProtection.isEnabled()).thenReturn(true) + whenever(networkProtectionState.isEnabled()).thenReturn(true) plugin.getUpdatedContent().test { val trackers = listOf( @@ -217,6 +225,8 @@ class AppTPAndNetPEnabledNotificationContentPluginTest { @Test fun getUpdateContentTrackersWithoutEntityThenReturnsCorrectUpdatedNotificationContent() = runTest { whenever(networkProtectionState.serverLocation()).thenReturn("Stockholm, SE") + whenever(appTrackingProtection.isEnabled()).thenReturn(true) + whenever(networkProtectionState.isEnabled()).thenReturn(true) plugin.getUpdatedContent().test { appTrackerBlockingStatsRepository.insert(listOf(aTrackerAndCompany(), aTrackerAndCompany())) @@ -232,6 +242,8 @@ class AppTPAndNetPEnabledNotificationContentPluginTest { @Test fun getUpdateContentTrackersWithoutEntityNoLocationThenReturnsCorrectUpdatedNotificationContent() = runTest { whenever(networkProtectionState.serverLocation()).thenReturn(null) + whenever(appTrackingProtection.isEnabled()).thenReturn(true) + whenever(networkProtectionState.isEnabled()).thenReturn(true) plugin.getUpdatedContent().test { appTrackerBlockingStatsRepository.insert(listOf(aTrackerAndCompany(), aTrackerAndCompany())) @@ -247,6 +259,8 @@ class AppTPAndNetPEnabledNotificationContentPluginTest { @Test fun getUpdateContentMultipleSameThenReturnsCorrectUpdatedNotificationContent() = runTest { whenever(networkProtectionState.serverLocation()).thenReturn("Stockholm, SE") + whenever(appTrackingProtection.isEnabled()).thenReturn(true) + whenever(networkProtectionState.isEnabled()).thenReturn(true) plugin.getUpdatedContent().test { appTrackerBlockingStatsRepository.insert(listOf(aTrackerAndCompany(), aTrackerAndCompany())) @@ -265,6 +279,8 @@ class AppTPAndNetPEnabledNotificationContentPluginTest { @Test fun getUpdateContentMultipleSameNoLocationThenReturnsCorrectUpdatedNotificationContent() = runTest { whenever(networkProtectionState.serverLocation()).thenReturn(null) + whenever(appTrackingProtection.isEnabled()).thenReturn(true) + whenever(networkProtectionState.isEnabled()).thenReturn(true) plugin.getUpdatedContent().test { appTrackerBlockingStatsRepository.insert(listOf(aTrackerAndCompany(), aTrackerAndCompany())) diff --git a/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTpEnabledNotificationContentPluginTest.kt b/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTpEnabledNotificationContentPluginTest.kt index 96ca650cdc9a..9212d08beb44 100644 --- a/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTpEnabledNotificationContentPluginTest.kt +++ b/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/service/notification/AppTpEnabledNotificationContentPluginTest.kt @@ -130,15 +130,12 @@ class AppTpEnabledNotificationContentPluginTest { } @Test - fun getUpdateContentAppTpNotEnabledThenReturnsCorrectInitialUpdatedNotificationContent() = runTest { + fun getUpdateContentAppTpNotEnabledThenReturnsNoContent() = runTest { whenever(appTrackingProtection.isEnabled()).thenReturn(false) + whenever(networkProtectionState.isEnabled()).thenReturn(false) plugin.getUpdatedContent().test { - val item = awaitItem() - - item.assertTextEquals("") - - cancelAndConsumeRemainingEvents() + expectNoEvents() } } @@ -162,20 +159,16 @@ class AppTpEnabledNotificationContentPluginTest { } @Test - fun getUpdateContentOneCompanyAppTpNotEnabledThenReturnsCorrectUpdatedNotificationContent() = runTest { + fun getUpdateContentOneCompanyAppTpNotEnabledThenReturnsNoContent() = runTest { whenever(appTrackingProtection.isEnabled()).thenReturn(false) + whenever(networkProtectionState.isEnabled()).thenReturn(false) plugin.getUpdatedContent().test { val trackers = listOf(aTrackerAndCompany()) appTrackerBlockingStatsRepository.insert(trackers) db.vpnAppTrackerBlockingDao().insertTrackerEntities(trackers.map { it.asEntity() }) - skipItems(1) - val item = awaitItem() - - item.assertTextEquals("") - - cancelAndConsumeRemainingEvents() + expectNoEvents() } } @@ -205,8 +198,9 @@ class AppTpEnabledNotificationContentPluginTest { } @Test - fun getUpdateContentMultipleDifferentAppsAppTpNotEnabledThenReturnsCorrectUpdatedNotificationContent() = runTest { + fun getUpdateContentMultipleDifferentAppsAppTpNotEnabledThenReturnsNoContent() = runTest { whenever(appTrackingProtection.isEnabled()).thenReturn(false) + whenever(networkProtectionState.isEnabled()).thenReturn(false) plugin.getUpdatedContent().test { val trackers = listOf( @@ -220,12 +214,7 @@ class AppTpEnabledNotificationContentPluginTest { appTrackerBlockingStatsRepository.insert(trackers) db.vpnAppTrackerBlockingDao().insertTrackerEntities(trackers.map { it.asEntity() }) - skipItems(1) - val item = awaitItem() - - item.assertTextEquals("") - - cancelAndConsumeRemainingEvents() + expectNoEvents() } } @@ -265,16 +254,13 @@ class AppTpEnabledNotificationContentPluginTest { } @Test - fun getUpdateContentMultipleSameAppTpNotEnabledThenReturnsCorrectUpdatedNotificationContent() = runTest { + fun getUpdateContentMultipleSameAppTpNotEnabledThenReturnsNoContent() = runTest { whenever(appTrackingProtection.isEnabled()).thenReturn(false) + whenever(networkProtectionState.isEnabled()).thenReturn(false) plugin.getUpdatedContent().test { appTrackerBlockingStatsRepository.insert(listOf(aTrackerAndCompany(), aTrackerAndCompany())) - val item = expectMostRecentItem() - - item.assertTextEquals("") - - cancelAndConsumeRemainingEvents() + expectNoEvents() } } From 388f64a40aa475d259aa45cda1312be20a1c59ef Mon Sep 17 00:00:00 2001 From: Aitor Viana Date: Tue, 22 Oct 2024 13:41:43 +0100 Subject: [PATCH 3/3] Fix notifications --- .../com/duckduckgo/mobile/android/vpn/Vpn.kt | 10 ++- .../android/vpn/VpnFeaturesRegistryImpl.kt | 66 ++++++++++++++----- .../mobile/android/vpn/di/VpnAppModule.kt | 24 ------- .../android/vpn/service/VpnActionReceiver.kt | 3 - .../vpn/VpnFeaturesRegistryImplTest.kt | 7 +- .../impl/snooze/VpnCallStateReceiver.kt | 2 +- .../impl/WgVpnNetworkStackTest.kt | 4 +- 7 files changed, 63 insertions(+), 53 deletions(-) diff --git a/app-tracking-protection/vpn-api/src/main/java/com/duckduckgo/mobile/android/vpn/Vpn.kt b/app-tracking-protection/vpn-api/src/main/java/com/duckduckgo/mobile/android/vpn/Vpn.kt index 0342a34ae555..912c8cc1448e 100644 --- a/app-tracking-protection/vpn-api/src/main/java/com/duckduckgo/mobile/android/vpn/Vpn.kt +++ b/app-tracking-protection/vpn-api/src/main/java/com/duckduckgo/mobile/android/vpn/Vpn.kt @@ -26,7 +26,15 @@ interface Vpn { suspend fun start() /** - * Disable the device VPN by stopping the VPN service + * Pauses the VPN tunnel. + * All features that were registered to use the VPN tunnel (eg. AppTP, NetP) continue to be registered and so a subsequent + * [start] call will re-enable them all + */ + suspend fun pause() + + /** + * Stops the VPN tunnel AND all features registered to use the VPN tunnel (eg. AppTP, NetP). A subsequent call to [start] + * will not re-start the VPN because no feature would be registered. */ suspend fun stop() diff --git a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/VpnFeaturesRegistryImpl.kt b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/VpnFeaturesRegistryImpl.kt index 840e3e1a2c4b..0cb49910f72b 100644 --- a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/VpnFeaturesRegistryImpl.kt +++ b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/VpnFeaturesRegistryImpl.kt @@ -21,8 +21,12 @@ import android.content.SharedPreferences import androidx.core.content.edit import com.duckduckgo.common.utils.DispatcherProvider import com.duckduckgo.data.store.api.SharedPreferencesProvider +import com.duckduckgo.di.scopes.AppScope import com.duckduckgo.mobile.android.vpn.service.TrackerBlockingVpnService +import com.squareup.anvil.annotations.ContributesBinding +import dagger.SingleInstanceIn import java.util.UUID +import javax.inject.Inject import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.withContext import logcat.logcat @@ -30,11 +34,20 @@ import logcat.logcat private const val PREFS_FILENAME = "com.duckduckgo.mobile.android.vpn.feature.registry.v1" private const val IS_INITIALIZED = "IS_INITIALIZED" -internal class VpnFeaturesRegistryImpl( +@ContributesBinding( + scope = AppScope::class, + boundType = VpnFeaturesRegistry::class, +) +@ContributesBinding( + scope = AppScope::class, + boundType = Vpn::class, +) +@SingleInstanceIn(AppScope::class) +class VpnFeaturesRegistryImpl @Inject constructor( private val vpnServiceWrapper: VpnServiceWrapper, private val sharedPreferencesProvider: SharedPreferencesProvider, private val dispatcherProvider: DispatcherProvider, -) : VpnFeaturesRegistry { +) : VpnFeaturesRegistry, Vpn { private val mutex = Mutex() @@ -103,6 +116,34 @@ internal class VpnFeaturesRegistryImpl( private fun registeredFeatures(): Map { return preferences.all.filter { it.key != IS_INITIALIZED } } + + override suspend fun start() = withContext(dispatcherProvider.io()) { + vpnServiceWrapper.startService() + } + + override suspend fun pause() { + vpnServiceWrapper.stopService() + } + + override suspend fun stop() { + try { + mutex.lock() + // unregister all features + getRegisteredFeatures().onEach { + preferences.edit(commit = true) { + remove(it.featureName) + } + } + // stop VPN + vpnServiceWrapper.stopService() + } finally { + mutex.unlock() + } + } + + override suspend fun snooze(triggerAtMillis: Long) { + vpnServiceWrapper.snoozeService(triggerAtMillis) + } } /** @@ -111,10 +152,9 @@ internal class VpnFeaturesRegistryImpl( * * The class is marked as open to be able to mock it in tests. */ -internal open class VpnServiceWrapper( +open class VpnServiceWrapper @Inject constructor( private val context: Context, - private val dispatcherProvider: DispatcherProvider, -) : Vpn { +) { open fun restartVpnService(forceRestart: Boolean) { TrackerBlockingVpnService.restartVpnService(context, forceRestart = forceRestart) } @@ -127,19 +167,11 @@ internal open class VpnServiceWrapper( TrackerBlockingVpnService.startService(context) } - open fun isServiceRunning(): Boolean { - return TrackerBlockingVpnService.isServiceRunning(context) - } - - override suspend fun start() = withContext(dispatcherProvider.io()) { - startService() - } - - override suspend fun stop() = withContext(dispatcherProvider.io()) { - stopService() + open fun snoozeService(triggerAtMillis: Long) { + TrackerBlockingVpnService.snoozeService(context, triggerAtMillis) } - override suspend fun snooze(triggerAtMillis: Long) { - TrackerBlockingVpnService.snoozeService(context, triggerAtMillis) + open fun isServiceRunning(): Boolean { + return TrackerBlockingVpnService.isServiceRunning(context) } } diff --git a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/di/VpnAppModule.kt b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/di/VpnAppModule.kt index 3241b8383823..1d07e4a49073 100644 --- a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/di/VpnAppModule.kt +++ b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/di/VpnAppModule.kt @@ -21,12 +21,7 @@ import android.content.res.Resources import android.net.ConnectivityManager import androidx.room.Room import com.duckduckgo.common.utils.DispatcherProvider -import com.duckduckgo.data.store.api.SharedPreferencesProvider import com.duckduckgo.di.scopes.AppScope -import com.duckduckgo.mobile.android.vpn.Vpn -import com.duckduckgo.mobile.android.vpn.VpnFeaturesRegistry -import com.duckduckgo.mobile.android.vpn.VpnFeaturesRegistryImpl -import com.duckduckgo.mobile.android.vpn.VpnServiceWrapper import com.duckduckgo.mobile.android.vpn.stats.AppTrackerBlockingStatsRepository import com.duckduckgo.mobile.android.vpn.stats.RealAppTrackerBlockingStatsRepository import com.duckduckgo.mobile.android.vpn.store.* @@ -88,25 +83,6 @@ object VpnAppModule { return context.resources } - @Provides - @SingleInstanceIn(AppScope::class) - fun provideVpnFeaturesRegistry( - context: Context, - sharedPreferencesProvider: SharedPreferencesProvider, - dispatcherProvider: DispatcherProvider, - ): VpnFeaturesRegistry { - return VpnFeaturesRegistryImpl(VpnServiceWrapper(context, dispatcherProvider), sharedPreferencesProvider, dispatcherProvider) - } - - @Provides - @SingleInstanceIn(AppScope::class) - fun provideVpnServiceWrapper( - context: Context, - dispatcherProvider: DispatcherProvider, - ): Vpn { - return VpnServiceWrapper(context, dispatcherProvider) - } - @Provides fun provideAppTrackerBlockingStatsRepository( vpnDatabase: VpnDatabase, diff --git a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/VpnActionReceiver.kt b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/VpnActionReceiver.kt index 2035550e26b7..28217114f859 100644 --- a/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/VpnActionReceiver.kt +++ b/app-tracking-protection/vpn-impl/src/main/java/com/duckduckgo/mobile/android/vpn/service/VpnActionReceiver.kt @@ -71,10 +71,7 @@ class VpnActionReceiver : BroadcastReceiver() { ACTION_VPN_DISABLE -> { logcat { "Entire VPN will disabled because the user asked it" } goAsync(pendingResult) { - // instead of stopping the VPN, we stop the VPN features individually vpn.stop() - appTrackingProtection.stop() - networkProtectionState.stop() } } diff --git a/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/VpnFeaturesRegistryImplTest.kt b/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/VpnFeaturesRegistryImplTest.kt index fc96c15f70d1..b9942041ca64 100644 --- a/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/VpnFeaturesRegistryImplTest.kt +++ b/app-tracking-protection/vpn-impl/src/test/java/com/duckduckgo/mobile/android/vpn/VpnFeaturesRegistryImplTest.kt @@ -20,7 +20,6 @@ import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import com.duckduckgo.common.test.CoroutineTestRule import com.duckduckgo.common.test.api.InMemorySharedPreferences -import com.duckduckgo.common.utils.DispatcherProvider import com.duckduckgo.data.store.api.SharedPreferencesProvider import kotlinx.coroutines.test.runTest import org.junit.Assert.* @@ -44,7 +43,7 @@ class VpnFeaturesRegistryImplTest { @Before fun setup() { val prefs = InMemorySharedPreferences() - vpnServiceWrapper = TestVpnServiceWrapper(coroutineTestRule.testDispatcherProvider) + vpnServiceWrapper = TestVpnServiceWrapper() whenever( sharedPreferencesProvider.getSharedPreferences(eq("com.duckduckgo.mobile.android.vpn.feature.registry.v1"), eq(true), eq(false)), @@ -163,9 +162,7 @@ class VpnFeaturesRegistryImplTest { BAR("BAR"), } - private class TestVpnServiceWrapper constructor( - dispatcher: DispatcherProvider, - ) : VpnServiceWrapper(InstrumentationRegistry.getInstrumentation().context, dispatcher) { + private class TestVpnServiceWrapper constructor() : VpnServiceWrapper(InstrumentationRegistry.getInstrumentation().context) { private var isRunning = false var restartCount = 0 diff --git a/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/snooze/VpnCallStateReceiver.kt b/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/snooze/VpnCallStateReceiver.kt index 59755635ebd4..9e914565bc4a 100644 --- a/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/snooze/VpnCallStateReceiver.kt +++ b/network-protection/network-protection-impl/src/main/java/com/duckduckgo/networkprotection/impl/snooze/VpnCallStateReceiver.kt @@ -71,7 +71,7 @@ class VpnCallStateReceiver @Inject constructor( if (state == TelephonyManager.CALL_STATE_IDLE) { vpn.start() } else { - vpn.stop() + vpn.pause() } } } diff --git a/network-protection/network-protection-impl/src/test/java/com/duckduckgo/networkprotection/impl/WgVpnNetworkStackTest.kt b/network-protection/network-protection-impl/src/test/java/com/duckduckgo/networkprotection/impl/WgVpnNetworkStackTest.kt index 7c6ea7b18132..324c9803056d 100644 --- a/network-protection/network-protection-impl/src/test/java/com/duckduckgo/networkprotection/impl/WgVpnNetworkStackTest.kt +++ b/network-protection/network-protection-impl/src/test/java/com/duckduckgo/networkprotection/impl/WgVpnNetworkStackTest.kt @@ -230,7 +230,7 @@ class WgVpnNetworkStackTest { } @Test - fun whenOnStopVpnWithSelfStopThenResetEnabledTimeInMillisAndServerDetails() = runTest { + fun whenOnStopVpnWithSelfPauseThenResetEnabledTimeInMillisAndServerDetails() = runTest { assertEquals( Result.success(Unit), wgVpnNetworkStack.onStopVpn(SELF_STOP()), @@ -242,7 +242,7 @@ class WgVpnNetworkStackTest { } @Test - fun whenOnStopVpnWithRestartThenResetEnabledTimeInMillisAndServerDetails() = runTest { + fun whenOnPauseVpnWithRestartThenResetEnabledTimeInMillisAndServerDetails() = runTest { assertEquals( Result.success(Unit), wgVpnNetworkStack.onStopVpn(RESTART),