From 9b6d8e63db85c30007b463e91f91a791969fa83f Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 16:36:24 +0200 Subject: Adding upstream version 1:3.38.4. Signed-off-by: Daniel Baumann --- tests/common/hostnames-test.txt | 11 + tests/common/meson.build | 22 ++ tests/common/ssids-test.txt | 3 + tests/common/test-hostname.c | 123 ++++++++++ tests/common/test-time-entry.c | 483 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 642 insertions(+) create mode 100644 tests/common/hostnames-test.txt create mode 100644 tests/common/meson.build create mode 100644 tests/common/ssids-test.txt create mode 100644 tests/common/test-hostname.c create mode 100644 tests/common/test-time-entry.c (limited to 'tests/common') diff --git a/tests/common/hostnames-test.txt b/tests/common/hostnames-test.txt new file mode 100644 index 0000000..5a31ce2 --- /dev/null +++ b/tests/common/hostnames-test.txt @@ -0,0 +1,11 @@ +# Pretty hostname, tab, display hostname, tab, real hostname +Lennart's PC Lennarts-PC lennarts-pc +Müllers Computer Mullers-Computer mullers-computer +Voran! Voran voran +Es war einmal ein Männlein Es-war-einmal-ein-Mannlein es-war-einmal-ein-mannlein +Jawoll. Ist doch wahr! Jawoll-Ist-doch-wahr jawoll-ist-doch-wahr +レナート localhost localhost +!!! localhost localhost +...zack!!! zack!... zack-zack zack-zack +Bãstien's computer... Foo-bar Bastiens-computer-Foo-bar bastiens-computer-foo-bar + localhost localhost diff --git a/tests/common/meson.build b/tests/common/meson.build new file mode 100644 index 0000000..0550092 --- /dev/null +++ b/tests/common/meson.build @@ -0,0 +1,22 @@ + + +test_units = [ + 'test-hostname', + # 'test-time-entry', # FIXME +] + +cflags = [ + '-DTEST_SRCDIR="@0@"'.format(meson.current_source_dir()), + '-DTEST_TOPSRCDIR="@0@"'.format(meson.source_root()) +] + +foreach unit: test_units + exe = executable( + unit, + unit + '.c', + include_directories : [ top_inc, common_inc ], + dependencies : common_deps + [libwidgets_dep], + c_args : cflags, + ) + test(unit, exe) +endforeach diff --git a/tests/common/ssids-test.txt b/tests/common/ssids-test.txt new file mode 100644 index 0000000..0545437 --- /dev/null +++ b/tests/common/ssids-test.txt @@ -0,0 +1,3 @@ +GNOME GNOME +0123456789abcdefghijklmnopqrstuvwxyz 0123456789abcdefghijklmnopqrstuv +レナート レナート diff --git a/tests/common/test-hostname.c b/tests/common/test-hostname.c new file mode 100644 index 0000000..0e3a3ae --- /dev/null +++ b/tests/common/test-hostname.c @@ -0,0 +1,123 @@ +#include "config.h" + +#include +#include +#include + +#include "hostname-helper.h" + +static void +test_hostname (void) +{ + g_autofree gchar *contents = NULL; + guint i; + g_auto(GStrv) lines = NULL; + + if (g_file_get_contents (TEST_SRCDIR "/hostnames-test.txt", &contents, NULL, NULL) == FALSE) { + g_warning ("Failed to load '%s'", TEST_SRCDIR "/hostnames-test.txt"); + g_test_fail (); + return; + } + + 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++) { + g_auto(GStrv) items = NULL; + g_autofree gchar *utf8 = NULL; + g_autofree gchar *result1 = NULL; + g_autofree gchar *result2 = NULL; + + if (*lines[i] == '#') + continue; + if (*lines[i] == '\0') + break; + + items = g_strsplit (lines[i], "\t", -1); + utf8 = g_locale_from_utf8 (items[0], -1, NULL, NULL, NULL); + + result1 = pretty_hostname_to_static (items[0], FALSE); + if (g_strcmp0 (result1, items[2]) != 0) { + g_error ("Result for '%s' doesn't match '%s' (got: '%s')", + utf8, items[2], result1); + g_test_fail (); + } else { + g_debug ("Result for '%s' matches '%s'", + utf8, result1); + } + + result2 = pretty_hostname_to_static (items[0], TRUE); + if (g_strcmp0 (result2, items[1]) != 0) { + g_error ("Result for '%s' doesn't match '%s' (got: '%s')", + utf8, items[1], result2); + g_test_fail (); + } else { + g_debug ("Result for '%s' matches '%s'", + utf8, result2); + } + } +} + +static void +test_ssid (void) +{ + g_autofree gchar *contents = NULL; + guint i; + g_auto(GStrv) lines = NULL; + + if (g_file_get_contents (TEST_SRCDIR "/ssids-test.txt", &contents, NULL, NULL) == FALSE) { + g_warning ("Failed to load '%s'", TEST_SRCDIR "/ssids-test.txt"); + g_test_fail (); + return; + } + + 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++) { + g_autofree gchar *ssid = NULL; + g_auto(GStrv) items = NULL; + + if (*lines[i] == '#') + continue; + if (*lines[i] == '\0') + break; + + items = g_strsplit (lines[i], "\t", -1); + ssid = pretty_hostname_to_ssid (items[0]); + g_assert_cmpstr (ssid, ==, items[1]); + } +} + +int main (int argc, char **argv) +{ + char *locale; + + /* 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); + + g_test_add_func ("/common/hostname", test_hostname); + g_test_add_func ("/common/ssid", test_ssid); + + return g_test_run (); +} diff --git a/tests/common/test-time-entry.c b/tests/common/test-time-entry.c new file mode 100644 index 0000000..e099efa --- /dev/null +++ b/tests/common/test-time-entry.c @@ -0,0 +1,483 @@ +#include +#include +/* #include "cc-datetime-resources.h" */ + +#undef NDEBUG +#undef G_DISABLE_ASSERT +#undef G_DISABLE_CHECKS +#undef G_DISABLE_CAST_CHECKS +#undef G_LOG_DOMAIN + +#include "cc-time-entry.c" + +static void +test_time (CcTimeEntry *time_entry, + guint hour, + guint minute, + gboolean is_24h) +{ + g_autofree char *str = NULL; + const char *entry_str; + guint entry_hour, entry_minute; + + g_assert_true (CC_IS_TIME_ENTRY (time_entry)); + g_assert_cmpint (hour, >=, 0); + g_assert_cmpint (hour, <= , 23); + g_assert_cmpint (minute, >=, 0); + g_assert_cmpint (minute, <= , 59); + + entry_hour = cc_time_entry_get_hour (time_entry); + g_assert_cmpint (entry_hour, ==, hour); + + entry_minute = cc_time_entry_get_minute (time_entry); + g_assert_cmpint (entry_minute, ==, minute); + + /* Convert 24 hour time to 12 hour */ + if (!is_24h) + { + /* 00:00 is 12:00 AM */ + if (hour == 0) + hour = 12; + else if (hour > 12) + hour = hour - 12; + } + + str = g_strdup_printf ("%02d:%02d", hour, minute); + entry_str = gtk_entry_get_text (GTK_ENTRY (time_entry)); + g_assert_cmpstr (entry_str, ==, str); +} + +static void +test_time_24h (void) +{ + GtkWidget *entry; + CcTimeEntry *time_entry; + + entry = cc_time_entry_new (); + time_entry = CC_TIME_ENTRY (entry); + g_object_ref_sink (entry); + g_assert (CC_IS_TIME_ENTRY (entry)); + + for (guint i = 0; i <= 25; i++) + { + guint hour; + gboolean is_am; + + cc_time_entry_set_time (time_entry, i, 0); + g_assert_false (cc_time_entry_get_am_pm (time_entry)); + + test_time (time_entry, i < 24 ? i : 23, 0, TRUE); + + hour = cc_time_entry_get_hour (time_entry); + is_am = cc_time_entry_get_is_am (time_entry); + if (hour < 12) + g_assert_true (is_am); + else + g_assert_false (is_am); + } + + g_object_unref (entry); +} + +static void +test_time_12h (void) +{ + GtkWidget *entry; + CcTimeEntry *time_entry; + + entry = cc_time_entry_new (); + time_entry = CC_TIME_ENTRY (entry); + g_object_ref_sink (entry); + g_assert (CC_IS_TIME_ENTRY (entry)); + + cc_time_entry_set_am_pm (time_entry, FALSE); + g_assert_false (cc_time_entry_get_am_pm (time_entry)); + cc_time_entry_set_am_pm (time_entry, TRUE); + g_assert_true (cc_time_entry_get_am_pm (time_entry)); + + for (guint i = 0; i <= 25; i++) + { + guint hour; + gboolean is_am; + + cc_time_entry_set_time (time_entry, i, 0); + + test_time (time_entry, i < 24 ? i : 23, 0, FALSE); + + hour = cc_time_entry_get_hour (time_entry); + is_am = cc_time_entry_get_is_am (time_entry); + + if (hour < 12) + g_assert_true (is_am); + else + g_assert_false (is_am); + } + + g_object_unref (entry); +} + +static void +test_time_hour_24h (void) +{ + GtkWidget *entry; + CcTimeEntry *time_entry; + int hour; + + entry = cc_time_entry_new (); + time_entry = CC_TIME_ENTRY (entry); + g_assert (CC_IS_TIME_ENTRY (entry)); + + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_set_position (GTK_EDITABLE (entry), 0); + + for (guint i = 1; i <= 25; i++) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_UP); + + /* Wrap if above limit */ + hour = i; + if (hour >= 24) + hour = hour - 24; + + test_time (time_entry, hour, 0, TRUE); + } + + cc_time_entry_set_time (time_entry, 0, 0); + hour = 0; + + for (int i = 25; i >= 0; i--) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_DOWN); + + /* Wrap if below limit */ + hour--; + if (hour < 0) + hour = 23; + + test_time (time_entry, hour, 0, TRUE); + } + + /* Put cursor at the one’s place and repeat the tests */ + gtk_editable_set_position (GTK_EDITABLE (entry), 1); + cc_time_entry_set_time (time_entry, 0, 0); + + for (guint i = 1; i <= 25; i++) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_UP); + + /* Wrap if above limit */ + hour = i; + if (hour >= 24) + hour = hour - 24; + + test_time (time_entry, hour, 0, TRUE); + } + + cc_time_entry_set_time (time_entry, 0, 0); + hour = 0; + + for (int i = 25; i >= 0; i--) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_DOWN); + + /* Wrap if below limit */ + hour--; + if (hour < 0) + hour = 23; + + test_time (time_entry, hour, 0, TRUE); + } + + g_object_ref_sink (entry); + g_object_unref (entry); +} + +static void +test_time_minute_24h (void) +{ + GtkWidget *entry; + CcTimeEntry *time_entry; + int minute; + + entry = cc_time_entry_new (); + time_entry = CC_TIME_ENTRY (entry); + g_assert (CC_IS_TIME_ENTRY (entry)); + + cc_time_entry_set_time (time_entry, 0, 0); + /* Set cursor at 10’s place of minute */ + gtk_editable_set_position (GTK_EDITABLE (entry), SEPARATOR_INDEX + 1); + + for (guint i = 1; i <= 61; i++) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_UP); + + /* Wrap if above limit */ + minute = i; + if (minute >= 60) + minute = minute - 60; + + test_time (time_entry, 0, minute, TRUE); + } + + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_set_position (GTK_EDITABLE (entry), SEPARATOR_INDEX + 1); + minute = 0; + + for (int i = 61; i >= 0; i--) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_DOWN); + + /* Wrap if below limit */ + minute--; + if (minute < 0) + minute = 59; + + test_time (time_entry, 0, minute, TRUE); + } + + /* Put cursor at the minute one’s place and repeat the tests */ + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_set_position (GTK_EDITABLE (entry), SEPARATOR_INDEX + 2); + + for (guint i = 1; i <= 61; i++) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_UP); + + /* Wrap if above limit */ + minute = i; + if (minute >= 60) + minute = minute - 60; + + test_time (time_entry, 0, minute, TRUE); + } + + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_set_position (GTK_EDITABLE (entry), SEPARATOR_INDEX + 2); + minute = 0; + + for (int i = 61; i >= 0; i--) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_DOWN); + + /* Wrap if below limit */ + minute--; + if (minute < 0) + minute = 59; + + test_time (time_entry, 0, minute, TRUE); + } + + g_object_ref_sink (entry); + g_object_unref (entry); +} + +static void +test_time_hour_12h (void) +{ + GtkWidget *entry; + CcTimeEntry *time_entry; + int hour; + + entry = cc_time_entry_new (); + time_entry = CC_TIME_ENTRY (entry); + g_assert (CC_IS_TIME_ENTRY (entry)); + + cc_time_entry_set_am_pm (time_entry, TRUE); + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_set_position (GTK_EDITABLE (entry), 0); + + for (guint i = 1; i <= 14; i++) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_UP); + + /* Wrap if above limit */ + hour = i; + if (hour >= 12) + hour = hour - 12; + + test_time (time_entry, hour, 0, FALSE); + } + + cc_time_entry_set_time (time_entry, 0, 0); + hour = 12; + + for (int i = 23; i >= 0; i--) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_DOWN); + + /* Wrap if below limit */ + hour--; + if (hour < 0) + hour = 11; /* Hour varies from 0 to 11 (0 is 12) */ + + test_time (time_entry, hour, 0, FALSE); + } + + cc_time_entry_set_time (time_entry, 0, 0); + /* Put cursor at the one’s place and repeat the tests */ + gtk_editable_set_position (GTK_EDITABLE (entry), 1); + + for (guint i = 1; i <= 14; i++) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_UP); + + /* Wrap if above limit */ + hour = i; + if (hour >= 12) + hour = hour - 12; + + test_time (time_entry, hour, 0, FALSE); + } + + cc_time_entry_set_time (time_entry, 0, 0); + hour = 0; + + for (int i = 23; i >= 0; i--) + { + g_signal_emit_by_name (entry, "change-value", GTK_SCROLL_STEP_DOWN); + + /* Wrap if below limit */ + hour--; + if (hour < 0) + hour = 11; /* Hour varies from 0 to 11 (0 is 12) */ + + test_time (time_entry, hour, 0, FALSE); + } + + g_object_ref_sink (entry); + g_object_unref (entry); +} + +static void +test_time_insertion (void) +{ + GtkWidget *entry; + CcTimeEntry *time_entry; + int position; + + entry = cc_time_entry_new (); + time_entry = CC_TIME_ENTRY (entry); + g_assert (CC_IS_TIME_ENTRY (entry)); + + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_set_position (GTK_EDITABLE (entry), 0); + + /* Test hour */ + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = 0; + /* 00:00 */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "0", -1, &position); + test_time (time_entry, 0, 0, TRUE); + + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = 0; + /* 20:00 */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "2", -1, &position); + test_time (time_entry, 20, 0, TRUE); + + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = 0; + /* 30:00 => 23:00 */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "3", -1, &position); + test_time (time_entry, 23, 0, TRUE); + + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = 1; + /* 09:00 */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "9", -1, &position); + test_time (time_entry, 9, 0, TRUE); + + /* Test minute */ + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = SEPARATOR_INDEX + 1; + /* 00:10 */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "1", -1, &position); + test_time (time_entry, 0, 10, TRUE); + + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = SEPARATOR_INDEX + 1; + /* 00:30 */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "3", -1, &position); + test_time (time_entry, 0, 30, TRUE); + + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = SEPARATOR_INDEX + 1; + /* 00:60 => 00:59 */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "6", -1, &position); + test_time (time_entry, 0, 59, TRUE); + + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = SEPARATOR_INDEX + 2; + /* 00:00 */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "0", -1, &position); + test_time (time_entry, 0, 0, TRUE); + + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = SEPARATOR_INDEX + 2; + /* 00:01 */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "1", -1, &position); + test_time (time_entry, 0, 1, TRUE); + + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = SEPARATOR_INDEX + 2; + /* 00:09 */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "9", -1, &position); + test_time (time_entry, 0, 9, TRUE); + + /* 12 Hour mode */ + cc_time_entry_set_am_pm (time_entry, TRUE); + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = 0; + /* 12:00 AM */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "1", -1, &position); + /* 12 AM Hour in 12H mode is 00 Hour in 24H mode */ + test_time (time_entry, 0, 0, FALSE); + + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = 1; + /* 11:00 AM */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "1", -1, &position); + test_time (time_entry, 11, 0, FALSE); + + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = 3; + /* 12:10 AM */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "1", -1, &position); + test_time (time_entry, 0, 10, FALSE); + + cc_time_entry_set_time (time_entry, 0, 0); + gtk_editable_delete_text (GTK_EDITABLE (entry), 0, 1); + position = 4; + /* 12:09 AM */ + gtk_editable_insert_text (GTK_EDITABLE (entry), "9", -1, &position); + test_time (time_entry, 0, 9, FALSE); + + g_object_ref_sink (entry); + g_object_unref (entry); +} + +int +main (int argc, + char **argv) +{ + setlocale (LC_ALL, ""); + g_test_init (&argc, &argv, NULL); + gtk_init (NULL, NULL); + + g_setenv ("G_DEBUG", "fatal_warnings", FALSE); + + g_test_add_func ("/datetime/time-24h", test_time_24h); + g_test_add_func ("/datetime/time-12h", test_time_12h); + g_test_add_func ("/datetime/time-hour-24h", test_time_hour_24h); + g_test_add_func ("/datetime/time-minute-24h", test_time_minute_24h); + g_test_add_func ("/datetime/time-hour-12h", test_time_hour_12h); + g_test_add_func ("/datetime/time-insertion", test_time_insertion); + + return g_test_run (); +} -- cgit v1.2.3