Skip to content

Commit

Permalink
Merge pull request #2200 from OneSignal/fix/setconsentgiven-before-in…
Browse files Browse the repository at this point in the history
…it-crash

Fix setting consentGiven throwing if called before initWithContext
  • Loading branch information
jkasten2 authored Oct 4, 2024
2 parents edcf734 + 92cb824 commit 8fb33fc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
}

// Services required by this class
private val operationRepo: IOperationRepo
get() = services.getService()
// WARNING: OperationRepo depends on OperationModelStore which in-turn depends
// on ApplicationService.appContext being non-null.
private var operationRepo: IOperationRepo? = null
private val identityModelStore: IdentityModelStore
get() = services.getService()
private val propertiesModelStore: PropertiesModelStore
Expand Down Expand Up @@ -208,6 +209,7 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
// get the current config model, if there is one
configModel = services.getService<ConfigModelStore>().model
sessionModel = services.getService<SessionModelStore>().model
operationRepo = services.getService<IOperationRepo>()

// initWithContext is called by our internal services/receivers/activites but they do not provide
// an appId (they don't know it). If the app has never called the external initWithContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,52 @@ class OneSignalImpTests : FunSpec({
// Then
exception.message shouldBe "Must call 'initWithContext' before 'logout'"
}

// consentRequired probably should have thrown like the other OneSignal methods in 5.0.0,
// but we can't make a breaking change to an existing API.
context("consentRequired") {
context("before initWithContext") {
test("set should not throw") {
// Given
val os = OneSignalImp()
// When
os.consentRequired = false
os.consentRequired = true
// Then
// Test fails if the above throws
}
test("get should not throw") {
// Given
val os = OneSignalImp()
// When
println(os.consentRequired)
// Then
// Test fails if the above throws
}
}
}

// consentGiven probably should have thrown like the other OneSignal methods in 5.0.0,
// but we can't make a breaking change to an existing API.
context("consentGiven") {
context("before initWithContext") {
test("set should not throw") {
// Given
val os = OneSignalImp()
// When
os.consentGiven = true
os.consentGiven = false
// Then
// Test fails if the above throws
}
test("get should not throw") {
// Given
val os = OneSignalImp()
// When
println(os.consentGiven)
// Then
// Test fails if the above throws
}
}
}
})

0 comments on commit 8fb33fc

Please sign in to comment.