diff --git a/domains/android/push/src/main/kotlin/app/dapk/st/push/PushModule.kt b/domains/android/push/src/main/kotlin/app/dapk/st/push/PushModule.kt index 67d12ae..221342e 100644 --- a/domains/android/push/src/main/kotlin/app/dapk/st/push/PushModule.kt +++ b/domains/android/push/src/main/kotlin/app/dapk/st/push/PushModule.kt @@ -9,6 +9,7 @@ import app.dapk.st.core.extensions.unsafeLazy import app.dapk.st.domain.push.PushTokenRegistrarPreferences import app.dapk.st.firebase.messaging.Messaging import app.dapk.st.push.messaging.MessagingPushTokenRegistrar +import app.dapk.st.push.unifiedpush.UnifiedPush import app.dapk.st.push.unifiedpush.UnifiedPushRegistrar class PushModule( @@ -28,7 +29,7 @@ class PushModule( pushHandler, messaging, ), - UnifiedPushRegistrar(context), + UnifiedPushRegistrar(context, object : UnifiedPush {}), PushTokenRegistrarPreferences(preferences) ) } diff --git a/domains/android/push/src/main/kotlin/app/dapk/st/push/unifiedpush/UnifiedPush.kt b/domains/android/push/src/main/kotlin/app/dapk/st/push/unifiedpush/UnifiedPush.kt new file mode 100644 index 0000000..8ebf2d5 --- /dev/null +++ b/domains/android/push/src/main/kotlin/app/dapk/st/push/unifiedpush/UnifiedPush.kt @@ -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) +} \ No newline at end of file diff --git a/domains/android/push/src/main/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushRegistrar.kt b/domains/android/push/src/main/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushRegistrar.kt index cbb5ad5..ac1dc0f 100644 --- a/domains/android/push/src/main/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushRegistrar.kt +++ b/domains/android/push/src/main/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushRegistrar.kt @@ -7,38 +7,39 @@ import app.dapk.st.core.AppLogTag import app.dapk.st.core.log import app.dapk.st.push.PushTokenRegistrar import app.dapk.st.push.Registrar -import org.unifiedpush.android.connector.UnifiedPush class UnifiedPushRegistrar( private val context: Context, + private val unifiedPush: UnifiedPush, + private val componentFactory: (Context) -> ComponentName = { ComponentName(it, UnifiedPushMessageReceiver::class.java) } ) : PushTokenRegistrar { fun registerSelection(registrar: Registrar) { log(AppLogTag.PUSH, "UnifiedPush - register: $registrar") - UnifiedPush.saveDistributor(context, registrar.id) + unifiedPush.saveDistributor(context, registrar.id) registerApp() } override suspend fun registerCurrentToken() { log(AppLogTag.PUSH, "UnifiedPush - register current token") - if (UnifiedPush.getDistributor(context).isNotEmpty()) { + if (unifiedPush.getDistributor(context).isNotEmpty()) { registerApp() } } private fun registerApp() { context.packageManager.setComponentEnabledSetting( - ComponentName(context, UnifiedPushMessageReceiver::class.java), + componentFactory(context), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP, ) - UnifiedPush.registerApp(context) + unifiedPush.registerApp(context) } override fun unregister() { - UnifiedPush.unregisterApp(context) + unifiedPush.unregisterApp(context) context.packageManager.setComponentEnabledSetting( - ComponentName(context, UnifiedPushMessageReceiver::class.java), + componentFactory(context), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP, ) diff --git a/domains/android/push/src/test/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushRegistrarTest.kt b/domains/android/push/src/test/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushRegistrarTest.kt new file mode 100644 index 0000000..993be26 --- /dev/null +++ b/domains/android/push/src/test/kotlin/app/dapk/st/push/unifiedpush/UnifiedPushRegistrarTest.kt @@ -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() +} \ No newline at end of file diff --git a/domains/android/stub/src/testFixtures/kotlin/fake/FakeContext.kt b/domains/android/stub/src/testFixtures/kotlin/fake/FakeContext.kt index 623c98c..122191e 100644 --- a/domains/android/stub/src/testFixtures/kotlin/fake/FakeContext.kt +++ b/domains/android/stub/src/testFixtures/kotlin/fake/FakeContext.kt @@ -1,8 +1,16 @@ package fake import android.content.Context +import android.content.pm.PackageManager +import io.mockk.every import io.mockk.mockk +import test.delegateReturn class FakeContext { val instance = mockk() + fun givenPackageManager() = every { instance.packageManager }.delegateReturn() +} + +class FakePackageManager { + val instance = mockk() } \ No newline at end of file