Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion e2e/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::time::{Duration, Instant};

use egui::accesskit::{Node, NodeId, Rect, Toggled, TreeUpdate};
use egui::accesskit::{Node, NodeId, Rect, Role, Toggled, TreeUpdate};
use egui::{Event, Pos2};
use egui_inspection::protocol::{self, Request, Response};

Expand Down Expand Up @@ -93,10 +93,25 @@ impl Tree {
self.find(id).map(said)
}

/// Whether the middle of a place is inside the window, where it can be clicked.
pub(crate) fn in_window(&self, place: Rect) -> bool {
let window = self.nodes.iter().find(|(_, node)| node.role() == Role::Window);
let Some(window) = window.and_then(|(_, node)| node.bounds()) else {
return true;
};
let at = middle(place);
let inside = |x: f32, from: f64, to: f64| (from..to).contains(&f64::from(x));
inside(at.x, window.x0, window.x1) && inside(at.y, window.y0, window.y1)
}

pub(crate) fn is_disabled(&self, id: &str) -> bool {
self.find(id).is_some_and(Node::is_disabled)
}

pub(crate) fn is_on(&self, id: &str) -> bool {
self.find(id).is_some_and(is_on)
}

pub(crate) fn has_focus(&self, id: &str) -> bool {
self.focused().is_some_and(|node| node.author_id() == Some(id))
}
Expand Down
33 changes: 32 additions & 1 deletion e2e/src/sdk/act.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl App {
pub(super) fn click_nth_where(&mut self, id: &str, n: usize, name: &str, which: impl Fn(&Node) -> bool) {
let bounds = self.place(name, |tree| {
let picked = tree.all(id).into_iter().filter(|node| which(node)).nth(n);
picked.and_then(Node::bounds)
picked.and_then(Node::bounds).filter(|bounds| tree.in_window(*bounds))
});
if id != DISMISS && self.uncover(bounds) {
return self.click_nth_where(id, n, name, which);
Expand Down Expand Up @@ -117,6 +117,37 @@ impl App {
self.clicked = Some(Instant::now());
}

/// The `n`th `id` clicked with a key held, Ctrl or Shift.
pub(super) fn click_holding(&mut self, held: Modifiers, id: &str, n: usize, name: &str) {
self.driver.apply(vec![Event::ModifiersChanged(held)]);
self.click_nth(id, n, name);
self.driver.apply(vec![Event::ModifiersChanged(Modifiers::NONE)]);
}

/// The right button, on a control.
pub(super) fn right_click(&mut self, id: &str, name: &str) {
let bounds = self.place(name, |tree| tree.find(id).and_then(Node::bounds));
self.click_at(middle(bounds), PointerButton::Secondary);
}

/// A press and a release elsewhere, the pointer moved half way between: what a gesture needs to
/// tell a drag from a click.
pub(super) fn drag(&mut self, from: Pos2, to: Pos2) {
let button = PointerButton::Primary;
self.driver.apply(vec![Event::PointerMoved(from)]);
self.driver.apply(vec![press(from, button, true)]);
self.driver.apply(vec![Event::PointerMoved(from + (to - from) / 2.0)]);
self.driver.apply(vec![Event::PointerMoved(to)]);
self.driver.apply(vec![press(to, button, false)]);
self.clicked = Some(Instant::now());
}

/// A control dragged by so much.
pub(super) fn drag_by(&mut self, id: &str, name: &str, by: egui::Vec2) {
let from = middle(self.place(name, |tree| tree.find(id).and_then(Node::bounds)));
self.drag(from, from + by);
}

/// Two clicks, sent together so that they are one double click however slowly the window draws.
/// ⚠️ A click just before makes egui count three, wherever it was: they wait their turn.
pub(super) fn double_click(&mut self, pos: Pos2) {
Expand Down
52 changes: 52 additions & 0 deletions e2e/src/sdk/calc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use egui::accesskit::Node;

use super::{App, Modifiers};
use crate::driver::Tree;

/// The calculation open. Its results are read by their figures: the language on the screen writes
/// the decimal mark and the units.
pub struct Calc<'a>(pub(super) &'a mut App);

impl Calc<'_> {
/// Picks a result, counted from 0.
pub fn pick(&mut self, result: usize) {
self.0.click_nth("calc-result", result, "the result");
}

/// Picks a result beside those picked, Ctrl held.
pub fn pick_also(&mut self, result: usize) {
self.0.click_holding(Modifiers::COMMAND, "calc-result", result, "the result");
}

/// The results, top to bottom, by their figures.
pub fn expect_results(&mut self, figures: &[&str], says: &str) {
self.0.expect(says, |tree| results(tree) == figures);
}

/// As many lines say why they have no result.
pub fn expect_errors(&mut self, count: usize, says: &str) {
self.0.expect(says, |tree| {
let errors = tree.all("calc-failure");
errors.len() == count && errors.iter().all(|error| !said(error).is_empty())
});
}

/// The footer sums what is picked, by its figures.
pub fn expect_sum(&mut self, figures: &str, says: &str) {
self.0.expect(says, |tree| {
tree.text("calc-summary").is_some_and(|said| digits(&said).contains(figures))
});
}
}

fn results(tree: &Tree) -> Vec<String> {
tree.all("calc-result").into_iter().map(|node| digits(said(node))).collect()
}

fn said(node: &Node) -> &str {
node.value().unwrap_or_default()
}

pub(super) fn digits(text: &str) -> String {
text.chars().filter(char::is_ascii_digit).collect()
}
2 changes: 1 addition & 1 deletion e2e/src/sdk/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Clipboard<'_> {
self.expect_that(says, |held| held.contains(words));
}

fn expect_that(&mut self, says: &str, holds: impl Fn(&str) -> bool) {
pub(super) fn expect_that(&mut self, says: &str, holds: impl Fn(&str) -> bool) {
let until = Instant::now() + PATIENCE;
loop {
let held = platform::clipboard::System.text().unwrap_or_default();
Expand Down
169 changes: 169 additions & 0 deletions e2e/src/sdk/drawing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
use egui::accesskit::Node;
use egui::{Pos2, Vec2};

use super::{App, Key, Modifiers, Shortcut};
use crate::current_step;

/// The drawing open. Places on it are named by the journey and stand still, zoom aside.
pub struct Drawing<'a>(pub(super) &'a mut App);

/// A tool in hand.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Tool {
Rectangle,
Ellipse,
Diamond,
Arrow,
Text,
}

/// A place on the drawing: its left, its middle or its right, each wide enough for a shape.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Spot {
Left,
Middle,
Right,
Below,
}

impl Spot {
/// Its corner, in points from the stage's.
fn corner(self) -> Vec2 {
match self {
Self::Left => Vec2::new(40.0, 40.0),
Self::Middle => Vec2::new(200.0, 40.0),
Self::Right => Vec2::new(360.0, 40.0),
Self::Below => Vec2::new(200.0, 220.0),
}
}

fn middle(self) -> Vec2 {
self.corner() + SHAPE / 2.0
}
}

const SHAPE: Vec2 = Vec2::new(100.0, 70.0);

impl Drawing<'_> {
/// Draws a shape at a place, its tool taken by its button.
pub fn draw(&mut self, tool: Tool, at: Spot) {
let shapes = self.shapes();
self.take(tool);
self.drag(at.corner(), at.corner() + SHAPE);
self.expect_shapes(shapes + 1, "the shape is drawn");
}

/// Draws an arrow from the shape of one place to the shape of another.
pub fn draw_arrow(&mut self, from: Spot, to: Spot) {
let shapes = self.shapes();
self.take(Tool::Arrow);
self.drag(from.middle(), to.middle());
self.expect_shapes(shapes + 1, "the arrow is drawn");
}

/// Writes a text at a place.
pub fn write(&mut self, text: &str, at: Spot) {
let shapes = self.shapes();
self.take(Tool::Text);
self.drag(at.corner(), at.corner());
self.0.expect("a text waits to be typed", |tree| tree.has("drawing-text"));
self.0.type_text(text);
self.0.press(Shortcut::Escape);
self.expect_shapes(shapes + 1, "the text is on the drawing");
}

/// Drags the shape of one place to another.
pub fn move_shape(&mut self, from: Spot, to: Spot) {
self.take_selection();
self.drag(from.middle(), to.middle());
}

/// Selects the shape of a place and removes it with Delete.
pub fn delete(&mut self, at: Spot) {
self.take_selection();
self.drag(at.middle(), at.middle());
self.0.press(Shortcut::Delete);
}

/// Zooms in, by its button.
pub fn zoom_in(&mut self) {
self.0.click("drawing-zoom-in", "the button that zooms in");
}

/// Shows the whole drawing, by its key.
pub fn show_whole(&mut self) {
// The stage takes the keys from a click on its ground.
self.take_selection();
self.drag(Vec2::new(8.0, 8.0), Vec2::new(8.0, 8.0));
self.0.key(Key::F, Modifiers::NONE);
}

/// Exports the drawing as a PNG, the dialog answering where.
pub fn export_png(&mut self, file: &str) {
self.0.files().answer(file);
self.0.click("drawing-export-png", "« PNG »");
}

/// Exports the drawing as an SVG, the dialog answering where.
pub fn export_svg(&mut self, file: &str) {
self.0.files().answer(file);
self.0.click("drawing-export-svg", "« SVG »");
}

/// As many shapes are on the drawing, arrows and texts among them.
pub fn expect_shapes(&mut self, count: usize, says: &str) {
let count = count.to_string();
self.0
.expect(says, |tree| tree.find("drawing-stage").and_then(Node::value) == Some(count.as_str()));
}

/// Between two SVG exports, the arrow moved.
pub fn expect_arrow_moved(&mut self, before: &str, after: &str, says: &str) {
let arrow = |bytes: Vec<u8>| {
let picture = String::from_utf8_lossy(&bytes).into_owned();
picture.split("<path").nth(1).map(str::to_owned)
};
let drawn = self.0.files().read(before, "the first export is written");
let redrawn = self.0.files().read(after, "the second export is written");
assert_ne!(arrow(drawn), arrow(redrawn), "« {} »: {says}", current_step());
}

/// The zoom, as « 125 % » says it.
pub fn expect_zoom(&mut self, percent: &str, says: &str) {
self.0
.expect(says, |tree| tree.text("drawing-zoom").is_some_and(|said| said.starts_with(percent)));
}

fn shapes(&mut self) -> usize {
self.0.expect("the drawing is open", |tree| tree.has("drawing-stage"));
let tree = self.0.tree();
let count = tree.find("drawing-stage").and_then(Node::value);
count.and_then(|count| count.parse().ok()).unwrap_or_default()
}

/// A tool by its button.
fn take(&mut self, tool: Tool) {
let id = match tool {
Tool::Rectangle => "drawing-tool-rectangle",
Tool::Ellipse => "drawing-tool-ellipse",
Tool::Diamond => "drawing-tool-diamond",
Tool::Arrow => "drawing-tool-arrow",
Tool::Text => "drawing-tool-text",
};
self.0.click(id, "the tool's button");
self.0.expect("the tool is in hand", |tree| tree.is_on(id));
}

fn take_selection(&mut self) {
self.0.click("drawing-tool-select", "the pointer's tool");
}

fn drag(&mut self, from: Vec2, to: Vec2) {
let stage = self
.0
.place("the drawing", |tree| tree.find("drawing-stage").and_then(Node::bounds));
#[expect(clippy::cast_possible_truncation, reason = "points on a screen")]
let corner = Pos2::new(stage.x0 as f32, stage.y0 as f32);
self.0.drag(corner + from, corner + to);
}
}
39 changes: 16 additions & 23 deletions e2e/src/sdk/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,6 @@ impl Editor<'_> {
self.0.expect("the calendar closes", |tree| !tree.has("date-day"));
}

/// Adds a task to the todo list, typed.
pub fn add_task(&mut self, words: &str) {
let before = self.0.tree().count("checklist-text");
self.0.click("checklist-add", "« Add a task »");
self.0.expect("a new row takes the keyboard", |tree| {
tree.count("checklist-text") == before + 1 && tree.has_focus("checklist-text")
});
self.0.type_text(words);
}

/// Types in a Note, where its caret is.
pub fn type_note(&mut self, text: &str) {
self.0.take_keyboard("editor-rich", "the Note's text");
Expand Down Expand Up @@ -137,6 +127,14 @@ impl Editor<'_> {
});
}

/// Makes the note a Note, from its *Kind* menu.
pub fn convert_to_note(&mut self) {
// The menu offers the other kinds, the snippet first: a list's has it above « Note ».
let note = if self.0.tree().has("checklist-add") { "editor-kind-1" } else { "editor-kind-0" };
self.0.click("editor-kind", "the « Kind » menu");
self.0.click(note, "« Note » in the menu");
}

/// Shows the text filled with its fields, or gives the code back.
pub fn flip_filled(&mut self) {
self.0
Expand Down Expand Up @@ -205,14 +203,6 @@ impl Editor<'_> {
.expect(says, |tree| tree.text("search-count").as_deref() == Some(counted));
}

