Пример #1
0
def test_manual_reparenting(enaml_qtbot):
    """Test manually reparenting widgets which are previously child and parent.

    """
    win = compile_source(MANUAL_REPARENTING, 'Main')()
    win.show()
    win.send_to_front()
    wait_for_window_displayed(enaml_qtbot, win)
    win.selected_disp = 'C2'
    win.displayables['C2'].btn_clicked = True
Пример #2
0
def test_validation_dock_layout1(enaml_qtbot, enaml_sleep):
    """Test that the validation of a layout.

    We check in particular that the proper warnings are raised and that doing
    so does not corrupt the globals.

    """
    win = compile_source(DOCK_AREA_TEMPLATE, 'Main')()
    win.show()
    wait_for_window_displayed(enaml_qtbot, win)
    enaml_qtbot.wait(enaml_sleep)
    win.area.layout = HSplitLayout('item1', 'item2')
    enaml_qtbot.wait(enaml_sleep)
Пример #3
0
def test_handling_nested_dock_area(enaml_qtbot, enaml_sleep):
    """Test that an inner dock area does not look for item in the outer one.

    """
    win = compile_source(NESTED_DOCK_AREA, 'Main')()
    win.show()
    wait_for_window_displayed(enaml_qtbot, win)
    enaml_qtbot.wait(enaml_sleep)
    dock_item = DockItem(win.inner, name='Inner2', title='Inner2')
    op = InsertTab(item=dock_item.name, target='Inner1', tab_position='top')
    win.inner.update_layout(op)
    enaml_qtbot.wait(enaml_sleep)
    assert dock_item.parent is win.inner
Пример #4
0
def test_validation_dock_layout2(enaml_qtbot, enaml_sleep):
    """Test that the validation of a layout.

    We check in particular that the proper warnings are raised and that doing
    so does not corrupt the globals.

    """
    win = compile_source(DOCK_AREA_TEMPLATE, 'Main')()
    win.show()
    wait_for_window_displayed(enaml_qtbot, win)
    enaml_qtbot.wait(enaml_sleep)
    glob = globals().copy()
    with pytest.warns(DockLayoutWarning):
        win.area.layout = HSplitLayout('item1', 'item2', 'item3')
    assert globals() == glob
    enaml_qtbot.wait(enaml_sleep)
Пример #5
0
def test_examples(enaml_qtbot, enaml_sleep, path, handler):
    """ Test the enaml examples.

    """
    dir_path = os.path.abspath(os.path.split(os.path.dirname(__file__))[0])
    enaml_file = os.path.join(dir_path, 'examples', os.path.normpath(path))

    with open(enaml_file, 'r') as f:
        enaml_code = f.read()

    # Parse and compile the Enaml source into a code object
    ast = parse(enaml_code, filename=enaml_file)
    code = EnamlCompiler.compile(ast, enaml_file)

    # Create a proper module in which to execute the compiled code so
    # that exceptions get reported with better meaning
    try:
        module = types.ModuleType('enaml_test')
        module.__file__ = os.path.abspath(enaml_file)
        sys.modules['enaml_test'] = module
        ns = module.__dict__

        # Put the directory of the Enaml file first in the path so relative
        # imports can work.
        sys.path.insert(0, os.path.abspath(os.path.dirname(enaml_file)))
        with imports():
            exec(code, ns)

        window = ns['Main']()
        window.show()
        window.send_to_front()
        wait_for_window_displayed(enaml_qtbot, window)
        enaml_qtbot.wait(enaml_sleep*1000)

        if handler is not None:
            handler(enaml_qtbot, window)

    finally:
        # Make sure we clean up the sys modification before leaving
        sys.path.pop(0)
        del sys.modules['enaml_test']
