tests: the focus test gives the X input focus itself, as the window manager the private Xvfb lacks

On the private Xvfb no window manager runs, so nobody calls XSetInputFocus: the
focus stays PointerRoot and GDK activates the toplevel under the pointer
(has_pointer_focus when no _NET_SUPPORTING_WM_CHECK), while Firefox presents a
window (gtk_window_present_with_time, XSetInputFocus without a WM) only when it
is not active already. XGetInputFocus therefore read PointerRoot for the whole
wait although the first page had the focus.

The test now does the window manager's part on Linux: it finds the window the
step names by its _NET_WM_NAME and gives it the X input focus (the first
session's, the second session's, and the session's own new window as a focus
on map), then reads the focus back before judging; on Windows the same steps
ask for the foreground as before. X errors no longer end the process.
This commit is contained in:
feder-cr committed 2026-11-22 12:57:09 +01:00
1 parent bc6d861f62
commit 6f0a3af9fd
2 files changed
+105 -45

No files matched your search

+74 -14
View File
@@ -5,7 +5,10 @@ first know that the OS did give the focus to the second session's window; otherw
it, or took it later than the read). The OS answers that without the engine:
* Linux: the X input focus (``XGetInputFocus`` on the display the sessions share); the display is a private Xvfb started for the test (``a_display_of_its_own``),
so no other test's headed window moves the focus in it;
so no other test's headed window moves the focus in it. No window manager runs there, so nobody ever sets the X input focus: it stays ``PointerRoot``
and GDK activates whichever toplevel is under the pointer (``has_pointer_focus``, ``gdk/x11/gdkdevicemanager-core-x11.c``), and Firefox raises a window
itself only when it is not active already (``nsWindow::SetFocus``, ``gtk_window_present_with_time``). The test therefore does what a window manager does
when the user clicks a window: ``XSetInputFocus`` on the toplevel the step names (found by its ``_NET_WM_NAME``), and reads ``XGetInputFocus`` back;
* Windows: the foreground window (``GetForegroundWindow``), the window of the session found by the title its page gave it.
"""
from __future__ import annotations
@@ -17,8 +20,23 @@ import sys
import threading
from typing import Iterator, Optional
#: ``None`` and ``PointerRoot``: what the X input focus is when no client holds it.
_X_NO_WINDOW = (0, 1)
#: ``RevertToParent`` and ``CurrentTime`` of ``XSetInputFocus`` (as GDK's own ``gdk_x11_window_focus`` without a window manager); ``IsViewable``.
_REVERT_TO_PARENT, _CURRENT_TIME, _IS_VIEWABLE = 2, 0, 2
class _XWindowAttributes(ctypes.Structure):
_fields_ = [("x", ctypes.c_int), ("y", ctypes.c_int), ("width", ctypes.c_int), ("height", ctypes.c_int), ("border_width", ctypes.c_int),
("depth", ctypes.c_int), ("visual", ctypes.c_void_p), ("root", ctypes.c_ulong), ("c_class", ctypes.c_int), ("bit_gravity", ctypes.c_int),
("win_gravity", ctypes.c_int), ("backing_store", ctypes.c_int), ("backing_planes", ctypes.c_ulong), ("backing_pixel", ctypes.c_ulong),
("save_under", ctypes.c_int), ("colormap", ctypes.c_ulong), ("map_installed", ctypes.c_int), ("map_state", ctypes.c_int),
("all_event_masks", ctypes.c_long), ("your_event_mask", ctypes.c_long), ("do_not_propagate_mask", ctypes.c_long),
("override_redirect", ctypes.c_int), ("screen", ctypes.c_void_p)]
#: An X error (a window gone between two requests, a focus asked for a window not yet viewable) is ignored instead of ending the process as Xlib's
#: default handler does; the wait that asked reads the focus again on its next look.
_X_ERROR_HANDLER = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p)
_ignore_x_error = _X_ERROR_HANDLER(lambda display, event: 0)
@contextlib.contextmanager
@@ -50,23 +68,68 @@ def a_display_of_its_own(monkeypatch) -> Iterator[str]:
class XFocus:
"""The X input focus of a display, read through a connection of its own (libX11, as the engine's own client library)."""
"""The X input focus of a display, read and given through a connection of its own (libX11, as the engine's own client library)."""
def __init__(self, display: str) -> None:
self.x11 = ctypes.CDLL("libX11.so.6")
self.x11.XOpenDisplay.restype, self.x11.XOpenDisplay.argtypes = ctypes.c_void_p, (ctypes.c_char_p,)
self.x11.XGetInputFocus.argtypes = (ctypes.c_void_p, ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_int))
self.x11.XCloseDisplay.argtypes = (ctypes.c_void_p,)
self.connection = self.x11.XOpenDisplay(display.encode())
x11 = self.x11 = ctypes.CDLL("libX11.so.6")
ulong_p, int_p = ctypes.POINTER(ctypes.c_ulong), ctypes.POINTER(ctypes.c_int)
x11.XOpenDisplay.restype, x11.XOpenDisplay.argtypes = ctypes.c_void_p, (ctypes.c_char_p,)
x11.XSetErrorHandler.argtypes = (_X_ERROR_HANDLER,)
x11.XDefaultRootWindow.restype, x11.XDefaultRootWindow.argtypes = ctypes.c_ulong, (ctypes.c_void_p,)
x11.XInternAtom.restype, x11.XInternAtom.argtypes = ctypes.c_ulong, (ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int)
x11.XQueryTree.argtypes = (ctypes.c_void_p, ctypes.c_ulong, ulong_p, ulong_p, ctypes.POINTER(ulong_p), ctypes.POINTER(ctypes.c_uint))
x11.XGetWindowAttributes.argtypes = (ctypes.c_void_p, ctypes.c_ulong, ctypes.POINTER(_XWindowAttributes))
x11.XGetWindowProperty.argtypes = (ctypes.c_void_p, ctypes.c_ulong, ctypes.c_ulong, ctypes.c_long, ctypes.c_long, ctypes.c_int, ctypes.c_ulong,
ulong_p, int_p, ulong_p, ulong_p, ctypes.POINTER(ctypes.c_void_p))
x11.XFree.argtypes = (ctypes.c_void_p,)
x11.XSetInputFocus.argtypes = (ctypes.c_void_p, ctypes.c_ulong, ctypes.c_int, ctypes.c_ulong)
x11.XSync.argtypes = (ctypes.c_void_p, ctypes.c_int)
x11.XGetInputFocus.argtypes = (ctypes.c_void_p, ulong_p, int_p)
x11.XCloseDisplay.argtypes = (ctypes.c_void_p,)
x11.XSetErrorHandler(_ignore_x_error)
self.connection = x11.XOpenDisplay(display.encode())
assert self.connection, "no connection to the display %r" % display
self.root = x11.XDefaultRootWindow(self.connection)
self.net_wm_name = x11.XInternAtom(self.connection, b"_NET_WM_NAME", False)
self.utf8_string = x11.XInternAtom(self.connection, b"UTF8_STRING", False)
def holder(self) -> int:
window, revert = ctypes.c_ulong(), ctypes.c_int()
self.x11.XGetInputFocus(self.connection, ctypes.byref(window), ctypes.byref(revert))
return int(window.value)
def is_a_window(self, holder: int) -> bool:
return holder not in _X_NO_WINDOW
def _title(self, window: int) -> str:
kind, size, count, left, data = ctypes.c_ulong(), ctypes.c_int(), ctypes.c_ulong(), ctypes.c_ulong(), ctypes.c_void_p()
if self.x11.XGetWindowProperty(self.connection, window, self.net_wm_name, 0, 1024, False, self.utf8_string, ctypes.byref(kind),
ctypes.byref(size), ctypes.byref(count), ctypes.byref(left), ctypes.byref(data)) != 0 or not data.value:
return ""
try:
return ctypes.string_at(data.value, count.value).decode("utf-8", "replace") if size.value == 8 else ""
finally:
self.x11.XFree(data)
def _viewable(self, window: int) -> bool:
attributes = _XWindowAttributes()
return bool(self.x11.XGetWindowAttributes(self.connection, window, ctypes.byref(attributes))) and attributes.map_state == _IS_VIEWABLE
def window_titled(self, title: str) -> Optional[int]:
"""The one viewable toplevel (a child of the root: no window manager reparents it) whose ``_NET_WM_NAME`` starts with ``title``, or None."""
root, parent, children, count = ctypes.c_ulong(), ctypes.c_ulong(), ctypes.POINTER(ctypes.c_ulong)(), ctypes.c_uint()
if not self.x11.XQueryTree(self.connection, self.root, ctypes.byref(root), ctypes.byref(parent), ctypes.byref(children), ctypes.byref(count)):
return None
try:
windows = [int(children[i]) for i in range(count.value)]
finally:
if children:
self.x11.XFree(children)
found = [window for window in windows if self._title(window).startswith(title) and self._viewable(window)]
return found[0] if len(found) == 1 else None
def ask_for_the_foreground(self, window: int) -> int:
"""What a window manager does when the user clicks the window (none runs on this display): ``XSetInputFocus`` on it, then the focus is read back."""
self.x11.XSetInputFocus(self.connection, window, _REVERT_TO_PARENT, _CURRENT_TIME)
self.x11.XSync(self.connection, False)
return self.holder()
def close(self) -> None:
self.x11.XCloseDisplay(self.connection)
@@ -87,9 +150,6 @@ class ForegroundWindow:
def holder(self) -> int:
return self.user32.GetForegroundWindow() or 0
def is_a_window(self, holder: int) -> bool:
return bool(holder)
def window_titled(self, title: str) -> Optional[int]:
"""The one visible top-level Firefox window whose title starts with ``title``, or None (none, or more than one)."""
found = []
@@ -19,8 +19,11 @@ Both sessions are headed on one display, and the test owns it: on Linux a privat
share is never used, so no neighbour's headed window moves the focus); on Windows the interactive desktop of the runner, which only this test touches because
it is ``wall_clock`` (the e2e run gives such a test the machine alone, after the parallel tests).
What the verdict waits for is the operating system, not a pause: before anything is judged the test reads from the OS that the focus really is where the step
says (the X input focus on Linux, ``GetForegroundWindow`` on Windows: first the first session's window, then the second session's window). "No blur" is judged
What the verdict waits for is the operating system, not a pause: before anything is judged the test asks the OS for the focus of the window the step names,
as a user's click does, and reads back that the OS gave it (``GetForegroundWindow`` on Windows, the X input focus on Linux: first the first session's window,
then the second session's window). On Linux no window manager runs on the private Xvfb, so nobody sets the X input focus (it stays ``PointerRoot`` and GDK
activates the toplevel under the pointer, which is not a step the test can name): the test does the window manager's part, ``XSetInputFocus`` on the window
(also on the session's own new window, as a window manager's focus on map). "No blur" is judged
after that and after a round trip to the first page's own process (two painted frames), which handles everything the parent sent it before: the deactivation
that would cause a blur has been delivered by then, so a late one cannot give a false green.
"""
@@ -47,9 +50,9 @@ HAS_FOCUS = "() => document.hasFocus()"
EVENTS = "() => window.__events"
#: A round trip to the page's own process: it has handled what the parent sent it before this ran (the deactivation, if the parent had one to send).
ROUND_TRIP = "() => new Promise(done => requestAnimationFrame(() => requestAnimationFrame(done)))"
#: Names the page, so that on Windows its top-level window is found by its title.
#: Names the page, so that its top-level window is found by its title (the window text on Windows, ``_NET_WM_NAME`` on X).
NAME = "(name) => { document.title = name; return document.title; }"
FIRST_TITLE, SECOND_TITLE = "focus-first-session", "focus-second-session"
FIRST_TITLE, SECOND_TITLE, OWN_TITLE = "focus-first-session", "focus-second-session", "focus-own-page"
@contextlib.contextmanager
@@ -66,37 +69,29 @@ def the_os_focus(monkeypatch):
reader.close()
def takes_the_focus(b):
"""Runs in the second session's own thread: opens a window, names it, and waits until its page has the focus."""
def opens_a_named_window(b):
"""Runs in the second session's own thread: opens a window and names it; gives its page."""
taker = b.new_page()
taker.goto("about:blank")
taker.evaluate(NAME, SECOND_TITLE)
return taker
def its_page_has_the_focus(taker):
"""Runs in the second session's own thread: waits until its page has the focus."""
wait_for_condition(taker, lambda: taker.evaluate(HAS_FOCUS), lambda focused: focused is True, "the second session's page never took the focus")
def os_focus_is_on_the_first_window(first, os_focus):
"""Waits until the OS says the first session's window holds the focus; gives what holds it."""
if sys.platform == "win32":
window = wait_for_condition(first, lambda: os_focus.window_titled(FIRST_TITLE), lambda hwnd: hwnd is not None,
"no single visible top-level window is titled %r" % FIRST_TITLE).history[-1][1]
wait_for_condition(first, lambda: os_focus.ask_for_the_foreground(window), lambda holder: holder == window,
"the first session's window never became the foreground window")
return window
seen = wait_for_condition(first, os_focus.holder, os_focus.is_a_window, "no window of the display ever held the X input focus")
return seen.history[-1][1]
def os_focus_is_on_the_window_titled(page, os_focus, title):
"""Waits until the OS says the one window titled ``title`` holds the focus, asking for it as a user's click does; gives that window.
def os_focus_is_on_the_second_window(first, os_focus, before):
"""Waits until the OS says a window other than the first one holds the focus (on Windows: the second session's window, by its title)."""
if sys.platform == "win32":
window = wait_for_condition(first, lambda: os_focus.window_titled(SECOND_TITLE), lambda hwnd: hwnd is not None,
"no single visible top-level window is titled %r" % SECOND_TITLE).history[-1][1]
assert window != before, "the second session's window is the first one's"
wait_for_condition(first, lambda: os_focus.ask_for_the_foreground(window), lambda holder: holder == window,
"the second session's window never became the foreground window")
return
wait_for_condition(first, os_focus.holder, lambda holder: os_focus.is_a_window(holder) and holder != before,
"the X input focus never left the first session's window %r" % before)
Windows: the foreground window (``SetForegroundWindow``, which the system may refuse: the read decides). Linux: no window manager runs on the private
Xvfb, so the X input focus stays ``PointerRoot`` unless a client sets it; the test sets it as a window manager would (``XSetInputFocus``)."""
window = wait_for_condition(page, lambda: os_focus.window_titled(title), lambda found: found is not None,
"no single visible top-level window is titled %r" % title).history[-1][1]
wait_for_condition(page, lambda: os_focus.ask_for_the_foreground(window), lambda holder: holder == window,
"the window titled %r never held the focus of the OS" % title)
return window
def another_session_takes_the_display(firefox_binary, os_focus, keeps, with_own_page=False):
@@ -107,11 +102,12 @@ def another_session_takes_the_display(firefox_binary, os_focus, keeps, with_own_
first.goto("about:blank")
first.evaluate(NAME, FIRST_TITLE)
first.evaluate(RECORD)
before = os_focus_is_on_the_window_titled(first, os_focus, FIRST_TITLE)
wait_for_condition(first, lambda: first.evaluate(HAS_FOCUS), lambda focused: focused is True, "the page never had the focus of its own window")
before = os_focus_is_on_the_first_window(first, os_focus)
with in_a_thread_of_its_own(firefox_binary) as b:
b.run(takes_the_focus)
os_focus_is_on_the_second_window(first, os_focus, before)
taker = b.run(opens_a_named_window)
assert os_focus_is_on_the_window_titled(first, os_focus, SECOND_TITLE) != before, "the second session's window is the first one's"
b.run(lambda _: its_page_has_the_focus(taker))
if keeps:
# the OS has moved the focus; one round trip to the page's process orders the read after the deactivation that would have followed
first.evaluate(ROUND_TRIP)
@@ -122,6 +118,10 @@ def another_session_takes_the_display(firefox_binary, os_focus, keeps, with_own_
if with_own_page:
own = a.new_page()
own.goto("about:blank")
own.evaluate(NAME, OWN_TITLE)
if sys.platform != "win32":
# Windows gives the session's new window the foreground itself; on the private Xvfb only a window manager would (focus on map)
os_focus_is_on_the_window_titled(own, os_focus, OWN_TITLE)
wait_for_condition(own, lambda: (own.evaluate(HAS_FOCUS), first.evaluate(EVENTS), first.evaluate(HAS_FOCUS)),
lambda state: state[0] is True and state[1].count("blur") == 1 and state[2] is False,
"a page of the session's own did not take the focus from the first with one blur")