-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
654 lines (576 loc) · 24.4 KB
/
test.py
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
import base64
import io
import unittest
import os
import zlib
import time
from ffsds4lib import ds4, sequencer
from Cryptodome.PublicKey import RSA
from Cryptodome.Signature import pss
from Cryptodome.Hash import SHA256
JEDI_TEST_CA = '''
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzps0083KBRnwO4AA0PBI
hoch62Y5RG4lg8IrwgjoZgL/xDarQlxnJLT8dYTB+iAq+EIVSZcZmLFrOyCJguDU
A6PJoGoHGuboGT4SlJtq1Injb8IZT5X+pI2jkCJgMpE3HYX51aD4p2uxtY7pssXr
l4h/ztMhk0zeV8LV093W/73TDQp6J0Uao6K3x17GZ0bBILu4rO+T4iTBEbgEB8xi
JJcORgsOMSlRntH6dWzjnWcokoT1y1nnjU8xKdtccD8wPEyKqnpi82kah5nCdFEE
SNej4ycVKw3D/qprLJFsgU8d0WL4cbKwe37OibRsiXe55AUejWTdcIslFySGMKMz
YwIDAQAB
-----END PUBLIC KEY-----
'''.strip()
TEST_DS4KEY = '''
AAAAAAAAAAAAAQAB3q2+765x6hsXUzjr8YywrxxLJfPx33VIIOVsm4RuTPj7GBnfBAb6+79gIHuP
D2tJXZAzIPVdf0zIfpP3UTU+aj+6sos7v6xVNmaLexQGPTlSNu1dCvx38q6OqUbefT8eSejEk+Tg
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gBbsz11ts9E/NuteC10NmmKZ+uUKUkEupXNn7dhydCOJW/FFVh
+lNjZQKBgDgK97JIKTUBp/y0pi6C7sXzCx/bM/CUvsuV2xjXm96FSMphSwM5uRzv
Yap2X/pyBMG4JVBsAel3QvaUaDepvl3k3UWimrwfkDMHiS4M7TIkGr/zVysy/ehW
fLL7/9OR/xTaWdZLbkdm3mnuJ+fxL8vRm8fHo9JpbIFWuo0K1Z2H
-----END RSA PRIVATE KEY-----
'''.strip()
INITIAL_DS4_INPUT_REPORT = bytes.fromhex('''
01
80 80 80 80
080000
00 00
0000
ff
0000 0000 0000
0000 0000 0000
0000000000
08
0000
00
00 80000000 80000000
00 80000000 80000000
00 80000000 80000000
000000
''')
FEATURE_CONFIG_ALL_ENABLED = bytes.fromhex('''
03
2127
04
4f
00
2c 56
a00f 3d00 e803
0100 0020
0d0d
00000000
00 00 00
000000000000000000000000000000000000000000
''')
# TODO: Touchpad packet sequences start from 1 but it shouldn't really matter as long as they are incrementing at the right time...
TP_SET_ONE_INVALIDATED = bytes.fromhex('''
01
02 8064800c 81c8c012
00 80000000 80000000
00 80000000 80000000
''')
TP_SET_ONE = bytes.fromhex('''
01
01 0064800c 01c8c012
00 80000000 80000000
00 80000000 80000000
''')
TP_SET_MULTI = bytes.fromhex('''
02
01 0064800c 01c8c012
02 0096800c 01fac012
00 80000000 80000000
''')
TP_SET_ONE_AFTER_RELEASE = bytes.fromhex('''
01
03 0264800c 03c8c012
00 80000000 80000000
00 80000000 80000000
''')
class DS4Test(unittest.TestCase):
def test_ds4key_load(self):
'''
DS4Key loading.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
_ds4key = ds4.DS4Key(ds4key_io)
def test_ds4key_sign(self):
'''
Challenge signing with DS4Key object directly.
'''
ds4key_bytes = base64.b64decode(TEST_DS4KEY)
ds4key_io = io.BytesIO(ds4key_bytes)
ds4id_expected = ds4key_bytes[:0x310]
ds4key = ds4.DS4Key(ds4key_io)
challenge = os.urandom(256)
response = ds4key.sign_challenge(challenge)
resp_bytes = bytes(response)
sig = resp_bytes[:0x100]
ds4id = resp_bytes[0x100:]
self.assertEqual(ds4id.hex(), ds4id_expected.hex(), 'DS4ID mismatch.')
ds4key_key = RSA.import_key(TEST_DS4KEY_JUST_KEY)
ds4key_pss = pss.new(ds4key_key)
sha = SHA256.new(challenge)
try:
ds4key_pss.verify(sha, sig)
except ValueError as e:
self.fail(f'Response verification failed. ({str(e)})')
def test_tracker_auth_full(self):
'''
Authentication with state tracker (API-level DS4 protocol mockup).
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
ds4id_expected = ds4key_io.getvalue()[:0x310]
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
challenge = io.BytesIO(b'\x00' * 256)
page = 0
tracker.auth.reset()
while challenge.tell() < 256:
data = bytearray(56)
challenge.readinto(data)
packet = b'\xf0\x01' + page.to_bytes(1, 'big') + b'\x00' + bytes(data)
crc = zlib.crc32(packet)
packet += crc.to_bytes(4, 'little')
assert len(packet) == 64, 'Test case bug: invalid challenge packet length.'
packet_io = io.BytesIO(packet)
tracker.auth.set_challenge(packet_io)
page += 1
wait_count = 0
while True:
response_io = io.BytesIO()
tracker.auth.get_status(response_io)
response = response_io.getvalue()
self.assertEqual(len(response), 16, 'Wrong GetAuthStatus length.')
self.assertEqual(response[0], 0xf2, 'Invalid GetAuthStatus magic.')
self.assertEqual(response[1], 0x1, 'Wrong GetAuthStatus sequence counter.')
self.assertIn(response[2], (0x01, 0x00), 'Invalid auth status.')
if response[2] == 0x01:
wait_count += 1
time.sleep(0.05)
if wait_count > 100:
self.fail('Auth took too long to respond.')
elif response[2] == 0x00:
break
remaining = 0x410
page = 0
response_io_full = io.BytesIO()
while remaining > 0:
response_io = io.BytesIO()
tracker.auth.get_response(response_io)
response = response_io.getvalue()
self.assertEqual(len(response), 64, 'Wrong GetResponse length.')
self.assertEqual(response[0], 0xf1, 'Invalid GetResponse magic.')
self.assertEqual(response[1], 0x1, 'Wrong GetResponse sequence counter.')
self.assertEqual(response[2], page, 'Wrong GetResponse page counter.')
data = response[4:4+min(56, remaining)]
response_io_full.write(data)
remaining = max(0, remaining - 56)
page += 1
response_full = response_io_full.getvalue()
assert len(response_full) == 0x410, 'Test case bug: wrong full response length.'
sig = response_full[:0x100]
ds4id = response_full[0x100:]
self.assertEqual(ds4id.hex(), ds4id_expected.hex(), 'DS4ID mismatch.')
ds4key_key = RSA.import_key(TEST_DS4KEY_JUST_KEY)
ds4key_pss = pss.new(ds4key_key)
sha = SHA256.new(challenge.getvalue())
try:
ds4key_pss.verify(sha, sig)
except ValueError as e:
self.fail(f'Response verification failed. ({str(e)})')
# TODO DS4 API
def test_input_init(self):
'''
Initialize input report.
'''
report = ds4.InputReport()
self.assertEqual(bytes(report).hex(), INITIAL_DS4_INPUT_REPORT.hex())
def test_input_set_dpad(self):
'''
Set DPad.
'''
report = ds4.InputReport()
report.set_dpad(ds4.DPadPosition.nw)
actual = bytes(report)[5] & 0xf
expected = 0x7
self.assertEqual(actual, expected)
def test_input_set_button(self):
'''
Set buttons.
'''
report = ds4.InputReport()
report.set_dpad(ds4.DPadPosition.s)
report.set_button(ds4.ButtonType.l3, True)
report.set_button(ds4.ButtonType.r1, True)
report.set_button(ds4.ButtonType.ps, True)
report.set_button(ds4.ButtonType.circle, True)
report.set_button(ds4.ButtonType.triangle, True)
actual = bytes(report)[5:8].hex()
expected = 'c44201'
self.assertEqual(actual, expected)
def test_input_clear_button(self):
'''
Clear buttons.
'''
report = ds4.InputReport()
report.set_dpad(ds4.DPadPosition.s)
report.set_button(ds4.ButtonType.l3, True)
report.set_button(ds4.ButtonType.r1, True)
report.set_button(ds4.ButtonType.ps, True)
report.set_button(ds4.ButtonType.circle, True)
report.set_button(ds4.ButtonType.triangle, True)
report.clear_buttons()
actual = bytes(report)[5:8].hex()
# dpad should stay south
expected = 'c00000'
self.assertEqual(actual, expected)
def test_input_touchpad_one(self):
'''
One frame 2 point touch.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
tracker.touch.queue_touchpad((100, 200), (200, 300))
report = tracker.prepare_for_report_submission()
actual = bytes(report)[33:61].hex()
expected = TP_SET_ONE.hex()
self.assertEqual(actual, expected)
def test_input_touchpad_multiple(self):
'''
Two frames with moving 2 point touch.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
tracker.touch.queue_touchpad((100, 200), (200, 300))
tracker.touch.queue_touchpad((150, 200), (250, 300))
report = tracker.prepare_for_report_submission()
actual = bytes(report)[33:61].hex()
expected = TP_SET_MULTI.hex()
self.assertEqual(actual, expected)
def test_input_touchpad_release_one(self):
'''
Release one point only will only result in that point being
invalidated. Only checks the invalidation states.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
tracker.touch.queue_touchpad((100, 200), (200, 300))
tracker.prepare_for_report_submission()
tracker.touch.queue_touchpad(release_pos0=False)
report = tracker.prepare_for_report_submission()
tp_report = bytes(report)[33:61]
actual = (bool(tp_report[2] & 0x80), bool(tp_report[6] & 0x80))
expected = (False, True)
self.assertEqual(actual, expected)
def test_input_touchpad_release_both(self):
'''
Release both points will cause both points to be invalidated. Only
checks the invalidation states.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
tracker.touch.queue_touchpad((100, 200), (200, 300))
tracker.prepare_for_report_submission()
tracker.touch.queue_touchpad()
report = tracker.prepare_for_report_submission()
tp_report = bytes(report)[33:61]
actual = (bool(tp_report[2] & 0x80), bool(tp_report[6] & 0x80))
expected = (True, True)
self.assertEqual(actual, expected)
def test_input_touchpad_hold(self):
'''
Not updating the points will cause them to be held at position with
frame seq incrementing.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
tracker.touch.queue_touchpad((100, 200), (200, 300))
# Hold for 2 frames
tracker.prepare_for_report_submission()
tracker.prepare_for_report_submission()
report = tracker.prepare_for_report_submission()
actual = bytes(report)[33:61].hex()
expected_bytes = bytearray(TP_SET_ONE)
# Frame sequence should be 3 now
expected_bytes[1] = 3
expected = expected_bytes.hex()
self.assertEqual(actual, expected)
def test_input_touchpad_nohold(self):
'''
Touch shouldn't be held when there are actual new points.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
tracker.touch.queue_touchpad((230, 125), (500, 100))
tracker.prepare_for_report_submission()
tracker.touch.queue_touchpad((100, 200), (200, 300))
report = tracker.prepare_for_report_submission() # this should do nothing to the touchpad frames now
actual = bytes(report)[33:61].hex()
expected_bytes = bytearray(TP_SET_ONE)
# Frame sequence should be 2 now
expected_bytes[1] = 2
expected = expected_bytes.hex()
# Exactly as before with empty unused frames
self.assertEqual(actual, expected)
def test_input_touchpad_nohold_after_release(self):
'''
Touch shouldn't be held when they are released. Instead they should be invalidated.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
tracker.touch.queue_touchpad((100, 200), (200, 300))
tracker.prepare_for_report_submission()
tracker.touch.queue_touchpad()
report = tracker.prepare_for_report_submission()
actual = bytes(report)[33:61].hex()
expected = TP_SET_ONE_INVALIDATED.hex()
self.assertEqual(actual, expected)
def test_input_touchpad_nohold_after_release_2(self):
'''
Invalidated touches will continue to be available despite invalidated.
This mimics official DS4 behavior.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
# Initial frame
tracker.touch.queue_touchpad((100, 200), (200, 300))
tracker.prepare_for_report_submission()
# Release - This is one packet despite all points are invalidated.
tracker.touch.queue_touchpad()
tracker.prepare_for_report_submission()
# Sustain the invalidated packet. No value should be incrementing.
report = tracker.prepare_for_report_submission()
actual = bytes(report)[33:61].hex()
expected = TP_SET_ONE_INVALIDATED.hex()
self.assertEqual(actual, expected)
def test_input_touchpad_touch_seq_inc(self):
'''
Touch sequence should be increased when different touches are
registered.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
# Initial frame
tracker.touch.queue_touchpad((100, 200), (200, 300))
tracker.prepare_for_report_submission()
# Release - This is one packet despite all points are invalidated.
tracker.touch.queue_touchpad()
tracker.prepare_for_report_submission()
# Second frame with 2 different points
tracker.touch.queue_touchpad((100, 200), (200, 300))
report = tracker.prepare_for_report_submission()
actual = bytes(report)[33:61].hex()
expected = TP_SET_ONE_AFTER_RELEASE.hex()
self.assertEqual(actual, expected)
def test_tracker_edit_context(self):
'''
Report editing context and buffer swapping.
'''
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
with tracker.start_modify_report() as report:
report: ds4.InputReport
report.set_button(ds4.ButtonType.ps, True)
with tracker.input_report_lock:
tracker.swap_buffer_nolock()
tracker.sync_buffer_nolock()
actual = tracker.input_report_submitting_buf[5:8].hex()
actual_next = tracker.input_report_submitting_buf[5:8].hex()
expected = '080001'
self.assertEqual(actual, expected)
self.assertEqual(actual_next, expected)
def test_imu_set_angular_velocity(self):
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
tracker.imu.set_angular_velocity(90, 90, 90)
report = tracker.prepare_for_report_submission()
actual = bytes(report)[13:25].hex()
# 1000 * 90(deg) / 61 = approx. 1475
# (1475, 1475, 1475), (0, 0, 0)
expected = 'c305c305c305000000000000'
self.assertEqual(actual, expected)
def test_imu_set_linear_acceleration(self):
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features)
tracker.imu.set_linear_acceleration(1, 1, 1)
report = tracker.prepare_for_report_submission()
actual = bytes(report)[13:25].hex()
# (0, 0, 0), (8192, 8192, 8192)
expected = '000000000000002000200020'
self.assertEqual(actual, expected)
def test_imu_set_attitude_position(self):
current_time = 0
_mock_time_func = lambda: current_time
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features, imu_time_func=_mock_time_func)
# Put into attitude-position mode
tracker.imu.use_attitude_position = True
# (30, 30, 30), (0, 0, 0) @ 50ms
current_time = 0.05
tracker.imu.set_attitude_position(current_time, 30, 30, 30, 0, 0, 0)
report = tracker.prepare_for_report_submission()
actual = bytes(report)[13:25].hex()
expected = '6c266c266c26000000000000'
self.assertEqual(actual, expected)
# (45, 45, 45), (3, 4, 5) @ 100ms
current_time = 0.10
tracker.imu.set_attitude_position(current_time, 45, 45, 45, 3, 4, 5)
report = tracker.prepare_for_report_submission()
actual = bytes(report)[13:25].hex()
expected = '361336133613eb0339058806'
self.assertEqual(actual, expected)
# (60, 50, 10), (6, 10, 5) @ 150ms
current_time = 0.15
tracker.imu.set_attitude_position(current_time, 60, 50, 10, x=6, y=10)
report = tracker.prepare_for_report_submission()
actual = bytes(report)[13:25].hex()
expected = '361367062dd3eb03d6070000'
self.assertEqual(actual, expected)
def test_imu_sustain(self):
current_time = 0
_mock_time_func = lambda: current_time
ds4key_io = io.BytesIO(base64.b64decode(TEST_DS4KEY))
ds4key = ds4.DS4Key(ds4key_io)
features = ds4.FeatureConfiguration()
tracker = ds4.DS4StateTracker(ds4key, features, imu_time_func=_mock_time_func)
# Put into attitude-position mode
tracker.imu.use_attitude_position = True
# (30, 30, 30), (0, 0, 0) @ 50ms
current_time = 0.05
tracker.imu.set_attitude_position(current_time, 30, 30, 30, 0, 0, 0)
report = tracker.prepare_for_report_submission()
actual = bytes(report)[13:25].hex()
expected = '6c266c266c26000000000000'
self.assertEqual(actual, expected)
# Sustain @ 100ms
current_time = 0.10
report = tracker.prepare_for_report_submission()
actual = bytes(report)[13:25].hex()
expected = '000000000000000000000000'
self.assertEqual(actual, expected)
# (45, 45, 45), (3, 4, 5) @ 150ms
current_time = 0.15
tracker.imu.set_attitude_position(current_time, 45, 45, 45, 3, 4, 5)
report = tracker.prepare_for_report_submission()
actual = bytes(report)[13:25].hex()
expected = '361336133613eb0339058806'
self.assertEqual(actual, expected)
def test_feature_config_all_enabled(self):
'''
Feature config - All enabled.
'''
features = ds4.FeatureConfiguration(
enable_touchpad=True,
enable_imu=True,
enable_led=True,
enable_rumble=True,
)
actual = bytes(features).hex()
self.assertEqual(actual, FEATURE_CONFIG_ALL_ENABLED.hex())
def test_sequencer_lineartween(self):
tw = sequencer.LinearTween(0, 5.0, (0, 5))
self.assertFalse(tw.done)
self.assertAlmostEqual(tw.at(1.0), 1.0)
self.assertAlmostEqual(tw.at(1.5), 1.5)
self.assertAlmostEqual(tw.at(2.0), 2.0)
self.assertAlmostEqual(tw.at(3.5), 3.5)
self.assertAlmostEqual(tw.at(5.0), 5.0)
self.assertTrue(tw.done)
def test_sequencer_polyeasetween_quad(self):
tw = sequencer.PolyEaseTween(0, 1, (0, 1))
self.assertFalse(tw.done)
self.assertAlmostEqual(tw.at(0.1), 0.02)
self.assertAlmostEqual(tw.at(0.35), 0.245)
self.assertAlmostEqual(tw.at(0.5), 0.5)
self.assertAlmostEqual(tw.at(0.62), 0.7112)
self.assertAlmostEqual(tw.at(0.77), 0.8942)
self.assertAlmostEqual(tw.at(0.9), 0.98)
self.assertAlmostEqual(tw.at(1.0), 1.0)
self.assertTrue(tw.done)
def test_sequencer_polyeasetween_cubic(self):
tw = sequencer.PolyEaseTween(0, 1, (0, 1), exp=3)
self.assertFalse(tw.done)
self.assertAlmostEqual(tw.at(0.1), 0.004)
self.assertAlmostEqual(tw.at(0.24), 0.055296)
self.assertAlmostEqual(tw.at(0.42), 0.296352)
self.assertAlmostEqual(tw.at(0.5), 0.5)
self.assertAlmostEqual(tw.at(0.71), 0.902444)
self.assertAlmostEqual(tw.at(0.87), 0.991212)
self.assertAlmostEqual(tw.at(0.9), 0.996)
self.assertAlmostEqual(tw.at(1.0), 1.0)
self.assertTrue(tw.done)
if __name__ == '__main__':
unittest.main()