Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
melonora committed Jul 16, 2024
1 parent 0a9b80e commit 85e94fe
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions napari/layers/shapes/_tests/test_shapes_mouse_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,87 @@ def test_line_fixed_angles(create_known_shapes_layer):
assert np.allclose(theta, 45.0)


def test_polyline_tablet(create_known_shapes_layer):
layer, n_shapes, known_non_shape = create_known_shapes_layer
desired_shape = np.array([[20, 30], [10, 50], [60, 40], [80, 20]])

layer.mode = 'add_polyline'
event = read_only_mouse_event(
type='mouse_press',
is_dragging=True,
position=desired_shape[0],
pos=desired_shape[0],
)
mouse_press_callbacks(layer, event)
assert layer.shape_type[-1] == 'path'

for coord in desired_shape[1:]:
event = read_only_mouse_event(
type='mouse_move',
is_dragging=True,
position=coord,
pos=coord,
)
mouse_move_callbacks(layer, event)

event = read_only_mouse_event(
type='mouse_release',
is_dragging=True,
position=desired_shape[-1],
pos=desired_shape[-1],
)
mouse_release_callbacks(layer, event)

assert len(layer.data) == n_shapes + 1
assert np.array_equal(desired_shape, layer.data[-1])

assert layer.shape_type[-1] == 'path'
assert not layer._is_creating

# Ensure it's selected, accounting for zero-indexing
assert len(layer.selected_data) == 1
assert layer.selected_data == {n_shapes}


def test_polyline_mouse(create_known_shapes_layer):
layer, n_shapes, known_non_shape = create_known_shapes_layer
desired_shape = np.array([[20, 30], [10, 50], [60, 40], [80, 20]])

layer.mode = 'add_polyline'

event = read_only_mouse_event(
type='mouse_press',
position=desired_shape[0],
pos=desired_shape[0],
)
mouse_press_callbacks(layer, event)
assert layer.shape_type[-1] == 'path'

for coord in desired_shape[1:]:
event = read_only_mouse_event(
type='mouse_move',
position=coord,
pos=coord,
)
mouse_move_callbacks(layer, event)

event = read_only_mouse_event(
type='mouse_press',
position=desired_shape[-1],
pos=desired_shape[-1],
)
mouse_press_callbacks(layer, event)

assert len(layer.data) == n_shapes + 1
assert np.array_equal(desired_shape, layer.data[-1])
assert layer.shape_type[-1] == 'path'
assert not layer._is_creating

# Ensure it's selected, accounting for zero-indexing
assert len(layer.selected_data) == 1
assert layer.selected_data == {n_shapes}


def test_polygon_lasso_tablet(create_known_shapes_layer):
"""Draw polygon with tablet simulated by mouse drag event."""
layer, n_shapes, known_non_shape = create_known_shapes_layer
Expand Down

0 comments on commit 85e94fe

Please sign in to comment.