]> Pileus Git - ~andy/gtk/blob - tests/a11y/value.c
Add accessibility for GtkLevelBar and value test
[~andy/gtk] / tests / a11y / value.c
1 /*
2  * Copyright (C) 2011 Red Hat Inc.
3  * Copyright (C) 2012 SUSE LLC.
4  *
5  * Author:
6  *      Mike Gorse <mgorse@suse.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <gtk/gtk.h>
23 #include <string.h>
24
25 static void
26 set_value (GtkWidget   *widget,
27            gint value)
28 {
29   if (GTK_IS_LEVEL_BAR (widget))
30     gtk_level_bar_set_value (GTK_LEVEL_BAR (widget), value);
31   else if (GTK_IS_RANGE (widget))
32     {
33       GtkAdjustment *adjustment = gtk_range_get_adjustment (GTK_RANGE (widget));
34       gtk_adjustment_set_value (adjustment, value);
35     }
36   else if (GTK_IS_SPIN_BUTTON (widget))
37     {
38       GtkAdjustment *adjustment = gtk_spin_button_get_adjustment (GTK_SPIN_BUTTON (widget));
39       gtk_adjustment_set_value (adjustment, value);
40     }
41 }
42
43 typedef struct
44 {
45   gint count;
46   gchar *last_name;
47 } NotifyData;
48
49 static void
50 notify_cb (GObject    *obj,
51            GParamSpec *pspec, NotifyData *data)
52 {
53   data->count++;
54   if (data->last_name)
55     g_free (data->last_name);
56   data->last_name = g_strdup (pspec->name);
57 }
58
59 static void
60 test_basic (GtkWidget *widget)
61 {
62   NotifyData notify_data;
63   AtkObject *atk_object;
64   AtkValue *atk_value;
65   gdouble value = 50;
66   GValue ret;
67
68   atk_object = gtk_widget_get_accessible (widget);
69   atk_value = ATK_VALUE (atk_object);
70
71   memset (&notify_data, 0, sizeof (notify_data));
72   g_signal_connect (atk_object, "notify", G_CALLBACK (notify_cb), &notify_data);
73   set_value (widget, value);
74   g_assert_cmpint (notify_data.count, ==, 1);
75   g_assert_cmpstr (notify_data.last_name, ==, "accessible-value");
76
77   memset (&ret, 0, sizeof (ret));
78   atk_value_get_current_value (atk_value, &ret);
79   g_assert_cmpfloat (g_value_get_double (&ret), ==, value);
80
81   g_free (notify_data.last_name);
82   g_signal_handlers_disconnect_by_func (atk_object, G_CALLBACK (notify_cb), &notify_data);
83 }
84
85 static void
86 setup_test (GtkWidget *widget)
87 {
88   set_value (widget, 0);
89 }
90
91 static void
92 add_value_test (const gchar      *prefix,
93                GTestFixtureFunc  test_func,
94                GtkWidget        *widget)
95 {
96   gchar *path;
97
98   path = g_strdup_printf ("%s/%s", prefix, G_OBJECT_TYPE_NAME (widget));
99   g_test_add_vtable (path,
100                      0,
101                      g_object_ref (widget),
102                      (GTestFixtureFunc) setup_test,
103                      (GTestFixtureFunc) test_func,
104                      (GTestFixtureFunc) g_object_unref);
105   g_free (path);
106 }
107
108 static void
109 add_value_tests (GtkWidget *widget)
110 {
111   g_object_ref_sink (widget);
112   add_value_test ("/value/basic", (GTestFixtureFunc) test_basic, widget);
113   g_object_unref (widget);
114 }
115
116 int
117 main (int argc, char *argv[])
118 {
119   gtk_test_init (&argc, &argv, NULL);
120
121   g_test_bug_base ("http://bugzilla.gnome.org/");
122
123   add_value_tests (gtk_spin_button_new_with_range (0, 100, 1));
124   add_value_tests (gtk_level_bar_new_for_interval (0, 100));
125
126   return g_test_run ();
127 }