Skip to content

Commit 9acc23d

Browse files
Add Gradient tool test for dragging the stop rearranges the ordering (#2565)
1 parent af4f57e commit 9acc23d

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

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

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,4 +783,96 @@ mod test_gradient {
783783
let updated_end = transform.transform_point2(updated_gradient.end);
784784
assert!(updated_end.abs_diff_eq(DVec2::new(100., 50.), 1e-10), "Expected end point at (100, 50), got {:?}", updated_end);
785785
}
786+
787+
#[tokio::test]
788+
async fn dragging_stop_reorders_gradient() {
789+
let mut editor = EditorTestUtils::create();
790+
editor.new_document().await;
791+
792+
editor.drag_tool(ToolType::Rectangle, -5., -3., 100., 100., ModifierKeys::empty()).await;
793+
794+
editor.select_primary_color(Color::GREEN).await;
795+
editor.select_secondary_color(Color::BLUE).await;
796+
797+
editor.drag_tool(ToolType::Gradient, 0., 0., 100., 0., ModifierKeys::empty()).await;
798+
799+
editor.select_tool(ToolType::Gradient).await;
800+
let click_position = DVec2::new(50., 0.);
801+
let modifier_keys = ModifierKeys::empty();
802+
803+
editor
804+
.handle_message(InputPreprocessorMessage::DoubleClick {
805+
editor_mouse_state: EditorMouseState {
806+
editor_position: click_position,
807+
mouse_keys: MouseKeys::LEFT,
808+
scroll_delta: ScrollDelta::default(),
809+
},
810+
modifier_keys,
811+
})
812+
.await;
813+
814+
let fills = get_fills(&mut editor).await;
815+
assert_eq!(fills.len(), 1);
816+
let (initial_fill, _) = fills.first().unwrap();
817+
let initial_gradient = initial_fill.as_gradient().unwrap();
818+
assert_eq!(initial_gradient.stops.len(), 3);
819+
820+
// Verify initial stop positions and colors
821+
let mut stops = initial_gradient.stops.clone();
822+
stops.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
823+
824+
assert!((stops[0].0 - 0.0).abs() < 0.001);
825+
assert!((stops[1].0 - 0.5).abs() < 0.1);
826+
assert!((stops[2].0 - 1.0).abs() < 0.001);
827+
828+
let middle_color = stops[1].1.to_rgba8_srgb();
829+
830+
// Simulate dragging the middle stop to position 0.8
831+
editor.select_tool(ToolType::Gradient).await;
832+
833+
editor
834+
.mousedown(
835+
EditorMouseState {
836+
editor_position: click_position,
837+
mouse_keys: MouseKeys::LEFT,
838+
scroll_delta: ScrollDelta::default(),
839+
},
840+
ModifierKeys::empty(),
841+
)
842+
.await;
843+
844+
let drag_position = DVec2::new(80., 0.);
845+
editor.move_mouse(drag_position.x, drag_position.y, ModifierKeys::empty(), MouseKeys::LEFT).await;
846+
847+
editor
848+
.mouseup(
849+
EditorMouseState {
850+
editor_position: drag_position,
851+
mouse_keys: MouseKeys::empty(),
852+
scroll_delta: ScrollDelta::default(),
853+
},
854+
ModifierKeys::empty(),
855+
)
856+
.await;
857+
858+
let fills_after_drag = get_fills(&mut editor).await;
859+
assert_eq!(fills_after_drag.len(), 1);
860+
let (updated_fill, _) = fills_after_drag.first().unwrap();
861+
let updated_gradient = updated_fill.as_gradient().unwrap();
862+
assert_eq!(updated_gradient.stops.len(), 3);
863+
864+
// Verify updated stop positions and colors
865+
let mut updated_stops = updated_gradient.stops.clone();
866+
updated_stops.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
867+
868+
// Check positions are now correctly ordered
869+
assert!((updated_stops[0].0 - 0.0).abs() < 0.001);
870+
assert!((updated_stops[1].0 - 0.8).abs() < 0.1);
871+
assert!((updated_stops[2].0 - 1.0).abs() < 0.001);
872+
873+
// Colors should maintain their associations with the stop points
874+
assert_eq!(updated_stops[0].1.to_rgba8_srgb(), Color::BLUE.to_rgba8_srgb());
875+
assert_eq!(updated_stops[1].1.to_rgba8_srgb(), middle_color);
876+
assert_eq!(updated_stops[2].1.to_rgba8_srgb(), Color::GREEN.to_rgba8_srgb());
877+
}
786878
}

0 commit comments

Comments
 (0)