Debian Codegraph @

glib/gio/tests/memory-monitor-portal.py.in

raw
1 #!/usr/bin/python3
2
3 # This program is free software; you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation; either version 3 of the License, or (at your option) any
6 # later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
7 # of the license.
8
9 __author__ = 'Bastien Nocera'
10 __email__ = '[email protected]'
11 __copyright__ = '(c) 2019 Red Hat Inc.'
12 __license__ = 'LGPL 3+'
13
14 import unittest
15 import sys
16 import subprocess
17 import fcntl
18 import os
19 import time
20
21 import taptestrunner
22
23 try:
24 # Do all non-standard imports here so we can skip the tests if any
25 # needed packages are not available.
26 import dbus
27 import dbus.mainloop.glib
28 import dbusmock
29 from gi.repository import GLib
30 from gi.repository import Gio
31
32 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
33
34 XDG_DESKTOP_PORTAL_PATH = "@libexecdir@/xdg-desktop-portal"
35
36 class TestLowMemoryMonitorPortal(dbusmock.DBusTestCase):
37 '''Test GMemoryMonitorPortal'''
38
39 @classmethod
40 def setUpClass(klass):
41 klass.start_system_bus()
42 klass.dbus_con = klass.get_dbus(True)
43 # Start session bus so that xdg-desktop-portal can run on it
44 klass.start_session_bus()
45
46 def setUp(self):
47 try:
48 Gio.MemoryMonitor
49 except AttributeError:
50 raise unittest.SkipTest('Low memory monitor not in '
51 'introspection data. Requires '
52 'GObject-Introspection ≥ 1.63.2')
53 try:
54 (self.p_mock, self.obj_lmm) = self.spawn_server_template(
55 'low_memory_monitor', {}, stdout=subprocess.PIPE)
56 except ModuleNotFoundError:
57 raise unittest.SkipTest("Low memory monitor dbusmock template not "
58 "found. Requires dbusmock ≥ 0.18.4.")
59 # set log to nonblocking
60 flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL)
61 fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
62 self.last_warning = -1
63 self.dbusmock = dbus.Interface(self.obj_lmm, dbusmock.MOCK_IFACE)
64 try:
65 self.xdp = subprocess.Popen([XDG_DESKTOP_PORTAL_PATH])
66 except FileNotFoundError:
67 raise unittest.SkipTest("xdg-desktop-portal not available")
68
69 try:
70 self.wait_for_bus_object('org.freedesktop.portal.Desktop',
71 '/org/freedesktop/portal/desktop')
72 except:
73 raise
74 # subprocess.Popen(['gdbus', 'monitor', '--session', '--dest', 'org.freedesktop.portal.Desktop'])
75
76 os.environ['GIO_USE_PORTALS'] = "1"
77 self.memory_monitor = Gio.MemoryMonitor.dup_default()
78 assert("GMemoryMonitorPortal" in str(self.memory_monitor))
79 self.memory_monitor.connect("low-memory-warning", self.portal_memory_warning_cb)
80 self.mainloop = GLib.MainLoop()
81 self.main_context = self.mainloop.get_context()
82
83 def tearDown(self):
84 self.p_mock.terminate()
85 self.p_mock.wait()
86
87 def assertEventually(self, condition, message=None, timeout=50):
88 '''Assert that condition function eventually returns True.
89
90 Timeout is in deciseconds, defaulting to 50 (5 seconds). message is
91 printed on failure.
92 '''
93 while timeout >= 0:
94 context = GLib.MainContext.default()
95 while context.iteration(False):
96 pass
97 if condition():
98 break
99 timeout -= 1
100 time.sleep(0.1)
101 else:
102 self.fail(message or 'timed out waiting for ' + str(condition))
103
104 def portal_memory_warning_cb(self, monitor, level):
105 self.last_warning = level
106 self.main_context.wakeup()
107
108 def test_low_memory_warning_portal_signal(self):
109 '''LowMemoryWarning signal'''
110
111 # Wait 2 seconds
112 timeout = 2
113 while timeout > 0:
114 time.sleep(0.5)
115 timeout -= 0.5
116 self.main_context.iteration(False)
117
118 self.dbusmock.EmitWarning(100)
119 # Wait 2 seconds or until warning
120 self.assertEventually(lambda: self.last_warning == 100, "'100' low-memory warning not received", 20)
121
122 self.dbusmock.EmitWarning(255)
123 # Wait 2 seconds or until warning
124 self.assertEventually(lambda: self.last_warning == 255, "'255' low-memory warning not received", 20)
125
126 except ImportError as e:
127 @unittest.skip("Cannot import %s" % e.name)
128 class TestLowMemoryMonitorPortal(unittest.TestCase):
129 def test_low_memory_warning_portal_signal(self):
130 pass
131
132 if __name__ == '__main__':
133 unittest.main(testRunner=taptestrunner.TAPTestRunner())