Files
dikte/tests/test_trayicon.py
T
yusufipek e40191e3a7 Give the tray an icon a black bar cannot swallow
A session that names no desktop, which is what i3 and a bare X11 login are,
leaves Qt with hicolor as its only icon theme, and hicolor has none of the four
names the tray asks for. So fromTheme returns nothing and the shapes drawn in
trayicon.py are used, as they are on macOS. They were drawn in black, which
macOS recolours through the mask and X11 does not, and i3's bar is black: the
icon was there all along, painted onto a bar of its own colour. Outside macOS
they are now white over a dark copy of themselves spread a pixel out, which
stands out on a dark bar and stays readable on a light one, and the mask is set
only where something reads it.

The .desktop files had the same hole from the other side. They named
audio-input-microphone, which is in Breeze and in Adwaita but not in hicolor, so
the menu entry and the autostart entry were blank on the same systems. install.sh
now draws the application icon into ~/.local/share/icons/hicolor and both entries
name it; uninstall.sh takes it back. The windows carry it too on X11, where a
window has no .desktop file to be looked up in.

Closes #27
2026-08-16 13:28:57 +03:00

156 lines
6.4 KiB
Python

"""The drawn icons, checked against the thing that made them necessary.
A tray never says what colour it is painting over, so the only question worth
asking of these pixmaps is whether they can be seen at all: a glyph drawn in
black on i3's black bar is an empty slot, which is what issue #27 was. So the
test blends each icon onto a bar of its own and asks whether anything of it
survives, once over black and once over white.
"""
import sys
import tempfile
import unittest
from unittest import mock
from PyQt6.QtGui import QImage
from PyQt6.QtWidgets import QApplication
import trayicon
from tests.support import DikteTest
# One application for the whole run; Qt allows no second one.
_app = QApplication.instance() or QApplication([])
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Breeze's own panel, as the middle case: not every bar is at one end.
CHARCOAL = (0x31, 0x36, 0x3B)
def _pixels(pixmap):
image = pixmap.toImage().convertToFormat(QImage.Format.Format_ARGB32)
for y in range(image.height()):
for x in range(image.width()):
yield image.pixelColor(x, y)
def _stands_out_from(pixmap, background):
"""True when some pixel of this icon differs from the bar behind it.
The icons are transparent everywhere they are not drawn, so the tray gets
the blend rather than the pixmap, and a pixel that blends back into the bar
is a pixel nobody sees. 60 out of 255 is the gap being asked for, which is
well under black against white and well over the antialiasing at an edge.
"""
for colour in _pixels(pixmap):
weight = colour.alphaF()
ink = (colour.red(), colour.green(), colour.blue())
for channel, behind in zip(ink, background):
if abs(channel * weight + behind * (1 - weight) - behind) > 60:
return True
return False
def _drawn(pixmap):
"""True when anything at all was painted onto this pixmap."""
return any(colour.alpha() > 128 for colour in _pixels(pixmap))
class Tray(DikteTest):
"""The four state icons, on a session whose theme has none of them."""
def setUp(self):
super().setUp()
# Held between calls on purpose, so a test does not read what the one
# before it drew on another platform.
self.patch_attr(trayicon, "_cache", {})
def test_a_name_we_do_not_draw_is_a_null_icon(self):
# dikte.py asks the theme first and falls through to here, so anything
# answered with a picture would be one the theme should have given.
self.assertTrue(trayicon.icon("emblem-important").isNull())
def test_every_state_has_a_shape(self):
for name in trayicon.SHAPES:
with self.subTest(name=name):
icon = trayicon.icon(name)
self.assertFalse(icon.isNull())
for size in trayicon.SIZES:
self.assertTrue(_drawn(icon.pixmap(size, size)))
def test_visible_on_a_bar_of_any_colour(self):
# The regression: on X11 the icon is composited over a bar whose colour
# nobody declares, and i3's is black.
with mock.patch.object(sys, "platform", "linux"):
for name in trayicon.SHAPES:
for size in trayicon.SIZES:
pixmap = trayicon.icon(name).pixmap(size, size)
for background in (BLACK, WHITE, CHARCOAL):
with self.subTest(name=name, size=size, bar=background):
self.assertTrue(_stands_out_from(pixmap, background))
def test_x11_is_not_handed_a_mask(self):
# Only macOS recolours one. Setting it elsewhere would promise a
# recolouring that never comes, and the outline would be the only thing
# keeping the icon visible either way.
with mock.patch.object(sys, "platform", "linux"):
self.assertFalse(trayicon.icon("media-record").isMask())
def test_macos_gets_a_flat_black_stencil(self):
# There the colour is thrown away and only the coverage is read, so an
# outline would come back as part of the glyph.
with mock.patch.object(sys, "platform", "darwin"):
icon = trayicon.icon("audio-input-microphone")
self.assertTrue(icon.isMask())
for colour in _pixels(icon.pixmap(22, 22)):
if colour.alpha() > 128:
self.assertEqual(
(colour.red(), colour.green(), colour.blue()), BLACK)
def test_the_two_platforms_do_not_share_a_cached_icon(self):
with mock.patch.object(sys, "platform", "darwin"):
self.assertTrue(trayicon.icon("media-record").isMask())
with mock.patch.object(sys, "platform", "linux"):
self.assertFalse(trayicon.icon("media-record").isMask())
class ApplicationIcon(DikteTest):
"""The picture the menu entry, the task bar and the Finder are given."""
def test_written_where_every_desktop_looks(self):
with tempfile.TemporaryDirectory() as root:
written = trayicon.write_hicolor(root)
self.assertEqual(len(written), len(trayicon.HICOLOR_SIZES))
for size, path in zip(trayicon.HICOLOR_SIZES, written):
with self.subTest(size=size):
self.assertEqual(
path.parts[-3:], (f"{size}x{size}", "apps", "dikte.png"))
self.assertTrue(path.is_file())
image = QImage(str(path))
self.assertEqual((image.width(), image.height()),
(size, size))
def test_the_installed_name_is_the_one_the_entries_use(self):
# install.sh writes Icon=dikte into both .desktop files, and a name that
# matches no installed file is the blank slot all over again.
with tempfile.TemporaryDirectory() as root:
self.assertTrue(
all(path.name == "dikte.png"
for path in trayicon.write_hicolor(root)))
def test_a_tile_rather_than_a_stencil(self):
# Coloured on purpose: this one is composited onto backgrounds that are
# nothing like a tray, so it carries its own ground.
pixmap = trayicon.app_pixmap(64)
for background in (BLACK, WHITE):
self.assertTrue(_stands_out_from(pixmap, background))
def test_offered_at_the_sizes_a_window_asks_for(self):
icon = trayicon.app_icon()
self.assertFalse(icon.isNull())
self.assertIn(48, [size.width() for size in icon.availableSizes()])
if __name__ == "__main__":
unittest.main()