~ubuntu-branches/ubuntu/trusty/geis/trusty

« back to all changes in this revision

Viewing changes to libgeis/backend/test_fixture/geis_backend_test_fixture.c

  • Committer: Package Import Robot
  • Author(s): Chase Douglas
  • Date: 2012-07-30 08:51:42 UTC
  • Revision ID: package-import@ubuntu.com-20120730085142-jrc33ygjvt0ob1wl
Tags: upstream-2.2.11
ImportĀ upstreamĀ versionĀ 2.2.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file geis_backend_test_fixture.c
 
3
 * @brief GEIS mock back end test fixture implementation
 
4
 *
 
5
 * Copyright 2010 Canonical Ltd.
 
6
 *
 
7
 * This library is free software; you can redistribute it and/or modify it under
 
8
 * the terms of the GNU Lesser General Public License as published by the Free
 
9
 * Software Foundation; either version 3 of the License, or (at your option) any
 
10
 * later version.
 
11
 *
 
12
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
14
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
15
 * details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 */
 
20
#include "geis_config.h"
 
21
#include "geis_backend.h"
 
22
#include "geis_backend_protected.h"
 
23
 
 
24
#include "geis_attr.h"
 
25
#include "geis_class.h"
 
26
#include "geis_device.h"
 
27
#include "geis_event.h"
 
28
#include "geis_filter.h"
 
29
#include "geis_filter_term.h"
 
30
#include "geis_frame.h"
 
31
#include "geis_group.h"
 
32
#include "geis_logging.h"
 
33
#include "geis_private.h"
 
34
#include "geis_subscription.h"
 
35
#include "geis_test_api.h"
 
36
#include "geis_touch.h"
 
37
#include <stdlib.h>
 
38
#include <string.h>
 
39
#include <sys/socket.h>
 
40
#include <unistd.h>
 
41
 
 
42
 
 
43
static inline GeisSize
 
44
_min(GeisSize a, GeisSize b)
 
45
{
 
46
  return (a < b) ? a : b;
 
47
}
 
48
 
 
49
 
 
50
static inline GeisSize
 
51
_max(GeisSize a, GeisSize b)
 
52
{
 
53
  return (a > b) ? a : b;
 
54
}
 
55
 
 
56
static GeisSize   g_min_touches = 1;
 
57
static GeisSize   g_max_touches = 5;
 
58
 
 
59
 
 
60
typedef struct GeisBackendTestFixture
 
61
{
 
62
  Geis                tf_geis;
 
63
} *GeisBackendTestFixture;
 
64
 
 
65
 
 
66
typedef struct TestBackendToken
 
67
{
 
68
  struct GeisBackendToken base;
 
69
  GeisBackendTestFixture  be;
 
70
  GeisInteger             min_touches;
 
71
  GeisInteger             max_touches;
 
72
} *TestBackendToken;
 
73
 
 
74
static inline TestBackendToken
 
75
_tbtoken_from_token(GeisBackendToken bet)
 
76
{
 
77
  return (TestBackendToken)bet;
 
78
}
 
79
 
 
80
static GeisBackendToken _token_clone(GeisBackendToken);
 
81
static void             _token_finalize(GeisBackendToken);
 
82
static void             _token_compose(GeisBackendToken, GeisBackendToken);
 
83
static GeisStatus       _token_activate(GeisBackendToken, GeisSubscription);
 
84
static GeisStatus       _token_deactivate(GeisBackendToken, GeisSubscription);
 
85
 
 
86
static struct GeisBackendTokenVtable _token_vtbl = {
 
87
  _token_clone,
 
88
  _token_finalize,
 
89
  _token_compose,
 
90
  _token_activate,
 
91
  _token_deactivate,
 
92
};
 
93
 
 
94
static GeisGestureClass g_poke_class = NULL;
 
95
 
 
96
static GeisStatus
 