/// The result of a calculation's first line, its figures alone: the marks are the language's.
pub fn expect_first_result(&mut self, figures: &str, says: &str) {
self.0.expect(says, |tree| {
let first = tree.all("calc-result").first().and_then(|result| result.value());
first.is_some_and(|result| digits(result) == figures)
});
}

/// The first line of the code goes on past the edge: the next row is another line's.
pub fn expect_line_unwrapped(&mut self, says: &str) {
self.mark_the_next_row();
Expand Down Expand Up @@ -269,6 +259,13 @@ impl Editor<'_> {
self.0.expect(says, |tree| held(tree).contains(code));
}

/// The note's kind is said, and cannot be changed.
pub fn expect_no_kind_to_choose(&mut self, says: &str) {
self.0.click("editor-kind", "the note's kind");
self.0
.expect_at_rest(says, |tree| tree.has("editor-kind") && !tree.has("editor-kind-0"));
}

/// The note's title.
pub fn expect_title(&mut self, title: &str, says: &str) {
self.0
Expand Down Expand Up @@ -296,7 +293,7 @@ impl Editor<'_> {
}

/// A button of the note's header, or its entry of the ⋯ menu where there is no room for it.
fn click_button_or_entry(&mut self, id: &str, name: &str) {
pub(super) fn click_button_or_entry(&mut self, id: &str, name: &str) {
if !self.0.tree().has(id) {
self.0.click("editor-more", "the note's « ⋯ »");
}
Expand Down Expand Up @@ -332,7 +329,3 @@ fn held(tree: &Tree) -> String {
fn notice(tree: &Tree) -> Option<&str> {
tree.find("editor-format-notice").and_then(Node::description)
}

fn digits(text: &str) -> String {
text.chars().filter(char::is_ascii_digit).collect()
}
Loading
Loading