fader_master/config/tests/editor.rs

326 lines
9.8 KiB
Rust

use fader_config::{
Action, MessageKind, MixerLayoutEntry, ProfileDocument, SelectorDraft, SelectorField,
SelectorPattern, TargetKind,
};
const PROFILE: &str = include_str!("../../examples/profiles/desktop.toml");
#[test]
fn draft_creates_an_application_group_and_fader_as_valid_toml() {
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
draft
.add_application(
"chat",
"Chat",
&[SelectorDraft {
field: SelectorField::ProcessBinary,
pattern: SelectorPattern::Exact("signal-desktop".to_owned()),
}],
)
.unwrap();
draft
.add_group("communications", "Communications", &["chat", "browser"])
.unwrap();
draft
.add_fader(
"communications-fader",
TargetKind::Group,
"communications",
true,
0.02,
)
.unwrap();
let profile = draft.preview().unwrap();
assert!(
profile
.applications()
.iter()
.any(|item| item.id() == "chat")
);
assert!(
profile
.groups()
.iter()
.any(|item| item.id() == "communications")
);
assert!(
profile
.faders()
.iter()
.any(|item| item.id() == "communications-fader")
);
ProfileDocument::parse(&draft.finish().unwrap()).unwrap();
}
#[test]
fn invalid_edits_and_referenced_deletes_roll_back_the_draft() {
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
let original = draft.source();
let invalid = draft
.add_application(
"broken",
"Broken",
&[SelectorDraft {
field: SelectorField::ApplicationName,
pattern: SelectorPattern::Regex("(".to_owned()),
}],
)
.unwrap_err();
assert!(invalid.to_string().contains("regex"));
assert_eq!(draft.source(), original);
let referenced = draft.remove_application("browser").unwrap_err();
assert!(referenced.to_string().contains("unknown application"));
assert_eq!(draft.source(), original);
}
#[test]
fn existing_applications_groups_and_faders_can_be_edited_and_deleted() {
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
draft
.set_application(
"browser",
"Web",
&[SelectorDraft {
field: SelectorField::ProcessBinary,
pattern: SelectorPattern::Exact("waterfox-bin".to_owned()),
}],
)
.unwrap();
draft.set_group("media", "Players", &["browser"]).unwrap();
draft
.set_fader(
"media-fader",
TargetKind::Application,
"browser",
false,
0.05,
)
.unwrap();
let profile = draft.preview().unwrap();
assert_eq!(profile.applications()[1].label(), "Web");
assert_eq!(profile.groups()[0].members(), &["browser"]);
assert_eq!(profile.faders()[0].target_kind(), TargetKind::Application);
assert!(!profile.faders()[0].pickup());
draft.remove_group("media").unwrap();
draft.remove_fader("media-fader").unwrap_err();
assert!(draft.source().contains("media-fader"));
}
#[test]
fn editing_group_members_preserves_the_configured_volume_mode_field() {
let source = PROFILE.replace(
"volume_mode = \"absolute\"",
"volume_mode = \"absolute\" # user-owned group mode",
);
let mut draft = ProfileDocument::parse(&source).unwrap();
draft.set_group("media", "Players", &["browser"]).unwrap();
assert!(
draft
.source()
.contains("volume_mode = \"absolute\" # user-owned group mode")
);
}
#[test]
fn draft_sets_and_unsets_the_default_controller() {
let source = PROFILE.replace("default_controller = \"desk-left\"\n", "");
let mut draft = ProfileDocument::parse(&source).unwrap();
draft.set_default_controller(Some("desk-left")).unwrap();
assert_eq!(
draft.preview().unwrap().default_controller(),
Some("desk-left")
);
let before_invalid = draft.source();
let error = draft.set_default_controller(Some("missing")).unwrap_err();
assert!(error.to_string().contains("unknown surface"));
assert_eq!(draft.source(), before_invalid);
draft.set_default_controller(None).unwrap();
assert_eq!(draft.preview().unwrap().default_controller(), None);
assert!(!draft.source().contains("default_controller"));
}
#[test]
fn draft_theme_edit_is_profile_backed() {
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
draft.set_theme(fader_config::UiTheme::Nord).unwrap();
assert_eq!(
draft.preview().unwrap().theme(),
fader_config::UiTheme::Nord
);
assert!(draft.source().contains("theme = \"nord\""));
}
#[test]
fn draft_adds_bindings_and_rejects_duplicate_physical_controls() {
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
draft
.add_binding(
"desk-left",
"media-fader",
Action::Volume,
MessageKind::Cc,
1,
49,
)
.unwrap();
let before_duplicate = draft.source();
let error = draft
.add_binding(
"desk-left",
"media-fader",
Action::ToggleMute,
MessageKind::Cc,
1,
49,
)
.unwrap_err();
assert!(error.to_string().contains("duplicate physical binding"));
assert_eq!(draft.source(), before_duplicate);
}
#[test]
fn mixer_layout_draft_reorders_separators_and_retains_hidden_faders() {
let source = PROFILE.replacen(
"[[bindings]]",
"# separator comment must move with its entry\n[[mixer_layout]]\nkind = \"separator\"\nid = \"outputs\"\nlabel = \"Outputs\"\n\n[[bindings]]",
1,
);
let mut draft = ProfileDocument::parse(&source).unwrap();
draft
.set_mixer_orientation(fader_config::MixerOrientation::Horizontal)
.expect("mixer orientation must be editable");
draft
.set_fader_hidden("media-fader", true)
.expect("fader must be hidden without deletion");
draft
.set_fader_density("media-fader", fader_config::FaderDensity::Thick)
.expect("fader density must be editable");
draft
.move_layout_entry("media-fader", 0)
.expect("hidden fader must remain reorderable");
draft
.set_separator_label("outputs", "Main outputs")
.expect("separator label must be editable");
let profile = draft.preview().unwrap();
assert_eq!(
profile.mixer_orientation(),
fader_config::MixerOrientation::Horizontal
);
assert_eq!(
profile.mixer_layout(),
[
MixerLayoutEntry::Fader {
fader: "media-fader".to_owned(),
hidden: true,
density: fader_config::FaderDensity::Thick,
},
MixerLayoutEntry::Separator {
id: "outputs".to_owned(),
label: "Main outputs".to_owned(),
},
]
);
assert_eq!(profile.faders().len(), 1, "hiding must not delete routing");
assert!(
draft
.source()
.contains("# separator comment must move with its entry")
);
draft.remove_separator("outputs").unwrap();
draft.set_fader_hidden("media-fader", false).unwrap();
assert_eq!(
draft.preview().unwrap().mixer_layout(),
[MixerLayoutEntry::Fader {
fader: "media-fader".to_owned(),
hidden: false,
density: fader_config::FaderDensity::Thick,
}]
);
}
#[test]
fn selector_and_binding_edits_preserve_unrelated_capture_and_actions() {
let mut draft = ProfileDocument::parse(PROFILE).unwrap();
draft
.add_application(
"multi",
"Multi",
&[
SelectorDraft {
field: SelectorField::ApplicationName,
pattern: SelectorPattern::Exact("Old".to_owned()),
},
SelectorDraft {
field: SelectorField::MediaName,
pattern: SelectorPattern::Regex("keep-me".to_owned()),
},
],
)
.unwrap();
draft
.set_application_selector(
"multi",
0,
&SelectorDraft {
field: SelectorField::ProcessBinary,
pattern: SelectorPattern::Exact("player-bin".to_owned()),
},
)
.unwrap();
draft
.set_binding(
"desk-left",
"media-fader",
Action::Volume,
MessageKind::Cc,
1,
7,
)
.unwrap();
let before_conflict = draft.source();
let conflict = draft
.set_binding(
"desk-left",
"media-fader",
Action::ToggleMute,
MessageKind::Cc,
1,
7,
)
.unwrap_err();
assert!(conflict.to_string().contains("duplicate physical binding"));
assert_eq!(draft.source(), before_conflict);
draft
.remove_bindings("media-fader", Action::ToggleMute)
.unwrap();
let profile = draft.preview().unwrap();
let multi = profile
.applications()
.iter()
.find(|application| application.id() == "multi")
.unwrap();
assert_eq!(multi.selectors().len(), 2);
assert_eq!(multi.selectors()[0].pattern(), ("player-bin", false));
assert_eq!(multi.selectors()[1].pattern(), ("keep-me", true));
assert!(profile.bindings().iter().any(|binding| {
binding.fader() == "media-fader"
&& binding.action() == Action::Volume
&& binding.number() == 7
}));
assert!(!profile.bindings().iter().any(|binding| {
binding.fader() == "media-fader" && binding.action() == Action::ToggleMute
}));
}