summaryrefslogtreecommitdiffstats
path: root/test/test-mock.c
blob: 8de7d1cb7452e1b5a808f06f209466a9e54ccfb7 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

#include <glib.h>
#include "uca-camera.h"
#include "uca-plugin-manager.h"

typedef struct {
    UcaPluginManager *manager;
    UcaCamera *camera;
} Fixture;

static gchar *
build_mock_plugin_path (void)
{
    gchar *cwd;
    gchar *plugin_path;

    cwd = g_get_current_dir ();
    plugin_path = g_build_filename (cwd, "plugins", "mock", NULL);
    g_free (cwd);
    return plugin_path;
}

static void
fixture_setup (Fixture *fixture, gconstpointer data)
{
    gchar *plugin_path;
    GError *error = NULL;

    plugin_path = build_mock_plugin_path ();
    g_setenv ("UCA_CAMERA_PATH", plugin_path, TRUE);
    g_free (plugin_path);

    fixture->manager = uca_plugin_manager_new ();
    fixture->camera = uca_plugin_manager_get_camera (fixture->manager,
                                                     "mock", &error, NULL);
    g_assert (error == NULL);
    g_assert (fixture->camera);
}

static void
fixture_teardown (Fixture *fixture, gconstpointer data)
{
    g_object_unref (fixture->camera);
    g_object_unref (fixture->manager);
}

static void
on_property_change (gpointer instance, GParamSpec *pspec, gpointer user_data)
{
    gboolean *success = (gboolean *) user_data;
    *success = TRUE;
}

static void
test_factory (Fixture *fixture, gconstpointer data)
{
    GError *error = NULL;
    UcaCamera *camera = uca_plugin_manager_get_camera (fixture->manager,
                                                       "fox994m3a0yxmy", &error, NULL);
    g_assert_error (error, UCA_PLUGIN_MANAGER_ERROR, UCA_PLUGIN_MANAGER_ERROR_MODULE_NOT_FOUND);
    g_assert (camera == NULL);
}

static void
test_recording (Fixture *fixture, gconstpointer data)
{
    GError *error = NULL;
    UcaCamera *camera = UCA_CAMERA (fixture->camera);

    uca_camera_stop_recording (camera, &error);
    g_assert_error (error, UCA_CAMERA_ERROR, UCA_CAMERA_ERROR_NOT_RECORDING);
    g_error_free (error);

    error = NULL;
    uca_camera_start_recording (camera, &error);
    g_assert_no_error (error);
    uca_camera_stop_recording (camera, &error);
    g_assert_no_error (error);
}

static void
test_recording_signal (Fixture *fixture, gconstpointer data)
{
    UcaCamera *camera = UCA_CAMERA (fixture->camera);
    gboolean success = FALSE;
    g_signal_connect (G_OBJECT (camera), "notify::is-recording",
            (GCallback) on_property_change, &success);

    uca_camera_start_recording (camera, NULL);
    g_assert (success == TRUE);

    success = FALSE;
    uca_camera_stop_recording (camera, NULL);
    g_assert (success == TRUE);
}

static void
grab_func (gpointer data, gpointer user_data)
{
    guint *count = (guint *) user_data;
    *count += 1;
}

static void
test_recording_async (Fixture *fixture, gconstpointer data)
{
    UcaCamera *camera = UCA_CAMERA (fixture->camera);

    guint count = 0;
    uca_camera_set_grab_func (camera, grab_func, &count);

    g_object_set (G_OBJECT (camera),
            "frames-per-second", 10.0,
            "transfer-asynchronously", TRUE,
            NULL);

    GError *error = NULL;
    uca_camera_start_recording (camera, &error);
    g_assert_no_error (error);

    /*
     * We sleep for an 1/8 of a second at 10 frames per second, thus we should
     * record 2 frames.
     */
    g_usleep (G_USEC_PER_SEC / 8);

    uca_camera_stop_recording (camera, &error);
    g_assert_no_error (error);
    g_assert_cmpint (count, ==, 2);
}

