Skip to content

Commit

Permalink
Start with polling interval as the background value
Browse files Browse the repository at this point in the history
This protects init in the background
onFocus is called immediately if the app is in focus and wakes so we start polling correctly

update unit test to account for this
  • Loading branch information
emawby committed Jun 14, 2024
1 parent 8feeda8 commit 4cb7cb1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ internal class NotificationPermissionController(
init {
this.enabled = notificationsEnabled()
_requestPermission.registerAsCallback(PERMISSION_TYPE, this)
pollingWaitInterval = _configModelStore.model.foregroundFetchNotificationPermissionInterval
pollingWaitInterval = _configModelStore.model.backgroundFetchNotificationPermissionInterval
registerPollingLifecycleListener()
coroutineScope.launch {
pollForPermission()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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
Expand Down Expand Up @@ -39,9 +38,14 @@ class NotificationPermissionControllerTests : FunSpec({
val mockRequestPermissionService = mockk<IRequestPermissionService>()
every { mockRequestPermissionService.registerAsCallback(any(), any()) } just runs
val mockPreferenceService = mockk<IPreferencesService>()

val focusHandlerList = mutableListOf<IApplicationLifecycleHandler>()
val mockAppService = mockk<IApplicationService>()
every { mockAppService.addApplicationLifecycleHandler(any()) } answers {
focusHandlerList.add(firstArg<IApplicationLifecycleHandler>())
}
every { mockAppService.appContext } returns ApplicationProvider.getApplicationContext()
var handlerFired = false
val notificationPermissionController = NotificationPermissionController(AndroidMockHelper.applicationService(), mockRequestPermissionService, AndroidMockHelper.applicationService(), mockPreferenceService, MockHelper.configModelStore())
val notificationPermissionController = NotificationPermissionController(mockAppService, mockRequestPermissionService, mockAppService, mockPreferenceService, MockHelper.configModelStore())

notificationPermissionController.subscribe(
object : INotificationPermissionChangedHandler {
Expand All @@ -50,6 +54,11 @@ class NotificationPermissionControllerTests : FunSpec({
}
},
)
// call onFocus to set the proper polling interval.
// This happens when registering the lifecycle handler
for (focusHandler in focusHandlerList) {
focusHandler.onFocus()
}

// When
// permission changes
Expand Down Expand Up @@ -83,6 +92,11 @@ class NotificationPermissionControllerTests : FunSpec({
}
},
)
// call onFocus to set the proper polling interval.
// This happens when registering the lifecycle handler
for (focusHandler in handlerList) {
focusHandler.onFocus()
}

// When
// the app has loses focus
Expand Down Expand Up @@ -121,6 +135,11 @@ class NotificationPermissionControllerTests : FunSpec({
}
},
)
// call onFocus to set the proper polling interval.
// This happens when registering the lifecycle handler
for (focusHandler in handlerList) {
focusHandler.onFocus()
}

// When
// the app loses focus
Expand Down

0 comments on commit 4cb7cb1

Please sign in to comment.