Skip to content

Commit 9fd560e

Browse files
committed
[User Model] Core Unit Tests
1 parent f2ec786 commit 9fd560e

37 files changed

+4486
-71
lines changed

OneSignalSDK/onesignal/src/main/java/com/onesignal/common/exceptions/BackendException.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ class BackendException(
1212
/**
1313
* The response, if one exists.
1414
*/
15-
val response: String?
15+
val response: String? = null
1616
) : Exception()

OneSignalSDK/onesignal/src/main/java/com/onesignal/core/internal/application/impl/ApplicationService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import com.onesignal.core.internal.application.IApplicationService
2626
import com.onesignal.debug.internal.logging.Logging
2727
import java.lang.ref.WeakReference
2828

29-
internal class ApplicationService() : IApplicationService, ActivityLifecycleCallbacks, OnGlobalLayoutListener {
29+
class ApplicationService() : IApplicationService, ActivityLifecycleCallbacks, OnGlobalLayoutListener {
3030
private val _activityLifecycleNotifier: IEventProducer<IActivityLifecycleHandler> = EventProducer()
3131
private val _applicationLifecycleNotifier: IEventProducer<IApplicationLifecycleHandler> = EventProducer()
3232
private val _systemConditionNotifier: IEventProducer<ISystemConditionHandler> = EventProducer()
@@ -102,6 +102,7 @@ internal class ApplicationService() : IApplicationService, ActivityLifecycleCall
102102
entryState = AppEntryAction.APP_OPEN
103103
if (isCurrentActivityNull && isContextActivity) {
104104
current = context as Activity?
105+
_activityReferences = 1
105106
_nextResumeIsFirstActivity = true
106107
}
107108
} else {

OneSignalSDK/onesignal/src/main/java/com/onesignal/core/internal/config/ConfigModel.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,20 @@ class ConfigModel : Model() {
5454
get() = getProperty(::unsubscribeWhenNotificationsDisabled.name) { false }
5555
set(value) { setProperty(::unsubscribeWhenNotificationsDisabled.name, value) }
5656

57+
/**
58+
* The timeout in milliseconds for an HTTP connection.
59+
*/
60+
var httpTimeout: Int
61+
get() = getProperty(::httpTimeout.name) { 120000 }
62+
set(value) { setProperty(::httpTimeout.name, value) }
63+
64+
/**
65+
* The timeout in milliseconds for an HTTP connection GET request.
66+
*/
67+
var httpGetTimeout: Int
68+
get() = getProperty(::httpGetTimeout.name) { 60000 }
69+
set(value) { setProperty(::httpGetTimeout.name, value) }
70+
5771
/**
5872
* Maximum time in milliseconds a user can spend out of focus before a new session is created.
5973
*/

OneSignalSDK/onesignal/src/main/java/com/onesignal/core/internal/http/impl/HttpClient.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.onesignal.core.internal.preferences.IPreferencesService
1111
import com.onesignal.core.internal.preferences.PreferenceOneSignalKeys
1212
import com.onesignal.core.internal.preferences.PreferenceStores
1313
import com.onesignal.debug.internal.logging.Logging
14+
import kotlinx.coroutines.DelicateCoroutinesApi
1415
import kotlinx.coroutines.Dispatchers
1516
import kotlinx.coroutines.GlobalScope
1617
import kotlinx.coroutines.TimeoutCancellationException
@@ -29,23 +30,23 @@ internal class HttpClient(
2930
private val _configModelStore: ConfigModelStore
3031
) : IHttpClient {
3132
override suspend fun post(url: String, body: JSONObject): HttpResponse {
32-
return makeRequest(url, "POST", body, TIMEOUT, null)
33+
return makeRequest(url, "POST", body, _configModelStore.model.httpTimeout, null)
3334
}
3435

3536
override suspend fun get(url: String, cacheKey: String?): HttpResponse {
36-
return makeRequest(url, null, null, GET_TIMEOUT, cacheKey)
37+
return makeRequest(url, null, null, _configModelStore.model.httpGetTimeout, cacheKey)
3738
}
3839

3940
override suspend fun put(url: String, body: JSONObject): HttpResponse {
40-
return makeRequest(url, "PUT", body, TIMEOUT, null)
41+
return makeRequest(url, "PUT", body, _configModelStore.model.httpTimeout, null)
4142
}
4243

4344
override suspend fun patch(url: String, body: JSONObject): HttpResponse {
44-
return makeRequest(url, "PATCH", body, TIMEOUT, null)
45+
return makeRequest(url, "PATCH", body, _configModelStore.model.httpTimeout, null)
4546
}
4647

4748
override suspend fun delete(url: String): HttpResponse {
48-
return makeRequest(url, "DELETE", null, TIMEOUT, null)
49+
return makeRequest(url, "DELETE", null, _configModelStore.model.httpTimeout, null)
4950
}
5051

5152
private suspend fun makeRequest(
@@ -73,6 +74,7 @@ internal class HttpClient(
7374
}
7475
}
7576

77+
@OptIn(DelicateCoroutinesApi::class)
7678
private suspend fun makeRequestIODispatcher(
7779
url: String,
7880
method: String?,
@@ -218,8 +220,6 @@ internal class HttpClient(
218220
companion object {
219221
private const val OS_API_VERSION = "1"
220222
private const val OS_ACCEPT_HEADER = "application/vnd.onesignal.v$OS_API_VERSION+json"
221-
private const val GET_TIMEOUT = 60000
222-
private const val TIMEOUT = 120000
223223
private const val THREAD_ID = 10000
224224
}
225225
}

OneSignalSDK/onesignal/src/main/java/com/onesignal/core/internal/operations/impl/OperationRepo.kt

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.onesignal.core.internal.operations.impl
22

33
import com.onesignal.common.threading.WaiterWithValue
4+
import com.onesignal.common.threading.suspendifyOnThread
45
import com.onesignal.core.internal.config.ConfigModelStore
56
import com.onesignal.core.internal.operations.ExecutionResult
67
import com.onesignal.core.internal.operations.GroupComparisonType
@@ -11,10 +12,6 @@ import com.onesignal.core.internal.startup.IStartableService
1112
import com.onesignal.core.internal.time.ITime
1213
import com.onesignal.debug.LogLevel
1314
import com.onesignal.debug.internal.logging.Logging
14-
import kotlinx.coroutines.Deferred
15-
import kotlinx.coroutines.DelicateCoroutinesApi
16-
import kotlinx.coroutines.GlobalScope
17-
import kotlinx.coroutines.async
1815
import kotlinx.coroutines.withTimeoutOrNull
1916
import java.util.UUID
2017

@@ -32,7 +29,6 @@ internal class OperationRepo(
3229

3330
private val _executorsMap: Map<String, IOperationExecutor>
3431
private val _queue = mutableListOf<OperationQueueItem>()
35-
private var _queueJob: Deferred<Unit>? = null
3632
private val _waiter = WaiterWithValue<Boolean>()
3733

3834
init {
@@ -51,8 +47,9 @@ internal class OperationRepo(
5147
}
5248

5349
override fun start() {
54-
// fire up an async job that will run "forever" so we don't hold up the other startable services.
55-
_queueJob = doWorkAsync()
50+
suspendifyOnThread {
51+
processQueueForever()
52+
}
5653
}
5754

5855
override fun enqueue(operation: Operation, flush: Boolean) {
@@ -82,8 +79,11 @@ internal class OperationRepo(
8279
_waiter.wake(flush)
8380
}
8481

85-
@OptIn(DelicateCoroutinesApi::class)
86-
private fun doWorkAsync() = GlobalScope.async {
82+
/**
83+
* The background processing that will never return. This should be called on it's own
84+
* dedicated thread.
85+
*/
86+
private suspend fun processQueueForever() {
8787
var lastSyncTime = _time.currentTimeMillis
8888
var force = false
8989

@@ -108,12 +108,10 @@ internal class OperationRepo(
108108
force = false
109109
}
110110

111-
if (ops == null) {
112-
continue
111+
if (ops != null) {
112+
executeOperations(ops!!)
113113
}
114114

115-
executeOperations(ops!!)
116-
117115
if (!force) {
118116
// potentially delay to prevent this from constant IO if a bunch of
119117
// operations are set sequentially.

OneSignalSDK/onesignal/src/main/java/com/onesignal/core/internal/preferences/impl/PreferencesService.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,14 @@ internal class PreferencesService(
7575
}
7676
}
7777

78-
return defValue
78+
return when (type) {
79+
String::class.java -> defValue as String?
80+
Boolean::class.java -> (defValue as Boolean?) ?: false
81+
Int::class.java -> (defValue as Int?) ?: 0
82+
Long::class.java -> (defValue as Long?) ?: 0
83+
Set::class.java -> defValue as Set<String>?
84+
else -> null
85+
}
7986
}
8087

8188
private fun save(store: String, key: String, value: Any?) {

OneSignalSDK/onesignal/src/main/java/com/onesignal/user/internal/backend/IUserBackendService.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class CreateUserResponse(
6262
val properties: PropertiesObject,
6363

6464
/**
65-
* The IDs of the subscriptions for the user.
65+
* The subscriptions for the user.
6666
*/
67-
val subscriptionIDs: List<String>
67+
val subscriptions: List<SubscriptionObject>
6868
)

OneSignalSDK/onesignal/src/main/java/com/onesignal/user/internal/backend/PropertiesObject.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.onesignal.user.internal.backend
33
class PropertiesObject(
44
val tags: Map<String, String>? = null,
55
val language: String? = null,
6-
val timezoneId: UInt? = null,
6+
val timezoneId: String? = null,
77
val country: String? = null,
88
val latitude: Double? = null,
99
val longitude: Double? = null,

OneSignalSDK/onesignal/src/main/java/com/onesignal/user/internal/backend/impl/IdentityBackendService.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.onesignal.user.internal.backend.impl
22

3+
import com.onesignal.core.internal.http.IHttpClient
34
import com.onesignal.user.internal.backend.IIdentityBackendService
45
import kotlinx.coroutines.yield
56

6-
internal class IdentityBackendService : IIdentityBackendService {
7+
internal class IdentityBackendService(
8+
private val _httpClient: IHttpClient
9+
) : IIdentityBackendService {
710
override suspend fun createAlias(appId: String, aliasLabel: String, aliasValue: String, identities: Map<String, String>): Map<String, String> {
811
// TODO: To Implement
912
yield()

OneSignalSDK/onesignal/src/main/java/com/onesignal/user/internal/backend/impl/UserBackendService.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package com.onesignal.user.internal.backend.impl
22

3+
import com.onesignal.core.internal.http.IHttpClient
34
import com.onesignal.user.internal.backend.CreateUserResponse
45
import com.onesignal.user.internal.backend.ISubscriptionBackendService
56
import com.onesignal.user.internal.backend.IUserBackendService
67
import com.onesignal.user.internal.backend.IdentityConstants
78
import com.onesignal.user.internal.backend.PropertiesDeltasObject
89
import com.onesignal.user.internal.backend.PropertiesObject
910
import com.onesignal.user.internal.backend.SubscriptionObject
11+
import com.onesignal.user.internal.backend.SubscriptionObjectType
1012
import com.onesignal.user.internal.subscriptions.SubscriptionModel
1113
import org.json.JSONArray
1214
import org.json.JSONObject
1315
import java.util.UUID
1416

1517
internal class UserBackendService(
18+
private val _httpClient: IHttpClient,
1619
private val _subscriptionBackend: ISubscriptionBackendService
1720
) : IUserBackendService {
1821

@@ -43,7 +46,7 @@ internal class UserBackendService(
4346
val mutIdentities = identities.toMutableMap()
4447
mutIdentities[IdentityConstants.ONESIGNAL_ID] = UUID.randomUUID().toString()
4548

46-
return CreateUserResponse(mutIdentities, properties, subscriptionIDs)
49+
return CreateUserResponse(mutIdentities, properties, subscriptionIDs.map { SubscriptionObject(it, SubscriptionObjectType.ANDROID_PUSH) })
4750
}
4851

4952
override suspend fun updateUser(appId: String, aliasLabel: String, aliasValue: String, properties: PropertiesObject, refreshDeviceMetadata: Boolean, propertyiesDelta: PropertiesDeltasObject) {

0 commit comments

Comments
 (0)