|
| 1 | +package com.onesignal.inAppMessages.internal |
| 2 | + |
| 3 | +import com.onesignal.core.internal.time.ITime |
| 4 | +import org.json.JSONArray |
| 5 | +import org.json.JSONObject |
| 6 | +import java.util.UUID |
| 7 | + |
| 8 | +class InAppMessagingHelpers { |
| 9 | + companion object { |
| 10 | + const val TEST_SPANISH_ANDROID_VARIANT_ID = "d8cc-11e4-bed1-df8f05be55ba-a4b3gj7f" |
| 11 | + const val TEST_ENGLISH_ANDROID_VARIANT_ID = "11e4-bed1-df8f05be55ba-a4b3gj7f-d8cc" |
| 12 | + const val IAM_CLICK_ID = "12345678-1234-1234-1234-123456789012" |
| 13 | + const val IAM_PAGE_ID = "12345678-1234-ABCD-1234-123456789012" |
| 14 | + |
| 15 | + // Most tests build a test message using only one trigger. |
| 16 | + // This convenience method makes it easy to build such a message |
| 17 | + internal fun buildTestMessageWithSingleTrigger(kind: Trigger.OSTriggerKind, key: String?, operator: String, value: Any): OSTestInAppMessageInternal { |
| 18 | + val triggersJson = basicTrigger(kind, key, operator, value) |
| 19 | + return buildTestMessage(triggersJson) |
| 20 | + } |
| 21 | + |
| 22 | + private fun buildTestMessage(triggerJson: JSONArray?): OSTestInAppMessageInternal { |
| 23 | + return OSTestInAppMessageInternal(basicIAMJSONObject(triggerJson)) |
| 24 | + } |
| 25 | + |
| 26 | + private fun basicTrigger( |
| 27 | + kind: Trigger.OSTriggerKind, |
| 28 | + key: String?, |
| 29 | + operator: String, |
| 30 | + value: Any |
| 31 | + ): JSONArray { |
| 32 | + val triggerJson: JSONObject = object : JSONObject() { |
| 33 | + init { |
| 34 | + put("id", UUID.randomUUID().toString()) |
| 35 | + put("kind", kind.toString()) |
| 36 | + put("property", key) |
| 37 | + put("operator", operator) |
| 38 | + put("value", value) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + return wrap(wrap(triggerJson)) |
| 43 | + } |
| 44 | + |
| 45 | + private fun wrap(`object`: Any?): JSONArray { |
| 46 | + return object : JSONArray() { |
| 47 | + init { |
| 48 | + put(`object`) |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + fun buildTestMessageWithRedisplay(limit: Int, delay: Long): OSTestInAppMessageInternal { |
| 54 | + return buildTestMessageWithMultipleDisplays(null, limit, delay) |
| 55 | + } |
| 56 | + |
| 57 | + private fun buildTestMessageWithMultipleDisplays( |
| 58 | + triggerJson: JSONArray?, |
| 59 | + limit: Int, |
| 60 | + delay: Long |
| 61 | + ): OSTestInAppMessageInternal { |
| 62 | + val json = basicIAMJSONObject(triggerJson) |
| 63 | + json.put("redisplay", object : JSONObject() { |
| 64 | + init { |
| 65 | + put("limit", limit) |
| 66 | + put("delay", delay) //in seconds |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + return OSTestInAppMessageInternal(json) |
| 71 | + } |
| 72 | + |
| 73 | + private fun basicIAMJSONObject(triggerJson: JSONArray?): JSONObject { |
| 74 | + val jsonObject = JSONObject() |
| 75 | + jsonObject.put("id", UUID.randomUUID().toString()) |
| 76 | + jsonObject.put("clickIds", JSONArray(listOf("clickId1", "clickId2", "clickId3"))) |
| 77 | + // shouldn't hard-code? |
| 78 | + jsonObject.put("displayedInSession", true) |
| 79 | + jsonObject.put("variants", JSONObject().apply { |
| 80 | + put("android", JSONObject().apply { |
| 81 | + put("es", TEST_SPANISH_ANDROID_VARIANT_ID) |
| 82 | + put("en", TEST_ENGLISH_ANDROID_VARIANT_ID) |
| 83 | + }) |
| 84 | + }) |
| 85 | + jsonObject.put("max_display_time", 30) |
| 86 | + if (triggerJson != null) { |
| 87 | + jsonObject.put("triggers", triggerJson) |
| 88 | + } else { |
| 89 | + jsonObject.put("triggers", JSONArray()) |
| 90 | + } |
| 91 | + jsonObject.put("actions", JSONArray().apply { |
| 92 | + put(buildTestActionJson()) |
| 93 | + }) |
| 94 | + |
| 95 | + return jsonObject |
| 96 | + } |
| 97 | + |
| 98 | + fun buildTestActionJson(): JSONObject { |
| 99 | + return object : JSONObject() { |
| 100 | + init { |
| 101 | + put("click_type", "button") |
| 102 | + put("id", IAM_CLICK_ID) |
| 103 | + put("name", "click_name") |
| 104 | + put("url", "https://www.onesignal.com") |
| 105 | + put("url_target", "webview") |
| 106 | + put("close", true) |
| 107 | + put("pageId", IAM_PAGE_ID) |
| 108 | + put("data", object : JSONObject() { |
| 109 | + init { |
| 110 | + put("test", "value") |
| 111 | + } |
| 112 | + }) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + class OSTestInAppMessageInternal( |
| 119 | + private val jsonObject: JSONObject |
| 120 | + ) { |
| 121 | + private val inAppMessage: InAppMessage by lazy { |
| 122 | + initializeInAppMessage() |
| 123 | + } |
| 124 | + |
| 125 | + private fun initializeInAppMessage(): InAppMessage { |
| 126 | + val time = object : ITime { |
| 127 | + override val currentTimeMillis: Long |
| 128 | + get() = System.currentTimeMillis() |
| 129 | + } |
| 130 | + |
| 131 | + return InAppMessage(jsonObject, time) |
| 132 | + } |
| 133 | + |
| 134 | + val messageId: String |
| 135 | + get() = inAppMessage.messageId |
| 136 | + |
| 137 | + val variants: Map<String, Map<String, String>> |
| 138 | + get() = inAppMessage.variants |
| 139 | + |
| 140 | + val clickedClickIds: MutableSet<String> |
| 141 | + get() = inAppMessage.clickedClickIds |
| 142 | + |
| 143 | + var isDisplayedInSession: Boolean |
| 144 | + get() = inAppMessage.isDisplayedInSession |
| 145 | + set(value) { |
| 146 | + inAppMessage.isDisplayedInSession = value |
| 147 | + } |
| 148 | + |
| 149 | + internal val redisplayStats: InAppMessageRedisplayStats |
| 150 | + get() = inAppMessage.redisplayStats |
| 151 | + |
| 152 | + // Extract limit and delay from the JSON object |
| 153 | + private val redisplayLimit: Int |
| 154 | + get() = jsonObject.optJSONObject("redisplay")?.optInt("limit", -1) ?: -1 |
| 155 | + |
| 156 | + private val redisplayDelay: Long |
| 157 | + get() = jsonObject.optJSONObject("redisplay")?.optLong("delay", -1L) ?: -1L |
| 158 | + |
| 159 | + fun isClickAvailable(clickId: String?): Boolean { |
| 160 | + return !clickedClickIds.contains(clickId) |
| 161 | + } |
| 162 | + |
| 163 | + fun addClickId(clickId: String) { |
| 164 | + clickedClickIds.add(clickId) |
| 165 | + } |
| 166 | + |
| 167 | + fun clearClickIds() { |
| 168 | + clickedClickIds.clear() |
| 169 | + } |
| 170 | + |
| 171 | + override fun toString(): String { |
| 172 | + return "OSTestInAppMessageInternal(jsonObject=$jsonObject, redisplayLimit=$redisplayLimit, redisplayDelay=$redisplayDelay)" |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
0 commit comments