xref: /aosp_15_r20/external/pigweed/pw_console/py/help_window_test.py (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2021 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14"""Tests for pw_console.console_app"""
15
16import inspect
17import logging
18import unittest
19from unittest.mock import MagicMock
20
21from jinja2 import Environment, PackageLoader, make_logging_undefined
22from prompt_toolkit.key_binding import KeyBindings
23
24from pw_console.help_window import HelpWindow
25
26_PW_CONSOLE_MODULE = 'pw_console'
27
28
29_jinja_env = Environment(
30    loader=PackageLoader(_PW_CONSOLE_MODULE),
31    undefined=make_logging_undefined(logger=logging.getLogger('pw_console')),
32    trim_blocks=True,
33    lstrip_blocks=True,
34)
35
36
37def _create_app_mock():
38    template = _jinja_env.get_template('keybind_list.jinja')
39    mock_app = MagicMock()
40    mock_app.get_template = MagicMock(return_value=template)
41    return mock_app
42
43
44class TestHelpWindow(unittest.TestCase):
45    """Tests for HelpWindow text and keybind lists."""
46
47    maxDiff = None
48
49    def test_instantiate(self) -> None:
50        app = _create_app_mock()
51        help_window = HelpWindow(app)
52        self.assertIsNotNone(help_window)
53
54    # pylint: disable=unused-variable,unused-argument
55    def test_add_keybind_help_text(self) -> None:
56        key_bindings = KeyBindings()
57
58        @key_bindings.add('f1')
59        def show_help(event):
60            """Toggle help window."""
61
62        @key_bindings.add('c-w')
63        @key_bindings.add('c-q')
64        def exit_(event):
65            """Quit the application."""
66
67        app = _create_app_mock()
68
69        help_window = HelpWindow(app)
70        help_window.add_keybind_help_text('Global', key_bindings)
71
72        self.assertEqual(
73            help_window.help_text_sections,
74            {
75                'Global': {
76                    'Quit the application.': ['Ctrl-Q', 'Ctrl-W'],
77                    'Toggle help window.': ['F1'],
78                }
79            },
80        )
81
82    def test_generate_keybind_help_text(self) -> None:
83        """Test keybind list template generation."""
84        global_bindings = KeyBindings()
85
86        @global_bindings.add('f1')
87        def show_help(event):
88            """Toggle help window."""
89
90        @global_bindings.add('c-w')
91        @global_bindings.add('c-q')
92        def exit_(event):
93            """Quit the application."""
94
95        focus_bindings = KeyBindings()
96
97        @focus_bindings.add('s-tab')
98        @focus_bindings.add('c-right')
99        @focus_bindings.add('c-down')
100        def app_focus_next(event):
101            """Move focus to the next widget."""
102
103        @focus_bindings.add('c-left')
104        @focus_bindings.add('c-up')
105        def app_focus_previous(event):
106            """Move focus to the previous widget."""
107
108        app = _create_app_mock()
109
110        help_window = HelpWindow(
111            app,
112            preamble='Pigweed CLI v0.1',
113            additional_help_text=inspect.cleandoc(
114                """
115                Welcome to the Pigweed Console!
116                Please enjoy this extra help text.
117            """
118            ),
119        )
120        help_window.add_keybind_help_text('Global', global_bindings)
121        help_window.add_keybind_help_text('Focus', focus_bindings)
122        help_window.generate_keybind_help_text()
123
124        self.assertIn(
125            inspect.cleandoc(
126                """
127            Welcome to the Pigweed Console!
128            Please enjoy this extra help text.
129            """
130            ),
131            help_window.help_text,
132        )
133        self.assertIn(
134            inspect.cleandoc(
135                """
136            ==== Global Keys ====
137            """
138            ),
139            help_window.help_text,
140        )
141        self.assertIn(
142            inspect.cleandoc(
143                """
144            Toggle help window. -----------------  F1
145            Quit the application. ---------------  Ctrl-Q
146                                                   Ctrl-W
147            """
148            ),
149            help_window.help_text,
150        )
151        self.assertIn(
152            inspect.cleandoc(
153                """
154            ==== Focus Keys ====
155            """
156            ),
157            help_window.help_text,
158        )
159        self.assertIn(
160            inspect.cleandoc(
161                """
162            Move focus to the next widget. ------  Ctrl-Down
163                                                   Ctrl-Right
164                                                   Shift-Tab
165            Move focus to the previous widget. --  Ctrl-Left
166                                                   Ctrl-Up
167            """
168            ),
169            help_window.help_text,
170        )
171
172
173if __name__ == '__main__':
174    unittest.main()
175