| 1 | //! Registry consistency: curated schemas, the parsed Fusion catalog and its grouping. |
| 2 | |
| 3 | use super::schemas::{ParamKind, schema_for_kind}; |
| 4 | use super::CURATED_TOOL_KINDS; |
| 5 | use std::collections::HashSet; |
| 6 | |
| 7 | #[test] |
| 8 | fn curated_schemas_cover_all_major_tools() { |
| 9 | let required = [ |
| 10 | // Original 18 |
| 11 | "Merge", "Transform", "Blur", "Glow", "SoftGlow", "BrightnessContrast", |
| 12 | "ColorCorrector", "Background", "TextPlus", "EllipseMask", "RectangleMask", |
| 13 | "Displace", "Vortex", "FastNoise", "UltraKeyer", "Loader", "Camera3D", "Shape3D", |
| 14 | // Wave 9E additions |
| 15 | "Dissolve", "ChannelBoolean", "Saver", "Switch", "Fuse.Switch", |
| 16 | "TimeStretcher", "TimeSpeed", |
| 17 | "Paint", "PaintMask", "WandMask", "BitmapMask", |
| 18 | "Drip", "Dent", "DaySky", "GridWarp", "PerspectivePositioner", |
| 19 | "sRender", "sEllipse", "sRectangle", "sNGon", "sBoolean", "sTransform", "sOutline", "sGrid", |
| 20 | "MtlBlinn", "MtlPhong", "MtlCookTorrance", |
| 21 | "LightPoint", "LightSpot", "LightDirectional", "LightAmbient", |
| 22 | "Cube3D", "SurfaceFBXMesh", "Custom", |
| 23 | "ErodeDilate", "DirectionalBlur", "VariBlur", "Sharpen", "Shadow", |
| 24 | "ColorGain", "HueCurves", "GamutConvert", "FilmGrain", |
| 25 | ]; |
| 26 | for tool in required { |
| 27 | assert!( |
| 28 | CURATED_TOOL_KINDS.contains(&tool), |
| 29 | "{tool} missing from CURATED_TOOL_KINDS" |
| 30 | ); |
| 31 | let s = schema_for_kind(tool); |
| 32 | assert!(!s.is_empty(), "expected non-empty curated schema for {tool}"); |
| 33 | } |
| 34 | for &tool in CURATED_TOOL_KINDS { |
| 35 | assert!( |
| 36 | !schema_for_kind(tool).is_empty(), |
| 37 | "CURATED_TOOL_KINDS entry {tool} has empty schema" |
| 38 | ); |
| 39 | } |
| 40 | assert!(schema_for_kind("UnknownCustomTool").is_empty()); |
| 41 | } |
| 42 | |
| 43 | #[test] |
| 44 | fn curated_schemas_have_unique_params_and_valid_bounds() { |
| 45 | let mut seen_tools = HashSet::new(); |
| 46 | for &tool in CURATED_TOOL_KINDS { |
| 47 | assert!(seen_tools.insert(tool), "duplicate tool in CURATED_TOOL_KINDS: {tool}"); |
| 48 | let schema = schema_for_kind(tool); |
| 49 | let mut names = HashSet::new(); |
| 50 | for spec in schema { |
| 51 | assert!( |
| 52 | names.insert(spec.name), |
| 53 | "duplicate parameter `{}` in schema for `{tool}`", |
| 54 | spec.name |
| 55 | ); |
| 56 | match spec.kind { |
| 57 | ParamKind::Float { min, max, default, step } => { |
| 58 | assert!(min.is_finite() && max.is_finite() && default.is_finite() && step.is_finite()); |
| 59 | assert!(min <= max, "{tool}.{}: min ({min}) > max ({max})", spec.name); |
| 60 | assert!(step > 0.0, "{tool}.{}: step ({step}) <= 0", spec.name); |
| 61 | assert!( |
| 62 | (min..=max).contains(&default), |
| 63 | "{tool}.{}: default ({default}) outside [{min}, {max}]", |
| 64 | spec.name |
| 65 | ); |
| 66 | } |
| 67 | ParamKind::Vec2 { min, max, default } => { |
| 68 | assert!(min.is_finite() && max.is_finite()); |
| 69 | assert!(min <= max, "{tool}.{}: min ({min}) > max ({max})", spec.name); |
| 70 | for (axis, d) in default.iter().enumerate() { |
| 71 | assert!( |
| 72 | d.is_finite() && (min..=max).contains(d), |
| 73 | "{tool}.{}[{axis}]: default ({d}) outside [{min}, {max}]", |
| 74 | spec.name |
| 75 | ); |
| 76 | } |
| 77 | } |
| 78 | ParamKind::Enum { options, default } => { |
| 79 | assert!(!options.is_empty(), "{tool}.{}: empty Enum options", spec.name); |
| 80 | assert!( |
| 81 | options.contains(&default), |
| 82 | "{tool}.{}: default `{default}` not in options {options:?}", |
| 83 | spec.name |
| 84 | ); |
| 85 | } |
| 86 | ParamKind::IntEnum { options, default } => { |
| 87 | assert!(!options.is_empty(), "{tool}.{}: empty IntEnum options", spec.name); |
| 88 | let mut codes = HashSet::new(); |
| 89 | for &(code, _) in options { |
| 90 | assert!( |
| 91 | codes.insert(code), |
| 92 | "{tool}.{}: duplicate IntEnum code {code}", |
| 93 | spec.name |
| 94 | ); |
| 95 | } |
| 96 | assert!( |
| 97 | codes.contains(&default), |
| 98 | "{tool}.{}: default {default} not in IntEnum options", |
| 99 | spec.name |
| 100 | ); |
| 101 | } |
| 102 | ParamKind::Color { r, g, b, a, default } => { |
| 103 | for ch in [r, g, b].into_iter().chain(a) { |
| 104 | assert!( |
| 105 | names.insert(ch), |
| 106 | "duplicate color channel `{ch}` in schema for `{tool}`" |
| 107 | ); |
| 108 | } |
| 109 | for (idx, c) in default.iter().enumerate() { |
| 110 | assert!( |
| 111 | c.is_finite() && (0.0..=1.0).contains(c), |
| 112 | "{tool}.{}[{idx}]: color default ({c}) outside [0, 1]", |
| 113 | spec.name |
| 114 | ); |
| 115 | } |
| 116 | } |
| 117 | ParamKind::Bool { .. } | ParamKind::Text { .. } => {} |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | mod catalog_layout { |
| 124 | use super::super::catalog::{self, catalog}; |
| 125 | use super::super::layout::{ControlDef, Kind, SECTIONS, ToolLayout}; |
| 126 | use super::super::{InspectorState, dynamic}; |
| 127 | use super::{CURATED_TOOL_KINDS, HashSet, schema_for_kind}; |
| 128 | use model::{Comp, CompHistory}; |
| 129 | use std::time::Instant; |
| 130 | |
| 131 | fn controls(layout: &ToolLayout) -> impl Iterator<Item = &ControlDef> { |
| 132 | layout.pages.iter().flat_map(|p| &p.sections).flat_map(|s| &s.controls) |
| 133 | } |
| 134 | |
| 135 | fn find<'a>(tool: &str, first_id: &str) -> &'a ControlDef { |
| 136 | let layout = &catalog()[tool].layout; |
| 137 | let found = controls(layout).find(|c| c.channels[0].id == first_id); |
| 138 | found.unwrap_or_else(|| panic!("{tool} has no row starting at {first_id}")) |
| 139 | } |
| 140 | |
| 141 | fn ids(def: &ControlDef) -> Vec<&str> { |
| 142 | def.channels.iter().map(|c| c.id.as_str()).collect() |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn catalog_has_every_introspected_tool() { |
| 147 | let cat = catalog(); |
| 148 | assert!(cat.len() > 100, "only {} tools", cat.len()); |
| 149 | for tool in ["Merge", "Transform", "Background", "TextPlus", "Blur", "MacroOperator", "GroupOperator"] { |
| 150 | assert!(cat.contains_key(tool), "{tool} missing from catalog"); |
| 151 | } |
| 152 | assert!(catalog::ready().is_some()); |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn rgba_color_inputs_fuse_into_one_row() { |
| 157 | let def = find("Background", "TopLeftRed"); |
| 158 | assert_eq!((def.kind, def.label.as_str()), (Kind::Color, "Top Left")); |
| 159 | assert_eq!(ids(def), ["TopLeftRed", "TopLeftGreen", "TopLeftBlue", "TopLeftAlpha"]); |
| 160 | assert_eq!(def.channels[3].default[0], 1.0); |
| 161 | |
| 162 | let def = find("TextPlus", "Red1"); |
| 163 | assert_eq!((def.kind, ids(def)), (Kind::Color, vec!["Red1", "Green1", "Blue1", "Alpha1"])); |
| 164 | |
| 165 | // Object/Material IDs share a color group but are lone ids, not RGB. |
| 166 | assert_eq!(find("Merge", "ObjectID").kind, Kind::Slider); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn range_inputs_fuse_into_one_row() { |
| 171 | let def = find("TextPlus", "Start"); |
| 172 | assert_eq!((def.kind, ids(def)), (Kind::Range, vec!["Start", "End"])); |
| 173 | assert_eq!(def.label, "Write On Start / End"); |
| 174 | assert_eq!(ids(find("Merge", "MaskLow")), ["MaskLow", "MaskHigh"]); |
| 175 | // Two Solid/Garbage ranges in one group stay two rows. |
| 176 | let keyer = controls(&catalog()["UltraKeyer"].layout).filter(|c| c.kind == Kind::Range && c.channels[0].id.contains(".MaskLow")); |
| 177 | assert_eq!(keyer.count(), 2); |
| 178 | } |
| 179 | |
| 180 | #[test] |
| 181 | fn angles_are_screws_and_toggles_group() { |
| 182 | assert_eq!(find("Merge", "Angle").kind, Kind::Screw); |
| 183 | assert_eq!(find("Transform", "Angle").kind, Kind::Screw); |
| 184 | let process = find("Merge", "ProcessRed"); |
| 185 | assert_eq!((process.kind, process.label.as_str()), (Kind::Toggles, "Process")); |
| 186 | assert_eq!(process.entries, ["Red", "Green", "Blue", "Alpha"]); |
| 187 | assert_eq!(find("Merge", "Center").kind, Kind::Point); |
| 188 | assert_eq!(find("Merge", "ApplyMode").kind, Kind::Combo); |
| 189 | assert!(find("Merge", "ApplyMode").fuid); |
| 190 | assert_eq!(find("Background", "UseGPU").kind, Kind::Segmented); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn pages_follow_fusion_tab_order() { |
| 195 | let names = |tool: &str| -> Vec<&str> { catalog()[tool].layout.pages.iter().map(|p| p.name.as_str()).collect() }; |
| 196 | assert_eq!(names("Merge"), ["Merge", "Channels", "Settings"]); |
| 197 | assert_eq!(names("Transform"), ["Controls", "Settings"]); |
| 198 | assert_eq!(names("TextPlus"), ["Text", "Layout", "Transform", "Shading", "Image", "Settings"]); |
| 199 | let merge = &catalog()["Merge"].layout.pages[0]; |
| 200 | assert_eq!(merge.sections.iter().map(|s| s.title.as_str()).collect::<Vec<_>>(), ["Merge", "Reference Size"]); |
| 201 | assert!(!merge.sections[1].open); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn section_markers_name_real_inputs() { |
| 206 | for (tools, id, title, _) in SECTIONS { |
| 207 | let hits = |tool: &str| controls(&catalog()[tool].layout).any(|c| c.channels[0].id == *id); |
| 208 | if tools.is_empty() { |
| 209 | assert!(catalog().keys().any(|t| hits(t)), "no tool has `{id}` for section `{title}`"); |
| 210 | } |
| 211 | for tool in *tools { |
| 212 | assert!(hits(tool), "{tool} has no `{id}` row for section `{title}`"); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | #[test] |
| 218 | fn every_layout_is_well_formed() { |
| 219 | for (tool, entry) in catalog() { |
| 220 | let mut seen = HashSet::new(); |
| 221 | for def in controls(&entry.layout) { |
| 222 | for ch in &def.channels { |
| 223 | assert!(seen.insert(ch.id.as_str()), "{tool}: `{}` appears in two rows", ch.id); |
| 224 | assert!(entry.layout.covered.contains(&ch.id), "{tool}: `{}` not covered", ch.id); |
| 225 | assert!(ch.scale[0] < ch.scale[1] && ch.scale.iter().all(|v| v.is_finite()), "{tool}.{} scale {:?}", ch.id, ch.scale); |
| 226 | } |
| 227 | if matches!(def.kind, Kind::Segmented | Kind::Combo) { |
| 228 | assert!(def.default_index < def.entries.len(), "{tool}.{}: default index out of range", def.label); |
| 229 | } |
| 230 | let n = def.channels.len(); |
| 231 | let ok = match def.kind { |
| 232 | Kind::Range => n == 2, |
| 233 | Kind::Color => (3..=4).contains(&n), |
| 234 | Kind::Toggles => n == def.entries.len(), |
| 235 | _ => n == 1, |
| 236 | }; |
| 237 | assert!(ok, "{tool}.{}: {n} channels", def.label); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | #[test] |
| 243 | fn curated_kinds_have_pages_in_catalog_or_fallback() { |
| 244 | for &tool in CURATED_TOOL_KINDS { |
| 245 | let layout = match catalog().get(tool) { |
| 246 | Some(entry) => &entry.layout, |
| 247 | None => &dynamic::curated_layout(tool).unwrap_or_else(|| panic!("{tool}: no catalog entry and no curated layout")), |
| 248 | }; |
| 249 | assert!(controls(layout).next().is_some(), "{tool} has no rows"); |
| 250 | } |
| 251 | let fallback = dynamic::curated_layout("Fuse.Switch").expect("curated fallback"); |
| 252 | assert!(fallback.covered.contains(schema_for_kind("Fuse.Switch")[0].name)); |
| 253 | } |
| 254 | |
| 255 | #[test] |
| 256 | fn user_controls_become_rows() { |
| 257 | let comp = Comp::from_fusion_str( |
| 258 | r#"Composition { |
| 259 | Tools = ordered() { |
| 260 | Mac = MacroOperator { |
| 261 | UserControls = ordered() { |
| 262 | Gain = { LINKS_Name = "Gain", LINKID_DataType = "Number", INPID_InputControl = "SliderControl", INP_Default = 0.5, INP_MaxScale = 4, ICS_ControlPage = "Look" }, |
| 263 | Mode = { LINKID_DataType = "Number", INPID_InputControl = "ComboControl", INP_Default = 1, { CCS_AddString = "Off" }, { CCS_AddString = "On" } }, |
| 264 | Spot = { LINKID_DataType = "Point", INPID_InputControl = "OffsetControl", INP_DefaultX = 0.25, INP_DefaultY = 0.75 }, |
| 265 | }, |
| 266 | }, |
| 267 | }, |
| 268 | }"#, |
| 269 | ) |
| 270 | .expect("comp"); |
| 271 | let defs = dynamic::user_defs(&comp.node(comp.find_node("Mac").unwrap()).raw); |
| 272 | assert_eq!(defs.len(), 3); |
| 273 | assert_eq!((defs[0].page.as_deref(), defs[0].scale), (Some("Look"), [0.0, 4.0])); |
| 274 | assert_eq!((defs[1].entries.len(), defs[1].default[0]), (2, 1.0)); |
| 275 | assert_eq!(defs[2].default, [0.25, 0.75]); |
| 276 | let page = super::super::layout::dynamic_page("Look", &defs); |
| 277 | let kinds: Vec<_> = page.sections[0].controls.iter().map(|c| c.kind).collect(); |
| 278 | assert_eq!(kinds, [Kind::Slider, Kind::Combo, Kind::Point]); |
| 279 | } |
| 280 | |
| 281 | /// Every tool, every page, builds under the frame guard without panicking. |
| 282 | #[test] |
| 283 | fn builds_every_page_of_every_tool() { |
| 284 | catalog(); |
| 285 | let theme = ui::Theme::dark(); |
| 286 | let mut state = InspectorState::new(&theme); |
| 287 | let mut ui = ui::Ui::new(theme); |
| 288 | let _ = ui.measure("M"); |
| 289 | let (mut history, stats) = (CompHistory::default(), engine::EvalStats::default()); |
| 290 | for (tool, entry) in catalog() { |
| 291 | let mut comp = Comp::from_fusion_str(&format!("Composition {{ Tools = ordered() {{ N = {tool} {{}}, }}, }}")).expect("comp"); |
| 292 | let node = comp.find_node("N").expect("node"); |
| 293 | for page in 0..entry.layout.pages.len().max(1) { |
| 294 | state.page.insert(node.0, page); |
| 295 | ui.begin([360.0, 900.0], 1.0, Instant::now()); |
| 296 | state.build_ui(&mut ui, ui::Id::ROOT.child("pane"), &mut comp, &mut history, Some(node), 0.0, &stats); |
| 297 | ui.end(); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | struct Harness { |
| 303 | ui: ui::Ui, |
| 304 | state: InspectorState, |
| 305 | comp: Comp, |
| 306 | history: CompHistory, |
| 307 | node: model::NodeId, |
| 308 | at: Instant, |
| 309 | } |
| 310 | |
| 311 | const PANE: &str = "pane"; |
| 312 | |
| 313 | impl Harness { |
| 314 | fn new(tool: &str, page: usize) -> Self { |
| 315 | catalog(); |
| 316 | let theme = ui::Theme::dark(); |
| 317 | let mut ui = ui::Ui::new(theme.clone()); |
| 318 | let _ = ui.measure("M"); |
| 319 | ui.set_reduce_motion(true); |
| 320 | let mut state = InspectorState::new(&theme); |
| 321 | let comp = Comp::from_fusion_str(&format!("Composition {{ Tools = ordered() {{ N = {tool} {{}}, }}, }}")).expect("comp"); |
| 322 | let node = comp.find_node("N").expect("node"); |
| 323 | state.page.insert(node.0, page); |
| 324 | let mut h = Self { ui, state, comp, history: CompHistory::default(), node, at: Instant::now() }; |
| 325 | h.frame(0); |
| 326 | h |
| 327 | } |
| 328 | |
| 329 | fn frame(&mut self, advance_ms: u64) { |
| 330 | self.at += std::time::Duration::from_millis(advance_ms); |
| 331 | self.ui.begin([360.0, 900.0], 1.0, self.at); |
| 332 | let pane = ui::Id::ROOT.child(PANE); |
| 333 | self.state.build_ui(&mut self.ui, pane, &mut self.comp, &mut self.history, Some(self.node), 0.0, &engine::EvalStats::default()); |
| 334 | self.ui.end(); |
| 335 | } |
| 336 | |
| 337 | /// Where the row `key`'s child `part` sits: the first section of the active page. |
| 338 | fn rect(&self, page: usize, key: &str, part: impl std::hash::Hash) -> [f32; 4] { |
| 339 | let section = ui::Id::ROOT.child(PANE).child("insp-scroll").child(("sec", (self.node.0, page, 0usize))); |
| 340 | let row = section.child("body").child(("row", self.node.0, key)); |
| 341 | self.ui.laid_out(row.child(part)).unwrap_or_else(|| panic!("no `{key}` row in page {page}")) |
| 342 | } |
| 343 | |
| 344 | fn click(&mut self, at: [f32; 2]) { |
| 345 | use winit::event::MouseButton::Left; |
| 346 | self.ui.event(ui::Event::PointerMoved(at)); |
| 347 | self.ui.event(ui::Event::Button { button: Left, pressed: true, at: self.at }); |
| 348 | self.frame(16); |
| 349 | self.ui.event(ui::Event::Button { button: Left, pressed: false, at: self.at }); |
| 350 | self.frame(16); |
| 351 | self.frame(16); |
| 352 | } |
| 353 | |
| 354 | fn number(&self, id: &str) -> Option<f64> { |
| 355 | self.comp.node(self.node).static_number(id) |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | fn center(r: [f32; 4]) -> [f32; 2] { |
| 360 | [(r[0] + r[2]) / 2.0, (r[1] + r[3]) / 2.0] |
| 361 | } |
| 362 | |
| 363 | #[test] |
| 364 | fn slider_drag_edits_and_undoes_as_one_step() { |
| 365 | let mut h = Harness::new("Merge", 2); |
| 366 | let track = h.rect(2, "Blend", ("sld", h.node.0, "Blend")); |
| 367 | h.click([track[0] + (track[2] - track[0]) * 0.25, (track[1] + track[3]) / 2.0]); |
| 368 | let value = h.number("Blend").expect("slider wrote Blend"); |
| 369 | assert!(value < 0.5, "clicking a quarter along the track set {value}"); |
| 370 | |
| 371 | h.frame(600); |
| 372 | assert!(h.history.undo(&mut h.comp)); |
| 373 | assert_eq!(h.number("Blend"), None); |
| 374 | } |
| 375 | |
| 376 | #[test] |
| 377 | fn checkbox_and_keyframe_diamond_edit_with_undo() { |
| 378 | let mut h = Harness::new("Merge", 2); |
| 379 | let check = h.rect(2, "ProcessWhenBlendIs00", ("chk", h.node.0, "ProcessWhenBlendIs00")); |
| 380 | h.click(center(check)); |
| 381 | assert_eq!(h.number("ProcessWhenBlendIs00"), Some(1.0)); |
| 382 | |
| 383 | let diamond = h.rect(2, "Blend", "kf"); |
| 384 | h.click(center(diamond)); |
| 385 | let state = |h: &Harness| super::super::keyframe_state(&h.comp, h.node, "Blend", 0.0); |
| 386 | assert_eq!(state(&h), super::super::KeyframeState::KeyframeAtCurrent); |
| 387 | h.click(center(diamond)); |
| 388 | assert_eq!(state(&h), super::super::KeyframeState::Static); |
| 389 | assert!(h.history.undo(&mut h.comp)); |
| 390 | assert_eq!(state(&h), super::super::KeyframeState::KeyframeAtCurrent); |
| 391 | } |
| 392 | |
| 393 | #[test] |
| 394 | fn color_channel_slider_and_typed_number_commit() { |
| 395 | let mut h = Harness::new("Background", 0); |
| 396 | let track = h.rect(0, "TopLeftRed", ("clr-sld", h.node.0, "TopLeftRed")); |
| 397 | h.click([track[0] + (track[2] - track[0]) * 0.75, (track[1] + track[3]) / 2.0]); |
| 398 | let slid = h.number("TopLeftRed").expect("channel slider wrote the red channel"); |
| 399 | assert!(slid > 0.5, "clicking three quarters along the track set {slid}"); |
| 400 | |
| 401 | let well = h.rect(0, "TopLeftGreen", ("num", h.node.0, "TopLeftGreen", None::<usize>)); |
| 402 | h.click(center(well)); |
| 403 | for ch in "0.3".chars() { |
| 404 | let key = winit::keyboard::Key::Character(ch.to_string().into()); |
| 405 | h.ui.event(ui::Event::Key { key, text: Some(ch.to_string()), modifiers: Default::default() }); |
| 406 | h.frame(16); |
| 407 | } |
| 408 | let enter = winit::keyboard::Key::Named(winit::keyboard::NamedKey::Enter); |
| 409 | h.ui.event(ui::Event::Key { key: enter, text: None, modifiers: Default::default() }); |
| 410 | h.frame(16); |
| 411 | h.frame(16); |
| 412 | let typed = h.number("TopLeftGreen").expect("typed value committed"); |
| 413 | assert!((typed - 0.3).abs() < 1e-9, "typed 0.3, got {typed}"); |
| 414 | } |
| 415 | |
| 416 | #[test] |
| 417 | fn reset_restores_defaults_but_keeps_animation() { |
| 418 | let mut h = Harness::new("Merge", 2); |
| 419 | h.comp.set_input_value(h.node, "Blend", model::ParamValue::Number(0.2)); |
| 420 | h.comp.set_keyframe(h.node, "Gain", 0.0, 0.4).expect("keyframe"); |
| 421 | h.comp.set_input_value(h.node, "ProcessWhenBlendIs00", model::ParamValue::Number(1.0)); |
| 422 | let node = h.node; |
| 423 | let pages = &catalog()["Merge"].layout.pages; |
| 424 | let mut actions = Vec::new(); |
| 425 | let mut edit = super::super::edit::EditState::default(); |
| 426 | let mut cx = super::super::edit::Cx { |
| 427 | comp: &mut h.comp, |
| 428 | history: &mut h.history, |
| 429 | actions: &mut actions, |
| 430 | node, |
| 431 | frame: 0.0, |
| 432 | now: h.at, |
| 433 | p: h.state.palette, |
| 434 | expr: &h.state.expr, |
| 435 | edit: &mut edit, |
| 436 | }; |
| 437 | cx.reset_defaults(pages.iter().flat_map(|p| &p.sections).flat_map(|s| &s.controls)); |
| 438 | assert_eq!(h.number("Blend"), Some(1.0)); |
| 439 | assert_eq!(h.number("ProcessWhenBlendIs00"), Some(0.0)); |
| 440 | assert_eq!(super::super::keyframe_state(&h.comp, node, "Gain", 0.0), super::super::KeyframeState::KeyframeAtCurrent); |
| 441 | assert!(h.history.undo(&mut h.comp)); |
| 442 | assert_eq!(h.number("Blend"), Some(0.2)); |
| 443 | } |
| 444 | |
| 445 | fn type_into(h: &mut Harness, well: [f32; 4], text: &str) { |
| 446 | h.click(center(well)); |
| 447 | for ch in text.chars() { |
| 448 | let key = winit::keyboard::Key::Character(ch.to_string().into()); |
| 449 | h.ui.event(ui::Event::Key { key, text: Some(ch.to_string()), modifiers: Default::default() }); |
| 450 | h.frame(16); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /// A color drag that moves no RGB channel (alpha on an RGB-only group, hue on gray) reaches |
| 455 | /// `write_numbers` with nothing to write. |
| 456 | #[test] |
| 457 | fn empty_edits_write_nothing_and_never_panic() { |
| 458 | let mut h = Harness::new("Background", 0); |
| 459 | let (node, mut actions) = (h.node, Vec::new()); |
| 460 | let channel = &find("Background", "TopLeftRed").channels[0]; |
| 461 | let mut edit = super::super::edit::EditState::default(); |
| 462 | let mut cx = super::super::edit::Cx { |
| 463 | comp: &mut h.comp, |
| 464 | history: &mut h.history, |
| 465 | actions: &mut actions, |
| 466 | node, |
| 467 | frame: 0.0, |
| 468 | now: h.at, |
| 469 | p: h.state.palette, |
| 470 | expr: &h.state.expr, |
| 471 | edit: &mut edit, |
| 472 | }; |
| 473 | cx.write_numbers("Color", &[], Some(&channel.id)); |
| 474 | cx.write_numbers("Color", &[], None); |
| 475 | assert!(actions.is_empty() && edit.last_scrub.is_none()); |
| 476 | assert!(!h.history.undo(&mut h.comp)); |
| 477 | } |
| 478 | |
| 479 | #[test] |
| 480 | fn typed_value_commits_to_the_node_it_was_typed_into() { |
| 481 | let mut h = Harness::new("Blur", 0); |
| 482 | h.comp = Comp::from_fusion_str("Composition { Tools = ordered() { Blur1 = Blur {}, Merge1 = Merge {}, }, }").expect("comp"); |
| 483 | h.node = h.comp.find_node("Blur1").expect("blur"); |
| 484 | h.frame(0); |
| 485 | let well = h.rect(0, "XBlurSize", ("num", h.node.0, "XBlurSize", None::<usize>)); |
| 486 | type_into(&mut h, well, "7"); |
| 487 | let blur = h.node; |
| 488 | |
| 489 | h.node = h.comp.find_node("Merge1").expect("merge"); |
| 490 | h.frame(16); |
| 491 | h.frame(16); |
| 492 | assert_eq!(h.comp.node(blur).static_number("XBlurSize"), Some(7.0)); |
| 493 | assert!(h.comp.node(h.node).inputs.values().all(|b| b.value.is_none()), "Merge1 was touched"); |
| 494 | } |
| 495 | |
| 496 | #[test] |
| 497 | fn typed_value_commits_to_its_node_on_deselect() { |
| 498 | let mut h = Harness::new("Blur", 0); |
| 499 | let well = h.rect(0, "XBlurSize", ("num", h.node.0, "XBlurSize", None::<usize>)); |
| 500 | type_into(&mut h, well, "9"); |
| 501 | h.at += std::time::Duration::from_millis(16); |
| 502 | h.ui.begin([360.0, 900.0], 1.0, h.at); |
| 503 | let pane = ui::Id::ROOT.child(PANE); |
| 504 | h.state.build_ui(&mut h.ui, pane, &mut h.comp, &mut h.history, None, 0.0, &engine::EvalStats::default()); |
| 505 | h.ui.end(); |
| 506 | assert_eq!(h.number("XBlurSize"), Some(9.0)); |
| 507 | assert!(h.state.edit.number.is_none()); |
| 508 | } |
| 509 | |
| 510 | #[test] |
| 511 | fn pages_are_not_rebuilt_by_value_edits() { |
| 512 | let mut h = Harness::new("Merge", 2); |
| 513 | let key = |h: &Harness| h.state.node_pages.as_ref().map(|n| n.key); |
| 514 | h.comp.set_input_value(h.node, "Blend", model::ParamValue::Number(0.2)); |
| 515 | h.frame(16); |
| 516 | let before = key(&h); |
| 517 | h.comp.set_input_value(h.node, "Blend", model::ParamValue::Number(0.7)); |
| 518 | h.frame(16); |
| 519 | assert_eq!(key(&h), before); |
| 520 | h.comp.set_input_value(h.node, "Stray", model::ParamValue::Number(1.0)); |
| 521 | h.frame(16); |
| 522 | assert_ne!(key(&h), before); |
| 523 | } |
| 524 | |
| 525 | #[test] |
| 526 | fn malformed_user_controls_extreme_bounds_and_stale_node_do_not_panic() { |
| 527 | let mut comp = Comp::from_fusion_str( |
| 528 | r#"Composition { |
| 529 | Tools = ordered() { |
| 530 | Mac = MacroOperator { |
| 531 | UserControls = ordered() { |
| 532 | BadClamp = { LINKID_DataType = "Number", INPID_InputControl = "SliderControl", INP_MinScale = 10, INP_MaxScale = 2, INP_MinAllowed = 5, INP_MaxAllowed = 1 }, |
| 533 | OverflowGroup = { LINKID_DataType = "Number", INPID_InputControl = "RangeControl", IC_ControlGroup = 1, IC_ControlID = 4294967295 }, |
| 534 | BadDefault = { LINKID_DataType = "Number", INPID_InputControl = "ComboControl", INP_Default = 99, { CCS_AddString = "OnlyOne" } }, |
| 535 | }, |
| 536 | }, |
| 537 | }, |
| 538 | }"#, |
| 539 | ) |
| 540 | .expect("comp"); |
| 541 | let mac = comp.find_node("Mac").unwrap(); |
| 542 | comp.set_input_value(mac, "BadClamp", model::ParamValue::Number(f64::NAN)); |
| 543 | |
| 544 | let theme = ui::Theme::dark(); |
| 545 | let mut state = InspectorState::new(&theme); |
| 546 | let mut ui = ui::Ui::new(theme); |
| 547 | let _ = ui.measure("M"); |
| 548 | let (mut history, stats) = (CompHistory::default(), engine::EvalStats::default()); |
| 549 | ui.begin([360.0, 900.0], 1.0, Instant::now()); |
| 550 | state.build_ui(&mut ui, ui::Id::ROOT.child("pane"), &mut comp, &mut history, Some(mac), 0.0, &stats); |
| 551 | ui.end(); |
| 552 | |
| 553 | // Direct edit with inverted allowed bounds and NaN must not panic. |
| 554 | let mut actions = Vec::new(); |
| 555 | let mut edit = super::super::edit::EditState::default(); |
| 556 | let mut cx = super::super::edit::Cx { |
| 557 | comp: &mut comp, |
| 558 | history: &mut history, |
| 559 | actions: &mut actions, |
| 560 | node: mac, |
| 561 | frame: 0.0, |
| 562 | now: Instant::now(), |
| 563 | p: state.palette, |
| 564 | expr: &state.expr, |
| 565 | edit: &mut edit, |
| 566 | }; |
| 567 | let bad_ch = super::super::layout::Channel { |
| 568 | id: "BadClamp".to_owned(), |
| 569 | default: [0.0, 0.0], |
| 570 | scale: [0.0, 1.0], |
| 571 | allowed: [Some(5.0), Some(1.0)], |
| 572 | integer: false, |
| 573 | }; |
| 574 | cx.write_numbers("BadClamp", &[(&bad_ch, 3.0), (&bad_ch, f64::NAN)], None); |
| 575 | |
| 576 | // Stale NodeId falls back to empty state cleanly. |
| 577 | ui.begin([360.0, 900.0], 1.0, Instant::now()); |
| 578 | state.build_ui(&mut ui, ui::Id::ROOT.child("pane"), &mut comp, &mut history, Some(model::NodeId(99)), 0.0, &stats); |
| 579 | ui.end(); |
| 580 | assert_eq!(super::super::keyframe_state(&comp, model::NodeId(99), "Size", 0.0), super::super::KeyframeState::Static); |
| 581 | } |
| 582 | } |