Skip to content

Commit

Permalink
Use Xvfb for automated tests (#37)
Browse files Browse the repository at this point in the history
* test buttonpress

* wait for xephyr to start

* install xephyr

* start events module

* try starting x

* /usr/bin/X not found

* Only console users are allowed to run the X server

* need sudo

* forgot sudo echo doesn't work

* revert sudo stuff, try -ac

* use xvfb not xephyr for automated tests

* install libxcb

* try a different one

* print xcb connection error

* specify display for xcb too

* delete unused benches

* use a labeled block to avoid clippy

* try_wait after kill to handle clippy

* bring back linux cfg for xcon stuff

* xquartz might have xvfb

* try lowercase xvfb on mac

* more debugging on mac

* both xvfb and Xvfb are shown in which but PATH isn't updating
  • Loading branch information
ntBre authored Dec 5, 2024
1 parent cc32dc8 commit 147d42b
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ jobs:
if: ${{ ! matrix.is_mac }}
run: |
sudo apt-get update
sudo apt-get install libxft-dev libxinerama-dev
sudo apt-get install libxft-dev libxinerama-dev xserver-xephyr libxcb1-dev
- name: Install X libraries (mac)
if: ${{ matrix.is_mac }}
run: |
brew install libxft libxinerama
brew install --cask xquartz
echo "PATH=$PATH:/opt/X11/bin" >> $GITHUB_ENV
- name: Install ${{ matrix.toolchain }}
uses: dtolnay/rust-toolchain@master
with:
Expand All @@ -39,5 +41,3 @@ jobs:
# https://twitter.com/jonhoo/status/1571290371124260865
- name: cargo test --locked
run: cargo test --locked --all-features --lib --bins --tests --examples -- --test-threads=1
- name: cargo bench
run: cargo bench
39 changes: 39 additions & 0 deletions src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use x11::xlib::{XButtonEvent, XEvent, _XDisplay};

pub enum Event {
Button(XButtonEvent),
}

impl Event {
pub fn button(
window: u64,
button: u32,
x: i32,
display: *mut _XDisplay,
state: u32,
) -> Self {
Self::Button(XButtonEvent {
type_: 0,
serial: 0,
send_event: 0,
display,
window,
root: 0,
subwindow: 0,
time: 0,
x,
y: 0,
x_root: 0,
y_root: 0,
state,
button,
same_screen: 0,
})
}

pub fn into_button(self) -> XEvent {
match self {
Event::Button(button) => XEvent { button },
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::ffi::{c_char, c_int, c_uint};
use enums::Clk;

pub mod enums;
pub mod events;

pub type Window = u64;

Expand Down
86 changes: 86 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2907,3 +2907,89 @@ fn main() {
drop(Box::from_raw(XCON));
}
}

#[cfg(test)]
mod tests {
use rwm::events::Event;
use xlib::Button1;

use super::*;

use std::process::Command;

#[test]
fn main() {
// setup xephyr
#[cfg(target_os = "linux")]
let mut cmd = Command::new("Xvfb").arg(":1").spawn().unwrap();

#[cfg(not(target_os = "linux"))]
let mut cmd = Command::new("xvfb").arg(":1").spawn().unwrap();

// wait for xephyr to start
unsafe {
while DPY.is_null() {
DPY = xlib::XOpenDisplay(c":1.0".as_ptr());
}
}

// goto for killing xephyr no matter what
let ok = 'defer: {
#[cfg(target_os = "linux")]
unsafe {
let xcon = match Connection::connect(Some(":1.0")) {
Ok((xcon, _)) => xcon,
Err(e) => {
eprintln!("rwm: cannot get xcb connection: {e:?}");
break 'defer false;
}
};
XCON = Box::into_raw(Box::new(xcon));
}
checkotherwm();
setup();
scan();

// instead of calling `run`, manually send some XEvents

// test that a mouse click on the initial (tiling) layout icon
// switches to floating mode
handlers::buttonpress(
&mut Event::button(
(unsafe { *SELMON }).barwin,
Button1,
CONFIG
.tags
.iter()
.map(|tag| textw(tag.as_ptr()))
.sum::<i32>()
+ 5,
unsafe { DPY },
0,
)
.into_button(),
);
unsafe {
assert!((*(*SELMON).lt[(*SELMON).sellt as usize])
.arrange
.is_none());
}

cleanup();
unsafe {
xlib::XCloseDisplay(DPY);

#[cfg(target_os = "linux")]
drop(Box::from_raw(XCON));
}

break 'defer true;
};

// kill xephyr when finished
cmd.kill().unwrap();
cmd.try_wait().unwrap();

assert!(ok);
}
}

0 comments on commit 147d42b

Please sign in to comment.