Debian Codegraph @

tests/test_camera.py

raw
1 # SPDX-License-Identifier: LGPL-2.1-or-later
2 #
3 # This file is formatted with Python Black
4
5 import tests as xdp
6
7 import dbus
8 import pytest
9
10
11 @pytest.fixture
12 def required_templates():
13 return {
14 "access": {},
15 "lockdown": {},
16 }
17
18
19 @pytest.fixture
20 def app_id():
21 # x-d-p currently defaults to the empty app id for the camera portal for
22 # host XdpAppInfos (which the XdpAppInfoTest is). So use the empty app_id
23 # for now.
24 return ""
25
26
27 class TestCamera:
28 def set_permissions(self, dbus_con, appid, permissions):
29 perm_store_intf = xdp.get_permission_store_iface(dbus_con)
30 perm_store_intf.SetPermission(
31 "devices",
32 True,
33 "camera",
34 appid,
35 permissions,
36 )
37
38 def test_version(self, portals, dbus_con):
39 xdp.check_version(dbus_con, "Camera", 1)
40
41 def test_access(self, portals, dbus_con, app_id):
42 camera_intf = xdp.get_portal_iface(dbus_con, "Camera")
43 mock_intf = xdp.get_mock_iface(dbus_con)
44
45 request = xdp.Request(dbus_con, camera_intf)
46 response = request.call(
47 "AccessCamera",
48 options={},
49 )
50
51 assert response
52 assert response.response == 0
53
54 # Check the impl portal was called with the right args
55 method_calls = mock_intf.GetMethodCalls("AccessDialog")
56 assert len(method_calls) == 1
57 _, args = method_calls[-1]
58 assert args[1] == app_id
59
60 @pytest.mark.parametrize("template_params", ({"access": {"response": 1}},))
61 def test_access_cancel(self, portals, dbus_con, app_id):
62 camera_intf = xdp.get_portal_iface(dbus_con, "Camera")
63 mock_intf = xdp.get_mock_iface(dbus_con)
64
65 request = xdp.Request(dbus_con, camera_intf)
66 response = request.call(
67 "AccessCamera",
68 options={},
69 )
70
71 assert response
72 assert response.response == 1
73
74 # Check the impl portal was called with the right args
75 method_calls = mock_intf.GetMethodCalls("AccessDialog")
76 assert len(method_calls) == 1
77 _, args = method_calls[-1]
78 assert args[1] == app_id
79
80 @pytest.mark.parametrize("template_params", ({"access": {"expect-close": True}},))
81 def test_access_close(self, portals, dbus_con, app_id):
82 camera_intf = xdp.get_portal_iface(dbus_con, "Camera")
83 mock_intf = xdp.get_mock_iface(dbus_con)
84
85 request = xdp.Request(dbus_con, camera_intf)
86 request.schedule_close(1000)
87 request.call(
88 "AccessCamera",
89 options={},
90 )
91
92 # Only true if the impl.Request was closed too
93 assert request.closed
94
95 # Check the impl portal was called with the right args
96 method_calls = mock_intf.GetMethodCalls("AccessDialog")
97 assert len(method_calls) == 1
98 _, args = method_calls[-1]
99 assert args[1] == app_id
100
101 @pytest.mark.parametrize(
102 "template_params", ({"lockdown": {"disable-camera": True}},)
103 )
104 def test_access_lockdown(self, portals, dbus_con, app_id):
105 camera_intf = xdp.get_portal_iface(dbus_con, "Camera")
106 mock_intf = xdp.get_mock_iface(dbus_con)
107
108 request = xdp.Request(dbus_con, camera_intf)
109 with pytest.raises(dbus.exceptions.DBusException) as excinfo:
110 request.call(
111 "AccessCamera",
112 options={},
113 )
114 assert (
115 excinfo.value.get_dbus_name() == "org.freedesktop.portal.Error.NotAllowed"
116 )
117
118 # Check the impl portal was called with the right args
119 method_calls = mock_intf.GetMethodCalls("AccessDialog")
120 assert len(method_calls) == 0
121
122 def test_access_denied(self, portals, dbus_con, app_id):
123 camera_intf = xdp.get_portal_iface(dbus_con, "Camera")
124 mock_intf = xdp.get_mock_iface(dbus_con)
125
126 self.set_permissions(dbus_con, app_id, ["no"])
127
128 request = xdp.Request(dbus_con, camera_intf)
129 response = request.call(
130 "AccessCamera",
131 options={},
132 )
133
134 assert response
135 assert response.response == 1
136
137 # Check the impl portal was called with the right args
138 method_calls = mock_intf.GetMethodCalls("AccessDialog")
139 assert len(method_calls) == 0