-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_runtime_ui_slice10.py
More file actions
155 lines (136 loc) · 6.8 KB
/
test_runtime_ui_slice10.py
File metadata and controls
155 lines (136 loc) · 6.8 KB
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
from __future__ import annotations
import json
import urllib.parse
import urllib.request
from Implementations.Reference.Runtime.python.cli import execute_example10_contract
from Implementations.Reference.Runtime.python.ui_runtime import (
ButtonBrowserUiRuntime,
ButtonRuntimeCore,
build_runtime,
default_example10_contract_path,
default_example10_wfrog_path,
)
def test_python_example10_headless_button_press_to_boolean() -> None:
runtime = ButtonRuntimeCore(
contract_path=default_example10_contract_path(),
wfrog_path=default_example10_wfrog_path(),
)
artifact = runtime.execute(False)
assert artifact["contract_ref"]["source_ref"]["example_id"] == "10_button_press_to_boolean"
assert artifact["outputs"]["public"]["pressed"] is False
assert artifact["outputs"]["ui"]["trigger_button"] is False
assert artifact["outputs"]["ui"]["pressed_indicator"] is False
artifact = runtime.execute(True)
assert artifact["execution_summary"]["trigger_pressed"] is True
assert artifact["outputs"]["public"]["pressed"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is True
assert artifact["outputs"]["ui"]["pressed_indicator"] is True
button = next(widget for widget in artifact["ui_runtime"]["widgets"] if widget["widget_id"] == "trigger_button")
assert button["class_ref"] == "frog.widgets.button"
assert button["runtime"]["event.pressed"] is True
assert button["runtime"]["value"] is True
def test_python_example10_cli_contract_entrypoint() -> None:
artifact = execute_example10_contract(True)
assert artifact["outputs"]["public"]["pressed"] is True
assert artifact["outputs"]["ui"]["pressed_indicator"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is True
def test_python_example10_browser_ui_consumes_default_svg_and_source_styles() -> None:
runtime = ButtonBrowserUiRuntime(
contract_path=default_example10_contract_path(),
wfrog_path=default_example10_wfrog_path(),
open_browser=False,
)
html = runtime.render_html()
assert "Button Switch Until Released" in html
assert "Python reference runtime" in html
assert "button contract executor" in html
assert 'data-runtime-language="python"' in html
assert 'data-execution-path="python_button_contract_executor"' in html
assert "data-class-ref='frog.widgets.button'" in html
assert "data-class-ref='frog.widgets.boolean_indicator'" in html
assert "data-asset-route='/asset/button_rectangular_svg'" in html
assert "data-asset-route='/asset/boolean_circular_svg'" in html
assert "data-frog-template=\"frog.realizations.default.button.rectangular\"" in html
assert "class='button-skin'" in html
assert "data-frog-asset-consumed='true'" in html
assert "data-frog-mechanical-action='switch_until_released'" in html
assert "data-frog-pressed-applies-when-value-true='true'" in html
assert "data-frog-pressed-applies-while-active='false'" in html
assert "data-frog-hover-applies-when-value-false-only='false'" in html
assert "data-frog-part='caption' data-svg-anchor='caption.anchor'" in html
assert "data-frog-part='state_text' data-svg-anchor='state_text.center'" in html
assert "class='button-press-overlay' type='button'" in html
assert "data-frog-part='face' data-frog-event='pressed' data-frog-public-input-id='trigger_pressed'" in html
assert "data-frog-host-overlay='input' data-frog-align-to-part='face'" in html
assert "--frog-button-face-fill:#e2e8f0;" in html
assert "--frog-button-face-hover-fill:#f1f5f9;" in html
assert "--frog-button-face-pressed-fill:#e2e8f0;" in html
assert "--frog-button-face-stroke-width:1px;" in html
assert "--frog-button-pressed-inset:0px;" in html
assert "--frog-button-state-text-font-weight:400;" in html
assert "--frog-button-caption-font-size:18px;" in html
assert "--frog-button-caption-font-weight:600;" in html
assert "--boolean-caption-font-size:18px;" in html
assert "--boolean-text-font-size:12px;" in html
assert "--boolean-text-font-weight:400;" in html
assert "--boolean-inner-border-width:0px;" in html
assert "fetch(\"/event\"" in html
assert 'mechanicalAction !== "switch_until_released"' in html
assert 'mechanicalAction !== "latch_until_released"' in html
assert 'publishEvent("press")' in html
assert 'publishEvent("release")' in html
assert "pointerdown" in html
assert "pointerup" in html
assert ">OFF</span>" in html
assert ">FALSE</span>" in html
assert "font-size:14px" not in html
assert "top:49px" not in html
assert "type='submit'" not in html
assert "missing-skin" not in html
assert "fallback" not in html
assert "Current runtime snapshot" not in html
assert "<pre>" not in html
runtime.run_once(True)
html = runtime.render_html()
assert "data-frog-visual-state='true'" in html
assert ">ON</span>" in html
assert ">TRUE</span>" in html
def test_python_example10_event_endpoint_is_momentary() -> None:
runtime = ButtonBrowserUiRuntime(
contract_path=default_example10_contract_path(),
wfrog_path=default_example10_wfrog_path(),
open_browser=False,
)
httpd, thread = runtime.serve_in_thread()
host, port = httpd.server_address
try:
body = urllib.parse.urlencode({"frog_event": "press", "trigger_pressed": "false"}).encode("utf-8")
request = urllib.request.Request(
f"http://{host}:{port}/event",
data=body,
headers={"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"},
method="POST",
)
with urllib.request.urlopen(request, timeout=5) as response:
artifact = json.loads(response.read().decode("utf-8"))
assert artifact["outputs"]["public"]["pressed"] is True
assert artifact["outputs"]["ui"]["trigger_button"] is True
assert artifact["outputs"]["ui"]["pressed_indicator"] is True
body = urllib.parse.urlencode({"frog_event": "release", "trigger_pressed": "true"}).encode("utf-8")
request = urllib.request.Request(
f"http://{host}:{port}/event",
data=body,
headers={"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"},
method="POST",
)
with urllib.request.urlopen(request, timeout=5) as response:
artifact = json.loads(response.read().decode("utf-8"))
assert artifact["outputs"]["public"]["pressed"] is False
assert artifact["outputs"]["ui"]["pressed_indicator"] is False
finally:
httpd.shutdown()
httpd.server_close()
thread.join(timeout=5)
def test_python_runtime_builder_dispatches_example10() -> None:
runtime = build_runtime(example="10", open_browser=False)
assert isinstance(runtime, ButtonBrowserUiRuntime)