Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jennantilla committed Jul 23, 2024
1 parent 94a0d30 commit 544bce1
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,35 @@ package com.onesignal.inAppMessages.internal
import com.onesignal.OneSignal
import com.onesignal.debug.LogLevel
import com.onesignal.debug.internal.logging.Logging
import com.onesignal.inAppMessages.IInAppMessageDidDismissEvent
import com.onesignal.inAppMessages.IInAppMessageDidDisplayEvent
import com.onesignal.inAppMessages.IInAppMessageLifecycleListener
import com.onesignal.inAppMessages.IInAppMessageWillDismissEvent
import com.onesignal.inAppMessages.IInAppMessageWillDisplayEvent
import com.onesignal.inAppMessages.internal.InAppMessagingHelpers.Companion.buildTestMessageWithRedisplay
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import java.util.UUID


class InAppMessagesTests : FunSpec({
val IAM_CLICK_ID = "button_id_123"
val LIMIT = 5
val DELAY: Long = 60

fun setLocalTriggerValue(key: String, localValue: String) {
if (localValue != null) OneSignal.InAppMessages.addTrigger(
key,
localValue
) else OneSignal.InAppMessages.removeTrigger(key)
val iamClickId = "button_id_123"
val limit = 5
val delay: Long = 60

fun setLocalTriggerValue(
key: String,
localValue: String,
) {
if (localValue != null) {
OneSignal.InAppMessages.addTrigger(
key,
localValue,
)
} else {
OneSignal.InAppMessages.removeTrigger(key)
}
}

fun comparativeOperatorTest(
operator: Trigger.OSTriggerOperator,
triggerValue: String,
localValue: String
localValue: String,
): Boolean {
// TODO
// setLocalTriggerValue("test_property", localValue)
Expand All @@ -52,7 +53,7 @@ class InAppMessagesTests : FunSpec({
Trigger.OSTriggerKind.SESSION_TIME,
null,
Trigger.OSTriggerOperator.GREATER_THAN_OR_EQUAL_TO.toString(),
3
3,
)
}

Expand Down Expand Up @@ -87,12 +88,12 @@ class InAppMessagesTests : FunSpec({

test("testBuiltMessageReDisplay") {
// Given
val message = buildTestMessageWithRedisplay(LIMIT, DELAY)
val message = buildTestMessageWithRedisplay(limit, delay)

// Then
message.redisplayStats.isRedisplayEnabled shouldBe true
message.redisplayStats.displayLimit shouldBe LIMIT
message.redisplayStats.displayDelay shouldBe DELAY
message.redisplayStats.displayLimit shouldBe limit
message.redisplayStats.displayDelay shouldBe delay
message.redisplayStats.lastDisplayTime shouldBe -1
message.redisplayStats.displayQuantity shouldBe 0

Expand All @@ -102,7 +103,7 @@ class InAppMessagesTests : FunSpec({
Trigger.OSTriggerKind.SESSION_TIME,
null,
Trigger.OSTriggerOperator.GREATER_THAN_OR_EQUAL_TO.toString(),
3
3,
)

// Then
Expand All @@ -116,56 +117,55 @@ class InAppMessagesTests : FunSpec({
test("testBuiltMessageRedisplayLimit") {
val message: InAppMessagingHelpers.OSTestInAppMessageInternal =
buildTestMessageWithRedisplay(
LIMIT,
DELAY
limit,
delay,
)
for (i in 0 until LIMIT) {
for (i in 0 until limit) {
message.redisplayStats.shouldDisplayAgain() shouldBe true
message.redisplayStats.incrementDisplayQuantity()
}
message.redisplayStats.incrementDisplayQuantity()
message.redisplayStats.shouldDisplayAgain() shouldBe false
}


test("testBuiltMessageRedisplayDelay") {
// TODO
}

test("testBuiltMessageRedisplayCLickId") {
val message: InAppMessagingHelpers.OSTestInAppMessageInternal =
buildTestMessageWithRedisplay(
LIMIT,
DELAY
limit,
delay,
)

message.clickedClickIds.isEmpty() shouldBe true
message.isClickAvailable(IAM_CLICK_ID)
message.isClickAvailable(iamClickId)

message.addClickId(IAM_CLICK_ID)
message.addClickId(iamClickId)
message.clearClickIds()

message.clickedClickIds.isEmpty() shouldBe true

message.addClickId(IAM_CLICK_ID)
message.addClickId(IAM_CLICK_ID)
message.addClickId(iamClickId)
message.addClickId(iamClickId)
message.clickedClickIds.size shouldBe 1

message.isClickAvailable(IAM_CLICK_ID) shouldBe false
message.isClickAvailable(iamClickId) shouldBe false

val messageWithoutDisplay2: InAppMessagingHelpers.OSTestInAppMessageInternal =
InAppMessagingHelpers.buildTestMessageWithSingleTrigger(
Trigger.OSTriggerKind.SESSION_TIME,
null,
Trigger.OSTriggerOperator.GREATER_THAN_OR_EQUAL_TO.toString(),
3
3,
)

messageWithoutDisplay2.addClickId(IAM_CLICK_ID)
messageWithoutDisplay2.isClickAvailable(IAM_CLICK_ID) shouldBe false
messageWithoutDisplay2.addClickId(iamClickId)
messageWithoutDisplay2.isClickAvailable(iamClickId) shouldBe false
}

test ("testBuiltMessageTrigger") {
test("testBuiltMessageTrigger") {
// TODO
}

Expand Down Expand Up @@ -233,15 +233,13 @@ class InAppMessagesTests : FunSpec({
// }
// OneSignal.InAppMessages.addClickListener(clickListener)


//assertMainThread()
//threadAndTaskWait()
// assertMainThread()
// threadAndTaskWait()

// call onMessageActionOccurredOnMessage

// Ensure we make REST call to OneSignal to report click.


// Ensure we fire public callback that In-App was clicked.
}

Expand All @@ -257,11 +255,11 @@ class InAppMessagesTests : FunSpec({
// TODO
}


/* Tests for IAM Lifecycle */
// Tests for IAM Lifecycle
// var iamLifecycleCounter = 0

test("testIAMLifecycleEventsFlow") {

// TODO
// add listener and incremenet counter
// val lifecycleListener = object : IInAppMessageLifecycleListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class InAppMessagingHelpers {

internal fun onMessageActionOccurredOnMessage(
message: InAppMessage,
clickResult: InAppMessageClickResult
clickResult: InAppMessageClickResult,
) {
val mockInAppMessageManager = mockk<InAppMessagesManager>()
mockInAppMessageManager.onMessageActionOccurredOnMessage(message, clickResult)
Expand All @@ -41,18 +41,26 @@ class InAppMessagingHelpers {
kind: Trigger.OSTriggerKind,
key: String?,
operator: String?,
value: Any?
value: Any?,
): OSTestInAppMessageInternal {
val triggersJson = basicTrigger(
kind, key,
operator!!, value!!
)
val triggersJson =
basicTrigger(
kind,
key,
operator!!,
value!!,
)
return buildTestMessageWithLiquid(triggersJson)
}

// Most tests build a test message using only one trigger.
// This convenience method makes it easy to build such a message
internal fun buildTestMessageWithSingleTrigger(kind: Trigger.OSTriggerKind, key: String?, operator: String, value: Any): OSTestInAppMessageInternal {
internal fun buildTestMessageWithSingleTrigger(
kind: Trigger.OSTriggerKind,
key: String?,
operator: String,
value: Any,
): OSTestInAppMessageInternal {
val triggersJson = basicTrigger(kind, key, operator, value)
return buildTestMessage(triggersJson)
}
Expand All @@ -65,17 +73,18 @@ class InAppMessagingHelpers {
kind: Trigger.OSTriggerKind,
key: String?,
operator: String,
value: Any
value: Any,
): JSONArray {
val triggerJson: JSONObject = object : JSONObject() {
init {
put("id", UUID.randomUUID().toString())
put("kind", kind.toString())
put("property", key)
put("operator", operator)
put("value", value)
val triggerJson: JSONObject =
object : JSONObject() {
init {
put("id", UUID.randomUUID().toString())
put("kind", kind.toString())
put("property", key)
put("operator", operator)
put("value", value)
}
}
}

return wrap(wrap(triggerJson))
}
Expand All @@ -88,22 +97,28 @@ class InAppMessagingHelpers {
}
}

fun buildTestMessageWithRedisplay(limit: Int, delay: Long): OSTestInAppMessageInternal {
fun buildTestMessageWithRedisplay(
limit: Int,
delay: Long,
): OSTestInAppMessageInternal {
return buildTestMessageWithMultipleDisplays(null, limit, delay)
}

private fun buildTestMessageWithMultipleDisplays(
triggerJson: JSONArray?,
limit: Int,
delay: Long
delay: Long,
): OSTestInAppMessageInternal {
val json = basicIAMJSONObject(triggerJson)
json.put("redisplay", object : JSONObject() {
init {
put("limit", limit)
put("delay", delay) //in seconds
}
})
json.put(
"redisplay",
object : JSONObject() {
init {
put("limit", limit)
put("delay", delay) // in seconds
}
},
)

return OSTestInAppMessageInternal(json)
}
Expand All @@ -114,21 +129,30 @@ class InAppMessagingHelpers {
jsonObject.put("clickIds", JSONArray(listOf("clickId1", "clickId2", "clickId3")))
// shouldn't hard-code?
jsonObject.put("displayedInSession", true)
jsonObject.put("variants", JSONObject().apply {
put("android", JSONObject().apply {
put("es", TEST_SPANISH_ANDROID_VARIANT_ID)
put("en", TEST_ENGLISH_ANDROID_VARIANT_ID)
})
})
jsonObject.put(
"variants",
JSONObject().apply {
put(
"android",
JSONObject().apply {
put("es", TEST_SPANISH_ANDROID_VARIANT_ID)
put("en", TEST_ENGLISH_ANDROID_VARIANT_ID)
},
)
},
)
jsonObject.put("max_display_time", 30)
if (triggerJson != null) {
jsonObject.put("triggers", triggerJson)
} else {
jsonObject.put("triggers", JSONArray())
}
jsonObject.put("actions", JSONArray().apply {
put(buildTestActionJson())
})
jsonObject.put(
"actions",
JSONArray().apply {
put(buildTestActionJson())
},
)

return jsonObject
}
Expand All @@ -143,17 +167,21 @@ class InAppMessagingHelpers {
put("url_target", "webview")
put("close", true)
put("pageId", IAM_PAGE_ID)
put("data", object : JSONObject() {
init {
put("test", "value")
}
})
put(
"data",
object : JSONObject() {
init {
put("test", "value")
}
},
)
}
}
}
}

// WIP

/** IAM Lifecycle */
internal fun onMessageWillDisplay(message: InAppMessage) {
val mockInAppMessageManager = mockk<InAppMessagesManager>()
Expand All @@ -178,17 +206,18 @@ class InAppMessagingHelpers {
// End IAM Lifecycle

class OSTestInAppMessageInternal(
private val jsonObject: JSONObject
private val jsonObject: JSONObject,
) {
private val inAppMessage: InAppMessage by lazy {
initializeInAppMessage()
}

private fun initializeInAppMessage(): InAppMessage {
val time = object : ITime {
override val currentTimeMillis: Long
get() = System.currentTimeMillis()
}
val time =
object : ITime {
override val currentTimeMillis: Long
get() = System.currentTimeMillis()
}

return InAppMessage(jsonObject, time)
}
Expand Down

0 comments on commit 544bce1

Please sign in to comment.