Skip to content

Commit

Permalink
test(clafrica): implement a test sandbox (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
pythonbrad authored Jun 29, 2023
1 parent fc27710 commit 866fb01
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 8 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ env:
jobs:
coverage:
runs-on: ubuntu-latest
env:
DISPLAY: ':99'
steps:
- uses: actions/checkout@v3
- uses: awalsh128/cache-apt-pkgs-action@latest
with:
packages: libxtst-dev libevdev-dev libxdo-dev
version: 1.0

- name: Setup headless environment
run: |
Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
- name: Dependencies
run: |
sudo apt-get install libxtst-dev libevdev-dev libxdo-dev --assume-yes
Expand Down Expand Up @@ -57,7 +63,7 @@ jobs:
--branch \
--ignore-not-existing \
--ignore "/*" \
--ignore "clafrica/src/{lib,main}.rs" \
--ignore "clafrica/src/{main}.rs" \
--excl-line "#\\[derive\\(" \
-o ./coverage/lcov.info
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
include:
- os: ubuntu-latest
headless: Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
- os: macos-latest
extra: -- --skip test_simple

steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -51,5 +53,5 @@ jobs:

- name: Run tests
run: |
cargo test --all-features --lib --bins --tests --verbose
cargo test --benches
cargo test --all-features --lib --bins --tests --verbose ${{matrix.extra}}
cargo test --benches ${{matrix.extra}}
24 changes: 20 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions clafrica/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ enigo = "0.1.2"
rdev = "0.5.2"
serde = { version = "1.0.163", features = ["serde_derive"] }
toml = "0.7.3"

[dev-dependencies]
rstk = "0.1.0"
18 changes: 18 additions & 0 deletions clafrica/data/test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Test file

[info]
name = "Test code"
description = "Test code for testing purpose."

[core]
buffer_size = 128

[data]
af = "ɑ"
af3 = "ɑ̄"
aff = "ɑɑ"
aff3 = "ɑ̄ɑ̄"
cc = { value = "ç", alias = ["ccced"]}
uu = "ʉ"
uu3 = "ʉ̄"
uuaf3 = "ʉ̄ɑ̄"
129 changes: 128 additions & 1 deletion clafrica/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,131 @@ pub fn run(config: config::Config, mut frontend: impl Frontend) -> Result<(), io
}

#[cfg(test)]
mod tests {}
mod tests {
use crate::{api, config::Config, run};
use rdev::{self, Button, EventType::*, Key::*};
use rstk::{self, TkPackLayout};
use std::{thread, time::Duration};

macro_rules! input {
( $( $key:expr )*, $delay:expr ) => (
$(
thread::sleep($delay);
rdev::simulate(&KeyPress($key)).unwrap();
thread::sleep($delay);
rdev::simulate(&KeyRelease($key)).unwrap();
)*
);
}

macro_rules! output {
( $textfield: expr, $expected: expr ) => {
// A loop to be sure to got something stable
loop {
let a = $textfield.get_to_end((1, 0));
let b = $textfield.get_to_end((1, 0));

if (a == b) {
let content = a.chars().filter(|c| *c != '\0').collect::<String>();
let content = content.trim();

assert_eq!(content, $expected);
break;
}
}
};
}

fn start_clafrica() {
use std::path::Path;

let test_config = Config::from_file(Path::new("./data/test.toml")).unwrap();

thread::spawn(move || {
run(test_config, api::Console).unwrap();
});
}

fn start_sandbox() -> rstk::TkText {
let root = rstk::trace_with("wish").unwrap();
root.title("Clafrica Test Environment");
let input_field = rstk::make_text(&root);
input_field.width(50);
input_field.height(12);
input_field.pack().layout();
root.geometry(200, 200, 0, 0);
rstk::tell_wish(
r#"
bind . <Leave> { destroy . };
chan configure stdout -encoding utf-8;
"#,
);
thread::sleep(Duration::from_secs(1));
input_field
}

#[test]
fn test_simple() {
let typing_speed_ms = Duration::from_millis(300);

// To detect excessive backspace
const LIMIT: &str = "bbb";

// Start the clafrica
start_clafrica();

// Start the sandbox
let textfield = start_sandbox();

rdev::simulate(&MouseMove { x: 100.0, y: 100.0 }).unwrap();
thread::sleep(typing_speed_ms);
rdev::simulate(&ButtonPress(Button::Left)).unwrap();
thread::sleep(typing_speed_ms);
rdev::simulate(&ButtonRelease(Button::Left)).unwrap();
thread::sleep(typing_speed_ms);

input!(KeyB KeyB KeyB Escape, typing_speed_ms);

input!(KeyU Backspace KeyU KeyU Backspace KeyU, typing_speed_ms);

input!(KeyC KeyC KeyC KeyE KeyD, typing_speed_ms);
input!(KeyU KeyU KeyA KeyF, typing_speed_ms);

input!(CapsLock, typing_speed_ms);
input!(Num3, typing_speed_ms);
input!(CapsLock, typing_speed_ms);

input!(KeyA KeyF KeyA KeyF, typing_speed_ms);
input!(KeyA KeyF KeyF, typing_speed_ms);

input!(CapsLock, typing_speed_ms);
input!(Num3, typing_speed_ms);
input!(CapsLock, typing_speed_ms);

input!(KeyU KeyU, typing_speed_ms);

input!(CapsLock, typing_speed_ms);
input!(Num3, typing_speed_ms);
input!(CapsLock, typing_speed_ms);

output!(textfield, format!("{LIMIT}uçʉ̄ɑ̄ɑɑɑ̄ɑ̄ʉ̄"));

// To verify that the undo (backspace) work as expected
(0..12).for_each(|_| {
input!(Backspace, typing_speed_ms);
});

output!(textfield, LIMIT);

input!(Escape, typing_speed_ms);

input!(ControlLeft ControlLeft, typing_speed_ms);
input!(KeyA KeyF, typing_speed_ms);
input!(ControlLeft ControlLeft, typing_speed_ms);
input!(KeyA KeyF, typing_speed_ms);

output!(textfield, format!("{LIMIT}afɑ"));

rstk::end_wish();
}
}

0 comments on commit 866fb01

Please sign in to comment.