summaryrefslogtreecommitdiff
path: root/tests/libtracker-extract/tracker-test-xmp.c
blob: e46d1fdc639f57387cc65dbda617b765c71b0172 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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
156
157
158
159
160
161
162
163
164
165
166
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright (C) 2010, Nokia <ivan.frade@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#include "config.h"

#include <glib-object.h>

#include <libtracker-extract/tracker-extract.h>

#define BROKEN_XMP "This is not even XML"
#define EXAMPLE_XMP   \
"   <x:xmpmeta   " \
"      xmlns:x=\'adobe:ns:meta/\'" \
"      xmlns:dc=\"http://purl.org/dc/elements/1.1/\"" \
"      xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"" \
"      xmlns:exif=\"http://ns.adobe.com/exif/1.0/\">" \
"     <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" \
"        <rdf:Description rdf:about=\"\">"\
"         <dc:format>application/pdf</dc:format>" \
"         <dc:title>Title of the content</dc:title>"   \
"         <dc:rights>CC share alike</dc:rights> " \
"         <dc:description>Description of the content</dc:description>" \
"         <dc:date>2010-03-18T15:17:04Z</dc:date>" \
"         <dc:keywords>test, data, xmp</dc:keywords>" \
"         <dc:subject>Subject of the content</dc:subject>" \
"         <dc:publisher>A honest developer</dc:publisher>" \
"         <dc:contributor>A honest contributor</dc:contributor>" \
"         <dc:type>PhysicalObject</dc:type>" \
"         <dc:identifier>12345</dc:identifier>" \
"         <dc:source>My dirty mind</dc:source>" \
"         <dc:language>Spanglish</dc:language>" \
"         <dc:relation>Single</dc:relation>" \
"         <dc:coverage>Pretty high after this test</dc:coverage>" \
"         <dc:creator>The ultimate creator</dc:creator>" \
"         <exif:Title>Title in exif</exif:Title>" \
"         <exif:DateTimeOriginal>2010-03-18T15:17:04Z</exif:DateTimeOriginal>" \
"         <exif:Artist>Artist in exif</exif:Artist>" \
"         <exif:Make>Make in exif</exif:Make>" \
"         <exif:Model>Model in exif</exif:Model>" \
"         <exif:Orientation>top - left</exif:Orientation>" \
"         <exif:Flash>0</exif:Flash>" \
"         <exif:MeteringMode>3</exif:MeteringMode>" \
"         <exif:ExposureTime>1000</exif:ExposureTime>" \
"         <exif:FNumber>12</exif:FNumber>" \
"         <exif:FocalLength>50</exif:FocalLength>" \
"         <exif:ISOSpeedRatings>400</exif:ISOSpeedRatings>" \
"         <exif:WhiteBalance>1</exif:WhiteBalance>" \
"         <exif:Copyright>Copyright in exif</exif:Copyright>" \
"         <xmp:CreateDate>2002-08-15T17:10:04Z</xmp:CreateDate>"\
"        </rdf:Description> " \
"     </rdf:RDF> " \
"   </x:xmpmeta>"

#define METERING_MODE_XMP \
"   <x:xmpmeta   "                            \
"      xmlns:x=\'adobe:ns:meta/\'"                              \
"      xmlns:exif=\"http://ns.adobe.com/exif/1.0/\">"           \
"     <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" \
"        <rdf:Description rdf:about=\"\">"                      \
"         <exif:MeteringMode>%d</exif:MeteringMode>"            \
"        </rdf:Description>"                                    \
"     </rdf:RDF></x:xmpmeta> "

#define ORIENTATION_XMP \
"   <x:xmpmeta   "                            \
"      xmlns:x=\'adobe:ns:meta/\'"                              \
"      xmlns:exif=\"http://ns.adobe.com/exif/1.0/\">"           \
"     <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">" \
"        <rdf:Description rdf:about=\"\">"                      \
"         <exif:Orientation>%s</exif:Orientation>"              \
"        </rdf:Description>"                                    \
"     </rdf:RDF></x:xmpmeta> "

typedef struct {
	const gchar *exif_value;
	const gchar *nepomuk_translation;
} ExifNepomuk;

static ExifNepomuk METERING_MODES [] = {
	{"0",  "nmm:metering-mode-other"},
	{"1", "nmm:metering-mode-average"},
	{"2", "nmm:metering-mode-center-weighted-average"},
	{"3", "nmm:metering-mode-spot"},
	{"4", "nmm:metering-mode-multispot"},
	{"5", "nmm:metering-mode-pattern"},
	{"6", "nmm:metering-mode-partial"},
	{NULL, NULL}
};

