File: test_prefseditor_keybindings.py

package info (click to toggle)
terminator 2.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,916 kB
  • sloc: python: 11,390; sh: 26; makefile: 15
file content (306 lines) | stat: -rw-r--r-- 9,841 bytes parent folder | download | duplicates (3)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import pytest
import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib

CONTROL_SHIFT_MOD = Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.SHIFT_MASK
CONTROL_ALT_MOD = Gdk.ModifierType.CONTROL_MASK | Gdk.ModifierType.MOD1_MASK
CONTROL_ALT_SHIFT_MOD = (
    Gdk.ModifierType.CONTROL_MASK
    | Gdk.ModifierType.MOD1_MASK
    | Gdk.ModifierType.SHIFT_MASK
)


class MessageDialogToken:
    def __init__(self):
        self.has_appeared = False


def detect_close_message_dialog(prefs_editor, message_dialog_token):
    # type: (terminatorlib.prefseditor.PrefsEditor, MessageDialogToken) -> bool
    """
    Checks whether a message dialog is displayed over the Preferences
    window and if so, closes it. This function is intended to be used with
    `GLib.timeout_add_seconds` or a similar function.
    """
    if prefs_editor.active_message_dialog is not None:
        message_dialog_token.has_appeared = True
        prefs_editor.active_message_dialog.hide()
        return False
    return True


def reset_config_keybindings():
    """
    Resets key bindings to the default.

    Key bindings are stored in `terminatorlib.config.ConfigBase`,
    which is implemented using the Borg design pattern - this means
    that simply recreating a `ConfigBase` instance won't reset the key
    bindings to the default.
    """
    from terminatorlib import config

    conf_base = config.ConfigBase()
    conf_base.keybindings = None
    conf_base.prepare_attributes()


def test_non_empty_default_keybinding_accels_are_distinct():
    """
    Tests that all non-empty key binding accelerators defined in
    the default config are distinct.
    """
    from terminatorlib import config

    all_default_accelerators = [
        Gtk.accelerator_parse(accel)
        for accel in config.DEFAULTS["keybindings"].values()
        if accel != ""  # ignore empty key bindings
    ]

    assert len(all_default_accelerators) == len(set(all_default_accelerators))


@pytest.mark.parametrize(
    "accel_params,expected",
    [
        # 1) 'edit_tab_title' Ctrl+Alt+A
        (("9", 97, CONTROL_ALT_MOD, 38), False,),
        # 2) 'edit_terminal_title' Ctrl+Alt+A
        (("10", 97, CONTROL_ALT_MOD, 38), True,),
        # 3) 'edit_window_title' F11
        (("11", 65480, Gdk.ModifierType(0), 95), True,),
        # 4) 'zoom_in' Shift+Ctrl+Z
        (("70", 122, CONTROL_SHIFT_MOD, 52), True,),
        # 5) 'close_terminal' Ctrl+Alt+{
        (("3", 123, CONTROL_ALT_SHIFT_MOD, 34), False,),
        # 6) 'zoom_in' Shift+Ctrl+B
        (("70", 98, CONTROL_SHIFT_MOD, 56), False,),
    ],
)
def test_message_dialog_is_shown_on_duplicate_accel_assignment(
    accel_params, expected
):
    """
    Tests that a message dialog appears every time a duplicate key binding accelerator
    is attempted to be assigned to a different action in `Preferences > Keybindings`,
    and doesn't appear if a key binding accelerator is not a duplicate.
    """
    from terminatorlib import terminal
    from terminatorlib import prefseditor

    path, key, mods, code = accel_params

    term = terminal.Terminal()
    prefs_editor = prefseditor.PrefsEditor(term=term)
    message_dialog = MessageDialogToken()
    # Check for an active message dialog every second
    GLib.timeout_add_seconds(
        1, detect_close_message_dialog, prefs_editor, message_dialog
    )

    widget = prefs_editor.builder.get_object("keybindingtreeview")
    treemodelfilter = widget.get_model()
    liststore = treemodelfilter.get_model()

    # Replace default accelerator with a test one
    prefs_editor.on_cellrenderer_accel_edited(
        liststore=liststore, path=path, key=key, mods=mods, _code=code
    )

    assert message_dialog.has_appeared == expected

    reset_config_keybindings()


@pytest.mark.parametrize(
    "accel_params",
    [
        # 1) 'edit_tab_title' Ctrl+Alt+A
        ("9", 97, CONTROL_ALT_MOD, 38),
        # 2) 'edit_terminal_title' Ctrl+Alt+A
        ("10", 97, CONTROL_ALT_MOD, 38),
        # 3) 'edit_window_title' F11
        ("11", 65480, Gdk.ModifierType(0), 95),
        # 4) 'zoom_in' Shift+Ctrl+Z
        ("70", 122, CONTROL_SHIFT_MOD, 52),
    ],
)
def test_duplicate_accels_not_possible_to_set(accel_params):
    """
    Tests that a duplicate key binding accelerator, that is a key binding accelerator
    which is already defined in the config cannot be used to refer to more than
    one action.
    """
    from terminatorlib import config
    from terminatorlib import terminal
    from terminatorlib import prefseditor

    path, key, mods, code = accel_params

    term = terminal.Terminal()
    prefs_editor = prefseditor.PrefsEditor(term=term)
    message_dialog = MessageDialogToken()

    # Check for an active message dialog every second
    GLib.timeout_add_seconds(
        1, detect_close_message_dialog, prefs_editor, message_dialog
    )

    widget = prefs_editor.builder.get_object("keybindingtreeview")
    treemodelfilter = widget.get_model()
    liststore = treemodelfilter.get_model()
    binding = liststore.get_value(liststore.get_iter(path), 0)

    all_default_accelerators = {
        Gtk.accelerator_parse(accel)
        for accel in config.DEFAULTS["keybindings"].values()
        if accel != ""  # ignore empty key bindings
    }
    # Check that a test accelerator is indeed a duplicate
    assert (key, mods) in all_default_accelerators

    default_accelerator = Gtk.accelerator_parse(
        config.DEFAULTS["keybindings"][binding]
    )

    # Replace default accelerator with a test one
    prefs_editor.on_cellrenderer_accel_edited(
        liststore=liststore, path=path, key=key, mods=mods, _code=code
    )

    new_accelerator = Gtk.accelerator_parse(
        prefs_editor.config["keybindings"][binding]
    )

    # Key binding accelerator value shouldn't have changed
    assert default_accelerator == new_accelerator

    reset_config_keybindings()


@pytest.mark.parametrize(
    "input_key_params,expected_accel",
    [
        # 1) `Ctrl+Shift+1` should become `Ctrl+!`
        (
            (Gdk.KEY_1, CONTROL_SHIFT_MOD, 10),
            (Gdk.KEY_exclam, Gdk.ModifierType.CONTROL_MASK),
        ),
        # 2) `Ctrl+a` shouldn't change
        (
            (Gdk.KEY_a, Gdk.ModifierType.CONTROL_MASK, 38),
            (Gdk.KEY_a, Gdk.ModifierType.CONTROL_MASK),
        ),
        # 3) `Ctrl+Shift+a` shouldn't change
        #((Gdk.KEY_a, CONTROL_SHIFT_MOD, 38), (Gdk.KEY_a, CONTROL_SHIFT_MOD),),
        # 4) `Ctrl+Shift+Alt+F1` shouldn't change
        (
            (Gdk.KEY_F1, CONTROL_ALT_SHIFT_MOD, 67),
            (Gdk.KEY_F1, CONTROL_ALT_SHIFT_MOD),
        ),
        # 5) `Shift+Up` shouldn't change
        (
            (Gdk.KEY_Up, Gdk.ModifierType.SHIFT_MASK, 111),
            (Gdk.KEY_Up, Gdk.ModifierType.SHIFT_MASK),
        ),
        # 6) `Ctrl+Shift+[` should become `Ctrl+{`
        (
            (Gdk.KEY_bracketleft, CONTROL_SHIFT_MOD, 34),
            (Gdk.KEY_braceleft, Gdk.ModifierType.CONTROL_MASK),
        ),
        # 7) `Shift+Tab` shouldn't change
        (
            (Gdk.KEY_Tab, Gdk.ModifierType.SHIFT_MASK, 23),
            (Gdk.KEY_Tab, Gdk.ModifierType.SHIFT_MASK),
        ),
    ],
)
def test_keybinding_edit_produce_expected_accels(
    input_key_params, expected_accel
):
    """
    Tests that editing a key binding using a predefined key combination
    `input_key_params` produces the expected accelerator.
    """
    from terminatorlib import terminal
    from terminatorlib import prefseditor

    term = terminal.Terminal()
    prefs_editor = prefseditor.PrefsEditor(term=term)

    widget = prefs_editor.builder.get_object("keybindingtreeview")
    treemodelfilter = widget.get_model()
    liststore = treemodelfilter.get_model()

    path = 0  # Edit the first listed key binding in `Preferences>Keybindings`
    key, mods, hardware_keycode = input_key_params

    prefs_editor.on_cellrenderer_accel_edited(
        liststore=liststore,
        path=str(path),
        key=key,
        mods=mods,
        _code=hardware_keycode,
    )

    liststore_iter = liststore.get_iter(path)
    keyval_after_edit = liststore.get_value(liststore_iter, 2)
    mods_after_edit = Gdk.ModifierType(liststore.get_value(liststore_iter, 3))

    accel_after_edit = (keyval_after_edit, mods_after_edit)

    assert accel_after_edit == expected_accel

    reset_config_keybindings()


@pytest.mark.parametrize(
    "accel_params",
    [
        # Format: (path, key, mods, hardware_keycode)
        # 1) 'broadcast_all' 1
        ("0", Gdk.KEY_1, Gdk.ModifierType(0), 10),
        # 2) 'broadcast_group' Ctrl+Alt+Shift+Up
        ("1", Gdk.KEY_Up, CONTROL_ALT_SHIFT_MOD, 111),
    ],
)
def test_keybinding_successfully_reassigned_after_clearing(accel_params):
    """
    Tests that a key binding is successfully reassigned after it has been cleared,
    that is no error is thrown at any stage.
    """
    from terminatorlib import terminal
    from terminatorlib import prefseditor

    term = terminal.Terminal()
    prefs_editor = prefseditor.PrefsEditor(term=term)

    widget = prefs_editor.builder.get_object("keybindingtreeview")
    treemodelfilter = widget.get_model()
    liststore = treemodelfilter.get_model()

    path, key, mods, hardware_keycode = accel_params
    # Assign a key binding
    prefs_editor.on_cellrenderer_accel_edited(
        liststore=liststore,
        path=path,
        key=key,
        mods=mods,
        _code=hardware_keycode,
    )
    # Clear the key binding
    prefs_editor.on_cellrenderer_accel_cleared(liststore=liststore, path=path)
    # Reassign the key binding
    prefs_editor.on_cellrenderer_accel_edited(
        liststore=liststore,
        path=path,
        key=key,
        mods=mods,
        _code=hardware_keycode,
    )

    reset_config_keybindings()