|
| 1 | +"""power-profiles-daemon >= 0.20 mock template |
| 2 | +
|
| 3 | +This creates the expected methods and properties of the main |
| 4 | +org.freedesktop.UPower.PowerProfiles object. |
| 5 | +
|
| 6 | +This provides the D-Bus API as of version 0.20. |
| 7 | +""" |
| 8 | + |
| 9 | +# This program is free software; you can redistribute it and/or modify it under |
| 10 | +# the terms of the GNU Lesser General Public License as published by the Free |
| 11 | +# Software Foundation; either version 3 of the License, or (at your option) any |
| 12 | +# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text |
| 13 | +# of the license. |
| 14 | + |
| 15 | +__author__ = "Bastien Nocera" |
| 16 | +__copyright__ = """ |
| 17 | +(c) 2021, Red Hat Inc. |
| 18 | +(c) 2017 - 2024 Martin Pitt <[email protected]> |
| 19 | +""" |
| 20 | + |
| 21 | +import dbus |
| 22 | + |
| 23 | +BUS_NAME = "org.freedesktop.UPower.PowerProfiles" |
| 24 | +MAIN_OBJ = "/org/freedesktop/UPower/PowerProfiles" |
| 25 | +MAIN_IFACE = "org.freedesktop.UPower.PowerProfiles" |
| 26 | +SYSTEM_BUS = True |
| 27 | + |
| 28 | + |
| 29 | +def hold_profile(self, profile, reason, application_id): |
| 30 | + self.cookie += 1 |
| 31 | + element = {"Profile": profile, "Reason": reason, "ApplicationId": application_id} |
| 32 | + self.holds[self.cookie] = element |
| 33 | + self.props[MAIN_IFACE]["ActiveProfileHolds"] = [] |
| 34 | + for value in self.holds.values(): |
| 35 | + self.props[MAIN_IFACE]["ActiveProfileHolds"].append(value) |
| 36 | + return self.cookie |
| 37 | + |
| 38 | + |
| 39 | +def release_profile(self, cookie): |
| 40 | + self.holds.pop(cookie) |
| 41 | + self.props[MAIN_IFACE]["ActiveProfileHolds"] = [] |
| 42 | + for value in self.holds.values(): |
| 43 | + self.props[MAIN_IFACE]["ActiveProfileHolds"].append(value) |
| 44 | + if len(self.props[MAIN_IFACE]["ActiveProfileHolds"]) == 0: |
| 45 | + self.props[MAIN_IFACE]["ActiveProfileHolds"] = dbus.Array([], signature="(aa{sv})") |
| 46 | + |
| 47 | + |
| 48 | +def load(mock, parameters): |
| 49 | + # Loaded! |
| 50 | + mock.loaded = True |
| 51 | + mock.cookie = 0 |
| 52 | + mock.hold_profile = hold_profile |
| 53 | + mock.release_profile = release_profile |
| 54 | + mock.holds = {} |
| 55 | + |
| 56 | + props = { |
| 57 | + "ActiveProfile": parameters.get("ActiveProfile", "balanced"), |
| 58 | + "PerformanceDegraded": parameters.get("PerformanceDegraded", ""), |
| 59 | + "Profiles": [ |
| 60 | + dbus.Dictionary({"Profile": "power-saver", "Driver": "dbusmock"}, signature="sv"), |
| 61 | + dbus.Dictionary({"Profile": "balanced", "Driver": "dbusmock"}, signature="sv"), |
| 62 | + dbus.Dictionary({"Profile": "performance", "Driver": "dbusmock"}, signature="sv"), |
| 63 | + ], |
| 64 | + "Actions": dbus.Array([], signature="s"), |
| 65 | + "ActiveProfileHolds": dbus.Array([], signature="(aa{sv})"), |
| 66 | + } |
| 67 | + mock.AddProperties(MAIN_IFACE, dbus.Dictionary(props, signature="sv")) |
| 68 | + |
| 69 | + mock.AddMethods( |
| 70 | + MAIN_IFACE, |
| 71 | + [ |
| 72 | + ("HoldProfile", "sss", "u", "ret = self.hold_profile(self, args[0], args[1], args[2])"), |
| 73 | + ("ReleaseProfile", "u", "", "self.release_profile(self, args[0])"), |
| 74 | + ], |
| 75 | + ) |
0 commit comments