aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_actions.py
blob: f6537fa6ab1d731e717d3007a2f5690b991ca45a (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
# deck-it - tests for actions
# 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 subprocess
import unittest

import deckit.config as config
from deckit.module import ModuleError
from deckit.modules import actions


class Actions(unittest.TestCase):
    def test_ids_match_the_config(self):
        self.assertEqual(tuple(actions.ACTIONS), config.ACTION_IDS)

    def test_state_lists_the_configured_actions_in_order(self):
        m = actions.make({"dashboard": {"actions": ["lock", "screenshot"]}})
        self.assertEqual([a["id"] for a in m.state()["actions"]], ["lock", "screenshot"])
        self.assertEqual(set(m.state()["actions"][0]), {"id", "label", "icon"})

    def test_only_configured_actions_run(self):
        calls = []
        m = actions.make({"dashboard": {"actions": ["lock"]}, "popen": calls.append})
        m.actions["lock"]({})
        self.assertEqual(calls, [["lock.sh"]])
        self.assertNotIn("screenshot", m.actions)

    def test_screenshot_opens_the_menu(self):
        calls = []
        m = actions.make({"dashboard": {"actions": ["screenshot"]}, "popen": calls.append})
        m.actions["screenshot"]({})
        self.assertEqual(calls, [["qar-scrotmenu.sh"]])

    def test_missing_command_is_a_module_error(self):
        def missing(cmd):
            raise FileNotFoundError(cmd[0])
        m = actions.make({"dashboard": {"actions": ["lock"]}, "popen": missing})
        with self.assertRaises(ModuleError):
            m.actions["lock"]({})


class Follow(unittest.TestCase):
    def test_sleeps_on_lock_and_wakes_on_unlock(self):
        keys = []
        was = actions.follow(lambda: True, keys.append, False)
        was = actions.follow(lambda: True, keys.append, was)
        was = actions.follow(lambda: False, keys.append, was)
        self.assertEqual(keys, ["KEYCODE_SLEEP", "KEYCODE_WAKEUP"])
        self.assertFalse(was)

    def test_nothing_while_unchanged(self):
        keys = []
        actions.follow(lambda: False, keys.append, False)
        self.assertEqual(keys, [])

    def test_adb_failure_is_ignored(self):
        def boom(code):
            raise subprocess.CalledProcessError(1, "adb")
        self.assertTrue(actions.follow(lambda: True, boom, False))


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