def test_weak_parent_is_dead(location_factory, ignore_parent, has_parent):
    import gc
    import weakref
    from kivy.uix.widget import Widget
    from kivy_garden.draggable import restore_widget_location
    loc = location_factory()
    loc['weak_parent'] = weakref.ref(Widget())
    loc['index'] = 0
    gc.collect()
    assert loc['weak_parent']() is None
    w = Widget()
    if has_parent:
        parent = Widget()
        parent.add_widget(w)
        parent.add_widget(Widget())
        assert parent.children.index(w) == 1
    if ignore_parent:
        restore_widget_location(w, loc, ignore_parent=True)
    else:
        with pytest.raises(ReferenceError):
            restore_widget_location(w, loc, ignore_parent=False)
    if has_parent:
        assert w.parent is parent
        assert parent.children.index(w) == 1
    else:
        assert w.parent is None
Beispiel #2
0
 def on_drag_start(self, touch):
     if self.has_screen('childWhenDragging'):
         restore_widget_location(
             self.get_screen('childWhenDragging'),
             self.drag_context.original_location,
         )
     return super().on_drag_start(touch)
def test_weak_parent_is_none(location_factory, ignore_parent, has_parent):
    from kivy.uix.widget import Widget
    from kivy_garden.draggable import restore_widget_location
    loc = location_factory()
    loc['weak_parent'] = None
    w = Widget()
    if has_parent:
        parent = Widget()
        parent.add_widget(w)
    restore_widget_location(w, loc, ignore_parent=ignore_parent)
    if ignore_parent and has_parent:
        assert w.parent is parent
    else:
        assert w.parent is None
def test_weak_parent_not_in_the_keys(location_factory, ignore_parent,
                                     has_parent):
    from kivy.uix.widget import Widget
    from kivy_garden.draggable import restore_widget_location
    loc = location_factory()
    assert 'weak_parent' not in loc
    w = Widget()
    if has_parent:
        parent = Widget()
        parent.add_widget(w)
    restore_widget_location(w, loc, ignore_parent=ignore_parent)
    if has_parent:
        assert w.parent is parent
    else:
        assert w.parent is None
def test_parent_is_alive():
    from kivy.uix.widget import Widget
    from kivy_garden.draggable import \
        save_widget_location, restore_widget_location
    parent = Widget()
    parent.add_widget(Widget())
    parent.add_widget(Widget())
    w = Widget()
    parent.add_widget(w)
    parent.add_widget(Widget())
    location = save_widget_location(w)
    assert location['index'] == 1
    assert location['weak_parent']() is parent
    parent.remove_widget(w)
    restore_widget_location(w, location)
    assert w.parent is parent
    assert parent.children.index(w) == 1
def test__save_when_widget_has_no_parent__restore_when_widget_has_parent():
    from kivy.uix.widget import Widget
    from kivy_garden.draggable import \
        save_widget_location, restore_widget_location
    w = Widget()
    location = save_widget_location(w)
    assert 'index' not in location
    assert 'weak_parent' not in location
    parent = Widget()
    parent.add_widget(Widget())
    parent.add_widget(Widget())
    parent.add_widget(w)
    parent.add_widget(Widget())
    assert parent.children.index(w) == 1
    restore_widget_location(w, location)
    assert w.parent is parent
    assert parent.children.index(w) == 1
def test_pos_hint_is_isolated():
    from kivy.uix.widget import Widget
    from kivy_garden.draggable import \
        save_widget_location, restore_widget_location
    w = Widget()
    location = save_widget_location(w)
    assert w.pos_hint == {}
    assert location['pos_hint'] == {}
    w.pos_hint['x'] = 0
    assert location['pos_hint'] == {}
    location['pos_hint']['y'] = 0
    restore_widget_location(w, location)
    assert w.pos_hint == {
        'y': 0,
    }
    location['pos_hint']['center_x'] = 0
    assert w.pos_hint == {
        'y': 0,
    }