static void
test_recording_property (Fixture *fixture, gconstpointer data)
{
    UcaCamera *camera = UCA_CAMERA (fixture->camera);

    gboolean is_recording = FALSE;
    uca_camera_start_recording (camera, NULL);
    g_object_get (G_OBJECT (camera), "is-recording", &is_recording, NULL);
    g_assert (is_recording == TRUE);
    g_assert (uca_camera_is_recording (camera));

    uca_camera_stop_recording (camera, NULL);
    g_object_get (G_OBJECT (camera), "is-recording", &is_recording, NULL);
    g_assert (is_recording == FALSE);
    g_assert (!uca_camera_is_recording (camera));
}

static void
test_recording_buffered (Fixture *fixture, gconstpointer data)
{
    UcaCamera *camera = UCA_CAMERA (fixture->camera);
    GError *error = NULL;
    guint width, height, bitdepth;
    gsize buffer_size;
    gchar *buffer;

    g_object_get (G_OBJECT (camera),
                  "roi-width", &width,
                  "roi-height", &height,
                  "sensor-bitdepth", &bitdepth,
                  NULL);

    buffer_size = width * height * (bitdepth <= 8 ? 1 : 2);
    buffer = g_malloc0 (buffer_size);

    g_object_set (G_OBJECT (camera),
                  "buffered", TRUE,
                  "num-buffers", 5,
                  NULL);

    uca_camera_start_recording (camera, &error);
    g_assert_no_error (error);

    for (int i = 0; i < 10; i++) {
        g_assert (uca_camera_grab (camera, (gpointer) buffer, &error));
        g_assert_no_error (error);
    }

    uca_camera_stop_recording (camera, &error);
    g_assert_no_error (error);

    g_free (buffer);
}


static void
test_base_properties (Fixture *fixture, gconstpointer data)
{
    UcaCamera *camera = UCA_CAMERA (fixture->camera);
    guint n_properties = 0;
    GParamSpec **properties = g_object_class_list_properties (G_OBJECT_GET_CLASS (camera), &n_properties);
    GValue val = {0};

    for (guint i = 0; i < n_properties; i++) {
        g_value_init (&val, properties[i]->value_type);
        g_object_get_property (G_OBJECT (camera), properties[i]->name, &val);
        g_value_unset (&val);
    }

    g_free (properties);
}

static void
test_fps_property (Fixture *fixture, gconstpointer data)
{
    gdouble frames_per_second;
    gdouble exposure_time = 0.5;

    g_object_set (G_OBJECT (fixture->camera),
                  "exposure-time", exposure_time,
                  NULL);
    g_object_get (G_OBJECT (fixture->camera),
                  "frames-per-second", &frames_per_second,
                  NULL);

    /*
     * The mock camera does not override the "frames-per-second" property, so we
     * check the implementation from the base camera.
     */
    g_assert_cmpfloat (frames_per_second, ==, 1.0 / exposure_time);
}

static void
test_property_units (Fixture *fixture, gconstpointer data)
{
    /* Default camera properties */
    g_assert (uca_camera_get_unit (fixture->camera, "sensor-width") == UCA_UNIT_PIXEL);
    g_assert (uca_camera_get_unit (fixture->camera, "name") == UCA_UNIT_NA);

    /* Mock-specific properties */
    g_assert (uca_camera_get_unit (fixture->camera, "frames-per-second") == UCA_UNIT_COUNT);
}

static void
test_binnings_properties (Fixture *fixture, gconstpointer data)
{
    UcaCamera *camera = UCA_CAMERA (fixture->camera);

    GValueArray *array = NULL;
    g_object_get (G_OBJECT (camera),
            "sensor-horizontal-binnings", &array,
            NULL);

    GValue *value = g_value_array_get_nth (array, 0);
    g_assert (value != NULL);
    g_assert (g_value_get_uint (value) == 1);
}

static void
test_signal (Fixture *fixture, gconstpointer data)
{
    UcaCamera *camera = UCA_CAMERA (fixture->camera);
    gboolean success = FALSE;
    g_signal_connect (camera, "notify::frames-per-second", (GCallback) on_property_change, &success);
    g_object_set (G_OBJECT (camera), "frames-per-second", 30.0, NULL);
    g_assert (success == TRUE);
}

