-
Notifications
You must be signed in to change notification settings - Fork 54
/
RiveDelegatesTest.swift
336 lines (286 loc) · 13.2 KB
/
RiveDelegatesTest.swift
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
//
// RiveDelegatesTest.swift
// RiveRuntimeTests
//
// Created by Maxwell Talbot on 12/05/2021.
// Copyright © 2021 Rive. All rights reserved.
//
import XCTest
import RiveRuntime
extension RiveFile {
convenience init(testfileName: String, extension ext: String = ".riv") throws {
let byteArray = RiveFile.getBytes(fileName: testfileName, extension: ext)
try self.init(byteArray: byteArray)
}
static func getBytes(fileName: String, extension ext: String = ".riv") -> [UInt8] {
guard let url = Bundle(for: DelegatesTest.self).url(forResource: fileName, withExtension: ext) else {
fatalError("Failed to locate \(fileName) in bundle.")
}
guard let data = try? Data(contentsOf: url) else {
fatalError("Failed to load \(url) from bundle.")
}
// Import the data into a RiveFile
return [UInt8](data)
}
}
class DrDelegate: RivePlayerDelegate, RiveStateMachineDelegate {
var stateMachinePlays = [String]()
var stateMachinePauses = [String]()
var stateMachineStops = [String]()
var linearAnimaitonPlays = [String]()
var linearAnimaitonPauses = [String]()
var linearAnimaitonStops = [String]()
var loops = [String]()
var stateMachineNames = [String]()
var stateMachineStates = [String]()
func player(playedWithModel riveModel: RiveModel?) {
if let stateMachineName = riveModel?.stateMachine?.name() {
stateMachinePlays.append(stateMachineName)
}
else if let animationName = riveModel?.animation?.name() {
linearAnimaitonPlays.append(animationName)
}
}
func player(pausedWithModel riveModel: RiveModel?) {
if let stateMachineName = riveModel?.stateMachine?.name() {
stateMachinePauses.append(stateMachineName)
}
else if let animationName = riveModel?.animation?.name() {
linearAnimaitonPauses.append(animationName)
}
}
func player(loopedWithModel riveModel: RiveModel?, type: Int) {
if let stateMachineName = riveModel?.stateMachine?.name() {
loops.append(stateMachineName)
}
else if let animationName = riveModel?.animation?.name() {
loops.append(animationName)
}
}
func player(stoppedWithModel riveModel: RiveModel?) {
if let stateMachineName = riveModel?.stateMachine?.name() {
stateMachineStops.append(stateMachineName)
}
else if let animationName = riveModel?.animation?.name() {
linearAnimaitonStops.append(animationName)
}
}
func player(didAdvanceby seconds: Double, riveModel: RiveModel?) { }
func stateMachine(_ stateMachine: RiveStateMachineInstance, didChangeState stateName: String) {
stateMachineNames.append(stateMachine.name())
stateMachineStates.append(stateName)
}
}
// Technical Note:
// We manually call view.advance(0) in these tests because the results are based on
// messages received by a delegate which is triggered by a timer. We can't wait for
// the timer so we trigger it manually
class DelegatesTest: XCTestCase {
func testPlay() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
viewModel.play(animationName: "one")
// This is necessary because play starts a timer that will eventually get to
// .advance(n) which triggers the event queue which sends word to the delegate...
// But the assert happens too fast, so we need to advance manually beforehand.
view.advance(delta: 0)
XCTAssertEqual(delegate.linearAnimaitonPlays.count, 1)
}
func testPlayTwice() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
viewModel.play(animationName: "one")
viewModel.play(animationName: "one")
view.advance(delta: 0)
XCTAssertEqual(delegate.linearAnimaitonPlays.count, 2)
}
func testPause() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
viewModel.play(animationName: "one")
viewModel.pause()
view.advance(delta: 0)
XCTAssertEqual(delegate.linearAnimaitonPauses.count, 1)
}
func testPauseWhenNotPlaying() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
viewModel.pause()
view.advance(delta: 0)
XCTAssertEqual(delegate.linearAnimaitonPauses.count, 0)
}
func testStop() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
viewModel.play(animationName: "one")
viewModel.stop()
view.advance(delta: 0)
XCTAssertEqual(delegate.linearAnimaitonStops.count, 1)
}
func testStopNotMounted() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, animationName: "one", autoPlay: false)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
viewModel.stop()
view.advance(delta: 0)
XCTAssertEqual(delegate.linearAnimaitonStops.count, 1)
}
func testStopPaused() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
viewModel.play(animationName: "one")
viewModel.pause()
viewModel.stop()
view.advance(delta: 0)
viewModel.riveView?.advance(delta: 0)
XCTAssertEqual(delegate.linearAnimaitonStops.count, 1)
}
func testLoopOneShot() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
viewModel.play(animationName: "one", loop: .oneShot)
view.advance(delta: Double(viewModel.riveModel!.animation!.effectiveDurationInSeconds()+0.1))
XCTAssertEqual(delegate.loops.count, 0)
XCTAssertEqual(delegate.linearAnimaitonPlays.count, 1)
XCTAssertEqual(delegate.linearAnimaitonPauses.count, 1)
XCTAssertEqual(delegate.linearAnimaitonStops.count, 0)
}
func testLoopLoop() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
viewModel.play(animationName: "one", loop: .loop)
view.advance(delta: Double(viewModel.riveModel!.animation!.effectiveDurationInSeconds()+0.1))
XCTAssertEqual(delegate.loops.count, 1)
XCTAssertEqual(delegate.linearAnimaitonPlays.count, 1)
XCTAssertEqual(delegate.linearAnimaitonPauses.count, 0)
XCTAssertEqual(delegate.linearAnimaitonStops.count, 0)
}
func testLoopPingPong() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "multiple_animations")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, autoPlay: false)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
viewModel.play(animationName: "one", loop: .pingPong)
view.advance(delta: Double(viewModel.riveModel!.animation!.effectiveDurationInSeconds()+0.1))
XCTAssertEqual(delegate.loops.count, 1)
XCTAssertEqual(delegate.linearAnimaitonPlays.count, 1)
XCTAssertEqual(delegate.linearAnimaitonPauses.count, 0)
XCTAssertEqual(delegate.linearAnimaitonStops.count, 0)
}
func testStateMachineLayerStates() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "what_a_state")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, stateMachineName: "State Machine 2", autoPlay: true)
let view = viewModel.createRiveView()
view.playerDelegate = delegate
view.stateMachineDelegate = delegate
view.advance(delta:0.1)
XCTAssertEqual(delegate.stateMachinePlays.count, 1)
XCTAssertEqual(delegate.stateMachineStates.count, 1)
XCTAssertEqual(delegate.stateMachineNames[0], "State Machine 2")
XCTAssertEqual(delegate.stateMachineStates[0], "go right")
view.advance(delta:1.1)
XCTAssertEqual(delegate.stateMachineStates.count, 2)
XCTAssertEqual(delegate.stateMachineNames[1], "State Machine 2")
XCTAssertEqual(delegate.stateMachineStates[1], "ExitState")
// takes an extra advance to trigger
view.advance(delta:0)
XCTAssertEqual(delegate.stateMachinePauses.count, 1)
}
func testStateMachineLayerStatesComplex() throws {
let delegate = DrDelegate()
let file = try RiveFile(testfileName: "what_a_state")
let model = RiveModel(riveFile: file)
let viewModel = RiveViewModel(model, stateMachineName: "State Machine 1", autoPlay: true)
let view = viewModel.createRiveView()
view.stateMachineDelegate = delegate
view.advance(delta:0.0)
XCTAssertEqual(delegate.stateMachineStates.count, 0)
viewModel.play()
// MARK: Input
// lets just start, expect 1 change.
viewModel.triggerInput("right")
// TODO: looks like we got a bit of a bug here
// If we do not call this advance, the first animation doesnt seem to get the delta applied.
view.advance(delta:0.0)
view.advance(delta:0.4)
XCTAssertEqual(delegate.stateMachineStates.count, 1)
XCTAssertEqual(delegate.stateMachineStates[0], "go right")
XCTAssertEqual(delegate.stateMachineNames.count, 1)
XCTAssertEqual(delegate.stateMachineNames[0], "State Machine 1")
delegate.stateMachineStates.removeAll()
// should be in same animation still. no state change
view.advance(delta:0.4)
XCTAssertEqual(0, delegate.stateMachineStates.count)
XCTAssertEqual(true, viewModel.isPlaying)
// animation came to an end inside this time period, this still means no state change
view.advance(delta:0.4)
XCTAssertEqual(false, viewModel.isPlaying)
XCTAssertEqual(0, delegate.stateMachineStates.count)
// animation is just kinda stuck there. no change no happening.
view.advance(delta:0.4)
XCTAssertEqual(false, viewModel.isPlaying)
XCTAssertEqual(0, delegate.stateMachineStates.count)
// MARK: Input
// ok lets change thigns up again.
viewModel.triggerInput("change")
view.advance(delta:0.0)
view.advance(delta:0.4)
XCTAssertEqual(true, viewModel.isPlaying)
XCTAssertEqual(1, delegate.stateMachineStates.count)
XCTAssertEqual("change!", delegate.stateMachineStates[0])
delegate.stateMachineStates.removeAll()
// as before lets advance inside the animation -> no change
view.advance(delta:0.4)
XCTAssertEqual(true, viewModel.isPlaying)
XCTAssertEqual(0, delegate.stateMachineStates.count)
// as before lets advance beyond the end of the animaiton, in this case change to exit!
view.advance(delta:0.4)
XCTAssertEqual(false, viewModel.isPlaying)
XCTAssertEqual(1, delegate.stateMachineStates.count)
XCTAssertEqual("ExitState", delegate.stateMachineStates[0])
delegate.stateMachineStates.removeAll()
// chill on exit. no change.
view.advance(delta:0.4)
XCTAssertEqual(false, viewModel.isPlaying)
XCTAssertEqual(0, delegate.stateMachineStates.count)
}
}