aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_tablet.py
blob: 611810aa6a7423996681113a2cb13c4c94051297 (plain)
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
# deck-it - tests for tablet
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

import unittest
import unittest.mock

from deckit.modules import tablet

BATTERY = """Current Battery Service state:
  AC powered: false
  USB powered: true
  Wireless powered: false
  status: 2
  health: 2
  present: true
  level: 85
  scale: 100
  voltage: 4200
  temperature: 312
"""

WIFI = """Wifi is enabled
Wifi scanning is always available
==== Primary ClientModeManager instance ====
Wifi is connected to "ExampleNet"
WifiInfo: SSID: "ExampleNet", BSSID: 02:00:00:00:00:00, RSSI: -58, Link speed: 866Mbps, Frequency: 5180MHz
"""


class Parse(unittest.TestCase):
    def test_battery(self):
        self.assertEqual(tablet.parse_battery(BATTERY),
                         {"level": 85, "status": "charging", "plug": "USB", "temp": 31.2})

    def test_wifi(self):
        self.assertEqual(tablet.parse_wifi(WIFI),
                         {"connected": True, "ssid": "ExampleNet", "rssi": -58, "speed": 866, "band": "5.2 GHz"})

    def test_wifi_off(self):
        self.assertEqual(tablet.parse_wifi("Wifi is disabled\n"), {"connected": False})


class Dim(unittest.TestCase):
    def test_dim_saves_and_undim_restores(self):
        calls, orig = [], tablet._adb
        answers = {"settings get system screen_brightness_mode": "1\n",
                   "settings get system screen_brightness": "200\n"}
        tablet._adb = lambda *a, **k: calls.append(a[-1]) or answers.get(a[-1], "")
        try:
            m = tablet.make({"tablet": {"dim_brightness": 5}})
            with unittest.mock.patch.object(tablet, "_find_tablet", return_value="x"):
                m.actions["dim"]({})
                m.actions["dim"]({})   # already dim: the saved level is kept
                m.actions["undim"]({})
                m.actions["undim"]({})  # already bright: nothing sent
        finally:
            tablet._adb = orig
        sets = [c for c in calls if "put" in c]
        self.assertEqual(sets, [
            "settings put system screen_brightness_mode 0; settings put system screen_brightness 5",
            "settings put system screen_brightness 200; settings put system screen_brightness_mode 1"])


if __name__ == "__main__":
    unittest.main()