1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
3
* Copyright (C) 2007-2009 Richard Hughes <richard@hughsie.com>
5
* Licensed under the GNU General Public License Version 2
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.
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.
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.
24
#include <glib-object.h>
25
#include <glib/gstdio.h>
26
#include <up-history-item.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"
37
#include "up-wakeups.h"
39
gchar *history_dir = NULL;
41
#define DBUS_SYSTEM_SOCKET "/var/run/dbus/system_bus_socket"
44
up_test_native_func (void)
48
path = up_native_get_native_path (NULL);
49
g_assert_cmpstr (path, ==, "/sys/dummy");
53
up_test_backend_func (void)
57
backend = up_backend_new ();
58
g_assert (backend != NULL);
61
g_object_unref (backend);
65
up_test_daemon_func (void)
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");
75
daemon = up_daemon_new ();
76
g_assert (daemon != NULL);
79
g_object_unref (daemon);
83
up_test_device_func (void)
87
device = up_device_new ();
88
g_assert (device != NULL);
91
g_object_unref (device);
95
up_test_device_list_func (void)
103
list = up_device_list_new ();
104
g_assert (list != NULL);
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);
113
found = up_device_list_lookup (list, native);
114
g_assert (found != NULL);
115
g_object_unref (found);
118
ret = up_device_list_remove (list, device);
122
g_object_unref (native);
123
g_object_unref (device);
124
g_object_unref (list);
128
up_test_history_remove_temp_files (void)
131
filename = g_build_filename (history_dir, "history-time-full-test.dat", NULL);
134
filename = g_build_filename (history_dir, "history-time-empty-test.dat", NULL);
137
filename = g_build_filename (history_dir, "history-charge-test.dat", NULL);
140
filename = g_build_filename (history_dir, "history-rate-test.dat", NULL);
146
up_test_history_func (void)
154
history = up_history_new ();
155
g_assert (history != NULL);
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);
163
/* remove previous test files */
164
up_test_history_remove_temp_files ();
166
/* setup fresh environment */
167
ret = up_history_set_id (history, "test");
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);
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);
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);
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);
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);
201
/* force a save to disk */
202
ret = up_history_save_data (history);
204
g_object_unref (history);
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));
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");
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);
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);
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);
241
g_object_unref (history);
243
/* remove these test files */
244
up_test_history_remove_temp_files ();
249
up_test_polkit_func (void)
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");
259
polkit = up_polkit_new ();
260
g_assert (polkit != NULL);
263
g_object_unref (polkit);
267
up_test_qos_func (void)
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");
278
g_assert (qos != NULL);
281
g_object_unref (qos);
285
up_test_wakeups_func (void)
289
wakeups = up_wakeups_new ();
290
g_assert (wakeups != NULL);
293
g_object_unref (wakeups);
297
main (int argc, char **argv)
300
g_test_init (&argc, &argv, NULL);
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);
306
g_setenv ("UPOWER_CONF_FILE_NAME", "../../etc/UPower.conf", TRUE);
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);
319
return g_test_run ();