Skip to content

Commit

Permalink
Create NotificationPermissionControllerTests
Browse files Browse the repository at this point in the history
Adding an initial test for the polling behavior
Adding mocking for setting notifications as enabled/disabled in ShadowRoboNotificationManager
Adding foregroundFetchNotificationPermissionInterval to the MockHelper configModel with a value of 1 ms
  • Loading branch information
emawby committed Jun 13, 2024
1 parent 54bb02d commit 4134618
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.onesignal.notifications.internal.permission

import br.com.colman.kotest.android.extensions.robolectric.RobolectricTest
import com.onesignal.core.internal.permissions.IRequestPermissionService
import com.onesignal.core.internal.preferences.IPreferencesService
import com.onesignal.debug.LogLevel
import com.onesignal.debug.internal.logging.Logging
import com.onesignal.mocks.AndroidMockHelper
import com.onesignal.mocks.MockHelper
import com.onesignal.notifications.internal.permissions.INotificationPermissionChangedHandler
import com.onesignal.notifications.internal.permissions.impl.NotificationPermissionController
import com.onesignal.notifications.shadows.ShadowRoboNotificationManager
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.runs
import kotlinx.coroutines.delay
import org.robolectric.annotation.Config

@Config(
packageName = "com.onesignal.example",
shadows = [ShadowRoboNotificationManager::class],
sdk = [33],
)
@RobolectricTest
class NotificationPermissionControllerTests : FunSpec({
beforeAny {
Logging.logLevel = LogLevel.NONE
ShadowRoboNotificationManager.reset()
}

test("NotificationPermissionController permission polling fires permission changed event") {
// Given
val mockRequestPermissionService = mockk<IRequestPermissionService>()
every { mockRequestPermissionService.registerAsCallback(any(), any()) } just runs
val mockPreferenceService = mockk<IPreferencesService>()

var handlerFired = false
val notificationPermissionController = NotificationPermissionController(AndroidMockHelper.applicationService(), mockRequestPermissionService, AndroidMockHelper.applicationService(), mockPreferenceService, MockHelper.configModelStore())

notificationPermissionController.subscribe(
object : INotificationPermissionChangedHandler {
override fun onNotificationPermissionChanged(enabled: Boolean) {
handlerFired = true
}
},
)

// When
// permission changes
ShadowRoboNotificationManager.setNotificationsEnabled(false)
delay(5)

// Then
// permissionChanged Event should fire
handlerFired shouldBe true
}
})
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ class ShadowRoboNotificationManager : ShadowNotificationManager() {
return notifications
}

fun setNotificationsEnabled(enabled: Boolean) {
mInstance.setNotificationsEnabled(enabled)
}

var lastChannel: NotificationChannel? = null
var lastChannelGroup: NotificationChannelGroup? = null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ object MockHelper {
configModel.opRepoPostCreateDelay = 1
configModel.opRepoPostCreateRetryUpTo = 1
configModel.opRepoDefaultFailRetryBackoff = 1
configModel.foregroundFetchNotificationPermissionInterval = 1

configModel.appId = DEFAULT_APP_ID

Expand Down

0 comments on commit 4134618

Please sign in to comment.