xmpwriter.c 5.23 KB
Newer Older
Thiago Santos's avatar
Thiago Santos committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/* GStreamer
 *
 * unit tests for xmp config library
 *
 * Copyright (C) 2011 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/check/gstcheck.h>

#include <gst/tag/tag.h>
#include <gst/tag/xmpwriter.h>

#include <string.h>

#define TEST_ELEMENT_TYPE (test_element_get_type())

typedef struct TestElement TestElement;
typedef struct TestElementClass TestElementClass;

struct TestElement
{
  GstElement parent;
};

struct TestElementClass
{
  GstElementClass parent_class;
};

GType test_element_get_type (void);

static void init_interface (GType type);

53
54
G_DEFINE_TYPE_WITH_CODE (TestElement, test_element, GST_TYPE_ELEMENT,
    init_interface (g_define_type_id));
Thiago Santos's avatar
Thiago Santos committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

static void
init_interface (GType type)
{
  static const GInterfaceInfo tagxmpwriter_info = {
    NULL,
    NULL,
    NULL,
  };

  g_type_add_interface_static (type, GST_TYPE_TAG_XMP_WRITER,
      &tagxmpwriter_info);
}

static void
test_element_class_init (TestElementClass * klass)
{
}

static void
75
test_element_init (TestElement * this)
Thiago Santos's avatar
Thiago Santos committed
76
77
78
79
80
81
{
}

static gboolean
gst_buffer_equals (GstBuffer * buf_a, GstBuffer * buf_b)
{
Wim Taymans's avatar
Wim Taymans committed
82
  gboolean res;
Wim Taymans's avatar
Wim Taymans committed
83
  GstMapInfo map1, map2;
Thiago Santos's avatar
Thiago Santos committed
84

Wim Taymans's avatar
Wim Taymans committed
85
86
  gst_buffer_map (buf_a, &map1, GST_MAP_READ);
  gst_buffer_map (buf_b, &map2, GST_MAP_READ);
Wim Taymans's avatar
Wim Taymans committed
87

Wim Taymans's avatar
Wim Taymans committed
88
89
  if (map1.size == map2.size) {
    res = memcmp (map1.data, map2.data, map1.size) == 0;
Wim Taymans's avatar
Wim Taymans committed
90
91
92
  } else {
    res = FALSE;
  }
Wim Taymans's avatar
Wim Taymans committed
93
94
  gst_buffer_unmap (buf_a, &map1);
  gst_buffer_unmap (buf_b, &map2);
Wim Taymans's avatar
Wim Taymans committed
95
96

  return res;
Thiago Santos's avatar
Thiago Santos committed
97
98
99
100
101
}

static GstTagList *
create_taglist (void)
{
102
  return gst_tag_list_new (GST_TAG_ARTIST, "artist",
Thiago Santos's avatar
Thiago Santos committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
      GST_TAG_TITLE, "title", GST_TAG_COPYRIGHT, "copyright", NULL);
}

GST_START_TEST (test_no_xmp)
{
  GstTagList *taglist = create_taglist ();
  GstElement *test_element =
      (GstElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);

  gst_tag_xmp_writer_remove_all_schemas (GST_TAG_XMP_WRITER (test_element));

  fail_unless (gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER
          (test_element), taglist, TRUE) == NULL);

  gst_object_unref (test_element);
  gst_tag_list_free (taglist);
}

GST_END_TEST;


GST_START_TEST (test_default)
{
  GstTagList *taglist = create_taglist ();
  GstElement *test_element =
      (GstElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);
  GstBuffer *buf;
  GstBuffer *buf2;

  buf =
      gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER
      (test_element), taglist, TRUE);
  buf2 = gst_tag_list_to_xmp_buffer (taglist, TRUE);
  fail_unless (gst_buffer_equals (buf, buf2));

  gst_object_unref (test_element);
  gst_buffer_unref (buf);
  gst_buffer_unref (buf2);
  gst_tag_list_free (taglist);
}

GST_END_TEST;


GST_START_TEST (test_disable)
{
  GstTagList *taglist;
  GstTagList *taglist2;
  GstElement *test_element =
      (GstElement *) g_object_new (TEST_ELEMENT_TYPE, NULL);
  GstBuffer *buf;
  const gchar *str;

156
  taglist = gst_tag_list_new (GST_TAG_ARTIST, "artist", NULL);
Thiago Santos's avatar
Thiago Santos committed
157
158
159
160
161
162
163
164
165

  /* add a tag that is mapped on xmp schema (as of Mar, 21th 2011) */
  gst_tag_list_add (taglist, GST_TAG_MERGE_REPLACE, GST_TAG_USER_RATING, 5,
      NULL);

  buf =
      gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER
      (test_element), taglist, TRUE);
  taglist2 = gst_tag_list_from_xmp_buffer (buf);
166
  fail_unless (gst_tag_list_is_equal (taglist, taglist2));
Thiago Santos's avatar
Thiago Santos committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
  gst_tag_list_free (taglist2);
  gst_buffer_unref (buf);

  gst_tag_xmp_writer_remove_schema (GST_TAG_XMP_WRITER (test_element), "xap");
  buf =
      gst_tag_xmp_writer_tag_list_to_xmp_buffer (GST_TAG_XMP_WRITER
      (test_element), taglist, TRUE);
  taglist2 = gst_tag_list_from_xmp_buffer (buf);

  /* artist should be there, but rating shouldn't */
  fail_unless (gst_tag_list_peek_string_index (taglist2, GST_TAG_ARTIST, 0,
          &str));
  fail_unless (gst_tag_list_get_value_index (taglist2, GST_TAG_USER_RATING,
          0) == NULL);

  gst_tag_list_free (taglist2);
  gst_buffer_unref (buf);

  gst_object_unref (test_element);
  gst_tag_list_free (taglist);
}

GST_END_TEST;


static Suite *
xmp_config_suite (void)
{
  Suite *s = suite_create ("xmpconfig interface");
  TCase *tc_chain = tcase_create ("configuration");

  suite_add_tcase (s, tc_chain);
  tcase_add_test (tc_chain, test_no_xmp);
  tcase_add_test (tc_chain, test_default);
  tcase_add_test (tc_chain, test_disable);

  return s;
}

int
main (int argc, char **argv)
{
  int nf;

  Suite *s = xmp_config_suite ();
  SRunner *sr = srunner_create (s);

  gst_check_init (&argc, &argv);

  srunner_run_all (sr, CK_NORMAL);
  nf = srunner_ntests_failed (sr);
  srunner_free (sr);

  return nf;
}