104 lines
6.1 KiB
QML
104 lines
6.1 KiB
QML
pragma ComponentBehavior: Bound
|
|
import QtQuick
|
|
import Quickshell
|
|
import "."
|
|
|
|
ShellRoot {
|
|
id: test
|
|
property var controller: null
|
|
property var discovery: null
|
|
property var adapter: null
|
|
property int scenario: -1
|
|
property int step: 0
|
|
property int successes: 0
|
|
property bool finished: false
|
|
readonly property var names: ["status and exact action", "BlueZ error", "malformed response", "watchdog", "invalid action", "close stops own search", "discovery deadline", "foreign search untouched", "disabled adapter", "discovery startup error", "adapter change stops lease", "startup timeout", "close before startup", "missing discovery executable"]
|
|
Component { id: controls; BluetoothController { backendCommand: ["python3", Quickshell.shellPath("bluetooth_fake.py"), "success"]; timeoutMs: 2000 } }
|
|
Component { id: scans; BluetoothDiscovery { backendCommand: ["python3", Quickshell.shellPath("bluetooth_fake.py"), "success"]; active: true; startTimeoutMs: 1000 } }
|
|
Component { id: adapters; QtObject { property bool enabled: true; property bool discovering: false; property string dbusPath: "/org/bluez/hci1" } }
|
|
Connections {
|
|
target: test.controller
|
|
function onSucceeded(action) { test.successes++; }
|
|
function onBusyChanged() { if (!test.controller.busy) Qt.callLater(test.advance); }
|
|
}
|
|
Connections {
|
|
target: test.discovery
|
|
function onOwnedChanged() { if (test.discovery.owned) Qt.callLater(test.advance); }
|
|
function onBusyChanged() { if (!test.discovery.busy) Qt.callLater(test.advance); }
|
|
function onStoppingChanged() { if (!test.discovery.stopping) Qt.callLater(test.advance); }
|
|
}
|
|
function check(value, message) { if (!value) { finished = true; console.error("BLUETOOTH_TEST_FAIL: " + message); Qt.exit(1); throw new Error(message); } }
|
|
function complete() { console.log("PASS Bluetooth: " + names[scenario]); Qt.callLater(next); }
|
|
function next() {
|
|
if (controller) controller.destroy();
|
|
if (discovery) discovery.destroy();
|
|
if (adapter) adapter.destroy();
|
|
controller = null; discovery = null; adapter = null;
|
|
scenario++; step = 0; successes = 0;
|
|
if (scenario === names.length) { finished = true; console.log("BLUETOOTH_TEST_PASS: 14 scenarios"); Qt.quit(); return; }
|
|
if (scenario <= 4) {
|
|
controller = controls.createObject(test);
|
|
const mode = ["success", "failure", "malformed", "stall", "success"][scenario];
|
|
controller.backendCommand = ["python3", Quickshell.shellPath("bluetooth_fake.py"), mode];
|
|
if (scenario === 3) controller.timeoutMs = 100;
|
|
if (scenario === 4) { check(!controller.start("bad command", ""), "unknown action accepted"); complete(); return; }
|
|
check(controller.refresh(), "refresh rejected");
|
|
} else {
|
|
adapter = adapters.createObject(test);
|
|
discovery = scans.createObject(test, {adapter: adapter});
|
|
if (scenario === 7) {
|
|
adapter.discovering = true;
|
|
check(!discovery.start(), "foreign search joined unexpectedly");
|
|
discovery.active = false;
|
|
check(adapter.discovering, "foreign search stopped"); complete(); return;
|
|
}
|
|
if (scenario === 8) {
|
|
adapter.enabled = false;
|
|
check(!discovery.start() && !discovery.busy, "disabled adapter searched"); complete(); return;
|
|
}
|
|
if (scenario === 9) discovery.backendCommand = ["python3", Quickshell.shellPath("bluetooth_fake.py"), "failure"];
|
|
if (scenario === 6) discovery.durationMs = 100;
|
|
if (scenario === 11 || scenario === 12) {
|
|
discovery.backendCommand = ["python3", Quickshell.shellPath("bluetooth_fake.py"), "stall"];
|
|
discovery.startTimeoutMs = 100;
|
|
}
|
|
if (scenario === 13) { discovery.backendCommand = ["adaptive-nonexistent-executable"]; discovery.startTimeoutMs = 100; }
|
|
check(discovery.start(), "discovery rejected");
|
|
check(!discovery.start(), "duplicate discovery accepted");
|
|
if (scenario === 12) discovery.active = false;
|
|
}
|
|
}
|
|
function advance() {
|
|
if (finished) return;
|
|
if (scenario <= 4) {
|
|
if (!controller || controller.busy) return;
|
|
if (scenario === 0) {
|
|
if (step === 0) {
|
|
check(controller.readiness === "ready" && controller.adapterCount === 2, "status lost");
|
|
step = 1;
|
|
check(controller.start("connect", "/org/bluez/hci1/dev_AA_BB_CC_DD_EE_FF"), "connect rejected");
|
|
check(!controller.refresh(), "duplicate command accepted"); return;
|
|
}
|
|
check(successes === 1 && controller.error === "", "action failed");
|
|
} else if (scenario === 1) check(controller.error.includes("Mock BlueZ NotReady") && controller.readiness === "unavailable", "backend failure lost");
|
|
else if (scenario === 2) check(controller.error !== "" && controller.readiness === "unavailable", "invalid response accepted");
|
|
else if (scenario === 3) check(controller.timedOut && controller.error !== "", "watchdog failed");
|
|
} else {
|
|
if (!discovery) return;
|
|
if (discovery.owned && step === 0) {
|
|
step = 1;
|
|
if (scenario === 5) discovery.active = false;
|
|
else if (scenario === 10) discovery.adapter = null;
|
|
return;
|
|
}
|
|
if (discovery.busy || discovery.stopping) return;
|
|
if (scenario === 9) check(discovery.error.includes("Mock BlueZ NotReady"), "discovery error lost");
|
|
else if (scenario === 11 || scenario === 13) check(!discovery.owned && discovery.error !== "", "startup failure not handled");
|
|
else check(!discovery.owned && discovery.error === "", "discovery cleanup failed");
|
|
}
|
|
complete();
|
|
}
|
|
Component.onCompleted: Qt.callLater(next)
|
|
Timer { interval: 18000; running: !test.finished; onTriggered: test.check(false, "harness timeout") }
|
|
}
|