FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/tests/test_native_views.py at v0.41.0 · pythonnative/pythonnative · GitHub
pythonnative
pythonnative
Repository navigation
Code
Issues
11
(11)
Pull requests
4
(4)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pythonnative
/
tests
/
test_native_views.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
481 lines (369 loc) · 15.8 KB
Breadcrumbs
pythonnative
/
tests
/
test_native_views.py
Copy path
File metadata and controls
481 lines (369 loc) · 15.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
"""Unit tests for the native_views package.
Tests the registry's batched ``apply_mutations`` commit channel, the tag
table, the base handler protocol, and shared utility functions.
Platform-specific handlers (android/ios) are not tested here since they
require their respective runtime environments; they are exercised by
E2E tests on device.
"""
import
itertools
from
typing
import
Any
,
Dict
,
Tuple
import
pytest
from
pythonnative
.
layout
import
LAYOUT_STYLE_KEYS
from
pythonnative
.
mutations
import
CreateOp
,
DestroyOp
,
InsertOp
,
SetFrameOp
,
UpdateOp
from
pythonnative
.
native_views
import
NativeViewRegistry
,
set_registry
from
pythonnative
.
native_views
.
base
import
(
ViewHandler
,
parse_color_int
,
)
# ======================================================================
# parse_color_int
# ======================================================================
def
test_parse_color_hex6
()
->
None
:
result
=
parse_color_int
(
"#FF0000"
)
assert
result
==
parse_color_int
(
"FF0000"
)
expected
=
int
(
"FFFF0000"
,
16
)
if
expected
>
0x7FFFFFFF
:
expected
-=
0x100000000
assert
result
==
expected
def
test_parse_color_hex8
()
->
None
:
result
=
parse_color_int
(
"#80FF0000"
)
raw
=
int
(
"80FF0000"
,
16
)
expected
=
raw
-
0x100000000
# signed conversion
assert
result
==
expected
def
test_parse_color_int_passthrough
()
->
None
:
assert
parse_color_int
(
0x00FF00
)
==
0x00FF00
def
test_parse_color_signed_conversion
()
->
None
:
result
=
parse_color_int
(
"#FFFFFFFF"
)
assert
result
<
0
def
test_parse_color_with_whitespace
()
->
None
:
assert
parse_color_int
(
" #FF0000 "
)
==
parse_color_int
(
"#FF0000"
)
# ======================================================================
# Layout-engine ownership
# ======================================================================
def
test_layout_style_keys_includes_flex_props
()
->
None
:
"""All flex / sizing props are owned by the layout engine, not handlers."""
for
key
in
(
"width"
,
"height"
,
"flex"
,
"flex_grow"
,
"flex_shrink"
,
"flex_basis"
,
"flex_direction"
,
"flex_wrap"
,
"justify_content"
,
"align_items"
,
"align_self"
,
"align_content"
,
"padding"
,
"margin"
,
"spacing"
,
"gap"
,
"position"
,
"top"
,
"right"
,
"bottom"
,
"left"
,
"aspect_ratio"
,
):
assert
key
in
LAYOUT_STYLE_KEYS
# ======================================================================
# ViewHandler protocol
# ======================================================================
def
test_view_handler_create_raises
()
->
None
:
handler
=
ViewHandler
()
with
pytest
.
raises
(
NotImplementedError
):
handler
.
create
(
1
, {})
def
test_view_handler_update_raises
()
->
None
:
handler
=
ViewHandler
()
with
pytest
.
raises
(
NotImplementedError
):
handler
.
update
(
None
, {})
def
test_view_handler_child_ops_default_noop
()
->
None
:
handler
=
ViewHandler
()
handler
.
insert_child
(
None
,
None
,
0
)
handler
.
remove_child
(
None
,
None
)
handler
.
destroy
(
None
)
def
test_view_handler_set_frame_default_noop
()
->
None
:
"""Default ``set_frame`` is a no-op so virtual nodes can opt out."""
handler
=
ViewHandler
()
handler
.
set_frame
(
None
,
0
,
0
,
100
,
50
)
def
test_view_handler_measure_intrinsic_default_zero
()
->
None
:
"""Default ``measure_intrinsic`` returns ``(0, 0)`` for handlers without intrinsic size."""
handler
=
ViewHandler
()
assert
handler
.
measure_intrinsic
(
None
,
100.0
,
100.0
)
==
(
0.0
,
0.0
)
def
test_view_handler_command_default_none
()
->
None
:
handler
=
ViewHandler
()
assert
handler
.
command
(
None
,
"anything"
, {})
is
None
def
test_view_handler_animation_defaults
()
->
None
:
"""Default animation hooks: no native driving, no presentation value."""
handler
=
ViewHandler
()
handler
.
set_animated_property
(
None
,
"opacity"
,
0.5
)
assert
handler
.
start_animation
(
None
,
1
,
"opacity"
, {
"kind"
:
"timing"
})
is
False
assert
handler
.
cancel_animation
(
None
,
1
)
is
None
# ======================================================================
# NativeViewRegistry (tag table + apply_mutations)
# ======================================================================
_tags
=
itertools
.
count
(
1
)
def
_next_tag
()
->
int
:
return
next
(
_tags
)
class
StubView
:
def
__init__
(
self
,
tag
:
int
,
type_name
:
str
,
props
:
Dict
[
str
,
Any
])
->
None
:
self
.
tag
=
tag
self
.
type_name
=
type_name
self
.
props
=
dict
(
props
)
self
.
children
:
list
=
[]
self
.
frame
:
Tuple
[
float
,
float
,
float
,
float
]
=
(
0.0
,
0.0
,
0.0
,
0.0
)
self
.
destroyed
=
False
class
StubHandler
(
ViewHandler
):
def
__init__
(
self
,
type_name
:
str
=
"Stub"
)
->
None
:
self
.
type_name
=
type_name
self
.
commands
:
list
=
[]
def
create
(
self
,
tag
:
int
,
props
:
Dict
[
str
,
Any
])
->
StubView
:
return
StubView
(
tag
,
self
.
type_name
,
props
)
def
update
(
self
,
native_view
:
Any
,
changed_props
:
Dict
[
str
,
Any
])
->
None
:
native_view
.
props
.
update
(
changed_props
)
def
insert_child
(
self
,
parent
:
Any
,
child
:
Any
,
index
:
int
)
->
None
:
if
child
in
parent
.
children
:
parent
.
children
.
remove
(
child
)
parent
.
children
.
insert
(
min
(
index
,
len
(
parent
.
children
)),
child
)
def
remove_child
(
self
,
parent
:
Any
,
child
:
Any
)
->
None
:
if
child
in
parent
.
children
:
parent
.
children
.
remove
(
child
)
def
destroy
(
self
,
native_view
:
Any
)
->
None
:
native_view
.
destroyed
=
True
def
set_frame
(
self
,
native_view
:
Any
,
x
:
float
,
y
:
float
,
width
:
float
,
height
:
float
)
->
None
:
native_view
.
frame
=
(
x
,
y
,
width
,
height
)
def
measure_intrinsic
(
self
,
native_view
:
Any
,
max_width
:
float
,
max_height
:
float
)
->
Tuple
[
float
,
float
]:
# Pretend the stub view is 40x10 plus its content length.
return
(
min
(
40.0
+
len
(
native_view
.
props
.
get
(
"text"
,
""
)),
max_width
),
10.0
)
def
command
(
self
,
native_view
:
Any
,
name
:
str
,
args
:
Dict
[
str
,
Any
])
->
Any
:
self
.
commands
.
append
((
name
,
dict
(
args
)))
return
"ok"
def
_make_registry
()
->
NativeViewRegistry
:
reg
=
NativeViewRegistry
()
reg
.
register
(
"Text"
,
StubHandler
(
"Text"
))
reg
.
register
(
"Column"
,
StubHandler
(
"Column"
))
return
reg
def
test_registry_create_registers_tag
()
->
None
:
reg
=
_make_registry
()
tag
=
_next_tag
()
reg
.
apply_mutations
([
CreateOp
(
tag
,
"Text"
, {
"text"
:
"hello"
})])
view
=
reg
.
resolve_view
(
tag
)
assert
isinstance
(
view
,
StubView
)
assert
view
.
props
[
"text"
]
==
"hello"
assert
reg
.
live_view_count
()
==
1
def
test_registry_unknown_type_is_isolated
(
capsys
:
pytest
.
CaptureFixture
[
str
])
->
None
:
"""A CreateOp for an unknown type is logged but does not abort the batch."""
reg
=
_make_registry
()
bad_tag
,
good_tag
=
_next_tag
(),
_next_tag
()
reg
.
apply_mutations
(
[
CreateOp
(
bad_tag
,
"NonExistent"
, {}),
CreateOp
(
good_tag
,
"Text"
, {
"text"
:
"still applied"
}),
]
)
captured
=
capsys
.
readouterr
()
assert
"CreateOp failed"
in
captured
.
err
assert
reg
.
resolve_view
(
bad_tag
)
is
None
assert
reg
.
resolve_view
(
good_tag
)
is
not
None
def
test_registry_update_view
()
->
None
:
reg
=
_make_registry
()
tag
=
_next_tag
()
reg
.
apply_mutations
([
CreateOp
(
tag
,
"Text"
, {
"text"
:
"old"
})])
reg
.
apply_mutations
([
UpdateOp
(
tag
, {
"text"
:
"new"
})])
assert
reg
.
resolve_view
(
tag
).
props
[
"text"
]
==
"new"
def
test_registry_update_unknown_tag_noop
()
->
None
:
reg
=
_make_registry
()
reg
.
apply_mutations
([
UpdateOp
(
999_999
, {
"a"
:
1
})])
def
test_registry_insert_child
()
->
None
:
reg
=
_make_registry
()
parent_tag
,
child_tag
=
_next_tag
(),
_next_tag
()
reg
.
apply_mutations
(
[
CreateOp
(
parent_tag
,
"Column"
, {}),
CreateOp
(
child_tag
,
"Text"
, {
"text"
:
"x"
}),
InsertOp
(
parent_tag
,
child_tag
,
0
),
]
)
parent
=
reg
.
resolve_view
(
parent_tag
)
child
=
reg
.
resolve_view
(
child_tag
)
assert
parent
.
children
==
[
child
]
def
test_registry_child_ops_unknown_tags_noop
()
->
None
:
reg
=
_make_registry
()
reg
.
apply_mutations
([
InsertOp
(
111_111
,
222_222
,
0
)])
def
test_registry_destroy_releases_tag_and_calls_handler
()
->
None
:
reg
=
_make_registry
()
tag
=
_next_tag
()
reg
.
apply_mutations
([
CreateOp
(
tag
,
"Text"
, {
"text"
:
"x"
})])
view
=
reg
.
resolve_view
(
tag
)
reg
.
apply_mutations
([
DestroyOp
(
tag
)])
assert
view
.
destroyed
is
True
assert
reg
.
resolve_view
(
tag
)
is
None
assert
reg
.
live_view_count
()
==
0
def
test_registry_set_frame_dispatches_to_handler
()
->
None
:
reg
=
_make_registry
()
tag
=
_next_tag
()
reg
.
apply_mutations
(
[
CreateOp
(
tag
,
"Text"
, {
"text"
:
"x"
}),
SetFrameOp
(
tag
,
5.0
,
10.0
,
100.0
,
50.0
),
]
)
assert
reg
.
resolve_view
(
tag
).
frame
==
(
5.0
,
10.0
,
100.0
,
50.0
)
def
test_registry_set_frame_unknown_tag_noop
()
->
None
:
reg
=
_make_registry
()
reg
.
apply_mutations
([
SetFrameOp
(
999_999
,
0
,
0
,
100
,
50
)])
def
test_registry_ops_apply_in_order
()
->
None
:
"""One transaction may create, attach, frame, and update in sequence."""
reg
=
_make_registry
()
parent_tag
,
child_tag
=
_next_tag
(),
_next_tag
()
reg
.
apply_mutations
(
[
CreateOp
(
parent_tag
,
"Column"
, {}),
CreateOp
(
child_tag
,
"Text"
, {
"text"
:
"a"
}),
InsertOp
(
parent_tag
,
child_tag
,
0
),
UpdateOp
(
child_tag
, {
"text"
:
"b"
}),
SetFrameOp
(
child_tag
,
0.0
,
0.0
,
60.0
,
16.0
),
]
)
child
=
reg
.
resolve_view
(
child_tag
)
assert
child
.
props
[
"text"
]
==
"b"
assert
child
.
frame
==
(
0.0
,
0.0
,
60.0
,
16.0
)
assert
reg
.
resolve_view
(
parent_tag
).
children
==
[
child
]
def
test_registry_measure_intrinsic_dispatches
()
->
None
:
reg
=
_make_registry
()
tag
=
_next_tag
()
reg
.
apply_mutations
([
CreateOp
(
tag
,
"Text"
, {
"text"
:
"abc"
})])
w
,
h
=
reg
.
measure_intrinsic
(
tag
,
1000.0
,
1000.0
)
assert
w
==
43.0
# 40 + 3
assert
h
==
10.0
def
test_registry_measure_intrinsic_unknown_tag_zero
()
->
None
:
reg
=
_make_registry
()
assert
reg
.
measure_intrinsic
(
999_999
,
1000.0
,
1000.0
)
==
(
0.0
,
0.0
)
def
test_registry_command_dispatches_by_tag
()
->
None
:
reg
=
NativeViewRegistry
()
handler
=
StubHandler
(
"Text"
)
reg
.
register
(
"Text"
,
handler
)
tag
=
_next_tag
()
reg
.
apply_mutations
([
CreateOp
(
tag
,
"Text"
, {
"text"
:
"x"
})])
result
=
reg
.
command
(
tag
,
"focus"
, {
"select_all"
:
True
})
assert
result
==
"ok"
assert
handler
.
commands
==
[(
"focus"
, {
"select_all"
:
True
})]
def
test_registry_command_unknown_tag_none
()
->
None
:
reg
=
_make_registry
()
assert
reg
.
command
(
999_999
,
"focus"
)
is
None
def
test_registry_animation_hooks_resolve_through_tag_table
()
->
None
:
class
AnimHandler
(
StubHandler
):
def
__init__
(
self
)
->
None
:
super
().
__init__
(
"Text"
)
self
.
applied
:
list
=
[]
self
.
started
:
list
=
[]
self
.
cancelled
:
list
=
[]
def
set_animated_property
(
self
,
native_view
:
Any
,
prop_name
:
str
,
value
:
Any
)
->
None
:
self
.
applied
.
append
((
prop_name
,
value
))
def
start_animation
(
self
,
native_view
:
Any
,
anim_id
:
int
,
prop_name
:
str
,
spec
:
Dict
[
str
,
Any
])
->
bool
:
self
.
started
.
append
((
anim_id
,
prop_name
))
return
True
def
cancel_animation
(
self
,
native_view
:
Any
,
anim_id
:
int
)
->
Any
:
self
.
cancelled
.
append
(
anim_id
)
return
0.5
reg
=
NativeViewRegistry
()
handler
=
AnimHandler
()
reg
.
register
(
"Text"
,
handler
)
tag
=
_next_tag
()
reg
.
apply_mutations
([
CreateOp
(
tag
,
"Text"
, {})])
reg
.
set_animated_property
(
tag
,
"opacity"
,
0.7
)
assert
handler
.
applied
==
[(
"opacity"
,
0.7
)]
assert
reg
.
start_animation
(
tag
,
1
,
"opacity"
, {
"kind"
:
"timing"
})
is
True
assert
handler
.
started
==
[(
1
,
"opacity"
)]
assert
reg
.
cancel_animation
(
tag
,
1
)
==
0.5
# Unknown tags: animation hooks are safe no-ops.
reg
.
set_animated_property
(
999_999
,
"opacity"
,
1.0
)
assert
reg
.
start_animation
(
999_999
,
2
,
"opacity"
, {})
is
False
assert
reg
.
cancel_animation
(
999_999
,
2
)
is
None
def
test_set_registry_injects
()
->
None
:
reg
=
NativeViewRegistry
()
set_registry
(
reg
)
from
pythonnative
.
native_views
import
_registry
assert
_registry
is
reg
set_registry
(
None
)
# ======================================================================
# _tripwire_log rate limiter
# ======================================================================
@
pytest
.
fixture
def
_tripwire_state
()
->
Any
:
"""Reset the per-label tripwire state before each test."""
import
pythonnative
.
native_views
as
nv
nv
.
_TRIPWIRE_LAST_LOG_TIME
.
clear
()
nv
.
_TRIPWIRE_SUPPRESSED_COUNT
.
clear
()
yield
nv
nv
.
_TRIPWIRE_LAST_LOG_TIME
.
clear
()
nv
.
_TRIPWIRE_SUPPRESSED_COUNT
.
clear
()
def
test_tripwire_log_first_call_emits
(
capsys
:
pytest
.
CaptureFixture
[
str
],
_tripwire_state
:
Any
)
->
None
:
_tripwire_state
.
_tripwire_log
(
"test:basic"
,
"first message"
)
captured
=
capsys
.
readouterr
()
assert
"first message"
in
captured
.
err
def
test_tripwire_log_subsequent_within_window_suppressed
(
capsys
:
pytest
.
CaptureFixture
[
str
],
_tripwire_state
:
Any
)
->
None
:
_tripwire_state
.
_tripwire_log
(
"test:burst"
,
"first"
)
capsys
.
readouterr
()
_tripwire_state
.
_tripwire_log
(
"test:burst"
,
"second"
)
_tripwire_state
.
_tripwire_log
(
"test:burst"
,
"third"
)
captured
=
capsys
.
readouterr
()
assert
captured
.
err
==
""
assert
_tripwire_state
.
_TRIPWIRE_SUPPRESSED_COUNT
[
"test:burst"
]
==
2
def
test_tripwire_log_after_window_emits_with_summary
(
monkeypatch
:
pytest
.
MonkeyPatch
,
capsys
:
pytest
.
CaptureFixture
[
str
],
_tripwire_state
:
Any
,
)
->
None
:
"""After the window expires, the next emit includes a ``+N similar`` suffix."""
import
time
as
_time
monkeypatch
.
setattr
(
_tripwire_state
,
"_TRIPWIRE_RATE_LIMIT_S"
,
0.05
)
_tripwire_state
.
_tripwire_log
(
"test:summary"
,
"first sample"
)
capsys
.
readouterr
()
_tripwire_state
.
_tripwire_log
(
"test:summary"
,
"suppressed-A"
)
_tripwire_state
.
_tripwire_log
(
"test:summary"
,
"suppressed-B"
)
_time
.
sleep
(
0.06
)
_tripwire_state
.
_tripwire_log
(
"test:summary"
,
"third sample"
)
captured
=
capsys
.
readouterr
()
assert
"third sample"
in
captured
.
err
assert
"+2 similar"
in
captured
.
err
assert
_tripwire_state
.
_TRIPWIRE_SUPPRESSED_COUNT
[
"test:summary"
]
==
0
def
test_tripwire_log_distinct_labels_independent
(
capsys
:
pytest
.
CaptureFixture
[
str
],
_tripwire_state
:
Any
)
->
None
:
"""A burst on one label must not silence a different label's first emit."""
_tripwire_state
.
_tripwire_log
(
"test:a"
,
"from a"
)
_tripwire_state
.
_tripwire_log
(
"test:a"
,
"still a, suppressed"
)
_tripwire_state
.
_tripwire_log
(
"test:b"
,
"from b"
)
captured
=
capsys
.
readouterr
()
assert
"from a"
in
captured
.
err
assert
"from b"
in
captured
.
err
assert
"still a"
not
in
captured
.
err
def
test_tripwire_log_suffix_omitted_when_zero_suppressed
(
monkeypatch
:
pytest
.
MonkeyPatch
,
capsys
:
pytest
.
CaptureFixture
[
str
],
_tripwire_state
:
Any
,
)
->
None
:
"""The ``+N similar`` suffix only appears when at least one was suppressed."""
import
time
as
_time
monkeypatch
.
setattr
(
_tripwire_state
,
"_TRIPWIRE_RATE_LIMIT_S"
,
0.01
)
_tripwire_state
.
_tripwire_log
(
"test:nosuffix"
,
"alpha"
)
_time
.
sleep
(
0.02
)
_tripwire_state
.
_tripwire_log
(
"test:nosuffix"
,
"beta"
)
captured
=
capsys
.
readouterr
()
assert
"beta"
in
captured
.
err
assert
"similar"
not
in
captured
.
err
def
test_registry_set_frame_nan_emits_tripwire
(
capsys
:
pytest
.
CaptureFixture
[
str
],
_tripwire_state
:
Any
)
->
None
:
"""A ``SetFrameOp`` with a NaN dimension fires the rate-limited tripwire."""
reg
=
_make_registry
()
tag
=
_next_tag
()
reg
.
apply_mutations
(
[
CreateOp
(
tag
,
"Text"
, {
"text"
:
"x"
}),
SetFrameOp
(
tag
,
0.0
,
0.0
,
float
(
"nan"
),
50.0
),
]
)
captured
=
capsys
.
readouterr
()
assert
"[set_frame:nan]"
in
captured
.
err
assert
"type='Text'"
in
captured
.
err
Back
|
FazBrowse Home
|
New Git URL