97
_add_device_term(GeisBackendToken     token GEIS_UNUSED,
 
98
                 void                *context GEIS_UNUSED,
 
99
                 GeisString           name,
 
100
                 GeisFilterOperation  op GEIS_UNUSED,
 
101
                 void                *value GEIS_UNUSED)
 
102
{
 
103
  GeisStatus status = GEIS_STATUS_SUCCESS;
 
104
  geis_error("called: name=%s", name);
 
105
  return status;
 
106
}
 
107
 
 
108
 
 
109
static void
 
110
_create_test_devices(GeisBackendTestFixture tf)
 
111
{
 
112
  GeisDevice device = geis_device_new("abs-test-device", 0);
 
113
  struct GeisFilterableAttribute attrs[] = {
 
114
    { GEIS_DEVICE_ATTRIBUTE_NAME,         GEIS_ATTR_TYPE_STRING,  _add_device_term, NULL },
 
115
    { GEIS_DEVICE_ATTRIBUTE_ID,           GEIS_ATTR_TYPE_INTEGER, _add_device_term, NULL },
 
116
    { GEIS_DEVICE_ATTRIBUTE_TOUCHES,      GEIS_ATTR_TYPE_INTEGER, _add_device_term, NULL },
 
117
    { GEIS_DEVICE_ATTRIBUTE_DIRECT_TOUCH, GEIS_ATTR_TYPE_BOOLEAN, _add_device_term, NULL }
 
118
  };
 
119
  GeisSize attr_count = sizeof(attrs) / sizeof(struct GeisFilterableAttribute);
 
120
 
 
121
  geis_register_device(tf->tf_geis, device, attr_count, attrs);
 
122
}
 
123
 
 
124
 
 
125
static GeisStatus
 
126
_add_class_term(GeisBackendToken     gbtoken,
 
127
                void                *context GEIS_UNUSED,
 
128
                GeisString           name,
 
129
                GeisFilterOperation  op,
 
130
                void                *value)
 
131
{
 
132
  GeisStatus status = GEIS_STATUS_SUCCESS;
 
133
  TestBackendToken token = _tbtoken_from_token(gbtoken);
 
134
 
 
135
  if (0 == strcmp(name, GEIS_CLASS_ATTRIBUTE_NAME)
 
136
      && op == GEIS_FILTER_OP_EQ)
 
137
  {
 
138
    GeisString class_name = (GeisString)value;
 
139
    geis_debug("called: attr=%s name=\"%s\"", name, class_name);
 
140
  }
 
141
  else if (0 == strcmp(name, GEIS_GESTURE_ATTRIBUTE_TOUCHES))
 
142
  {
 
143
    GeisInteger touches = *(GeisInteger*)value;
 
144
    switch (op)
 
145
    {
 
146
      case GEIS_FILTER_OP_GT:
 
147
        token->min_touches = _max(token->min_touches, touches+1);
 
148
        break;
 
149
      case GEIS_FILTER_OP_GE:
 
150
        token->min_touches = _max(token->min_touches, touches);
 
151
        break;
 
152
      case GEIS_FILTER_OP_LT:
 
153
        token->max_touches = _min(touches-1, token->max_touches);
 
154
        break;
 
155
      case GEIS_FILTER_OP_LE:
 
156
        token->max_touches = _min(touches, token->max_touches);
 
157
        break;
 
158
      case GEIS_FILTER_OP_EQ:
 
159
        token->min_touches = _max(token->min_touches, touches);
 
160
        token->max_touches = _min(touches, token->max_touches);
 
161
        break;
 
162
      case GEIS_FILTER_OP_NE:
 
163
        break;
 
164
    }
 
165
    geis_debug("called: attr=%s touches=\"%d\" min=%d max=%d", name, touches, token->min_touches, token->max_touches);
 
166
  }
 
167
  return status;
 
168
}
 
169
 
 
170
 
 
171
static void
 
172
_create_test_classes(GeisBackendTestFixture tf)
 
