gtktestatcontext.c 12.5 KB
Newer Older
Emmanuele Bassi's avatar
Emmanuele Bassi committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* gtktestatcontext.c: Test AT context
 *
 * Copyright 2020  GNOME Foundation
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gtktestatcontextprivate.h"

#include "gtkatcontextprivate.h"
26
#include "gtkaccessibleprivate.h"
27
#include "gtkdebug.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
28
#include "gtkenums.h"
29
#include "gtkprivate.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
30
#include "gtktypebuiltins.h"
Emmanuele Bassi's avatar
Emmanuele Bassi committed
31
32
33
34
35
36

struct _GtkTestATContext
{
  GtkATContext parent_instance;
};

Emmanuele Bassi's avatar
Emmanuele Bassi committed
37
38
39
40
41
struct _GtkTestATContextClass
{
  GtkATContextClass parent_class;
};

Emmanuele Bassi's avatar
Emmanuele Bassi committed
42
43
44
G_DEFINE_TYPE (GtkTestATContext, gtk_test_at_context, GTK_TYPE_AT_CONTEXT)

static void
45
46
47
gtk_test_at_context_state_change (GtkATContext                *self,
                                  GtkAccessibleStateChange     changed_states,
                                  GtkAccessiblePropertyChange  changed_properties,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
48
                                  GtkAccessibleRelationChange  changed_relations,
49
50
51
                                  GtkAccessibleAttributeSet   *states,
                                  GtkAccessibleAttributeSet   *properties,
                                  GtkAccessibleAttributeSet   *relations)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