Пример #6
0
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)
Пример #7
0
def test_focus_tracking(enaml_qtbot, enaml_sleep):
    """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 in (win.f2, win.f3, win.f4, win.f1):
        enaml_qtbot.keyClick(win.proxy.widget, QtCore.Qt.Key_Tab)
        assert win.tracker.focused_widget is w

    for w in (win.f4, win.f3, win.f2, win.f1):
        enaml_qtbot.keyClick(win.tracker.focused_widget.proxy.widget,
                             QtCore.Qt.Key_Tab, QtCore.Qt.ShiftModifier)
        assert win.tracker.focused_widget is w
Пример #8
0
def test_looper_refresh(enaml_qtbot, enaml_sleep):
    """ Test that the loop index is valid when items are reordered.

    """
    source = dedent("""\
    from enaml.core.api import Looper
    from enaml.widgets.api import Window, Container, Label, PushButton


    enamldef Main(Window): window:
        attr data: list = []
        alias container
        Container: container:
            Looper:
                iterable << window.data
                Label:
                    text << '{} {}'.format(loop.index, loop.item)


    """)
    tester = compile_source(source, 'Main')()
    tester.show()
    wait_for_window_displayed(enaml_qtbot, tester)

    for data in (
            # Set initial
        ['a', 'b', 'c'],

            # Resort order data and ensure it matches
        ['b', 'c', 'a'],

            # Remove and resort ensure it still matches
        ['a', 'b']):
        enaml_qtbot.wait(enaml_sleep)
        tester.data = data
        expected = ['{} {}'.format(*it) for it in enumerate(data)]
        assert [
            c.text for c in tester.container.children if isinstance(c, Label)
        ] == expected
Пример #9
0
def test_attr_type_1(enaml_qtbot, enaml_sleep):
    import datetime
    source = dedent("""\
    import datetime
    from enaml.widgets.api import *

    enamldef Main(Window):
        attr d: datetime.date = datetime.date.today()
        Container:
            Label:
                text << str(d)
    """)
    tester = compile_source(source, 'Main')()
    tester.show()
    wait_for_window_displayed(enaml_qtbot, tester)

    # Should work
    tester.d = (datetime.datetime.now() - datetime.timedelta(days=1)).date()

    # Should raise a type error
    with pytest.raises(TypeError):
        tester.d = datetime.time(7, 0)
Пример #10
0
def test_dock_area_interactions(enaml_qtbot, enaml_sleep):
    """Test interations with the dock area.

    """
    # Since timers are used the sleep must be greater than the default
    enaml_sleep = max(300, enaml_sleep)
    from enaml.qt.QtCore import Qt, QPoint
    from enaml.qt.QtWidgets import QWidget
    from enaml.layout.api import FloatItem, InsertTab

    dir_path = os.path.abspath(os.path.split(os.path.dirname(__file__))[0])
    example_path = os.path.join(dir_path, 'examples', 'widgets',
                                'dock_area.enaml')
    with open(example_path) as f:
        source = f.read()

    win = compile_source(source, 'Main')()
    win.show()

    wait_for_window_displayed(enaml_qtbot, win)
    enaml_qtbot.wait(enaml_sleep)
    _children = win.children[0].children
    btn_save, btn_restore, btn_add, cmb_styles, cbx_evts, dock = _children

    # Enable dock events
    enaml_qtbot.mouseClick(cbx_evts.proxy.widget, Qt.LeftButton)
    enaml_qtbot.wait(enaml_sleep)

    with pytest.warns(DockLayoutWarning):
        # Change styles
        cmb_styles.proxy.widget.setFocus()
        enaml_qtbot.keyClick(cmb_styles.proxy.widget, Qt.Key_Up)

        # Save layout
        enaml_qtbot.mouseClick(btn_save.proxy.widget, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Toggle closable
        di = dock.find('item_2')
        di.closable = False
        enaml_qtbot.wait(enaml_sleep)
        di.closable = True

        # Check alert
        di.alert('info')

        # Maximize it
        tb = di.proxy.widget.titleBarWidget()
        enaml_qtbot.mouseClick(tb._max_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Minimize it
        enaml_qtbot.mouseClick(tb._restore_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Pin it
        enaml_qtbot.mouseClick(tb._pin_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # TODO: Open and close it using clicks
        dock_area = dock.proxy.widget
        for c in dock_area.dockBarContainers():
            dock_bar = c[0]
            dock_area.extendFromDockBar(dock_bar)
            enaml_qtbot.wait(enaml_sleep)
            dock_area.retractToDockBar(dock_bar)
            enaml_qtbot.wait(enaml_sleep)

        # Unpin
        enaml_qtbot.mouseClick(tb._pin_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Note the location of the dock item title
        title = tb._title_label
        ref = win.proxy.widget

        # Maximize and minimize it by double clicking
        pos = title.mapTo(tb, title.pos())
        enaml_qtbot.mouseDClick(tb, Qt.LeftButton, pos=pos)
        enaml_qtbot.wait(enaml_sleep)
        enaml_qtbot.mouseDClick(tb, Qt.LeftButton, pos=pos)
        enaml_qtbot.wait(enaml_sleep)

        # Float it
        op = FloatItem(item=di.name)
        dock.update_layout(op)
        enaml_qtbot.wait(enaml_sleep)

        # Link it
        enaml_qtbot.mouseClick(tb._link_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Unlink it
        enaml_qtbot.mouseClick(tb._link_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Restore it
        op = InsertTab(item=di.name, target='item_1')
        dock.update_layout(op)
        enaml_qtbot.wait(enaml_sleep)

        # TODO: Drag it around

        # Add items
        enaml_qtbot.mouseClick(btn_add.proxy.widget, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Close all added items
        for i in range(10, 15):
            di = dock.find('item_%i' % i)
            if di:
                enaml_qtbot.mouseClick(
                    di.proxy.widget.titleBarWidget()._close_button,
                    Qt.LeftButton)

        # Restore layout
        enaml_qtbot.mouseClick(btn_restore.proxy.widget, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

    enaml_qtbot.wait(enaml_sleep)
Пример #11
0
def assert_window_displays(enaml_qtbot, enaml_sleep, window):
    #: Make sure the imported library actually works
    window.show()
    window.send_to_front()
    wait_for_window_displayed(enaml_qtbot, window)
    enaml_qtbot.wait(enaml_sleep * 1000)
Пример #12
0
def test_dock_area_interactions(enaml_qtbot, enaml_sleep):
    """Test interations with the dock area.

    """
    # Since timers are used the sleep must be greater than the default
    enaml_sleep = max(300, enaml_sleep)
    from enaml.qt.QtCore import Qt, QPoint
    from enaml.qt.QtWidgets import QWidget
    from enaml.layout.api import FloatItem, InsertTab

    dir_path = os.path.abspath(os.path.split(os.path.dirname(__file__))[0])
    example_path = os.path.join(dir_path, 'examples', 'widgets',
                                'dock_area.enaml')
    with open(example_path) as f:
        source = f.read()

    win = compile_source(source, 'Main')()
    win.show()

    wait_for_window_displayed(enaml_qtbot, win)
    enaml_qtbot.wait(enaml_sleep)
    _children = win.children[0].children
    btn_save, btn_restore, btn_add, cmb_styles, cbx_evts, dock = _children

    # Enable dock events
    enaml_qtbot.mouseClick(cbx_evts.proxy.widget, Qt.LeftButton)
    enaml_qtbot.wait(enaml_sleep)

    with pytest.warns(DockLayoutWarning):
        # Change styles
        cmb_styles.proxy.widget.setFocus()
        enaml_qtbot.keyClick(cmb_styles.proxy.widget, Qt.Key_Up)

        # Save layout
        enaml_qtbot.mouseClick(btn_save.proxy.widget, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Toggle closable
        di = dock.find('item_2')
        di.closable = False
        enaml_qtbot.wait(enaml_sleep)
        di.closable = True

        # Check alert
        di.alert('info')

        # Maximize it
        tb = di.proxy.widget.titleBarWidget()
        enaml_qtbot.mouseClick(tb._max_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Minimize it
        enaml_qtbot.mouseClick(tb._restore_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Pin it
        enaml_qtbot.mouseClick(tb._pin_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # TODO: Open and close it using clicks
        dock_area = dock.proxy.widget
        for c in dock_area.dockBarContainers():
            dock_bar = c[0]
            dock_area.extendFromDockBar(dock_bar)
            enaml_qtbot.wait(enaml_sleep)
            dock_area.retractToDockBar(dock_bar)
            enaml_qtbot.wait(enaml_sleep)

        # Unpin
        enaml_qtbot.mouseClick(tb._pin_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Note the location of the dock item title
        title = tb._title_label
        ref = win.proxy.widget

        # Maximize and minimize it by double clicking
        pos = title.mapTo(tb, title.pos())
        enaml_qtbot.mouseDClick(tb, Qt.LeftButton, pos=pos)
        enaml_qtbot.wait(enaml_sleep)
        enaml_qtbot.mouseDClick(tb, Qt.LeftButton, pos=pos)
        enaml_qtbot.wait(enaml_sleep)

        # Float it
        op = FloatItem(item=di.name)
        dock.update_layout(op)
        enaml_qtbot.wait(enaml_sleep)

        # Link it
        enaml_qtbot.mouseClick(tb._link_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Unlink it
        enaml_qtbot.mouseClick(tb._link_button, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Restore it
        op = InsertTab(item=di.name, target='item_1')
        dock.update_layout(op)
        enaml_qtbot.wait(enaml_sleep)

        # TODO: Drag it around

        # Add items
        enaml_qtbot.mouseClick(btn_add.proxy.widget, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

        # Close all added items
        for i in range(10, 15):
            di = dock.find('item_%i'%i)
            if di:
                enaml_qtbot.mouseClick(
                    di.proxy.widget.titleBarWidget()._close_button,
                    Qt.LeftButton)

        # Restore layout
        enaml_qtbot.mouseClick(btn_restore.proxy.widget, Qt.LeftButton)
        enaml_qtbot.wait(enaml_sleep)

    enaml_qtbot.wait(enaml_sleep)
Пример #13
0
def assert_window_displays(enaml_qtbot, enaml_sleep, window):
    #: Make sure the imported library actually works
    window.show()
    window.send_to_front()
    wait_for_window_displayed(enaml_qtbot, window)
    enaml_qtbot.wait(enaml_sleep*1000)