Guides for automated testing

Is it possible to use GDK for automated testing?

I need to programatically:

  • Send key press / key release events
  • Move the mouse, including drag and drop (click, drag and then drop/unclick)

In theory I think it’s possible.

I google it and did not find much material.

Does any app does it?

Thanks.

No, that’s not what GDK is for.

You can inject events into the GTK event queue—with GTK3—only for your application; you cannot test other processes. The main issue is that complex operations, like drag and drop, are made of long sequences of events and involve far too much machinery to effectively be automated.

Not really, no.

Some frameworks and applications may use the accessibility API to perform actions such as “click this button” or “put some text into an input field”; you can look at Dog Tail for a testing framework that does that.

Pitivi has quite a lot of tests for the GUI performed by directly calling the event handlers or the virtual methods. See for example in https://gitlab.gnome.org/GNOME/pitivi/blob/master/tests/test_timeline_markers.py how the do_* methods are called.

GSequencer has got functional UI tests.

http://git.savannah.nongnu.org/cgit/gsequencer.git/tree/ags/test/X?h=3.0.x

I have written a small article related:

Here is the example:

The test runner needs its very own thread and lock your UI mutex. The magic behind it is:

void
ags_test_enter()
{
  g_rec_mutex_lock(ags_test_get_driver_mutex());
  gdk_threads_enter();
}

void
ags_test_leave()
{
  gdk_threads_leave();
  g_rec_mutex_unlock(ags_test_get_driver_mutex());
}

Further I have got a GSource function frequently lock/unlock the test driver mutex. This is where the test runner may do its job. The source function needs to be attached to Gtk±3 main context, which can be retrieved by calling g_main_context_default().

gboolean
ags_functional_test_util_driver_dispatch(GSource *source,
                                         GSourceFunc callback,
                                         gpointer user_data)
{
  g_main_context_iteration(g_main_context_default(),
                           FALSE);

  g_rec_mutex_unlock(ags_test_get_driver_mutex());

  usleep(4000);

  g_rec_mutex_lock(ags_test_get_driver_mutex());

  g_main_context_iteration(g_main_context_default(),
                           FALSE);

  return(G_SOURCE_CONTINUE);
}

Note I just migrated to Gtk±3 and have to verify everything still works as expected.

Tests using Gtk±2:

Tests using Gtk±3.0:

best regards,
Joël

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.