-
Notifications
You must be signed in to change notification settings - Fork 376
Fix: NPE in SyncJobService since 5.4 #2500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
175 changes: 175 additions & 0 deletions
175
OneSignalSDK/onesignal/core/src/test/java/com/onesignal/core/services/SyncJobServiceTests.kt
This file contains hidden or 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,175 @@ | ||
| package com.onesignal.core.services | ||
|
|
||
| import android.app.job.JobParameters | ||
| import com.onesignal.OneSignal | ||
| import com.onesignal.core.internal.background.IBackgroundManager | ||
| import com.onesignal.debug.LogLevel | ||
| import com.onesignal.debug.internal.logging.Logging | ||
| import com.onesignal.mocks.IOMockHelper | ||
| import com.onesignal.mocks.IOMockHelper.awaitIO | ||
| import io.kotest.core.spec.style.FunSpec | ||
| import io.kotest.matchers.shouldBe | ||
| import io.mockk.coEvery | ||
| import io.mockk.coVerify | ||
| import io.mockk.every | ||
| import io.mockk.mockk | ||
| import io.mockk.mockkObject | ||
| import io.mockk.spyk | ||
| import io.mockk.unmockkAll | ||
| import io.mockk.verify | ||
|
|
||
| private class Mocks { | ||
| val syncJobService = spyk(SyncJobService(), recordPrivateCalls = true) | ||
| val jobParameters = mockk<JobParameters>(relaxed = true) | ||
| val mockBackgroundManager = mockk<IBackgroundManager>(relaxed = true) | ||
| } | ||
|
|
||
| class SyncJobServiceTests : FunSpec({ | ||
| lateinit var mocks: Mocks | ||
|
|
||
| listener(IOMockHelper) | ||
|
|
||
| beforeAny { | ||
| Logging.logLevel = LogLevel.NONE | ||
| mocks = Mocks() // fresh instance for each test | ||
| mockkObject(OneSignal) | ||
| every { OneSignal.getService<IBackgroundManager>() } returns mocks.mockBackgroundManager | ||
| } | ||
|
|
||
| afterAny { | ||
| unmockkAll() | ||
| } | ||
|
|
||
| test("onStartJob returns true when initWithContext fails") { | ||
| // Given | ||
| val syncJobService = mocks.syncJobService | ||
| val jobParameters = mocks.jobParameters | ||
| coEvery { OneSignal.initWithContext(any()) } returns false | ||
|
|
||
| // When | ||
| val result = syncJobService.onStartJob(jobParameters) | ||
|
|
||
| // Then | ||
| result shouldBe true | ||
| } | ||
|
|
||
| test("onStartJob calls runBackgroundServices when initWithContext succeeds") { | ||
| // Given | ||
| val mockBackgroundManager = mocks.mockBackgroundManager | ||
| val syncJobService = mocks.syncJobService | ||
| val jobParameters = mocks.jobParameters | ||
| coEvery { OneSignal.initWithContext(any()) } returns true | ||
| every { mockBackgroundManager.needsJobReschedule } returns false | ||
|
|
||
| // When | ||
| val result = syncJobService.onStartJob(jobParameters) | ||
| awaitIO() | ||
|
|
||
| // Then | ||
| result shouldBe true | ||
| coVerify { mockBackgroundManager.runBackgroundServices() } | ||
| verify { syncJobService.jobFinished(jobParameters, false) } | ||
| } | ||
|
|
||
| test("onStartJob calls jobFinished with false when initWithContext failed") { | ||
| // Given | ||
| val syncJobService = mocks.syncJobService | ||
| val jobParameters = mocks.jobParameters | ||
| coEvery { OneSignal.initWithContext(any()) } returns false | ||
|
|
||
| // When | ||
| syncJobService.onStartJob(jobParameters) | ||
|
|
||
| // Then | ||
| verify { syncJobService.jobFinished(jobParameters, false) } | ||
| } | ||
|
|
||
| test("onStartJob calls jobFinished with false when needsJobReschedule is false") { | ||
| // Given | ||
| val syncJobService = mocks.syncJobService | ||
| val jobParameters = mocks.jobParameters | ||
| coEvery { OneSignal.initWithContext(any()) } returns true | ||
| every { mocks.mockBackgroundManager.needsJobReschedule } returns false | ||
|
|
||
| // When | ||
| syncJobService.onStartJob(jobParameters) | ||
| awaitIO() | ||
|
|
||
| // Then | ||
| verify { syncJobService.jobFinished(jobParameters, false) } | ||
| } | ||
|
|
||
| test("onStartJob calls jobFinished with true when needsJobReschedule is true") { | ||
| // Given | ||
| val mockBackgroundManager = mocks.mockBackgroundManager | ||
| val syncJobService = mocks.syncJobService | ||
| val jobParameters = mocks.jobParameters | ||
| coEvery { OneSignal.initWithContext(any()) } returns true | ||
| every { mockBackgroundManager.needsJobReschedule } returns true | ||
|
|
||
| // When | ||
| syncJobService.onStartJob(jobParameters) | ||
| awaitIO() | ||
|
|
||
| // Then | ||
| verify { syncJobService.jobFinished(jobParameters, true) } | ||
| verify { mockBackgroundManager.needsJobReschedule = false } | ||
| } | ||
|
|
||
| test("onStartJob resets needsJobReschedule to false after reading it") { | ||
| // Given | ||
| val mockBackgroundManager = mocks.mockBackgroundManager | ||
| val syncJobService = mocks.syncJobService | ||
| val jobParameters = mocks.jobParameters | ||
| coEvery { OneSignal.initWithContext(any()) } returns true | ||
| every { mockBackgroundManager.needsJobReschedule } returns true | ||
|
|
||
| // When | ||
| syncJobService.onStartJob(jobParameters) | ||
| awaitIO() | ||
|
|
||
| // Then | ||
| verify { mockBackgroundManager.needsJobReschedule = false } | ||
| } | ||
|
|
||
| test("onStopJob returns false when OneSignal.getService throws") { | ||
| // Given | ||
| val syncJobService = mocks.syncJobService | ||
| val jobParameters = mocks.jobParameters | ||
| coEvery { OneSignal.getService<Any>() } throws NullPointerException() | ||
|
|
||
| // When | ||
| val result = syncJobService.onStopJob(jobParameters) | ||
|
|
||
| // Then | ||
| result shouldBe false | ||
| } | ||
|
|
||
| test("onStopJob calls cancelRunBackgroundServices and returns its result") { | ||
| // Given | ||
| val mockBackgroundManager = mocks.mockBackgroundManager | ||
| val syncJobService = mocks.syncJobService | ||
| val jobParameters = mocks.jobParameters | ||
| every { mockBackgroundManager.cancelRunBackgroundServices() } returns true | ||
|
|
||
| // When | ||
| val result = syncJobService.onStopJob(jobParameters) | ||
|
|
||
| // Then | ||
| result shouldBe true | ||
| verify { mockBackgroundManager.cancelRunBackgroundServices() } | ||
| } | ||
|
|
||
| test("onStopJob returns false when cancelRunBackgroundServices returns false") { | ||
| // Given | ||
| val mockBackgroundManager = mocks.mockBackgroundManager | ||
| every { mockBackgroundManager.cancelRunBackgroundServices() } returns false | ||
|
|
||
| // When | ||
| val result = mocks.syncJobService.onStopJob(mocks.jobParameters) | ||
|
|
||
| // Then | ||
| result shouldBe false | ||
| verify { mockBackgroundManager.cancelRunBackgroundServices() } | ||
| } | ||
| }) |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.