Repository navigation
Wait for a battery event after a failed charge-limit write #20879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Cynnamoroll
wants to merge
2
commits into
omacom:quattro
Choose a base branch
from
Cynnamoroll:contributions/power-wave3-5e635d50
base: quattro
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import os | ||
| from pathlib import Path | ||
| import subprocess | ||
| import sys | ||
| from tempfile import TemporaryDirectory | ||
|
|
||
| from gi.repository import Gio, GLib | ||
|
|
||
| UPOWER = "org.freedesktop.UPower" | ||
| DEVICE = UPOWER + ".Device" | ||
| ROOT = "/org/freedesktop/UPower" | ||
| BATTERY = ROOT + "/devices/battery_BAT0" | ||
| bus = Gio.bus_get_sync(Gio.BusType.SESSION) | ||
| loop = GLib.MainLoop() | ||
| props = { | ||
| "IsPresent": GLib.Variant("b", True), | ||
| "PowerSupply": GLib.Variant("b", True), | ||
| "ChargeThresholdSupported": GLib.Variant("b", True), | ||
| "ChargeThresholdEnabled": GLib.Variant("b", False), | ||
| "Percentage": GLib.Variant("d", 100.0), | ||
| "State": GLib.Variant("u", 1), # Charging; 100% also finishes a top-up. | ||
| } | ||
| attempts = 0 | ||
| accept_threshold = False | ||
|
|
||
|
|
||
| def method_call(connection, sender, path, interface, method, params, invocation): | ||
| global attempts | ||
| if method == "EnumerateDevices": | ||
| invocation.return_value(GLib.Variant("(ao)", ([BATTERY],))) | ||
| elif method == "GetDisplayDevice": | ||
| invocation.return_value(GLib.Variant("(o)", (BATTERY,))) | ||
| elif method == "EnableChargeThreshold": | ||
| attempts += 1 | ||
| assert params.unpack() == (True,) | ||
| if accept_threshold: | ||
| props["ChargeThresholdEnabled"] = GLib.Variant("b", True) | ||
| invocation.return_value(None) | ||
| else: | ||
| invocation.return_dbus_error("org.freedesktop.UPower.GeneralError", "test threshold write rejected") | ||
|
|
||
|
|
||
| def get_property(connection, sender, path, interface, name): | ||
| return GLib.Variant("b", False) if name == "OnBattery" else props[name] | ||
|
|
||
|
|
||
| def register(path, interface, methods, properties): | ||
| xml = f'<node><interface name="{interface}">{methods}' + "".join( | ||
| f'<property name="{name}" type="{value.get_type_string()}" access="read"/>' | ||
| for name, value in properties.items() | ||
| ) + "</interface></node>" | ||
| bus.register_object(path, Gio.DBusNodeInfo.new_for_xml(xml).interfaces[0], method_call, get_property) | ||
|
|
||
|
|
||
| register(ROOT, UPOWER, """ | ||
| <method name="EnumerateDevices"><arg type="ao" direction="out"/></method> | ||
| <method name="GetDisplayDevice"><arg type="o" direction="out"/></method> | ||
| """, {"OnBattery": GLib.Variant("b", False)}) | ||
| register(BATTERY, DEVICE, """ | ||
| <method name="EnableChargeThreshold"><arg type="b" direction="in"/></method> | ||
| """, props) | ||
| bus.call_sync("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", | ||
| "RequestName", GLib.Variant("(su)", (UPOWER, 0)), None, 0, 5000, None) | ||
|
|
||
|
|
||
| def update_percentage(percentage): | ||
| changed = {"Percentage": GLib.Variant("d", percentage)} | ||
| props.update(changed) | ||
| bus.emit_signal(None, BATTERY, "org.freedesktop.DBus.Properties", "PropertiesChanged", | ||
| GLib.Variant("(sa{sv}as)", (DEVICE, changed, []))) | ||
|
|
||
|
|
||
| with TemporaryDirectory() as tmp: | ||
| home = Path(tmp) | ||
| state = home / "state" | ||
| marker = state / "omarchy" / "battery" / "top-up-once" | ||
| marker.parent.mkdir(parents=True) | ||
| marker.write_text("1\n") | ||
| repo = Path(sys.argv[1]) | ||
| config = home / "config" | ||
| config.mkdir() | ||
| (config / "battery").symlink_to(repo / "shell/plugins/services/battery", target_is_directory=True) | ||
| (config / "Commons").symlink_to(repo / "shell/Commons", target_is_directory=True) | ||
| (config / "shell.qml").write_text('import Quickshell\nimport "battery" as Battery\nShellRoot { Battery.Service {} }\n') | ||
| (home / "run").mkdir(mode=0o700) | ||
| env = {**os.environ, "DBUS_SYSTEM_BUS_ADDRESS": os.environ["DBUS_SESSION_BUS_ADDRESS"], | ||
| "HOME": tmp, "XDG_RUNTIME_DIR": str(home / "run"), "XDG_CONFIG_HOME": str(home / ".config"), | ||
| "XDG_CACHE_HOME": str(home / ".cache"), "XDG_STATE_HOME": str(state), | ||
| "OMARCHY_BATTERY_STATE_DIR": str(marker.parent), "OMARCHY_POWER_SUPPLY_PATH": str(home / "sys"), | ||
| "QT_QPA_PLATFORM": "offscreen", "QML_IMPORT_PATH": str(repo / "shell"), | ||
| "QS_DISABLE_CRASH_HANDLER": "1", "PATH": f"{repo}/bin:{os.environ['PATH']}"} | ||
| with (home / "quickshell.log").open("w+") as log: | ||
| shell = subprocess.Popen(["quickshell", "-p", str(config), "--no-color"], stdout=log, stderr=log, env=env) | ||
| try: | ||
| # Dispatch the real Quickshell battery service and its real CLI on a | ||
| # private bus. The fixture only supplies the battery and write outcome. | ||
| GLib.timeout_add(1200, loop.quit) | ||
| loop.run() | ||
| assert shell.poll() is None, "Quickshell exited before settling" | ||
| assert attempts == 1, f"expected one failed charge-limit write, saw {attempts}" | ||
| assert marker.exists(), "a failed write must keep the request pending" | ||
|
|
||
| update_percentage(95.0) | ||
| GLib.timeout_add(200, loop.quit) | ||
| loop.run() | ||
| assert attempts == 1, "charging below the limit must not retry" | ||
|
|
||
| accept_threshold = True | ||
| update_percentage(100.0) | ||
| GLib.timeout_add(1000, loop.quit) | ||
| loop.run() | ||
| assert attempts == 2, f"a new full-charge event must retry exactly once, saw {attempts}" | ||
| assert not marker.exists(), "a successful retry must clear the pending request" | ||
| except Exception: | ||
| log.seek(0) | ||
| print(log.read()[-5000:], file=sys.stderr) | ||
| raise | ||
| finally: | ||
| shell.terminate() | ||
| shell.wait(timeout=5) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.