summaryrefslogtreecommitdiffstats
path: root/tests/printers
diff options
context:
space:
mode:
Diffstat (limited to 'tests/printers')
-rw-r--r--tests/printers/canonicalization-test.txt16
-rw-r--r--tests/printers/meson.build22
-rw-r--r--tests/printers/shift-test.txt9
-rw-r--r--tests/printers/test-canonicalization.c119
-rw-r--r--tests/printers/test-shift.c95
5 files changed, 261 insertions, 0 deletions
diff --git a/tests/printers/canonicalization-test.txt b/tests/printers/canonicalization-test.txt
new file mode 100644
index 0000000..10ceee1
--- /dev/null
+++ b/tests/printers/canonicalization-test.txt
@@ -0,0 +1,16 @@
+# already_present_printer1, space, already_present_printer2, ...., tab,
+# new device id, tab,
+# new device make and model, tab,
+# new device original name, tab,
+# new device info, tab,
+# canonicalized name
+# - stripped leading and trailing spaces
+# - characters not in ALLOWED_CHARACTERS replaced by '-'
+# - everything behind strings in residues removed
+# - leading and trailing '-' removed
+# - recurring '-' merged
+# - indexed if the name is already in any of given lists
+Canon-BJ-30 Canon-BJ-30-2 Canon BJ-30 - CUPS+Gutenprint v5.2.9 Simplified Canon-BJ-30-3
+Canon-BJ-30 HP Business Inkjet 1000, hpcups 3.14.6 HP-Business-Inkjet-1000
+ ...A---strange;;printer ;;;;info A-strange-printer-info
+HP-Something MFG:HP;MDL:Officejet 7300 series; HP Officejet 7300 series Officejet 7300 series Officejet-7300
diff --git a/tests/printers/meson.build b/tests/printers/meson.build
new file mode 100644
index 0000000..60f144f
--- /dev/null
+++ b/tests/printers/meson.build
@@ -0,0 +1,22 @@
+
+test_units = [
+ #'test-canonicalization',
+ 'test-shift'
+]
+
+includes = [top_inc, include_directories('../../panels/printers')]
+cflags = '-DTEST_SRCDIR="@0@"'.format(meson.current_source_dir())
+
+foreach unit: test_units
+ exe = executable(
+ unit,
+ [unit + '.c'],
+ include_directories : includes,
+ dependencies : common_deps,
+ link_with : [printers_panel_lib],
+ c_args : cflags
+ )
+
+ test(unit, exe)
+endforeach
+
diff --git a/tests/printers/shift-test.txt b/tests/printers/shift-test.txt
new file mode 100644
index 0000000..b9eced4
--- /dev/null
+++ b/tests/printers/shift-test.txt
@@ -0,0 +1,9 @@
+# String, tab, string shifted by 1 position left
+Canon anon
+-JetDirect JetDirect
+localhost ocalhost
+Šípková Růženka ípková Růženka
+ space space
+[][ ][
+…... ...
+
diff --git a/tests/printers/test-canonicalization.c b/tests/printers/test-canonicalization.c
new file mode 100644
index 0000000..d555660
--- /dev/null
+++ b/tests/printers/test-canonicalization.c
@@ -0,0 +1,119 @@
+#include "config.h"
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <locale.h>
+
+#include "pp-utils.h"
+
+static void
+test_canonicalization (gconstpointer data)
+{
+ const char *contents = data;
+ guint i, j;
+ char **lines;
+
+ lines = g_strsplit (contents, "\n", -1);
+ if (lines == NULL)
+ {
+ g_warning ("Test file is empty");
+ g_test_fail ();
+ return;
+ }
+
+ for (i = 0; lines[i] != NULL; i++)
+ {
+ char **items;
+
+ if (*lines[i] == '#')
+ continue;
+
+ if (*lines[i] == '\0')
+ break;
+
+ items = g_strsplit (lines[i], "\t", -1);
+ if (g_strv_length (items) == 6)
+ {
+ PpPrintDevice *device;
+ gchar **already_present_printers;
+ gchar *canonicalized_name;
+ GList *devices = NULL;
+
+ already_present_printers = g_strsplit (items[0], " ", -1);
+
+ for (j = 0; already_present_printers[j] != NULL; j++)
+ devices = g_list_append (devices, g_strdup (already_present_printers[j]));
+
+ device = g_object_new (PP_TYPE_PRINT_DEVICE,
+ "device-id", items[1],
+ "device-make-and-model", items[2],
+ "device-original-name", items[3],
+ "device-info", items[4],
+ NULL);
+
+ canonicalized_name =
+ canonicalize_device_name (devices, NULL, NULL, 0, device);
+
+ if (g_strcmp0 (canonicalized_name, items[5]) != 0)
+ {
+ g_error ("Result for ('%s', '%s', '%s', '%s') doesn't match '%s' (got: '%s')",
+ items[1], items[2], items[3], items[4], items[5], canonicalized_name);
+ g_test_fail ();
+ }
+ else
+ {
+ g_debug ("Result for ('%s', '%s', '%s', '%s') matches '%s'",
+ items[1], items[2], items[3], items[4], canonicalized_name);
+ }
+
+ g_free (canonicalized_name);
+ g_object_unref (device);
+ g_list_free_full (devices, (GDestroyNotify) g_free);
+ g_strfreev (already_present_printers);
+ }
+ else
+ {
+ g_warning ("Line number %u has not correct number of items!", i);
+ g_test_fail ();
+ }
+
+ g_strfreev (items);
+ }
+
+ g_strfreev (lines);
+}
+
+int
+main (int argc, char **argv)
+{
+ char *locale;
+ char *contents;
+
+ /* Running in some locales will
+ * break the tests as "ü" will be transliterated to
+ * "ue" in de_DE, and 'u"' in the C locale.
+ *
+ * Work around that by forcing en_US with UTF-8 in
+ * our tests
+ * https://bugzilla.gnome.org/show_bug.cgi?id=650342 */
+
+ locale = setlocale (LC_ALL, "en_US.UTF-8");
+ if (locale == NULL)
+ {
+ g_debug("Missing en_US.UTF-8 locale, ignoring test.");
+ return 0;
+ }
+
+ bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+ g_test_init (&argc, &argv, NULL);
+
+ if (g_file_get_contents (TEST_SRCDIR "/canonicalization-test.txt", &contents, NULL, NULL) == FALSE)
+ {
+ g_warning ("Failed to load '%s'", TEST_SRCDIR "/canonicalization-test.txt");
+ return 1;
+ }
+
+ g_test_add_data_func ("/printers/canonicalization", contents, test_canonicalization);
+
+ return g_test_run ();
+}
diff --git a/tests/printers/test-shift.c b/tests/printers/test-shift.c
new file mode 100644
index 0000000..e85fe9a
--- /dev/null
+++ b/tests/printers/test-shift.c
@@ -0,0 +1,95 @@
+#include "config.h"
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <locale.h>
+
+#include "pp-utils.h"
+
+static void
+test_shift (gconstpointer data)
+{
+ const char *contents = data;
+ guint i;
+ char *str;
+ char **lines;
+
+ lines = g_strsplit (contents, "\n", -1);
+ if (lines == NULL)
+ {
+ g_warning ("Test file is empty");
+ g_test_fail ();
+ return;
+ }
+
+ for (i = 0; lines[i] != NULL; i++)
+ {
+ char **items;
+ char *utf8;
+
+ if (*lines[i] == '#')
+ continue;
+
+ if (*lines[i] == '\0')
+ break;
+
+ items = g_strsplit (lines[i], "\t", -1);
+ str = g_strdup (items[0]);
+ shift_string_left (str);
+ utf8 = g_locale_from_utf8 (items[0], -1, NULL, NULL, NULL);
+ if (g_strcmp0 (str, items[1]) != 0)
+ {
+ g_error ("Result for '%s' doesn't match '%s' (got: '%s')",
+ utf8, items[1], str);
+ g_test_fail ();
+ }
+ else
+ {
+ g_debug ("Result for '%s' matches '%s'",
+ utf8, str);
+ }
+
+ g_free (str);
+ g_free (utf8);
+
+ g_strfreev (items);
+ }
+
+ g_strfreev (lines);
+
+}
+
+int
+main (int argc, char **argv)
+{
+ char *locale;
+ char *contents;
+
+ /* Running in some locales will
+ * break the tests as "ü" will be transliterated to
+ * "ue" in de_DE, and 'u"' in the C locale.
+ *
+ * Work around that by forcing en_US with UTF-8 in
+ * our tests
+ * https://bugzilla.gnome.org/show_bug.cgi?id=650342 */
+
+ locale = setlocale (LC_ALL, "en_US.UTF-8");
+ if (locale == NULL)
+ {
+ g_debug("Missing en_US.UTF-8 locale, ignoring test.");
+ return 0;
+ }
+
+ bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
+ g_test_init (&argc, &argv, NULL);
+
+ if (g_file_get_contents (TEST_SRCDIR "/shift-test.txt", &contents, NULL, NULL) == FALSE)
+ {
+ g_warning ("Failed to load '%s'", TEST_SRCDIR "/shift-test.txt");
+ return 1;
+ }
+
+ g_test_add_data_func ("/printers/shift", contents, test_shift);
+
+ return g_test_run ();
+}