| 1 |
|
| 2 |
|
| 3 |
use super::super::config::config_file; |
| 4 |
use super::super::fixtures::{FW12, external, fw12}; |
| 5 |
use super::*; |
| 6 |
|
| 7 |
#[test] |
| 8 |
fn parses_the_real_capture() { |
| 9 |
let outputs = parse(FW12).unwrap(); |
| 10 |
assert_eq!(outputs.len(), 1); |
| 11 |
let panel = &outputs[0]; |
| 12 |
assert_eq!(panel.name, "eDP-1"); |
| 13 |
assert_eq!(panel.make, "BOE"); |
| 14 |
assert_eq!(panel.model, "NV122WUM-N42"); |
| 15 |
assert!(panel.active && panel.dpms && panel.focused); |
| 16 |
assert_eq!(panel.transform, "normal"); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#[test] |
| 23 |
fn an_unknown_serial_is_a_string_not_a_null() { |
| 24 |
assert_eq!(fw12().serial, "Unknown"); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
#[test] |
| 29 |
fn refresh_is_millihertz() { |
| 30 |
let mode = fw12().current_mode.expect("the panel has a current mode"); |
| 31 |
assert_eq!(mode.refresh, 60002); |
| 32 |
assert_eq!(mode.spelled(), "1920x1200@60.002Hz"); |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#[test] |
| 39 |
fn the_logical_and_physical_sizes_both_survive() { |
| 40 |
let panel = fw12(); |
| 41 |
assert_eq!((panel.rect.width, panel.rect.height), (1536, 960)); |
| 42 |
let mode = panel.current_mode.unwrap(); |
| 43 |
assert_eq!((mode.width, mode.height), (1920, 1200)); |
| 44 |
assert!((panel.scale - 1.25).abs() < f64::EPSILON); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#[test] |
| 51 |
fn the_panel_advertises_exactly_one_mode() { |
| 52 |
assert_eq!(fw12().modes.len(), 1); |
| 53 |
} |
| 54 |
|
| 55 |
#[test] |
| 56 |
fn malformed_json_is_an_error() { |
| 57 |
assert!(parse("not json").is_err()); |
| 58 |
assert!(parse("{}").is_err(), "an object is not a list of outputs"); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#[test] |
| 64 |
fn unknown_fields_are_ignored() { |
| 65 |
let raw = r#"[{"name":"HDMI-A-1","something_new":{"nested":true}}]"#; |
| 66 |
assert_eq!(parse(raw).unwrap()[0].name, "HDMI-A-1"); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
#[test] |
| 71 |
fn absent_fields_take_the_safe_default() { |
| 72 |
let output = parse(r#"[{"name":"DP-1"}]"#).unwrap().remove(0); |
| 73 |
assert!(output.dpms, "an output sway does not describe as asleep"); |
| 74 |
assert!((output.scale - 1.0).abs() < f64::EPSILON); |
| 75 |
assert!(output.modes.is_empty()); |
| 76 |
assert_eq!(output.current_mode, None); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
#[test] |
| 84 |
fn every_output_is_matched_by_its_connector() { |
| 85 |
let monitor = external("DP-3", "Example Co", "PA279CV", "K8LMQS032990"); |
| 86 |
assert_eq!(monitor.identifier(), "DP-3"); |
| 87 |
assert_eq!(fw12().identifier(), "eDP-1"); |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
fn every_laptop_panel_connector_reads_as_built_in() { |
| 92 |
for name in ["eDP-1", "eDP-2", "LVDS-1", "DSI-1"] { |
| 93 |
let mut panel = external(name, "BOE", "NV122WUM-N42", UNKNOWN); |
| 94 |
panel.name = name.into(); |
| 95 |
assert!(panel.built_in(), "{name}"); |
| 96 |
assert_eq!(panel.identifier(), name); |
| 97 |
} |
| 98 |
assert!(!external("DP-3", "Example Co", "PA279CV", "S1").built_in()); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
#[test] |
| 105 |
fn no_edid_text_reaches_a_stanza_however_hostile() { |
| 106 |
for make in [ |
| 107 |
"Ex\"Co", |
| 108 |
"Ex\\Co", |
| 109 |
"Ex\noutput * scale 3", |
| 110 |
"Ex\rCo", |
| 111 |
"Ex\tCo", |
| 112 |
] { |
| 113 |
let output = external("DP-1", make, "PA279CV", "S1"); |
| 114 |
assert_eq!(output.identifier(), "DP-1", "{make:?}"); |
| 115 |
let line = Directive::new(&output, "scale", "2").line(); |
| 116 |
assert_eq!(line, "output DP-1 scale 2", "{make:?}"); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
#[test] |
| 124 |
fn punctuation_still_distinguishes_two_monitors() { |
| 125 |
let one = external("DP-1", "Example Co.", "PA279CV", "S1"); |
| 126 |
let two = external("DP-1", "Example Co", "PA279CV", "S1"); |
| 127 |
assert_ne!(one.fingerprint(), two.fingerprint()); |
| 128 |
assert!(one.fingerprint().is_some()); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
#[test] |
| 135 |
fn an_anonymous_output_has_no_fingerprint() { |
| 136 |
assert_eq!( |
| 137 |
external("HDMI-A-1", UNKNOWN, UNKNOWN, UNKNOWN).fingerprint(), |
| 138 |
None |
| 139 |
); |
| 140 |
assert_eq!(external("HDMI-A-1", "", "", "").fingerprint(), None); |
| 141 |
|
| 142 |
|
| 143 |
assert_eq!( |
| 144 |
external("HDMI-A-1", UNKNOWN, UNKNOWN, "S1").fingerprint(), |
| 145 |
None |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
#[test] |
| 151 |
fn the_built_in_panel_has_no_fingerprint() { |
| 152 |
assert_eq!(fw12().fingerprint(), None); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
#[test] |
| 159 |
fn a_stanza_identifier_is_one_word() { |
| 160 |
assert!(is_one_word("eDP-1")); |
| 161 |
assert!(is_one_word("HDMI-A-1")); |
| 162 |
assert!(!is_one_word("BOE NV122WUM-N42 Unknown")); |
| 163 |
assert!(!is_one_word("Ex\"Co")); |
| 164 |
assert!(!is_one_word("Ex\\Co")); |
| 165 |
assert!(!is_one_word("Ex\nCo")); |
| 166 |
assert!(!is_one_word("")); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
#[test] |
| 172 |
fn the_runtime_command_and_the_config_line_are_the_same_words() { |
| 173 |
let directive = Directive::new(&fw12(), "scale", "1.25"); |
| 174 |
assert_eq!(directive.line(), "output eDP-1 scale 1.25"); |
| 175 |
assert_eq!( |
| 176 |
directive.invocation().display(), |
| 177 |
"swaymsg output eDP-1 scale 1.25", |
| 178 |
); |
| 179 |
assert_eq!( |
| 180 |
directive.invocation().display(), |
| 181 |
format!("swaymsg {}", directive.line()), |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
#[test] |
| 191 |
fn neither_consumer_quotes_a_connector_name() { |
| 192 |
let monitor = external("DP-3", "Example Co", "PA279CV", "K8LMQS032990"); |
| 193 |
let directive = Directive::new(&monitor, "scale", "2"); |
| 194 |
assert_eq!(directive.line(), "output DP-3 scale 2"); |
| 195 |
assert_eq!( |
| 196 |
directive.invocation().display(), |
| 197 |
"swaymsg output DP-3 scale 2", |
| 198 |
"the log pane's single quotes appear only around an argument with \ |
| 199 |
whitespace in it, and there is none left", |
| 200 |
); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
#[test] |
| 206 |
fn scales_are_spelled_the_way_a_person_would_type_them() { |
| 207 |
assert_eq!(spell_scale(1.0), "1"); |
| 208 |
assert_eq!(spell_scale(1.25), "1.25"); |
| 209 |
assert_eq!(spell_scale(1.5), "1.5"); |
| 210 |
assert_eq!(spell_scale(2.0), "2"); |
| 211 |
} |
| 212 |
|
| 213 |
#[test] |
| 214 |
fn the_scale_key_always_moves() { |
| 215 |
let mut panel = fw12(); |
| 216 |
assert!((panel.next_scale() - 1.5).abs() < f64::EPSILON); |
| 217 |
panel.scale = 2.0; |
| 218 |
assert!( |
| 219 |
(panel.next_scale() - 1.0).abs() < f64::EPSILON, |
| 220 |
"the top rung wraps rather than dead-ending", |
| 221 |
); |
| 222 |
|
| 223 |
panel.scale = 1.1; |
| 224 |
assert!((panel.next_scale() - 1.25).abs() < f64::EPSILON); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
#[test] |
| 230 |
fn only_a_rotation_that_is_not_the_default_is_written() { |
| 231 |
let mut panel = fw12(); |
| 232 |
assert!(!config_file(&[panel.clone()]).contains("transform")); |
| 233 |
panel.transform = "90".into(); |
| 234 |
assert!(config_file(&[panel]).contains("output eDP-1 transform 90")); |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
#[test] |
| 240 |
fn a_disabled_output_is_persisted_as_disabled() { |
| 241 |
let mut monitor = external("DP-3", "Example Co", "PA279CV", "S1"); |
| 242 |
monitor.active = false; |
| 243 |
let file = config_file(&[fw12(), monitor]); |
| 244 |
assert!(file.contains("scale 1"), "{file}"); |
| 245 |
assert!(file.contains("output DP-3 enable false"), "{file}"); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
#[test] |
| 251 |
fn the_last_usable_output_cannot_be_switched_off() { |
| 252 |
let panel = fw12(); |
| 253 |
let reason = refuse( |
| 254 |
std::slice::from_ref(&panel), |
| 255 |
&panel, |
| 256 |
&Change::Enabled(false), |
| 257 |
) |
| 258 |
.expect("refused with a reason"); |
| 259 |
assert!(reason.contains("eDP-1"), "{reason}"); |
| 260 |
} |
| 261 |
|
| 262 |
#[test] |
| 263 |
fn switching_one_off_is_allowed_while_another_is_usable() { |
| 264 |
let panel = fw12(); |
| 265 |
let monitor = external("DP-3", "Example Co", "PA279CV", "S1"); |
| 266 |
let outputs = vec![panel.clone(), monitor]; |
| 267 |
assert!(refuse(&outputs, &panel, &Change::Enabled(false)).is_none()); |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
#[test] |
| 273 |
fn an_asleep_second_output_does_not_count_as_a_screen() { |
| 274 |
let panel = fw12(); |
| 275 |
let mut dark = external("DP-3", "Example Co", "PA279CV", "S1"); |
| 276 |
dark.dpms = false; |
| 277 |
let outputs = vec![panel.clone(), dark]; |
| 278 |
assert!(refuse(&outputs, &panel, &Change::Enabled(false)).is_some()); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
#[test] |
| 285 |
fn a_scale_that_leaves_no_usable_geometry_is_refused() { |
| 286 |
let panel = fw12(); |
| 287 |
assert!(refuse(std::slice::from_ref(&panel), &panel, &Change::Scale(4.0)).is_some()); |
| 288 |
assert!(refuse(std::slice::from_ref(&panel), &panel, &Change::Scale(2.0)).is_none()); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
#[test] |
| 295 |
fn a_scale_on_an_output_with_no_mode_is_allowed() { |
| 296 |
let mut unknown = external("DP-9", "", "", ""); |
| 297 |
unknown.current_mode = None; |
| 298 |
unknown.modes = Vec::new(); |
| 299 |
assert!(refuse(&[unknown.clone()], &unknown, &Change::Scale(3.0)).is_none()); |
| 300 |
} |
| 301 |
|