mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-11-02 17:32:18 +01:00
gio/tests: Add document portal tests
While this can be tested using desktop apps, adding some unit tests makes simpler to verify the edge cases
This commit is contained in:
committed by
Marco Trevisan
parent
ddc324da03
commit
d81701de56
@@ -367,6 +367,12 @@ if host_system != 'windows'
|
||||
'ggtknotificationbackend.c',
|
||||
)
|
||||
|
||||
document_portal_sources = [files(
|
||||
'gdocumentportal.c',
|
||||
),
|
||||
xdp_dbus_generated,
|
||||
]
|
||||
|
||||
portal_sources = [files(
|
||||
'gdocumentportal.c',
|
||||
'gopenuriportal.c',
|
||||
@@ -379,7 +385,7 @@ if host_system != 'windows'
|
||||
'gportalnotificationbackend.c',
|
||||
'gsandbox.c',
|
||||
),
|
||||
xdp_dbus_generated
|
||||
document_portal_sources,
|
||||
]
|
||||
|
||||
gio_unix_include_headers = files(
|
||||
|
||||
166
gio/tests/documentportal.c
Normal file
166
gio/tests/documentportal.c
Normal file
@@ -0,0 +1,166 @@
|
||||
/* Unit tests for gdocumentportal
|
||||
* GIO - GLib Input, Output and Streaming Library
|
||||
*
|
||||
* Copyright (C) 2025 Marco Trevisan
|
||||
*
|
||||
* SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
*
|
||||
* 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, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
* Author: Marco Trevisan <marco.trevisan@canonical.com>
|
||||
*/
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
#include "gdbus-sessionbus.h"
|
||||
#include "fake-document-portal.h"
|
||||
#include "gdocumentportal.h"
|
||||
|
||||
static void
|
||||
test_document_portal_add_uri (void)
|
||||
{
|
||||
GFakeDocumentPortalThread *thread = NULL;
|
||||
GFile *file;
|
||||
GList *uris = NULL; /* (element-type utf8) */
|
||||
GList *portal_uris = NULL; /* (element-type utf8) */
|
||||
GFileIOStream *iostream = NULL;
|
||||
GError *error = NULL;
|
||||
char *basename;
|
||||
char *expected_name;
|
||||
|
||||
/* Run a fake-document-portal */
|
||||
thread = g_fake_document_portal_thread_new (session_bus_get_address ());
|
||||
g_fake_document_portal_thread_run (thread);
|
||||
|
||||
file = g_file_new_tmp ("test_document_portal_add_uri_XXXXXX",
|
||||
&iostream, NULL);
|
||||
g_assert_no_error (error);
|
||||
g_io_stream_close ((GIOStream *) iostream, NULL, &error);
|
||||
g_assert_no_error (error);
|
||||
g_object_unref (iostream);
|
||||
|
||||
uris = g_list_append (uris, g_file_get_uri (file));
|
||||
portal_uris = g_document_portal_add_documents (uris, "org.gnome.glib.gio", &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
basename = g_file_get_basename (file);
|
||||
expected_name = g_strdup_printf ("file:/document-portal/document-id-0/%s", basename);
|
||||
|
||||
g_assert_cmpuint (g_list_length (portal_uris), ==, 1);
|
||||
g_assert_cmpstr (g_list_nth_data (portal_uris, 0), ==, expected_name);
|
||||
|
||||
g_fake_document_portal_thread_stop (thread);
|
||||
g_clear_object (&thread);
|
||||
g_clear_object (&file);
|
||||
g_clear_pointer (&expected_name, g_free);
|
||||
g_clear_pointer (&basename, g_free);
|
||||
g_clear_list (&uris, g_free);
|
||||
g_clear_list (&portal_uris, g_free);
|
||||
}
|
||||
|
||||
static void
|
||||
test_document_portal_add_not_existent_uri (void)
|
||||
{
|
||||
GFakeDocumentPortalThread *thread = NULL;
|
||||
GList *uris = NULL; /* (element-type const char*) */
|
||||
GList *portal_uris = NULL; /* (element-type utf8) */
|
||||
GError *error = NULL;
|
||||
const char *uri;
|
||||
|
||||
/* Run a fake-document-portal */
|
||||
thread = g_fake_document_portal_thread_new (session_bus_get_address ());
|
||||
g_fake_document_portal_thread_run (thread);
|
||||
|
||||
uri = "file:/no-existent-path-really!";
|
||||
uris = g_list_append (uris, (char *) uri);
|
||||
portal_uris = g_document_portal_add_documents (uris, "org.gnome.glib.gio", &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
g_assert_cmpuint (g_list_length (portal_uris), ==, 1);
|
||||
g_assert_cmpstr (g_list_nth_data (portal_uris, 0), ==, uri);
|
||||
|
||||
g_fake_document_portal_thread_stop (thread);
|
||||
g_clear_object (&thread);
|
||||
g_clear_list (&uris, NULL);
|
||||
g_clear_list (&portal_uris, g_free);
|
||||
}
|
||||
|
||||
static void
|
||||
test_document_portal_add_existent_and_not_existent_uris (void)
|
||||
{
|
||||
GFakeDocumentPortalThread *thread = NULL;
|
||||
GFile *file;
|
||||
GList *uris = NULL; /* (element-type utf8) */
|
||||
GList *portal_uris = NULL; /* (element-type utf8) */
|
||||
GFileIOStream *iostream = NULL;
|
||||
GError *error = NULL;
|
||||
const char *invalid_uri;
|
||||
char *basename;
|
||||
char *expected_name0;
|
||||
char *expected_name1;
|
||||
|
||||
/* Run a fake-document-portal */
|
||||
thread = g_fake_document_portal_thread_new (session_bus_get_address ());
|
||||
g_fake_document_portal_thread_run (thread);
|
||||
|
||||
file = g_file_new_tmp ("test_document_portal_add_existent_and_not_existent_uris_XXXXXX",
|
||||
&iostream, NULL);
|
||||
g_assert_no_error (error);
|
||||
g_io_stream_close ((GIOStream *) iostream, NULL, &error);
|
||||
g_assert_no_error (error);
|
||||
g_object_unref (iostream);
|
||||
|
||||
invalid_uri = "file:/no-existent-path-really!";
|
||||
|
||||
uris = g_list_append (uris, g_file_get_uri (file));
|
||||
uris = g_list_append (uris, g_strdup (invalid_uri));
|
||||
uris = g_list_append (uris, g_file_get_uri (file));
|
||||
uris = g_list_append (uris, g_strdup (invalid_uri));
|
||||
|
||||
portal_uris = g_document_portal_add_documents (uris, "org.gnome.glib.gio", &error);
|
||||
g_assert_no_error (error);
|
||||
|
||||
basename = g_file_get_basename (file);
|
||||
expected_name0 = g_strdup_printf ("file:/document-portal/document-id-0/%s", basename);
|
||||
expected_name1 = g_strdup_printf ("file:/document-portal/document-id-1/%s", basename);
|
||||
|
||||
g_assert_cmpuint (g_list_length (portal_uris), ==, 4);
|
||||
g_assert_cmpstr (g_list_nth_data (portal_uris, 0), ==, expected_name0);
|
||||
g_assert_cmpstr (g_list_nth_data (portal_uris, 1), ==, invalid_uri);
|
||||
g_assert_cmpstr (g_list_nth_data (portal_uris, 2), ==, expected_name1);
|
||||
g_assert_cmpstr (g_list_nth_data (portal_uris, 3), ==, invalid_uri);
|
||||
|
||||
g_fake_document_portal_thread_stop (thread);
|
||||
g_clear_object (&thread);
|
||||
g_clear_object (&file);
|
||||
g_clear_pointer (&expected_name1, g_free);
|
||||
g_clear_pointer (&expected_name0, g_free);
|
||||
g_clear_pointer (&basename, g_free);
|
||||
g_clear_list (&uris, g_free);
|
||||
g_clear_list (&portal_uris, g_free);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char *argv[])
|
||||
{
|
||||
g_setenv ("LC_ALL", "C", TRUE);
|
||||
g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
|
||||
|
||||
g_test_add_func ("/document-portal/add-uri", test_document_portal_add_uri);
|
||||
g_test_add_func ("/document-portal/add-not-existent-uri", test_document_portal_add_not_existent_uri);
|
||||
g_test_add_func ("/document-portal/add-existent-and-not-existent-uri", test_document_portal_add_existent_and_not_existent_uris);
|
||||
|
||||
return session_bus_run ();
|
||||
}
|
||||
@@ -602,15 +602,25 @@ if host_machine.system() != 'windows'
|
||||
'--c-namespace', 'Fake',
|
||||
'@INPUT@'])
|
||||
|
||||
fake_document_portal_sources = [
|
||||
'fake-document-portal.c',
|
||||
fake_document_portal_generated,
|
||||
'fake-desktop-portal.c',
|
||||
fake_openuri_portal_generated,
|
||||
fake_request_portal_generated,
|
||||
]
|
||||
|
||||
if not glib_have_cocoa
|
||||
gio_tests += {
|
||||
'documentportal' : {
|
||||
'extra_sources' : [
|
||||
extra_sources,
|
||||
document_portal_sources,
|
||||
fake_document_portal_sources,
|
||||
],
|
||||
},
|
||||
'dbus-appinfo' : {
|
||||
'extra_sources' : [extra_sources,
|
||||
'fake-document-portal.c',
|
||||
fake_document_portal_generated,
|
||||
'fake-desktop-portal.c',
|
||||
fake_openuri_portal_generated,
|
||||
fake_request_portal_generated],
|
||||
'extra_sources' : [extra_sources, fake_document_portal_sources],
|
||||
},
|
||||
}
|
||||
endif
|
||||
|
||||
Reference in New Issue
Block a user