~ubuntu-branches/ubuntu/precise/rhythmbox/precise-201203091205

« back to all changes in this revision

Viewing changes to tests/test-rhythmdb-tree-serialization.c

Tags: upstream-0.9.5
ImportĀ upstreamĀ versionĀ 0.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 *  arch-tag: Serialization tests for the RhythmDB tree database
 
3
 *
 
4
 *  Copyright (C) 2003 Colin Walters <walters@verbum.org>
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 
19
 */
 
20
 
 
21
#include <glib.h>
 
22
#include <gtk/gtk.h>
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <libxml/tree.h>
 
26
#include "rhythmdb-tree.h"
 
27
#include "rb-debug.h"
 
28
#include "rb-thread-helpers.h"
 
29
 
 
30
static RhythmDBEntry *
 
31
create_entry (RhythmDB *db, const char *name, const char *album,
 
32
              const char *artist, const char *genre)
 
33
{
 
34
        RhythmDBEntry *entry;
 
35
        GValue val = {0, };
 
36
 
 
37
        entry = rhythmdb_entry_new (db, RHYTHMDB_ENTRY_TYPE_SONG, "file:///yay.ogg");
 
38
        g_assert (entry);
 
39
        g_value_init (&val, G_TYPE_STRING);
 
40
        g_value_set_static_string (&val, genre);
 
41
        rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_GENRE, &val);
 
42
        g_value_unset (&val);
 
43
 
 
44
        g_value_init (&val, G_TYPE_STRING);
 
45
        g_value_set_static_string (&val, artist);
 
46
        rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_ARTIST, &val);
 
47
        g_value_unset (&val);
 
48
 
 
49
        g_value_init (&val, G_TYPE_STRING);
 
50
        g_value_set_static_string (&val, album);
 
51
        rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_ALBUM, &val);
 
52
        g_value_unset (&val);
 
53
 
 
54
        g_value_init (&val, G_TYPE_STRING);
 
55
        g_value_set_static_string (&val, name);
 
56
        rhythmdb_entry_set (db, entry, RHYTHMDB_PROP_TITLE, &val);
 
57
        g_value_unset (&val);
 
58
 
 
59
        return entry;
 
60
}
 
61
 
 
62
static void
 
63
assert_xml_file_has_entry (const char *entry)
 
64
{
 
65
        xmlDocPtr doc;
 
66
        xmlNodePtr root, child;
 
67
        
 
68
        doc = xmlParseFile ("test.xml");
 
69
        g_assert (doc != NULL);
 
70
 
 
71
        root = xmlDocGetRootElement (doc);
 
72
        
 
73
        child = root->children;
 
74
        for (; child != NULL; child = child->next) {
 
75
                xmlNodePtr sub_child;
 
76
 
 
77
                if (child->type != XML_ELEMENT_NODE)
 
78
                        continue;
 
79
                
 
80
                for (sub_child = child->children; sub_child; sub_child = sub_child->next) {
 
81
                        if (sub_child->type != XML_ELEMENT_NODE)
 
82
                                continue;
 
83
 
 
84
                        if (!strcmp (sub_child->name, "title")) {
 
85
                                if (!strcmp (sub_child->children->content,
 
86
                                             entry))
 
87
                                        goto out;
 
88
                        }
 
89
                }
 
90
        }
 
91
        
 
92
        g_assert_not_reached ();
 
93
        out:
 
94
        xmlFreeDoc (doc);
 
95
}
 
96
 
 
97
 
 
98
int
 
99
main (int argc, char **argv)
 
100
{
 
101
        RhythmDB *db;
 
102
        RhythmDBEntry *entry;
 
103
        xmlDocPtr doc;
 
104
 
 
105
        gtk_init (&argc, &argv);
 
106
        g_thread_init (NULL);
 
107
        gdk_threads_init ();
 
108
        rb_thread_helpers_init ();
 
109
        rb_debug_init (TRUE);
 
110
 
 
111
        GDK_THREADS_ENTER ();
 
112
 
 
113
        db = rhythmdb_tree_new ("test.xml");
 
114
 
 
115
        /**
 
116
         *  TEST 1: Save with no entries
 
117
         */
 
118
        g_print ("Test 1\n");
 
119
        rhythmdb_read_lock (db);
 
120
 
 
121
        rhythmdb_save (db);
 
122
 
 
123
        doc = xmlParseFile ("test.xml");
 
124
        g_assert (doc != NULL);
 
125
        xmlFreeDoc (doc);
 
126
 
 
127
        rhythmdb_read_unlock (db);
 
128
        g_print ("Test 1: PASS\n");
 
129
 
 
130
        /**
 
131
         *  TEST 2: Save with a single entry
 
132
         */
 
133
        g_print ("Test 1\n");
 
134
        rhythmdb_write_lock (db);
 
135
 
 
136
        entry = create_entry (db, "Sin", "Pretty Hate Machine", "Nine Inch Nails", "Rock");
 
137
 
 
138
        rhythmdb_save (db);
 
139
 
 
140
        assert_xml_file_has_entry ("Sin");
 
141
 
 
142
        rhythmdb_write_unlock (db);
 
143
 
 
144
        /**
 
145
         * THE END
 
146
         */
 
147
        rhythmdb_shutdown (db);
 
148
        g_object_unref (G_OBJECT (db));
 
149
        GDK_THREADS_LEAVE ();
 
150
        
 
151
        exit (0);
 
152
}