52
{
53
  if (GTK_DEBUG_CHECK (A11Y))
54
    {
55
56
57
      char *states_str = gtk_accessible_attribute_set_to_string (states);
      char *properties_str = gtk_accessible_attribute_set_to_string (properties);
      char *relations_str = gtk_accessible_attribute_set_to_string (relations);
58
59
60
61
      GtkAccessibleRole role = gtk_at_context_get_accessible_role (self);
      GtkAccessible *accessible = gtk_at_context_get_accessible (self);
      GEnumClass *class = g_type_class_ref (GTK_TYPE_ACCESSIBLE_ROLE);
      GEnumValue *value = g_enum_get_value (class, role);
62

63
64
65
66
67
68
69
70
71
72
73
      g_print ("*** Accessible state changed for accessible “%s”, with role “%s” (%d):\n"
           "***     states = %s\n"
           "*** properties = %s\n"
           "***  relations = %s\n",
            G_OBJECT_TYPE_NAME (accessible),
           value->value_nick,
           role,
           states_str,
           properties_str,
           relations_str);
      g_type_class_unref (class);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
74

75
76
77
78
      g_free (states_str);
      g_free (properties_str);
      g_free (relations_str);
    }
Emmanuele Bassi's avatar
Emmanuele Bassi committed
79
80
}

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
static void
gtk_test_at_context_platform_change (GtkATContext                *self,
                                     GtkAccessiblePlatformChange  changed_platform)
{
  if (GTK_DEBUG_CHECK (A11Y))
    {
      GtkAccessible *accessible;

      accessible = gtk_at_context_get_accessible (self);

      g_print ("*** Accessible platform state changed for accessible “%s”:\n",
               G_OBJECT_TYPE_NAME (accessible));
      if (changed_platform & GTK_ACCESSIBLE_PLATFORM_CHANGE_FOCUSABLE)
        g_print ("***  focusable = %d\n",
                 gtk_accessible_get_platform_state (accessible, GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSABLE));
      if (changed_platform & GTK_ACCESSIBLE_PLATFORM_CHANGE_FOCUSED)
        g_print ("***    focused = %d\n",
                 gtk_accessible_get_platform_state (accessible, GTK_ACCESSIBLE_PLATFORM_STATE_FOCUSED));
99
100
101
      if (changed_platform & GTK_ACCESSIBLE_PLATFORM_CHANGE_ACTIVE)
        g_print ("***    active = %d\n",
                 gtk_accessible_get_platform_state (accessible, GTK_ACCESSIBLE_PLATFORM_STATE_ACTIVE));
102
103
104
    }
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
105
106
107
108
109
110
static void
gtk_test_at_context_class_init (GtkTestATContextClass *klass)
{
  GtkATContextClass *context_class = GTK_AT_CONTEXT_CLASS (klass);

  context_class->state_change = gtk_test_at_context_state_change;
111
  context_class->platform_change = gtk_test_at_context_platform_change;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
112
113
114
115
116
117
118
}

static void
gtk_test_at_context_init (GtkTestATContext *self)
{
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
119
120
/*< private >
 * gtk_test_at_context_new:
Matthias Clasen's avatar
Matthias Clasen committed
121
122
123
 * @accessible_role: the `GtkAccessibleRole` for the AT context
 * @accessible: the `GtkAccessible` instance which owns the AT context
 * @display: a `GdkDisplay`
Emmanuele Bassi's avatar
Emmanuele Bassi committed
124
 *
Matthias Clasen's avatar
Matthias Clasen committed
125
 * Creates a new `GtkTestATContext` instance for @accessible, using the
Emmanuele Bassi's avatar
Emmanuele Bassi committed
126
127
 * given @accessible_role.
 *
Matthias Clasen's avatar
Matthias Clasen committed
128
 * Returns: (transfer full): the newly created `GtkTestATContext` instance
Emmanuele Bassi's avatar
Emmanuele Bassi committed
129
 */
Emmanuele Bassi's avatar
Emmanuele Bassi committed
130
131
GtkATContext *
gtk_test_at_context_new (GtkAccessibleRole  accessible_role,
132
133
                         GtkAccessible     *accessible,
                         GdkDisplay        *display)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
134
135
136
137
{
  return g_object_new (GTK_TYPE_TEST_AT_CONTEXT,
                       "accessible-role", accessible_role,
                       "accessible", accessible,
138
                       "display", display,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
139
140
                       NULL);
}
Emmanuele Bassi's avatar
Emmanuele Bassi committed
141

142
143
/**
 * gtk_test_accessible_has_role:
Matthias Clasen's avatar
Matthias Clasen committed
144
145
 * @accessible: a `GtkAccessible`
 * @role: a `GtkAccessibleRole`
146
 *
Matthias Clasen's avatar
Matthias Clasen committed
147
 * Checks whether the `GtkAccessible:accessible-role` of the accessible
148
149
150
151
 * is @role.
 *
 * Returns: %TRUE if the role matches
 */
Emmanuele Bassi's avatar
Emmanuele Bassi committed
152
153
154
155
156
157
gboolean
gtk_test_accessible_has_role (GtkAccessible     *accessible,
                              GtkAccessibleRole  role)
{
  g_return_val_if_fail (GTK_IS_ACCESSIBLE (accessible), FALSE);

158
  return gtk_accessible_get_accessible_role (accessible) == role;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
159
160
}

161
162
/**
 * gtk_test_accessible_has_property:
Matthias Clasen's avatar
Matthias Clasen committed
163
164
 * @accessible: a `GtkAccessible`
 * @property: a `GtkAccessibleProperty`
165
 *
Matthias Clasen's avatar
Matthias Clasen committed
166
 * Checks whether the `GtkAccessible` has @property set.
167
168
169
 *
 * Returns: %TRUE if the @property is set in the @accessible
 */
Emmanuele Bassi's avatar
Emmanuele Bassi committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
gboolean
gtk_test_accessible_has_property (GtkAccessible         *accessible,
                                  GtkAccessibleProperty  property)
{
  GtkATContext *context;

  g_return_val_if_fail (GTK_IS_ACCESSIBLE (accessible), FALSE);

  context = gtk_accessible_get_at_context (accessible);
  if (context == NULL)
    return FALSE;

  return gtk_at_context_has_accessible_property (context, property);
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
185
186
/**
 * gtk_test_accessible_check_property:
Matthias Clasen's avatar
Matthias Clasen committed
187
188
 * @accessible: a `GtkAccessible`
 * @property: a `GtkAccessibleProperty`
Emmanuele Bassi's avatar
Emmanuele Bassi committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
 * @...: the expected value of @property
 *
 * Checks whether the accessible @property of @accessible is set to
 * a specific value.
 *
 * Returns: (transfer full): the value of the accessible property
 */
char *
gtk_test_accessible_check_property (GtkAccessible         *accessible,
                                    GtkAccessibleProperty  property,
                                    ...)
{
  char *res = NULL;
  va_list args;

  va_start (args, property);

206
  GError *error = NULL;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
207
  GtkAccessibleValue *check_value =
208
    gtk_accessible_value_collect_for_property (property, &error, &args);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
209
210
211

  va_end (args);

212
213
214
215
216
217
  if (error != NULL)
    {
      res = g_strdup (error->message);
      g_error_free (error);
      return res;
    }
218

Emmanuele Bassi's avatar
Emmanuele Bassi committed
219
  if (check_value == NULL)
220
    check_value = gtk_accessible_value_get_default_for_property (property);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236

  GtkATContext *context = gtk_accessible_get_at_context (accessible);
  GtkAccessibleValue *real_value =
    gtk_at_context_get_accessible_property (context, property);

  if (gtk_accessible_value_equal (check_value, real_value))
    goto out;

  res = gtk_accessible_value_to_string (real_value);

out:
  gtk_accessible_value_unref (check_value);

  return res;
}

237
238
/**
 * gtk_test_accessible_has_state:
Matthias Clasen's avatar
Matthias Clasen committed
239
240
 * @accessible: a `GtkAccessible`
 * @state: a `GtkAccessibleState`
241
 *
Matthias Clasen's avatar
Matthias Clasen committed
242
 * Checks whether the `GtkAccessible` has @state set.
243
244
245
 *
 * Returns: %TRUE if the @state is set in the @accessible
 */
Emmanuele Bassi's avatar
Emmanuele Bassi committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
gboolean
gtk_test_accessible_has_state (GtkAccessible      *accessible,
                               GtkAccessibleState  state)
{
  GtkATContext *context;

  g_return_val_if_fail (GTK_IS_ACCESSIBLE (accessible), FALSE);

  context = gtk_accessible_get_at_context (accessible);
  if (context == NULL)
    return FALSE;

  return gtk_at_context_has_accessible_state (context, state);
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
261
262
/**
 * gtk_test_accessible_check_state:
Matthias Clasen's avatar
Matthias Clasen committed
263
264
 * @accessible: a `GtkAccessible`
 * @state: a `GtkAccessibleState`
Emmanuele Bassi's avatar
Emmanuele Bassi committed
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
 * @...: the expected value of @state
 *
 * Checks whether the accessible @state of @accessible is set to
 * a specific value.
 *
 * Returns: (transfer full): the value of the accessible state
 */
char *
gtk_test_accessible_check_state (GtkAccessible      *accessible,
                                 GtkAccessibleState  state,
                                 ...)
{
  char *res = NULL;
  va_list args;

  va_start (args, state);

282
  GError *error = NULL;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
283
  GtkAccessibleValue *check_value =
284
    gtk_accessible_value_collect_for_state (state, &error, &args);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
285
286
287

  va_end (args);

288
289
290
291
292
293
  if (error != NULL)
    {
      res = g_strdup (error->message);
      g_error_free (error);
      return res;
    }
294

Emmanuele Bassi's avatar
Emmanuele Bassi committed
295
  if (check_value == NULL)
296
    check_value = gtk_accessible_value_get_default_for_state (state);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312

  GtkATContext *context = gtk_accessible_get_at_context (accessible);
  GtkAccessibleValue *real_value =
    gtk_at_context_get_accessible_state (context, state);

  if (gtk_accessible_value_equal (check_value, real_value))
    goto out;

  res = gtk_accessible_value_to_string (real_value);

out:
  gtk_accessible_value_unref (check_value);

  return res;
}

313
314
/**
 * gtk_test_accessible_has_relation:
Matthias Clasen's avatar
Matthias Clasen committed
315
316
 * @accessible: a `GtkAccessible`
 * @relation: a `GtkAccessibleRelation`
317
 *
Matthias Clasen's avatar
Matthias Clasen committed
318
 * Checks whether the `GtkAccessible` has @relation set.
319
320
321
 *
 * Returns: %TRUE if the @relation is set in the @accessible
 */
Emmanuele Bassi's avatar
Emmanuele Bassi committed
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
gboolean
gtk_test_accessible_has_relation (GtkAccessible         *accessible,
                                  GtkAccessibleRelation  relation)
{
  GtkATContext *context;

  g_return_val_if_fail (GTK_IS_ACCESSIBLE (accessible), FALSE);

  context = gtk_accessible_get_at_context (accessible);
  if (context == NULL)
    return FALSE;

  return gtk_at_context_has_accessible_relation (context, relation);
}

Emmanuele Bassi's avatar
Emmanuele Bassi committed
337
338
/**
 * gtk_test_accessible_check_relation:
Matthias Clasen's avatar
Matthias Clasen committed
339
340
 * @accessible: a `GtkAccessible`
 * @relation: a `GtkAccessibleRelation`
Emmanuele Bassi's avatar
Emmanuele Bassi committed
341
342
343
344
345
346
347
348
349
350
351
 * @...: the expected value of @relation
 *
 * Checks whether the accessible @relation of @accessible is set to
 * a specific value.
 *
 * Returns: (transfer full): the value of the accessible relation
 */
char *
gtk_test_accessible_check_relation (GtkAccessible         *accessible,
                                    GtkAccessibleRelation  relation,
                                    ...)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
352
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
353
354
  char *res = NULL;
  va_list args;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
355

Emmanuele Bassi's avatar
Emmanuele Bassi committed
356
  va_start (args, relation);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
357

358
  GError *error = NULL;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
359
  GtkAccessibleValue *check_value =
360
    gtk_accessible_value_collect_for_relation (relation, &error, &args);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
361

Emmanuele Bassi's avatar
Emmanuele Bassi committed
362
  va_end (args);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
363

364
365
366
367
368
369
  if (error != NULL)
    {
      res = g_strdup (error->message);
      g_error_free (error);
      return res;
    }
370

Emmanuele Bassi's avatar
Emmanuele Bassi committed
371
  if (check_value == NULL)
372
    check_value = gtk_accessible_value_get_default_for_relation (relation);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
373

Emmanuele Bassi's avatar
Emmanuele Bassi committed
374
375
376
  GtkATContext *context = gtk_accessible_get_at_context (accessible);
  GtkAccessibleValue *real_value =
    gtk_at_context_get_accessible_relation (context, relation);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
377

Emmanuele Bassi's avatar
Emmanuele Bassi committed
378
379
  if (gtk_accessible_value_equal (check_value, real_value))
    goto out;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
380

Emmanuele Bassi's avatar
Emmanuele Bassi committed
381
  res = gtk_accessible_value_to_string (real_value);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
382

Emmanuele Bassi's avatar
Emmanuele Bassi committed
383
384
385
386
out:
  gtk_accessible_value_unref (check_value);

  return res;
Emmanuele Bassi's avatar
Emmanuele Bassi committed
387
388
}

389
390
391
392
393
394
395
/*< private >
 * gtk_test_accessible_assertion_message_role:
 * @domain: a domain
 * @file: a file name
 * @line: the line in @file
 * @func: a function name in @file
 * @expr: the expression being tested
Matthias Clasen's avatar
Matthias Clasen committed
396
397
398
 * @accessible: a `GtkAccessible`
 * @expected_role: the expected `GtkAccessibleRole`
 * @actual_role: the actual `GtkAccessibleRole`
399
400
401
 *
 * Prints an assertion message for gtk_test_accessible_assert_role().
 */
Emmanuele Bassi's avatar
Emmanuele Bassi committed
402
void
Emmanuele Bassi's avatar
Emmanuele Bassi committed
403
404
405
406
407
408
409
410
gtk_test_accessible_assertion_message_role (const char        *domain,
                                            const char        *file,
                                            int                line,
                                            const char        *func,
                                            const char        *expr,
                                            GtkAccessible     *accessible,
                                            GtkAccessibleRole  expected_role,
                                            GtkAccessibleRole  actual_role)
Emmanuele Bassi's avatar
Emmanuele Bassi committed
411
{
Emmanuele Bassi's avatar
Emmanuele Bassi committed
412
413
414
  char *role_name = g_enum_to_string (GTK_TYPE_ACCESSIBLE_ROLE, actual_role);
  char *s = g_strdup_printf ("assertion failed: (%s): %s.accessible-role = %s (%d)",
                             expr,
Emmanuele Bassi's avatar
Emmanuele Bassi committed
415
                             G_OBJECT_TYPE_NAME (accessible),
Emmanuele Bassi's avatar
Emmanuele Bassi committed
416
417
                             role_name,
                             actual_role);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
418

Emmanuele Bassi's avatar
Emmanuele Bassi committed
419
  g_assertion_message (domain, file, line, func, s);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
420

Emmanuele Bassi's avatar
Emmanuele Bassi committed
421
  g_free (role_name);
Emmanuele Bassi's avatar
Emmanuele Bassi committed
422
423
  g_free (s);
}