Debian Codegraph @

lib/packagekit-glib2/pk-test-daemon.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2007-2014 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU Lesser General Public License Version 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; 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 <string.h>
25 #include <glib-object.h>
26 #include <glib/gstdio.h>
27 #include <gio/gunixsocketaddress.h>
28
29 #include "pk-client.h"
30 #include "pk-client-helper.h"
31 #include "pk-control.h"
32 #include "pk-console-private.h"
33 #include "pk-offline.h"
34 #include "pk-offline-private.h"
35 #include "pk-package-ids.h"
36 #include "pk-results.h"
37 #include "pk-task.h"
38 #include "pk-task-text.h"
39 #include "pk-task-wrapper.h"
40 #include "pk-transaction-list.h"
41 #include "pk-version.h"
42 #include "pk-control-sync.h"
43 #include "pk-client-sync.h"
44 #include "pk-debug.h"
45
46 /** ver:1.0 ***********************************************************/
47 static GMainLoop *_test_loop = NULL;
48 static guint _test_loop_timeout_id = 0;
49
50 static gboolean
51 _g_test_hang_check_cb (gpointer user_data)
52 {
53 guint timeout_ms = *((guint*) user_data);
54 g_main_loop_quit (_test_loop);
55 g_warning ("loop not completed in %ims", timeout_ms);
56 g_assert_not_reached ();
57 return FALSE;
58 }
59
60 /*
61 * _g_test_loop_run_with_timeout:
62 **/
63 static void
64 _g_test_loop_run_with_timeout (guint timeout_ms)
65 {
66 g_assert (_test_loop_timeout_id == 0);
67 _test_loop = g_main_loop_new (NULL, FALSE);
68 _test_loop_timeout_id = g_timeout_add (timeout_ms, _g_test_hang_check_cb, &timeout_ms);
69 g_main_loop_run (_test_loop);
70 }
71
72 #if 0
73 static gboolean
74 _g_test_hang_wait_cb (gpointer user_data)
75 {
76 g_main_loop_quit (_test_loop);
77 _test_loop_timeout_id = 0;
78 return FALSE;
79 }
80
81 /*
82 * _g_test_loop_wait:
83 **/
84 static void
85 _g_test_loop_wait (guint timeout_ms)
86 {
87 g_assert (_test_loop_timeout_id == 0);
88 _test_loop = g_main_loop_new (NULL, FALSE);
89 _test_loop_timeout_id = g_timeout_add (timeout_ms, _g_test_hang_wait_cb, &timeout_ms);
90 g_main_loop_run (_test_loop);
91 }
92 #endif
93
94 /*
95 * _g_test_loop_quit:
96 **/
97 static void
98 _g_test_loop_quit (void)
99 {
100 if (_test_loop_timeout_id > 0) {
101 g_source_remove (_test_loop_timeout_id);
102 _test_loop_timeout_id = 0;
103 }
104 if (_test_loop != NULL) {
105 g_main_loop_quit (_test_loop);
106 g_main_loop_unref (_test_loop);
107 _test_loop = NULL;
108 }
109 }
110
111 /**********************************************************************/
112
113 static void
114 pk_test_offline_cb (GObject *object, GAsyncResult *res, gpointer user_data)
115 {
116 PkClient *client = PK_CLIENT (object);
117 g_autoptr(GError) error = NULL;
118 g_autoptr(PkResults) results = NULL;
119
120 /* get the results */
121 results = pk_client_generic_finish (client, res, &error);
122 g_assert_no_error (error);
123 g_assert (results != NULL);
124
125 _g_test_loop_quit ();
126 }
127
128 static void
129 pk_test_offline_func (void)
130 {
131 gboolean ret;
132 g_autoptr(GError) error = NULL;
133 g_autofree gchar *data = NULL;
134 g_autoptr(PkClient) client = NULL;
135 g_auto(GStrv) package_ids = NULL;
136
137 /* set up an offline update */
138 client = pk_client_new ();
139 package_ids = pk_package_ids_from_string ("powertop;1.8-1.fc8;i386;fedora");
140 pk_client_update_packages_async (client,
141 pk_bitfield_from_enums (PK_TRANSACTION_FLAG_ENUM_ONLY_DOWNLOAD, -1),
142 package_ids,
143 NULL,
144 NULL, NULL,
145 pk_test_offline_cb, NULL);
146 _g_test_loop_run_with_timeout (25000);
147 g_assert (g_file_test (PK_OFFLINE_PREPARED_FILENAME, G_FILE_TEST_EXISTS));
148 g_assert (!g_file_test (PK_OFFLINE_TRIGGER_FILENAME, G_FILE_TEST_EXISTS));
149
150 /* check prepared contents */
151 ret = g_file_get_contents (PK_OFFLINE_PREPARED_FILENAME, &data, NULL, &error);
152 g_assert_no_error (error);
153 g_assert (ret);
154 g_assert_cmpstr (data, ==, "powertop;1.8-1.fc8;i386;fedora");
155
156 /* trigger */
157 ret = pk_offline_trigger_with_flags (PK_OFFLINE_ACTION_REBOOT, PK_OFFLINE_FLAGS_INTERACTIVE, NULL, &error);
158 g_assert_no_error (error);
159 g_assert (ret);
160 g_assert (g_file_test (PK_OFFLINE_PREPARED_FILENAME, G_FILE_TEST_EXISTS));
161 g_assert (g_file_test (PK_OFFLINE_TRIGGER_FILENAME, G_FILE_TEST_EXISTS));
162 g_assert (g_file_test (PK_OFFLINE_ACTION_FILENAME, G_FILE_TEST_EXISTS));
163 g_assert (!g_file_test (PK_OFFLINE_RESULTS_FILENAME, G_FILE_TEST_EXISTS));
164
165 /* cancel the trigger */
166 ret = pk_offline_cancel_with_flags (PK_OFFLINE_FLAGS_INTERACTIVE, NULL, &error);
167 g_assert_no_error (error);
168 g_assert (ret);
169 g_assert (g_file_test (PK_OFFLINE_PREPARED_FILENAME, G_FILE_TEST_EXISTS));
170 g_assert (!g_file_test (PK_OFFLINE_TRIGGER_FILENAME, G_FILE_TEST_EXISTS));
171 g_assert (g_file_test (PK_OFFLINE_ACTION_FILENAME, G_FILE_TEST_EXISTS));
172 g_assert (!g_file_test (PK_OFFLINE_RESULTS_FILENAME, G_FILE_TEST_EXISTS));
173
174 /* ensure a cache update kills the prepared update file */
175 pk_client_refresh_cache_async (client, FALSE, NULL, NULL, NULL,
176 pk_test_offline_cb, NULL);
177 _g_test_loop_run_with_timeout (25000);
178 g_assert (!g_file_test (PK_OFFLINE_PREPARED_FILENAME, G_FILE_TEST_EXISTS));
179 g_assert (!g_file_test (PK_OFFLINE_TRIGGER_FILENAME, G_FILE_TEST_EXISTS));
180 g_assert (g_file_test (PK_OFFLINE_ACTION_FILENAME, G_FILE_TEST_EXISTS));
181 g_assert (!g_file_test (PK_OFFLINE_RESULTS_FILENAME, G_FILE_TEST_EXISTS));
182 }
183
184 /*
185 * pk_test_client_helper_output_cb:
186 **/
187 static gboolean
188 pk_test_client_helper_output_cb (GSocket *socket, GIOCondition condition, gpointer user_data)
189 {
190 GError *error = NULL;
191 gsize len;
192 gchar buffer[6] = {0};
193 gboolean ret = TRUE;
194
195 /* the helper process exited */
196 if ((condition & G_IO_HUP) > 0) {
197 g_debug ("socket was disconnected");
198 ret = FALSE;
199 goto out;
200 }
201
202 /* there is data */
203 if ((condition & G_IO_IN) > 0) {
204 len = g_socket_receive (socket, buffer, 6, NULL, &error);
205 g_assert_no_error (error);
206 g_assert_cmpint (len, >, 0);
207
208 /* good for us */
209 if (strncmp (buffer, "pong\n", len) == 0) {
210 _g_test_loop_quit ();
211 goto out;
212 }
213 /* bad */
214 g_warning ("child returned unexpected data: %s", buffer);
215 }
216 out:
217 return ret;
218 }
219
220 static void
221 pk_test_client_helper_func (void)
222 {
223 gboolean ret;
224 GError *error = NULL;
225 gchar *filename;
226 PkClientHelper *client_helper;
227 const gchar *argv[] = { TESTDATADIR "/pk-client-helper-test.py", NULL };
228 const gchar *envp[] = { "DAVE=1", NULL };
229 GSocket *socket = NULL;
230 GSocketAddress *address = NULL;
231 gsize wrote;
232 GSource *source;
233
234 client_helper = pk_client_helper_new ();
235 g_assert (client_helper != NULL);
236
237 /* unref without using */
238 g_object_unref (client_helper);
239
240 /* new object */
241 client_helper = pk_client_helper_new ();
242 g_assert (client_helper != NULL);
243
244 /* create a socket filename */
245 filename = g_build_filename (g_get_tmp_dir (), "pk-self-test.socket", NULL);
246
247 /* ensure previous sockets are deleted */
248 g_unlink (filename);
249
250 /* start a demo program */
251 ret = pk_client_helper_start (client_helper, filename, (gchar**) argv, (gchar**) envp, &error);
252 g_assert_no_error (error);
253 g_assert (ret);
254 g_assert (g_file_test (filename, G_FILE_TEST_EXISTS));
255
256 /* create socket */
257 socket = g_socket_new (G_SOCKET_FAMILY_UNIX, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, &error);
258 g_assert_no_error (error);
259 g_assert (socket != NULL);
260 g_socket_set_blocking (socket, FALSE);
261 g_socket_set_keepalive (socket, TRUE);
262
263 /* connect to it */
264 address = g_unix_socket_address_new (filename);
265 ret = g_socket_connect (socket, address, NULL, &error);
266 g_assert_no_error (error);
267 g_assert (ret);
268
269 /* socket has data */
270 source = g_socket_create_source (socket, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, NULL);
271 g_source_set_callback (source, G_SOURCE_FUNC (pk_test_client_helper_output_cb), NULL, NULL);
272 g_source_attach (source, NULL);
273
274 /* send some data */
275 wrote = g_socket_send (socket, "ping\n", 5, NULL, &error);
276 g_assert_no_error (error);
277 g_assert_cmpint (wrote, ==, 5);
278
279 /* run for a few seconds */
280 _g_test_loop_run_with_timeout (1000);
281
282 /* stop the demo program */
283 ret = pk_client_helper_stop (client_helper, &error);
284 g_assert_no_error (error);
285 g_assert (ret);
286
287 /* delete socket */
288 g_unlink (filename);
289
290 g_free (filename);
291 g_object_unref (socket);
292 g_object_unref (address);
293 g_object_unref (client_helper);
294 }
295
296 static void
297 pk_test_client_resolve_cb (GObject *object, GAsyncResult *res, gpointer user_data)
298 {
299 PkClient *client = PK_CLIENT (object);
300 GError *error = NULL;
301 PkResults *results = NULL;
302 PkExitEnum exit_enum;
303 GPtrArray *packages;
304 gboolean idle;
305
306 /* get the results */
307 results = pk_client_generic_finish (client, res, &error);
308 g_assert_no_error (error);
309 g_assert (results != NULL);
310
311 exit_enum = pk_results_get_exit_code (results);
312 g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
313
314 packages = pk_results_get_package_array (results);
315 g_assert (packages != NULL);
316
317 /* check idle */
318 g_object_get (client, "idle", &idle, NULL);
319 g_assert (idle);
320 g_assert_cmpint (packages->len, ==, 2);
321
322 g_ptr_array_unref (packages);
323
324 g_debug ("results exit enum = %s", pk_exit_enum_to_string (exit_enum));
325
326 g_object_unref (results);
327 _g_test_loop_quit ();
328 }
329
330 static void
331 pk_test_client_get_details_cb (GObject *object, GAsyncResult *res, gpointer user_data)
332 {
333 PkClient *client = PK_CLIENT (object);
334 GError *error = NULL;
335 PkResults *results = NULL;
336 PkExitEnum exit_enum;
337 GPtrArray *details;
338
339 /* get the results */
340 results = pk_client_generic_finish (client, res, &error);
341 g_assert_no_error (error);
342 g_assert (results != NULL);
343
344 exit_enum = pk_results_get_exit_code (results);
345 g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
346
347 details = pk_results_get_details_array (results);
348 g_assert (details != NULL);
349 g_assert_cmpint (details->len, ==, 1);
350
351 g_ptr_array_unref (details);
352
353 g_debug ("results exit enum = %s", pk_exit_enum_to_string (exit_enum));
354
355 g_object_unref (results);
356 _g_test_loop_quit ();
357 }
358
359 static void
360 pk_test_client_get_updates_cb (GObject *object, GAsyncResult *res, gpointer user_data)
361 {
362 PkClient *client = PK_CLIENT (object);
363 GError *error = NULL;
364 PkResults *results = NULL;
365 PkExitEnum exit_enum;
366 PkPackageSack *sack;
367 guint size;
368
369 /* get the results */
370 results = pk_client_generic_finish (client, res, &error);
371 g_assert_no_error (error);
372 g_assert (results != NULL);
373
374 exit_enum = pk_results_get_exit_code (results);
375 g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
376
377 sack = pk_results_get_package_sack (results);
378 g_assert (sack != NULL);
379
380 /* check size */
381 size = pk_package_sack_get_size (sack);
382 g_assert_cmpint (size, ==, 3);
383
384 g_object_unref (sack);
385
386 g_debug ("results exit enum = %s", pk_exit_enum_to_string (exit_enum));
387
388 g_object_unref (results);
389 _g_test_loop_quit ();
390 }
391
392 static void
393 pk_test_client_search_name_cb (GObject *object, GAsyncResult *res, gpointer user_data)
394 {
395 PkClient *client = PK_CLIENT (object);
396 GError *error = NULL;
397 PkResults *results = NULL;
398 PkExitEnum exit_enum;
399 PkError *error_code = NULL;
400
401 /* get the results */
402 results = pk_client_generic_finish (client, res, &error);
403 g_assert_no_error (error);
404 g_assert (results != NULL);
405
406 exit_enum = pk_results_get_exit_code (results);
407 g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_CANCELLED);
408
409 /* check error code */
410 error_code = pk_results_get_error_code (results);
411 g_assert_cmpint (pk_error_get_code (error_code), ==, PK_ERROR_ENUM_TRANSACTION_CANCELLED);
412 g_assert_cmpstr (pk_error_get_details (error_code), ==, "The task was stopped successfully");
413 // g_assert_cmpstr (pk_error_get_details (error_code), ==, "transaction was cancelled");
414
415 if (error_code != NULL)
416 g_object_unref (error_code);
417 if (results != NULL)
418 g_object_unref (results);
419 _g_test_loop_quit ();
420 }
421
422 static void
423 pk_test_client_search_name_cancellable_cancelled_cb (GObject *object, GAsyncResult *res, gpointer user_data)
424 {
425 PkClient *client = PK_CLIENT (object);
426 GError *error = NULL;
427 PkResults *results = NULL;
428 results = pk_client_generic_finish (client, res, &error);
429 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
430 g_assert (results == NULL);
431 _g_test_loop_quit ();
432 }
433
434 static guint _progress_cb = 0;
435 static guint _status_cb = 0;
436 static guint _package_cb = 0;
437 static guint _allow_cancel_cb = 0;
438 gchar *_tid = NULL;
439
440 static void
441 pk_test_client_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data)
442 {
443 gchar *tid;
444 if (type == PK_PROGRESS_TYPE_PACKAGE_ID)
445 _package_cb++;
446 if (type == PK_PROGRESS_TYPE_PERCENTAGE)
447 _progress_cb++;
448 if (type == PK_PROGRESS_TYPE_ALLOW_CANCEL)
449 _allow_cancel_cb++;
450 if (type == PK_PROGRESS_TYPE_STATUS)
451 _status_cb++;
452
453 /* get the running transaction id if we've not set it before */
454 g_object_get (progress, "transaction-id", &tid, NULL);
455 if (tid != NULL && _tid == NULL)
456 _tid = g_strdup (tid);
457 g_free (tid);
458 }
459
460 static gboolean
461 pk_test_client_cancel_cb (GCancellable *cancellable)
462 {
463 g_debug ("cancelling method");
464 g_cancellable_cancel (cancellable);
465 return FALSE;
466 }
467
468 static void
469 pk_test_client_download_cb (GObject *object, GAsyncResult *res, gpointer user_data)
470 {
471 PkClient *client = PK_CLIENT (object);
472 GError *error = NULL;
473 PkResults *results = NULL;
474 PkExitEnum exit_enum;
475 PkFiles *item;
476 GPtrArray *array = NULL;
477 gchar *package_id = NULL;
478 gchar **files = NULL;
479
480 /* get the results */
481 results = pk_client_generic_finish (client, res, &error);
482 g_assert_no_error (error);
483 g_assert (results != NULL);
484
485 exit_enum = pk_results_get_exit_code (results);
486 g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
487
488 /* check number */
489 array = pk_results_get_files_array (results);
490 g_assert_cmpint (array->len, ==, 2);
491
492 /* check a result */
493 item = g_ptr_array_index (array, 0);
494 g_object_get (item,
495 "package-id", &package_id,
496 "files", &files,
497 NULL);
498 g_assert_cmpstr (package_id, ==, "powertop-common;1.8-1.fc8;i386;fedora");
499 g_assert_cmpint (g_strv_length (files), ==, 1);
500 g_assert_cmpstr (files[0], ==, "/tmp/powertop-common-1.8-1.fc8.rpm");
501
502 g_strfreev (files);
503 g_free (package_id);
504 g_ptr_array_unref (array);
505 if (results != NULL)
506 g_object_unref (results);
507 _g_test_loop_quit ();
508 }
509
510 #if 0
511 static void
512 pk_test_client_recursive_signal_cb (PkControl *control, gpointer user_data)
513 {
514 gboolean ret;
515 ret = pk_control_get_properties (control, NULL, NULL);
516 g_assert (ret);
517 }
518 #endif
519
520 static void
521 pk_test_client_notify_idle_cb (PkClient *client, GParamSpec *pspec, gpointer user_data)
522 {
523 gboolean idle;
524 g_object_get (client, "idle", &idle, NULL);
525 g_debug ("idle=%i", idle);
526 }
527
528 static void
529 pk_test_client_update_system_socket_test_cb (GObject *object, GAsyncResult *res, gpointer user_data)
530 {
531 PkClient *client = PK_CLIENT (object);
532 GError *error = NULL;
533 g_autoptr(PkResults) results = NULL;
534 g_autoptr(GPtrArray) categories = NULL;
535
536 /* get the results */
537 results = pk_client_generic_finish (client, res, &error);
538 g_assert_no_error (error);
539 g_assert (results != NULL);
540
541 /* make sure we handled the ping/pong frontend-socket thing, which is 5 + 1 */
542 categories = pk_results_get_category_array (results);
543 g_assert_cmpint (categories->len, ==, 1);
544 _g_test_loop_quit ();
545 }
546
547 static void
548 pk_test_client_func (void)
549 {
550 gchar **package_ids;
551 // gchar *file;
552 gboolean ret;
553 gchar **values;
554 GError *error = NULL;
555 PkProgress *progress;
556 gchar *tid;
557 PkRoleEnum role;
558 PkStatusEnum status;
559 // PkResults *results;
560 g_autoptr(GCancellable) cancellable = NULL;
561 g_autoptr(PkClient) client = NULL;
562
563 #if 0
564 /* test user temp */
565 file = pk_client_get_user_temp ("self-test", NULL);
566 g_assert (g_str_has_suffix (file, "/.PackageKit/self-test"));
567 g_assert (g_str_has_prefix (file, "/home/"));
568 g_free (file);
569
570 /* test native TRUE */
571 ret = pk_client_is_file_native ("/tmp");
572 g_assert (ret);
573
574 /* test native FALSE */
575 ret = pk_client_is_file_native ("/tmp/.gvfs/moo");
576 g_assert (!ret);
577
578 /* test resolve NULL */
579 file = pk_client_real_path (NULL);
580 g_assert (file == NULL);
581
582 /* test resolve /etc/hosts */
583 file = pk_client_real_path ("/etc/hosts");
584 g_assert_cmpstr (file, ==, "/etc/hosts");
585 g_free (file);
586
587 /* test resolve /etc/../etc/hosts */
588 file = pk_client_real_path ("/etc/../etc/hosts");
589 g_assert_cmpstr (file, ==, "/etc/hosts");
590 g_free (file);
591 #endif
592
593 /* get client */
594 client = pk_client_new ();
595 g_signal_connect (client, "notify::idle",
596 G_CALLBACK (pk_test_client_notify_idle_cb), NULL);
597 g_assert (client != NULL);
598
599 /* check idle */
600 g_object_get (client, "idle", &ret, NULL);
601 g_assert (ret);
602
603 /* resolve package */
604 package_ids = pk_package_ids_from_string ("glib2;2.14.0;i386;fedora&powertop");
605 pk_client_resolve_async (client, pk_bitfield_value (PK_FILTER_ENUM_INSTALLED), package_ids, NULL,
606 (PkProgressCallback) pk_test_client_progress_cb, NULL,
607 (GAsyncReadyCallback) pk_test_client_resolve_cb, NULL);
608 g_strfreev (package_ids);
609 _g_test_loop_run_with_timeout (15000);
610 g_debug ("resolved in %f", g_test_timer_elapsed ());
611
612 /* check idle */
613 g_object_get (client, "idle", &ret, NULL);
614 g_assert (ret);
615
616 /* get progress of past transaction */
617 progress = pk_client_get_progress (client, _tid, NULL, &error);
618 g_object_get (progress,
619 "transaction-id", &tid,
620 "role", &role,
621 "status", &status,
622 NULL);
623 g_assert_cmpstr (tid, ==, _tid);
624 g_assert_cmpint (role, ==, PK_ROLE_ENUM_RESOLVE);
625 g_assert_cmpint (status, ==, PK_STATUS_ENUM_FINISHED);
626 g_debug ("got progress in %f", g_test_timer_elapsed ());
627 g_object_unref (progress);
628 g_free (tid);
629 g_free (_tid);
630
631 /* got updates */
632 g_assert_cmpint (_progress_cb, >, 0);
633 g_assert_cmpint (_status_cb, >, 0);
634
635 /* reset */
636 _progress_cb = 0;
637 _status_cb = 0;
638 // _package_cb = 0;
639
640 /* get details about package */
641 package_ids = pk_package_ids_from_id ("powertop;1.8-1.fc8;i386;fedora");
642 pk_client_get_details_async (client, package_ids, NULL,
643 (PkProgressCallback) pk_test_client_progress_cb, NULL,
644 (GAsyncReadyCallback) pk_test_client_get_details_cb, NULL);
645 g_strfreev (package_ids);
646 _g_test_loop_run_with_timeout (15000);
647 g_debug ("resolved in %f", g_test_timer_elapsed ());
648
649 /* got updates */
650 g_assert_cmpint (_progress_cb, >, 0);
651 g_assert_cmpint (_status_cb, >, 0);
652
653 /* reset */
654 _progress_cb = 0;
655 _status_cb = 0;
656 // _package_cb = 0;
657
658 /* get updates */
659 pk_client_get_updates_async (client, pk_bitfield_value (PK_FILTER_ENUM_NONE), NULL,
660 (PkProgressCallback) pk_test_client_progress_cb, NULL,
661 (GAsyncReadyCallback) pk_test_client_get_updates_cb, NULL);
662 _g_test_loop_run_with_timeout (15000);
663 g_debug ("got updates in %f", g_test_timer_elapsed ());
664
665 /* it takes more than 50ms to get the progress of the transaction, and if
666 * getting updates from internal cache, then it'll take a shed load less
667 * than this to complete */
668 if (g_test_timer_elapsed () > 100) {
669 /* got status updates */
670 g_assert_cmpint (_status_cb, >, 0);
671 }
672
673 /* search by name */
674 cancellable = g_cancellable_new ();
675 values = g_strsplit ("power", "&", -1);
676 pk_client_search_names_async (client, pk_bitfield_value (PK_FILTER_ENUM_NONE), values, cancellable,
677 (PkProgressCallback) pk_test_client_progress_cb, NULL,
678 (GAsyncReadyCallback) pk_test_client_search_name_cb, NULL);
679 g_timeout_add (500, G_SOURCE_FUNC (pk_test_client_cancel_cb), cancellable);
680 _g_test_loop_run_with_timeout (15000);
681 g_debug ("cancelled in %f", g_test_timer_elapsed ());
682
683 /* ensure we abort with error if we cancel */
684 pk_client_search_names_async (client, pk_bitfield_value (PK_FILTER_ENUM_NONE), values, cancellable,
685 (PkProgressCallback) pk_test_client_progress_cb, NULL,
686 (GAsyncReadyCallback) pk_test_client_search_name_cancellable_cancelled_cb, NULL);
687 _g_test_loop_run_with_timeout (15000);
688
689 g_strfreev (values);
690
691 /* okay now */
692 g_cancellable_reset (cancellable);
693
694 /* do the update-packages role to trigger the fake pipe stuff */
695 package_ids = pk_package_ids_from_string ("testsocket;0.1;i386;fedora");
696 pk_client_update_packages_async (client, 0, package_ids,
697 NULL,
698 (PkProgressCallback) pk_test_client_progress_cb, NULL,
699 (GAsyncReadyCallback) pk_test_client_update_system_socket_test_cb, NULL);
700 g_strfreev (package_ids);
701 _g_test_loop_run_with_timeout (15000);
702
703 /* ensure previous files are deleted */
704 g_unlink ("/tmp/powertop-1.8-1.fc8.rpm");
705 g_unlink ("/tmp/powertop-common-1.8-1.fc8.rpm");
706
707 /* do downloads */
708 package_ids = pk_package_ids_from_id ("powertop;1.8-1.fc8;i386;fedora");
709 pk_client_download_packages_async (client, package_ids, "/tmp", cancellable,
710 (PkProgressCallback) pk_test_client_progress_cb, NULL,
711 (GAsyncReadyCallback) pk_test_client_download_cb, NULL);
712 g_strfreev (package_ids);
713 _g_test_loop_run_with_timeout (15000);
714 g_debug ("downloaded and copied in %f", g_test_timer_elapsed ());
715
716 /* test recursive signal handling */
717 #if 0
718 g_signal_connect (client->priv->control, "repo-list-changed", G_CALLBACK (pk_test_client_recursive_signal_cb), NULL);
719 results = pk_client_repo_set_data (client, "dave", "moo", "data", NULL, NULL, NULL, NULL);
720 g_assert (results != NULL);
721 g_object_unref (results);
722 #endif
723 }
724
725 static void
726 pk_test_console_func (void)
727 {
728 gboolean ret;
729
730 /* get prompt 1 */
731 ret = pk_console_get_prompt ("press enter", TRUE);
732 g_assert (ret);
733
734 /* get prompt 2 */
735 ret = pk_console_get_prompt ("press enter", TRUE);
736 g_assert (ret);
737
738 /* get prompt 3 */
739 ret = pk_console_get_prompt ("press Y", TRUE);
740 g_assert (ret);
741
742 /* get prompt 3 */
743 ret = pk_console_get_prompt ("press N", TRUE);
744 g_assert (!ret);
745 }
746
747 static guint _refcount = 0;
748
749 static void
750 pk_test_control_get_tid_cb (GObject *object, GAsyncResult *res, gpointer user_data)
751 {
752 PkControl *control = PK_CONTROL (object);
753 GError *error = NULL;
754 gchar *tid;
755
756 /* get the result */
757 tid = pk_control_get_tid_finish (control, res, &error);
758 g_assert_no_error (error);
759 g_assert (tid != NULL);
760
761 g_debug ("tid = %s", tid);
762 g_free (tid);
763 if (--_refcount == 0)
764 _g_test_loop_quit ();
765 }
766
767 static void
768 pk_test_control_get_properties_cb (GObject *object, GAsyncResult *res, gpointer user_data)
769 {
770 PkControl *control = PK_CONTROL (object);
771 GError *error = NULL;
772 gboolean ret;
773 PkBitfield roles;
774 PkBitfield filters;
775 PkBitfield groups;
776 gchar **mime_types;
777 gchar *text;
778
779 /* get the result */
780 ret = pk_control_get_properties_finish (control, res, &error);
781 g_assert_no_error (error);
782 g_assert (ret);
783
784 /* get values */
785 g_object_get (control,
786 "mime-types", &mime_types,
787 "roles", &roles,
788 "filters", &filters,
789 "groups", &groups,
790 NULL);
791
792 /* check mime_types */
793 text = g_strjoinv (";", mime_types);
794 g_assert_cmpstr (text, ==, "application/x-rpm;application/x-deb");
795 g_free (text);
796 g_strfreev (mime_types);
797
798 /* check roles */
799 text = pk_role_bitfield_to_string (roles);
800 g_assert_cmpstr (text, ==, "cancel;depends-on;get-details;get-files;get-packages;get-repo-list;"
801 "required-by;get-update-detail;get-updates;install-files;install-packages;install-signature;"
802 "refresh-cache;remove-packages;repo-enable;repo-set-data;resolve;"
803 "search-details;search-file;search-group;search-name;update-packages;"
804 "what-provides;download-packages;get-distro-upgrades;"
805 "get-old-transactions;repair-system;get-details-local;"
806 "get-files-local;upgrade-system");
807 g_free (text);
808
809 /* check filters */
810 text = pk_filter_bitfield_to_string (filters);
811 g_assert_cmpstr (text, ==, "installed;devel;gui");
812 g_free (text);
813
814 /* check groups */
815 text = pk_group_bitfield_to_string (groups);
816 g_assert_cmpstr (text, ==, "accessibility;games;system");
817 g_debug ("groups = %s", text);
818
819 g_free (text);
820
821 if (--_refcount == 0)
822 _g_test_loop_quit ();
823 }
824
825 static void
826 pk_test_control_get_time_since_action_cb (GObject *object, GAsyncResult *res, gpointer user_data)
827 {
828 PkControl *control = PK_CONTROL (object);
829 GError *error = NULL;
830 guint seconds;
831
832 /* get the result */
833 seconds = pk_control_get_time_since_action_finish (control, res, &error);
834 g_assert_no_error (error);
835 g_assert_cmpint (seconds, !=, 0);
836
837 _g_test_loop_quit ();
838 }
839
840 static void
841 pk_test_control_can_authorize_cb (GObject *object, GAsyncResult *res, gpointer user_data)
842 {
843 PkControl *control = PK_CONTROL (object);
844 GError *error = NULL;
845 PkAuthorizeEnum auth;
846
847 /* get the result */
848 auth = pk_control_can_authorize_finish (control, res, &error);
849 g_assert_no_error (error);
850 g_assert_cmpint (auth, !=, PK_AUTHORIZE_ENUM_UNKNOWN);
851
852 _g_test_loop_quit ();
853 }
854
855 static void
856 pk_test_control_func (void)
857 {
858 PkControl *control;
859 guint version;
860 GError *error = NULL;
861 gboolean ret;
862 gchar *text;
863 PkBitfield roles;
864 guint i;
865 const guint LOOP_SIZE = 5;
866
867 /* get control */
868 control = pk_control_new ();
869 g_assert (control != NULL);
870
871 /* get TID async */
872 _refcount = 1;
873 pk_control_get_tid_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_tid_cb, NULL);
874 _g_test_loop_run_with_timeout (5000);
875 g_debug ("got tid in %f", g_test_timer_elapsed ());
876
877 /* get multiple TIDs async */
878 _refcount = LOOP_SIZE;
879 for (i = 0; i < _refcount; i++) {
880 g_debug ("getting #%i", i+1);
881 pk_control_get_tid_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_tid_cb, NULL);
882 }
883 _g_test_loop_run_with_timeout (5000);
884 g_debug ("got %i tids in %f", LOOP_SIZE, g_test_timer_elapsed ());
885
886 /* get properties async */
887 _refcount = 1;
888 pk_control_get_properties_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_properties_cb, NULL);
889 _g_test_loop_run_with_timeout (5000);
890 g_debug ("got properties types in %f", g_test_timer_elapsed ());
891
892 /* get properties async (again, to test caching) */
893 _refcount = 1;
894 pk_control_get_properties_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_properties_cb, NULL);
895 _g_test_loop_run_with_timeout (5000);
896 g_debug ("got properties in %f", g_test_timer_elapsed ());
897
898 /* do multiple requests async */
899 _refcount = LOOP_SIZE * 4;
900 for (i = 0; i < _refcount; i++) {
901 g_debug ("getting #%i", i+1);
902 pk_control_get_tid_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_tid_cb, NULL);
903 pk_control_get_properties_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_properties_cb, NULL);
904 pk_control_get_tid_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_tid_cb, NULL);
905 pk_control_get_properties_async (control, NULL, (GAsyncReadyCallback) pk_test_control_get_properties_cb, NULL);
906 }
907 _g_test_loop_run_with_timeout (5000);
908 g_debug ("got %i 2*properties and 2*tids in %f", LOOP_SIZE, g_test_timer_elapsed ());
909
910 /* get time since async */
911 pk_control_get_time_since_action_async (control, PK_ROLE_ENUM_GET_UPDATES, NULL, (GAsyncReadyCallback) pk_test_control_get_time_since_action_cb, NULL);
912 _g_test_loop_run_with_timeout (5000);
913 g_debug ("got get time since in %f", g_test_timer_elapsed ());
914
915 /* get auth state async */
916 pk_control_can_authorize_async (control, "org.freedesktop.packagekit.system-update", NULL,
917 (GAsyncReadyCallback) pk_test_control_can_authorize_cb, NULL);
918 _g_test_loop_run_with_timeout (5000);
919 g_debug ("get auth state in %f", g_test_timer_elapsed ());
920
921 /* version major */
922 g_object_get (control, "version-major", &version, NULL);
923 g_assert_cmpint (version, ==, PK_MAJOR_VERSION);
924
925 /* version minor */
926 g_object_get (control, "version-minor", &version, NULL);
927 g_assert_cmpint (version, ==, PK_MINOR_VERSION);
928
929 /* version micro */
930 g_object_get (control, "version-micro", &version, NULL);
931 g_assert_cmpint (version, ==, PK_MICRO_VERSION);
932
933 /* get properties sync */
934 ret = pk_control_get_properties (control, NULL, &error);
935 g_assert_no_error (error);
936 g_assert (ret);
937
938 /* get data */
939 g_object_get (control,
940 "roles", &roles,
941 NULL);
942
943 /* check data */
944 text = pk_role_bitfield_to_string (roles);
945 g_assert_cmpstr (text, ==, "cancel;depends-on;get-details;get-files;get-packages;get-repo-list;"
946 "required-by;get-update-detail;get-updates;install-files;install-packages;install-signature;"
947 "refresh-cache;remove-packages;repo-enable;repo-set-data;resolve;"
948 "search-details;search-file;search-group;search-name;update-packages;"
949 "what-provides;download-packages;get-distro-upgrades;"
950 "get-old-transactions;repair-system;get-details-local;"
951 "get-files-local;upgrade-system");
952 g_free (text);
953
954 g_object_unref (control);
955 }
956
957 static void
958 pk_test_package_sack_resolve_cb (GObject *object, GAsyncResult *res, gpointer user_data)
959 {
960 PkPackageSack *sack = PK_PACKAGE_SACK (object);
961 GError *error = NULL;
962 gboolean ret;
963
964 /* get the result */
965 ret = pk_package_sack_merge_generic_finish (sack, res, &error);
966 g_assert_no_error (error);
967 g_assert (ret);
968
969 _g_test_loop_quit ();
970 }
971
972 static void
973 pk_test_package_sack_details_cb (GObject *object, GAsyncResult *res, gpointer user_data)
974 {
975 PkPackageSack *sack = PK_PACKAGE_SACK (object);
976 GError *error = NULL;
977 gboolean ret;
978
979 /* get the result */
980 ret = pk_package_sack_merge_generic_finish (sack, res, &error);
981 g_assert_no_error (error);
982 g_assert (ret);
983
984 _g_test_loop_quit ();
985 }
986
987 static void
988 pk_test_package_sack_update_detail_cb (GObject *object, GAsyncResult *res, gpointer user_data)
989 {
990 PkPackageSack *sack = PK_PACKAGE_SACK (object);
991 GError *error = NULL;
992 gboolean ret;
993
994 /* get the result */
995 ret = pk_package_sack_merge_generic_finish (sack, res, &error);
996 g_assert_no_error (error);
997 g_assert (ret);
998
999 _g_test_loop_quit ();
1000 }
1001
1002 /*
1003 * pk_test_package_sack_filter_cb:
1004 **/
1005 static gboolean
1006 pk_test_package_sack_filter_cb (PkPackage *package, gpointer user_data)
1007 {
1008 if (pk_package_get_info (package) == PK_INFO_ENUM_UNKNOWN)
1009 return FALSE;
1010 return TRUE;
1011 }
1012
1013 static void
1014 pk_test_package_sack_func (void)
1015 {
1016 gboolean ret;
1017 PkPackageSack *sack;
1018 PkPackage *package;
1019 gchar *text;
1020 gchar **strv;
1021 guint size;
1022 PkInfoEnum info = PK_INFO_ENUM_UNKNOWN;
1023 guint64 bytes;
1024
1025 sack = pk_package_sack_new ();
1026 g_assert (sack != NULL);
1027
1028 /* get size of unused package sack */
1029 size = pk_package_sack_get_size (sack);
1030 g_assert (size == 0);
1031
1032 /* remove package not present */
1033 ret = pk_package_sack_remove_package_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1034 g_assert (!ret);
1035
1036 /* find package not present */
1037 package = pk_package_sack_find_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1038 g_assert (package == NULL);
1039
1040 /* add package */
1041 ret = pk_package_sack_add_package_by_id (sack, "powertop;1.8-1.fc8;i386;fedora", NULL);
1042 g_assert (ret);
1043
1044 /* get size of package sack */
1045 size = pk_package_sack_get_size (sack);
1046 g_assert (size == 1);
1047
1048 /* merge resolve results */
1049 pk_package_sack_resolve_async (sack, NULL, NULL, NULL, (GAsyncReadyCallback) pk_test_package_sack_resolve_cb, NULL);
1050 _g_test_loop_run_with_timeout (5000);
1051 g_debug ("resolved in %f", g_test_timer_elapsed ());
1052
1053 /* find package which is present */
1054 package = pk_package_sack_find_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1055 g_assert (package != NULL);
1056
1057 /* check new summary */
1058 g_object_get (package,
1059 "info", &info,
1060 "summary", &text,
1061 NULL);
1062 g_assert_cmpstr (text, ==, "Power consumption monitor");
1063
1064 /* check new info */
1065 g_assert_cmpint (info, ==, PK_INFO_ENUM_INSTALLED);
1066
1067 g_free (text);
1068 g_object_unref (package);
1069
1070 /* merge details results */
1071 pk_package_sack_get_details_async (sack, NULL, NULL, NULL, (GAsyncReadyCallback) pk_test_package_sack_details_cb, NULL);
1072 _g_test_loop_run_with_timeout (5000);
1073 g_debug ("got details in %f", g_test_timer_elapsed ());
1074
1075 /* find package which is present */
1076 package = pk_package_sack_find_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1077 g_assert (package != NULL);
1078
1079 /* check new url */
1080 g_object_get (package,
1081 "url", &text,
1082 NULL);
1083 g_assert_cmpstr (text, ==, "http://live.gnome.org/powertop");
1084 g_object_unref (package);
1085 g_free (text);
1086
1087 /* merge update detail results */
1088 pk_package_sack_get_update_detail_async (sack, NULL, NULL, NULL, (GAsyncReadyCallback) pk_test_package_sack_update_detail_cb, NULL);
1089 _g_test_loop_run_with_timeout (5000);
1090 g_debug ("got update detail in %f", g_test_timer_elapsed ());
1091
1092 /* find package which is present */
1093 package = pk_package_sack_find_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1094 g_assert (package != NULL);
1095
1096 /* check new vendor url */
1097 g_object_get (package,
1098 "update-vendor-urls", &strv,
1099 NULL);
1100 g_assert (strv != NULL);
1101 g_assert_cmpstr (strv[0], ==, "http://www.distro-update.org/page?moo");
1102 g_strfreev (strv);
1103
1104 g_object_unref (package);
1105
1106 /* chck size in bytes */
1107 bytes = pk_package_sack_get_total_bytes (sack);
1108 g_assert_cmpint (bytes, ==, 103424);
1109
1110 /* remove package */
1111 ret = pk_package_sack_remove_package_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1112 g_assert (ret);
1113
1114 /* get size of package sack */
1115 size = pk_package_sack_get_size (sack);
1116 g_assert_cmpint (size, ==, 0);
1117
1118 /* remove already removed package */
1119 ret = pk_package_sack_remove_package_by_id (sack, "powertop;1.8-1.fc8;i386;fedora");
1120 g_assert (!ret);
1121
1122 /* remove by filter */
1123 pk_package_sack_add_package_by_id (sack, "powertop;1.8-1.fc8;i386;fedora", NULL);
1124 pk_package_sack_add_package_by_id (sack, "powertop-debuginfo;1.8-1.fc8;i386;fedora", NULL);
1125 ret = pk_package_sack_remove_by_filter (sack, pk_test_package_sack_filter_cb, NULL);
1126 g_assert (ret);
1127
1128 /* check all removed */
1129 size = pk_package_sack_get_size (sack);
1130 g_assert_cmpint (size, ==, 0);
1131
1132 g_object_unref (sack);
1133 }
1134
1135 static void
1136 pk_test_task_install_packages_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1137 {
1138 PkTask *task = PK_TASK (object);
1139 g_autoptr(GError) error = NULL;
1140 g_autoptr(PkResults) results = NULL;
1141
1142 /* get the results */
1143 results = pk_task_generic_finish (task, res, &error);
1144 g_assert (results == NULL);
1145 g_assert_cmpstr (error->message, ==, "could not do untrusted question as no klass support");
1146
1147 _g_test_loop_quit ();
1148 }
1149
1150 static void
1151 pk_test_task_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data)
1152 {
1153 PkStatusEnum status;
1154 if (type == PK_PROGRESS_TYPE_STATUS) {
1155 g_object_get (progress,
1156 "status", &status,
1157 NULL);
1158 g_debug ("now %s", pk_status_enum_to_string (status));
1159 }
1160 }
1161
1162 static void
1163 pk_test_task_func (void)
1164 {
1165 PkTask *task;
1166 gchar **package_ids;
1167
1168 task = pk_task_new ();
1169 g_assert (task != NULL);
1170
1171 /* install package */
1172 package_ids = pk_package_ids_from_id ("glib2;2.14.0;i386;fedora");
1173 pk_task_install_packages_async (task, package_ids, NULL,
1174 (PkProgressCallback) pk_test_task_progress_cb, NULL,
1175 (GAsyncReadyCallback) pk_test_task_install_packages_cb, NULL);
1176 g_strfreev (package_ids);
1177 _g_test_loop_run_with_timeout (150000);
1178 g_debug ("installed in %f", g_test_timer_elapsed ());
1179
1180 g_object_unref (task);
1181 }
1182
1183 static void
1184 pk_test_task_text_install_packages_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1185 {
1186 PkTaskText *task = PK_TASK_TEXT (object);
1187 GError *error = NULL;
1188 PkResults *results;
1189 PkExitEnum exit_enum;
1190 GPtrArray *packages;
1191
1192 /* get the results */
1193 results = pk_task_generic_finish (PK_TASK (task), res, &error);
1194 g_assert_no_error (error);
1195 g_assert (results != NULL);
1196
1197 exit_enum = pk_results_get_exit_code (results);
1198 g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
1199
1200 packages = pk_results_get_package_array (results);
1201 g_assert (packages != NULL);
1202 g_assert_cmpint (packages->len, ==, 4);
1203
1204 g_ptr_array_unref (packages);
1205
1206 g_debug ("results exit enum = %s", pk_exit_enum_to_string (exit_enum));
1207
1208 if (results != NULL)
1209 g_object_unref (results);
1210 _g_test_loop_quit ();
1211 }
1212
1213 static void
1214 pk_test_task_text_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data)
1215 {
1216 PkStatusEnum status;
1217 if (type == PK_PROGRESS_TYPE_STATUS) {
1218 g_object_get (progress,
1219 "status", &status,
1220 NULL);
1221 g_debug ("now %s", pk_status_enum_to_string (status));
1222 }
1223 }
1224
1225 static void
1226 pk_test_task_text_func (void)
1227 {
1228 PkTaskText *task;
1229 gchar **package_ids;
1230
1231 task = pk_task_text_new ();
1232 g_assert (task != NULL);
1233
1234 /* For testing, you will need to manually do:
1235 pkcon repo-set-data dummy use-gpg 1
1236 pkcon repo-set-data dummy use-eula 1
1237 pkcon repo-set-data dummy use-media 1
1238 */
1239
1240 /* install package */
1241 package_ids = pk_package_ids_from_id ("vips-doc;7.12.4-2.fc8;noarch;linva");
1242 pk_task_install_packages_async (PK_TASK (task), package_ids, NULL,
1243 (PkProgressCallback) pk_test_task_text_progress_cb, NULL,
1244 (GAsyncReadyCallback) pk_test_task_text_install_packages_cb, NULL);
1245 g_strfreev (package_ids);
1246 _g_test_loop_run_with_timeout (150000);
1247 g_debug ("installed in %f", g_test_timer_elapsed ());
1248
1249 g_object_unref (task);
1250 }
1251
1252 static void
1253 pk_test_task_wrapper_install_packages_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1254 {
1255 PkTaskWrapper *task = PK_TASK_WRAPPER (object);
1256 GError *error = NULL;
1257 PkResults *results;
1258 PkExitEnum exit_enum;
1259 GPtrArray *packages;
1260
1261 /* get the results */
1262 results = pk_task_generic_finish (PK_TASK (task), res, &error);
1263 g_assert_no_error (error);
1264 g_assert (results != NULL);
1265
1266 exit_enum = pk_results_get_exit_code (results);
1267 g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
1268
1269 packages = pk_results_get_package_array (results);
1270 g_assert (packages != NULL);
1271 g_assert_cmpint (packages->len, ==, 4);
1272
1273 g_ptr_array_unref (packages);
1274
1275 g_debug ("results exit enum = %s", pk_exit_enum_to_string (exit_enum));
1276
1277 if (results != NULL)
1278 g_object_unref (results);
1279 _g_test_loop_quit ();
1280 }
1281
1282 static void
1283 pk_test_task_wrapper_progress_cb (PkProgress *progress, PkProgressType type, gpointer user_data)
1284 {
1285 PkStatusEnum status;
1286 if (type == PK_PROGRESS_TYPE_STATUS) {
1287 g_object_get (progress,
1288 "status", &status,
1289 NULL);
1290 g_debug ("now %s", pk_status_enum_to_string (status));
1291 }
1292 }
1293
1294 static void
1295 pk_test_task_wrapper_func (void)
1296 {
1297 PkTaskWrapper *task;
1298 gchar **package_ids;
1299
1300 task = pk_task_wrapper_new ();
1301 g_assert (task != NULL);
1302
1303 /* install package */
1304 package_ids = pk_package_ids_from_id ("vips-doc;7.12.4-2.fc8;noarch;linva");
1305 pk_task_install_packages_async (PK_TASK (task), package_ids, NULL,
1306 (PkProgressCallback) pk_test_task_wrapper_progress_cb, NULL,
1307 (GAsyncReadyCallback) pk_test_task_wrapper_install_packages_cb, NULL);
1308 g_strfreev (package_ids);
1309 _g_test_loop_run_with_timeout (150000);
1310 g_debug ("installed in %f", g_test_timer_elapsed ());
1311
1312 g_object_unref (task);
1313 }
1314
1315 guint _added = 0;
1316 guint _removed = 0;
1317 //guint _refcount = 0;
1318
1319 static void
1320 pk_test_transaction_list_resolve_cb (GObject *object, GAsyncResult *res, gpointer user_data)
1321 {
1322 PkClient *client = PK_CLIENT (object);
1323 GError *error = NULL;
1324 PkResults *results = NULL;
1325 PkExitEnum exit_enum;
1326
1327 /* get the results */
1328 results = pk_client_generic_finish (client, res, &error);
1329 g_assert_no_error (error);
1330 g_assert (results != NULL);
1331
1332 exit_enum = pk_results_get_exit_code (results);
1333 g_assert_cmpint (exit_enum, ==, PK_EXIT_ENUM_SUCCESS);
1334
1335 if (results != NULL)
1336 g_object_unref (results);
1337 if (--_refcount == 0)
1338 _g_test_loop_quit ();
1339 }
1340
1341 static void
1342 pk_test_transaction_list_added_cb (PkTransactionList *tlist, const gchar *tid, gpointer user_data)
1343 {
1344 g_debug ("added %s", tid);
1345 _added++;
1346 }
1347
1348 static void
1349 pk_test_transaction_list_removed_cb (PkTransactionList *tlist, const gchar *tid, gpointer user_data)
1350 {
1351 g_debug ("removed %s", tid);
1352 _removed++;
1353 }
1354
1355 static gboolean
1356 pk_transaction_list_delay_cb (gpointer user_data)
1357 {
1358 _g_test_loop_quit ();
1359 return FALSE;
1360 }
1361
1362 static void
1363 pk_test_transaction_list_func (void)
1364 {
1365 PkTransactionList *tlist;
1366 PkClient *client;
1367 gchar **package_ids;
1368
1369 /* get transaction_list object */
1370 tlist = pk_transaction_list_new ();
1371 g_assert (tlist != NULL);
1372 g_signal_connect (tlist, "added",
1373 G_CALLBACK (pk_test_transaction_list_added_cb), NULL);
1374 g_signal_connect (tlist, "removed",
1375 G_CALLBACK (pk_test_transaction_list_removed_cb), NULL);
1376
1377 /* get client */
1378 client = pk_client_new ();
1379 g_assert (client != NULL);
1380
1381 /* resolve package */
1382 package_ids = pk_package_ids_from_string ("glib2;2.14.0;i386;fedora&powertop");
1383 _refcount = 2;
1384 pk_client_resolve_async (client,
1385 pk_bitfield_value (PK_FILTER_ENUM_INSTALLED),
1386 package_ids,
1387 NULL,
1388 NULL,
1389 NULL,
1390 (GAsyncReadyCallback) pk_test_transaction_list_resolve_cb,
1391 NULL);
1392 pk_client_resolve_async (client,
1393 pk_bitfield_value (PK_FILTER_ENUM_NOT_INSTALLED),
1394 package_ids,
1395 NULL,
1396 NULL,
1397 NULL,
1398 (GAsyncReadyCallback) pk_test_transaction_list_resolve_cb,
1399 NULL);
1400 g_strfreev (package_ids);
1401 _g_test_loop_run_with_timeout (15000);
1402 g_debug ("resolved in %f", g_test_timer_elapsed ());
1403
1404 /* wait for remove */
1405 g_timeout_add (100, G_SOURCE_FUNC (pk_transaction_list_delay_cb), NULL);
1406 _g_test_loop_run_with_timeout (15000);
1407 g_debug ("resolved in %f", g_test_timer_elapsed ());
1408
1409 /* correct number of added signals */
1410 g_assert_cmpint (_added, ==, 2);
1411
1412 /* correct number of removed signals */
1413 g_assert_cmpint (_removed, ==, 2);
1414
1415 g_object_unref (tlist);
1416 g_object_unref (client);
1417 }
1418
1419 int
1420 main (int argc, char **argv)
1421 {
1422 g_test_init (&argc, &argv, NULL);
1423
1424 pk_debug_set_verbose (TRUE);
1425 pk_debug_add_log_domain (G_LOG_DOMAIN);
1426
1427 #ifndef PK_ENABLE_DAEMON_TESTS
1428 return 0;
1429 #endif
1430
1431 /* some libraries need to know */
1432 g_setenv ("PK_SELF_TEST", "1", TRUE);
1433
1434 /* tests go here */
1435 if(0) g_test_add_func ("/packagekit-glib2/offline", pk_test_offline_func);
1436 g_test_add_func ("/packagekit-glib2/control", pk_test_control_func);
1437 g_test_add_func ("/packagekit-glib2/transaction-list", pk_test_transaction_list_func);
1438 g_test_add_func ("/packagekit-glib2/client-helper", pk_test_client_helper_func);
1439 g_test_add_func ("/packagekit-glib2/client", pk_test_client_func);
1440 g_test_add_func ("/packagekit-glib2/package-sack", pk_test_package_sack_func);
1441 g_test_add_func ("/packagekit-glib2/task", pk_test_task_func);
1442 g_test_add_func ("/packagekit-glib2/task-wrapper", pk_test_task_wrapper_func);
1443 g_test_add_func ("/packagekit-glib2/task-text", pk_test_task_text_func);
1444 g_test_add_func ("/packagekit-glib2/console", pk_test_console_func);
1445
1446 return g_test_run ();
1447 }
1448