• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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__ = 'hadess@hadess.net'
11__copyright__ = '(c) 2019 Red Hat Inc.'
12__license__ = 'LGPL 3+'
13
14import unittest
15import sys
16import subprocess
17import fcntl
18import os
19import time
20
21import taptestrunner
22
23try:
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 = os.path.expanduser("~/.cache/jhbuild/build/xdg-desktop-portal/xdg-desktop-portal")
35    XDG_DESKTOP_PORTAL_PATH = "@libexecdir@/xdg-desktop-portal"
36
37    class TestLowMemoryMonitorPortal(dbusmock.DBusTestCase):
38        '''Test GMemoryMonitorPortal'''
39
40        @classmethod
41        def setUpClass(klass):
42            klass.start_system_bus()
43            klass.dbus_con = klass.get_dbus(True)
44            # Start session bus so that xdg-desktop-portal can run on it
45            klass.start_session_bus()
46
47        def setUp(self):
48            try:
49                Gio.MemoryMonitor
50            except AttributeError:
51                raise unittest.SkipTest('Low memory monitor not in '
52                                        'introspection data. Requires '
53                                        'GObject-Introspection ≥ 1.63.2')
54            try:
55                (self.p_mock, self.obj_lmm) = self.spawn_server_template(
56                    'low_memory_monitor', {}, stdout=subprocess.PIPE)
57            except ModuleNotFoundError:
58                raise unittest.SkipTest("Low memory monitor dbusmock template not "
59                                        "found. Requires dbusmock ≥ 0.18.4.")
60            # set log to nonblocking
61            flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL)
62            fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
63            self.last_warning = -1
64            self.dbusmock = dbus.Interface(self.obj_lmm, dbusmock.MOCK_IFACE)
65            try:
66                self.xdp = subprocess.Popen([XDG_DESKTOP_PORTAL_PATH])
67            except FileNotFoundError:
68                raise unittest.SkipTest("xdg-desktop-portal not available")
69
70            try:
71                self.wait_for_bus_object('org.freedesktop.portal.Desktop',
72                                        '/org/freedesktop/portal/desktop')
73            except:
74                raise
75            # subprocess.Popen(['gdbus', 'monitor', '--session', '--dest', 'org.freedesktop.portal.Desktop'])
76
77            os.environ['GTK_USE_PORTAL'] = "1"
78            self.memory_monitor = Gio.MemoryMonitor.dup_default()
79            assert("GMemoryMonitorPortal" in str(self.memory_monitor))
80            self.memory_monitor.connect("low-memory-warning", self.portal_memory_warning_cb)
81            self.mainloop = GLib.MainLoop()
82            self.main_context = self.mainloop.get_context()
83
84        def tearDown(self):
85            self.p_mock.terminate()
86            self.p_mock.wait()
87
88        def portal_memory_warning_cb(self, monitor, level):
89            self.last_warning = level
90            self.main_context.wakeup()
91
92        def test_low_memory_warning_portal_signal(self):
93            '''LowMemoryWarning signal'''
94
95            # Wait 2 seconds
96            timeout = 2
97            while timeout > 0:
98                time.sleep(0.5)
99                timeout -= 0.5
100                self.main_context.iteration(False)
101
102            self.dbusmock.EmitWarning(100)
103            # Wait 2 seconds or until warning
104            timeout = 2
105            while timeout > 0 and self.last_warning != 100:
106                time.sleep(0.5)
107                timeout -= 0.5
108                self.main_context.iteration(False)
109            self.assertEqual(self.last_warning, 100)
110
111            self.dbusmock.EmitWarning(255)
112            # Wait 2 seconds or until warning
113            timeout = 2
114            while timeout > 0 and self.last_warning != 255:
115                time.sleep(0.5)
116                timeout -= 0.5
117                self.main_context.iteration(False)
118            self.assertEqual(self.last_warning, 255)
119
120except ImportError as e:
121    @unittest.skip("Cannot import %s" % e.name)
122    class TestLowMemoryMonitorPortal(unittest.TestCase):
123        def test_low_memory_warning_portal_signal(self):
124            pass
125
126if __name__ == '__main__':
127    unittest.main(testRunner=taptestrunner.TAPTestRunner())
128