diff --git a/OneSignalSDK/onesignal/in-app-messages/src/test/java/com/onesignal/inAppMessages/internal/InAppMessagesTests.kt b/OneSignalSDK/onesignal/in-app-messages/src/test/java/com/onesignal/inAppMessages/internal/InAppMessagesTests.kt index 280709467f..04f45ce397 100644 --- a/OneSignalSDK/onesignal/in-app-messages/src/test/java/com/onesignal/inAppMessages/internal/InAppMessagesTests.kt +++ b/OneSignalSDK/onesignal/in-app-messages/src/test/java/com/onesignal/inAppMessages/internal/InAppMessagesTests.kt @@ -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) @@ -52,7 +53,7 @@ class InAppMessagesTests : FunSpec({ Trigger.OSTriggerKind.SESSION_TIME, null, Trigger.OSTriggerOperator.GREATER_THAN_OR_EQUAL_TO.toString(), - 3 + 3, ) } @@ -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 @@ -102,7 +103,7 @@ class InAppMessagesTests : FunSpec({ Trigger.OSTriggerKind.SESSION_TIME, null, Trigger.OSTriggerOperator.GREATER_THAN_OR_EQUAL_TO.toString(), - 3 + 3, ) // Then @@ -116,10 +117,10 @@ 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() } @@ -127,7 +128,6 @@ class InAppMessagesTests : FunSpec({ message.redisplayStats.shouldDisplayAgain() shouldBe false } - test("testBuiltMessageRedisplayDelay") { // TODO } @@ -135,37 +135,37 @@ class InAppMessagesTests : FunSpec({ 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 } @@ -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. } @@ -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 { diff --git a/OneSignalSDK/onesignal/in-app-messages/src/test/java/com/onesignal/inAppMessages/internal/InAppMessagingHelpers.kt b/OneSignalSDK/onesignal/in-app-messages/src/test/java/com/onesignal/inAppMessages/internal/InAppMessagingHelpers.kt index c3dbf9748b..24f521f77a 100644 --- a/OneSignalSDK/onesignal/in-app-messages/src/test/java/com/onesignal/inAppMessages/internal/InAppMessagingHelpers.kt +++ b/OneSignalSDK/onesignal/in-app-messages/src/test/java/com/onesignal/inAppMessages/internal/InAppMessagingHelpers.kt @@ -25,7 +25,7 @@ class InAppMessagingHelpers { internal fun onMessageActionOccurredOnMessage( message: InAppMessage, - clickResult: InAppMessageClickResult + clickResult: InAppMessageClickResult, ) { val mockInAppMessageManager = mockk() mockInAppMessageManager.onMessageActionOccurredOnMessage(message, clickResult) @@ -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) } @@ -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)) } @@ -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) } @@ -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 } @@ -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() @@ -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) }