def test_parent_is_dead():
    import gc
    from kivy.uix.widget import Widget
    from kivy_garden.draggable import \
        save_widget_location, restore_widget_location
    parent = Widget()
    parent.add_widget(Widget())
    parent.add_widget(Widget())
    w = Widget()
    parent.add_widget(w)
    parent.add_widget(Widget())
    location = save_widget_location(w)
    assert location['index'] == 1
    assert location['weak_parent']() is parent
    parent.remove_widget(w)
    del parent
    gc.collect()
    assert location['index'] == 1
    assert location['weak_parent']() is None
    restore_widget_location(w, location)
    assert w.parent is None
def test_sizing_info(location_factory, ignore_parent):
    from kivy.uix.widget import Widget
    from kivy_garden.draggable import restore_widget_location
    w = Widget()
    loc = location_factory()
    restore_widget_location(w, loc, ignore_parent=ignore_parent)
    loc['width'] = 0
    loc['x'] = 0
    loc['size_hint_x'] = 0
    loc['pos_hint']['center'][0] = 0
    loc['size_hint_min_x'] = 0
    loc['size_hint_min_y'] = 0
    assert w.size == [
        2,
        2,
    ]
    assert w.pos == [
        2,
        2,
    ]
    assert w.size_hint == [
        2,
        2,
    ]
    assert w.pos_hint == {
        'center': [
            2,
            2,
        ],
    }
    assert w.size_hint_min == [
        2,
        2,
    ]
    assert w.size_hint_max == [
        2,
        2,
    ]
def test_weak_parent_is_alive(location_factory, ignore_parent, has_parent):
    import weakref
    from kivy.uix.widget import Widget
    from kivy_garden.draggable import restore_widget_location
    prev_parent = Widget()
    loc = location_factory()
    loc['weak_parent'] = weakref.ref(prev_parent)
    loc['index'] = 0
    w = Widget()
    if has_parent:
        parent = Widget()
        parent.add_widget(w)
        parent.add_widget(Widget())
        assert parent.children.index(w) == 1
    restore_widget_location(w, loc, ignore_parent=ignore_parent)
    if ignore_parent:
        if has_parent:
            assert w.parent is parent
            assert parent.children.index(w) == 1
        else:
            assert w.parent is None
    else:
        assert w.parent is prev_parent
        assert prev_parent.children.index(w) == 0
def test_no_parent():
    from kivy.uix.widget import Widget
    from kivy_garden.draggable import \
        save_widget_location, restore_widget_location
    w = Widget()
    location = save_widget_location(w)
    assert location == {
        'x': 0,
        'y': 0,
        'width': 100,
        'height': 100,
        'size_hint_x': 1,
        'size_hint_y': 1,
        'pos_hint': {},
        'size_hint_min_x': None,
        'size_hint_min_y': None,
        'size_hint_max_x': None,
        'size_hint_max_y': None,
    }
    w.pos = [
        20,
        20,
    ]
    w.size = [
        40,
        40,
    ]
    w.size_hint = [
        None,
        None,
    ]
    w.pos_hint = {
        'x': 1,
        'top': .5,
    }
    w.size_hint_min = [
        30,
        30,
    ]
    w.size_hint_max = [
        70,
        70,
    ]
    restore_widget_location(w, location)
    assert w.size == [
        100,
        100,
    ]
    assert w.pos == [
        0,
        0,
    ]
    assert w.size_hint == [
        1,
        1,
    ]
    assert w.pos_hint == {}
    assert w.size_hint_min == [
        None,
        None,
    ]
    assert w.size_hint_max == [
        None,
        None,
    ]
 def cancel_ongoing_drags(self):
     for draggable in tuple(KXDraggableBehavior.ongoing_drags()):
         original_location = draggable.drag_context.original_location
         draggable.drag_cancel()
         restore_widget_location(draggable, original_location)