Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow tests to set the AppInfo to type HOST with a specific app id #1309

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ xdp_method_info_sources = files('xdp-method-info.c') + xdp_method_info_built_sou
xdp_utils_includes = include_directories('.')
xdp_utils_sources = files('xdp-utils.c')

if have_libsystemd
xdp_utils_sources += sd_escape_sources
endif

xdg_desktop_portal_sources = files(
'account.c',
'background.c',
Expand Down Expand Up @@ -114,10 +118,6 @@ if have_geoclue
)
endif

if have_libsystemd
xdg_desktop_portal_sources += sd_escape_sources
endif

common_deps = [
glib_dep,
gio_dep,
Expand Down
15 changes: 14 additions & 1 deletion src/xdp-utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ _xdp_parse_app_id_from_unit_name (const char *unit)
}
#endif /* HAVE_LIBSYSTEMD */

void
static void
set_appid_from_pid (XdpAppInfo *app_info, pid_t pid)
{
#ifdef HAVE_LIBSYSTEMD
Expand Down Expand Up @@ -238,6 +238,14 @@ xdp_app_info_new_host (pid_t pid)
return app_info;
}

static XdpAppInfo *
xdp_app_info_new_test_host (const char *app_id)
{
XdpAppInfo *app_info = xdp_app_info_new (XDP_APP_INFO_KIND_HOST);
app_info->id = g_strdup (app_id);
return app_info;
}

