-
-
Notifications
You must be signed in to change notification settings - Fork 540
/
AssetLoaderServiceTest.kt
496 lines (363 loc) · 15.6 KB
/
AssetLoaderServiceTest.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* 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.app
import com.almasb.fxgl.app.services.FXGLAssetLoaderService
import com.almasb.fxgl.audio.AudioPlayer
import com.almasb.fxgl.core.asset.AssetType
import com.almasb.fxgl.logging.ConsoleOutput
import com.almasb.fxgl.logging.Logger
import com.almasb.fxgl.logging.LoggerConfig
import com.almasb.fxgl.logging.LoggerLevel
import com.almasb.fxgl.test.InjectInTest
import com.almasb.fxgl.test.RunWithFX
import com.almasb.fxgl.texture.getDummyImage
import com.almasb.fxgl.ui.UIController
import org.hamcrest.CoreMatchers.*
import org.hamcrest.MatcherAssert.assertThat
import org.junit.jupiter.api.Assertions.*
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import java.lang.ClassCastException
import java.lang.invoke.MethodHandles
/**
*
*
* @author Almas Baimagambetov (almaslvl@gmail.com)
*/
@ExtendWith(RunWithFX::class)
class AssetLoaderServiceTest {
private val TEXT_ASSETS = arrayOf("test1.txt")
private val TEXT_DATA = arrayOf("Lorem ipsum dolor sit amet, consectetuer adipiscing elit.\n" +
"Aenean commodo ligula eget dolor.\n" +
"Aenean massa.\n" +
"Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.\n" +
"Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem.\n" +
"Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.\n" +
"In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo.\n" +
"Nullam dictum felis eu pede mollis pretium.\n" +
"Integer tincidunt.\n" +
"Cras dapibus.\n" +
"Vivamus elementum semper nisi.\n" +
"Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim.\n" +
"Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet.\n" +
"Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui.\n" +
"Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing\n" +
"sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante\n" +
"tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet\n" +
"rci eget eros faucibus tincidunt")
private lateinit var assetLoader: FXGLAssetLoaderService
@BeforeEach
fun setUp() {
assetLoader = FXGLAssetLoaderService()
InjectInTest.inject(MethodHandles.lookup(), assetLoader, "audioService", AudioPlayer())
InjectInTest.inject(MethodHandles.lookup(), assetLoader, "userAppClass", AssetLoaderServiceTest::class.java)
assetLoader.onInit()
}
@Test
fun loadImage() {
var image = assetLoader.loadImage("brick.png")
assertThat(image.width, `is`(64.0))
assertThat(image.height, `is`(64.0))
image = assetLoader.loadImage("bla-bla")
assertThat(image, `is`(notNullValue()))
}
@Test
fun `Load image from URL`() {
val image = assetLoader.loadImage(javaClass.getResource("test_icon.png"))
assertThat(image.width, `is`(256.0))
assertThat(image.height, `is`(256.0))
}
@Test
fun `URL based loads are cached`() {
val image1 = assetLoader.loadImage(javaClass.getResource("test_icon.png"))
val image2 = assetLoader.loadImage(javaClass.getResource("test_icon.png"))
assertThat(image1, `is`(not(getDummyImage())))
assertTrue(image1 === image2)
}
@Test
fun loadTexture() {
var texture = assetLoader.loadTexture("brick.png")
assertThat(texture.image.width, `is`(64.0))
assertThat(texture.image.height, `is`(64.0))
texture = assetLoader.loadTexture("bla-bla")
assertThat(texture, `is`(notNullValue()))
}
@Test
fun `Load texture from URL`() {
val texture = assetLoader.loadTexture(javaClass.getResource("test_icon.png"))
assertThat(texture.image.width, `is`(256.0))
assertThat(texture.image.height, `is`(256.0))
}
@Test
fun loadResizedTexture() {
var texture = assetLoader.loadTexture("brick.png", 32.0, 32.0)
assertThat(texture.image.width, `is`(32.0))
assertThat(texture.image.height, `is`(32.0))
assertTrue(texture.image === assetLoader.loadTexture("brick.png", 32.0, 32.0).image)
texture.dispose()
texture = assetLoader.loadTexture("bla-bla", 32.0, 32.0)
assertThat(texture, `is`(notNullValue()))
}
@Test
fun loadSound() {
var sound = assetLoader.loadSound("intro.wav")
assertThat(sound, `is`(notNullValue()))
assertTrue(sound === assetLoader.loadSound("intro.wav"))
sound = assetLoader.loadSound("bla-bla")
assertThat(sound, `is`(notNullValue()))
}
@Test
fun `Load sound from URL`() {
val sound = assetLoader.loadSound(javaClass.getResource("/fxglassets/sounds/intro.wav"))
assertThat(sound, `is`(notNullValue()))
}
@Test
fun loadMusic() {
// Note: the loading might fail on linux if missing libavformat for jfxmedia, but dummy music object should be loaded
var music = assetLoader.loadMusic("intro.mp3")
assertThat(music, `is`(notNullValue()))
music.dispose()
music = assetLoader.loadMusic("bla-bla")
assertThat(music, `is`(notNullValue()))
}
@Test
fun `Load music from URL`() {
val music = assetLoader.loadMusic(javaClass.getResource("/fxglassets/music/intro.mp3"))
assertThat(music, `is`(notNullValue()))
}
@Test
fun loadText() {
for (i in TEXT_ASSETS.indices) {
val textAsset = TEXT_ASSETS[i]
val actualLines = assetLoader.loadText(textAsset)
val expectedLines = TEXT_DATA[i].split("\n".toRegex()).dropLastWhile { it.isEmpty() }
assertThat(actualLines, `is`(expectedLines))
}
val lines = assetLoader.loadText("bla-bla")
assertThat(lines.size, `is`(0))
// ensure a new copy of the list is loaded so that original cannot be modified
assertTrue(assetLoader.loadText("test1.txt") !== assetLoader.loadText("test1.txt"))
}
@Test
fun `Load Text from URL`() {
val actualLines = assetLoader.loadText(javaClass.getResource("/fxglassets/text/test1.txt"))
val expectedLines = TEXT_DATA[0].split("\n".toRegex()).dropLastWhile { it.isEmpty() }
assertThat(actualLines, `is`(expectedLines))
val lines = assetLoader.loadText("bla-bla")
assertThat(lines.size, `is`(0))
}
@Test
fun `Relative and absolute URL load calls cache the same object`() {
// these are loaded from two different places (assets/text and fxglassets/text), so shouldn't be same object
assertTrue(assetLoader.loadText("test1.txt") !== assetLoader.loadText(javaClass.getResource("/fxglassets/text/test1.txt")))
// these are loaded from the same place, so should be same object
assertTrue(assetLoader.loadImage("brick2.png") === assetLoader.loadImage(javaClass.getResource("/fxglassets/textures/brick2.png")))
}
@Test
fun `Load DialogueGraph`() {
val graph = assetLoader.loadDialogueGraph("simple.json")
assertThat(graph.nodes.size, `is`(3))
assertThat(graph.edges.size, `is`(2))
assertThat(graph.startNode.text, `is`("Simple start."))
}
@Test
fun `Load DialogueGraph from URL`() {
val graph = assetLoader.loadDialogueGraph(javaClass.getResource("/fxglassets/dialogues/simple.json"))
assertThat(graph.nodes.size, `is`(3))
assertThat(graph.edges.size, `is`(2))
assertThat(graph.startNode.text, `is`("Simple start."))
}
@Test
fun loadResourceBundle() {
val resourceBundle = assetLoader.loadResourceBundle("test.properties")
val resourceBundle2 = assetLoader.loadResourceBundle("test.properties")
assertTrue(resourceBundle === resourceBundle2)
assertThat(resourceBundle, `is`(notNullValue()))
assertThat(resourceBundle.getString("testKey"), `is`("testValue"))
val bundle = assetLoader.loadResourceBundle("bla-bla")
assertThat(bundle, `is`(notNullValue()))
assertThat(bundle.keySet().size, `is`(0))
}
@Test
fun `Load ResourceBundle from URL`() {
val resourceBundle = assetLoader.loadResourceBundle(javaClass.getResource("/fxglassets/properties/test.properties"))
assertThat(resourceBundle, `is`(notNullValue()))
assertThat(resourceBundle.getString("testKey"), `is`("testValue"))
}
@Test
fun `Load PropertyMap`() {
var map = assetLoader.loadPropertyMap("properties/test.properties")
assertThat(map, `is`(notNullValue()))
assertThat(map.getString("testKey"), `is`("testValue"))
map = assetLoader.loadPropertyMap("bla-bla")
assertThat(map, `is`(notNullValue()))
assertThat(map.keys().size, `is`(0))
}
@Test
fun `Load PropertyMap from URL`() {
val map = assetLoader.loadPropertyMap(javaClass.getResource("/fxglassets/properties/test.properties"))
assertThat(map, `is`(notNullValue()))
assertThat(map.getString("testKey"), `is`("testValue"))
}
@Test
fun loadCursorImage() {
var cursorImage = assetLoader.loadCursorImage("test_cursor.png")
assertThat(cursorImage, `is`(notNullValue()))
assertThat(cursorImage.width, `is`(64.0))
assertThat(cursorImage.height, `is`(64.0))
cursorImage = assetLoader.loadCursorImage("bla-bla")
assertThat(cursorImage, `is`(notNullValue()))
}
@Test
fun loadUI() {
var count = 0
val controller = object : UIController {
override fun init() {
count++
}
}
var ui = assetLoader.loadUI("test_ui.fxml", controller)
assertThat(ui, `is`(notNullValue()))
assertThat(ui.root, `is`(notNullValue()))
assertThat(count, `is`(1))
// UI objects are not cached
assertFalse(assetLoader.loadUI("test_ui.fxml", controller) === ui)
assertThat(count, `is`(2))
ui = assetLoader.loadUI("bla-bla", controller)
assertThat(ui, `is`(notNullValue()))
assertThat(ui.root, `is`(notNullValue()))
assertThat(count, `is`(2))
}
@Test
fun `Load UI from URL`() {
val ui = assetLoader.loadUI(javaClass.getResource("/fxglassets/ui/test_ui.fxml"), object : UIController {
override fun init() {
}
})
assertThat(ui, `is`(notNullValue()))
assertThat(ui.root, `is`(notNullValue()))
}
@Test
fun loadCSS() {
var css = assetLoader.loadCSS("test.css")
assertThat(css, `is`(notNullValue()))
css = assetLoader.loadCSS("bla-bla")
assertThat(css, `is`(notNullValue()))
}
@Test
fun `Load CSS from URL`() {
val css = assetLoader.loadCSS(javaClass.getResource("/fxglassets/ui/css/test.css"))
assertThat(css, `is`(notNullValue()))
}
@Test
fun loadFont() {
var fontFactory = assetLoader.loadFont("test.ttf")
assertThat(fontFactory, `is`(notNullValue()))
val fontFactory2 = assetLoader.loadFont("test.ttf")
assertTrue(fontFactory === fontFactory2)
val font = fontFactory.newFont(18.0)
assertThat(font, `is`(notNullValue()))
assertThat(font.family, `is`("Elektra"))
assertThat(font.name, `is`("Elektra"))
assertThat(font.size, `is`(18.0))
fontFactory = assetLoader.loadFont("bla-bla")
assertThat(fontFactory, `is`(notNullValue()))
assertThat(fontFactory.newFont(18.0), `is`(notNullValue()))
}
@Test
fun `Load Font from URL`() {
val fontFactory = assetLoader.loadFont(javaClass.getResource("/fxglassets/ui/fonts/test.ttf"))
assertThat(fontFactory, `is`(notNullValue()))
val font = fontFactory.newFont(18.0)
assertThat(font, `is`(notNullValue()))
assertThat(font.family, `is`("Elektra"))
assertThat(font.name, `is`("Elektra"))
assertThat(font.size, `is`(18.0))
}
@Test
fun `Load JSON`() {
var obj = assetLoader.loadJSON("json/test.json", TestJSONData::class.java)
assertTrue(obj.isPresent)
assertThat(obj.get().name, `is`("TestName"))
assertThat(obj.get().value, `is`(892))
// invalid URL
obj = assetLoader.loadJSON("bla-bla", TestJSONData::class.java)
assertFalse(obj.isPresent)
// try to load as diff incompatible object
val obj2 = assetLoader.loadJSON("json/test.json", String::class.java)
assertFalse(obj2.isPresent)
}
@Test
fun `Load JSON from URL`() {
val obj = assetLoader.loadJSON(javaClass.getResource("/fxglassets/json/test.json"), TestJSONData::class.java)
assertTrue(obj.isPresent)
assertThat(obj.get().name, `is`("TestName"))
assertThat(obj.get().value, `is`(892))
}
@Test
fun `Load obj Model3D`() {
val model = assetLoader.loadModel3D("cube.obj")
assertThat(model, `is`(notNullValue()))
}
@Test
fun `Load obj Model3D from URL`() {
val model = assetLoader.loadModel3D(javaClass.getResource("/fxglassets/models/cube.obj"))
assertThat(model, `is`(notNullValue()))
}
@Test
fun getStream() {
val stream = assetLoader.getStream("/assets/scripts/test.js")
assertThat(stream, `is`(notNullValue()))
stream.close()
}
@Test
fun `getStream throws if no valid stream`() {
assertThrows(IllegalArgumentException::class.java) {
assetLoader.getStream("bla-bla")
}
}
@Test
fun `Check loaded from cache when present`() {
// ensure cache is clean
assetLoader.clearCache()
val texture = assetLoader.loadTexture("brick.png")
val texture2 = assetLoader.loadTexture("brick.png")
assertThat(texture2.image, `is`(texture.image))
assetLoader.clearCache()
val texture3 = assetLoader.loadTexture("brick.png")
assertThat(texture3.image, `is`(not(texture.image)))
texture.dispose()
texture2.dispose()
texture3.dispose()
assetLoader.clearCache()
}
@Test
fun `load throws ClassCastException if T does not match asset type`() {
assertThrows(ClassCastException::class.java) {
// try to load Image as String
val s: String = assetLoader.load(AssetType.IMAGE, "brick.png")
}
}
@Test
fun `Loading a valid resource in wrong format does not crash`() {
// try loading txt as font object
val font = assetLoader.loadFont(javaClass.getResource("/fxglassets/text/test1.txt"))
assertThat(font, `is`(notNullValue()))
// try loading txt as Image
val image1 = assetLoader.loadImage(javaClass.getResource("/fxglassets/text/test_level.txt"))
assertThat(image1, `is`(notNullValue()))
// try loading txt as Texture
val texture = assetLoader.loadTexture(javaClass.getResource("/fxglassets/text/test_level.txt"), 64.0, 64.0)
assertThat(texture, `is`(notNullValue()))
}
class TestJSONData
@JvmOverloads constructor(
var name: String = "",
var value: Int = 0
)
}