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
33 changes: 25 additions & 8 deletions crates/platform/src/dialog.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Choosing a file to read, or where to write one. An end-to-end scenario cannot reach the
//! system's dialog, so the `e2e` flavour answers the path the scenario fixed instead.
//! system's dialog, so the `e2e` flavour answers the path the scenario wrote down instead.

mod system;

Expand All @@ -21,8 +21,8 @@ pub trait FileDialog {
/// `on_missing` hears a desktop with no dialog to show, which otherwise reads as a cancel.
pub fn system(on_missing: impl Fn() + Send + Sync + 'static) -> Arc<dyn FileDialog + Send + Sync> {
#[cfg(feature = "e2e")]
if let Some(fixed) = from_scenario() {
return Arc::new(fixed);
if let Some(written) = from_scenario() {
return Arc::new(written);
}
system::system(on_missing)
}
Expand All @@ -49,7 +49,7 @@ impl FileDialog for NoDialog {
}
}

/// Answers one path to whatever is asked: a scenario's file, or a test's.
/// Answers one path to whatever is asked: a test's file.
pub struct Fixed(pub PathBuf);

impl FileDialog for Fixed {
Expand All @@ -62,13 +62,30 @@ impl FileDialog for Fixed {
}
}

/// Where a scenario names the file every dialog answers.
/// Where a scenario names the file it writes a dialog's answer in: a path, and a cancel when it
/// holds none.
pub const E2E_DIALOG: &str = "DEVNOTES_E2E_DIALOG";

/// The `e2e` flavour's dialog, when the scenario fixed a path.
/// Answers the path written in a file, read again at each question.
#[cfg(feature = "e2e")]
pub fn from_scenario() -> Option<Fixed> {
std::env::var_os(E2E_DIALOG).map(|path| Fixed(PathBuf::from(path)))
pub struct Written(PathBuf);

#[cfg(feature = "e2e")]
impl FileDialog for Written {
fn open(&self) -> Option<PathBuf> {
let path = std::fs::read_to_string(&self.0).ok()?;
(!path.is_empty()).then(|| PathBuf::from(path))
}

fn save(&self, _name: &str) -> Option<PathBuf> {
self.open()
}
}

/// The `e2e` flavour's dialog, when the scenario answers it.
#[cfg(feature = "e2e")]
pub fn from_scenario() -> Option<Written> {
std::env::var_os(E2E_DIALOG).map(|file| Written(PathBuf::from(file)))
}

#[cfg(test)]
Expand Down
23 changes: 22 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, TreeUpdate};
use egui::accesskit::{Node, NodeId, Rect, Toggled, TreeUpdate};
use egui::{Event, Pos2};
use egui_inspection::protocol::{self, Request, Response};

Expand Down Expand Up @@ -82,6 +82,12 @@ impl Tree {
self.all(id).len()
}

/// Whether anything on the screen says `words`.
pub(crate) fn mentions(&self, words: &str) -> bool {
let said = self.nodes.iter().flat_map(|(_, node)| [node.label(), node.value()]);
said.flatten().any(|text| text.contains(words))
}

/// What a node says: its name, or its value when it has no name.
pub(crate) fn text(&self, id: &str) -> Option<String> {
self.find(id).map(said)
Expand All @@ -95,6 +101,11 @@ impl Tree {
self.focused().is_some_and(|node| node.author_id() == Some(id))
}

/// What the control that has the keyboard says.
pub(crate) fn focused_says(&self) -> String {
self.focused().map(said).unwrap_or_default()
}

/// Where the keyboard is, as a failure says it.
pub(crate) fn keyboard(&self) -> String {
match self.focused() {
Expand Down Expand Up @@ -122,6 +133,10 @@ pub(crate) fn within(node: &Node, place: Rect) -> bool {
})
}

pub(crate) fn is_on(node: &Node) -> bool {
node.toggled() == Some(Toggled::True)
}

pub(crate) fn top(node: &Node) -> f64 {
node.bounds().map_or(f64::INFINITY, |bounds| bounds.y0)
}
Expand Down Expand Up @@ -201,6 +216,12 @@ impl Driver {
self.ask(&Request::ApplyEvents { events });
}

/// The window's inside, in points.
pub(crate) fn resize(&mut self, width: u32, height: u32) {
self.ask(&Request::Resize { width, height });
self.settle();
}

/// The directory of the profile the application runs on.
pub(crate) fn profile(&self) -> &Path {
&self.profile
Expand Down
4 changes: 3 additions & 1 deletion e2e/src/old.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ impl App {
/// As `launch`, every file dialog answering `dialog`: no driver reaches the system's.
pub fn launch_answering(scenario: &'static str, profile: &Path, dialog: &Path) -> Self {
let mut command = Command::new(binary());
command.env(platform::dialog::E2E_DIALOG, dialog);
let answer = profile.join("dialog-answer.txt");
std::fs::write(&answer, dialog.display().to_string()).expect("the dialog's answer is written");
command.env(platform::dialog::E2E_DIALOG, answer);
Self::start(scenario, profile, command)
}

Expand Down
33 changes: 33 additions & 0 deletions e2e/src/sdk/act.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use super::{App, Shortcut};
use crate::current_step;
use crate::driver::{PATIENCE, Tree, middle};

/// A toast's cross.
const DISMISS: &str = "status-dismiss";
/// How far down a card the middle of its title's band is, in points.
const CARD_BAND: f32 = 14.0;
/// Clicks closer than this are one gesture to egui.
Expand Down Expand Up @@ -81,9 +83,26 @@ impl App {
let picked = tree.all(id).into_iter().filter(|node| which(node)).nth(n);
picked.and_then(Node::bounds)
});
if id != DISMISS && self.uncover(bounds) {
return self.click_nth_where(id, n, name, which);
}
self.click_at(middle(bounds), PointerButton::Primary);
}

/// Takes a toast standing over a place off the screen: whether one was.
fn uncover(&mut self, place: Rect) -> bool {
let tree = self.tree();
let bounds = |id| tree.all(id).into_iter().filter_map(Node::bounds);
let mut toasts = bounds("status-toast").zip(bounds(DISMISS));
let Some(at) = toasts.position(|(words, cross)| covers(words, cross, middle(place))) else {
return false;
};
let shown = tree.count(DISMISS);
self.click_nth(DISMISS, at, "the toast's cross");
self.expect("the toast goes", |tree| tree.count(DISMISS) < shown);
true
}

/// A card by its title's band: a todo list's is mostly the boxes of its items.
pub(super) fn click_card(&mut self, card: Rect) {
#[expect(clippy::cast_possible_truncation, reason = "points on a screen")]
Expand All @@ -110,6 +129,12 @@ impl App {
self.clicked = Some(Instant::now());
}

/// The pointer over a control, once it is shown.
pub(super) fn hover(&mut self, id: &str, name: &str) {
let bounds = self.place(name, |tree| tree.find(id).and_then(Node::bounds));
self.move_over(bounds);
}

/// The pointer over a place: what a card shows on hover.
pub(super) fn move_over(&mut self, bounds: Rect) {
self.driver.apply(vec![Event::PointerMoved(middle(bounds))]);
Expand Down Expand Up @@ -216,6 +241,14 @@ impl App {
}
}

/// Whether a toast, from its words to its cross and the margin around them, stands over a point.
fn covers(words: Rect, cross: Rect, at: Pos2) -> bool {
const MARGIN: f64 = 40.0;
let (x, y) = (f64::from(at.x), f64::from(at.y));
let high = words.y0.min(cross.y0) - MARGIN / 2.0..words.y1.max(cross.y1) + MARGIN / 2.0;
(words.x0 - MARGIN..cross.x1 + MARGIN).contains(&x) && high.contains(&y)
}

fn press(pos: Pos2, button: PointerButton, pressed: bool) -> Event {
Event::PointerButton { pos, button, pressed, modifiers: Modifiers::NONE }
}
74 changes: 74 additions & 0 deletions e2e/src/sdk/attachments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::sync::Arc;

use egui::accesskit::Node;
use egui::{Color32, ColorImage, Event};

use super::{App, Shortcut};

/// The files attached to the note open.
pub struct Attachments<'a>(pub(super) &'a mut App);

impl Attachments<'_> {
/// *Attach*, the dialog answering one of the person's files.
pub fn add(&mut self, file: &str) {
self.0.files().answer(file);
self.0.click("attachment-add", "the button that attaches a file");
}

/// Pastes a picture in the note.
pub fn paste_picture(&mut self) {
let picture = Arc::new(ColorImage::filled([8, 8], Color32::RED));
self.0.driver.apply(vec![Event::PasteImage(picture)]);
}

/// Looks at the first picture, then grows it over the window.
pub fn grow(&mut self) {
self.0.click("attachment-preview", "the picture's eye");
self.0.click("attachment-zoom", "the picture");
self.0
.expect("the picture grows over the window", |tree| tree.has("lightbox-image"));
}

/// Takes the grown picture away.
pub fn shrink(&mut self) {
self.0.press(Shortcut::Escape);
self.0.expect("the picture goes, the note stays", |tree| {
!tree.has("lightbox-image") && tree.has("editor-title")
});
}

/// Saves a file elsewhere, the dialog answering where.
pub fn save(&mut self, n: usize, file: &str) {
self.0.files().answer(file);
self.0.click_nth("attachment-save", n, "the file's « Save »");
}

/// Clicks a file's cross: the first time asks, the second removes.
pub fn remove(&mut self, n: usize) {
self.0.click_nth("attachment-remove", n, "the file's cross");
}

/// As many files are attached.
pub fn expect(&mut self, count: usize, says: &str) {
self.0.expect(says, |tree| tree.count("attachment-open") == count);
}

/// As many files go by this name.
pub fn expect_named(&mut self, name: &str, count: usize, says: &str) {
self.0.expect(says, |tree| {
let files = tree.all("attachment-open");
files.into_iter().filter(|file| called(file, name)).count() == count
});
}

/// The file asked for is refused: a banner says it, which is then dismissed.
pub fn expect_refused(&mut self, says: &str) {
self.0.expect(says, |tree| tree.has("error-banner"));
self.0.click("error-dismiss", "the banner's cross");
self.0.expect("the banner goes", |tree| !tree.has("error-banner"));
}
}

fn called(file: &Node, name: &str) -> bool {
file.label().is_some_and(|label| label.contains(name))
}
Loading
Loading