~ubuntu-branches/ubuntu/vivid/upower/vivid

« back to all changes in this revision

Viewing changes to .pc/00git_updates.patch/src/up-self-test.c

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-10-08 18:26:25 UTC
  • mfrom: (49.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20131008182625-roccyppydypnly0t
Tags: 0.9.22-1
* New upstream release.
* Drop 00git_updates.patch, included in new release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2
 
 *
3
 
 * Copyright (C) 2007-2009 Richard Hughes <richard@hughsie.com>
4
 
 *
5
 
 * Licensed under the GNU General Public License Version 2
6
 
 *
7
 
 * This program is free software; you can redistribute it and/or modify
8
 
 * it under the terms of the GNU General Public License as published by
9
 
 * the Free Software Foundation; either version 2 of the License, or
10
 
 * (at your option) any later version.
11
 
 *
12
 
 * This program is distributed in the hope that it will be useful,
13
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
 * GNU General Public License for more details.
16
 
 *
17
 
 * You should have received a copy of the GNU General Public License
18
 
 * along with this program; if not, write to the Free Software
19
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
 
 */
21
 
 
22
 
#include "config.h"
23
 
 
24
 
#include <glib-object.h>
25
 
#include <glib/gstdio.h>
26
 
#include <up-history-item.h>
27
 
#include <stdlib.h>
28
 
#include <errno.h>
29
 
#include "up-backend.h"
30
 
#include "up-daemon.h"
31
 
#include "up-device.h"
32
 
#include "up-device-list.h"
33
 
#include "up-history.h"
34
 
#include "up-native.h"
35
 
#include "up-polkit.h"
36
 
#include "up-qos.h"
37
 
#include "up-wakeups.h"
38
 
 
39
 
gchar *history_dir = NULL;
40
 
 
41
 
#define DBUS_SYSTEM_SOCKET "/var/run/dbus/system_bus_socket"
42
 
 
43
 
static void
44
 
up_test_native_func (void)
45
 
{
46
 
        const gchar *path;
47
 
 
48
 
        path = up_native_get_native_path (NULL);
49
 
        g_assert_cmpstr (path, ==, "/sys/dummy");
50
 
}
51
 
 
52
 
static void
53
 
up_test_backend_func (void)
54
 
{
55
 
        UpBackend *backend;
56
 
 
57
 
        backend = up_backend_new ();
58
 
        g_assert (backend != NULL);
59
 
 
60
 
        /* unref */
61
 
        g_object_unref (backend);
62
 
}
63
 
 
64
 
static void
65
 
up_test_daemon_func (void)
66
 
{
67
 
        UpDaemon *daemon;
68
 
 
69
 
        /* needs polkit, which only listens to the system bus */
70
 
        if (!g_file_test (DBUS_SYSTEM_SOCKET, G_FILE_TEST_EXISTS)) {
71
 
                puts("No system D-BUS running, skipping test");
72
 
                return;
73
 
        }
74
 
 
75
 
        daemon = up_daemon_new ();
76
 
        g_assert (daemon != NULL);
77
 
 
78
 
        /* unref */
79
 
        g_object_unref (daemon);
80
 
}
81
 
 
82
 
static void
83
 
up_test_device_func (void)
84
 
{
85
 
        UpDevice *device;
86
 
 
87
 
        device = up_device_new ();
88
 
        g_assert (device != NULL);
89
 
 
90
 
        /* unref */
91
 
        g_object_unref (device);
92
 
}
93
 
 
94
 
static void
95
 
up_test_device_list_func (void)
96
 
{
97
 
        UpDeviceList *list;
98
 
        GObject *native;
99
 
        GObject *device;
100
 
        GObject *found;
101
 
        gboolean ret;
102
 
 
103
 
        list = up_device_list_new ();
104
 
        g_assert (list != NULL);
105
 
 
106
 
        /* add device */
107
 
        native = g_object_new (G_TYPE_OBJECT, NULL);
108
 
        device = g_object_new (G_TYPE_OBJECT, NULL);
109
 
        ret = up_device_list_insert (list, native, device);
110
 
        g_assert (ret);
111
 
 
112
 
        /* find device */
113
 
        found = up_device_list_lookup (list, native);
114
 
        g_assert (found != NULL);
115
 
        g_object_unref (found);
116
 
 
117
 
        /* remove device */
118
 
        ret = up_device_list_remove (list, device);
119
 
        g_assert (ret);
120
 
 
121
 
        /* unref */
122
 
        g_object_unref (native);
123
 
        g_object_unref (device);
124
 
        g_object_unref (list);
125
 
}
126
 
 
127
 
static void
128
 
up_test_history_remove_temp_files (void)
129
 
{
130
 
        gchar *filename;
131
 
        filename = g_build_filename (history_dir, "history-time-full-test.dat", NULL);
132
 
        g_unlink (filename);
133
 
        g_free (filename);
134
 
        filename = g_build_filename (history_dir, "history-time-empty-test.dat", NULL);
135
 
        g_unlink (filename);
136
 
        g_free (filename);
137
 
        filename = g_build_filename (history_dir, "history-charge-test.dat", NULL);
138
 
        g_unlink (filename);
139
 
        g_free (filename);
140
 
        filename = g_build_filename (history_dir, "history-rate-test.dat", NULL);
141
 
        g_unlink (filename);
142
 
        g_free (filename);
143
 
}
144
 
 
145
 
static void
146
 
up_test_history_func (void)
147
 
{
148
 
        UpHistory *history;
149
 
        gboolean ret;
150
 
        GPtrArray *array;
151
 
        gchar *filename;
152
 
        UpHistoryItem *item;
153
 
 
154
 
        history = up_history_new ();
155
 
        g_assert (history != NULL);
156
 
 
157
 
        /* set a temporary directory for the history */
158
 
        history_dir = g_build_filename (g_get_tmp_dir(), "upower-test.XXXXXX", NULL);
159
 
        if (mkdtemp (history_dir) == NULL)
160
 
                g_error ("Cannot create temporary directory: %s", g_strerror(errno));
161
 
        up_history_set_directory (history, history_dir);
162
 
 
163
 
        /* remove previous test files */
164
 
        up_test_history_remove_temp_files ();
165
 
 
166
 
        /* setup fresh environment */
167
 
        ret = up_history_set_id (history, "test");
168
 
        g_assert (ret);
169
 
 
170
 
        /* get nonexistant data */
171
 
        array = up_history_get_data (history, UP_HISTORY_TYPE_CHARGE, 10, 100);
172
 
        g_assert (array != NULL);
173
 
        g_assert_cmpint (array->len, ==, 0);
174
 
 
175
 
        /* setup some fake device */
176
 
        up_history_set_state (history, UP_DEVICE_STATE_CHARGING);
177
 
        up_history_set_charge_data (history, 90);
178
 
        up_history_set_rate_data (history, 1.00f);
179
 
        up_history_set_time_empty_data (history, 12345);
180
 
        up_history_set_time_full_data (history, 54321);
181
 
 
182
 
        /* sleep for a little bit */
183
 
        g_usleep (3 * G_USEC_PER_SEC);
184
 
        up_history_set_charge_data (history, 91);
185
 
        up_history_set_rate_data (history, 1.01f);
186
 
        up_history_set_time_empty_data (history, 12344);
187
 
        up_history_set_time_full_data (history, 54320);
188
 
 
189
 
        /* get data for last 10 seconds */
190
 
        array = up_history_get_data (history, UP_HISTORY_TYPE_CHARGE, 10, 100);
191
 
        g_assert (array != NULL);
192
 
        g_assert_cmpint (array->len, ==, 2);
193
 
 
194
 
        /* get the first item, which should be the most recent */
195
 
        item = g_ptr_array_index (array, 0);
196
 
        g_assert (item != NULL);
197
 
        g_assert_cmpint (up_history_item_get_value (item), ==, 91);
198
 
        g_assert_cmpint (up_history_item_get_time (item), >, 1000000);
199
 
        g_ptr_array_unref (array);
200
 
 
201
 
        /* force a save to disk */
202
 
        ret = up_history_save_data (history);
203
 
        g_assert (ret);
204
 
        g_object_unref (history);
205
 
 
206
 
        /* ensure the file was created */
207
 
        filename = g_build_filename (history_dir, "history-charge-test.dat", NULL);
208
 
        g_assert (g_file_test (filename, G_FILE_TEST_EXISTS));
209
 
        g_free (filename);
210
 
 
211
 
        /* ensure we can load from disk */
212
 
        history = up_history_new ();
213
 
        up_history_set_directory (history, history_dir);
214
 
        up_history_set_id (history, "test");
215
 
 
216
 
        /* get data for last 10 seconds */
217
 
        array = up_history_get_data (history, UP_HISTORY_TYPE_CHARGE, 10, 100);
218
 
        g_assert (array != NULL);
219
 
        g_assert_cmpint (array->len, ==, 3); /* we have inserted an unknown as the first entry */
220
 
        item = g_ptr_array_index (array, 1);
221
 
        g_assert (item != NULL);
222
 
        g_assert_cmpint (up_history_item_get_value (item), ==, 91);
223
 
        g_assert_cmpint (up_history_item_get_time (item), >, 1000000);
224
 
        g_ptr_array_unref (array);
225
 
 
226
 
        /* ensure old entries are purged */
227
 
        up_history_set_max_data_age (history, 2);
228
 
        g_usleep (1100 * G_USEC_PER_SEC / 1000);
229
 
        g_object_unref (history);
230
 
 
231
 
        /* ensure only 2 points are returned */
232
 
        history = up_history_new ();
233
 
        up_history_set_directory (history, history_dir);
234
 
        up_history_set_id (history, "test");
235
 
        array = up_history_get_data (history, UP_HISTORY_TYPE_CHARGE, 10, 100);
236
 
        g_assert (array != NULL);
237
 
        g_assert_cmpint (array->len, ==, 2);
238
 
        g_ptr_array_unref (array);
239
 
 
240
 
        /* unref */
241
 
        g_object_unref (history);
242
 
 
243
 
        /* remove these test files */
244
 
        up_test_history_remove_temp_files ();
245
 
        rmdir (history_dir);
246
 
}
247
 
 
248
 
static void
249
 
up_test_polkit_func (void)
250
 
{
251
 
        UpPolkit *polkit;
252
 
 
253
 
        /* polkit only listens to the system bus */
254
 
        if (!g_file_test (DBUS_SYSTEM_SOCKET, G_FILE_TEST_EXISTS)) {
255
 
                puts("No system D-BUS running, skipping test");
256
 
                return;
257
 
        }
258
 
 
259
 
        polkit = up_polkit_new ();
260
 
        g_assert (polkit != NULL);
261
 
 
262
 
        /* unref */
263
 
        g_object_unref (polkit);
264
 
}
265
 
 
266
 
static void
267
 
up_test_qos_func (void)
268
 
{
269
 
        UpQos *qos;
270
 
 
271
 
        /* needs polkit, which only listens to the system bus */
272
 
        if (!g_file_test (DBUS_SYSTEM_SOCKET, G_FILE_TEST_EXISTS)) {
273
 
                puts("No system D-BUS running, skipping test");
274
 
                return;
275
 
        }
276
 
 
277
 
        qos = up_qos_new ();
278
 
        g_assert (qos != NULL);
279
 
 
280
 
        /* unref */
281
 
        g_object_unref (qos);
282
 
}
283
 
 
284
 
static void
285
 
up_test_wakeups_func (void)
286
 
{
287
 
        UpWakeups *wakeups;
288
 
 
289
 
        wakeups = up_wakeups_new ();
290
 
        g_assert (wakeups != NULL);
291
 
 
292
 
        /* unref */
293
 
        g_object_unref (wakeups);
294
 
}
295
 
 
296
 
int
297
 
main (int argc, char **argv)
298
 
{
299
 
        g_type_init ();
300
 
        g_test_init (&argc, &argv, NULL);
301
 
 
302
 
        /* make check, vs. make distcheck */
303
 
        if (g_file_test ("../etc/UPower.conf", G_FILE_TEST_EXISTS))
304
 
                g_setenv ("UPOWER_CONF_FILE_NAME", "../etc/UPower.conf", TRUE);
305
 
        else
306
 
                g_setenv ("UPOWER_CONF_FILE_NAME", "../../etc/UPower.conf", TRUE);
307
 
 
308
 
        /* tests go here */
309
 
        g_test_add_func ("/power/backend", up_test_backend_func);
310
 
        g_test_add_func ("/power/device", up_test_device_func);
311
 
        g_test_add_func ("/power/device_list", up_test_device_list_func);
312
 
        g_test_add_func ("/power/history", up_test_history_func);
313
 
        g_test_add_func ("/power/native", up_test_native_func);
314
 
        g_test_add_func ("/power/polkit", up_test_polkit_func);
315
 
        g_test_add_func ("/power/qos", up_test_qos_func);
316
 
        g_test_add_func ("/power/wakeups", up_test_wakeups_func);
317
 
        g_test_add_func ("/power/daemon", up_test_daemon_func);
318
 
 
319
 
        return g_test_run ();
320
 
}
321