static ExifNepomuk ORIENTATIONS [] = {
	{"top - right", "nfo:orientation-top-mirror"},
	{"bottom - right", "nfo:orientation-bottom-mirror"},
	{"bottom - left", "nfo:orientation-bottom"},
	{"left - top", "nfo:orientation-left-mirror"},
	{"right - top", "nfo:orientation-right"},
	{"right - bottom", "nfo:orientation-right-mirror"},
	{"left - bottom", "nfo:orientation-left"},
	{"invalid value", "nfo:orientation-top"}
};

static TrackerXmpData *
get_example_expected (void)
{
	TrackerXmpData *data;

	data = g_new0 (TrackerXmpData, 1);

	/* NS_DC */
	data->title = g_strdup ("Title of the content");
	data->rights = g_strdup ("CC share alike");
	data->creator = g_strdup ("The ultimate creator");
	data->description = g_strdup ("Description of the content");
	data->date = g_strdup ("2010-03-18T15:17:04Z");
	data->keywords = g_strdup ("test, data, xmp");
	data->subject = g_strdup ("Subject of the content");

	data->publisher = g_strdup ("A honest developer");     /* publisher */
	data->contributor = g_strdup ("A honest contributor");
	data->type = NULL ;
	data->format = g_strdup ("application/pdf");
	data->identifier = g_strdup ("12345");
	data->source = g_strdup ("My dirty mind");
	data->language = g_strdup ("Spanglish");
	data->relation = g_strdup ("Single");
	data->coverage = g_strdup ("Pretty high after this test");

	/* NS_CC */
	data->license = NULL;

	/* NS_PDF */
	data->pdf_title = NULL;
	data->pdf_keywords = NULL;

	/* NS_EXIF*/
	data->title2 = g_strdup ("Title in exif");
	data->time_original = g_strdup ("2010-03-18T15:17:04Z");
	data->artist = g_strdup ("Artist in exif");
	data->make = g_strdup ("Make in exif");
	data->model = g_strdup ("Model in exif");
	data->orientation = g_strdup ("nfo:orientation-top");
	data->flash = g_strdup ("nmm:flash-off");
	data->metering_mode = g_strdup ("nmm:metering-mode-spot");
	data->exposure_time = g_strdup ("1000");                      /* exposure time */
	data->fnumber = g_strdup ("12");                              /* fnumber */
	data->focal_length = g_strdup ("50");                         /* focal length */

	data->iso_speed_ratings = g_strdup ("400");                   /* iso speed rating */
	data->white_balance = g_strdup ("nmm:white-balance-manual");
	data->copyright = g_strdup ("Copyright in exif");
	
	/* NS_XAP */
	data->rating = NULL;

	/* NS_IPTC4XMP */
	/* NS_PHOTOSHOP */
	data->address = NULL;                /* address */
	data->country = NULL;                /* country */
	data->state = NULL;                  /* state */
	data->city = NULL;                   /* city */
	
	return data;
};

static void
test_parsing_xmp (void)
{
	TrackerXmpData *data;
	TrackerXmpData *expected;

	if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDERR)) {
		data = tracker_xmp_new (BROKEN_XMP, strlen (BROKEN_XMP), "test://file");
		g_assert (data != NULL);

		tracker_xmp_free (data);
	}

	g_test_trap_assert_stderr ("*parsing failure*");

	data = tracker_xmp_new (EXAMPLE_XMP, strlen (EXAMPLE_XMP), "test://file");
	expected = get_example_expected ();

	/* NS_DC */
	g_assert_cmpstr (data->format, ==, expected->format);
	g_assert_cmpstr (data->title, ==, expected->title);
	g_assert_cmpstr (data->rights, ==, expected->rights);
	g_assert_cmpstr (data->description, ==, expected->description);
	g_assert_cmpstr (data->date, ==, expected->date);
	g_assert_cmpstr (data->keywords, ==, expected->keywords);
	g_assert_cmpstr (data->subject, ==, expected->subject);
	g_assert_cmpstr (data->publisher, ==, expected->publisher);
	g_assert_cmpstr (data->contributor, ==, expected->contributor);
	g_assert_cmpstr (data->identifier, ==, expected->identifier);
	g_assert_cmpstr (data->source, ==, expected->source);
	g_assert_cmpstr (data->language, ==, expected->language);
	g_assert_cmpstr (data->relation, ==, expected->relation);
	g_assert_cmpstr (data->coverage, ==, expected->coverage);
	g_assert_cmpstr (data->creator, ==, expected->creator);

	/* NS_EXIF*/
	g_assert_cmpstr (data->title2, ==, expected->title2);
	g_assert_cmpstr (data->time_original, ==, expected->time_original);
	g_assert_cmpstr (data->artist, ==, expected->artist);
	g_assert_cmpstr (data->make, ==, expected->make);
	g_assert_cmpstr (data->model, ==, expected->model);
	g_assert_cmpstr (data->orientation, ==, expected->orientation);
	g_assert_cmpstr (data->flash, ==, expected->flash);
	g_assert_cmpstr (data->metering_mode, ==, expected->metering_mode);
	g_assert_cmpstr (data->exposure_time, ==, expected->exposure_time);
	g_assert_cmpstr (data->fnumber, ==, expected->fnumber);
	g_assert_cmpstr (data->focal_length, ==, expected->focal_length);

	g_assert_cmpstr (data->iso_speed_ratings, ==, expected->iso_speed_ratings);
	g_assert_cmpstr (data->white_balance, ==, expected->white_balance);
	g_assert_cmpstr (data->copyright, ==, expected->copyright);

	tracker_xmp_free (expected);
	tracker_xmp_free (data);
}

