support 'Div2View.setVariable(name, value)' for array variables

commit_hash:276ff6c4ce8bdbaf83c06e9cde40c8eeece4e9d8
This commit is contained in:
gulevsky
2025-02-27 19:21:42 +03:00
parent ed3badedb3
commit 393e81ada7
6 changed files with 262 additions and 5 deletions
+4
View File
@@ -1544,6 +1544,7 @@
"client/android/div/src/test/java/com/yandex/div/core/view2/DivVisibilityActionDispatcherTest.kt":"divkit/public/client/android/div/src/test/java/com/yandex/div/core/view2/DivVisibilityActionDispatcherTest.kt",
"client/android/div/src/test/java/com/yandex/div/core/view2/DivVisibilityActionTrackerTest.kt":"divkit/public/client/android/div/src/test/java/com/yandex/div/core/view2/DivVisibilityActionTrackerTest.kt",
"client/android/div/src/test/java/com/yandex/div/core/view2/GlobalVariableScopesTest.kt":"divkit/public/client/android/div/src/test/java/com/yandex/div/core/view2/GlobalVariableScopesTest.kt",
"client/android/div/src/test/java/com/yandex/div/core/view2/SetVariableValueTest.kt":"divkit/public/client/android/div/src/test/java/com/yandex/div/core/view2/SetVariableValueTest.kt",
"client/android/div/src/test/java/com/yandex/div/core/view2/TestHelpers.kt":"divkit/public/client/android/div/src/test/java/com/yandex/div/core/view2/TestHelpers.kt",
"client/android/div/src/test/java/com/yandex/div/core/view2/VariableUpdatesTest.kt":"divkit/public/client/android/div/src/test/java/com/yandex/div/core/view2/VariableUpdatesTest.kt",
"client/android/div/src/test/java/com/yandex/div/core/view2/animations/DivComparatorTest.kt":"divkit/public/client/android/div/src/test/java/com/yandex/div/core/view2/animations/DivComparatorTest.kt",
@@ -19188,6 +19189,8 @@
"client/web/divkit/tests/hermione/screens/crossplatform/unit/div-text/with_menu_items/firefoxMobile/with_menu_items.png":"divkit/public/client/web/divkit/tests/hermione/screens/crossplatform/unit/div-text/with_menu_items/firefoxMobile/with_menu_items.png",
"client/web/divkit/tests/hermione/screens/crossplatform/unit/div-text/with_set_state_action/chromeMobile/with_set_state_action.png":"divkit/public/client/web/divkit/tests/hermione/screens/crossplatform/unit/div-text/with_set_state_action/chromeMobile/with_set_state_action.png",
"client/web/divkit/tests/hermione/screens/crossplatform/unit/div-text/with_set_state_action/firefoxMobile/with_set_state_action.png":"divkit/public/client/web/divkit/tests/hermione/screens/crossplatform/unit/div-text/with_set_state_action/firefoxMobile/with_set_state_action.png",
"client/web/divkit/tests/hermione/screens/crossplatform/unit/variables/set_value/chromeMobile/set_value.png":"divkit/public/client/web/divkit/tests/hermione/screens/crossplatform/unit/variables/set_value/chromeMobile/set_value.png",
"client/web/divkit/tests/hermione/screens/crossplatform/unit/variables/set_value/firefoxMobile/set_value.png":"divkit/public/client/web/divkit/tests/hermione/screens/crossplatform/unit/variables/set_value/firefoxMobile/set_value.png",
"client/web/divkit/tests/hermione/screens/custom/corners/chromeMobile/corners.png":"divkit/public/client/web/divkit/tests/hermione/screens/custom/corners/chromeMobile/corners.png",
"client/web/divkit/tests/hermione/screens/custom/corners/firefoxMobile/corners.png":"divkit/public/client/web/divkit/tests/hermione/screens/custom/corners/firefoxMobile/corners.png",
"client/web/divkit/tests/hermione/screens/custom/title-with-constant-expression/chromeMobile/title-with-constant-expression.png":"divkit/public/client/web/divkit/tests/hermione/screens/custom/title-with-constant-expression/chromeMobile/title-with-constant-expression.png",
@@ -21608,6 +21611,7 @@
"test_data/unit_test_data/patches/tabs/tabs-patch-transactional-success.json":"divkit/public/test_data/unit_test_data/patches/tabs/tabs-patch-transactional-success.json",
"test_data/unit_test_data/patches/tabs/tabs-success-partial.json":"divkit/public/test_data/unit_test_data/patches/tabs/tabs-success-partial.json",
"test_data/unit_test_data/patches/tabs/tabs-success-transactional.json":"divkit/public/test_data/unit_test_data/patches/tabs/tabs-success-transactional.json",
"test_data/unit_test_data/variables/set_value.json":"divkit/public/test_data/unit_test_data/variables/set_value.json",
"version":"divkit/public/version",
"visual-editor/.eslintrc.base.cjs":"divkit/public/visual-editor/.eslintrc.base.cjs",
"visual-editor/.eslintrc.cjs":"divkit/public/visual-editor/.eslintrc.cjs",
@@ -220,11 +220,7 @@ sealed class Variable {
is ColorVariable -> value = newValue.parseAsColor()
is UrlVariable -> value = newValue.parseAsUri()
is DictVariable -> value = newValue.parseAsJsonObject()
is ArrayVariable -> {
throw VariableMutationException(
"Url action set_variable not allowed for arrays, use property \"typed\" instead"
)
}
is ArrayVariable -> value = newValue.parseAsJsonArray()
}
}
@@ -315,6 +311,14 @@ sealed class Variable {
}
}
private fun String.parseAsJsonArray(): JSONArray {
return try {
JSONArray(this)
} catch (e: JSONException) {
throw VariableMutationException(cause = e)
}
}
fun writeToJSON(): JSONObject {
val serializable: JSONSerializable = when (this) {
is ArrayVariable -> com.yandex.div2.ArrayVariable(this.name, this.value)
@@ -0,0 +1,147 @@
package com.yandex.div.core.view2
import android.app.Activity
import android.widget.TextView
import com.yandex.div.BuildConfig
import com.yandex.div.DivDataTag
import com.yandex.div.core.Div2Context
import com.yandex.div.core.DivConfiguration
import com.yandex.div.data.DivParsingEnvironment
import com.yandex.div.internal.util.textString
import com.yandex.div.json.ParsingErrorLogger
import com.yandex.div2.DivData
import org.json.JSONObject
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.robolectric.Robolectric
import org.robolectric.RobolectricTestRunner
import java.io.File
@RunWith(RobolectricTestRunner::class)
class SetVariableValueTest {
private val activity = Robolectric.buildActivity(Activity::class.java).get()
private val divContext = Div2Context(
baseContext = activity,
lifecycleOwner = null,
configuration = DivConfiguration.Builder(mock()).build()
)
private val divView = Div2View(divContext)
@Before
fun setUp() {
val path = "${BuildConfig.DIV2_JSON_PATH}/unit_test_data/variables/set_value.json"
val testJson = JSONObject(File(path).readText(Charsets.UTF_8))
val environment = DivParsingEnvironment(ParsingErrorLogger.ASSERT)
val divData = DivData(environment, testJson.getJSONObject("card"))
divView.setData(divData, DivDataTag("tag"))
}
@Test
fun `boolean variable updated successfully`() {
assertNull(divView.setVariable("bool_var", "true"))
assertEquals(divView.findViewWithTag<TextView>("bool_var_text").textString, "true")
}
@Test
fun `boolean variable update failed due to type mismatch`() {
assertEquals(
"Variable 'bool_var' mutation failed!",
divView.setVariable("bool_var", "new string value")?.message
)
assertEquals(divView.findViewWithTag<TextView>("bool_var_text").textString, "false")
}
@Test
fun `integer variable updated successfully`() {
assertNull(divView.setVariable("int_var", "1"))
assertEquals(divView.findViewWithTag<TextView>("int_var_text").textString, "1")
}
@Test
fun `integer variable update failed due to type mismatch`() {
assertEquals(
"Variable 'int_var' mutation failed!",
divView.setVariable("int_var", "new string value")?.message
)
assertEquals(divView.findViewWithTag<TextView>("int_var_text").textString, "0")
}
@Test
fun `number variable updated successfully`() {
assertNull(divView.setVariable("number_var", "1.1"))
assertEquals(divView.findViewWithTag<TextView>("number_var_text").textString, "1.1")
}
@Test
fun `number variable update failed due to type mismatch`() {
assertEquals(
"Variable 'number_var' mutation failed!",
divView.setVariable("number_var", "new string value")?.message
)
assertEquals(divView.findViewWithTag<TextView>("number_var_text").textString, "0.1")
}
@Test
fun `string variable updated successfully`() {
assertNull(divView.setVariable("string_var", "new value"))
assertEquals(divView.findViewWithTag<TextView>("string_var_text").textString, "new value")
}
@Test
fun `color variable updated successfully`() {
assertNull(divView.setVariable("color_var", "#FFFFFF"))
assertEquals(divView.findViewWithTag<TextView>("color_var_text").textString, "#FFFFFFFF")
}
@Test
fun `color variable update failed due to type mismatch`() {
assertEquals(
"Variable 'color_var' mutation failed!",
divView.setVariable("color_var", "new string value")?.message
)
assertEquals(divView.findViewWithTag<TextView>("color_var_text").textString, "#FF000000")
}
@Test
fun `url variable updated successfully`() {
assertNull(divView.setVariable("url_var", "https://new/url"))
assertEquals(divView.findViewWithTag<TextView>("url_var_text").textString, "https://new/url")
}
@Test
fun `dict variable updated successfully`() {
assertNull(divView.setVariable("dict_var", "{\"value\":\"new value\"}"))
assertEquals(divView.findViewWithTag<TextView>("dict_var_text").textString, "{\"value\":\"new value\"}")
}
@Test
fun `dict variable update failed due to type mismatch`() {
assertEquals(
"Variable 'dict_var' mutation failed!",
divView.setVariable("dict_var", "new string value")?.message
)
assertEquals(divView.findViewWithTag<TextView>("dict_var_text").textString, "{\"value\":\"initial value\"}")
}
@Test
fun `array variable updated successfully`() {
assertNull(divView.setVariable("array_var", "[\"new value\"]"))
assertEquals(divView.findViewWithTag<TextView>("array_var_text").textString, "[\"new value\"]")
}
@Test
fun `array variable update failed due to type mismatch`() {
assertEquals(
"Variable 'array_var' mutation failed!",
divView.setVariable("array_var", "new string value")?.message
)
assertEquals(divView.findViewWithTag<TextView>("array_var_text").textString, "[\"initial value\"]")
}
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

@@ -0,0 +1,102 @@
{
"card": {
"log_id": "test_card",
"variables": [
{
"name": "bool_var",
"type": "boolean",
"value": false
},
{
"name": "int_var",
"type": "integer",
"value": 0
},
{
"name": "number_var",
"type": "number",
"value": 0.1
},
{
"name": "string_var",
"type": "string",
"value": "initial value"
},
{
"name": "color_var",
"type": "color",
"value": "#000000"
},
{
"name": "url_var",
"type": "url",
"value": "https://initial/url"
},
{
"name": "dict_var",
"type": "dict",
"value": {
"value": "initial value"
}
},
{
"name": "array_var",
"type": "array",
"value": [
"initial value"
]
}
],
"states": [
{
"state_id": 0,
"div": {
"type": "container",
"orientation": "vertical",
"items": [
{
"type": "text",
"id": "bool_var_text",
"text": "@{bool_var}"
},
{
"type": "text",
"id": "int_var_text",
"text": "@{int_var}"
},
{
"type": "text",
"id": "number_var_text",
"text": "@{number_var}"
},
{
"type": "text",
"id": "string_var_text",
"text": "@{string_var}"
},
{
"type": "text",
"id": "color_var_text",
"text": "@{color_var}"
},
{
"type": "text",
"id": "url_var_text",
"text": "@{url_var}"
},
{
"type": "text",
"id": "dict_var_text",
"text": "@{dict_var}"
},
{
"type": "text",
"id": "array_var_text",
"text": "@{array_var}"
}
]
}
}
]
}
}