Merge branch 'w32-contenttype' into 'main'

Various contenttype-related test fixes on win32

See merge request GNOME/glib!2499
This commit is contained in:
Philip Withnall 2022-03-17 15:14:14 +00:00
commit 9b77b75f2d
2 changed files with 51 additions and 20 deletions

View File

@ -128,7 +128,8 @@ g_content_type_is_a (const gchar *type,
const gchar *supertype)
{
gboolean res;
char *value_utf8;
char *perceived_type;
char *perceived_supertype;
g_return_val_if_fail (type != NULL, FALSE);
g_return_val_if_fail (supertype != NULL, FALSE);
@ -136,12 +137,15 @@ g_content_type_is_a (const gchar *type,
if (g_content_type_equals (type, supertype))
return TRUE;
res = FALSE;
value_utf8 = get_registry_classes_key (type, L"PerceivedType");
if (value_utf8 && strcmp (value_utf8, supertype) == 0)
res = TRUE;
g_free (value_utf8);
perceived_type = get_registry_classes_key (type, L"PerceivedType");
perceived_supertype = get_registry_classes_key (supertype, L"PerceivedType");
res = perceived_type && perceived_supertype &&
strcmp (perceived_type, perceived_supertype) == 0;
g_free (perceived_type);
g_free (perceived_supertype);
return res;
}
@ -342,7 +346,8 @@ g_content_type_from_mime_type (const gchar *mime_type)
content_type = get_registry_classes_key (key, L"Extension");
g_free (key);
return content_type;
return content_type ? g_steal_pointer (&content_type) : g_strdup ("*");
}
gchar *
@ -354,6 +359,7 @@ g_content_type_guess (const gchar *filename,
char *basename;
char *type;
char *dot;
size_t i;
type = NULL;
@ -366,11 +372,21 @@ g_content_type_guess (const gchar *filename,
if (filename)
{
basename = g_path_get_basename (filename);
dot = strrchr (basename, '.');
if (dot)
type = g_strdup (dot);
g_free (basename);
i = strlen (filename);
if (i > 0 && filename[i - 1] == G_DIR_SEPARATOR)
{
type = g_strdup ("inode/directory");
if (result_uncertain)
*result_uncertain = TRUE;
}
else
{
basename = g_path_get_basename (filename);
dot = strrchr (basename, '.');
if (dot)
type = g_strdup (dot);
g_free (basename);
}
}
if (type)

View File

@ -30,7 +30,7 @@ test_guess (void)
existing_directory = (gchar *) g_getenv ("SYSTEMROOT");
if (existing_directory)
existing_directory = g_strdup_printf ("%s/", existing_directory);
existing_directory = g_strdup_printf ("%s" G_DIR_SEPARATOR_S, existing_directory);
#else
existing_directory = g_strdup ("/etc/");
#endif
@ -56,7 +56,8 @@ test_guess (void)
g_free (res);
g_free (expected);
/* Sadly OSX just doesn't have as large and robust of a mime type database as Linux */
/* Sadly win32 & OSX just don't have as large and robust of a mime type database as Linux */
#ifndef G_OS_WIN32
#ifndef __APPLE__
res = g_content_type_guess ("foo", data, sizeof (data) - 1, &uncertain);
expected = g_content_type_from_mime_type ("text/plain");
@ -110,7 +111,7 @@ test_guess (void)
g_assert_false (uncertain);
g_free (res);
g_free (expected);
#endif
#endif /* __APPLE__ */
res = g_content_type_guess (NULL, (guchar *)"%!PS-Adobe-2.0 EPSF-1.2", 23, &uncertain);
expected = g_content_type_from_mime_type ("image/x-eps");
@ -126,6 +127,7 @@ test_guess (void)
g_assert_false (uncertain);
g_free (res);
g_free (expected);
#endif /* G_OS_WIN32 */
}
static void
@ -200,6 +202,13 @@ test_executable (void)
{
gchar *type;
#ifdef G_OS_WIN32
type = g_content_type_from_mime_type ("application/vnd.microsoft.portable-executable");
/* FIXME: the MIME is not in the default `MIME\Database\Content Type` registry.
* g_assert_true (g_content_type_can_be_executable (type));
*/
g_free (type);
#else
type = g_content_type_from_mime_type ("application/x-executable");
g_assert_true (g_content_type_can_be_executable (type));
g_free (type);
@ -207,7 +216,7 @@ test_executable (void)
type = g_content_type_from_mime_type ("text/plain");
g_assert_true (g_content_type_can_be_executable (type));
g_free (type);
#endif
type = g_content_type_from_mime_type ("image/png");
g_assert_false (g_content_type_can_be_executable (type));
g_free (type);
@ -244,7 +253,9 @@ test_icon (void)
#ifdef __APPLE__
g_assert_true (g_strv_contains (names, "text-*"));
#else
#ifndef G_OS_WIN32
g_assert_true (g_strv_contains (names, "text-plain"));
#endif
g_assert_true (g_strv_contains (names, "text-x-generic"));
#endif
}
@ -259,9 +270,13 @@ test_icon (void)
const gchar *const *names;
names = g_themed_icon_get_names (G_THEMED_ICON (icon));
#ifdef G_OS_WIN32
g_assert_true (g_strv_contains (names, "text-x-generic"));
#else
g_assert_true (g_strv_contains (names, "application-rtf"));
#ifndef __APPLE__
g_assert_true (g_strv_contains (names, "x-office-document"));
#endif
#endif
}
g_object_unref (icon);
@ -329,8 +344,8 @@ test_tree (void)
gchar **types;
gsize i;
#ifdef __APPLE__
g_test_skip ("The OSX backend does not implement g_content_type_guess_for_tree()");
#if defined(__APPLE__) || defined(G_OS_WIN32)
g_test_skip ("The OSX & Windows backends do not implement g_content_type_guess_for_tree()");
return;
#endif
@ -355,7 +370,7 @@ test_type_is_a_special_case (void)
/* Everything but the inode type is application/octet-stream */
res = g_content_type_is_a ("inode/directory", "application/octet-stream");
g_assert_false (res);
#ifndef __APPLE__
#if !defined(__APPLE__) && !defined(G_OS_WIN32)
res = g_content_type_is_a ("anything", "application/octet-stream");
g_assert_true (res);
#endif