choroba has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. I'm trying to give an old Tk application some love, and I noticed it had many tests that nobody had run for many years: many of them were failing.

I fixed all of them, and to prevent the same situation in the future, I decided to run the tests in GitHub Actions. One of the tests needs the Tk application to run, so I decided to run it headless in Xvfb.

The test failed in Xvfb, not only in GH Actions, but even when I ran it locally. In a real X session, it passed.

I was able to reduce the test to the following:

#!/usr/bin/perl use strict; use warnings; use Tk; my $mw = 'Tk::MainWindow'->new; my $current = 0; my @buttons = map { my $i = $_; $mw->Button(-text => $i, -command => sub { $current = $i } )->pack } 1, 2; $mw->after(100, sub { $mw->eventGenerate('<KeyPress>', -keysym => $_) for qw( Tab Return ); $mw->idletasks(); $mw->DESTROY; }); Tk::MainLoop(); use Test::More tests => 1; is $current, 1, 'Events processed';

Steps to reproduce:

$ tktest.pl 1..1 ok 1 - Events processed $ Xvfb :19 & $ pid=$! $ DISPLAY=:19 tktest.pl 1..1 not ok 1 - Events processed # Failed test 'Events processed' # at /home/choroba/tktest.pl line 27. # got: '0' # expected: '1' # Looks like you failed 1 test of 1. $ kill $pid

Update: Under Xvnc, the behaviour is the same as under Xvfb, i.e. the events are ignored, even when I run the test from an xterm in a vnc viewer.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re: Headless testing of Tk under Xvfb (Solved)
by choroba (Cardinal) on Feb 19, 2025 at 15:50 UTC
    Nevermind, it seems
    $mw->focusForce;
    is needed before the events are generated. Mere focus is not enough.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]