pause/tests/test_tray.py
agent 3b3b1c0f45 Fix top-bar icon showing as three dots
The AppIndicator extension looks up IconName with a fresh icon theme
whose search path is *only* our IconThemePath (data/icons), replacing
the system dirs. Without the system hicolor/index.theme it didn't know
about symbolic/apps, found nothing, and showed its "missing" placeholder.
GTK's own lookup keeps the system dirs, which is why this passed in
testing.

Ship an index.theme in data/icons/hicolor, and test that it lists the
folder the icons live in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F7w6vnQked3XNiTdTZDP8r
2026-10-26 14:05:54 +00:00

144 lines
6.2 KiB
Python

import configparser
import unittest
from gi.repository import Gio, GLib
from pause.app import ICON_DIR
from pause.tray import MENU_PATH, ITEM_PATH, WATCHER, MenuItem, Tray, separator
WATCHER_XML = Gio.DBusNodeInfo.new_for_xml(f"""
<node><interface name="{WATCHER}">
<method name="RegisterStatusNotifierItem"><arg name="service" type="s"/></method>
</interface></node>""")
class TrayTest(unittest.TestCase):
"""Talks to the tray over a private session bus, like the Shell extension does."""
def setUp(self):
self.bus = Gio.TestDBus.new(Gio.TestDBusFlags.NONE)
self.bus.up()
addr = self.bus.get_bus_address()
flags = (Gio.DBusConnectionFlags.AUTHENTICATION_CLIENT
| Gio.DBusConnectionFlags.MESSAGE_BUS_CONNECTION)
self.app_conn = Gio.DBusConnection.new_for_address_sync(addr, flags, None, None)
self.host = Gio.DBusConnection.new_for_address_sync(addr, flags, None, None)
self.clicked = []
self.tray = Tray(self.app_conn, "test.App", "Test", "/icons")
def tearDown(self):
self.tray.close()
self.app_conn.close_sync(None)
self.host.close_sync(None)
self.bus.down()
def spin(self, until):
ctx = GLib.MainContext.default()
for _ in range(200):
if until():
return
ctx.iteration(False)
GLib.usleep(5000)
self.fail("timed out waiting for D-Bus")
def call(self, path, iface, method, args=None):
# Async: the tray answers on this same main loop, so a sync call would deadlock.
result = []
self.host.call(self.app_conn.get_unique_name(), path, iface, method, args, None,
Gio.DBusCallFlags.NONE, 1000, None,
lambda conn, res: result.append(conn.call_finish(res)))
self.spin(lambda: result)
return result[0].unpack()
def prop(self, path, iface, name):
return self.call(path, "org.freedesktop.DBus.Properties", "Get",
GLib.Variant("(ss)", (iface, name)))[0]
def show(self, icon="icon-on", tooltip="Next break in 5 min"):
self.tray.update(icon, tooltip, [
MenuItem("status", tooltip),
separator("sep"),
MenuItem("now", "Take a Break Now", lambda: self.clicked.append("now")),
MenuItem("on", "Turn Back On", lambda: self.clicked.append("on"), visible=False),
])
def test_registers_when_watcher_appears(self):
registered = []
def on_call(conn, sender, path, iface, method, params, invocation):
registered.append((sender, params.unpack()[0]))
invocation.return_value(None)
self.host.register_object("/StatusNotifierWatcher", WATCHER_XML.interfaces[0],
on_call, None, None)
Gio.bus_own_name_on_connection(self.host, WATCHER, Gio.BusNameOwnerFlags.NONE,
None, None)
self.spin(lambda: registered)
self.assertEqual(registered, [(self.app_conn.get_unique_name(), ITEM_PATH)])
def test_item_properties(self):
self.show()
sni = "org.kde.StatusNotifierItem"
self.assertEqual(self.prop(ITEM_PATH, sni, "IconName"), "icon-on")
self.assertEqual(self.prop(ITEM_PATH, sni, "IconThemePath"), "/icons")
self.assertEqual(self.prop(ITEM_PATH, sni, "Menu"), MENU_PATH)
self.assertEqual(self.prop(ITEM_PATH, sni, "ToolTip"),
("", [], "Test", "Next break in 5 min"))
props = self.call(ITEM_PATH, "org.freedesktop.DBus.Properties", "GetAll",
GLib.Variant("(s)", (sni,)))[0]
self.assertTrue(props["ItemIsMenu"])
def test_layout(self):
self.show()
revision, (root_id, root_props, children) = self.call(
MENU_PATH, "com.canonical.dbusmenu", "GetLayout",
GLib.Variant("(iias)", (0, -1, [])))
self.assertEqual(root_id, 0)
self.assertEqual(root_props["children-display"], "submenu")
status, sep, now, on = [props for _, props, _ in children]
self.assertEqual(status, {"label": "Next break in 5 min", "enabled": False,
"visible": True})
self.assertEqual(sep, {"type": "separator"})
self.assertEqual(now["label"], "Take a Break Now")
self.assertTrue(now["enabled"])
self.assertFalse(on["visible"])
def test_click_runs_action(self):
self.show()
children = self.call(MENU_PATH, "com.canonical.dbusmenu", "GetLayout",
GLib.Variant("(iias)", (0, -1, [])))[1][2]
now_id = children[2][0]
self.call(MENU_PATH, "com.canonical.dbusmenu", "Event",
GLib.Variant("(isvu)", (now_id, "clicked", GLib.Variant("i", 0), 0)))
self.spin(lambda: self.clicked)
self.assertEqual(self.clicked, ["now"])
def test_signals_only_on_change(self):
signals = []
self.host.signal_subscribe(None, None, None, None, None, Gio.DBusSignalFlags.NONE,
lambda *a: signals.append(a[4]))
self.show()
self.spin(lambda: {"NewIcon", "NewToolTip", "LayoutUpdated"} <= set(signals))
signals.clear()
self.show() # same again
self.show(tooltip="Next break in 4 min")
self.spin(lambda: "LayoutUpdated" in signals)
self.assertEqual(sorted(signals), ["LayoutUpdated", "NewToolTip"])
class IconDirTest(unittest.TestCase):
def test_icons_findable_from_icon_theme_path_alone(self):
# The AppIndicator extension searches *only* IconThemePath, not the
# system icon dirs, so the theme there needs its own index.theme
# listing every folder an icon lives in.
index = configparser.ConfigParser()
self.assertTrue(index.read(ICON_DIR / "hicolor" / "index.theme"))
listed = index["Icon Theme"]["Directories"].split(",")
for name in ("net.reudy.Pause-symbolic", "net.reudy.Pause-off-symbolic"):
found = list((ICON_DIR / "hicolor").glob(f"*/*/{name}.svg"))
self.assertEqual(len(found), 1, name)
self.assertIn(str(found[0].parent.relative_to(ICON_DIR / "hicolor")), listed)
if __name__ == "__main__":
unittest.main()