Skip to content

Commit 92cb824

Browse files
committed
Fix consentGiven called before initWithContext
Prevent throwing if consentGiven is set before calling initWithContext. Since consentGiven accesses operationRepo which requires ApplicationService.context to be non-null we stopped using the lazy-loading pattern for operationRepo. We put the assignment into initWithContext to make this more clear. Also added tests to prevent this mistake in the future.
1 parent edcf734 commit 92cb824

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

OneSignalSDK/onesignal/core/src/main/java/com/onesignal/internal/OneSignalImp.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,9 @@ internal class OneSignalImp : IOneSignal, IServiceProvider {
126126
}
127127

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

212214
// initWithContext is called by our internal services/receivers/activites but they do not provide
213215
// an appId (they don't know it). If the app has never called the external initWithContext

OneSignalSDK/onesignal/core/src/test/java/com/onesignal/internal/OneSignalImpTests.kt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,52 @@ class OneSignalImpTests : FunSpec({
3838
// Then
3939
exception.message shouldBe "Must call 'initWithContext' before 'logout'"
4040
}
41+
42+
// consentRequired probably should have thrown like the other OneSignal methods in 5.0.0,
43+
// but we can't make a breaking change to an existing API.
44+
context("consentRequired") {
45+
context("before initWithContext") {
46+
test("set should not throw") {
47+
// Given
48+
val os = OneSignalImp()
49+
// When
50+
os.consentRequired = false
51+
os.consentRequired = true
52+
// Then
53+
// Test fails if the above throws
54+
}
55+
test("get should not throw") {
56+
// Given
57+
val os = OneSignalImp()
58+
// When
59+
println(os.consentRequired)
60+
// Then
61+
// Test fails if the above throws
62+
}
63+
}
64+
}
65+
66+
// consentGiven probably should have thrown like the other OneSignal methods in 5.0.0,
67+
// but we can't make a breaking change to an existing API.
68+
context("consentGiven") {
69+
context("before initWithContext") {
70+
test("set should not throw") {
71+
// Given
72+
val os = OneSignalImp()
73+
// When
74+
os.consentGiven = true
75+
os.consentGiven = false
76+
// Then
77+
// Test fails if the above throws
78+
}
79+
test("get should not throw") {
80+
// Given
81+
val os = OneSignalImp()
82+
// When
83+
println(os.consentGiven)
84+
// Then
85+
// Test fails if the above throws
86+
}
87+
}
88+
}
4189
})

0 commit comments

Comments
 (0)