-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2112 from OneSignal/fix/poll_for_notification_per…
…mission Poll for notification permission changes
- Loading branch information
Showing
5 changed files
with
260 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
.../com/onesignal/notifications/internal/permission/NotificationPermissionControllerTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package com.onesignal.notifications.internal.permission | ||
|
||
import androidx.test.core.app.ApplicationProvider | ||
import br.com.colman.kotest.android.extensions.robolectric.RobolectricTest | ||
import com.onesignal.core.internal.application.IApplicationLifecycleHandler | ||
import com.onesignal.core.internal.application.IApplicationService | ||
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.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() | ||
} | ||
|
||
beforeEach { | ||
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>() | ||
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(mockAppService, mockRequestPermissionService, mockAppService, mockPreferenceService, MockHelper.configModelStore()) | ||
|
||
notificationPermissionController.subscribe( | ||
object : INotificationPermissionChangedHandler { | ||
override fun onNotificationPermissionChanged(enabled: Boolean) { | ||
handlerFired = true | ||
} | ||
}, | ||
) | ||
// call onFocus to set the proper polling interval. | ||
// This happens when registering the lifecycle handler | ||
for (focusHandler in focusHandlerList) { | ||
focusHandler.onFocus() | ||
} | ||
|
||
// When | ||
// permission changes | ||
ShadowRoboNotificationManager.setShadowNotificationsEnabled(false) | ||
delay(100) | ||
|
||
// Then | ||
// permissionChanged Event should fire | ||
handlerFired shouldBe true | ||
} | ||
|
||
test("NotificationPermissionController permission polling pauses when app loses") { | ||
// Given | ||
val mockRequestPermissionService = mockk<IRequestPermissionService>() | ||
every { mockRequestPermissionService.registerAsCallback(any(), any()) } just runs | ||
val mockPreferenceService = mockk<IPreferencesService>() | ||
val handlerList = mutableListOf<IApplicationLifecycleHandler>() | ||
val mockAppService = mockk<IApplicationService>() | ||
every { mockAppService.addApplicationLifecycleHandler(any()) } answers { | ||
handlerList.add(firstArg<IApplicationLifecycleHandler>()) | ||
} | ||
every { mockAppService.appContext } returns ApplicationProvider.getApplicationContext() | ||
|
||
var handlerFired = false | ||
val notificationPermissionController = NotificationPermissionController(mockAppService, mockRequestPermissionService, mockAppService, mockPreferenceService, MockHelper.configModelStore()) | ||
|
||
notificationPermissionController.subscribe( | ||
object : INotificationPermissionChangedHandler { | ||
override fun onNotificationPermissionChanged(enabled: Boolean) { | ||
handlerFired = true | ||
} | ||
}, | ||
) | ||
// 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 | ||
for (handler in handlerList) { | ||
handler.onUnfocused() | ||
} | ||
delay(100) | ||
// the permission changes | ||
ShadowRoboNotificationManager.setShadowNotificationsEnabled(false) | ||
delay(100) | ||
|
||
// Then | ||
// permissionChanged Event should not fire | ||
handlerFired shouldBe false | ||
} | ||
|
||
test("NotificationPermissionController permission polling resumes when app gains focus") { | ||
// Given | ||
val mockRequestPermissionService = mockk<IRequestPermissionService>() | ||
every { mockRequestPermissionService.registerAsCallback(any(), any()) } just runs | ||
val mockPreferenceService = mockk<IPreferencesService>() | ||
val handlerList = mutableListOf<IApplicationLifecycleHandler>() | ||
val mockAppService = mockk<IApplicationService>() | ||
every { mockAppService.addApplicationLifecycleHandler(any()) } answers { | ||
handlerList.add(firstArg<IApplicationLifecycleHandler>()) | ||
} | ||
every { mockAppService.appContext } returns ApplicationProvider.getApplicationContext() | ||
|
||
var handlerFired = false | ||
val notificationPermissionController = NotificationPermissionController(mockAppService, mockRequestPermissionService, mockAppService, mockPreferenceService, MockHelper.configModelStore()) | ||
|
||
notificationPermissionController.subscribe( | ||
object : INotificationPermissionChangedHandler { | ||
override fun onNotificationPermissionChanged(enabled: Boolean) { | ||
handlerFired = true | ||
} | ||
}, | ||
) | ||
// 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 | ||
for (handler in handlerList) { | ||
handler.onUnfocused() | ||
} | ||
delay(100) | ||
// the permission changes | ||
ShadowRoboNotificationManager.setShadowNotificationsEnabled(false) | ||
delay(100) | ||
// the app regains focus | ||
for (handler in handlerList) { | ||
handler.onFocus() | ||
} | ||
delay(5) | ||
|
||
// Then | ||
// permissionChanged Event should fire | ||
handlerFired shouldBe true | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters