Add Spline tool test for drawing with PTZ applied to the canvas view (#2573)

* Adding tests for spline PTZ

* Some refactoring

* Feedback applied from James

* Unnecessary changes

* refactor with current merged pr

* Code refactoring based on guidelines

* cli failing
This commit is contained in:
Rahat
2025-04-24 20:39:33 +01:00
committed by GitHub
parent e60a9c36d8
commit 2dee47a6ee
4 changed files with 211 additions and 52 deletions
@@ -462,8 +462,8 @@ mod test_line_tool {
if let Some((start_input, end_input)) = get_line_node_inputs(&mut editor).await {
match (start_input, end_input) {
(start_input, end_input) => {
assert!((start_input - DVec2::ZERO).length() < 1.0, "Start point should be near (0,0)");
assert!((end_input - DVec2::new(100.0, 100.0)).length() < 1.0, "End point should be near (100,100)");
assert!((start_input - DVec2::ZERO).length() < 1., "Start point should be near (0,0)");
assert!((end_input - DVec2::new(100., 100.)).length() < 1., "End point should be near (100,100)");
}
}
}
@@ -473,9 +473,13 @@ mod test_line_tool {
async fn test_line_tool_with_transformed_viewport() {
let mut editor = EditorTestUtils::create();
editor.new_document().await;
editor.handle_message(NavigationMessage::CanvasZoomSet { zoom_factor: 2.0 }).await;
editor.handle_message(NavigationMessage::CanvasPan { delta: DVec2::new(100.0, 50.0) }).await;
editor.handle_message(NavigationMessage::CanvasTiltSet { angle_radians: 30.0_f64.to_radians() }).await;
editor.handle_message(NavigationMessage::CanvasZoomSet { zoom_factor: 2. }).await;
editor.handle_message(NavigationMessage::CanvasPan { delta: DVec2::new(100., 50.) }).await;
editor
.handle_message(NavigationMessage::CanvasTiltSet {
angle_radians: (30. as f64).to_radians(),
})
.await;
editor.drag_tool(ToolType::Line, 0., 0., 100., 100., ModifierKeys::empty()).await;
if let Some((start_input, end_input)) = get_line_node_inputs(&mut editor).await {
let document = editor.active_document();
@@ -483,16 +487,16 @@ mod test_line_tool {
let viewport_to_document = document_to_viewport.inverse();
let expected_start = viewport_to_document.transform_point2(DVec2::ZERO);
let expected_end = viewport_to_document.transform_point2(DVec2::new(100.0, 100.0));
let expected_end = viewport_to_document.transform_point2(DVec2::new(100., 100.));
assert!(
(start_input - expected_start).length() < 1.0,
(start_input - expected_start).length() < 1.,
"Start point should match expected document coordinates. Got {:?}, expected {:?}",
start_input,
expected_start
);
assert!(
(end_input - expected_end).length() < 1.0,
(end_input - expected_end).length() < 1.,
"End point should match expected document coordinates. Got {:?}, expected {:?}",
end_input,
expected_end
@@ -519,7 +523,7 @@ mod test_line_tool {
let updated_line_vec = updated_end - updated_start;
let updated_angle = updated_line_vec.angle_to(DVec2::X);
assert!((original_angle - updated_angle).abs() < 0.1, "Line angle should be locked when Ctrl is kept pressed");
assert!((updated_start - updated_end).length() > 1.0, "Line should be able to change length when Ctrl is kept pressed");
assert!((updated_start - updated_end).length() > 1., "Line should be able to change length when Ctrl is kept pressed");
}
}
}
@@ -538,8 +542,8 @@ mod test_line_tool {
(start_input, end_input) => {
let expected_start = DVec2::new(0., 100.);
let expected_end = DVec2::new(200., 100.);
assert!((start_input - expected_start).length() < 1.0, "start point should be near (0,100)");
assert!((end_input - expected_end).length() < 1.0, "end point should be near (200,100)");
assert!((start_input - expected_start).length() < 1., "start point should be near (0,100)");
assert!((end_input - expected_end).length() < 1., "end point should be near (200,100)");
}
}
}
@@ -556,9 +560,9 @@ mod test_line_tool {
let line_vec = end_input - start_input;
let angle_radians = line_vec.angle_to(DVec2::X);
let angle_degrees = angle_radians.to_degrees();
let nearest_angle = (angle_degrees / 15.0).round() * 15.0;
let nearest_angle = (angle_degrees / 15.).round() * 15.;
assert!((angle_degrees - nearest_angle).abs() < 1.0, "Angle should snap to the nearest 15 degrees");
assert!((angle_degrees - nearest_angle).abs() < 1., "Angle should snap to the nearest 15 degrees");
}
}
}
@@ -648,4 +648,155 @@ mod test_spline_tool {
assert_point_positions(&extended_vector_data, layer_to_viewport, &all_expected_points, 1e-10);
}
#[tokio::test]
async fn test_spline_with_zoomed_view() {
let mut editor = EditorTestUtils::create();
editor.new_document().await;
// Zooming the viewport
editor.handle_message(NavigationMessage::CanvasZoomSet { zoom_factor: 2.0 }).await;
// Selecting the spline tool
editor.select_tool(ToolType::Spline).await;
// Adding points by clicking at different positions
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(50.0, 50.0), ModifierKeys::empty()).await;
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(100.0, 50.0), ModifierKeys::empty()).await;
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(150.0, 100.0), ModifierKeys::empty()).await;
// Finish the spline
editor.handle_message(SplineToolMessage::Confirm).await;
// Evaluate the graph to ensure everything is processed
editor.eval_graph().await;
// Get the layer and vector data
let document = editor.active_document();
let network_interface = &document.network_interface;
let layer = network_interface
.selected_nodes()
.selected_visible_and_unlocked_layers(network_interface)
.next()
.expect("Should have a selected layer");
let vector_data = network_interface.compute_modified_vector(layer).expect("Should have vector data");
let layer_to_viewport = document.metadata().transform_to_viewport(layer);
// Expected points in viewport coordinates
let expected_points = vec![DVec2::new(50.0, 50.0), DVec2::new(100.0, 50.0), DVec2::new(150.0, 100.0)];
// Assert all points are correctly positioned
assert_point_positions(&vector_data, layer_to_viewport, &expected_points, 1e-10);
}
#[tokio::test]
async fn test_spline_with_panned_view() {
let mut editor = EditorTestUtils::create();
editor.new_document().await;
let pan_amount = DVec2::new(200.0, 150.0);
editor.handle_message(NavigationMessage::CanvasPan { delta: pan_amount }).await;
editor.select_tool(ToolType::Spline).await;
// Add points by clicking at different positions
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(50.0, 50.0), ModifierKeys::empty()).await;
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(100.0, 50.0), ModifierKeys::empty()).await;
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(150.0, 100.0), ModifierKeys::empty()).await;
editor.handle_message(SplineToolMessage::Confirm).await;
// Evaluating the graph to ensure everything is processed
editor.eval_graph().await;
// Get the layer and vector data
let document = editor.active_document();
let network_interface = &document.network_interface;
let layer = network_interface
.selected_nodes()
.selected_visible_and_unlocked_layers(network_interface)
.next()
.expect("Should have a selected layer");
let vector_data = network_interface.compute_modified_vector(layer).expect("Should have vector data");
let layer_to_viewport = document.metadata().transform_to_viewport(layer);
// Expected points in viewport coordinates
let expected_points = vec![DVec2::new(50.0, 50.0), DVec2::new(100.0, 50.0), DVec2::new(150.0, 100.0)];
// Assert all points are correctly positioned
assert_point_positions(&vector_data, layer_to_viewport, &expected_points, 1e-10);
}
#[tokio::test]
async fn test_spline_with_tilted_view() {
let mut editor = EditorTestUtils::create();
editor.new_document().await;
// Tilt/rotate the viewport (45 degrees)
editor.handle_message(NavigationMessage::CanvasTiltSet { angle_radians: 45.0_f64.to_radians() }).await;
editor.select_tool(ToolType::Spline).await;
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(50.0, 50.0), ModifierKeys::empty()).await;
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(100.0, 50.0), ModifierKeys::empty()).await;
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(150.0, 100.0), ModifierKeys::empty()).await;
editor.handle_message(SplineToolMessage::Confirm).await;
// Evaluating the graph to ensure everything is processed
editor.eval_graph().await;
// Get the layer and vector data
let document = editor.active_document();
let network_interface = &document.network_interface;
let layer = network_interface
.selected_nodes()
.selected_visible_and_unlocked_layers(network_interface)
.next()
.expect("Should have a selected layer");
let vector_data = network_interface.compute_modified_vector(layer).expect("Should have vector data");
let layer_to_viewport = document.metadata().transform_to_viewport(layer);
// Expected points in viewport coordinates
let expected_points = vec![DVec2::new(50.0, 50.0), DVec2::new(100.0, 50.0), DVec2::new(150.0, 100.0)];
// Assert all points are correctly positioned
assert_point_positions(&vector_data, layer_to_viewport, &expected_points, 1e-10);
}
#[tokio::test]
async fn test_spline_with_combined_transformations() {
let mut editor = EditorTestUtils::create();
editor.new_document().await;
// Applying multiple transformations
editor.handle_message(NavigationMessage::CanvasZoomSet { zoom_factor: 1.5 }).await;
editor.handle_message(NavigationMessage::CanvasPan { delta: DVec2::new(100.0, 75.0) }).await;
editor.handle_message(NavigationMessage::CanvasTiltSet { angle_radians: 30.0_f64.to_radians() }).await;
editor.select_tool(ToolType::Spline).await;
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(50.0, 50.0), ModifierKeys::empty()).await;
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(100.0, 50.0), ModifierKeys::empty()).await;
editor.click_tool(ToolType::Spline, MouseKeys::LEFT, DVec2::new(150.0, 100.0), ModifierKeys::empty()).await;
editor.handle_message(SplineToolMessage::Confirm).await;
editor.eval_graph().await;
// Get the layer and vector data
let document = editor.active_document();
let network_interface = &document.network_interface;
let layer = network_interface
.selected_nodes()
.selected_visible_and_unlocked_layers(network_interface)
.next()
.expect("Should have a selected layer");
let vector_data = network_interface.compute_modified_vector(layer).expect("Should have vector data");
let layer_to_viewport = document.metadata().transform_to_viewport(layer);
// Expected points in viewport coordinates
let expected_points = vec![DVec2::new(50.0, 50.0), DVec2::new(100.0, 50.0), DVec2::new(150.0, 100.0)];
// Assert all points are correctly positioned
assert_point_positions(&vector_data, layer_to_viewport, &expected_points, 1e-10);
}
}