Files
geminux/scripts/test-power-policy.py
T

216 lines
8.0 KiB
Python
Executable File

#!/usr/bin/env python3
"""Verify zero-poll daemon design and Geminux power/lid-suspend policy logic."""
import os
import re
import shutil
import subprocess
import sys
import tempfile
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DAEMON_DIR = os.path.join(BASE_DIR, "src", "geminux-daemons")
POLICY_C = os.path.join(DAEMON_DIR, "geminux-power-policy.c")
GESTURE_C = os.path.join(DAEMON_DIR, "geminux-touch-gesture.c")
DAEMON_FILES = {
"geminux-powerd.c": "epoll_wait",
"geminux-statusd.c": "epoll_wait",
"geminux-touchd.c": "epoll_wait",
"geminux-ccci-daemon.c": "epoll_wait",
"geminux-modem-ctl.c": "epoll_wait",
"geminux-bt-obex.c": "g_main_loop_run",
"geminux-leds.c": "epoll_wait",
}
C_TEST = r"""
#include <assert.h>
#include <string.h>
#include "geminux-power-policy.h"
#include "geminux-touch-gesture.h"
static int count_decision(const geminux_gesture_action_t *a, int mode) {
int c = 0;
for (int i = 0; i < a->decision_count; i++) {
if (a->decisions[i].allow_mode == mode) c++;
}
return c;
}
static void test_touch_gestures(void) {
geminux_gesture_state_t gs;
geminux_gesture_action_t act;
/* 1. 1-finger tap -> REJECT passthrough, no synthetic click */
geminux_gesture_init(&gs, true);
geminux_gesture_on_begin(&gs, 2, 100, 500.0, 400.0, 0, &act);
assert(gs.phase == GESTURE_PENDING_1F);
assert(act.decision_count == 0);
assert(act.next_timer_ms == GEMINUX_LONGPRESS_MS);
geminux_gesture_on_end(&gs, 2, 100, 504.0, 403.0, 90, &act);
assert(act.decision_count == 1);
assert(act.decisions[0].devid == 2 && act.decisions[0].touchid == 100);
assert(act.decisions[0].allow_mode == GEMINUX_ALLOW_REJECT);
assert(act.click_button == 0);
assert(gs.phase == GESTURE_IDLE);
/* 2. 1-finger drag -> held during 45ms grace, REJECTed on timer */
geminux_gesture_init(&gs, true);
geminux_gesture_on_begin(&gs, 2, 101, 500.0, 400.0, 1000, &act);
geminux_gesture_on_update(&gs, 2, 101, 550.0, 400.0, 1020, &act);
assert(gs.phase == GESTURE_PENDING_1F);
assert(act.decision_count == 0);
assert(act.next_timer_ms == 25);
geminux_gesture_on_timer(&gs, 1045, &act);
assert(gs.phase == GESTURE_PASSTHROUGH_1F);
assert(count_decision(&act, GEMINUX_ALLOW_REJECT) == 1);
geminux_gesture_on_end(&gs, 2, 101, 600.0, 400.0, 1150, &act);
assert(act.click_button == 0);
assert(gs.phase == GESTURE_IDLE);
/* 3. 1-finger 500ms stationary hold -> ACCEPT + Button3 right-click */
geminux_gesture_init(&gs, true);
geminux_gesture_on_begin(&gs, 2, 102, 700.0, 300.0, 2000, &act);
geminux_gesture_on_timer(&gs, 2500, &act);
assert(gs.phase == GESTURE_ACCEPTED_1F_HOLD);
assert(count_decision(&act, GEMINUX_ALLOW_ACCEPT) == 1);
assert(act.click_button == 3);
geminux_gesture_on_end(&gs, 2, 102, 705.0, 302.0, 2600, &act);
assert(act.click_button == 0);
assert(gs.phase == GESTURE_IDLE);
/* 4. 2-finger tap -> ACCEPT both + Button3 right-click on release */
geminux_gesture_init(&gs, true);
geminux_gesture_on_begin(&gs, 2, 201, 600.0, 400.0, 3000, &act);
geminux_gesture_on_begin(&gs, 2, 202, 680.0, 410.0, 3015, &act);
assert(gs.phase == GESTURE_ACCEPTED_MULTI);
assert(count_decision(&act, GEMINUX_ALLOW_ACCEPT) == 2);
assert(act.next_timer_ms == 0);
geminux_gesture_on_end(&gs, 2, 201, 604.0, 402.0, 3110, &act);
assert(act.click_button == 0);
geminux_gesture_on_end(&gs, 2, 202, 684.0, 412.0, 3120, &act);
assert(act.click_button == 3);
assert(gs.phase == GESTURE_IDLE);
/* 5. 3-finger tap -> ACCEPT all 3 + Button2 middle-click on release */
geminux_gesture_init(&gs, true);
geminux_gesture_on_begin(&gs, 2, 301, 500.0, 400.0, 4000, &act);
geminux_gesture_on_begin(&gs, 2, 302, 600.0, 400.0, 4010, &act);
assert(count_decision(&act, GEMINUX_ALLOW_ACCEPT) == 2);
geminux_gesture_on_begin(&gs, 2, 303, 700.0, 400.0, 4025, &act);
assert(count_decision(&act, GEMINUX_ALLOW_ACCEPT) == 1);
geminux_gesture_on_end(&gs, 2, 301, 505.0, 402.0, 4120, &act);
geminux_gesture_on_end(&gs, 2, 302, 605.0, 402.0, 4135, &act);
assert(act.click_button == 0);
geminux_gesture_on_end(&gs, 2, 303, 705.0, 402.0, 4150, &act);
assert(act.click_button == 2);
assert(gs.phase == GESTURE_IDLE);
/* 6. 2-finger drag -> natural vertical & horizontal scroll ticks, no tap click */
int total_v = 0, total_h = 0;
geminux_gesture_init(&gs, true);
geminux_gesture_on_begin(&gs, 2, 401, 600.0, 300.0, 5000, &act);
geminux_gesture_on_begin(&gs, 2, 402, 680.0, 300.0, 5010, &act);
geminux_gesture_on_update(&gs, 2, 401, 730.0, 410.0, 5080, &act);
total_v += act.wheel_v;
total_h += act.wheel_h;
geminux_gesture_on_update(&gs, 2, 402, 810.0, 410.0, 5081, &act);
total_v += act.wheel_v;
total_h += act.wheel_h;
assert(gs.scrolled);
assert(total_v == 2);
assert(total_h == 2);
geminux_gesture_on_end(&gs, 2, 401, 730.0, 410.0, 5140, &act);
geminux_gesture_on_end(&gs, 2, 402, 810.0, 410.0, 5150, &act);
assert(act.click_button == 0);
assert(gs.phase == GESTURE_IDLE);
}
int main(void) {
geminux_power_policy_t pol;
geminux_policy_init(&pol);
assert(pol.profile == POWER_PROFILE_BALANCED);
assert(pol.lid_suspend_enabled);
assert(!geminux_should_suspend_on_lid(&pol));
/* Closing lid on battery should trigger S3 suspend */
pol.lid_closed = true;
assert(geminux_should_suspend_on_lid(&pol));
/* Active voice call inhibitor blocks lid suspend */
assert(geminux_policy_add_inhibit(&pol, "call"));
assert(!geminux_should_suspend_on_lid(&pol));
assert(geminux_policy_del_inhibit(&pol, "call"));
assert(geminux_should_suspend_on_lid(&pol));
/* Connected external HDMI display blocks lid suspend */
pol.hdmi_connected = true;
assert(!geminux_should_suspend_on_lid(&pol));
pol.hdmi_connected = false;
assert(geminux_should_suspend_on_lid(&pol));
/* Profile parameters: eco disables A72 big cluster, balanced/perf enable A72 */
geminux_profile_params_t eco = geminux_policy_params(POWER_PROFILE_ECO, false);
assert(strcmp(eco.governor, "schedutil") == 0);
assert(!eco.a72_online);
assert(eco.suspend_timeout_s > 0);
geminux_profile_params_t bal = geminux_policy_params(POWER_PROFILE_BALANCED, false);
assert(strcmp(bal.governor, "schedutil") == 0);
assert(bal.a72_online);
geminux_profile_params_t perf = geminux_policy_params(POWER_PROFILE_PERFORMANCE, true);
assert(strcmp(perf.governor, "schedutil") == 0);
assert(perf.a72_online);
assert(perf.suspend_timeout_s == 0);
test_touch_gestures();
return 0;
}
"""
def main():
for fname, required_loop in DAEMON_FILES.items():
path = os.path.join(DAEMON_DIR, fname)
with open(path, "r", encoding="utf-8") as handle:
src = handle.read()
for bad in (r"\bsleep\s*\(", r"\busleep\s*\(", r"\bnanosleep\s*\("):
if re.search(bad, src):
raise AssertionError(f"Busy-wait sleep call ({bad}) detected in {fname}")
if required_loop not in src:
raise AssertionError(f"Expected event loop primitive {required_loop} missing in {fname}")
with tempfile.TemporaryDirectory(prefix="geminux-power-test-") as tmp:
src = os.path.join(tmp, "test_power.c")
bin_path = os.path.join(tmp, "test_power")
with open(src, "w", encoding="utf-8") as handle:
handle.write(C_TEST)
gcc = shutil.which("gcc")
if gcc:
subprocess.run(
[gcc, "-std=c11", "-Wall", "-Wextra", "-Werror",
"-I", DAEMON_DIR, src, POLICY_C, GESTURE_C, "-lm", "-o", bin_path],
check=True,
)
subprocess.run([bin_path], check=True)
else:
subprocess.run(
["docker", "run", "--rm", "-v", f"{BASE_DIR}:/geminux:ro",
"-v", f"{tmp}:/work", "alpine:3.24", "sh", "-eu", "-c",
"apk add --no-cache build-base >/dev/null && "
"gcc -std=c11 -Wall -Wextra -Werror -I/geminux/src/geminux-daemons "
"/work/test_power.c /geminux/src/geminux-daemons/geminux-power-policy.c "
"/geminux/src/geminux-daemons/geminux-touch-gesture.c -lm "
"-o /work/test_power && /work/test_power"],
check=True,
)
print("[PASS] Zero-poll daemon loops, power/lid policy, and touch gestures verified")
return 0
if __name__ == "__main__":
sys.exit(main())