1
1
package app.rive.runtime.kotlin.core
2
2
3
3
import androidx.test.ext.junit.runners.AndroidJUnit4
4
+ import androidx.test.internal.runner.junit4.statement.UiThreadStatement
5
+ import app.rive.runtime.kotlin.RiveAnimationView
6
+ import app.rive.runtime.kotlin.core.errors.RiveException
4
7
import app.rive.runtime.kotlin.core.errors.TextValueRunException
5
8
import app.rive.runtime.kotlin.test.R
6
9
import org.junit.Assert.assertEquals
10
+ import org.junit.Assert.assertTrue
11
+ import org.junit.Assert.fail
7
12
import org.junit.Before
8
13
import org.junit.Test
9
14
import org.junit.runner.RunWith
@@ -14,25 +19,162 @@ class RiveTextValueRunTest {
14
19
private val testUtils = TestUtils ()
15
20
private val appContext = testUtils.context
16
21
private lateinit var file: File
22
+ private lateinit var nestePathFile: File
23
+ private lateinit var mockView: RiveAnimationView
17
24
18
25
@Before
19
26
fun init () {
20
27
file = File (
21
28
appContext.resources.openRawResource(R .raw.hello_world_text).readBytes()
22
29
)
30
+ nestePathFile= File (
31
+ appContext.resources.openRawResource(R .raw.runtime_nested_text_runs).readBytes()
32
+ )
33
+
34
+ mockView = TestUtils .MockRiveAnimationView (appContext)
23
35
}
24
36
25
37
@Test
26
38
fun read_and_update_text_run () {
27
- val textRun = file.firstArtboard.textRun(" name" )
39
+ var artboard = file.firstArtboard;
40
+ assertEquals(artboard.dependencies.count(), 0 )
41
+
42
+ val textRun = artboard.textRun(" name" )
43
+
44
+ // Confirm original value
28
45
assertEquals(" world" , textRun.text)
46
+ assertEquals(" world" , artboard.getTextRunValue(" name" ))
47
+
29
48
var updateValue = " username"
49
+
50
+ // Setting the text run directly
30
51
textRun.text = updateValue
31
52
assertEquals(updateValue, textRun.text)
53
+ assertEquals(updateValue, artboard.getTextRunValue(" name" ))
54
+
55
+ // Setting through the helper method
56
+ updateValue = " new value"
57
+ artboard.setTextRunValue(" name" ,updateValue)
58
+ assertEquals(updateValue, textRun.text)
59
+ assertEquals(updateValue, artboard.getTextRunValue(" name" ))
60
+
61
+ // Only accessing .textRun should add to the dependencies.
62
+ assertEquals(artboard.dependencies.count(), 1 )
32
63
}
33
64
34
65
@Test(expected = TextValueRunException ::class )
35
66
fun read_non_existing_text_run () {
36
67
file.firstArtboard.textRun(" wrong-name" )
37
68
}
69
+
70
+ @Test
71
+ fun read_and_update_text_run_at_path () {
72
+ val artboard = nestePathFile.firstArtboard;
73
+ assertEquals(artboard.dependencies.count(), 0 )
74
+
75
+ nestedTextRunHelper(artboard, " ArtboardBRun" , " ArtboardB-1" ," Artboard B Run" , " ArtboardB-1" )
76
+ nestedTextRunHelper(artboard, " ArtboardBRun" , " ArtboardB-2" ," Artboard B Run" , " ArtboardB-2" )
77
+ nestedTextRunHelper(artboard, " ArtboardCRun" , " ArtboardB-1/ArtboardC-1" ," Artboard C Run" , " ArtboardB-1/C-1" )
78
+ nestedTextRunHelper(artboard, " ArtboardCRun" , " ArtboardB-1/ArtboardC-2" ," Artboard C Run" , " ArtboardB-1/C-2" )
79
+ nestedTextRunHelper(artboard, " ArtboardCRun" , " ArtboardB-2/ArtboardC-1" ," Artboard C Run" , " ArtboardB-2/C-1" )
80
+ nestedTextRunHelper(artboard, " ArtboardCRun" , " ArtboardB-2/ArtboardC-2" ," Artboard C Run" , " ArtboardB-2/C-2" )
81
+
82
+ // Only accessing the textRun directly should increase the dependency.
83
+ // Calling getTextRunValue and setTextRunValue should not.
84
+ assertEquals(artboard.dependencies.count(), 6 )
85
+ }
86
+
87
+ private fun nestedTextRunHelper (artboard : Artboard , name : String , path : String , originalValue : String , updatedValue : String ) {
88
+ // Get the text value run. This should increase the dependency count
89
+ val textRun = artboard.textRun(name, path = path)
90
+
91
+ // Assert the original value is correct
92
+ assertEquals(originalValue, textRun.text)
93
+ assertEquals(originalValue, artboard.getTextRunValue(name, path = path))
94
+
95
+ // Update the `textRun` reference directly
96
+ textRun.text = updatedValue
97
+ assertEquals(updatedValue, textRun.text)
98
+ assertEquals(updatedValue, artboard.getTextRunValue(name, path = path))
99
+
100
+ // Update the text run back to the original value through the helper method
101
+ artboard.setTextRunValue(name, originalValue, path)
102
+ assertEquals(originalValue, textRun.text)
103
+ assertEquals(originalValue, artboard.getTextRunValue(name, path = path))
104
+ }
105
+
106
+ @Test
107
+ fun viewSetGetTextRun () {
108
+ UiThreadStatement .runOnUiThread {
109
+ mockView.setRiveResource(R .raw.hello_world_text, autoplay = false )
110
+ mockView.play(listOf (" State Machine 1" ), areStateMachines = true )
111
+ assertEquals(true , mockView.isPlaying)
112
+
113
+ assertEquals(mockView.controller.activeArtboard?.dependencies?.count(), 1 )
114
+ val textValue = mockView.getTextRunValue(" name" )
115
+ assertEquals(textValue, " world" )
116
+
117
+ var newValue = " New Value" ;
118
+ mockView.setTextRunValue(" name" , newValue)
119
+ val textValueUpdated = mockView.getTextRunValue(" name" )
120
+ assertEquals(textValueUpdated, newValue)
121
+
122
+ assertEquals(mockView.controller.activeArtboard?.dependencies?.count(), 1 )
123
+
124
+ // Test for throwing an error when giving a wrong text run name
125
+ try {
126
+ mockView.setTextRunValue(" non_existent_text_run" , " Some Value" )
127
+ fail(" Expected an exception to be thrown" )
128
+ } catch (e: Exception ) {
129
+ assertTrue(e is RiveException )
130
+ assertTrue(e.message?.contains(" No Rive TextValueRun found" ) == true )
131
+ }
132
+
133
+ // Test for throwing an error when giving a wrong text run name for a nested artboard
134
+ try {
135
+ mockView.setTextRunValue(" non_existent_text_run" , " Some Value" , " ArtboardB-1" )
136
+ fail(" Expected an exception to be thrown" )
137
+ } catch (e: Exception ) {
138
+ assertTrue(e is RiveException )
139
+ assertTrue(e.message?.contains(" No Rive TextValueRun found" ) == true )
140
+ }
141
+ }
142
+ }
143
+ @Test
144
+ fun viewSetGetNestedTextRun () {
145
+ UiThreadStatement .runOnUiThread {
146
+ mockView.setRiveResource(R .raw.runtime_nested_text_runs, autoplay = false )
147
+ mockView.play(listOf (" State Machine 1" ), areStateMachines = true )
148
+ assertEquals(true , mockView.isPlaying)
149
+
150
+ assertEquals(mockView.controller.activeArtboard?.dependencies?.count(), 1 )
151
+ val textValue = mockView.getTextRunValue(" ArtboardBRun" , " ArtboardB-1" )
152
+ assertEquals(textValue, " Artboard B Run" )
153
+
154
+ var newValue = " New Value" ;
155
+ mockView.setTextRunValue(" ArtboardBRun" , newValue, " ArtboardB-1" )
156
+ val textValueUpdated = mockView.getTextRunValue(" ArtboardBRun" , " ArtboardB-1" )
157
+ assertEquals(textValueUpdated, newValue)
158
+
159
+ assertEquals(mockView.controller.activeArtboard?.dependencies?.count(), 1 )
160
+
161
+ // Test for throwing an error when giving a wrong text run name for a nested artboard
162
+ try {
163
+ mockView.setTextRunValue(" non_existent_text_run" , " Some Value" , " ArtboardB-1" )
164
+ fail(" Expected an exception to be thrown" )
165
+ } catch (e: Exception ) {
166
+ assertTrue(e is RiveException )
167
+ assertTrue(e.message?.contains(" No Rive TextValueRun found" ) == true )
168
+ }
169
+
170
+ // Test for throwing an error when giving a wrong path for a nested artboard
171
+ try {
172
+ mockView.setTextRunValue(" ArtboardBRun" , " Some Value" , " non_existent_path" )
173
+ fail(" Expected an exception to be thrown" )
174
+ } catch (e: Exception ) {
175
+ assertTrue(e is RiveException )
176
+ assertTrue(e.message?.contains(" No Rive TextValueRun found" ) == true )
177
+ }
178
+ }
179
+ }
38
180
}
0 commit comments