Skip to main content

max / alloy

4.1 KB · 118 lines History Blame Raw
1 //! Tests for [`super`].
2
3 use super::super::UNKNOWN;
4 use super::super::fixtures::external;
5 use super::*;
6
7 fn remembered(connector: &str, scale: f64) -> monitors::Remembered {
8 monitors::Remembered {
9 connector: connector.to_string(),
10 scale,
11 transform: String::new(),
12 enabled: true,
13 last_seen: 1_700_000_000,
14 description: "Example Co PA279CV".to_string(),
15 }
16 }
17
18 /// The move that the whole design exists to make: a monitor known at DP-1
19 /// turns up on DP-2, and its settings come with it.
20 #[test]
21 fn a_monitor_on_a_new_port_brings_its_settings() {
22 let monitor = external("DP-2", "Example Co", "PA279CV", "S1");
23 let mut registry = monitors::Registry::default();
24 registry.remember(
25 monitor.fingerprint().expect("identifiable"),
26 remembered("DP-1", 1.5),
27 );
28
29 let moved = moves(std::slice::from_ref(&monitor), &registry);
30 assert_eq!(moved.len(), 1);
31 assert_eq!(moved[0].from, "DP-1");
32 assert_eq!(moved[0].to, "DP-2");
33
34 let lines: Vec<String> = moved[0].directives().iter().map(Directive::line).collect();
35 assert_eq!(lines, vec!["output DP-2 scale 1.5"]);
36 }
37
38 /// A monitor that has not moved is not a move, and neither is one nobody
39 /// has seen before. Both would be work with nothing to fix.
40 #[test]
41 fn nothing_moves_when_nothing_moved() {
42 let monitor = external("DP-1", "Example Co", "PA279CV", "S1");
43 let mut registry = monitors::Registry::default();
44 registry.remember(
45 monitor.fingerprint().expect("identifiable"),
46 remembered("DP-1", 1.5),
47 );
48 assert!(moves(std::slice::from_ref(&monitor), &registry).is_empty());
49
50 assert!(
51 moves(
52 std::slice::from_ref(&monitor),
53 &monitors::Registry::default()
54 )
55 .is_empty(),
56 "an unknown monitor is left alone rather than guessed at"
57 );
58 }
59
60 /// An anonymous output cannot be moved, because it cannot be recognised.
61 /// It is skipped rather than matched against something.
62 #[test]
63 fn an_anonymous_output_is_never_moved() {
64 let anonymous = external("DP-2", UNKNOWN, UNKNOWN, UNKNOWN);
65 let mut registry = monitors::Registry::default();
66 registry.remember("whatever".to_string(), remembered("DP-1", 2.0));
67 assert!(moves(std::slice::from_ref(&anonymous), &registry).is_empty());
68 }
69
70 /// Recording is what makes the next run able to notice a move, and it is
71 /// also where expiry happens: one pass, so a table cannot be written
72 /// without being pruned.
73 #[test]
74 fn recording_stores_the_settings_and_prunes_the_table() {
75 let mut monitor = external("DP-2", "Example Co", "PA279CV", "S1");
76 monitor.scale = 1.75;
77 let mut registry = monitors::Registry::default();
78 registry.remember("ancient".to_string(), remembered("DP-9", 1.0));
79
80 let now = 1_700_000_000 + 400 * 24 * 60 * 60;
81 remember(std::slice::from_ref(&monitor), &mut registry, now);
82
83 let entry = registry
84 .get(&monitor.fingerprint().expect("identifiable"))
85 .expect("the attached monitor is recorded");
86 assert_eq!(entry.connector, "DP-2");
87 assert!((entry.scale - 1.75).abs() < f64::EPSILON);
88 assert_eq!(entry.last_seen, now);
89 assert!(
90 registry.get("ancient").is_none(),
91 "a row older than the forget window should have gone"
92 );
93 }
94
95 /// A disabled monitor stays disabled when it moves. The `enable false` the
96 /// user wrote is a setting like any other, and losing it on a replug would
97 /// turn a screen back on that was deliberately off.
98 #[test]
99 fn a_disabled_monitor_stays_disabled_across_a_move() {
100 let monitor = external("DP-2", "Example Co", "PA279CV", "S1");
101 let mut entry = remembered("DP-1", 1.0);
102 entry.enabled = false;
103 entry.transform = "90".to_string();
104 let mut registry = monitors::Registry::default();
105 registry.remember(monitor.fingerprint().expect("identifiable"), entry);
106
107 let moved = moves(std::slice::from_ref(&monitor), &registry);
108 let lines: Vec<String> = moved[0].directives().iter().map(Directive::line).collect();
109 assert_eq!(
110 lines,
111 vec![
112 "output DP-2 scale 1",
113 "output DP-2 transform 90",
114 "output DP-2 enable false",
115 ]
116 );
117 }
118