static void
test_xmp_metering_mode (void)
{
	gint i;

	for (i = 0; METERING_MODES[i].exif_value != NULL; i++) {
		TrackerXmpData *data;
		gchar *xmp;

		xmp = g_strdup_printf (METERING_MODE_XMP, i);
		data = tracker_xmp_new (xmp, strlen (xmp), "local://file");
		g_free (xmp);

		g_assert_cmpstr (data->metering_mode, ==, METERING_MODES[i].nepomuk_translation);
		tracker_xmp_free (data);
	}
}

static void
test_xmp_orientation (void)
{
	gint i;

	for (i = 0; i < G_N_ELEMENTS (ORIENTATIONS); i++) {
		TrackerXmpData *data;
		gchar *xmp;

		xmp = g_strdup_printf (ORIENTATION_XMP, ORIENTATIONS[i].exif_value);
		data = tracker_xmp_new (xmp, strlen (xmp), "local://file");
		g_free (xmp);

		g_assert_cmpstr (data->orientation, ==, ORIENTATIONS[i].nepomuk_translation);
		tracker_xmp_free (data);
	}
}

static void
test_xmp_apply (void)
{
	TrackerSparqlBuilder *metadata, *preupdate;
	TrackerXmpData *data;

	metadata = tracker_sparql_builder_new_update ();
	preupdate = tracker_sparql_builder_new_update ();

	data = tracker_xmp_new (EXAMPLE_XMP, strlen (EXAMPLE_XMP), "urn:uuid:test");
	g_assert (data != NULL);

	tracker_sparql_builder_insert_open (metadata, NULL);
	tracker_sparql_builder_subject_iri (metadata, "urn:uuid:test");

	g_assert (tracker_xmp_apply (preupdate, metadata, "urn:uuid:test", data));

	tracker_sparql_builder_insert_close (metadata);

	/* This is the only way to check the sparql is kinda correct */

	/* Disabled this for 0.8.5. It was reporting 41 not 50, this
	 * test is not credible and I can't see how it can be trusted
	 * as a method for making sure the query is correct.
	 *
	 * -mr
	 */

	/* g_assert_cmpint (tracker_sparql_builder_get_length (metadata), ==, 50); */
}

static void
test_xmp_apply_location (void)
{
	TrackerXmpData data = { 0, };
	TrackerSparqlBuilder *metadata, *preupdate;

	data.address = g_strdup ("Itamerenkatu 11-13");
	data.city = g_strdup ("Helsinki");
	data.state = g_strdup ("N/A");
	data.country = g_strdup ("Findland");

	metadata = tracker_sparql_builder_new_update ();
	preupdate = tracker_sparql_builder_new_update ();

	tracker_sparql_builder_insert_open (metadata, NULL);
	tracker_sparql_builder_subject_iri (metadata, "urn:uuid:test");

	g_assert (tracker_xmp_apply (preupdate, metadata, "urn:uuid:test", &data));

	tracker_sparql_builder_insert_close (metadata);

	/* This is the only way to check the sparql is kinda correct */

	/* The builder just contains this:
	   <urn:uuid:test> slo:location [ a slo:GeoLocation ;
		 slo:postalAddress <urn:uuid:c50c4305-c617-4188-b3d3-42ba2291d0de>] .
	} */

	g_assert_cmpint (tracker_sparql_builder_get_length (metadata), >=, 3);
}

int
main (int    argc,
      char **argv)
{
	gint result;

	g_type_init ();
	g_thread_init (NULL);
	g_test_init (&argc, &argv, NULL);

	g_test_message ("Testing XMP");

#ifdef HAVE_EXEMPI
	g_test_add_func ("/libtracker-extract/tracker-xmp/parsing_xmp",
	                 test_parsing_xmp);

	g_test_add_func ("/libtracker-extract/tracker-xmp/metering-mode",
	                 test_xmp_metering_mode);

	g_test_add_func ("/libtracker-extract/tracker-xmp/orientation",
	                 test_xmp_orientation);

	g_test_add_func ("/libtracker-extract/tracker-xmp/sparql_translation",
	                 test_xmp_apply);

#endif

	g_test_add_func ("/libtracker-extract/tracker-xmp/sparql_translation_location",
	                 test_xmp_apply_location);

	result = g_test_run ();

	return result;
}