hythmbox-2.98/tests/test-utils.c

No issues found

  1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2  *
  3  *  This program is free software; you can redistribute it and/or modify
  4  *  it under the terms of the GNU General Public License as published by
  5  *  the Free Software Foundation; either version 2 of the License, or
  6  *  (at your option) any later version.
  7  *
  8  *  The Rhythmbox authors hereby grant permission for non-GPL compatible
  9  *  GStreamer plugins to be used and distributed together with GStreamer
 10  *  and Rhythmbox. This permission is above and beyond the permissions granted
 11  *  by the GPL license by which Rhythmbox is covered. If you modify this code
 12  *  you may extend this exception to your version of the code, but you are not
 13  *  obligated to do so. If you do not wish to do so, delete this exception
 14  *  statement from your version.
 15  *
 16  *  This program is distributed in the hope that it will be useful,
 17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19  *  GNU General Public License for more details.
 20  *
 21  *  You should have received a copy of the GNU General Public License
 22  *  along with this program; if not, write to the Free Software
 23  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 24  *
 25  */
 26 
 27 
 28 #include "config.h"
 29 
 30 #include <check.h>
 31 #include <gtk/gtk.h>
 32 
 33 #include "test-utils.h"
 34 #include "rhythmdb.h"
 35 #include "rhythmdb-tree.h"
 36 #include "rb-debug.h"
 37 #include "rb-util.h"
 38 
 39 static gboolean init_in_tests;
 40 static int argc_;
 41 static char **argv_;
 42 
 43 void
 44 init_once (gboolean test)
 45 {
 46 	if (test != init_in_tests)
 47 		return;
 48 
 49 	gtk_init (&argc_, &argv_);
 50 	GDK_THREADS_ENTER ();
 51 }
 52 
 53 void
 54 init_setup (SRunner *runner, int argc, char **argv)
 55 {
 56 	init_in_tests = (srunner_fork_status (runner) == CK_FORK);
 57 	argc_ = argc;
 58 	argv_ = argv;
 59 }
 60 
 61 void
 62 start_test_case (void)
 63 {
 64 	fprintf (stderr, "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
 65 }
 66 
 67 void
 68 end_step (void)
 69 {
 70 	while (gtk_events_pending ())
 71 		gtk_main_iteration_do (FALSE);
 72 	fprintf (stderr, "----------------------------------------------------------------\n");
 73 }
 74 
 75 void
 76 end_test_case (void)
 77 {
 78 	fprintf (stderr, "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n");
 79 }
 80 
 81 gboolean waiting, signaled;
 82 char *sig_name;
 83 gulong sig_handler;
 84 GObject *sig_object;
 85 
 86 static void
 87 mark_signal (void)
 88 {
 89 	g_signal_handler_disconnect (sig_object, sig_handler);
 90 	sig_object = NULL;
 91 	sig_handler = 0;
 92 
 93 	if (signaled) {
 94 		rb_debug ("got signal '%s' multiple times", sig_name);
 95 	} else {
 96 		rb_debug ("got signal '%s'", sig_name);
 97 		signaled = TRUE;
 98 		if (waiting)
 99 			gtk_main_quit ();
100 	}
101 }
102 
103 void
104 set_waiting_signal (GObject *o, const char *name)
105 {
106 	signaled = FALSE;
107 	waiting = FALSE;
108 	sig_name = g_strdup (name);
109 	sig_object = o;
110 	sig_handler = g_signal_connect (o, sig_name, G_CALLBACK (mark_signal), NULL);
111 }
112 
113 void
114 wait_for_signal (void)
115 {
116 	if (!signaled) {
117 		rb_debug ("waiting for signal '%s'", sig_name);
118 		waiting = TRUE;
119 		gtk_main ();
120 	} else {
121 		rb_debug ("no need to wait for signal '%s', already received", sig_name);
122 	}
123 
124 	g_free (sig_name);
125 	sig_name = NULL;
126 }
127 
128 
129 /* common setup and teardown */
130 RhythmDB *db = NULL;
131 gboolean waiting_db, finalised_db;
132 
133 void
134 test_rhythmdb_setup (void)
135 {
136 	RhythmDBEntryTypeClass *etype_class;
137 
138 	init_once (TRUE);
139 
140 	db = rhythmdb_tree_new ("test");
141 	fail_unless (db != NULL, "failed to initialise DB");
142 	rhythmdb_start_action_thread (db);
143 
144 	/* allow songs and ignored entries to be synced to for the tests */
145 	etype_class = RHYTHMDB_ENTRY_TYPE_GET_CLASS (RHYTHMDB_ENTRY_TYPE_SONG);
146 	etype_class->can_sync_metadata = (RhythmDBEntryTypeBooleanFunc)rb_true_function;
147 	etype_class->sync_metadata = (RhythmDBEntryTypeSyncFunc)rb_null_function;
148 
149 	etype_class = RHYTHMDB_ENTRY_TYPE_GET_CLASS (RHYTHMDB_ENTRY_TYPE_IGNORE);
150 	etype_class->can_sync_metadata = (RhythmDBEntryTypeBooleanFunc)rb_true_function;
151 	etype_class->sync_metadata = (RhythmDBEntryTypeSyncFunc)rb_null_function;
152 }
153 
154 void
155 test_rhythmdb_shutdown (void)
156 {
157 	fail_unless (db != NULL, "failed to shutdown DB");
158 	rhythmdb_shutdown (db);
159 
160 	/* release the reference, and wait until after finalisation */
161 	g_object_weak_ref (G_OBJECT (db), (GWeakNotify)gtk_main_quit, NULL);
162 	g_idle_add ((GSourceFunc)g_object_unref, db);
163 	gtk_main ();
164 	db = NULL;
165 }
166 
167 void
168 set_entry_string (RhythmDB *db, RhythmDBEntry *entry, RhythmDBPropType prop, const char *value)
169 {
170 	GValue v = {0,};
171 
172 	g_value_init (&v, G_TYPE_STRING);
173 	g_value_set_string (&v, value);
174 	rhythmdb_entry_set (db, entry, prop, &v);
175 	g_value_unset (&v);
176 }
177 
178 void
179 set_entry_ulong (RhythmDB *db, RhythmDBEntry *entry, RhythmDBPropType prop, gulong value)
180 {
181 	GValue v = {0,};
182 
183 	g_value_init (&v, G_TYPE_ULONG);
184 	g_value_set_ulong (&v, value);
185 	rhythmdb_entry_set (db, entry, prop, &v);
186 	g_value_unset (&v);
187 }
188 
189 void
190 set_entry_hidden (RhythmDB *db, RhythmDBEntry *entry, gboolean hidden)
191 {
192 	GValue v = {0,};
193 
194 	g_value_init (&v, G_TYPE_BOOLEAN);
195 	g_value_set_boolean (&v, hidden);
196 	rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_HIDDEN, &v);
197 	g_value_unset (&v);
198 }