tests: Add layer-shell scaffolding and an overflow test
Browse files at this commitce501bca9e753093170123ed62ae7bb6796a8255Parent: 45e9bb73 changed files +322 -0
- src/tests/client.rs +231 -0
- src/tests/layer_shell.rs +90 -0
- src/tests/mod.rs +1 -0
src/tests/client.rsmodified+231 -0
| @@ -16,6 +16,12 @@ use smithay::reexports::wayland_protocols::wp::viewporter::client::wp_viewporter | ||
| 16 | 16 | use smithay::reexports::wayland_protocols::xdg::shell::client::xdg_surface::{self, XdgSurface}; |
| 17 | 17 | use smithay::reexports::wayland_protocols::xdg::shell::client::xdg_toplevel::{self, XdgToplevel}; |
| 18 | 18 | use smithay::reexports::wayland_protocols::xdg::shell::client::xdg_wm_base::{self, XdgWmBase}; |
| 19 | +use smithay::reexports::wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_shell_v1::{ | |
| 20 | + self, ZwlrLayerShellV1, | |
| 21 | +}; | |
| 22 | +use smithay::reexports::wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1::{ | |
| 23 | + self, ZwlrLayerSurfaceV1, | |
| 24 | +}; | |
| 19 | 25 | use wayland_backend::client::Backend; |
| 20 | 26 | use wayland_client::globals::Global; |
| 21 | 27 | use wayland_client::protocol::wl_buffer::{self, WlBuffer}; |
| @@ -46,10 +52,12 @@ pub struct State { | ||
| 46 | 52 | |
| 47 | 53 | pub compositor: Option<WlCompositor>, |
| 48 | 54 | pub xdg_wm_base: Option<XdgWmBase>, |
| 55 | + pub layer_shell: Option<ZwlrLayerShellV1>, | |
| 49 | 56 | pub spbm: Option<WpSinglePixelBufferManagerV1>, |
| 50 | 57 | pub viewporter: Option<WpViewporter>, |
| 51 | 58 | |
| 52 | 59 | pub windows: Vec<Window>, |
| 60 | + pub layers: Vec<LayerSurface>, | |
| 53 | 61 | } |
| 54 | 62 | |
| 55 | 63 | pub struct Window { |
| @@ -67,6 +75,19 @@ pub struct Window { | ||
| 67 | 75 | pub configures_looked_at: usize, |
| 68 | 76 | } |
| 69 | 77 | |
| 78 | +pub struct LayerSurface { | |
| 79 | + pub qh: QueueHandle<State>, | |
| 80 | + pub spbm: WpSinglePixelBufferManagerV1, | |
| 81 | + | |
| 82 | + pub surface: WlSurface, | |
| 83 | + pub layer_surface: ZwlrLayerSurfaceV1, | |
| 84 | + pub viewport: WpViewport, | |
| 85 | + pub configures_received: Vec<(u32, LayerConfigure)>, | |
| 86 | + pub close_requested: bool, | |
| 87 | + | |
| 88 | + pub configures_looked_at: usize, | |
| 89 | +} | |
| 90 | + | |
| 70 | 91 | #[derive(Debug, Clone, Default)] |
| 71 | 92 | pub struct Configure { |
| 72 | 93 | pub size: (i32, i32), |
| @@ -74,6 +95,30 @@ pub struct Configure { | ||
| 74 | 95 | pub states: Vec<xdg_toplevel::State>, |
| 75 | 96 | } |
| 76 | 97 | |
| 98 | +#[derive(Debug, Clone, Copy)] | |
| 99 | +pub struct LayerConfigure { | |
| 100 | + pub size: (u32, u32), | |
| 101 | +} | |
| 102 | + | |
| 103 | +#[derive(Clone, Copy, Default)] | |
| 104 | +pub struct LayerMargin { | |
| 105 | + pub top: i32, | |
| 106 | + pub right: i32, | |
| 107 | + pub bottom: i32, | |
| 108 | + pub left: i32, | |
| 109 | +} | |
| 110 | + | |
| 111 | +#[derive(Clone, Copy, Default)] | |
| 112 | +pub struct LayerConfigureProps { | |
| 113 | + pub size: Option<(u32, u32)>, | |
| 114 | + pub anchor: Option<zwlr_layer_surface_v1::Anchor>, | |
| 115 | + pub exclusive_zone: Option<i32>, | |
| 116 | + pub margin: Option<LayerMargin>, | |
| 117 | + pub kb_interactivity: Option<zwlr_layer_surface_v1::KeyboardInteractivity>, | |
| 118 | + pub layer: Option<zwlr_layer_shell_v1::Layer>, | |
| 119 | + pub exclusive_edge: Option<zwlr_layer_surface_v1::Anchor>, | |
| 120 | +} | |
| 121 | + | |
| 77 | 122 | #[derive(Default)] |
| 78 | 123 | pub struct SyncData { |
| 79 | 124 | pub done: AtomicBool, |
| @@ -103,6 +148,13 @@ impl fmt::Display for Configure { | ||
| 103 | 148 | } |
| 104 | 149 | } |
| 105 | 150 | |
| 151 | +impl fmt::Display for LayerConfigure { | |
| 152 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
| 153 | + write!(f, "size: {} × {}", self.size.0, self.size.1)?; | |
| 154 | + Ok(()) | |
| 155 | + } | |
| 156 | +} | |
| 157 | + | |
| 106 | 158 | impl Client { |
| 107 | 159 | pub fn new(stream: UnixStream) -> Self { |
| 108 | 160 | let id = ClientId::next(); |
| @@ -126,9 +178,11 @@ impl Client { | ||
| 126 | 178 | outputs: HashMap::new(), |
| 127 | 179 | compositor: None, |
| 128 | 180 | xdg_wm_base: None, |
| 181 | + layer_shell: None, | |
| 129 | 182 | spbm: None, |
| 130 | 183 | viewporter: None, |
| 131 | 184 | windows: Vec::new(), |
| 185 | + layers: Vec::new(), | |
| 132 | 186 | }; |
| 133 | 187 | |
| 134 | 188 | Self { |
| @@ -162,6 +216,19 @@ impl Client { | ||
| 162 | 216 | self.state.window(surface) |
| 163 | 217 | } |
| 164 | 218 | |
| 219 | + pub fn create_layer( | |
| 220 | + &mut self, | |
| 221 | + output: Option<&WlOutput>, | |
| 222 | + layer: zwlr_layer_shell_v1::Layer, | |
| 223 | + namespace: &str, | |
| 224 | + ) -> &mut LayerSurface { | |
| 225 | + self.state.create_layer(output, layer, namespace.to_owned()) | |
| 226 | + } | |
| 227 | + | |
| 228 | + pub fn layer(&mut self, surface: &WlSurface) -> &mut LayerSurface { | |
| 229 | + self.state.layer(surface) | |
| 230 | + } | |
| 231 | + | |
| 165 | 232 | pub fn output(&mut self, name: &str) -> WlOutput { |
| 166 | 233 | self.state |
| 167 | 234 | .outputs |
| @@ -209,6 +276,45 @@ impl State { | ||
| 209 | 276 | .find(|w| w.surface == *surface) |
| 210 | 277 | .unwrap() |
| 211 | 278 | } |
| 279 | + | |
| 280 | + pub fn create_layer( | |
| 281 | + &mut self, | |
| 282 | + output: Option<&WlOutput>, | |
| 283 | + layer: zwlr_layer_shell_v1::Layer, | |
| 284 | + namespace: String, | |
| 285 | + ) -> &mut LayerSurface { | |
| 286 | + let compositor = self.compositor.as_ref().unwrap(); | |
| 287 | + let layer_shell = self.layer_shell.as_ref().unwrap(); | |
| 288 | + let viewporter = self.viewporter.as_ref().unwrap(); | |
| 289 | + | |
| 290 | + let surface = compositor.create_surface(&self.qh, ()); | |
| 291 | + let layer_surface = | |
| 292 | + layer_shell.get_layer_surface(&surface, output, layer, namespace, &self.qh, ()); | |
| 293 | + let viewport = viewporter.get_viewport(&surface, &self.qh, ()); | |
| 294 | + | |
| 295 | + let layer_surface = LayerSurface { | |
| 296 | + qh: self.qh.clone(), | |
| 297 | + spbm: self.spbm.clone().unwrap(), | |
| 298 | + | |
| 299 | + surface, | |
| 300 | + layer_surface, | |
| 301 | + viewport, | |
| 302 | + configures_received: Vec::new(), | |
| 303 | + close_requested: false, | |
| 304 | + | |
| 305 | + configures_looked_at: 0, | |
| 306 | + }; | |
| 307 | + | |
| 308 | + self.layers.push(layer_surface); | |
| 309 | + self.layers.last_mut().unwrap() | |
| 310 | + } | |
| 311 | + | |
| 312 | + pub fn layer(&mut self, surface: &WlSurface) -> &mut LayerSurface { | |
| 313 | + self.layers | |
| 314 | + .iter_mut() | |
| 315 | + .find(|w| w.surface == *surface) | |
| 316 | + .unwrap() | |
| 317 | + } | |
| 212 | 318 | } |
| 213 | 319 | |
| 214 | 320 | impl Window { |
| @@ -269,6 +375,83 @@ impl Window { | ||
| 269 | 375 | } |
| 270 | 376 | } |
| 271 | 377 | |
| 378 | +impl LayerSurface { | |
| 379 | + pub fn commit(&self) { | |
| 380 | + self.surface.commit(); | |
| 381 | + } | |
| 382 | + | |
| 383 | + pub fn ack_last(&self) { | |
| 384 | + let serial = self.configures_received.last().unwrap().0; | |
| 385 | + self.layer_surface.ack_configure(serial); | |
| 386 | + } | |
| 387 | + | |
| 388 | + pub fn ack_last_and_commit(&self) { | |
| 389 | + self.ack_last(); | |
| 390 | + self.commit(); | |
| 391 | + } | |
| 392 | + | |
| 393 | + pub fn set_configure_props(&self, props: LayerConfigureProps) { | |
| 394 | + let LayerConfigureProps { | |
| 395 | + size, | |
| 396 | + anchor, | |
| 397 | + exclusive_zone, | |
| 398 | + margin, | |
| 399 | + kb_interactivity, | |
| 400 | + layer, | |
| 401 | + exclusive_edge, | |
| 402 | + } = props; | |
| 403 | + | |
| 404 | + if let Some(x) = size { | |
| 405 | + self.layer_surface.set_size(x.0, x.1); | |
| 406 | + } | |
| 407 | + if let Some(x) = anchor { | |
| 408 | + self.layer_surface.set_anchor(x); | |
| 409 | + } | |
| 410 | + if let Some(x) = exclusive_zone { | |
| 411 | + self.layer_surface.set_exclusive_zone(x); | |
| 412 | + } | |
| 413 | + if let Some(x) = margin { | |
| 414 | + self.layer_surface | |
| 415 | + .set_margin(x.top, x.right, x.bottom, x.left); | |
| 416 | + } | |
| 417 | + if let Some(x) = kb_interactivity { | |
| 418 | + self.layer_surface.set_keyboard_interactivity(x); | |
| 419 | + } | |
| 420 | + if let Some(x) = layer { | |
| 421 | + self.layer_surface.set_layer(x); | |
| 422 | + } | |
| 423 | + if let Some(x) = exclusive_edge { | |
| 424 | + self.layer_surface.set_exclusive_edge(x); | |
| 425 | + } | |
| 426 | + } | |
| 427 | + | |
| 428 | + pub fn attach_new_buffer(&self) { | |
| 429 | + let buffer = self.spbm.create_u32_rgba_buffer(0, 0, 0, 0, &self.qh, ()); | |
| 430 | + self.surface.attach(Some(&buffer), 0, 0); | |
| 431 | + } | |
| 432 | + | |
| 433 | + pub fn set_size(&self, w: u16, h: u16) { | |
| 434 | + self.viewport.set_destination(i32::from(w), i32::from(h)); | |
| 435 | + } | |
| 436 | + | |
| 437 | + pub fn recent_configures(&mut self) -> impl Iterator<Item = &LayerConfigure> { | |
| 438 | + let start = self.configures_looked_at; | |
| 439 | + self.configures_looked_at = self.configures_received.len(); | |
| 440 | + self.configures_received[start..].iter().map(|(_, c)| c) | |
| 441 | + } | |
| 442 | + | |
| 443 | + pub fn format_recent_configures(&mut self) -> String { | |
| 444 | + let mut buf = String::new(); | |
| 445 | + for configure in self.recent_configures() { | |
| 446 | + if !buf.is_empty() { | |
| 447 | + buf.push('\n'); | |
| 448 | + } | |
| 449 | + write!(buf, "{configure}").unwrap(); | |
| 450 | + } | |
| 451 | + buf | |
| 452 | + } | |
| 453 | +} | |
| 454 | + | |
| 272 | 455 | impl Dispatch<WlCallback, Arc<SyncData>> for State { |
| 273 | 456 | fn event( |
| 274 | 457 | _state: &mut Self, |
| @@ -306,6 +489,9 @@ impl Dispatch<WlRegistry, ()> for State { | ||
| 306 | 489 | } else if interface == XdgWmBase::interface().name { |
| 307 | 490 | let version = min(version, XdgWmBase::interface().version); |
| 308 | 491 | state.xdg_wm_base = Some(registry.bind(name, version, qh, ())); |
| 492 | + } else if interface == ZwlrLayerShellV1::interface().name { | |
| 493 | + let version = min(version, ZwlrLayerShellV1::interface().version); | |
| 494 | + state.layer_shell = Some(registry.bind(name, version, qh, ())); | |
| 309 | 495 | } else if interface == WpSinglePixelBufferManagerV1::interface().name { |
| 310 | 496 | let version = min(version, WpSinglePixelBufferManagerV1::interface().version); |
| 311 | 497 | state.spbm = Some(registry.bind(name, version, qh, ())); |
| @@ -385,6 +571,19 @@ impl Dispatch<XdgWmBase, ()> for State { | ||
| 385 | 571 | } |
| 386 | 572 | } |
| 387 | 573 | |
| 574 | +impl Dispatch<ZwlrLayerShellV1, ()> for State { | |
| 575 | + fn event( | |
| 576 | + _state: &mut Self, | |
| 577 | + _proxy: &ZwlrLayerShellV1, | |
| 578 | + _event: <ZwlrLayerShellV1 as wayland_client::Proxy>::Event, | |
| 579 | + _data: &(), | |
| 580 | + _conn: &Connection, | |
| 581 | + _qhandle: &QueueHandle<Self>, | |
| 582 | + ) { | |
| 583 | + unreachable!() | |
| 584 | + } | |
| 585 | +} | |
| 586 | + | |
| 388 | 587 | impl Dispatch<WlSurface, ()> for State { |
| 389 | 588 | fn event( |
| 390 | 589 | _state: &mut Self, |
| @@ -470,6 +669,38 @@ impl Dispatch<XdgToplevel, ()> for State { | ||
| 470 | 669 | } |
| 471 | 670 | } |
| 472 | 671 | |
| 672 | +impl Dispatch<ZwlrLayerSurfaceV1, ()> for State { | |
| 673 | + fn event( | |
| 674 | + state: &mut Self, | |
| 675 | + layer_surface: &ZwlrLayerSurfaceV1, | |
| 676 | + event: <ZwlrLayerSurfaceV1 as wayland_client::Proxy>::Event, | |
| 677 | + _data: &(), | |
| 678 | + _conn: &Connection, | |
| 679 | + _qhandle: &QueueHandle<Self>, | |
| 680 | + ) { | |
| 681 | + let layer_surface = state | |
| 682 | + .layers | |
| 683 | + .iter_mut() | |
| 684 | + .find(|w| w.layer_surface == *layer_surface) | |
| 685 | + .unwrap(); | |
| 686 | + | |
| 687 | + match event { | |
| 688 | + zwlr_layer_surface_v1::Event::Configure { | |
| 689 | + serial, | |
| 690 | + width, | |
| 691 | + height, | |
| 692 | + } => { | |
| 693 | + let configure = LayerConfigure { | |
| 694 | + size: (width, height), | |
| 695 | + }; | |
| 696 | + layer_surface.configures_received.push((serial, configure)); | |
| 697 | + } | |
| 698 | + zwlr_layer_surface_v1::Event::Closed => layer_surface.close_requested = true, | |
| 699 | + _ => unreachable!(), | |
| 700 | + } | |
| 701 | + } | |
| 702 | +} | |
| 703 | + | |
| 473 | 704 | impl Dispatch<WlBuffer, ()> for State { |
| 474 | 705 | fn event( |
| 475 | 706 | _state: &mut Self, |
src/tests/layer_shell.rsadded+90 -0
| @@ -0,0 +1,90 @@ | ||
| 1 | +use insta::assert_snapshot; | |
| 2 | +use smithay::reexports::wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_shell_v1::Layer; | |
| 3 | +use smithay::reexports::wayland_protocols_wlr::layer_shell::v1::client::zwlr_layer_surface_v1::Anchor; | |
| 4 | + | |
| 5 | +use super::*; | |
| 6 | +use crate::tests::client::{LayerConfigureProps, LayerMargin}; | |
| 7 | + | |
| 8 | +#[test] | |
| 9 | +fn simple_top_anchor() { | |
| 10 | + let mut f = Fixture::new(); | |
| 11 | + f.add_output(1, (1920, 1080)); | |
| 12 | + let id = f.add_client(); | |
| 13 | + | |
| 14 | + let layer = f.client(id).create_layer(None, Layer::Top, ""); | |
| 15 | + let surface = layer.surface.clone(); | |
| 16 | + layer.set_configure_props(LayerConfigureProps { | |
| 17 | + anchor: Some(Anchor::Left | Anchor::Right | Anchor::Top), | |
| 18 | + size: Some((0, 50)), | |
| 19 | + ..Default::default() | |
| 20 | + }); | |
| 21 | + layer.commit(); | |
| 22 | + f.roundtrip(id); | |
| 23 | + | |
| 24 | + let layer = f.client(id).layer(&surface); | |
| 25 | + layer.attach_new_buffer(); | |
| 26 | + layer.set_size(100, 100); | |
| 27 | + layer.ack_last_and_commit(); | |
| 28 | + f.double_roundtrip(id); | |
| 29 | + | |
| 30 | + let layer = f.client(id).layer(&surface); | |
| 31 | + assert_snapshot!(layer.format_recent_configures(), @"size: 1920 × 50"); | |
| 32 | +} | |
| 33 | + | |
| 34 | +#[test] | |
| 35 | +fn margin_overflow() { | |
| 36 | + let mut f = Fixture::new(); | |
| 37 | + f.add_output(1, (1920, 1080)); | |
| 38 | + let id = f.add_client(); | |
| 39 | + | |
| 40 | + let layer = f.client(id).create_layer(None, Layer::Top, ""); | |
| 41 | + let surface = layer.surface.clone(); | |
| 42 | + layer.set_configure_props(LayerConfigureProps { | |
| 43 | + anchor: Some(Anchor::Left | Anchor::Right | Anchor::Top | Anchor::Bottom), | |
| 44 | + margin: Some(LayerMargin { | |
| 45 | + top: i32::MAX, | |
| 46 | + right: i32::MAX, | |
| 47 | + bottom: i32::MAX, | |
| 48 | + left: i32::MAX, | |
| 49 | + }), | |
| 50 | + exclusive_zone: Some(i32::MAX), | |
| 51 | + ..Default::default() | |
| 52 | + }); | |
| 53 | + layer.commit(); | |
| 54 | + f.roundtrip(id); | |
| 55 | + | |
| 56 | + let layer = f.client(id).layer(&surface); | |
| 57 | + layer.attach_new_buffer(); | |
| 58 | + layer.set_size(100, 100); | |
| 59 | + layer.ack_last_and_commit(); | |
| 60 | + f.double_roundtrip(id); | |
| 61 | + | |
| 62 | + let layer = f.client(id).layer(&surface); | |
| 63 | + assert_snapshot!(layer.format_recent_configures(), @"size: 0 × 0"); | |
| 64 | + | |
| 65 | + // Add a second one for good measure. | |
| 66 | + let layer = f.client(id).create_layer(None, Layer::Top, ""); | |
| 67 | + let surface = layer.surface.clone(); | |
| 68 | + layer.set_configure_props(LayerConfigureProps { | |
| 69 | + anchor: Some(Anchor::Left | Anchor::Right | Anchor::Top | Anchor::Bottom), | |
| 70 | + margin: Some(LayerMargin { | |
| 71 | + top: i32::MAX, | |
| 72 | + right: i32::MAX, | |
| 73 | + bottom: i32::MAX, | |
| 74 | + left: i32::MAX, | |
| 75 | + }), | |
| 76 | + exclusive_zone: Some(i32::MAX), | |
| 77 | + ..Default::default() | |
| 78 | + }); | |
| 79 | + layer.commit(); | |
| 80 | + f.roundtrip(id); | |
| 81 | + | |
| 82 | + let layer = f.client(id).layer(&surface); | |
| 83 | + layer.attach_new_buffer(); | |
| 84 | + layer.set_size(100, 100); | |
| 85 | + layer.ack_last_and_commit(); | |
| 86 | + f.double_roundtrip(id); | |
| 87 | + | |
| 88 | + let layer = f.client(id).layer(&surface); | |
| 89 | + assert_snapshot!(layer.format_recent_configures(), @"size: 0 × 0"); | |
| 90 | +} |
src/tests/mod.rsmodified+1 -0
| @@ -6,5 +6,6 @@ mod server; | ||
| 6 | 6 | |
| 7 | 7 | mod floating; |
| 8 | 8 | mod fullscreen; |
| 9 | +mod layer_shell; | |
| 9 | 10 | mod transactions; |
| 10 | 11 | mod window_opening; |