adding unified push registrar tests
This commit is contained in:
parent
1e8d868348
commit
8545d0dab4
|
@ -9,6 +9,7 @@ import app.dapk.st.core.extensions.unsafeLazy
|
||||||
import app.dapk.st.domain.push.PushTokenRegistrarPreferences
|
import app.dapk.st.domain.push.PushTokenRegistrarPreferences
|
||||||
import app.dapk.st.firebase.messaging.Messaging
|
import app.dapk.st.firebase.messaging.Messaging
|
||||||
import app.dapk.st.push.messaging.MessagingPushTokenRegistrar
|
import app.dapk.st.push.messaging.MessagingPushTokenRegistrar
|
||||||
|
import app.dapk.st.push.unifiedpush.UnifiedPush
|
||||||
import app.dapk.st.push.unifiedpush.UnifiedPushRegistrar
|
import app.dapk.st.push.unifiedpush.UnifiedPushRegistrar
|
||||||
|
|
||||||
class PushModule(
|
class PushModule(
|
||||||
|
@ -28,7 +29,7 @@ class PushModule(
|
||||||
pushHandler,
|
pushHandler,
|
||||||
messaging,
|
messaging,
|
||||||
),
|
),
|
||||||
UnifiedPushRegistrar(context),
|
UnifiedPushRegistrar(context, object : UnifiedPush {}),
|
||||||
PushTokenRegistrarPreferences(preferences)
|
PushTokenRegistrarPreferences(preferences)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
package app.dapk.st.push.unifiedpush
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import org.unifiedpush.android.connector.UnifiedPush
|
||||||
|
|
||||||
|
interface UnifiedPush {
|
||||||
|
fun saveDistributor(context: Context, distributor: String) = UnifiedPush.saveDistributor(context, distributor)
|
||||||
|
fun getDistributor(context: Context): String = UnifiedPush.getDistributor(context)
|
||||||
|
fun registerApp(context: Context) = UnifiedPush.registerApp(context)
|
||||||
|
fun unregisterApp(context: Context) = UnifiedPush.unregisterApp(context)
|
||||||
|
}
|
|
@ -7,38 +7,39 @@ import app.dapk.st.core.AppLogTag
|
||||||
import app.dapk.st.core.log
|
import app.dapk.st.core.log
|
||||||
import app.dapk.st.push.PushTokenRegistrar
|
import app.dapk.st.push.PushTokenRegistrar
|
||||||
import app.dapk.st.push.Registrar
|
import app.dapk.st.push.Registrar
|
||||||
import org.unifiedpush.android.connector.UnifiedPush
|
|
||||||
|
|
||||||
class UnifiedPushRegistrar(
|
class UnifiedPushRegistrar(
|
||||||
private val context: Context,
|
private val context: Context,
|
||||||
|
private val unifiedPush: UnifiedPush,
|
||||||
|
private val componentFactory: (Context) -> ComponentName = { ComponentName(it, UnifiedPushMessageReceiver::class.java) }
|
||||||
) : PushTokenRegistrar {
|
) : PushTokenRegistrar {
|
||||||
|
|
||||||
fun registerSelection(registrar: Registrar) {
|
fun registerSelection(registrar: Registrar) {
|
||||||
log(AppLogTag.PUSH, "UnifiedPush - register: $registrar")
|
log(AppLogTag.PUSH, "UnifiedPush - register: $registrar")
|
||||||
UnifiedPush.saveDistributor(context, registrar.id)
|
unifiedPush.saveDistributor(context, registrar.id)
|
||||||
registerApp()
|
registerApp()
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun registerCurrentToken() {
|
override suspend fun registerCurrentToken() {
|
||||||
log(AppLogTag.PUSH, "UnifiedPush - register current token")
|
log(AppLogTag.PUSH, "UnifiedPush - register current token")
|
||||||
if (UnifiedPush.getDistributor(context).isNotEmpty()) {
|
if (unifiedPush.getDistributor(context).isNotEmpty()) {
|
||||||
registerApp()
|
registerApp()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun registerApp() {
|
private fun registerApp() {
|
||||||
context.packageManager.setComponentEnabledSetting(
|
context.packageManager.setComponentEnabledSetting(
|
||||||
ComponentName(context, UnifiedPushMessageReceiver::class.java),
|
componentFactory(context),
|
||||||
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
|
||||||
PackageManager.DONT_KILL_APP,
|
PackageManager.DONT_KILL_APP,
|
||||||
)
|
)
|
||||||
UnifiedPush.registerApp(context)
|
unifiedPush.registerApp(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun unregister() {
|
override fun unregister() {
|
||||||
UnifiedPush.unregisterApp(context)
|
unifiedPush.unregisterApp(context)
|
||||||
context.packageManager.setComponentEnabledSetting(
|
context.packageManager.setComponentEnabledSetting(
|
||||||
ComponentName(context, UnifiedPushMessageReceiver::class.java),
|
componentFactory(context),
|
||||||
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
|
||||||
PackageManager.DONT_KILL_APP,
|
PackageManager.DONT_KILL_APP,
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
package app.dapk.st.push.unifiedpush
|
||||||
|
|
||||||
|
import android.content.ComponentName
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import app.dapk.st.push.Registrar
|
||||||
|
import fake.FakeContext
|
||||||
|
import fake.FakePackageManager
|
||||||
|
import io.mockk.Called
|
||||||
|
import io.mockk.every
|
||||||
|
import io.mockk.mockk
|
||||||
|
import io.mockk.verify
|
||||||
|
import org.junit.Test
|
||||||
|
import test.delegateReturn
|
||||||
|
import test.runExpectTest
|
||||||
|
|
||||||
|
private val A_COMPONENT_NAME = FakeComponentName()
|
||||||
|
private val A_REGISTRAR_SELECTION = Registrar("a-registrar")
|
||||||
|
private const val A_SAVED_DISTRIBUTOR = "a distributor"
|
||||||
|
|
||||||
|
class UnifiedPushRegistrarTest {
|
||||||
|
|
||||||
|
private val fakePackageManager = FakePackageManager()
|
||||||
|
private val fakeContext = FakeContext().also {
|
||||||
|
it.givenPackageManager().returns(fakePackageManager.instance)
|
||||||
|
}
|
||||||
|
private val fakeUnifiedPush = FakeUnifiedPush()
|
||||||
|
private val fakeComponentFactory = { _: Context -> A_COMPONENT_NAME.instance }
|
||||||
|
|
||||||
|
private val registrar = UnifiedPushRegistrar(fakeContext.instance, fakeUnifiedPush, fakeComponentFactory)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when unregistering, then updates unified push and disables component`() = runExpectTest {
|
||||||
|
fakeUnifiedPush.expect { it.unregisterApp(fakeContext.instance) }
|
||||||
|
fakePackageManager.instance.expect {
|
||||||
|
it.setComponentEnabledSetting(A_COMPONENT_NAME.instance, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP)
|
||||||
|
}
|
||||||
|
|
||||||
|
registrar.unregister()
|
||||||
|
|
||||||
|
verifyExpects()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `when registering selection, then updates unified push and enables component`() = runExpectTest {
|
||||||
|
fakeUnifiedPush.expect { it.registerApp(fakeContext.instance) }
|
||||||
|
fakeUnifiedPush.expect { it.saveDistributor(fakeContext.instance, A_REGISTRAR_SELECTION.id) }
|
||||||
|
fakePackageManager.instance.expect {
|
||||||
|
it.setComponentEnabledSetting(A_COMPONENT_NAME.instance, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP)
|
||||||
|
}
|
||||||
|
|
||||||
|
registrar.registerSelection(A_REGISTRAR_SELECTION)
|
||||||
|
|
||||||
|
verifyExpects()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given saved distributor, when registering current token, then updates unified push and enables component`() = runExpectTest {
|
||||||
|
fakeUnifiedPush.givenDistributor(fakeContext.instance).returns(A_SAVED_DISTRIBUTOR)
|
||||||
|
fakeUnifiedPush.expect { it.registerApp(fakeContext.instance) }
|
||||||
|
fakePackageManager.instance.expect {
|
||||||
|
it.setComponentEnabledSetting(A_COMPONENT_NAME.instance, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP)
|
||||||
|
}
|
||||||
|
|
||||||
|
registrar.registerCurrentToken()
|
||||||
|
|
||||||
|
verifyExpects()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `given no distributor, when registering current token, then does nothing`() = runExpectTest {
|
||||||
|
fakeUnifiedPush.givenDistributor(fakeContext.instance).returns("")
|
||||||
|
|
||||||
|
registrar.registerCurrentToken()
|
||||||
|
|
||||||
|
verify(exactly = 0) { fakeUnifiedPush.registerApp(any()) }
|
||||||
|
verify { fakePackageManager.instance wasNot Called }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class FakeUnifiedPush : UnifiedPush by mockk() {
|
||||||
|
fun givenDistributor(context: Context) = every { getDistributor(context) }.delegateReturn()
|
||||||
|
}
|
||||||
|
|
||||||
|
class FakeComponentName {
|
||||||
|
val instance = mockk<ComponentName>()
|
||||||
|
}
|
|
@ -1,8 +1,16 @@
|
||||||
package fake
|
package fake
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
|
import test.delegateReturn
|
||||||
|
|
||||||
class FakeContext {
|
class FakeContext {
|
||||||
val instance = mockk<Context>()
|
val instance = mockk<Context>()
|
||||||
|
fun givenPackageManager() = every { instance.packageManager }.delegateReturn()
|
||||||
|
}
|
||||||
|
|
||||||
|
class FakePackageManager {
|
||||||
|
val instance = mockk<PackageManager>()
|
||||||
}
|
}
|
Loading…
Reference in New Issue