Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use C language to test the function of Gtk and its application

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use C language to test the function of Gtk and applications. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.

How to test the functionality of your application.

Automated testing is used to ensure the quality of your program and to make it run as expected. Unit testing only tests a part of your algorithm and does not focus on the adaptability of the components. This is why there is functional testing, which is sometimes referred to as integration testing.

Functional testing simply interacts with your user interface, whether it's a website or a desktop application. To show how functional testing works, let's take testing a Gtk+ application as an example. For simplicity, we use examples from Gtk+ 2.0 tutorials in this tutorial.

Basic settin

For each functional test, you usually need to define some global variables, such as "user interaction delay" or "failed timeout" (that is, if an event does not occur within a specified time, the program will be interrupted).

# define TTT_FUNCTIONAL_TEST_UTIL_IDLE_CONDITION (f) ((TttFunctionalTestUtilIdleCondition) (f)) # define TTT_FUNCTIONAL_TEST_UTIL_REACTION_TIME (125000) # define TTT_FUNCTIONAL_TEST_UTIL_REACTION_TIME_LONG (500000) typedef gboolean (* TttFunctionalTestUtilIdleCondition) (gpointer data); struct timespec ttt_functional_test_util_default_timeout = {20,0,}

Now we can implement our own timeout function. Here, in order to get the desired delay, we use the usleep function.

Void ttt_functional_test_util_reaction_time () {usleep (TTT_FUNCTIONAL_TEST_UTIL_REACTION_TIME);} void ttt_functional_test_util_reaction_time_long () {usleep (TTT_FUNCTIONAL_TEST_UTIL_REACTION_TIME_LONG);}

The timeout function does not delay execution until the control state is obtained. This is very helpful for an action executed asynchronously, which is why such a long delay is adopted.

Void ttt_functional_test_util_idle_condition_and_timeout (TttFunctionalTestUtilIdleCondition idle_condition, struct timespec * timeout, pointer data) {struct timespec start_time, current_time; clock_gettime (CLOCK_MONOTONIC, & start_time); while (TTT_FUNCTIONAL_TEST_UTIL_IDLE_CONDITION (idle_condition) (data)) {ttt_functional_test_util_reaction_time () Clock_gettime (CLOCK_MONOTONIC, & current_time); if (start_time.tv_sec + timeout- > tv_sec)

< current_time.tv_sec){ break; } } ttt_functional_test_util_reaction_time(); } 与图形化用户界面交互 为了模拟用户交互的操作, Gdk 库 为我们提供了一些需要的函数。要完成我们的工作,我们只需要如下 3 个函数: gdk_display_warp_pointer() gdk_test_simulate_button() gdk_test_simulate_key() 举个例子,为了测试按钮点击,我们可以这么做: gboolean ttt_functional_test_util_button_click(GtkButton *button) { GtkWidget *widget; GdkWindow *window; gint x, y; gint origin_x, origin_y; if(button == NULL || !GTK_IS_BUTTON(button)){ return(FALSE); } widget = button; if(!GTK_WIDGET_REALIZED(widget)){ ttt_functional_test_util_reaction_time_long(); } /* retrieve window and pointer position */ gdk_threads_enter(); window = gtk_widget_get_window(widget); x = widget->

Allocation.x + widget- > allocation.width / 2.0; y = widget- > allocation.y + widget- > allocation.height / 2.0; gdk_window_get_origin (window, & origin_x, & origin_y); gdk_display_warp_pointer (gtk_widget_get_display (widget), gtk_widget_get_screen (widget), origin_x + x, origin_y + y) Gdk_threads_leave (); / * click the button * / ttt_functional_test_util_reaction_time () Gdk_test_simulate_button (window, x, y, 1, GDK_BUTTON1_MASK, GDK_BUTTON_PRESS); ttt_functional_test_util_reaction_time () Gdk_test_simulate_button (window, x, y, 1, GDK_BUTTON1_MASK, GDK_BUTTON_RELEASE); ttt_functional_test_util_reaction_time (); ttt_functional_test_util_reaction_time_long () Return (TRUE);}

We want to make sure that the button is active, so we provide an idle condition function:

Gboolean ttt_functional_test_util_idle_test_toggle_active (GtkToggleButton * * toggle_button) {gboolean do_idle; do_idle = TRUE; gdk_threads_enter (); if (* toggle_button! = NULL & & GTK_IS_TOGGLE_BUTTON (* toggle_button) & & gtk_toggle_button_get_active (* toggle_button)) {do_idle = FALSE;} gdk_threads_leave (); return (do_idle);}

Test scenario

Because the Tictactoe program is very simple, we just need to make sure that a GtkToggleButton button is clicked. Once the button is definitely activated, the functional test can be performed. To click the button, we use the convenient util function mentioned above.

As shown in the figure, we assume that if the line is filled, player A wins, because player B does not notice and only populates the second line.

GtkWindow * window; Tictactoe * ttt; void* ttt_functional_test_gtk_main (void*) {gtk_main (); pthread_exit (NULL);} void ttt_functional_test_dumb_player_b () {GtkButton * buttons [3] [3]; guint I; / * to avoid race-conditions copy the buttons * / gdk_threads_enter (); memcpy (buttons, ttt- > buttons, 9 * sizeof (GtkButton *)); gdk_threads_leave () / * TEST 1-the dumb player B * / for (I = 0; I < 3; iTunes +) {/ * assert player A clicks the button successfully * / if (! ttt_functional_test_util_button_click (buttons [0] [I])) {exit (- 1) } functional_test_util_idle_condition_and_timeout (ttt_functional_test_util_idle_test_toggle_active, ttt_functional_test_util_default_timeout, & buttons [0] [I]); / * assert player B clicks the button successfully * / if (! ttt_functional_test_util_button_click (buttons [1] [I])) {exit (- 1) } functional_test_util_idle_condition_and_timeout (ttt_functional_test_util_idle_test_toggle_active, ttt_functional_test_util_default_timeout, & buttons [1] [I]);}} int main (int argc, char * * argv) {pthread_t thread; gtk_init (& argc, & argv) / * start the tictactoe application * / window = gtk_window_new (GTK_WINDOW_TOPLEVEL); ttt = tictactoe_new (); gtk_container_add (window, ttt); gtk_widget_show_all (window); / * start the Gtk+ dispatcher * / pthread_create (& thread, NULL, ttt_functional_test_gtk_main, NULL); / * launch test routines * / ttt_functional_test_dumb_player_b () / * terminate the application * / gdk_threads_enter (); gtk_main_quit (); gdk_threads_leave (); return (0);} above is how to use C language to test Gtk and applications. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report