static void
xdp_app_info_free (XdpAppInfo *app_info)
{
Expand Down Expand Up @@ -927,6 +935,11 @@ xdp_invocation_lookup_app_info_sync (GDBusMethodInvocation *invocation,
{
GDBusConnection *connection = g_dbus_method_invocation_get_connection (invocation);
const gchar *sender = g_dbus_method_invocation_get_sender (invocation);
const char *test_override_app_id;

test_override_app_id = g_getenv ("XDG_DESKTOP_PORTAL_TEST_APP_ID");
if (test_override_app_id)
return xdp_app_info_new_test_host (test_override_app_id);

return xdp_connection_lookup_app_info_sync (connection, sender, cancellable, error);
}
Expand Down
4 changes: 3 additions & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,14 @@ class PortalMock:
Parent class for portal tests.
"""

def __init__(self, session_bus, portal_name: str):
def __init__(self, session_bus, portal_name: str, app_id: str = "org.example.App"):
self.bus = session_bus
self.portal_name = portal_name
self.p_mock = None
self.xdp = None
self.portal_interfaces: Dict[str, dbus.Interface] = {}
self.dbus_monitor = None
self.app_id = app_id

@property
def interface_name(self) -> str:
Expand Down Expand Up @@ -412,6 +413,7 @@ def start_xdp(self):
env["G_DEBUG"] = "fatal-criticals"
env["XDG_DESKTOP_PORTAL_DIR"] = portal_dir
env["XDG_CURRENT_DESKTOP"] = "test"
env["XDG_DESKTOP_PORTAL_TEST_APP_ID"] = self.app_id

xdp = subprocess.Popen(argv, env=env)

Expand Down
12 changes: 10 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@ def params() -> dict[str, Any]:


@pytest.fixture
def portal_mock(session_bus, portal_name, params, portal_has_impl) -> PortalMock:
def app_id():
"""
Default fixture providing the app id of the connecting process
"""
return "org.example.App"


@pytest.fixture
def portal_mock(session_bus, portal_name, params, portal_has_impl, app_id) -> PortalMock:
"""
Fixture yielding a PortalMock object with the impl started, if applicable.
"""
pmock = PortalMock(session_bus, portal_name)
pmock = PortalMock(session_bus, portal_name, app_id)
if portal_has_impl:
pmock.start_impl_portal(params)
pmock.start_xdp()
Expand Down
9 changes: 3 additions & 6 deletions tests/limited-portals.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,15 @@ global_setup (void)
g_autofree gchar *argv0 = NULL;
g_autoptr(GSubprocessLauncher) launcher = NULL;
g_autoptr(GSubprocess) subprocess = NULL;
g_autoptr(XdpAppInfo) appinfo = NULL;
guint name_timeout;
const char *argv[4];
GQuark portal_errors G_GNUC_UNUSED;
static gboolean name_appeared;
guint watch;
guint timeout_mult = 1;

appid = "org.example.App";

update_data_dirs ();

g_mkdtemp (outdir);
Expand All @@ -154,6 +155,7 @@ global_setup (void)
g_setenv ("XDG_CURRENT_DESKTOP", "limited", TRUE);
g_setenv ("XDG_RUNTIME_DIR", outdir, TRUE);
g_setenv ("XDG_DATA_HOME", outdir, TRUE);
g_setenv ("XDG_DESKTOP_PORTAL_TEST_APP_ID", appid, TRUE);

/* Re-defining dbus-daemon with a custom script */
setup_dbus_daemon_wrapper (outdir);
Expand Down Expand Up @@ -320,10 +322,6 @@ global_setup (void)
&error);
g_assert_no_error (error);

appinfo = xdp_get_app_info_from_pid (getpid (), &error);
g_assert_no_error (error);
appid = g_strdup (xdp_app_info_get_id (appinfo));

/* make sure errors are registered */
portal_errors = XDG_DESKTOP_PORTAL_ERROR;
}
Expand Down Expand Up @@ -380,7 +378,6 @@ global_teardown (void)

g_object_unref (lockdown);
g_object_unref (permission_store);
g_free (appid);

g_object_unref (session_bus);

Expand Down
4 changes: 0 additions & 4 deletions tests/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ test_portals = executable(
impl_built_sources,
permission_store_built_sources,
portal_built_sources,
sd_escape_sources,
test_portals_sources,
xdp_utils_sources,
dependencies: [common_deps, libportal_dep, libsystemd_dep],
Expand All @@ -144,7 +143,6 @@ limited_portals = executable(
impl_built_sources,
permission_store_built_sources,
portal_built_sources,
sd_escape_sources,
limited_portals_sources,
xdp_utils_sources,
dependencies: [common_deps, libportal_dep, libsystemd_dep],
Expand Down Expand Up @@ -231,7 +229,6 @@ test_permission_store = executable(
'utils.c',
permission_store_built_sources,
xdp_utils_sources,
sd_escape_sources,
dependencies: [common_deps, libsystemd_dep],
include_directories: [common_includes, xdp_utils_includes],
install: enable_installed_tests,
Expand All @@ -250,7 +247,6 @@ test_xdp_utils = executable(
'test-xdp-utils.c',
'utils.c',
xdp_utils_sources,
sd_escape_sources,
dependencies: [common_deps, libsystemd_dep],
include_directories: [common_includes, xdp_utils_includes],
install: enable_installed_tests,
Expand Down
9 changes: 3 additions & 6 deletions tests/test-portals.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,15 @@ global_setup (void)
g_autofree gchar *argv0 = NULL;
g_autoptr(GSubprocessLauncher) launcher = NULL;
g_autoptr(GSubprocess) subprocess = NULL;
g_autoptr(XdpAppInfo) appinfo = NULL;
guint name_timeout;
const char *argv[4];
GQuark portal_errors G_GNUC_UNUSED;
static gboolean name_appeared;
guint watch;
guint timeout_mult = 1;

appid = "org.example.App";

update_data_dirs ();

g_mkdtemp (outdir);
Expand All @@ -154,6 +155,7 @@ global_setup (void)
g_setenv ("XDG_CURRENT_DESKTOP", "test", TRUE);
g_setenv ("XDG_RUNTIME_DIR", outdir, TRUE);
g_setenv ("XDG_DATA_HOME", outdir, TRUE);
g_setenv ("XDG_DESKTOP_PORTAL_TEST_APP_ID", appid, TRUE);

/* Re-defining dbus-daemon with a custom script */
setup_dbus_daemon_wrapper (outdir);
Expand Down Expand Up @@ -333,10 +335,6 @@ global_setup (void)
&error);
g_assert_no_error (error);

appinfo = xdp_get_app_info_from_pid (getpid (), &error);
g_assert_no_error (error);
appid = g_strdup (xdp_app_info_get_id (appinfo));

/* make sure errors are registered */
portal_errors = XDG_DESKTOP_PORTAL_ERROR;
}
Expand Down Expand Up @@ -393,7 +391,6 @@ global_teardown (void)

g_object_unref (lockdown);
g_object_unref (permission_store);
g_free (appid);

g_object_unref (session_bus);

Expand Down
8 changes: 4 additions & 4 deletions tests/test_globalshortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class TestGlobalShortcuts:
def test_version(self, portal_mock):
portal_mock.check_version(1)

def test_global_shortcuts_create_close_session(self, portal_mock):
def test_global_shortcuts_create_close_session(self, portal_mock, app_id):
request = portal_mock.create_request()
options = {
"session_handle_token": "session_token0",
Expand All @@ -38,7 +38,7 @@ def test_global_shortcuts_create_close_session(self, portal_mock):
assert len(method_calls) > 0
_, args = method_calls[-1]
assert args[1] == session.handle
# assert args[2] == "" # appid, not necessary empty
assert args[2] == app_id

session.close()

Expand All @@ -49,7 +49,7 @@ def test_global_shortcuts_create_close_session(self, portal_mock):
assert session.closed

@pytest.mark.parametrize("params", ({"force-close": 500},))
def test_global_shortcuts_create_session_signal_closed(self, portal_mock):
def test_global_shortcuts_create_session_signal_closed(self, portal_mock, app_id):
request = portal_mock.create_request()
options = {
"session_handle_token": "session_token0",
Expand All @@ -67,7 +67,7 @@ def test_global_shortcuts_create_session_signal_closed(self, portal_mock):
assert len(method_calls) > 0
_, args = method_calls[-1]
assert args[1] == session.handle
# assert args[2] == "" # appid, not necessary empty
assert args[2] == app_id

# Now expect the backend to close it

Expand Down
Loading