Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add gsd-rfkill mock #223

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions dbusmock/templates/gsd_rfkill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""gsd-rfkill mock template

This creates the expected properties of the GNOME Settings Daemon's
rfkill object. You can specify any property such as AirplaneMode in
"parameters".
"""

# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.

__author__ = "Guido Günther"
__copyright__ = "2024 The Phosh Developers"

import dbus

from dbusmock import MOCK_IFACE

SYSTEM_BUS = False
BUS_NAME = "org.gnome.SettingsDaemon.Rfkill"
MAIN_OBJ = "/org/gnome/SettingsDaemon/Rfkill"
MAIN_IFACE = "org.gnome.SettingsDaemon.Rfkill"


def load(mock, parameters):
props = dbus.Dictionary(
{
"AirplaneMode": parameters.get("AirplaneMode", False),
"BluetoothAirplaneMode": parameters.get("BluetoothAirplaneMode", False),
"BluetoothHardwareAirplaneMode": parameters.get("BluetoothHardwareAirplaneMode", False),
"BluetoothHasAirplaneMode": parameters.get("BluetoothHasAirplanemode", True),
"HardwareAirplaneMode": parameters.get("HardwareAirplaneMode", False),
"HasAirplaneMode": parameters.get("HasAirplaneMode", True),
"ShouldShowAirplaneMode": parameters.get("ShouldShowAirplaneMode", True),
"WwanAirplaneMode": parameters.get("WwanAirplaneMode", False),
"WwanHardwareAirplaneMode": parameters.get("WwanHardwareAirplaneMode", False),
"WwanHasAirplaneMode": parameters.get("WwanHasAirplaneMode", True),
},
signature="sv",
)
mock.AddProperties(MAIN_IFACE, props)


@dbus.service.method(MOCK_IFACE, in_signature="b", out_signature="b")
def SetAirplaneMode(self, mode):
"""
Convenience method to toggle airplane mode
"""
self.props[MAIN_IFACE]["AirplaneMode"] = mode
self.props[MAIN_IFACE]["BluetoothAirplaneMode"] = mode
self.props[MAIN_IFACE]["WwanAirplaneMode"] = mode
return mode
63 changes: 63 additions & 0 deletions tests/test_gsd_rfkill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation; either version 3 of the License, or (at your option) any
# later version. See http://www.gnu.org/copyleft/lgpl.html for the full text
# of the license.

__author__ = "Guido Günther"
__copyright__ = "2024 The Phosh Developers"

import fcntl
import os
import subprocess
import sys
import unittest

import dbus

import dbusmock


class TestGsdRfkill(dbusmock.DBusTestCase):
"""Test mocked GNOME Settings Daemon Rfkill"""

@classmethod
def setUpClass(cls):
cls.start_session_bus()
cls.dbus_con = cls.get_dbus()

def setUp(self):
(self.p_mock, self.p_obj) = self.spawn_server_template("gsd_rfkill", {}, stdout=subprocess.PIPE)
# set log to nonblocking
flags = fcntl.fcntl(self.p_mock.stdout, fcntl.F_GETFL)
fcntl.fcntl(self.p_mock.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)

def tearDown(self):
self.p_mock.stdout.close()
self.p_mock.terminate()
self.p_mock.wait()

def test_mainobject(self):
propiface = dbus.Interface(self.p_obj, dbus.PROPERTIES_IFACE)

mode = propiface.Get("org.gnome.SettingsDaemon.Rfkill", "AirplaneMode")
self.assertEqual(mode, False)
mode = propiface.Get("org.gnome.SettingsDaemon.Rfkill", "HasAirplaneMode")
self.assertEqual(mode, True)

def test_airplane_mode(self):
propiface = dbus.Interface(self.p_obj, dbus.PROPERTIES_IFACE)

self.p_obj.SetAirplaneMode(True)

mode = propiface.Get("org.gnome.SettingsDaemon.Rfkill", "AirplaneMode")
self.assertEqual(mode, True)
mode = propiface.Get("org.gnome.SettingsDaemon.Rfkill", "BluetoothAirplaneMode")
self.assertEqual(mode, True)
mode = propiface.Get("org.gnome.SettingsDaemon.Rfkill", "WwanAirplaneMode")
self.assertEqual(mode, True)


if __name__ == "__main__":
# avoid writing to stderr
unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout))