File: test_dbus.py

package info (click to toggle)
phosh 0.51.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 9,620 kB
  • sloc: ansic: 81,727; xml: 3,903; python: 502; sh: 456; makefile: 34; lisp: 22; javascript: 6
file content (167 lines) | stat: -rwxr-xr-x 5,445 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3

# Copyright (C) 2025 Phosh.mobi e.V.
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Inspired by g-c-c's scenario tester which is
# Copyright (C) 2021 Red Hat Inc.
# Author: Bastien Nocera <hadess@hadess.net>
#
# Author: Guido Günther <agx@sigxcpu.org>

import os
import subprocess
import dbusmock
from collections import OrderedDict
from dbusmock import DBusTestCase
from dbus.mainloop.glib import DBusGMainLoop
from pathlib import Path

from gi.repository import Gio

from . import Phosh, set_nonblock

DBusGMainLoop(set_as_default=True)


class PhoshDBusTestCase(DBusTestCase):
    @classmethod
    def setUpClass(klass):
        klass.mocks = OrderedDict()
        topsrcdir = os.environ["TOPSRCDIR"]
        topbuilddir = os.environ["TOPBUILDDIR"]
        env = {}

        # Start system bus
        DBusTestCase.setUpClass()
        klass.test_bus = Gio.TestDBus.new(Gio.TestDBusFlags.NONE)
        klass.test_bus.up()
        os.environ["DBUS_SYSTEM_BUS_ADDRESS"] = klass.test_bus.get_bus_address()

        # Start session bus
        klass.session_test_bus = Gio.TestDBus.new(Gio.TestDBusFlags.NONE)
        klass.session_test_bus.up()
        os.environ["DBUS_SESSION_BUS_ADDRESS"] = (
            klass.session_test_bus.get_bus_address()
        )

        # Add the templates we want running before phosh starts
        flash_mock = (
            Path(topsrcdir) / "tests" / "integration" / "oneplus,fajita.umockdev"
        )
        udev_mock_script = ["umockdev-run", "-d", str(flash_mock), "--"]

        # TODO: start udev mock for e.g. backlight
        klass.start_from_template("bluez5")
        klass.start_from_template("gsd_rfkill")
        klass.start_from_template("modemmanager")
        klass.start_from_template("networkmanager")

        # Setup logging
        env["G_MESSAGES_DEBUG"] = " ".join(
            [
                "phosh-brightness-manager",
                "phosh-backlight",
                "phosh-backlight-sysfs",
                "phosh-bt-manager",
                "phosh-cell-broadcast-manager",
                "phosh-udev-manager",
                "phosh-torch-manager",
                "phosh-wifi-manager",
                "phosh-wwan-manager",
                "phosh-wwan-mm",
            ]
        )
        env["XDG_CURRENT_DESKTOP"] = "Phosh:GNOME"

        klass.phosh = Phosh(
            topsrcdir, topbuilddir, env, wrapper=udev_mock_script
        ).spawn_nested()

    @classmethod
    def tearDownClass(klass):
        success = klass.phosh.teardown_nested()
        assert success is True

        for mock_server, mock_obj in reversed(klass.mocks.values()):
            mock_server.terminate()
            mock_server.wait()

        DBusTestCase.tearDownClass()

        criticals = klass.phosh.get_criticals()
        assert not criticals, f"Log contains criticals: {criticals}"

        warnings = klass.phosh.get_warnings()
        if warnings:
            print(f"Log contains warnings: {warnings}")

    @classmethod
    def start_from_template(klass, template, params={}):
        mock_server, mock_obj = klass.spawn_server_template(
            template, params, stdout=subprocess.PIPE
        )
        set_nonblock(mock_server.stdout)
        mocks = (mock_server, mock_obj)
        assert klass.mocks.setdefault(template, mocks) == mocks
        return mocks

    def __init__(self, methodName):
        super().__init__(methodName)

    def test_mm(self):
        mm = self.mocks["modemmanager"][1]
        mm.AddSimpleModem()

        assert self.phosh.wait_for_output(" Modem is present\n")
        assert self.phosh.check_for_stdout(" Enabling cell broadcast interface")

        # No data connection yet:
        assert self.phosh.check_for_stdout(" WWAN data connection present: 0")
        subprocess.check_output(["nmcli", "c", "add", "connection.type", "gsm"])
        # Data connection available:
        assert self.phosh.wait_for_output(" WWAN data connection present: 1")

        # Add cell broadcast message
        cbm_text = "Dies ist ein Test für Cellbroadcasts"
        cbm_channel = 4371
        mm.AddCbm(2, cbm_channel, cbm_text)
        assert self.phosh.wait_for_output(f" Received cbm {cbm_channel}: {cbm_text}")

    def test_wifi(self):
        nm = self.mocks["networkmanager"][1]

        assert self.phosh.check_for_stdout(" NM Wi-Fi enabled: 0, present: 0")

        # Add and enable Wi-Fi
        nm.AddWiFiDevice(
            "wifi0", "wlan0", dbusmock.templates.networkmanager.DeviceState.ACTIVATED
        )
        assert self.phosh.wait_for_output(" NM Wi-Fi enabled: 1, present: 1")

    def test_bt(self):
        adapter_name = "hci0"

        assert self.phosh.check_for_stdout(" BT enabled: 1")

        bt = self.mocks["bluez5"][1]
        bt.AddAdapter(adapter_name, "my-phone")
        assert self.phosh.wait_for_output(" State: BLUETOOTH_ADAPTER_STATE_ON")

    def test_torch(self):
        assert self.phosh.wait_for_output(
            " Found torch device 'white:flash' with min brightness 1 and max brightness 255",
            ignore_present=True,
        )

    def test_backlight(self):
        assert self.phosh.wait_for_output(
            " Backlight brightness maps to linear brightness curve",
            ignore_present=True,
        )

        assert self.phosh.wait_for_output(
            " Found HEADLESS-1 for brightness control",
            ignore_present=True,
        )