FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pythonnative/src/pythonnative/testing/backend.py at v0.43.1 · 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
/
src
/
pythonnative
/
testing
/
backend.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
322 lines (266 loc) · 12.8 KB
Breadcrumbs
pythonnative
/
src
/
pythonnative
/
testing
/
backend.py
Copy path
File metadata and controls
322 lines (266 loc) · 12.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
"""In-memory backend implementing the batched mutation protocol.
[`FakeBackend`][pythonnative.testing.FakeBackend] speaks the same
protocol as the platform registries (``apply_mutations``,
``resolve_view``, ``measure_intrinsic``, ``command``, plus the animation
hooks) while keeping a real tree of
[`FakeView`][pythonnative.testing.FakeView] objects, so tests can assert
on structure, props, and frames without a device.
The fake raises on malformed transactions, including unknown tags, double
destroys, and insertion into a destroyed parent. Production bridge renderers
validate versioned commits before mutation and fail the surface on rejection.
Recorded op shapes (in ``FakeBackend.ops``):
- ``("create", type_name, view.id)``
- ``("update", type_name, view.id, tuple(sorted(changed_keys)))``
- ``("insert_child", parent.id, child.id, index)``
- ``("destroy", view.id)``
- ``("set_frame", view.id, x, y, w, h)``
"""
from
__future__
import
annotations
from
typing
import
Any
,
Dict
,
Iterator
,
List
,
Optional
,
Sequence
,
Tuple
from
..
mutations
import
CreateOp
,
DestroyOp
,
InsertOp
,
Mutation
,
SetFrameOp
,
UpdateOp
__all__
=
[
"DEFAULT_INTRINSIC"
,
"FakeBackend"
,
"FakeView"
]
DEFAULT_INTRINSIC
:
Dict
[
str
,
Tuple
[
float
,
float
]]
=
{
"Text"
: (
60.0
,
16.0
),
"Button"
: (
80.0
,
32.0
),
"Image"
: (
40.0
,
40.0
),
"TextInput"
: (
120.0
,
32.0
),
"TabBar"
: (
320.0
,
49.0
),
}
"""Intrinsic sizes reported for content-sized leaves (what platform measure hooks would return)."""
class
FakeView
:
"""Simulated native view: type, props, children, and last frame.
Attributes:
tag: The reconciler-assigned tag (use with ``fire`` / events).
type_name: Native type, e.g. ``"Text"``.
props: Native-safe props (event callbacks are stripped; they
live in the event registry keyed by ``tag``).
children: Child views in order.
frame: ``(x, y, width, height)`` from the last layout pass.
"""
_next_id
=
0
def
__init__
(
self
,
tag
:
int
,
type_name
:
str
,
props
:
Dict
[
str
,
Any
])
->
None
:
FakeView
.
_next_id
+=
1
self
.
id
=
FakeView
.
_next_id
self
.
tag
=
tag
self
.
type_name
=
type_name
self
.
props
:
Dict
[
str
,
Any
]
=
dict
(
props
)
self
.
children
:
List
[
FakeView
]
=
[]
self
.
parent
:
Optional
[
FakeView
]
=
None
self
.
frame
:
Tuple
[
float
,
float
,
float
,
float
]
=
(
0.0
,
0.0
,
0.0
,
0.0
)
self
.
destroyed
=
False
def
__repr__
(
self
)
->
str
:
label
=
self
.
text
suffix
=
f"
{
label
!r
}
"
if
label
else
""
return
f"<
{
self
.
type_name
}
tag=
{
self
.
tag
}
{
suffix
}
>"
# -- content --------------------------------------------------------
@
property
def
text
(
self
)
->
Optional
[
str
]:
"""Visible text for text-bearing views (``Text.text``, ``Button.title``, ``TextInput.value``)."""
for
key
in
(
"text"
,
"title"
,
"value"
,
"placeholder"
):
value
=
self
.
props
.
get
(
key
)
if
isinstance
(
value
,
str
):
return
value
return
None
@
property
def
hidden
(
self
)
->
bool
:
"""Whether this view is removed from layout (``display: "none"``)."""
return
self
.
props
.
get
(
"display"
)
==
"none"
or
self
.
type_name
==
"Screen"
and
self
.
props
.
get
(
"active"
)
is
False
@
property
def
test_id
(
self
)
->
Optional
[
str
]:
"""The ``test_id`` prop, if set."""
return
self
.
props
.
get
(
"test_id"
)
@
property
def
label
(
self
)
->
Optional
[
str
]:
"""The ``accessibility_label`` prop, if set."""
return
self
.
props
.
get
(
"accessibility_label"
)
# -- traversal ------------------------------------------------------
def
walk
(
self
,
*
,
include_hidden
:
bool
=
True
)
->
Iterator
[
"FakeView"
]:
"""Yield this view and every descendant, depth-first.
With ``include_hidden=False`` subtrees under a ``display: "none"``
view are skipped (what a user can see).
"""
if
not
include_hidden
and
self
.
hidden
:
return
yield
self
for
child
in
self
.
children
:
yield
from
child
.
walk
(
include_hidden
=
include_hidden
)
def
find_all
(
self
,
predicate_or_type
:
Any
)
->
List
[
"FakeView"
]:
"""Every view in this subtree matching a type name or predicate."""
if
isinstance
(
predicate_or_type
,
str
):
wanted
=
predicate_or_type
def
predicate
(
v
:
"FakeView"
)
->
bool
:
return
v
.
type_name
==
wanted
else
:
predicate
=
predicate_or_type
return
[
v
for
v
in
self
.
walk
()
if
predicate
(
v
)]
def
find_first
(
self
,
predicate_or_type
:
Any
)
->
Optional
[
"FakeView"
]:
"""Return the first view in this subtree matching a type name or predicate, or ``None``."""
found
=
self
.
find_all
(
predicate_or_type
)
return
found
[
0
]
if
found
else
None
def
dump
(
self
,
indent
:
int
=
0
)
->
str
:
"""Indented, human-readable subtree (for failing-test output)."""
line
=
" "
*
indent
+
repr
(
self
)
x
,
y
,
w
,
h
=
self
.
frame
if
w
or
h
:
line
+=
f" @(
{
x
:g
}
,
{
y
:g
}
{
w
:g
}
x
{
h
:g
}
)"
return
"
\n
"
.
join
([
line
,
*
(
c
.
dump
(
indent
+
2
)
for
c
in
self
.
children
)])
class
FakeBackend
:
"""Tag-table backend recording one tuple per applied mutation.
Args:
intrinsic: Override the intrinsic sizes used by
``measure_intrinsic`` (defaults to
[`DEFAULT_INTRINSIC`][pythonnative.testing.backend.DEFAULT_INTRINSIC]).
"""
def
__init__
(
self
,
intrinsic
:
Optional
[
Dict
[
str
,
Tuple
[
float
,
float
]]]
=
None
)
->
None
:
self
.
intrinsic
=
dict
(
DEFAULT_INTRINSIC
if
intrinsic
is
None
else
intrinsic
)
self
.
views
:
Dict
[
int
,
FakeView
]
=
{}
self
.
ops
:
List
[
Any
]
=
[]
self
.
batches
:
List
[
List
[
Any
]]
=
[]
self
.
measure_calls
:
List
[
int
]
=
[]
self
.
commands
:
List
[
Tuple
[
int
,
str
,
Dict
[
str
,
Any
]]]
=
[]
self
.
animated
:
List
[
Tuple
[
int
,
str
,
Any
]]
=
[]
self
.
last_create_props
:
Dict
[
str
,
Any
]
=
{}
self
.
last_update_changes
:
Dict
[
str
,
Any
]
=
{}
# ------------------------------------------------------------------
# Commit channel
# ------------------------------------------------------------------
def
apply_mutations
(
self
,
ops
:
Sequence
[
Mutation
])
->
None
:
"""Apply one committed batch to the view tree, recording each op in ``ops`` and ``batches``.
Raises ``AssertionError`` on malformed transactions (unknown tags, double creates or destroys).
"""
batch
:
List
[
Any
]
=
[]
for
op
in
ops
:
recorded
=
self
.
_apply_one
(
op
)
self
.
ops
.
append
(
recorded
)
batch
.
append
(
recorded
)
self
.
batches
.
append
(
batch
)
def
_apply_one
(
self
,
op
:
Mutation
)
->
Tuple
[
Any
, ...]:
if
isinstance
(
op
,
CreateOp
):
if
op
.
tag
in
self
.
views
:
raise
AssertionError
(
f"create: tag
{
op
.
tag
}
already registered"
)
view
=
FakeView
(
op
.
tag
,
op
.
type_name
,
op
.
props
)
self
.
views
[
op
.
tag
]
=
view
self
.
last_create_props
=
dict
(
op
.
props
)
return
(
"create"
,
op
.
type_name
,
view
.
id
)
if
isinstance
(
op
,
UpdateOp
):
view
=
self
.
_require
(
op
.
tag
,
"update"
)
view
.
props
.
update
(
op
.
changed_props
)
self
.
last_update_changes
=
dict
(
op
.
changed_props
)
return
(
"update"
,
view
.
type_name
,
view
.
id
,
tuple
(
sorted
(
op
.
changed_props
)))
if
isinstance
(
op
,
InsertOp
):
parent
=
self
.
_require
(
op
.
parent_tag
,
"insert_child"
)
child
=
self
.
_require
(
op
.
child_tag
,
"insert_child"
)
if
child
.
parent
is
not
None
:
child
.
parent
.
children
.
remove
(
child
)
child
.
parent
=
None
index
=
max
(
0
,
min
(
op
.
index
,
len
(
parent
.
children
)))
parent
.
children
.
insert
(
index
,
child
)
child
.
parent
=
parent
return
(
"insert_child"
,
parent
.
id
,
child
.
id
,
index
)
if
isinstance
(
op
,
DestroyOp
):
view
=
self
.
views
.
pop
(
op
.
tag
,
None
)
if
view
is
None
:
raise
AssertionError
(
f"destroy: unknown tag
{
op
.
tag
}
"
)
if
view
.
parent
is
not
None
:
view
.
parent
.
children
.
remove
(
view
)
view
.
parent
=
None
view
.
destroyed
=
True
return
(
"destroy"
,
view
.
id
)
if
isinstance
(
op
,
SetFrameOp
):
view
=
self
.
_require
(
op
.
tag
,
"set_frame"
)
view
.
frame
=
(
op
.
x
,
op
.
y
,
op
.
width
,
op
.
height
)
return
(
"set_frame"
,
view
.
id
,
op
.
x
,
op
.
y
,
op
.
width
,
op
.
height
)
raise
AssertionError
(
f"unknown mutation op:
{
op
!r
}
"
)
def
_require
(
self
,
tag
:
int
,
op_name
:
str
)
->
FakeView
:
view
=
self
.
views
.
get
(
tag
)
if
view
is
None
:
raise
AssertionError
(
f"
{
op_name
}
: unknown tag
{
tag
}
"
)
return
view
# ------------------------------------------------------------------
# Imperative escape hatches
# ------------------------------------------------------------------
def
resolve_view
(
self
,
tag
:
int
)
->
Optional
[
FakeView
]:
"""Return the live view registered under ``tag``, or ``None``."""
return
self
.
views
.
get
(
tag
)
def
measure_intrinsic
(
self
,
tag
:
int
,
max_width
:
float
,
max_height
:
float
)
->
Tuple
[
float
,
float
]:
"""Return the configured intrinsic size for the view's type and record the call in ``measure_calls``.
Unknown tags and types without an entry measure as ``(0.0, 0.0)``; the constraints are ignored.
"""
view
=
self
.
views
.
get
(
tag
)
if
view
is
None
:
return
(
0.0
,
0.0
)
self
.
measure_calls
.
append
(
view
.
id
)
return
self
.
intrinsic
.
get
(
view
.
type_name
, (
0.0
,
0.0
))
def
command
(
self
,
tag
:
int
,
name
:
str
,
args
:
Optional
[
Dict
[
str
,
Any
]]
=
None
)
->
Any
:
"""Record an imperative view command in ``commands`` and return ``None``."""
self
.
commands
.
append
((
tag
,
name
,
dict
(
args
or
{})))
view
=
self
.
views
.
get
(
tag
)
if
view
is
not
None
and
view
.
type_name
==
"VirtualList"
:
arguments
=
args
or
{}
if
name
==
"scroll_to_index"
:
self
.
request_list
(
tag
,
int
(
arguments
[
"index"
]))
elif
name
==
"scroll_to_end"
:
self
.
request_list
(
tag
,
max
(
0
,
len
(
view
.
props
[
"keys"
])
-
1
))
return
None
def
request_list
(
self
,
tag
:
int
,
first
:
int
,
*
,
extent
:
float
=
800.0
)
->
None
:
"""Simulate a native viewport using the shared list request protocol."""
from
..
events
import
dispatch_event
view
=
self
.
_require
(
tag
,
"request_list"
)
if
view
.
type_name
!=
"VirtualList"
:
raise
TypeError
(
"request_list requires a VirtualList tag"
)
keys
=
view
.
props
[
"keys"
]
heights
=
view
.
props
[
"row_heights"
]
first
=
max
(
0
,
min
(
first
,
len
(
keys
)
-
1
))
last
,
covered
=
first
,
0.0
while
last
<
len
(
keys
)
and
covered
<
extent
:
covered
+=
heights
[
last
]
last
+=
1
offset
=
sum
(
heights
[:
first
])
dispatch_event
(
tag
,
"on_scroll"
,
{
"first"
:
first
,
"last"
:
last
-
1
,
"extent"
:
extent
,
"range"
:
sum
(
heights
),
"x"
:
offset
if
view
.
props
.
get
(
"horizontal"
)
else
0
,
"y"
:
0
if
view
.
props
.
get
(
"horizontal"
)
else
offset
,
},
)
if
keys
:
dispatch_event
(
tag
,
"on_bind_row"
,
{
"key"
:
keys
[
first
],
"index"
:
first
,
"revision"
:
view
.
props
[
"revision"
],
"extent"
:
extent
,
"width"
:
view
.
frame
[
2
],
},
)
def
set_animated_property
(
self
,
tag
:
int
,
prop_name
:
str
,
value
:
Any
)
->
None
:
"""Record an animated property write in ``animated`` without touching ``props``."""
self
.
animated
.
append
((
tag
,
prop_name
,
value
))
def
start_animation
(
self
,
tag
:
int
,
anim_id
:
int
,
prop_name
:
str
,
spec
:
Dict
[
str
,
Any
])
->
bool
:
"""Decline native animation (return ``False``) so animations run through the Python driver."""
return
False
def
cancel_animation
(
self
,
tag
:
int
,
anim_id
:
int
)
->
Any
:
"""Do nothing; the fake never starts native animations."""
return
None
# ------------------------------------------------------------------
# Diagnostics
# ------------------------------------------------------------------
def
live_view_count
(
self
)
->
int
:
"""Return how many views are currently registered (created and not yet destroyed)."""
return
len
(
self
.
views
)
def
ops_of
(
self
,
kind
:
str
)
->
List
[
Any
]:
"""Every recorded op tuple whose first element is ``kind``."""
return
[
op
for
op
in
self
.
ops
if
op
[
0
]
==
kind
]
def
detached_views
(
self
,
type_name
:
Optional
[
str
]
=
None
)
->
List
[
FakeView
]:
"""Live views never inserted into a parent (``Portal`` overlays, the root)."""
out
=
[
v
for
v
in
self
.
views
.
values
()
if
v
.
parent
is
None
]
if
type_name
is
not
None
:
out
=
[
v
for
v
in
out
if
v
.
type_name
==
type_name
]
return
out
Back
|
FazBrowse Home
|
New Git URL