xref: /aosp_15_r20/external/pigweed/pw_console/py/console_app_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 logging
17import unittest
18
19from prompt_toolkit.application import create_app_session
20from prompt_toolkit.output import ColorDepth
21
22# inclusive-language: ignore
23from prompt_toolkit.output import DummyOutput as FakeOutput
24
25from pw_console.console_app import ConsoleApp
26from pw_console.console_prefs import ConsolePrefs
27
28
29class TestConsoleApp(unittest.TestCase):
30    """Tests for ConsoleApp."""
31
32    def test_instantiate(self) -> None:
33        """Test init."""
34        with create_app_session(output=FakeOutput()):
35            prefs = ConsolePrefs(
36                project_file=False, project_user_file=False, user_file=False
37            )
38            prefs.set_code_theme('default')
39            console_app = ConsoleApp(
40                color_depth=ColorDepth.DEPTH_8_BIT, prefs=prefs
41            )
42
43            self.assertIsNotNone(console_app)
44
45    def test_multiple_loggers_in_one_pane(self) -> None:
46        """Test window resizing."""
47        # pylint: disable=protected-access
48        with create_app_session(output=FakeOutput()):
49            prefs = ConsolePrefs(
50                project_file=False, project_user_file=False, user_file=False
51            )
52            prefs.set_code_theme('default')
53            console_app = ConsoleApp(
54                color_depth=ColorDepth.DEPTH_8_BIT, prefs=prefs
55            )
56
57            loggers = {
58                'Logs': [
59                    logging.getLogger('test_log1'),
60                    logging.getLogger('test_log2'),
61                    logging.getLogger('test_log3'),
62                ]
63            }
64            for window_title, logger_instances in loggers.items():
65                console_app.add_log_handler(window_title, logger_instances)
66
67            # Two panes, one for the loggers and one for the repl.
68            window_list = console_app.window_manager.first_window_list()
69            self.assertEqual(len(window_list.active_panes), 2)
70
71            self.assertEqual(window_list.active_panes[0].pane_title(), 'Logs')
72            self.assertEqual(
73                window_list.active_panes[0]._pane_subtitle,
74                'test_log1, test_log2, test_log3',
75            )
76            self.assertEqual(
77                window_list.active_panes[0].pane_subtitle(),
78                'test_log1 + 3 more',
79            )
80
81
82if __name__ == '__main__':
83    unittest.main()
84