Commit af43a834 authored by Ivan Frade's avatar Ivan Frade

tests/libtracker-extract: unit tests for tracker-iptc

parent fd2ec123
......@@ -230,6 +230,8 @@ parse_iptc (const unsigned char *buffer,
#ifndef TRACKER_DISABLE_DEPRECATED
// LCOV_EXCL_START
/**
* tracker_iptc_read:
* @buffer: a chunk of data with iptc data in it.
......@@ -262,6 +264,8 @@ tracker_iptc_read (const unsigned char *buffer,
return parse_iptc (buffer, len, uri, data);
}
// LCOV_EXCL_STOP
#endif /* TRACKER_DISABLE_DEPRECATED */
/**
......
......@@ -9,6 +9,11 @@ TEST_PROGS += \
tracker-exif-test \
tracker-guarantee-test
if HAVE_IPTC
# This test also requires libjpeg...
TEST_PROGS += tracker-iptc-test
endif
if HAVE_ENCA
TEST_PROGS += tracker-encoding
else
......@@ -46,6 +51,10 @@ tracker_exif_test_SOURCES = tracker-exif-test.c
tracker_guarantee_test_SOURCES = tracker-guarantee-test.c
tracker_iptc_test_SOURCES = tracker-iptc-test.c
tracker_iptc_test_LDADD = $(LDADD) $(LIBJPEG_LIBS)
tracker_iptc_test_CFLAGS = $(LIBJPEG_CFLAGS)
EXTRA_DIST = encoding-detect.bin \
areas.xmp \
areas-with-contacts.xmp \
......@@ -55,4 +64,5 @@ EXTRA_DIST = encoding-detect.bin \
exif-img.jpg \
exif-free-img.jpg \
guarantee-mtime-test.txt \
getline-test.txt
getline-test.txt \
iptc-img.jpg
/*
* Copyright (C) 2011, Nokia <ivan.frade@nokia.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser 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 <stdio.h>
#include <string.h>
#include <setjmp.h>
#include <jpeglib.h>
#include <glib.h>
#include <gio/gio.h>
#include <libtracker-extract/tracker-iptc.h>
#define PS3_NAMESPACE "Photoshop 3.0\0"
#define PS3_NAMESPACE_LENGTH 14
#include <libiptcdata/iptc-jpeg.h>
struct tej_error_mgr {
struct jpeg_error_mgr jpeg;
jmp_buf setjmp_buffer;
};
static void
extract_jpeg_error_exit (j_common_ptr cinfo)
{
struct tej_error_mgr *h = (struct tej_error_mgr *) cinfo->err;
(*cinfo->err->output_message)(cinfo);
longjmp (h->setjmp_buffer, 1);
}
/*
* libiptcdata doesn't scan the file until find the IPTC blob.
* We need to find the blob ourselves. This code comes from tracker-extract-jpeg
*/
TrackerIptcData *
load_iptc_blob (const gchar *filename)
{
struct jpeg_decompress_struct cinfo;
struct tej_error_mgr tejerr;
struct jpeg_marker_struct *marker;
TrackerIptcData *id = NULL;
FILE *f;
gchar *uri;
GFile *file;
file = g_file_new_for_path (filename);
uri = g_file_get_uri (file);
g_object_unref (file);
f = fopen (filename, "r");
cinfo.err = jpeg_std_error (&tejerr.jpeg);
tejerr.jpeg.error_exit = extract_jpeg_error_exit;
if (setjmp (tejerr.setjmp_buffer)) {
fclose (f);
g_free (uri);
return NULL;
}
jpeg_create_decompress (&cinfo);
jpeg_save_markers (&cinfo, JPEG_COM, 0xFFFF);
jpeg_save_markers (&cinfo, JPEG_APP0 + 1, 0xFFFF);
jpeg_save_markers (&cinfo, JPEG_APP0 + 13, 0xFFFF);
jpeg_stdio_src (&cinfo, f);
jpeg_read_header (&cinfo, TRUE);
marker = (struct jpeg_marker_struct *) &cinfo.marker_list;
while (marker) {
gchar *str;
gsize len;
gint offset;
guint sublen;
switch (marker->marker) {
case JPEG_COM:
break;
case JPEG_APP0 + 1:
break;
case JPEG_APP0 + 13:
str = (gchar*) marker->data;
len = marker->data_length;
if (len > 0 && strncmp (PS3_NAMESPACE, str, PS3_NAMESPACE_LENGTH) == 0) {
offset = iptc_jpeg_ps3_find_iptc ((guchar *)str, len, &sublen);
if (offset > 0 && sublen > 0) {
id = tracker_iptc_new ((const guchar *)str + offset, sublen, uri);
}
}
break;
default:
marker = marker->next;
continue;
}
marker = marker->next;
}
g_free (uri);
fclose (f);
return id;
}
void
test_iptc_extraction ()
{
TrackerIptcData *data;
data = load_iptc_blob ("./iptc-img.jpg");
g_assert (data);
g_assert_cmpstr (data->keywords, ==, "Coverage, test");
g_assert (g_str_has_prefix (data->date_created, "2011-10-22"));
g_assert_cmpstr (data->byline, ==, "BylineValue");
g_assert_cmpstr (data->byline_title, ==, "BylineTitleValue");
g_assert_cmpstr (data->credit, ==, "CreditValue");
g_assert_cmpstr (data->copyright_notice, ==, "IptcToolAuthors");
g_assert_cmpstr (data->image_orientation, ==, "nfo:orientation-left");
g_assert_cmpstr (data->city, ==, "Helsinki");
g_assert_cmpstr (data->state, ==, "N/A");
g_assert_cmpstr (data->sublocation, ==, "Ruoholahti");
g_assert_cmpstr (data->country_name, ==, "Finland");
g_assert_cmpstr (data->contact, ==, "Dilbert");
tracker_iptc_free (data);
}
int
main (int argc, char **argv)
{
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/libtracker-extract/iptc/extraction",
test_iptc_extraction);
return g_test_run ();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment