| 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, 2021 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 |
class TestPowerProfileMonitor(dbusmock.DBusTestCase): |
| 35 |
'''Test GPowerProfileMonitorDBus''' |
| 36 |
|
| 37 |
@classmethod |
| 38 |
def setUpClass(klass): |
| 39 |
klass.start_system_bus() |
| 40 |
klass.dbus_con = klass.get_dbus(True) |
| 41 |
|
| 42 |
def setUp(self): |
| 43 |
try: |
| 44 |
Gio.PowerProfileMonitor |
| 45 |
except AttributeError: |
| 46 |
raise unittest.SkipTest('Power Profile Monitor not in ' |
| 47 |
'introspection data. Requires ' |
| 48 |
'GObject-Introspection ≥ 1.63.2') # FIXME version |
| 49 |
try: |
| 50 |
(self.p_mock, self.obj_ppd) = self.spawn_server_template( |
| 51 |
'power_profiles_daemon', {}, stdout=subprocess.PIPE) |
| 52 |
except ModuleNotFoundError: |
| 53 |
raise unittest.SkipTest("power-profiles-daemon dbusmock template not " |
| 54 |
"found. Requires dbusmock > 0.23.1.") # FIXME version |
| 55 |
# set log to nonblocking |
| 56 |
flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL) |
| 57 |
fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK) |
| 58 |
self.power_saver_enabled = False |
| 59 |
self.dbus_props = dbus.Interface(self.obj_ppd, dbus.PROPERTIES_IFACE) |
| 60 |
self.power_profile_monitor = Gio.PowerProfileMonitor.dup_default() |
| 61 |
self.power_profile_monitor.connect("notify::power-saver-enabled", self.power_saver_enabled_cb) |
| 62 |
self.mainloop = GLib.MainLoop() |
| 63 |
self.main_context = self.mainloop.get_context() |
| 64 |
|
| 65 |
def tearDown(self): |
| 66 |
self.p_mock.terminate() |
| 67 |
self.p_mock.wait() |
| 68 |
|
| 69 |
def assertEventually(self, condition, message=None, timeout=50): |
| 70 |
'''Assert that condition function eventually returns True. |
| 71 |
|
| 72 |
Timeout is in deciseconds, defaulting to 50 (5 seconds). message is |
| 73 |
printed on failure. |
| 74 |
''' |
| 75 |
while timeout >= 0: |
| 76 |
context = GLib.MainContext.default() |
| 77 |
while context.iteration(False): |
| 78 |
pass |
| 79 |
if condition(): |
| 80 |
break |
| 81 |
timeout -= 1 |
| 82 |
time.sleep(0.1) |
| 83 |
else: |
| 84 |
self.fail(message or 'timed out waiting for ' + str(condition)) |
| 85 |
|
| 86 |
def power_saver_enabled_cb(self, spec, data): |
| 87 |
self.power_saver_enabled = self.power_profile_monitor.get_power_saver_enabled() |
| 88 |
self.main_context.wakeup() |
| 89 |
|
| 90 |
def test_power_profile_power_saver_enabled(self): |
| 91 |
'''power-saver-enabled property''' |
| 92 |
|
| 93 |
self.assertEqual(self.power_profile_monitor.get_power_saver_enabled(), False) |
| 94 |
self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('power-saver', variant_level=1)) |
| 95 |
self.assertEventually(lambda: self.power_saver_enabled == True, "power-saver didn't become enabled", 10) |
| 96 |
|
| 97 |
self.dbus_props.Set('net.hadess.PowerProfiles', 'ActiveProfile', dbus.String('balanced', variant_level=1)) |
| 98 |
self.assertEventually(lambda: self.power_saver_enabled == False, "power-saver didn't become disabled", 10) |
| 99 |
|
| 100 |
except ImportError as e: |
| 101 |
@unittest.skip("Cannot import %s" % e.name) |
| 102 |
class TestPowerProfileMonitor(unittest.TestCase): |
| 103 |
def test_power_profile_power_saver_enabled(self): |
| 104 |
pass |
| 105 |
|
| 106 |
if __name__ == '__main__': |
| 107 |
unittest.main(testRunner=taptestrunner.TAPTestRunner()) |