81 lines
2.7 KiB
Python
Executable File
81 lines
2.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""Host integration test for the charger plug/unplug status bar refresh."""
|
|
import os
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
|
|
BASE = pathlib.Path(__file__).resolve().parent.parent
|
|
HELPER = BASE / "overlay/rootfs/usr/bin/bananarchy-battery-charge-event.sh"
|
|
RULES = BASE / "overlay/rootfs/etc/udev/rules.d/85-bananarchy-battery.rules"
|
|
HOOK = BASE / "overlay/rootfs/usr/share/sxmo/default_hooks/sxmo_hook_battery.sh"
|
|
|
|
HOOK_STUB = '''#!/bin/sh
|
|
printf '%s\\n' "$*" >> "$FAKE_ROOT/hook.log"
|
|
exit 0
|
|
'''
|
|
|
|
|
|
def main():
|
|
for path in (HELPER, RULES, HOOK):
|
|
if not path.is_file():
|
|
sys.stderr.write("FAIL: missing %s\n" % path)
|
|
return 1
|
|
if subprocess.run(["sh", "-n", str(HELPER)]).returncode or \
|
|
subprocess.run(["sh", "-n", str(HOOK)]).returncode:
|
|
sys.stderr.write("FAIL: shell syntax\n")
|
|
return 1
|
|
rules = RULES.read_text(encoding="utf-8")
|
|
if "bananarchy-battery-charge-event.sh" not in rules or \
|
|
'KERNEL=="pm8916-lbc-chgr"' not in rules or \
|
|
"ENV{POWER_SUPPLY_ONLINE}" not in rules:
|
|
sys.stderr.write("FAIL: charger uevent rules are not wired\n")
|
|
return 1
|
|
hook = HOOK.read_text(encoding="utf-8")
|
|
if '"force"' not in hook or 'FORCE_UPDATE=1' not in hook or \
|
|
'rm -f "$ICON_FILE"' not in hook:
|
|
sys.stderr.write("FAIL: battery hook cannot force an icon republish\n")
|
|
return 1
|
|
print("[ok] udev rules and forced battery hook")
|
|
with tempfile.TemporaryDirectory(prefix="bananarchy-battery-") as tmp:
|
|
root = pathlib.Path(tmp)
|
|
stub = root / "hook.sh"
|
|
stub.write_text(HOOK_STUB, encoding="utf-8")
|
|
stub.chmod(0o755)
|
|
log = root / "hook.log"
|
|
env = dict(os.environ)
|
|
env.update({
|
|
"FAKE_ROOT": str(root),
|
|
"BANANARCHY_BATTERY_HOOK": str(stub),
|
|
"BANANARCHY_BATTERY_LOCK": str(root / "charge.lock"),
|
|
"BANANARCHY_BATTERY_RECHECK_DELAY": "0",
|
|
"DISPLAY": ":0",
|
|
"XDG_RUNTIME_DIR": str(root / "runtime"),
|
|
"HOME": str(root / "home"),
|
|
})
|
|
res = subprocess.run(["sh", str(HELPER)], env=env,
|
|
capture_output=True, text=True)
|
|
if res.returncode:
|
|
sys.stderr.write("FAIL: helper exited %d: %s\n" % (res.returncode, res.stderr))
|
|
return 1
|
|
deadline = time.time() + 3
|
|
calls = []
|
|
while time.time() < deadline:
|
|
calls = log.read_text().splitlines() if log.exists() else []
|
|
if len(calls) >= 2:
|
|
break
|
|
time.sleep(0.1)
|
|
if calls != ["force", "force"]:
|
|
sys.stderr.write("FAIL: expected an immediate and one delayed forced "
|
|
"refresh, got %r\n" % calls)
|
|
return 1
|
|
print("[ok] immediate + delayed forced charge icon refresh")
|
|
print("battery charger event: all checks passed")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|