Skip to content

Commit

Permalink
Update comments, add more test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Kyle Rich <[email protected]>
  • Loading branch information
malikoth committed Apr 24, 2023
1 parent cf692a5 commit 29f560d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
9 changes: 8 additions & 1 deletion core/src/toga/widgets/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,32 @@ def remove(self, *children):
Any nominated child widget that is not a child of this widget will
not have any change in parentage.
Refreshes the widget after removal if any children were removed.
Raises ``ValueError`` if this widget cannot have children.
:param children: The child nodes to remove.
"""
removed = False

for child in children:
if child.parent is self:
removed = True
super().remove(child)

child.app = None
child.window = None

self._impl.remove_child(child._impl)

if self.window:
if self.window and removed:
self.window.content.refresh()

def clear(self):
"""Remove all child widgets of this node.
Refreshes the widget after removal if any children were removed.
Raises ``ValueError`` if this widget cannot have children.
"""
self.remove(*self.children)
Expand Down
43 changes: 41 additions & 2 deletions core/tests/widgets/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ def test_clear_all_children(widget):
assert child.app == app
assert child.window == window

# Remove children
# Clear children
widget.clear()

# Parent doesn't know about the removed children, and vice versa
Expand All @@ -655,7 +655,7 @@ def test_clear_all_children(widget):
assert child3.app is None
assert child3.window is None

# The impl's remove_child has been invoked twice
# The impl's remove_child has been invoked thrice
assert_action_performed_with(widget, "remove child", child=child1._impl)
assert_action_performed_with(widget, "remove child", child=child2._impl)
assert_action_performed_with(widget, "remove child", child=child3._impl)
Expand All @@ -664,6 +664,45 @@ def test_clear_all_children(widget):
window.content.refresh.assert_called_once_with()


def test_clear_no_children(widget):
"No changes are made (no-op) if widget has no children"
app = toga.App("Test", "com.example.test")
window = Mock()
widget.app = app
widget.window = window

assert widget.children == []

# Clear children
widget.clear()

# Parent doesn't have any children still
assert widget.children == []

# The window layout has not been refreshed
window.content.refresh.assert_not_called()


def test_clear_leaf_node():
"No changes are made to leaf node that cannot have children"
leaf = TestLeafWidget()
app = toga.App("Test", "com.example.test")
window = Mock()
leaf.app = app
leaf.window = window

assert leaf.children == []

# Clear children
leaf.clear()

# Parent doesn't have any children still
assert leaf.children == []

# The window layout has not been refreshed
window.content.refresh.assert_not_called()


def test_remove_from_non_parent(widget):
"Trying to remove a child from a widget other than it's parent is a no-op"
# Create a second parent widget, and add a child to it
Expand Down

0 comments on commit 29f560d

Please sign in to comment.