mirror of
https://github.com/OrcaSlicer/OrcaSlicer.git
synced 2026-11-29 00:21:56 -05:00
The sweep's objects, presets, filaments, settings, project files, slicing, gizmos and GUI tools are tested by Python tests that compare the app's state after every step with the values their entries write out, in place of the replays, and the existing tests' checks that could not fail now can. The calls these tests found wrong now behave as described: moving one instance no longer sinks the others, a toolpath export writes the layers shown, an auto-answered dialog no longer reports its call blocked, and a web dialog that has just opened takes requests.
333 lines
20 KiB
Python
333 lines
20 KiB
Python
#!/usr/bin/env python3
|
|
"""Checks preferences_get and preferences_set against the Preferences dialog.
|
|
|
|
tools/mcp/orca_mcp.py launch --datadir <a scratch folder>
|
|
tools/mcp/test_preferences.py
|
|
|
|
On each of the dialog's tabs (window_open, then widget_list of the dialog), the controls named after a key are the keys
|
|
preferences_get lists for that tab. The MCP server's switch and port read as the dialog shows them: on, and the port this
|
|
session's launch gave. preferences_set writes a checkbox, a choice and a number in one call, and each reads back from
|
|
preferences_get and from its control in the dialog opened again. A preference read at the next start says so in
|
|
preferences_get and in the reply. A call with a good key and a refused one (the language) changes nothing, and so does a
|
|
value no choice has. The backup period runs the backup timer only while Auto backup is on, read from the timer's log
|
|
line. The downloads folder takes a folder this test makes in its own temporary folder, which preferences_get and the
|
|
dialog's row then show, and refuses one that does not exist; while the dialog is open a call cannot write it, and its
|
|
Browse button, answered with another such folder, shows that folder in the open dialog's row. Hide login side panel,
|
|
whose control has an effect in the app (Ops::apply_preference()): the Home page hides its login side panel as soon as
|
|
preferences_set writes it, without the dialog, and shows it again when it is written back. Last, preset sync and stealth
|
|
mode are written on and off again: with no account signed in, turning sync on starts no sync, and stealth mode signs out
|
|
of no account.
|
|
|
|
It writes only preferences whose effect stays inside the app it drives; it signs in to nothing and needs no network.
|
|
Exits 0 when every check passes.
|
|
"""
|
|
|
|
import os
|
|
import re
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
import orca_mcp # noqa: E402
|
|
from orca_mcp import McpError # noqa: E402
|
|
|
|
TABS = ["general", "files", "behaviour", "control", "graphics", "online", "developer"]
|
|
# A widget_list line of a control named after a key: its kind, its label and value, its name and its state.
|
|
CONTROL = re.compile(r'^\s*\[w\d+\] (\w+)(?: "(?P<label>[^"]*)")?(?: = "(?P<value>[^"]*)")? \([^)]*\) name=(?P<name>\S+)(?P<rest>.*)$')
|
|
# The home page's login side panel, as display reads it; "missing" where the page has no such element.
|
|
LOGIN_PANEL = "(function () { var e = document.getElementById('LeftBoard'); return e ? getComputedStyle(e).display : 'missing'; })()"
|
|
# What the backup timer logs when its period changes.
|
|
TIMER_LINE = "set_interval exit, and new interval is:"
|
|
|
|
results = []
|
|
|
|
|
|
def check(name, ok, detail=""):
|
|
results.append(bool(ok))
|
|
print(("PASS " if ok else "FAIL ") + name + ("" if ok else " " + str(detail)[:600]), flush=True)
|
|
|
|
|
|
def refusal(client, tool, **args):
|
|
"""The text of the error `tool` replies, or None when it runs."""
|
|
try:
|
|
client.call(tool, **args)
|
|
except McpError as e:
|
|
return str(e)
|
|
return None
|
|
|
|
|
|
def dialog_listing(client, tab, key=None):
|
|
"""The widget_list text of the Preferences dialog opened on `tab`, which it closes again."""
|
|
args = {"window": "preferences", "tab": tab}
|
|
if key:
|
|
args["key"] = key
|
|
opened = client.call("window_open", **args)
|
|
client.call("app_wait", **{"for": "dialog"})
|
|
_, texts = client.call_with_text("widget_list", target="dialog", max_nodes=2000)
|
|
client.call("dialog_answer", dialog={"title": "Preferences"}, button="close")
|
|
if isinstance(opened, dict) and opened.get("job"):
|
|
client.call("app_wait", **{"for": "job", "job": opened["job"], "timeout_s": 30})
|
|
return "\n".join(texts)
|
|
|
|
|
|
def dialog_controls(client, tab, key=None):
|
|
"""{name: (label, value, rest)} of the Preferences dialog's controls named after a key, opened on `tab`."""
|
|
controls = {}
|
|
for line in dialog_listing(client, tab, key).splitlines():
|
|
m = CONTROL.match(line)
|
|
if m:
|
|
controls[m.group("name")] = (m.group("label"), m.group("value"), m.group("rest"))
|
|
return controls
|
|
|
|
|
|
def items_by_key(client, **args):
|
|
return {item["key"]: item for item in client.call("preferences_get", **args)["items"]}
|
|
|
|
|
|
def shown_value(item, control):
|
|
"""What `item`'s control shows, in preferences_get's terms: True or False, a choice's value, a number."""
|
|
label, value, rest = control
|
|
if item["kind"] == "checkbox":
|
|
return "[checked]" in rest
|
|
if item["kind"] == "choice":
|
|
return next((c["value"] for c in item.get("choices", []) if c["label"] == value), value)
|
|
if item["kind"] == "number":
|
|
return float(value)
|
|
return value
|
|
|
|
|
|
def other_choice(item):
|
|
return next(c["value"] for c in item["choices"] if c["value"] != item["value"])
|
|
|
|
|
|
def wait_for(predicate, timeout):
|
|
deadline = time.time() + timeout
|
|
while True:
|
|
value = predicate()
|
|
if value or time.time() > deadline:
|
|
return value
|
|
time.sleep(0.25)
|
|
|
|
|
|
def log_lines(client):
|
|
return client.call("app_log", lines=2000).get("lines", [])
|
|
|
|
|
|
def new_lines(before, after):
|
|
"""The lines of `after` past the last line of `before`."""
|
|
start = after.index(before[-1]) + 1 if before and before[-1] in after else 0
|
|
return after[start:]
|
|
|
|
|
|
def main():
|
|
client, session = orca_mcp.session_client()
|
|
|
|
# ---- the dialog's controls named after keys are the keys preferences_get lists, tab by tab ----
|
|
every = client.call("preferences_get")
|
|
check("preferences_get with no argument lists the dialog's preferences of every tab",
|
|
{item["tab"] for item in every["items"]} == set(TABS) and every["total"] == len(every["items"]), every.get("total"))
|
|
for tab in TABS:
|
|
controls = dialog_controls(client, tab)
|
|
listed = items_by_key(client, tab=tab)
|
|
check(f"on {tab}, every control named after a key is a preference preferences_get lists",
|
|
set(controls) <= set(listed), sorted(set(controls) - set(listed)))
|
|
check(f"on {tab}, every preference preferences_get lists is a control of the tab",
|
|
set(listed) <= set(controls), sorted(set(listed) - set(controls)))
|
|
mismatched = [key for key in sorted(set(controls) & set(listed))
|
|
if listed[key]["kind"] in ("checkbox", "choice") and not listed[key].get("refused")
|
|
and shown_value(listed[key], controls[key]) != listed[key]["value"]]
|
|
check(f"on {tab}, each checkbox and choice shows the value preferences_get gives", not mismatched, mismatched)
|
|
|
|
# ---- the MCP server's switch and port, as the dialog shows them ----
|
|
# The harness launches the app with --mcp and --mcp-port, which store neither: the switch is checked while the server
|
|
# runs and the port field shows the port in use.
|
|
server = items_by_key(client, keys=["mcp_enabled", "mcp_port"])
|
|
controls = dialog_controls(client, "general", "mcp_enabled")
|
|
check("preferences_get gives the MCP server's switch on while it runs, as its control shows it",
|
|
server["mcp_enabled"]["value"] is True and "[checked]" in controls.get("mcp_enabled", ("", "", ""))[2],
|
|
(server["mcp_enabled"], controls.get("mcp_enabled")))
|
|
check("and its port the one this session's server is on, as its field shows it",
|
|
server["mcp_port"]["value"] == session["port"] and controls.get("mcp_port", (None, None, None))[1] == str(session["port"]),
|
|
(server["mcp_port"], controls.get("mcp_port"), session["port"]))
|
|
|
|
# ---- a checkbox, a choice and a number written in one call, read back ----
|
|
before = items_by_key(client, keys=["auto_arrange", "auto_calculate_flush", "opengl_fps_cap"])
|
|
values = {"auto_arrange": not before["auto_arrange"]["value"],
|
|
"auto_calculate_flush": other_choice(before["auto_calculate_flush"]),
|
|
"opengl_fps_cap": 45 if before["opengl_fps_cap"]["value"] != 45 else 30}
|
|
reply = client.call("preferences_set", values=values)
|
|
changed = {c["key"]: c for c in reply.get("changed", [])}
|
|
check("preferences_set replies each key with its value before and after",
|
|
set(changed) == set(values) and all(changed[k]["before"] == before[k]["value"] and changed[k]["after"] == values[k]
|
|
for k in values), reply)
|
|
after = items_by_key(client, keys=list(values))
|
|
check("preferences_get reads the three values written", all(after[k]["value"] == values[k] for k in values),
|
|
{k: after[k]["value"] for k in values})
|
|
for key in values:
|
|
controls = dialog_controls(client, after[key]["tab"], key)
|
|
shown = shown_value(after[key], controls[key]) if key in controls else None
|
|
check(f"the dialog opened again shows {key} as written", shown == values[key], (shown, values[key]))
|
|
again = client.call("preferences_set", values={"auto_arrange": values["auto_arrange"]})
|
|
check("a value already stored is left as it is and named unchanged",
|
|
again.get("changed") == [] and again.get("unchanged") == ["auto_arrange"], again)
|
|
|
|
# ---- a preference read at the next start says so ----
|
|
# Multi device management's row says "(Requires restart)": the value is stored at once and read at the next start.
|
|
multi = items_by_key(client, keys=["enable_multi_machine", "auto_arrange"])
|
|
check("preferences_get marks a preference read at the next start, and not another",
|
|
multi["enable_multi_machine"].get("restart_needed") is True and "restart_needed" not in multi["auto_arrange"], multi)
|
|
try:
|
|
reply = client.call("preferences_set", values={"enable_multi_machine": not multi["enable_multi_machine"]["value"],
|
|
"auto_arrange": not values["auto_arrange"]})
|
|
except McpError as e:
|
|
reply = {"error": str(e)}
|
|
changed = {c["key"]: c for c in reply.get("changed", [])}
|
|
check("preferences_set's reply marks it too, and not the other key",
|
|
changed.get("enable_multi_machine", {}).get("restart_needed") is True
|
|
and "restart_needed" not in changed.get("auto_arrange", {"restart_needed": None}), reply)
|
|
client.call("preferences_set", values={"enable_multi_machine": multi["enable_multi_machine"]["value"],
|
|
"auto_arrange": values["auto_arrange"]})
|
|
|
|
# ---- a refused key refuses the whole call ----
|
|
flipped = not values["auto_arrange"]
|
|
text = refusal(client, "preferences_set", values={"auto_arrange": flipped, "language": "de"})
|
|
check("a call with a good key and the language is refused, naming the language",
|
|
text is not None and "language" in text and "preference_refused" in text, text)
|
|
check("and the good key is unchanged", items_by_key(client, keys=["auto_arrange"])["auto_arrange"]["value"] == values["auto_arrange"])
|
|
text = refusal(client, "preferences_set", values={"auto_arrange": flipped, "auto_calculate_flush": "sometimes"})
|
|
check("a value no choice has is refused with the choice's values",
|
|
text is not None and "no_such_choice" in text and "color change" in text, text)
|
|
check("and nothing is written", items_by_key(client, keys=["auto_arrange"])["auto_arrange"]["value"] == values["auto_arrange"])
|
|
text = refusal(client, "preferences_set", values={"opengl_fps_cap": 1000})
|
|
check("a number outside its range is refused with the range", text is not None and "out_of_range" in text and "240" in text, text)
|
|
text = refusal(client, "preferences_set", values={"python_scripts_unrestricted": True})
|
|
check("the Python scripts' switch is no preference of the dialog", text is not None and "not_a_preference" in text, text)
|
|
|
|
# ---- the backup period runs the backup timer only while Auto backup is on ----
|
|
# The timer is read from the app's log: each change of it logs "set_interval exit, and new interval is: <s>"
|
|
# (_BBS_Backup_Manager::set_interval()), and a period of 0 stops it, as the app's start sets it with the switch off.
|
|
def timer_lines():
|
|
lines = client.call("app_log", lines=2000).get("lines", [])
|
|
return [line for line in lines if TIMER_LINE in line]
|
|
|
|
def timer(line):
|
|
return int(line.rsplit(":", 1)[1].strip())
|
|
|
|
def periods_set_by(values):
|
|
"""The timer's periods the call `values` logged, in order."""
|
|
before = timer_lines()
|
|
client.call("preferences_set", values=values)
|
|
after = timer_lines()
|
|
start = after.index(before[-1]) + 1 if before and before[-1] in after else 0
|
|
return [timer(line) for line in after[start:]]
|
|
|
|
stored = items_by_key(client, keys=["backup_interval"])["backup_interval"]["value"]
|
|
off, on, both = [p for p in (30, 31, 37, 41) if p != stored][:3]
|
|
client.call("preferences_set", values={"backup_switch": True})
|
|
client.call("preferences_set", values={"backup_switch": False})
|
|
logged = periods_set_by({"backup_interval": off})
|
|
check("with Auto backup off, preferences_set backup_interval leaves the backup timer off", logged == [0], logged)
|
|
client.call("preferences_set", values={"backup_switch": True})
|
|
logged = periods_set_by({"backup_interval": on})
|
|
check("with Auto backup on, the same write sets the timer to the period", logged == [on], logged)
|
|
logged = periods_set_by({"backup_switch": False, "backup_interval": both})
|
|
check("a call that turns Auto backup off and writes the period sees the switch it leaves: the timer is off, never the period",
|
|
logged and all(period == 0 for period in logged), logged)
|
|
|
|
# ---- the downloads folder takes the absolute path of an existing folder ----
|
|
# The folder is one this test makes in its own temporary folder; the dialog's row shows it as the Browse button
|
|
# leaves it, in the label beside the button.
|
|
folder = tempfile.mkdtemp(prefix="downloads_")
|
|
reply = client.call("preferences_set", values={"download_path": folder})
|
|
changed = reply.get("changed", [])
|
|
check("preferences_set download_path writes a folder that exists",
|
|
[c["key"] for c in changed] == ["download_path"] and changed[0]["after"] == folder, reply)
|
|
stored_path = items_by_key(client, keys=["download_path"])["download_path"]["value"]
|
|
check("preferences_get reads the folder back", stored_path == folder, stored_path)
|
|
listing = dialog_listing(client, "files", "download_path")
|
|
check("the dialog opened again shows the folder in its downloads row", f'label "{folder}" (wxStaticText)' in listing,
|
|
[line.strip() for line in listing.splitlines() if "wxStaticText" in line][:8])
|
|
missing = folder + "-missing"
|
|
text = refusal(client, "preferences_set", values={"download_path": missing})
|
|
check("a folder that does not exist is refused, naming it", text is not None and "not_a_folder" in text and missing in text, text)
|
|
stored_path = items_by_key(client, keys=["download_path"])["download_path"]["value"]
|
|
check("and the stored folder is unchanged", stored_path == folder, stored_path)
|
|
|
|
# While the Preferences dialog is open, a call that writes is refused before it runs (the dialog is modal), so only
|
|
# the dialog's Browse button changes the folder an open dialog shows: answered with another folder, its row shows it.
|
|
browsed = tempfile.mkdtemp(prefix="browsed_")
|
|
opened = client.call("window_open", window="preferences", tab="files", key="download_path")
|
|
client.call("app_wait", **{"for": "dialog"})
|
|
text = refusal(client, "preferences_set", values={"download_path": browsed})
|
|
check("with the Preferences dialog open, preferences_set is refused before it writes",
|
|
text is not None and "modal dialog is open" in text, text)
|
|
client.call("dialog_auto_answer", rules=[{"match": "Choose Download Directory", "button": "ok", "path": browsed}])
|
|
try:
|
|
client.call("widget_click", target={"name": "download_path", "within": "Preferences"})
|
|
_, texts = client.call_with_text("widget_list", target="dialog", max_nodes=2000)
|
|
listing = "\n".join(texts)
|
|
finally:
|
|
client.call("dialog_auto_answer", rules=[])
|
|
client.call("dialog_answer", dialog={"title": "Preferences"}, button="close")
|
|
if isinstance(opened, dict) and opened.get("job"):
|
|
client.call("app_wait", **{"for": "job", "job": opened["job"], "timeout_s": 30})
|
|
check("the open dialog's row shows the folder its Browse button picked", f'label "{browsed}" (wxStaticText)' in listing,
|
|
[line.strip() for line in listing.splitlines() if "wxStaticText" in line][:8])
|
|
stored_path = items_by_key(client, keys=["download_path"])["download_path"]["value"]
|
|
check("and preferences_get reads that folder", stored_path == browsed, stored_path)
|
|
|
|
# ---- an effect of a checkbox's control, without the dialog ----
|
|
# Hide login side panel's control sends the cloud providers to the Home page (WebViewPanel::SendCloudProvidersInfo()),
|
|
# which hides its login side panel; the effect runs through Ops::apply_preference() for the dialog and the call alike.
|
|
client.call("preferences_set", values={"hide_login_side_panel": False})
|
|
client.call("view_set", tab="home")
|
|
|
|
def panel():
|
|
try:
|
|
return client.call("webview_run_javascript", code=LOGIN_PANEL).get("result")
|
|
except McpError:
|
|
return None
|
|
|
|
# The page shows the panel with jQuery's show(), which leaves its display "block" (home.js).
|
|
shown = wait_for(lambda: panel() == "block", 20)
|
|
check("the Home page shows its login side panel", shown, panel())
|
|
client.call("preferences_set", values={"hide_login_side_panel": True})
|
|
hidden = wait_for(lambda: panel() == "none", 10)
|
|
check("preferences_set hide_login_side_panel true hides it at once, without the dialog", hidden, panel())
|
|
client.call("preferences_set", values={"hide_login_side_panel": False})
|
|
shown_again = wait_for(lambda: panel() == "block", 10)
|
|
check("and false shows it again", shown_again, panel())
|
|
client.call("view_set", tab="prepare")
|
|
|
|
# ---- preset sync and stealth mode, with no account signed in ----
|
|
# Each is written on and off again, as its control does, and read back from preferences_get and from the dialog. Their
|
|
# effects (GUI_App::start_sync_user_preset(), GUI_App::on_stealth_mode_enter()) act only for a signed-in account: the
|
|
# log shows that turning sync on started no sync ("start_sync_service...") and that stealth mode ran its effect. Sync
|
|
# goes first, while stealth mode is off, as the dialog leaves its checkbox enabled only then.
|
|
for key, effect in (("sync_user_preset", None), ("stealth_mode", "logout: on_stealth_mode_enter")):
|
|
start = items_by_key(client, keys=[key])[key]["value"]
|
|
for value in (not start, start):
|
|
before = log_lines(client)
|
|
try:
|
|
reply = client.call("preferences_set", values={key: value})
|
|
except McpError as e:
|
|
reply = {"error": str(e)}
|
|
logged = new_lines(before, log_lines(client))
|
|
check(f"preferences_set {key} {value} writes it", [c["key"] for c in reply.get("changed", [])] == [key], reply)
|
|
read = items_by_key(client, keys=[key])[key]["value"]
|
|
control = dialog_controls(client, "online", key).get(key)
|
|
check(f"and preferences_get and the dialog read {key} {value}",
|
|
read is value and control is not None and ("[checked]" in control[2]) is value, (read, control))
|
|
check(f"and writing {key} {value} starts no preset sync", not any("start_sync_service" in line for line in logged),
|
|
[line for line in logged if "sync" in line][:5])
|
|
if effect and value:
|
|
check(f"and {key} on runs its control's effect", any(effect in line for line in logged), logged[-10:])
|
|
|
|
print(f"\n{sum(results)}/{len(results)} passed")
|
|
return 0 if all(results) else 1
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|