static void
test_overwriting_units (Fixture *fixture, gconstpointer data)
{
    uca_camera_register_unit (fixture->camera, "sensor-width", UCA_UNIT_PIXEL);
}

static void
test_can_be_written (Fixture *fixture, gconstpointer data)
{
    GError *error = NULL;

    /* read-only cannot ever be written */
    g_assert (!uca_camera_is_writable_during_acquisition (fixture->camera, "name"));

    /* unset properties cannot be written */
    g_assert (!uca_camera_is_writable_during_acquisition (fixture->camera, "roi-width"));

    /* check trivial cases */
    uca_camera_set_writable (fixture->camera, "roi-width", TRUE);
    g_assert (uca_camera_is_writable_during_acquisition (fixture->camera, "roi-width"));

    uca_camera_set_writable (fixture->camera, "roi-height", FALSE);
    g_assert (!uca_camera_is_writable_during_acquisition (fixture->camera, "roi-height"));

    /* Now, do a real test */
    uca_camera_set_writable (fixture->camera, "roi-height", TRUE);
    uca_camera_start_recording (fixture->camera, &error);
    g_assert_no_error (error);
    g_object_set (fixture->camera, "roi-height", 128, NULL);
#if (GLIB_CHECK_VERSION (2, 34, 0))
    uca_camera_set_writable (fixture->camera, "roi-x0", FALSE);
    g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Property 'roi-x0' cant be changed during acquisition");
    g_object_set (fixture->camera, "roi-x0", 64, NULL);
    g_test_assert_expected_messages ();
#endif
    uca_camera_stop_recording (fixture->camera, &error);
    g_assert_no_error (error);
}

static void
test_factory_hashtable (Fixture *fixture, gconstpointer data)
{
    GError *error = NULL;

    guint checkvalue = 42;

    gchar *foo = "roi-x0";
    gchar *bar = "roi-y0";
    GValue baz = G_VALUE_INIT;
    g_value_init(&baz, G_TYPE_UINT);
    g_value_set_uint(&baz, checkvalue);

    GHashTable *ght = g_hash_table_new (NULL, NULL);
    g_hash_table_insert(ght, foo, &baz);
    g_hash_table_insert(ght, bar, &baz);

    UcaCamera *camera = uca_plugin_manager_get_camerah (fixture->manager,
                                                     "mock", ght, &error);
    g_hash_table_destroy(ght);

    g_assert (error == NULL);
    g_assert (camera);

    guint roi_x0 = 0;
    g_object_get (G_OBJECT (camera), "roi-x0", &roi_x0, NULL);
    g_assert (roi_x0 == checkvalue);

    guint roi_y0 = 0;
    g_object_get (G_OBJECT (camera), "roi-y0", &roi_y0, NULL);
    g_assert (roi_y0 == checkvalue);

    g_object_unref(camera);
}

int main (int argc, char *argv[])
{
    gsize n_tests;

#if !(GLIB_CHECK_VERSION (2, 36, 0))
    g_type_init ();
#endif

    g_test_init (&argc, &argv, NULL);
    g_test_bug_base ("http://ufo.kit.edu/ufo/ticket");

    struct {
        const gchar *name;
        void (*test_func) (Fixture *fixture, gconstpointer data);
    }
    tests[] = {
        {"/factory", test_factory},
        {"/factory/hashtable", test_factory_hashtable},
        {"/signal", test_signal},
        {"/recording", test_recording},
        {"/recording/signal", test_recording_signal},
        {"/recording/asynchronous", test_recording_async},
        {"/recording/buffered", test_recording_buffered},
        {"/properties/base", test_base_properties},
        {"/properties/recording", test_recording_property},
        {"/properties/binnings", test_binnings_properties},
        {"/properties/frames-per-second", test_fps_property},
        {"/properties/units", test_property_units},
        {"/properties/units/overwrite", test_overwriting_units},
        {"/properties/can-be-written", test_can_be_written},
    };

    n_tests = sizeof(tests) / sizeof(tests[0]);

    for (gsize i = 0; i < n_tests; i++)
        g_test_add (tests[i].name, Fixture, NULL, fixture_setup, tests[i].test_func, fixture_teardown);

    return g_test_run ();
}