173
{
 
174
  if (!g_poke_class)
 
175
  {
 
176
    g_poke_class = geis_gesture_class_new("poke", 2100);
 
177
    struct GeisFilterableAttribute attrs[] = {
 
178
      { GEIS_CLASS_ATTRIBUTE_NAME,      GEIS_ATTR_TYPE_STRING,  _add_class_term, g_poke_class },
 
179
      { GEIS_CLASS_ATTRIBUTE_ID,        GEIS_ATTR_TYPE_INTEGER, _add_class_term, g_poke_class },
 
180
      { GEIS_GESTURE_ATTRIBUTE_TOUCHES, GEIS_ATTR_TYPE_INTEGER, _add_class_term, g_poke_class }
 
181
    };
 
182
    GeisSize attr_count = sizeof(attrs) / sizeof(struct GeisFilterableAttribute);
 
183
 
 
184
    geis_register_gesture_class(tf->tf_geis, g_poke_class, attr_count, attrs);
 
185
  }
 
186
}
 
187
 
 
188
 
 
189
static void
 
190
_construct(void *mem, Geis geis)
 
191
{
 
192
  GeisBackendTestFixture tf = (GeisBackendTestFixture)mem;
 
193
  tf->tf_geis = geis;
 
194
 
 
195
  _create_test_devices(tf);
 
196
  _create_test_classes(tf);
 
197
  geis_post_event(tf->tf_geis, geis_event_new(GEIS_EVENT_INIT_COMPLETE));
 
198
}
 
199
 
 
200
 
 
201
static void 
 
202
_finalize(GeisBackend be)
 
203
{
 
204
  GeisBackendTestFixture tf GEIS_UNUSED = (GeisBackendTestFixture)be;
 
205
}
 
206
 
 
207
 
 
208
static GeisBackendToken
 
209
_create_token(GeisBackend be, GeisBackendTokenInitState init_state)
 
210
{
 
211
  TestBackendToken token = NULL;
 
212
  token = calloc(1, sizeof(struct TestBackendToken));
 
213
  if (token)
 
214
  {
 
215
    token->base.vtbl = &_token_vtbl;
 
216
    token->be = (GeisBackendTestFixture)be;
 
217
    if (init_state == GEIS_BACKEND_TOKEN_INIT_ALL)
 
218
    {
 
219
      token->min_touches = g_min_touches;
 
220
      token->max_touches = g_max_touches;
 
221
    }
 
222
    else
 
223
    {
 
224
      token->min_touches = g_max_touches;
 
225
      token->max_touches = g_min_touches;
 
226
    }
 
227
  }
 
228
  return (GeisBackendToken)token;
 
229
}
 
230
 
 
231
 
 
232
static GeisStatus
 
233
_gmock_accept_gesture(GeisBackend   be GEIS_UNUSED,
 
234
                      GeisGroup     group GEIS_UNUSED,
 
235
                      GeisGestureId gesture_ID GEIS_UNUSED)
 
236
{
 
237
  return GEIS_STATUS_SUCCESS;
 
238
}
 
239
 
 
240
 
 
241
static GeisStatus
 
242
_gmock_reject_gesture(GeisBackend   be GEIS_UNUSED,
 
243
                      GeisGroup     group GEIS_UNUSED,
 
244
                      GeisGestureId gesture_ID GEIS_UNUSED)
 
245
{
 
246
  return GEIS_STATUS_SUCCESS;
 
247
}
 
248
 
 
249
 
 
250
static GeisStatus
 
251
_gmock_get_configuration(GeisBackend      be GEIS_UNUSED,
 
252
                         GeisSubscription subscription GEIS_UNUSED,
 
253
                         GeisString       item_name GEIS_UNUSED,
 
254
                         GeisPointer      item_value GEIS_UNUSED)
 
255
{
 
256
  return GEIS_STATUS_NOT_SUPPORTED;
 
257
}
 
258
 
 
259
 
 
260
static GeisStatus
 
261
_gmock_set_configuration(GeisBackend      be GEIS_UNUSED,
 
262
                         GeisSubscription subscription GEIS_UNUSED,
 
263
                         GeisString       item_name GEIS_UNUSED,
 
264
                         GeisPointer      item_value GEIS_UNUSED)
 
265
{
 
266
  return GEIS_STATUS_NOT_SUPPORTED;
 
267
}
 
268
 
 
269
 
 
270
static struct GeisBackendVtable tf_vtbl = {
 
271
  _construct,
 
272
  _finalize,
 
273
  _create_token,
 
274
  _gmock_accept_gesture,
 
275
  _gmock_reject_gesture,
 
276
  _gmock_get_configuration,
 
277
  _gmock_set_configuration
 
278
};
 
279
 
 
280
 
 
281
/*
 
282
 * Generates a gesture event, the contets of which varies accoring to what's in
 
283
 * the token.
 
284
 */
 
285
static void
 
286
_create_gesture_events_for_token(TestBackendToken token)
 
287
{
 
288
  int i;
 
289
 
 
290
  GeisFloat    attr_float;
 
291
  GeisInteger  attr_int;
 
292
  GeisAttr     attr = NULL;
 
293
 
 
294
  GeisEvent    event = geis_event_new(GEIS_EVENT_GESTURE_BEGIN);
 
295
  GeisGroupSet groupset = geis_groupset_new();
 
296
  GeisGroup    group = geis_group_new(1);
 
297
  GeisAttr     group_attr = geis_attr_new(GEIS_EVENT_ATTRIBUTE_GROUPSET,
 
298
                                          GEIS_ATTR_TYPE_POINTER,
 
299
                                          groupset);
 
300
  GeisTouchSet touchset = geis_touchset_new();
 
301
  GeisAttr     touch_attr = geis_attr_new(GEIS_EVENT_ATTRIBUTE_TOUCHSET,
 
302
                                          GEIS_ATTR_TYPE_POINTER,
 
303
                                          touchset);
 
304
 
 
305
  GeisFrame frame = geis_frame_new(1);
 
306
  geis_frame_set_is_class(frame, g_poke_class);
 
307
 
 
308
  attr_int = 13;
 
309
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_DEVICE_ID,
 
310
                       GEIS_ATTR_TYPE_INTEGER,
 
311
                       &attr_int);
 
312
  geis_frame_add_attr(frame, attr);
 
313
 
 
314
  attr_int = 1;
 
315
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_TIMESTAMP,
 
316
                       GEIS_ATTR_TYPE_INTEGER,
 
317
                       &attr_int);
 
318
  geis_frame_add_attr(frame, attr);
 
319
 
 
320
  attr_int = 2;
 
321
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_ROOT_WINDOW_ID,
 
322
                       GEIS_ATTR_TYPE_INTEGER,
 
323
                       &attr_int);
 
324
  geis_frame_add_attr(frame, attr);
 
325
 
 
326
  attr_int = 3;
 
327
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_EVENT_WINDOW_ID,
 
328
                       GEIS_ATTR_TYPE_INTEGER,
 
329
                       &attr_int);
 
330
  geis_frame_add_attr(frame, attr);
 
331
 
 
332
  attr_int = 4;
 
333
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_CHILD_WINDOW_ID,
 
334
                       GEIS_ATTR_TYPE_INTEGER,
 
335
                       &attr_int);
 
336
  geis_frame_add_attr(frame, attr);
 
337
 
 
338
  attr_float = 123.456;
 
339
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_FOCUS_X,
 
340
                       GEIS_ATTR_TYPE_FLOAT,
 
341
                       &attr_float);
 
342
  geis_frame_add_attr(frame, attr);
 
343
 
 
344
  attr_float = 987.654;
 
345
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_FOCUS_Y,
 
