Add basic client-server resize animation tests
Browse files at this commit7a6be2a923d8d19d544bb6c52cf172b89ca8298cParent: bba9ca12 changed files +196 -0
- src/tests/animations.rs +195 -0
- src/tests/mod.rs +1 -0
src/tests/animations.rsadded+195 -0
| @@ -0,0 +1,195 @@ | |||
| 1 | +use std::fmt::Write as _; | ||
| 2 | +use std::time::Duration; | ||
| 3 | + | ||
| 4 | +use insta::assert_snapshot; | ||
| 5 | +use niri_config::{AnimationCurve, AnimationKind, Config, EasingParams, FloatOrInt}; | ||
| 6 | +use niri_ipc::SizeChange; | ||
| 7 | +use smithay::utils::{Point, Size}; | ||
| 8 | +use wayland_client::protocol::wl_surface::WlSurface; | ||
| 9 | + | ||
| 10 | +use super::client::ClientId; | ||
| 11 | +use super::*; | ||
| 12 | +use crate::niri::Niri; | ||
| 13 | + | ||
| 14 | +fn format_tiles(niri: &Niri) -> String { | ||
| 15 | + let mut buf = String::new(); | ||
| 16 | + let ws = niri.layout.active_workspace().unwrap(); | ||
| 17 | + let mut tiles: Vec<_> = ws.tiles_with_render_positions().collect(); | ||
| 18 | + | ||
| 19 | + // We sort by id since that gives us a consistent order (from first opened to last), but we | ||
| 20 | + // don't print the id since it's nondeterministic (the id is a global counter across all | ||
| 21 | + // running tests in the same binary). | ||
| 22 | + tiles.sort_by_key(|(tile, _, _)| tile.window().id().get()); | ||
| 23 | + for (tile, pos, _visible) in tiles { | ||
| 24 | + let Size { w, h, .. } = tile.animated_tile_size(); | ||
| 25 | + let Point { x, y, .. } = pos; | ||
| 26 | + writeln!(&mut buf, "{w:>3.0} × {h:>3.0} at x:{x:>3.0} y:{y:>3.0}").unwrap(); | ||
| 27 | + } | ||
| 28 | + buf | ||
| 29 | +} | ||
| 30 | + | ||
| 31 | +fn create_window(f: &mut Fixture, id: ClientId, w: u16, h: u16) -> WlSurface { | ||
| 32 | + let window = f.client(id).create_window(); | ||
| 33 | + let surface = window.surface.clone(); | ||
| 34 | + window.commit(); | ||
| 35 | + f.roundtrip(id); | ||
| 36 | + | ||
| 37 | + let window = f.client(id).window(&surface); | ||
| 38 | + window.attach_new_buffer(); | ||
| 39 | + window.set_size(w, h); | ||
| 40 | + window.ack_last_and_commit(); | ||
| 41 | + f.roundtrip(id); | ||
| 42 | + | ||
| 43 | + surface | ||
| 44 | +} | ||
| 45 | + | ||
| 46 | +fn complete_animations(niri: &mut Niri) { | ||
| 47 | + niri.clock.set_complete_instantly(true); | ||
| 48 | + niri.advance_animations(); | ||
| 49 | + niri.clock.set_complete_instantly(false); | ||
| 50 | +} | ||
| 51 | + | ||
| 52 | +fn set_time(niri: &mut Niri, time: Duration) { | ||
| 53 | + // This is a bit involved because we're dealing with an AdjustableClock that maintains its own | ||
| 54 | + // internal current_time. | ||
| 55 | + | ||
| 56 | + // First, reset current_time to zero by matching unadjusted time to it (at rate 0.0), then | ||
| 57 | + // setting unadjusted time to zero at rate 1.0 (causing current_time to also go to zero). | ||
| 58 | + let now = niri.clock.now(); | ||
| 59 | + niri.clock.set_unadjusted(now); | ||
| 60 | + let _ = niri.clock.now(); | ||
| 61 | + niri.clock.set_unadjusted(Duration::ZERO); | ||
| 62 | + niri.clock.set_rate(1.0); | ||
| 63 | + let _ = niri.clock.now(); | ||
| 64 | + | ||
| 65 | + // Now, set the desired time at rate 1.0. | ||
| 66 | + niri.clock.set_unadjusted(time); | ||
| 67 | + let _ = niri.clock.now(); | ||
| 68 | + | ||
| 69 | + // Freeze the clock so that clear() inside the niri loop callback followed by some get() | ||
| 70 | + // doesn't replace it with the monotonic time. | ||
| 71 | + niri.clock.set_rate(0.0); | ||
| 72 | +} | ||
| 73 | + | ||
| 74 | +// Sets up a fixture with linear animations, a renderer, and an output. | ||
| 75 | +fn set_up() -> Fixture { | ||
| 76 | + const LINEAR: AnimationKind = AnimationKind::Easing(EasingParams { | ||
| 77 | + duration_ms: 1000, | ||
| 78 | + curve: AnimationCurve::Linear, | ||
| 79 | + }); | ||
| 80 | + | ||
| 81 | + let mut config = Config::default(); | ||
| 82 | + config.layout.gaps = FloatOrInt(0.0); | ||
| 83 | + config.animations.window_resize.anim.kind = LINEAR; | ||
| 84 | + config.animations.window_movement.0.kind = LINEAR; | ||
| 85 | + | ||
| 86 | + let mut f = Fixture::with_config(config); | ||
| 87 | + f.niri_state().backend.headless().add_renderer().unwrap(); | ||
| 88 | + f.add_output(1, (1920, 1080)); | ||
| 89 | + | ||
| 90 | + f | ||
| 91 | +} | ||
| 92 | + | ||
| 93 | +fn set_up_two_in_column() -> (Fixture, ClientId, WlSurface, WlSurface) { | ||
| 94 | + let mut f = set_up(); | ||
| 95 | + | ||
| 96 | + let id = f.add_client(); | ||
| 97 | + | ||
| 98 | + let surface1 = create_window(&mut f, id, 100, 100); | ||
| 99 | + let surface2 = create_window(&mut f, id, 200, 200); | ||
| 100 | + f.double_roundtrip(id); | ||
| 101 | + | ||
| 102 | + let _ = f.client(id).window(&surface1).recent_configures(); | ||
| 103 | + let _ = f.client(id).window(&surface2).recent_configures(); | ||
| 104 | + | ||
| 105 | + // Consume into one column. | ||
| 106 | + f.niri().layout.focus_left(); | ||
| 107 | + f.niri().layout.consume_into_column(); | ||
| 108 | + f.double_roundtrip(id); | ||
| 109 | + | ||
| 110 | + // Commit for the column consume. | ||
| 111 | + let window = f.client(id).window(&surface1); | ||
| 112 | + window.ack_last_and_commit(); | ||
| 113 | + | ||
| 114 | + let window = f.client(id).window(&surface2); | ||
| 115 | + window.ack_last_and_commit(); | ||
| 116 | + | ||
| 117 | + f.double_roundtrip(id); | ||
| 118 | + | ||
| 119 | + set_time(f.niri(), Duration::ZERO); | ||
| 120 | + complete_animations(f.niri()); | ||
| 121 | + | ||
| 122 | + (f, id, surface1, surface2) | ||
| 123 | +} | ||
| 124 | + | ||
| 125 | +#[test] | ||
| 126 | +fn height_resize_animates_next_y() { | ||
| 127 | + let (mut f, id, surface1, surface2) = set_up_two_in_column(); | ||
| 128 | + | ||
| 129 | + // Issue a resize. | ||
| 130 | + f.niri() | ||
| 131 | + .layout | ||
| 132 | + .set_window_height(None, SizeChange::AdjustFixed(-50)); | ||
| 133 | + f.double_roundtrip(id); | ||
| 134 | + | ||
| 135 | + // The top window shrinks in response, the bottom remains as is. | ||
| 136 | + let window = f.client(id).window(&surface1); | ||
| 137 | + window.set_size(100, 50); | ||
| 138 | + window.ack_last_and_commit(); | ||
| 139 | + let window = f.client(id).window(&surface2); | ||
| 140 | + window.ack_last_and_commit(); | ||
| 141 | + | ||
| 142 | + // This starts the resize animation for the top window and the Y move for the bottom. | ||
| 143 | + f.roundtrip(id); | ||
| 144 | + | ||
| 145 | + // No time had passed yet, so we're at the initial state. | ||
| 146 | + assert_snapshot!(format_tiles(f.niri()), @r" | ||
| 147 | + 100 × 100 at x: 0 y: 0 | ||
| 148 | + 200 × 200 at x: 0 y:100 | ||
| 149 | + "); | ||
| 150 | + | ||
| 151 | + // Advance the time halfway. | ||
| 152 | + set_time(f.niri(), Duration::from_millis(500)); | ||
| 153 | + f.niri().advance_animations(); | ||
| 154 | + | ||
| 155 | + // Top window is half-resized at 75 px tall, bottom window is at y=75 matching it. | ||
| 156 | + assert_snapshot!(format_tiles(f.niri()), @r" | ||
| 157 | + 100 × 75 at x: 0 y: 0 | ||
| 158 | + 200 × 200 at x: 0 y: 75 | ||
| 159 | + "); | ||
| 160 | + | ||
| 161 | + // Advance the time to completion. | ||
| 162 | + set_time(f.niri(), Duration::from_millis(1000)); | ||
| 163 | + f.niri().advance_animations(); | ||
| 164 | + | ||
| 165 | + // Final state at 50 px. | ||
| 166 | + assert_snapshot!(format_tiles(f.niri()), @r" | ||
| 167 | + 100 × 50 at x: 0 y: 0 | ||
| 168 | + 200 × 200 at x: 0 y: 50 | ||
| 169 | + "); | ||
| 170 | +} | ||
| 171 | + | ||
| 172 | +#[test] | ||
| 173 | +fn clientside_height_change_doesnt_animate() { | ||
| 174 | + let (mut f, id, surface1, _surface2) = set_up_two_in_column(); | ||
| 175 | + | ||
| 176 | + // The initial state. | ||
| 177 | + assert_snapshot!(format_tiles(f.niri()), @r" | ||
| 178 | + 100 × 100 at x: 0 y: 0 | ||
| 179 | + 200 × 200 at x: 0 y:100 | ||
| 180 | + "); | ||
| 181 | + | ||
| 182 | + // The top window shrinks by itself, without a niri-issued resize. | ||
| 183 | + let window = f.client(id).window(&surface1); | ||
| 184 | + window.set_size(100, 50); | ||
| 185 | + window.commit(); | ||
| 186 | + | ||
| 187 | + // This does not start any animations. | ||
| 188 | + f.roundtrip(id); | ||
| 189 | + | ||
| 190 | + // No time had passed yet, but we are at the final state right away. | ||
| 191 | + assert_snapshot!(format_tiles(f.niri()), @r" | ||
| 192 | + 100 × 50 at x: 0 y: 0 | ||
| 193 | + 200 × 200 at x: 0 y: 50 | ||
| 194 | + "); | ||
| 195 | +} | ||
src/tests/mod.rsmodified+1 -0
| @@ -4,6 +4,7 @@ mod client; | |||
| 4 | mod fixture; | 4 | mod fixture; |
| 5 | mod server; | 5 | mod server; |
| 6 | 6 | ||
| 7 | +mod animations; | ||
| 7 | mod floating; | 8 | mod floating; |
| 8 | mod fullscreen; | 9 | mod fullscreen; |
| 9 | mod layer_shell; | 10 | mod layer_shell; |