[fenix] For https://github.com/mozilla-mobile/fenix/issues/14680 - Revert ObserverRegistry changes to WifiConnectionMonitor
parent
9b9f049e26
commit
c3ba6ed946
@ -1,89 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.wifi
|
||||
|
||||
import io.mockk.Called
|
||||
import io.mockk.Runs
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import mozilla.components.feature.sitepermissions.SitePermissionsRules.Action
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.mozilla.fenix.settings.PhoneFeature.AUTOPLAY_AUDIBLE
|
||||
import org.mozilla.fenix.settings.PhoneFeature.AUTOPLAY_INAUDIBLE
|
||||
import org.mozilla.fenix.settings.sitepermissions.AUTOPLAY_ALLOW_ALL
|
||||
import org.mozilla.fenix.settings.sitepermissions.AUTOPLAY_ALLOW_ON_WIFI
|
||||
import org.mozilla.fenix.settings.sitepermissions.AUTOPLAY_BLOCK_ALL
|
||||
import org.mozilla.fenix.utils.Settings
|
||||
|
||||
class SitePermissionsWifiIntegrationTest {
|
||||
|
||||
private lateinit var settings: Settings
|
||||
private lateinit var wifiConnectionMonitor: WifiConnectionMonitor
|
||||
private lateinit var wifiIntegration: SitePermissionsWifiIntegration
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
settings = mockk()
|
||||
wifiConnectionMonitor = mockk(relaxed = true)
|
||||
wifiIntegration = SitePermissionsWifiIntegration(settings, wifiConnectionMonitor)
|
||||
|
||||
every { settings.setSitePermissionsPhoneFeatureAction(any(), any()) } just Runs
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `add and remove wifi connected listener`() {
|
||||
wifiIntegration.addWifiConnectedListener()
|
||||
verify { wifiConnectionMonitor.register(any()) }
|
||||
|
||||
wifiIntegration.removeWifiConnectedListener()
|
||||
verify { wifiConnectionMonitor.unregister(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `start and stop wifi connection monitor`() {
|
||||
wifiIntegration.start()
|
||||
verify { wifiConnectionMonitor.start() }
|
||||
|
||||
wifiIntegration.stop()
|
||||
verify { wifiConnectionMonitor.stop() }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `add only if autoplay is only allowed on wifi`() {
|
||||
every { settings.getAutoplayUserSetting(default = AUTOPLAY_BLOCK_ALL) } returns AUTOPLAY_ALLOW_ALL
|
||||
wifiIntegration.maybeAddWifiConnectedListener()
|
||||
verify { wifiConnectionMonitor wasNot Called }
|
||||
|
||||
every { settings.getAutoplayUserSetting(default = AUTOPLAY_BLOCK_ALL) } returns AUTOPLAY_ALLOW_ON_WIFI
|
||||
wifiIntegration.maybeAddWifiConnectedListener()
|
||||
verify { wifiConnectionMonitor.register(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `listener removes itself if autoplay is not only allowed on wifi`() {
|
||||
every { settings.getAutoplayUserSetting(default = AUTOPLAY_BLOCK_ALL) } returns AUTOPLAY_ALLOW_ALL
|
||||
wifiIntegration.onWifiConnectionChanged(connected = true)
|
||||
verify { wifiConnectionMonitor.unregister(any()) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `listener sets audible and inaudible settings to allowed on connect`() {
|
||||
every { settings.getAutoplayUserSetting(default = AUTOPLAY_BLOCK_ALL) } returns AUTOPLAY_ALLOW_ON_WIFI
|
||||
wifiIntegration.onWifiConnectionChanged(connected = true)
|
||||
verify { settings.setSitePermissionsPhoneFeatureAction(AUTOPLAY_AUDIBLE, Action.ALLOWED) }
|
||||
verify { settings.setSitePermissionsPhoneFeatureAction(AUTOPLAY_INAUDIBLE, Action.ALLOWED) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `listener sets audible and inaudible settings to blocked on disconnected`() {
|
||||
every { settings.getAutoplayUserSetting(default = AUTOPLAY_BLOCK_ALL) } returns AUTOPLAY_ALLOW_ON_WIFI
|
||||
wifiIntegration.onWifiConnectionChanged(connected = false)
|
||||
verify { settings.setSitePermissionsPhoneFeatureAction(AUTOPLAY_AUDIBLE, Action.BLOCKED) }
|
||||
verify { settings.setSitePermissionsPhoneFeatureAction(AUTOPLAY_INAUDIBLE, Action.BLOCKED) }
|
||||
}
|
||||
}
|
@ -1,91 +0,0 @@
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
package org.mozilla.fenix.wifi
|
||||
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkRequest
|
||||
import io.mockk.Runs
|
||||
import io.mockk.every
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.mockkConstructor
|
||||
import io.mockk.slot
|
||||
import io.mockk.unmockkConstructor
|
||||
import io.mockk.verify
|
||||
import org.junit.After
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
class WifiConnectionMonitorTest {
|
||||
|
||||
private lateinit var connectivityManager: ConnectivityManager
|
||||
private lateinit var wifiConnectionMonitor: WifiConnectionMonitor
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
mockkConstructor(NetworkRequest.Builder::class)
|
||||
connectivityManager = mockk(relaxUnitFun = true)
|
||||
wifiConnectionMonitor = WifiConnectionMonitor(connectivityManager)
|
||||
|
||||
every {
|
||||
anyConstructed<NetworkRequest.Builder>().addTransportType(any())
|
||||
} answers { self as NetworkRequest.Builder }
|
||||
}
|
||||
|
||||
@After
|
||||
fun teardown() {
|
||||
unmockkConstructor(NetworkRequest.Builder::class)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `start runs only once`() {
|
||||
wifiConnectionMonitor.start()
|
||||
wifiConnectionMonitor.start()
|
||||
|
||||
verify(exactly = 1) {
|
||||
connectivityManager.registerNetworkCallback(any(), any<ConnectivityManager.NetworkCallback>())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `stop only runs after start`() {
|
||||
wifiConnectionMonitor.stop()
|
||||
verify(exactly = 0) {
|
||||
connectivityManager.unregisterNetworkCallback(any<ConnectivityManager.NetworkCallback>())
|
||||
}
|
||||
|
||||
wifiConnectionMonitor.start()
|
||||
wifiConnectionMonitor.stop()
|
||||
verify {
|
||||
connectivityManager.unregisterNetworkCallback(any<ConnectivityManager.NetworkCallback>())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `passes results from connectivity manager to observers`() {
|
||||
val slot = slot<ConnectivityManager.NetworkCallback>()
|
||||
every { connectivityManager.registerNetworkCallback(any(), capture(slot)) } just Runs
|
||||
|
||||
wifiConnectionMonitor.start()
|
||||
|
||||
// Immediately notifies observer when registered
|
||||
val observer = mockk<WifiConnectionMonitor.Observer>(relaxed = true)
|
||||
wifiConnectionMonitor.register(observer)
|
||||
verify { observer.onWifiConnectionChanged(connected = false) }
|
||||
|
||||
// Notifies observer when network is available or lost
|
||||
slot.captured.onAvailable(mockk())
|
||||
verify { observer.onWifiConnectionChanged(connected = true) }
|
||||
|
||||
slot.captured.onLost(mockk())
|
||||
verify { observer.onWifiConnectionChanged(connected = false) }
|
||||
}
|
||||
|
||||
private fun captureNetworkCallback(): ConnectivityManager.NetworkCallback {
|
||||
val slot = slot<ConnectivityManager.NetworkCallback>()
|
||||
verify { connectivityManager.registerNetworkCallback(any(), capture(slot)) }
|
||||
return slot.captured
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue