File: test_focus_traversal.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (120 lines) | stat: -rw-r--r-- 3,596 bytes parent folder | download
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
#------------------------------------------------------------------------------
# Copyright (c) 2020-2024, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
"""Test the focus traversal functionalities

"""
import os
import sys

import pytest

from utils import compile_source, wait_for_window_displayed


SOURCE ="""
from enaml.widgets.api import Window, GroupBox, Field, Container, Feature, FocusTracker


enamldef LinkField(Field):
    attr next_field
    attr prev_field


enamldef Main(Window):
    title = 'Focus Traversal'

    alias tracker
    alias f1
    alias f2
    alias f3
    alias f4
    alias f5
    alias f6
    alias f7

    FocusTracker: tracker:
        pass

    Container:
        features = Feature.FocusTraversal

        next_focus_child => (current):      # triggered on Tab
            child = getattr(current, 'next_field', None)
            return child

        previous_focus_child => (current):  # triggered on Shift+Tab
            child = getattr(current, 'prev_field', None)
            return child

        GroupBox:
            title = 'First Group'
            LinkField: f1:
                placeholder = '1'
                next_field = f4
                prev_field = f5
            LinkField: f2:
                placeholder = '5'
                next_field = f7
                prev_field = f6
            LinkField: f3:
                placeholder = '3'
                next_field = f6
                prev_field = f4
            LinkField: f4:
                placeholder = '2'
                next_field = f3
                prev_field = f1
        GroupBox:
            title = 'Second Group'
            LinkField: f5:
                placeholder = '7'
                next_field = f1
                prev_field = f7
            LinkField: f6:
                placeholder = '4'
                next_field = f2
                prev_field = f3
            LinkField: f7:
                placeholder = '6'
                next_field = f5
                prev_field = f2

"""

@pytest.mark.parametrize("widgets, mods",
                         [(["f4", "f3", "f6", "f2", "f7", "f5", "f1"], [False]*7),
                          (["f5", "f7", "f2", "f6", "f3", "f4", "f1"], [True]*7),
                          (["f4", "f1", "f5", "f7", "f5", "f1", "f4", "f3", "f4"],
                           [False, True, True, True, False, False, False, False, True]
                           )
                         ]
                        )
def test_focus_traversal(enaml_qtbot, enaml_sleep, widgets, mods):
    """Test moving the focus forward in the presence of a custom focus traversal.

    """
    from enaml.qt import QtCore

    win = compile_source(SOURCE, 'Main')()
    win.show()
    wait_for_window_displayed(enaml_qtbot, win)

    enaml_qtbot.mouseClick(win.f1.proxy.widget, QtCore.Qt.LeftButton)
    assert win.tracker.focused_widget is win.f1

    for w, mod in zip(widgets, mods):
        # If we do not send the key press to the focused widget we can get aberrant
        # behavior, typically if we send the key press to the window the custom
        # behavior of enaml will be bypassed.
        enaml_qtbot.keyClick(
            win.tracker.focused_widget.proxy.widget,
            QtCore.Qt.Key_Tab,
            QtCore.Qt.ShiftModifier if mod else QtCore.Qt.NoModifier
        )
        assert win.tracker.focused_widget is getattr(win, w)