Skip to content

Commit b45f7ef

Browse files
authored
Add Line tool tests for drawing within a transformed artboard (#2572)
* Adding tests for artboard with transform * Hypercube changes suggested
1 parent 1a5bef1 commit b45f7ef

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

editor/src/messages/tool/tool_messages/line_tool.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,9 @@ fn generate_line(tool_data: &mut LineToolData, snap_data: SnapData, lock_angle:
432432

433433
#[cfg(test)]
434434
mod test_line_tool {
435+
use crate::messages::portfolio::document::graph_operation::utility_types::TransformIn;
435436
use crate::{messages::tool::common_functionality::graph_modification_utils::NodeGraphLayer, test_utils::test_prelude::*};
437+
use glam::DAffine2;
436438
use graph_craft::document::value::TaggedValue;
437439

438440
async fn get_line_node_inputs(editor: &mut EditorTestUtils) -> Option<(DVec2, DVec2)> {
@@ -561,4 +563,42 @@ mod test_line_tool {
561563
}
562564
}
563565
}
566+
567+
#[tokio::test]
568+
async fn test_line_tool_with_transformed_artboard() {
569+
let mut editor = EditorTestUtils::create();
570+
editor.new_document().await;
571+
editor.drag_tool(ToolType::Artboard, 0., 0., 200., 200., ModifierKeys::empty()).await;
572+
573+
let artboard_id = editor.get_selected_layer().await.expect("Should have selected the artboard");
574+
575+
editor
576+
.handle_message(GraphOperationMessage::TransformChange {
577+
layer: artboard_id,
578+
transform: DAffine2::from_angle(45.0_f64.to_radians()),
579+
transform_in: TransformIn::Local,
580+
skip_rerender: false,
581+
})
582+
.await;
583+
584+
editor.drag_tool(ToolType::Line, 50., 50., 150., 150., ModifierKeys::empty()).await;
585+
586+
let (start_input, end_input) = get_line_node_inputs(&mut editor).await.expect("Line was not created successfully within transformed artboard");
587+
// The line should still be diagonal with equal change in x and y
588+
let line_vector = end_input - start_input;
589+
// Verifying the line is approximately 100*sqrt(2) units in length (diagonal of 100x100 square)
590+
let line_length = line_vector.length();
591+
assert!(
592+
(line_length - 141.42).abs() < 1.0, // 100 * sqrt(2) ~= 141.42
593+
"Line length should be approximately 141.42 units. Got: {line_length}"
594+
);
595+
assert!((line_vector.x - 100.0).abs() < 1.0, "X-component of line vector should be approximately 100. Got: {}", line_vector.x);
596+
assert!(
597+
(line_vector.y.abs() - 100.0).abs() < 1.0,
598+
"Absolute Y-component of line vector should be approximately 100. Got: {}",
599+
line_vector.y.abs()
600+
);
601+
let angle_degrees = line_vector.angle_to(DVec2::X).to_degrees();
602+
assert!((angle_degrees - (-45.0)).abs() < 1.0, "Line angle should be close to -45 degrees. Got: {angle_degrees}");
603+
}
564604
}

editor/src/test_utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::messages::tool::tool_messages::tool_prelude::Key;
88
use crate::messages::tool::utility_types::ToolType;
99
use crate::node_graph_executor::Instrumented;
1010
use crate::node_graph_executor::NodeRuntime;
11+
use crate::test_utils::test_prelude::LayerNodeIdentifier;
1112
use glam::DVec2;
1213
use graph_craft::document::DocumentNode;
1314
use graphene_core::InputAccessor;
@@ -239,6 +240,9 @@ impl EditorTestUtils {
239240

240241
self.press(Key::Enter, ModifierKeys::empty()).await;
241242
}
243+
pub async fn get_selected_layer(&mut self) -> Option<LayerNodeIdentifier> {
244+
self.active_document().network_interface.selected_nodes().selected_layers(self.active_document().metadata()).next()
245+
}
242246
}
243247

244248
pub trait FrontendMessageTestUtils {

0 commit comments

Comments
 (0)