346
                       GEIS_ATTR_TYPE_FLOAT,
 
347
                       &attr_float);
 
348
  geis_frame_add_attr(frame, attr);
 
349
 
 
350
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_GESTURE_NAME,
 
351
                       GEIS_ATTR_TYPE_STRING,
 
352
                       "mock gesture");
 
353
  geis_frame_add_attr(frame, attr);
 
354
 
 
355
  attr = geis_attr_new(GEIS_GESTURE_ATTRIBUTE_TOUCHES,
 
356
                       GEIS_ATTR_TYPE_INTEGER,
 
357
                       &token->min_touches);
 
358
  geis_frame_add_attr(frame, attr);
 
359
 
 
360
  for (i = 0; i < token->min_touches; ++i)
 
361
  {
 
362
    GeisTouch    touch = geis_touch_new(1);
 
363
    geis_touchset_insert(touchset, touch);
 
364
    geis_frame_add_touchid(frame, geis_touch_id(touch));
 
365
  }
 
366
 
 
367
  geis_group_insert_frame(group, frame);
 
368
 
 
369
  geis_groupset_insert(groupset, group);
 
370
 
 
371
  geis_event_add_attr(event, group_attr);
 
372
  geis_event_add_attr(event, touch_attr);
 
373
 
 
374
  geis_post_event(token->be->tf_geis, event);
 
375
}
 
376
 
 
377
 
 
378
static GeisBackendToken
 
379
_token_clone(GeisBackendToken gbtoken)
 
380
{
 
381
  TestBackendToken token = _tbtoken_from_token(gbtoken);
 
382
  TestBackendToken new_token = calloc(1, sizeof(struct TestBackendToken));
 
383
  if (new_token)
 
384
  {
 
385
    memcpy(new_token, token, sizeof(struct TestBackendToken));
 
386
    return &new_token->base;
 
387
  }
 
388
  return NULL;
 
389
}
 
390
 
 
391
 
 
392
void             
 
393
_token_finalize(GeisBackendToken gbtoken GEIS_UNUSED)
 
394
{
 
395
}
 
396
 
 
397
 
 
398
void 
 
399
_token_compose(GeisBackendToken lhs, GeisBackendToken rhs)
 
400
{
 
401
  TestBackendToken token1 = _tbtoken_from_token(lhs);
 
402
  TestBackendToken token2 = _tbtoken_from_token(rhs);
 
403
  token1->min_touches = _min(token1->min_touches, token2->min_touches);
 
404
  token1->max_touches = _max(token1->max_touches, token2->max_touches);
 
405
}
 
406
 
 
407
 
 
408
GeisStatus             
 
409
_token_activate(GeisBackendToken gbtoken,
 
410
                GeisSubscription subscription GEIS_UNUSED)
 
411
{
 
412
  GeisStatus status = GEIS_STATUS_SUCCESS;
 
413
  TestBackendToken token = _tbtoken_from_token(gbtoken);
 
414
  _create_gesture_events_for_token(token);
 
415
  return status;
 
416
}
 
417
 
 
418
 
 
419
GeisStatus             
 
420
_token_deactivate(GeisBackendToken gbtoken GEIS_UNUSED,
 
421
                  GeisSubscription subscription GEIS_UNUSED)
 
422
{
 
423
  GeisStatus status = GEIS_STATUS_SUCCESS;
 
424
  return status;
 
425
}
 
426
 
 
427
 
 
428
__attribute__((constructor))
 
429
static void _register_test_fixture()
 
430
{
 
431
  geis_register_backend(GEIS_INIT_MOCK_BACKEND,
 
432
                        sizeof(struct GeisBackendTestFixture),
 
433
                        &tf_vtbl);
 
434
}
 
435
 
 
436
 
 
437
/* A dummy routine to force linkage of this module without dlopening it */
 
438
void
 
439
geis_include_backend_test_fixture()
 
440
{
 
441
}