forge
Explore
Log in

Don't you dare go Hollow

layout: Add tests for various operations

Browse files at this commit

84885fe5fbf910f78a6f0595c8c3ee23ad07beaaIvan MolodetskikhParent: 99b4ea6
src/layout.rsmodified+250 -0
@@ -773,6 +773,7 @@ impl<W: LayoutElement> MonitorSet<W> {
773 ws.window_under(pos_within_output)773 ws.window_under(pos_within_output)
774 }774 }
775 775
776+ #[cfg(test)]
776 fn verify_invariants(&self) {777 fn verify_invariants(&self) {
777 let (monitors, &primary_idx, &active_monitor_idx) = match &self {778 let (monitors, &primary_idx, &active_monitor_idx) = match &self {
778 MonitorSet::Normal {779 MonitorSet::Normal {
@@ -1438,6 +1439,7 @@ impl<W: LayoutElement> Workspace<W> {
1438 self.activate_column(column_idx);1439 self.activate_column(column_idx);
1439 }1440 }
1440 1441
1442+ #[cfg(test)]
1441 fn verify_invariants(&self) {1443 fn verify_invariants(&self) {
1442 assert!(self.view_size.w > 0);1444 assert!(self.view_size.w > 0);
1443 assert!(self.view_size.h > 0);1445 assert!(self.view_size.h > 0);
@@ -1892,6 +1894,7 @@ impl<W: LayoutElement> Column<W> {
1892 self.active_window_idx = new_idx;1894 self.active_window_idx = new_idx;
1893 }1895 }
1894 1896
1897+ #[cfg(test)]
1895 fn verify_invariants(&self) {1898 fn verify_invariants(&self) {
1896 assert!(!self.windows.is_empty(), "columns can't be empty");1899 assert!(!self.windows.is_empty(), "columns can't be empty");
1897 assert!(self.active_window_idx < self.windows.len());1900 assert!(self.active_window_idx < self.windows.len());
@@ -1970,3 +1973,250 @@ pub fn configure_new_window(view_size: Size<i32, Logical>, window: &Window) {
1970 state.bounds = Some(bounds);1973 state.bounds = Some(bounds);
1971 });1974 });
1972 }1975 }
1976+
1977+#[cfg(test)]
1978+mod tests {
1979+ use std::cell::Cell;
1980+ use std::rc::Rc;
1981+
1982+ use smithay::output::{Mode, PhysicalProperties, Subpixel};
1983+ use smithay::utils::IsAlive;
1984+
1985+ use super::*;
1986+
1987+ #[derive(Debug)]
1988+ struct TestWindowInner {
1989+ id: usize,
1990+ bbox: Cell<Rectangle<i32, Logical>>,
1991+ }
1992+
1993+ #[derive(Debug, Clone)]
1994+ struct TestWindow(Rc<TestWindowInner>);
1995+
1996+ impl TestWindow {
1997+ fn new(id: usize, bbox: Rectangle<i32, Logical>) -> Self {
1998+ Self(Rc::new(TestWindowInner {
1999+ id,
2000+ bbox: Cell::new(bbox),
2001+ }))
2002+ }
2003+ }
2004+
2005+ impl PartialEq for TestWindow {
2006+ fn eq(&self, other: &Self) -> bool {
2007+ self.0.id == other.0.id
2008+ }
2009+ }
2010+
2011+ impl IsAlive for TestWindow {
2012+ fn alive(&self) -> bool {
2013+ true
2014+ }
2015+ }
2016+
2017+ impl SpaceElement for TestWindow {
2018+ fn bbox(&self) -> Rectangle<i32, Logical> {
2019+ self.0.bbox.get()
2020+ }
2021+
2022+ fn is_in_input_region(&self, _point: &Point<f64, Logical>) -> bool {
2023+ false
2024+ }
2025+
2026+ fn set_activate(&self, _activated: bool) {}
2027+ fn output_enter(&self, _output: &Output, _overlap: Rectangle<i32, Logical>) {}
2028+ fn output_leave(&self, _output: &Output) {}
2029+ }
2030+
2031+ impl LayoutElement for TestWindow {
2032+ fn request_size(&self, _size: Size<i32, Logical>) {}
2033+
2034+ fn request_fullscreen(&self, _size: Size<i32, Logical>) {}
2035+
2036+ fn min_size(&self) -> Size<i32, Logical> {
2037+ Size::from((0, 0))
2038+ }
2039+
2040+ fn max_size(&self) -> Size<i32, Logical> {
2041+ Size::from((0, 0))
2042+ }
2043+
2044+ fn is_wl_surface(&self, _wl_surface: &WlSurface) -> bool {
2045+ false
2046+ }
2047+
2048+ fn send_frame<T, F>(
2049+ &self,
2050+ _output: &Output,
2051+ _time: T,
2052+ _throttle: Option<Duration>,
2053+ _primary_scan_out_output: F,
2054+ ) where
2055+ T: Into<Duration>,
2056+ F: FnMut(&WlSurface, &SurfaceData) -> Option<Output> + Copy,
2057+ {
2058+ }
2059+
2060+ fn send_dmabuf_feedback<'a, P, F>(
2061+ &self,
2062+ _output: &Output,
2063+ _primary_scan_out_output: P,
2064+ _select_dmabuf_feedback: F,
2065+ ) where
2066+ P: FnMut(&WlSurface, &SurfaceData) -> Option<Output> + Copy,
2067+ F: Fn(&WlSurface, &SurfaceData) -> &'a DmabufFeedback + Copy,
2068+ {
2069+ }
2070+ }
2071+
2072+ #[derive(Debug, Clone, Copy)]
2073+ enum Op {
2074+ AddOutput(usize),
2075+ RemoveOutput(usize),
2076+ FocusOutput(usize),
2077+ AddWindow {
2078+ id: usize,
2079+ bbox: Rectangle<i32, Logical>,
2080+ activate: bool,
2081+ },
2082+ CloseWindow(usize),
2083+ FocusColumnLeft,
2084+ FocusColumnRight,
2085+ MoveColumnLeft,
2086+ MoveColumnRight,
2087+ ConsumeWindowIntoColumn,
2088+ ExpelWindowFromColumn,
2089+ FocusWorkspaceDown,
2090+ FocusWorkspaceUp,
2091+ MoveWindowToWorkspaceDown,
2092+ MoveWindowToWorkspaceUp,
2093+ }
2094+
2095+ impl Op {
2096+ fn apply(self, monitor_set: &mut MonitorSet<TestWindow>) {
2097+ match self {
2098+ Op::AddOutput(id) => {
2099+ let name = format!("output{id}");
2100+ if monitor_set.outputs().any(|o| o.name() == name) {
2101+ return;
2102+ }
2103+
2104+ let output = Output::new(
2105+ name,
2106+ PhysicalProperties {
2107+ size: Size::from((1280, 720)),
2108+ subpixel: Subpixel::Unknown,
2109+ make: String::new(),
2110+ model: String::new(),
2111+ },
2112+ );
2113+ output.change_current_state(
2114+ Some(Mode {
2115+ size: Size::from((1280, 720)),
2116+ refresh: 60,
2117+ }),
2118+ None,
2119+ None,
2120+ None,
2121+ );
2122+ monitor_set.add_output(output.clone());
2123+ }
2124+ Op::RemoveOutput(id) => {
2125+ let name = format!("output{id}");
2126+ let Some(output) = monitor_set.outputs().find(|o| o.name() == name).cloned()
2127+ else {
2128+ return;
2129+ };
2130+
2131+ monitor_set.remove_output(&output);
2132+ }
2133+ Op::FocusOutput(id) => {
2134+ let name = format!("output{id}");
2135+ let Some(output) = monitor_set.outputs().find(|o| o.name() == name).cloned()
2136+ else {
2137+ return;
2138+ };
2139+
2140+ monitor_set.focus_output(&output);
2141+ }
2142+ Op::AddWindow { id, bbox, activate } => {
2143+ let win = TestWindow::new(id, bbox);
2144+ monitor_set.add_window(win, activate);
2145+ }
2146+ Op::CloseWindow(id) => {
2147+ let dummy = TestWindow::new(id, Rectangle::default());
2148+ monitor_set.remove_window(&dummy);
2149+ }
2150+ Op::FocusColumnLeft => monitor_set.focus_left(),
2151+ Op::FocusColumnRight => monitor_set.focus_right(),
2152+ Op::MoveColumnLeft => monitor_set.move_left(),
2153+ Op::MoveColumnRight => monitor_set.move_right(),
2154+ Op::ConsumeWindowIntoColumn => monitor_set.consume_into_column(),
2155+ Op::ExpelWindowFromColumn => monitor_set.expel_from_column(),
2156+ Op::FocusWorkspaceDown => monitor_set.switch_workspace_down(),
2157+ Op::FocusWorkspaceUp => monitor_set.switch_workspace_up(),
2158+ Op::MoveWindowToWorkspaceDown => monitor_set.move_to_workspace_down(),
2159+ Op::MoveWindowToWorkspaceUp => monitor_set.move_to_workspace_up(),
2160+ }
2161+ }
2162+ }
2163+
2164+ #[test]
2165+ fn operations() {
2166+ let every_op = [
2167+ Op::AddOutput(0),
2168+ Op::AddOutput(1),
2169+ Op::AddOutput(2),
2170+ Op::RemoveOutput(0),
2171+ Op::RemoveOutput(1),
2172+ Op::RemoveOutput(2),
2173+ Op::FocusOutput(0),
2174+ Op::FocusOutput(1),
2175+ Op::FocusOutput(2),
2176+ Op::AddWindow {
2177+ id: 0,
2178+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2179+ activate: true,
2180+ },
2181+ Op::AddWindow {
2182+ id: 1,
2183+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2184+ activate: true,
2185+ },
2186+ Op::AddWindow {
2187+ id: 2,
2188+ bbox: Rectangle::from_loc_and_size((0, 0), (100, 200)),
2189+ activate: true,
2190+ },
2191+ Op::CloseWindow(0),
2192+ Op::CloseWindow(1),
2193+ Op::CloseWindow(2),
2194+ Op::FocusColumnLeft,
2195+ Op::FocusColumnRight,
2196+ Op::MoveColumnLeft,
2197+ Op::MoveColumnRight,
2198+ Op::ConsumeWindowIntoColumn,
2199+ Op::ExpelWindowFromColumn,
2200+ Op::FocusWorkspaceDown,
2201+ Op::FocusWorkspaceUp,
2202+ Op::MoveWindowToWorkspaceDown,
2203+ Op::MoveWindowToWorkspaceUp,
2204+ ];
2205+
2206+ for third in every_op {
2207+ for second in every_op {
2208+ for first in every_op {
2209+ eprintln!("{first:?}, {second:?}, {third:?}");
2210+
2211+ let mut monitor_set = MonitorSet::default();
2212+ first.apply(&mut monitor_set);
2213+ monitor_set.verify_invariants();
2214+ second.apply(&mut monitor_set);
2215+ monitor_set.verify_invariants();
2216+ third.apply(&mut monitor_set);
2217+ monitor_set.verify_invariants();
2218+ }
2219+ }
2220+ }
2221+ }
2222+}