-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
ViewComponentTest.kt
249 lines (177 loc) · 6.31 KB
/
ViewComponentTest.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* FXGL - JavaFX Game Library. The MIT License (MIT).
* Copyright (c) AlmasB (almaslvl@gmail.com).
* See LICENSE for details.
*/
@file:Suppress("JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE")
package com.almasb.fxgl.entity.components
import com.almasb.fxgl.core.View
import com.almasb.fxgl.entity.Entity
import com.almasb.fxgl.entity.component.ComponentHelper
import javafx.event.EventHandler
import javafx.scene.Node
import javafx.scene.Parent
import javafx.scene.input.MouseButton
import javafx.scene.input.MouseEvent
import javafx.scene.shape.Circle
import javafx.scene.shape.Rectangle
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream
class ViewComponentTest {
private lateinit var view: ViewComponent
@BeforeEach
fun setUp() {
view = ViewComponent()
}
@ParameterizedTest
@MethodSource("childProvider")
fun `Add and remove children`(node: Node) {
assertThat(view.children.size, `is`(0))
view.addChild(node)
assertThat(view.children.size, `is`(1))
assertThat(view.children[0], `is`<Node>(node))
view.removeChild(node)
assertThat(view.children.size, `is`(0))
}
@Test
fun `Add children with and without transforms`() {
assertThat(view.children.size, `is`(0))
val rect = Rectangle()
val rect2 = Rectangle()
view.addChild(rect, isTransformApplied = true)
view.addChild(rect2, isTransformApplied = false)
val e = Entity()
ComponentHelper.setEntity(view, e)
view.onAdded()
assertThat(rect.parent.transforms.size, `is`(4))
assertThat(rect2.parent.transforms.size, `is`(0))
}
@Test
fun `Get child with cast`() {
val rect = Rectangle()
val circle = Circle()
view.addChild(rect)
view.addChild(circle)
val rect2: Rectangle = view.getChild(0, Rectangle::class.java)
val circle2: Circle = view.getChild(1, Circle::class.java)
assertThat(rect2, `is`(rect))
assertThat(circle2, `is`(circle))
}
@Test
fun `View type children are correctly updated onUpdate and disposed on remove`() {
val child = TestView()
view.addChild(child)
assertFalse(child.isDisposed)
assertFalse(child.isUpdated)
view.onUpdate(0.016)
assertFalse(child.isDisposed)
assertTrue(child.isUpdated)
view.onRemoved()
assertTrue(child.isDisposed)
}
@Test
fun `Clear children`() {
assertThat(view.children.size, `is`(0))
val child = TestView()
val child2 = TestView()
view.addChild(child)
view.addChild(child2, false)
view.addChild(Rectangle())
view.addChild(Rectangle(), false)
assertThat(view.children.size, `is`(4))
assertFalse(child.isDisposed)
assertFalse(child2.isDisposed)
view.clearChildren()
assertThat(view.children.size, `is`(0))
assertTrue(child.isDisposed)
assertTrue(child2.isDisposed)
}
@Test
fun `Opacity is set to node only and not parent`() {
val rect = Rectangle()
view.addChild(rect)
assertThat(view.opacity, `is`(1.0))
view.opacity = 0.35
assertThat(view.opacity, `is`(0.35))
assertThat(view.parent.opacity, `is`(1.0))
}
@Test
fun `Visibility is set to view and the parent`() {
assertTrue(view.isVisible)
assertTrue(view.parent.isVisible)
assertTrue(view.visibleProperty.value)
view.isVisible = false
assertFalse(view.isVisible)
assertFalse(view.parent.isVisible)
assertFalse(view.visibleProperty.value)
}
@Test
fun `Add remove general click listener`() {
var count = 0
val l = EventHandler<MouseEvent> { count++ }
view.addEventHandler(MouseEvent.MOUSE_CLICKED, l)
val e0 = MouseEvent(MouseEvent.MOUSE_PRESSED, 0.0, 0.0, 0.0, 0.0, MouseButton.PRIMARY, 1,
false, false, false,
false, false, false, false, false, false, false, null)
val e1 = MouseEvent(MouseEvent.MOUSE_CLICKED, 0.0, 0.0, 0.0, 0.0, MouseButton.PRIMARY, 1,
false, false, false,
false, false, false, false, false, false, false, null)
assertThat(count, `is`(0))
// PRESS does not trigger click
view.parent.fireEvent(e0)
assertThat(count, `is`(0))
view.parent.fireEvent(e1)
assertThat(count, `is`(1))
view.removeEventHandler(MouseEvent.MOUSE_CLICKED, l)
view.parent.fireEvent(e1)
assertThat(count, `is`(1))
}
@Test
fun `Add remove on click listener`() {
var count = 0
val l = EventHandler<MouseEvent> { count++ }
view.addOnClickHandler(l)
val e0 = MouseEvent(MouseEvent.MOUSE_PRESSED, 0.0, 0.0, 0.0, 0.0, MouseButton.PRIMARY, 1,
false, false, false,
false, false, false, false, false, false, false, null)
val e1 = MouseEvent(MouseEvent.MOUSE_CLICKED, 0.0, 0.0, 0.0, 0.0, MouseButton.PRIMARY, 1,
false, false, false,
false, false, false, false, false, false, false, null)
assertThat(count, `is`(0))
// PRESS does not trigger click
view.parent.fireEvent(e0)
assertThat(count, `is`(0))
view.parent.fireEvent(e1)
assertThat(count, `is`(1))
view.removeOnClickHandler(l)
view.parent.fireEvent(e1)
assertThat(count, `is`(1))
}
companion object {
@Suppress("UNUSED")
@JvmStatic
fun childProvider(): Stream<Node> {
return Stream.of(Rectangle(), TestView())
}
}
private class TestView : Parent(), View {
var isDisposed = false
var isUpdated = false
override fun onUpdate(tpf: Double) {
isUpdated = true
}
override fun getNode(): Node {
return this
}
override fun dispose() {
isDisposed = true
}
}
}