From 828b0cd63f0129e8ea6fe8f8b53cf52ca4c8c605 Mon Sep 17 00:00:00 2001 From: Valentin Millet Date: Fri, 4 Dec 2026 00:39:31 +0100 Subject: [PATCH 1/2] E2E: journey of reaching DevNotes from outside its window --- e2e/Cargo.toml | 4 +- e2e/src/driver.rs | 12 ++ e2e/src/sdk/http.rs | 28 ++- e2e/src/sdk/mod.rs | 72 ++++++- e2e/src/sdk/palette.rs | 93 +++++++++ e2e/src/sdk/prefs.rs | 2 + e2e/src/sdk/shortcut.rs | 3 + e2e/src/sdk/tray.rs | 188 ++++++++++++++++++ e2e/src/sdk/window.rs | 59 ++++-- e2e/tests/07_reaching_from_anywhere.rs | 226 +++++++++++++++++++++ e2e/tests/09_quick_palette.rs | 80 -------- e2e/tests/13_making_it_mine.rs | 8 +- e2e/tests/43_close_to_tray.rs | 259 ------------------------- 13 files changed, 667 insertions(+), 367 deletions(-) create mode 100644 e2e/src/sdk/palette.rs create mode 100644 e2e/src/sdk/tray.rs create mode 100644 e2e/tests/07_reaching_from_anywhere.rs delete mode 100644 e2e/tests/09_quick_palette.rs delete mode 100644 e2e/tests/43_close_to_tray.rs diff --git a/e2e/Cargo.toml b/e2e/Cargo.toml index 6758baa65..bab67083b 100644 --- a/e2e/Cargo.toml +++ b/e2e/Cargo.toml @@ -17,8 +17,8 @@ serde_json.workspace = true tempfile.workspace = true tungstenite.workspace = true -# The watcher and host a Linux scenario serves on a bus of its own. -[target.'cfg(target_os = "linux")'.dev-dependencies] +# The watcher and host a Linux journey serves on a bus of its own. +[target.'cfg(target_os = "linux")'.dependencies] zbus.workspace = true [lints] diff --git a/e2e/src/driver.rs b/e2e/src/driver.rs index c0c2ffc7b..ddcee23e7 100644 --- a/e2e/src/driver.rs +++ b/e2e/src/driver.rs @@ -238,6 +238,18 @@ impl Driver { self.ask(&Request::Settle { max_steps: 10 }); } + /// Until what puts the window out of sight is done. A minimised window paints nothing, as one + /// closed to the tray on macOS: the frame waited for is answered « not painting », which says + /// the window is away and its process alive. + pub(crate) fn settle_out_of_sight(&mut self) { + match exchange(&mut self.stream, &Request::Settle { max_steps: 10 }) { + Ok(Response::Error { message }) if message.contains("not painting") => {} + Ok(Response::Error { message }) => panic!("the application refused to wait: {message}"), + Ok(_) => {} + Err(error) => panic!("the application stopped answering: {error}"), + } + } + pub(crate) fn apply(&mut self, events: Vec) { self.ask(&Request::ApplyEvents { events }); } diff --git a/e2e/src/sdk/http.rs b/e2e/src/sdk/http.rs index d46d06365..3f92d5257 100644 --- a/e2e/src/sdk/http.rs +++ b/e2e/src/sdk/http.rs @@ -1,9 +1,35 @@ -use super::App; +use super::{App, Shortcut}; +use crate::driver::said; /// The HTTP area: its rail, its tabs and the request that is open. pub struct Http<'a>(pub(super) &'a mut App); impl Http<'_> { + /// A collection, from the empty rail or its button. + pub fn new_collection(&mut self, name: &str) { + self.0.click("http-rail-empty-create", "the button of a first collection"); + self.name_new(name); + } + + /// A request in a collection, from its ⋯ menu. + pub fn new_request(&mut self, collection: &str, name: &str) { + self.0.click_where("http-node-menu", "the collection's « ⋯ »", |menu| { + said(menu).ends_with(collection) + }); + self.0.click("http-node-new", "« New »"); + self.0.click("http-node-new-http", "« HTTP request »"); + self.name_new(name); + } + + /// Names what is being made in the rail. + fn name_new(&mut self, name: &str) { + self.0.fill("http-create-input", "the new one's name", name); + self.0.press(Shortcut::Enter); + self.0.expect("it is in the rail", |tree| { + tree.texts("http-node-name").iter().any(|node| node.ends_with(name)) + }); + } + /// Opens a draft in a tab of its own. pub fn new_draft(&mut self) { self.0.click("http-tab-new", "the button of a new request"); diff --git a/e2e/src/sdk/mod.rs b/e2e/src/sdk/mod.rs index ff32d8dfc..5364aeddf 100644 --- a/e2e/src/sdk/mod.rs +++ b/e2e/src/sdk/mod.rs @@ -17,6 +17,7 @@ mod history; mod http; mod json; mod notes; +mod palette; mod prefs; mod rail; mod rich; @@ -26,6 +27,7 @@ mod tags; mod todo; mod tools; mod trash; +mod tray; mod window; pub use attachments::Attachments; @@ -44,6 +46,7 @@ pub use history::History; pub use http::Http; pub use json::{Json, JsonView}; pub use notes::{Kind, Named, Notes, QuickFilter, Sample}; +pub use palette::Palette; pub use prefs::{CardKey, DefaultKind, Density, Indent, Language, Page, Prefs, Switch, Theme}; pub use rail::Rail; pub use rich::{DrawingSize, Presentation, Rich, RichTool}; @@ -53,6 +56,7 @@ pub use tags::Tags; pub use todo::Todo; pub use tools::Tools; pub use trash::Trash; +pub use tray::{Tray, TrayEntry}; pub use window::{Area, Window}; use std::ffi::OsString; @@ -72,6 +76,20 @@ pub struct App { room: TempDir, /// When the last click was. clicked: Option, + /// How many times the window had gone out of sight before it last did. + put_away: u32, + /// The bus the tray is served on. + #[cfg(target_os = "linux")] + session: Option, +} + +/// What a person asks of DevNotes from anywhere, by a key of the whole machine. A second launch +/// stands for the key, which no driver presses. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Global { + Palette, + NewNote, + PasteFromClipboard, } impl App { @@ -91,6 +109,20 @@ impl App { Self::launch(journey, vec![(platform::updater::E2E_UPDATES, served.into())]).with_a_library() } + /// As `open`, the tray on a bus of the journey's own under Linux. + pub fn open_with_a_tray(journey: &'static str) -> Self { + #[cfg(target_os = "linux")] + { + let session = tray::linux::Session::start(); + let bus = vec![("DBUS_SESSION_BUS_ADDRESS", session.address.clone().into())]; + let mut app = Self::launch(journey, bus); + app.session = Some(session); + app.with_a_library() + } + #[cfg(not(target_os = "linux"))] + Self::open(journey) + } + fn launch(journey: &'static str, mut set: Vec<(&'static str, OsString)>) -> Self { let room = TempDir::new().expect("a directory of the journey's own"); let profile = room.path().join("profile"); @@ -99,7 +131,14 @@ impl App { } set.push((platform::dialog::E2E_DIALOG, files::answer(room.path()).into())); let driver = Driver::launch(journey, profile, terminal(room.path()), set); - App { driver, room, clicked: None } + App { + driver, + room, + clicked: None, + put_away: 0, + #[cfg(target_os = "linux")] + session: None, + } } fn with_a_library(mut self) -> Self { @@ -218,6 +257,16 @@ impl App { Help(self) } + /// The palette. + pub fn palette(&mut self) -> Palette<'_> { + Palette(self) + } + + /// The icon in the system's tray. + pub fn tray(&mut self) -> Tray<'_> { + Tray(self) + } + /// The window itself. pub fn window(&mut self) -> Window<'_> { Window(self) @@ -246,6 +295,23 @@ impl App { self.key_printed(Key::Semicolon, place, modifiers); } + /// Asks for something from anywhere on the machine, the window in sight or not. + pub fn press_global(&mut self, global: Global) { + self.driver.hand_over(Some(match global { + Global::Palette => "palette", + Global::NewNote => "new-note", + Global::PasteFromClipboard => "capture", + })); + self.driver.settle(); + } + + /// DevNotes ends by itself, everything written; started again, its library is unlocked. + pub fn start_after_the_end(&mut self) { + self.driver.ended(); + self.driver.start_again(); + self.gate().unlock(PASSPHRASE); + } + /// Types a text where the keyboard is. pub fn type_text(&mut self, text: &str) { self.driver.apply(vec![Event::Text(text.to_owned())]); @@ -319,10 +385,6 @@ impl App { self.driver.ended(); } - fn start_again(&mut self) { - self.driver.start_again(); - } - fn profile(&self) -> &Path { self.driver.profile() } diff --git a/e2e/src/sdk/palette.rs b/e2e/src/sdk/palette.rs new file mode 100644 index 000000000..a4d35d6d8 --- /dev/null +++ b/e2e/src/sdk/palette.rs @@ -0,0 +1,93 @@ +use super::{App, Shortcut}; + +/// The palette, over the window or over whatever was on the screen. +pub struct Palette<'a>(pub(super) &'a mut App); + +impl Palette<'_> { + /// Opens it by its key in the window. + pub fn open(&mut self) { + self.0.press(Shortcut::Palette); + self.expect_open("the palette opens"); + } + + /// Types over what its field holds. + pub fn search(&mut self, words: &str) { + self.0.press(Shortcut::SelectAll); + self.0.type_text(words); + } + + /// Narrows it to a place or a tag: what is typed, then Enter on the first answer. + pub fn narrow_to(&mut self, words: &str) { + self.search(words); + self.0.expect("the palette offers where to search", |tree| { + tree.count("palette-option") > 0 && !tree.has("palette-chip") + }); + self.0.press(Shortcut::Enter); + self.0.expect("the palette is narrowed", |tree| tree.has("palette-chip")); + } + + /// Takes the first answer, by Enter. + pub fn choose_first(&mut self) { + self.0.press(Shortcut::Enter); + self.expect_closed("the palette closes"); + } + + /// Copies the first answer. + pub fn copy_first(&mut self) { + self.0.copy(); + } + + /// Closes it. + pub fn close(&mut self) { + self.0.press(Shortcut::Escape); + self.expect_closed("the palette closes"); + } + + /// The palette is open, the keyboard in its field. + pub fn expect_open(&mut self, says: &str) { + self.0.expect(says, |tree| tree.has_focus("palette-input")); + } + + /// The palette is not on the screen. + pub fn expect_closed(&mut self, says: &str) { + self.0.expect(says, |tree| !tree.has("palette-input")); + } + + /// Some answers are listed, whatever they are. + pub fn expect_answers(&mut self, says: &str) { + self.0 + .expect(says, |tree| tree.count("palette-option") > 0 && !tree.has("palette-empty")); + } + + /// There is one answer, whatever it is. + pub fn expect_one_answer(&mut self, says: &str) { + self.0.expect(says, |tree| tree.count("palette-option") == 1); + } + + /// This is the one answer. + pub fn expect_only(&mut self, words: &str, says: &str) { + self.0.expect(says, |tree| { + let answers = tree.texts("palette-option"); + answers.len() == 1 && answers[0].contains(words) + }); + } + + /// This is among the answers. + pub fn expect_found(&mut self, words: &str, says: &str) { + self.0 + .expect(says, |tree| tree.texts("palette-option").iter().any(|answer| answer.contains(words))); + } + + /// Nothing is found, and a note named after what was typed is offered. + pub fn expect_creation_offered(&mut self, says: &str) { + self.0.expect(says, |tree| { + tree.has("palette-create") && tree.has("palette-empty") && tree.count("palette-option") == 0 + }); + } + + /// The commands are listed. + pub fn expect_commands(&mut self, says: &str) { + self.0 + .expect(says, |tree| tree.count("palette-option") >= 5 && tree.has("palette-group")); + } +} diff --git a/e2e/src/sdk/prefs.rs b/e2e/src/sdk/prefs.rs index 0215a379e..0faf51965 100644 --- a/e2e/src/sdk/prefs.rs +++ b/e2e/src/sdk/prefs.rs @@ -54,6 +54,7 @@ pub enum Indent { pub enum Switch { WrapLines, CloseToTray, + MinimizeToTray, CopyAcknowledged, } @@ -165,6 +166,7 @@ impl Prefs<'_> { match switch { Switch::WrapLines => self.choose("setting-code-wrap", "the switch of the wrapped lines"), Switch::CloseToTray => self.choose("setting-close-to-tray", "« Close to tray »"), + Switch::MinimizeToTray => self.choose("setting-minimize-to-tray", "« Minimize to tray »"), Switch::CopyAcknowledged => { self.choose("setting-copy-confirmation", "the switch of the copy's acknowledgement"); } diff --git a/e2e/src/sdk/shortcut.rs b/e2e/src/sdk/shortcut.rs index 123e27fbf..7cd373ae2 100644 --- a/e2e/src/sdk/shortcut.rs +++ b/e2e/src/sdk/shortcut.rs @@ -42,6 +42,8 @@ pub enum Shortcut { Undo, /// Ctrl+A. SelectAll, + /// Ctrl+Alt+P: the palette. + Palette, /// Ctrl+K: the search. Search, /// Ctrl+F. @@ -84,6 +86,7 @@ impl Shortcut { Self::Delete => (Key::Delete, Modifiers::NONE), Self::Undo => (Key::Z, Modifiers::COMMAND), Self::SelectAll => (Key::A, Modifiers::COMMAND), + Self::Palette => (Key::P, Modifiers::COMMAND | Modifiers::ALT), Self::Search => (Key::K, Modifiers::COMMAND), Self::Find => (Key::F, Modifiers::COMMAND), Self::GoToLine => (Key::G, Modifiers::COMMAND | Modifiers::ALT), diff --git a/e2e/src/sdk/tray.rs b/e2e/src/sdk/tray.rs new file mode 100644 index 000000000..2d003c391 --- /dev/null +++ b/e2e/src/sdk/tray.rs @@ -0,0 +1,188 @@ +use super::App; + +/// The icon in the system's tray, and its menu. +pub struct Tray<'a>(pub(super) &'a mut App); + +/// An entry of the tray's menu. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TrayEntry { + Open, + NewNote, + PasteFromClipboard, + Palette, + Quit, +} + +impl Tray<'_> { + /// Chooses an entry of the menu: under Linux on the item itself, served on the journey's bus; on + /// the others, whose icon no driver reaches, as the menu hands it over. + pub fn choose(&mut self, entry: TrayEntry) { + #[cfg(target_os = "linux")] + self.0.session.as_ref().expect("the journey has its own bus").click(entry); + #[cfg(not(target_os = "linux"))] + self.0.driver.hand_over(Some(match entry { + TrayEntry::Open => "tray:open", + TrayEntry::NewNote => "tray:new-note", + TrayEntry::PasteFromClipboard => "tray:capture", + TrayEntry::Palette => "tray:palette", + TrayEntry::Quit => "tray:quit", + })); + } +} + +/// A session bus, a `StatusNotifierWatcher` and a host's calls, as a panel makes them. +#[cfg(target_os = "linux")] +pub(super) mod linux { + use std::cell::OnceCell; + use std::collections::HashMap; + use std::io::{BufRead, BufReader}; + use std::process::{Child, Command, Stdio}; + use std::sync::mpsc::{self, Receiver, Sender}; + + use zbus::blocking::{Connection, Proxy}; + use zbus::zvariant::{OwnedObjectPath, OwnedValue, Structure, Value}; + + use super::TrayEntry; + use crate::driver::PATIENCE; + + /// An entry of a menu as dbusmenu sends it: id, properties, children. + type Entry = (i32, HashMap, Vec); + + /// The journey's own: the machine's may have a panel on it, or no bus at all. + pub(crate) struct Session { + daemon: Child, + pub(crate) address: String, + watcher: OnceCell, + menu: OnceCell, + } + + impl Session { + pub(crate) fn start() -> Self { + let mut daemon = Command::new("dbus-daemon") + .args(["--session", "--nofork", "--print-address"]) + .stdout(Stdio::piped()) + .spawn() + .expect("dbus-daemon runs"); + let mut address = String::new(); + BufReader::new(daemon.stdout.take().expect("its address")) + .read_line(&mut address) + .expect("dbus-daemon says its address"); + Session { + daemon, + address: address.trim().to_owned(), + watcher: OnceCell::new(), + menu: OnceCell::new(), + } + } + + /// The panel comes after the application, as a session's may. + pub(super) fn click(&self, entry: TrayEntry) { + let watcher = self.watcher.get_or_init(|| Watcher::serve(&self.address)); + self.menu.get_or_init(|| Menu::of(watcher)).click(entry); + } + } + + impl Drop for Session { + fn drop(&mut self) { + let _ = self.daemon.kill(); + let _ = self.daemon.wait(); + } + } + + /// The one call an item makes of a watcher; a host's calls are made from here. + struct Registry { + told: Sender, + } + + #[zbus::interface(name = "org.kde.StatusNotifierWatcher")] + impl Registry { + /// By its bus name, or by its path from its own connection. + #[expect(clippy::needless_pass_by_value, reason = "zbus hands the header over by value")] + fn register_status_notifier_item( + &self, + service: &str, + #[zbus(header)] header: zbus::message::Header<'_>, + ) { + let name = if service.starts_with('/') { + header.sender().map(ToString::to_string).unwrap_or_default() + } else { + service.to_owned() + }; + let _ = self.told.send(name); + } + } + + struct Watcher { + connection: Connection, + registered: Receiver, + } + + impl Watcher { + fn serve(address: &str) -> Self { + let (told, registered) = mpsc::channel(); + let connection = zbus::blocking::connection::Builder::address(address) + .unwrap() + .name("org.kde.StatusNotifierWatcher") + .unwrap() + .serve_at("/StatusNotifierWatcher", Registry { told }) + .unwrap() + .build() + .unwrap(); + Watcher { connection, registered } + } + } + + /// The item's menu as a host reads it, its separators left out. + struct Menu { + proxy: Proxy<'static>, + entries: Vec, + } + + impl Menu { + /// Of the first item to register: a condition, waited for. + fn of(watcher: &Watcher) -> Self { + let item = watcher + .registered + .recv_timeout(PATIENCE) + .expect("an item registers with the watcher"); + let sni = Proxy::new( + &watcher.connection, + item.clone(), + "/StatusNotifierItem", + "org.kde.StatusNotifierItem", + ) + .unwrap(); + let path: OwnedObjectPath = sni.get_property("Menu").unwrap(); + let proxy = + Proxy::new(&watcher.connection, item, path.as_str().to_owned(), "com.canonical.dbusmenu") + .unwrap(); + let (_, (_, _, children)): (u32, Entry) = + proxy.call("GetLayout", &(0_i32, 1_i32, Vec::::new())).unwrap(); + let entries = children.iter().filter_map(entry).collect(); + Menu { proxy, entries } + } + + fn click(&self, entry: TrayEntry) { + let at = match entry { + TrayEntry::Open => 0, + TrayEntry::NewNote => 1, + TrayEntry::PasteFromClipboard => 2, + TrayEntry::Palette => 3, + TrayEntry::Quit => 4, + }; + self.proxy + .call::<_, _, ()>("Event", &(self.entries[at], "clicked", Value::from(0_i32), 0_u32)) + .unwrap(); + } + } + + /// An entry's id; `None` for a separator. + fn entry(child: &OwnedValue) -> Option { + let child = Structure::try_from(child.try_clone().unwrap()).expect("an entry"); + let fields = child.fields(); + let id = i32::try_from(&fields[0]).expect("an entry's id"); + let properties = HashMap::::try_from(fields[1].try_clone().unwrap()).unwrap(); + let kind = properties.get("type").and_then(|kind| kind.downcast_ref::().ok()); + (kind.as_deref() != Some("separator")).then_some(id) + } +} diff --git a/e2e/src/sdk/window.rs b/e2e/src/sdk/window.rs index 0668c3de2..c3ae1b7a3 100644 --- a/e2e/src/sdk/window.rs +++ b/e2e/src/sdk/window.rs @@ -47,19 +47,38 @@ impl Window<'_> { self.0.double_click(middle(toggle) - egui::vec2(100.0, 0.0)); } - /// Minimises the window by its control, then starts DevNotes again, which brings it back. - pub fn minimize_and_come_back(&mut self) { - let before = self.put_away(); + /// Minimises the window by its control. + pub fn minimize(&mut self) { + self.0.put_away = self.times_put_away(); self.0.click("window-minimize", "the window's « Minimise »"); + } + + /// Minimises the window, « Minimize to tray » on: until the system says it is minimised, which + /// the preference acts on. + pub fn minimize_to_tray(&mut self) { + self.minimize(); + self.0.driver.settle_out_of_sight(); + } + + /// Closes the window as its close button does, DevNotes staying in the tray. + pub fn close_to_tray(&mut self) { + self.0.put_away = self.times_put_away(); + self.0.driver.hand_over(Some("close")); + self.0.driver.settle_out_of_sight(); + } + + /// Starts DevNotes a second time while it runs. + pub fn start_again(&mut self) { self.0.launch_again(); - self.expect_back_after(before, "the window went out of sight and is back"); } - /// Closes the window by its control, DevNotes ending with it, then starts it again. - pub fn close_and_start_again(&mut self) { - self.0.click("window-close", "the window's « Close »"); - self.0.ended(); - self.0.start_again(); + /// Closes the window, DevNotes ending with it: by its control, or as macOS' own does. + pub fn close_for_good(&mut self) { + if cfg!(target_os = "macos") { + self.0.driver.hand_over(Some("close")); + } else { + self.0.click("window-close", "the window's « Close »"); + } } /// The word the title bar has for the theme it would switch to, which says the theme on. @@ -129,18 +148,24 @@ impl Window<'_> { self.0.expect(says, |tree| tree.text("file-menu").as_deref() == Some(word)); } - /// How many times the window went out of sight. - fn put_away(&mut self) -> u32 { - let count = self.0.tree().text("window-put-away").and_then(|count| count.parse().ok()); - count.expect("the e2e flavour counts the window put away") - } - - fn expect_back_after(&mut self, before: u32, says: &str) { - let once_more = (before + 1).to_string(); + /// The window went out of sight once since it was put away, and is back. + pub fn expect_back(&mut self, says: &str) { + let once_more = (self.0.put_away + 1).to_string(); self.0.expect(says, |tree| { tree.has("window-shown") && tree.text("window-put-away").as_deref() == Some(once_more.as_str()) }); } + + /// The window never left the screen. + pub fn expect_in_sight(&mut self, says: &str) { + self.0.expect(says, |tree| tree.has("window-shown")); + } + + /// How many times the window went out of sight. + fn times_put_away(&mut self) -> u32 { + let count = self.0.tree().text("window-put-away").and_then(|count| count.parse().ok()); + count.expect("the e2e flavour counts the window put away") + } } /// The areas' switch beside the theme's button, above the notes' header. diff --git a/e2e/tests/07_reaching_from_anywhere.rs b/e2e/tests/07_reaching_from_anywhere.rs new file mode 100644 index 000000000..ee4db9cf4 --- /dev/null +++ b/e2e/tests/07_reaching_from_anywhere.rs @@ -0,0 +1,226 @@ +//! Journey 7, « Reaching DevNotes from anywhere »: a second launch, the palette, the clipboard, the +//! keys of the whole machine and the tray, with the window in sight, put away or minimised — the +//! same on every system. + +use e2e::{App, Area, Global, Sample, Switch, TrayEntry, step}; + +const SNIPPET: &str = "Jump host"; +const NOTHING: &str = "zq nothing is called this"; +const WORDS: &str = "captured from elsewhere"; +/// macOS keeps its own buttons, its « minimise » among them, out of a journey's reach: closing with +/// « Close to tray » is what minimises there. +const OWN_BUTTONS: bool = !cfg!(target_os = "macos"); + +#[test] +fn reaching_from_anywhere() { + let mut app = App::open_with_a_tray("07_reaching_from_anywhere"); + app.notes().write_snippet(SNIPPET, "ssh {{host}} -p {{port=22}}"); + app.window().go_to(Area::Http); + app.http().new_collection("Shop"); + app.http().new_request("Shop", "Checkout"); + app.window().go_to(Area::Notes); + + a_second_launch(&mut app); + the_palette(&mut app); + from_the_machine(&mut app); + put_away(&mut app); + the_tray(&mut app); + without_the_tray(app); +} + +fn a_second_launch(app: &mut App) { + step("a second launch hands over to the running window, and ends", || { + app.window().start_again(); + app.window().expect_in_sight("the one window is on the screen"); + app.notes().expect_card(SNIPPET, "the library is as it was, still open"); + }); +} + +fn the_palette(app: &mut App) { + step("the palette opens from the window's key, narrows, and opens a note on Enter", || { + app.palette().open(); + app.palette().expect_answers("the recent notes are listed"); + app.capture("palette"); + app.palette().search("withRetry"); + app.palette().expect_one_answer("one note answers"); + app.palette().choose_first(); + app.editor().expect_code_open("the note is open"); + app.editor().close(); + }); + + step("it copies a note, asking for its fields", || { + app.press_global(Global::Palette); + app.palette().expect_open("a second launch opens it too"); + app.palette().search(SNIPPET); + app.palette().expect_only(SNIPPET, "the snippet alone answers"); + app.palette().copy_first(); + app.fields().expect_open("the form asks for the fields"); + app.fields().type_value(0, "db.internal"); + app.fields().copy(); + app.clipboard().expect("ssh db.internal -p 22", "the snippet is copied, filled"); + }); + + step("a name nothing has becomes a new note", || { + app.palette().open(); + app.palette().search(NOTHING); + app.palette().expect_creation_offered("a note of that name is offered"); + app.palette().choose_first(); + app.editor().expect_title(NOTHING, "the note is open, named so"); + app.editor().close(); + app.notes().expect_card(NOTHING, "it has its card"); + }); + + step("« > » lists the commands", || { + app.palette().open(); + app.palette().search(">"); + app.palette().expect_commands("the commands are listed"); + app.palette().close(); + }); + + step("« @ » chooses where to search, the notes of one space included", || { + app.rail().new_space("Annex"); + app.notes().write_snippet("Annex door", "open sesame"); + app.rail().show_all_spaces(); + app.palette().open(); + app.palette().narrow_to("@Annex"); + app.palette().expect_only("Annex door", "the space's one note is listed"); + app.palette().close(); + }); + + step("« # » lists the notes of a tag", || { + app.notes().open(SNIPPET); + app.editor().add_tag("quokka"); + app.editor().close(); + app.palette().open(); + app.palette().narrow_to("#quokka"); + app.palette().expect_only(SNIPPET, "the one note with the tag is listed"); + app.palette().close(); + }); + + step("it finds requests, tools and spaces too", || { + app.palette().open(); + app.palette().search("Checkout"); + app.palette().expect_found("Checkout", "the request is found"); + app.palette().search("base64"); + app.palette().expect_found("Base64", "the tool is found"); + app.palette().search("Annex"); + app.palette().expect_found("Annex", "the space is found, with its note"); + app.palette().close(); + }); +} + +fn from_the_machine(app: &mut App) { + step("the clipboard is captured into a note", || { + app.notes().write_snippet("Source", WORDS); + app.notes().copy("Source"); + app.clipboard().expect(WORDS, "the words are on the clipboard"); + app.press_global(Global::PasteFromClipboard); + app.rich().expect_text(WORDS, "a note holds them"); + app.editor().close(); + }); + + step("a new empty note opens", || { + app.press_global(Global::NewNote); + app.editor().expect_title("", "an empty note is open"); + app.editor().close(); + }); +} + +fn put_away(app: &mut App) { + step("closed with « Close to tray » on, DevNotes stays, and a second launch brings it back", || { + app.window().close_to_tray(); + app.window().start_again(); + app.window().expect_back("the window went away once, and is back"); + app.notes().expect_card(SNIPPET, "the library is still open"); + }); + + step("the keys of the machine answer with the window put away", || { + app.window().close_to_tray(); + app.press_global(Global::Palette); + app.window().expect_back("the window is back"); + app.palette().expect_open("the palette over it"); + app.palette().close(); + app.window().close_to_tray(); + app.press_global(Global::NewNote); + app.window().expect_back("the window is back"); + app.editor().expect_title("", "an empty note is open"); + app.editor().close(); + app.window().close_to_tray(); + app.press_global(Global::PasteFromClipboard); + app.window().expect_back("the window is back"); + app.rich().expect_text(WORDS, "a note holds the clipboard"); + app.editor().close(); + }); + + if !OWN_BUTTONS { + step("macOS has its own « minimise »: closing minimised, and the keys answered", || { + app.window().expect_system_buttons("DevNotes draws none of its own"); + }); + return; + } + + step("they answer with it minimised", || { + app.window().minimize(); + app.press_global(Global::Palette); + app.window().expect_back("the window is back"); + app.palette().expect_open("the palette over it"); + app.palette().close(); + app.window().minimize(); + app.press_global(Global::NewNote); + app.window().expect_back("the window is back"); + app.editor().leave_by_escape(); + }); + + step("with « Minimize to tray » on, minimising puts it away too", || { + app.prefs().change(|prefs| prefs.flip(Switch::MinimizeToTray)); + app.window().minimize_to_tray(); + app.window().start_again(); + app.window().expect_back("a second launch brings it back"); + app.prefs().change(|prefs| prefs.flip(Switch::MinimizeToTray)); + }); +} + +fn the_tray(app: &mut App) { + step("« Palette », « New note » and « Paste from clipboard » do what they say", || { + app.window().close_to_tray(); + app.tray().choose(TrayEntry::Palette); + app.window().expect_back("the window is back"); + app.palette().expect_open("the palette over it"); + app.palette().close(); + app.window().close_to_tray(); + app.tray().choose(TrayEntry::NewNote); + app.window().expect_back("the window is back"); + app.editor().expect_title("", "an empty note is open"); + app.editor().close(); + app.window().close_to_tray(); + app.tray().choose(TrayEntry::PasteFromClipboard); + app.window().expect_back("the window is back"); + app.rich().expect_text(WORDS, "a note holds the clipboard"); + }); + + step("« Open DevNotes » brings the window back as it was", || { + app.window().close_to_tray(); + app.tray().choose(TrayEntry::Open); + app.window().expect_back("the window is back"); + app.rich().expect_text(WORDS, "the note is still open"); + app.editor().close(); + }); +} + +fn without_the_tray(mut app: App) { + step("« Quit » ends DevNotes, its window out of sight", || { + app.window().close_to_tray(); + app.tray().choose(TrayEntry::Quit); + app.start_after_the_end(); + app.notes().expect_card(SNIPPET, "the library is as it was left"); + }); + + step("with « Close to tray » off, closing ends it, and the library is found as it was", || { + app.prefs().change(|prefs| prefs.flip(Switch::CloseToTray)); + app.window().close_for_good(); + app.start_after_the_end(); + app.notes().expect_card(Sample::Welcome, "the library is as it was left"); + }); + + app.quit(); +} diff --git a/e2e/tests/09_quick_palette.rs b/e2e/tests/09_quick_palette.rs deleted file mode 100644 index 1125985fa..000000000 --- a/e2e/tests/09_quick_palette.rs +++ /dev/null @@ -1,80 +0,0 @@ -//! The palette, opened from a second launch as the global shortcut will open it, and from its -//! key in the window: it narrows to what is typed, opens on Enter, offers to create a note named -//! after a query that finds nothing, and lists the commands after `>`. What is searched is in the -//! samples' code, which no translation touches. - -use e2e::old::{App, step}; -use egui::{Key, Modifiers}; - -const CARDS: usize = 6; -const NOTHING: &str = "zq query matching nothing at all"; - -fn opened(app: &mut App) { - app.eventually("the palette is open, typing in its field", |tree| { - tree.focused().starts_with("palette-input") - }); -} - -/// What the field holds, then the palette's answer, past its pause. -fn search(app: &mut App, words: &str) { - app.press(Key::A, Modifiers::COMMAND); - app.type_text(words); -} - -fn editor_title(tree: &e2e::old::Tree) -> Option { - tree.find("editor-title").and_then(|field| field.value()).map(str::to_owned) -} - -#[test] -fn quick_palette() { - let profile = tempfile::tempdir().unwrap(); - let mut app = App::launch("09_quick_palette", profile.path()); - app.create_library(); - app.eventually("the samples are drawn", |tree| tree.count("note-card") == CARDS); - - step("a second launch opens it, on the recent notes", || { - app.act("palette"); - opened(&mut app); - app.eventually("recent notes, and nothing said empty", |tree| { - tree.count("palette-option") > 0 && !tree.has("palette-empty") - }); - app.capture("palette-recent"); - }); - - step("it narrows to what is typed, and Enter opens the note", || { - search(&mut app, "withRetry"); - app.eventually("one note answers", |tree| tree.count("palette-option") == 1); - app.press(Key::Enter, Modifiers::NONE); - app.eventually("the editor is open on it, the palette gone", |tree| { - tree.has("editor-title") && !tree.has("palette-input") - }); - app.click("editor-close"); - app.eventually("the editor is closed", |tree| !tree.has("editor-title")); - }); - - step("a query that finds nothing offers a note named after it", || { - app.press(Key::P, Modifiers::COMMAND | Modifiers::ALT); - opened(&mut app); - search(&mut app, NOTHING); - app.eventually("the offer replaces the list, and says nothing matched", |tree| { - tree.has("palette-create") && tree.has("palette-empty") && tree.count("palette-option") == 0 - }); - app.press(Key::Enter, Modifiers::NONE); - app.eventually("the note opens titled, already written", |tree| { - editor_title(tree).as_deref() == Some(NOTHING) - }); - app.click("editor-close"); - app.eventually("one card more", |tree| tree.count("note-card") == CARDS + 1); - }); - - step("`>` lists the commands, and Escape closes", || { - app.press(Key::P, Modifiers::COMMAND | Modifiers::ALT); - opened(&mut app); - app.type_text(">"); - app.eventually("the commands", |tree| tree.count("palette-option") >= 5 && tree.has("palette-group")); - app.press(Key::Escape, Modifiers::NONE); - app.eventually("the palette is closed", |tree| !tree.has("palette-input")); - }); - - app.quit(); -} diff --git a/e2e/tests/13_making_it_mine.rs b/e2e/tests/13_making_it_mine.rs index 30a14e92d..9f17dee8e 100644 --- a/e2e/tests/13_making_it_mine.rs +++ b/e2e/tests/13_making_it_mine.rs @@ -263,13 +263,15 @@ fn the_window(app: &mut App) { }); step("minimised, it goes out of sight, and a new launch brings it back", || { - app.window().minimize_and_come_back(); + app.window().minimize(); + app.window().start_again(); + app.window().expect_back("the window went out of sight and is back"); }); step("closed, with « Close to tray » off, it ends DevNotes", || { app.prefs().change(|prefs| prefs.flip(Switch::CloseToTray)); - app.window().close_and_start_again(); - app.gate().unlock(e2e::PASSPHRASE); + app.window().close_for_good(); + app.start_after_the_end(); }); } diff --git a/e2e/tests/43_close_to_tray.rs b/e2e/tests/43_close_to_tray.rs deleted file mode 100644 index 44d663400..000000000 --- a/e2e/tests/43_close_to_tray.rs +++ /dev/null @@ -1,259 +0,0 @@ -//! Closing with « Close to tray » on hides the window and keeps the application, tray icon or not, -//! the same on every system; a second launch, the palette's global action or the tray's menu brings -//! it back, the palette open when asked. Off, the close button ends the application. Under Linux -//! the tray itself is driven too: a watcher served on a bus of the scenario's own, started after -//! the application as a session's panel may be, gets the item, and its menu answers. - -use e2e::old::{App, step}; -use egui::{Key, Modifiers}; - -const CARDS: usize = 6; - -/// Closed as its close button does; how many times the window had gone out of sight before. -/// Nothing is read while it is away: macOS minimises it, and a minimised window paints nothing. -fn close(app: &mut App) -> u32 { - let before = put_away(app); - app.act_hiding("close"); - before -} - -fn put_away(app: &mut App) -> u32 { - let tree = app.tree(); - tree.text("window-put-away") - .and_then(|count| count.parse().ok()) - .expect("the e2e flavour counts the window put away") -} - -/// Back in sight, having gone once more: the close hid it, and what brought it back found the -/// same process, its library still open. -fn back(app: &mut App, before: u32, also: impl Fn(&e2e::old::Tree) -> bool) { - let once_more = (before + 1).to_string(); - app.eventually("the window went out of sight once, and is back", |tree| { - tree.has("window-shown") - && tree.text("window-put-away").as_deref() == Some(once_more.as_str()) - && also(tree) - }); -} - -fn with_the_palette(tree: &e2e::old::Tree) -> bool { - tree.focused().starts_with("palette-input") -} - -fn close_the_palette(app: &mut App) { - app.press(Key::Escape, Modifiers::NONE); - app.eventually("the palette is closed", |tree| !tree.has("palette-input")); -} - -#[test] -fn close_to_tray() { - let profile = tempfile::tempdir().unwrap(); - let mut app = App::launch("43_close_to_tray", profile.path()); - app.create_library(); - app.eventually("the samples are drawn", |tree| { - tree.count("note-card") == CARDS && tree.has("window-shown") - }); - - step("closed, it hides, and a second launch, as the menu's entry, brings it back", || { - let before = close(&mut app); - app.launch_again(); - back(&mut app, before, |tree| tree.count("note-card") == CARDS && !tree.has("palette-input")); - }); - - step("hidden, the palette's global action brings it back with the palette", || { - let before = close(&mut app); - app.act("palette"); - back(&mut app, before, with_the_palette); - close_the_palette(&mut app); - }); - - step("the tray's Palette does the same", || { - let before = close(&mut app); - app.act("tray:palette"); - back(&mut app, before, with_the_palette); - close_the_palette(&mut app); - }); - - step("the tray's Open brings the window back as it was", || { - let before = close(&mut app); - app.act("tray:open"); - back(&mut app, before, |tree| tree.count("note-card") == CARDS); - }); - - // The tray's Quit, out of sight: nothing is left to ask, and the process ends. - close(&mut app); - app.ends_on("tray:quit"); -} - -#[test] -fn with_the_preference_off_the_close_button_ends_the_application() { - let profile = tempfile::tempdir().unwrap(); - std::fs::write(profile.path().join("preferences.json"), r#"{ "devnotes.closeToTray": "false" }"#) - .unwrap(); - let mut app = App::launch("43_close_to_tray", profile.path()); - app.wait_for("vault-confirmation"); - - app.ends_on("close"); -} - -#[cfg(target_os = "linux")] -#[test] -fn the_linux_tray_answers_a_watcher_started_after_it() { - let bus = linux::Bus::start(); - let profile = tempfile::tempdir().unwrap(); - let mut app = App::launch_on_bus("43_close_to_tray", profile.path(), &bus.address); - app.create_library(); - app.eventually("the samples are drawn", |tree| tree.count("note-card") == CARDS); - - let watcher = linux::Watcher::serve(&bus.address); - let menu = linux::Menu::of(&watcher); - step("a watcher started after the application gets its item and menu", || { - assert_eq!(menu.entries.len(), 6, "{:?}", menu.entries); - assert_eq!(menu.entries[4].1, "separator"); - }); - - step("hidden, the menu's Palette brings the window back with the palette", || { - let before = close(&mut app); - menu.click(menu.entries[3].0); - back(&mut app, before, with_the_palette); - close_the_palette(&mut app); - }); - - menu.click(menu.entries[5].0); - app.ended(); -} - -/// A session bus, a `StatusNotifierWatcher` and a host's calls, as a panel makes them. -#[cfg(target_os = "linux")] -mod linux { - use std::collections::HashMap; - use std::io::{BufRead, BufReader}; - use std::process::{Child, Command, Stdio}; - use std::sync::mpsc::{self, Receiver, Sender}; - use std::time::Duration; - - use zbus::blocking::{Connection, Proxy}; - use zbus::zvariant::{OwnedObjectPath, OwnedValue, Structure, Value}; - - /// An entry of a menu as dbusmenu sends it: id, properties, children. - type Entry = (i32, HashMap, Vec); - - /// The scenario's own: the machine's may have a panel on it, or no bus at all. - pub(super) struct Bus { - daemon: Child, - pub(super) address: String, - } - - impl Bus { - pub(super) fn start() -> Self { - let mut daemon = Command::new("dbus-daemon") - .args(["--session", "--nofork", "--print-address"]) - .stdout(Stdio::piped()) - .spawn() - .expect("dbus-daemon runs"); - let mut address = String::new(); - BufReader::new(daemon.stdout.take().expect("its address")) - .read_line(&mut address) - .expect("dbus-daemon says its address"); - Bus { daemon, address: address.trim().to_owned() } - } - } - - impl Drop for Bus { - fn drop(&mut self) { - let _ = self.daemon.kill(); - let _ = self.daemon.wait(); - } - } - - /// The one call an item makes of a watcher; a host's calls are made from here. - struct Registry { - told: Sender, - } - - #[zbus::interface(name = "org.kde.StatusNotifierWatcher")] - impl Registry { - /// By its bus name, or by its path from its own connection. - #[expect(clippy::needless_pass_by_value, reason = "zbus hands the header over by value")] - fn register_status_notifier_item( - &self, - service: &str, - #[zbus(header)] header: zbus::message::Header<'_>, - ) { - let name = if service.starts_with('/') { - header.sender().map(ToString::to_string).unwrap_or_default() - } else { - service.to_owned() - }; - let _ = self.told.send(name); - } - } - - pub(super) struct Watcher { - connection: Connection, - registered: Receiver, - } - - impl Watcher { - pub(super) fn serve(address: &str) -> Self { - let (told, registered) = mpsc::channel(); - let connection = zbus::blocking::connection::Builder::address(address) - .unwrap() - .name("org.kde.StatusNotifierWatcher") - .unwrap() - .serve_at("/StatusNotifierWatcher", Registry { told }) - .unwrap() - .build() - .unwrap(); - Watcher { connection, registered } - } - } - - /// The item's menu as a host reads it: each entry's id and type (`standard`, `separator`). - pub(super) struct Menu { - proxy: Proxy<'static>, - pub(super) entries: Vec<(i32, String)>, - } - - impl Menu { - /// Of the first item to register: a condition, waited for. - pub(super) fn of(watcher: &Watcher) -> Self { - let item = watcher - .registered - .recv_timeout(Duration::from_secs(30)) - .expect("an item registers with the watcher"); - let sni = Proxy::new( - &watcher.connection, - item.clone(), - "/StatusNotifierItem", - "org.kde.StatusNotifierItem", - ) - .unwrap(); - let path: OwnedObjectPath = sni.get_property("Menu").unwrap(); - let proxy = - Proxy::new(&watcher.connection, item, path.as_str().to_owned(), "com.canonical.dbusmenu") - .unwrap(); - let (_, (_, _, children)): (u32, Entry) = - proxy.call("GetLayout", &(0_i32, 1_i32, Vec::::new())).unwrap(); - let entries = children.iter().map(entry).collect(); - Menu { proxy, entries } - } - - pub(super) fn click(&self, id: i32) { - self.proxy - .call::<_, _, ()>("Event", &(id, "clicked", Value::from(0_i32), 0_u32)) - .unwrap(); - } - } - - fn entry(child: &OwnedValue) -> (i32, String) { - let child = Structure::try_from(child.try_clone().unwrap()).expect("an entry"); - let fields = child.fields(); - let id = i32::try_from(&fields[0]).expect("an entry's id"); - let properties = HashMap::::try_from(fields[1].try_clone().unwrap()).unwrap(); - let kind = properties - .get("type") - .and_then(|kind| kind.downcast_ref::().ok()) - .unwrap_or_else(|| "standard".to_owned()); - (id, kind) - } -} From 2bb0bfa5252223af430af46dd702c37faf2056c6 Mon Sep 17 00:00:00 2001 From: Valentin Millet Date: Fri, 4 Dec 2026 10:00:24 +0100 Subject: [PATCH 2/2] A window that is not back says whether it left, came back, or left twice --- e2e/src/sdk/window.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/e2e/src/sdk/window.rs b/e2e/src/sdk/window.rs index c3ae1b7a3..9ce5808fa 100644 --- a/e2e/src/sdk/window.rs +++ b/e2e/src/sdk/window.rs @@ -1,6 +1,7 @@ use egui::accesskit::Node; use super::App; +use crate::current_step; use crate::driver::{Tree, middle}; /// The window itself: its title bar and what the system does with it. @@ -150,10 +151,13 @@ impl Window<'_> { /// The window went out of sight once since it was put away, and is back. pub fn expect_back(&mut self, says: &str) { - let once_more = (self.0.put_away + 1).to_string(); - self.0.expect(says, |tree| { - tree.has("window-shown") && tree.text("window-put-away").as_deref() == Some(once_more.as_str()) - }); + let before = self.0.put_away; + let gone = |tree: &Tree| times(tree).is_some_and(|now| now > before); + self.0.expect(&format!("{says}: it went out of sight"), gone); + self.0 + .expect(&format!("{says}: it is in sight again"), |tree| tree.has("window-shown")); + let since = self.times_put_away() - before; + assert_eq!(since, 1, "« {} »: {says} — it went out of sight {since} times", current_step()); } /// The window never left the screen. @@ -163,11 +167,14 @@ impl Window<'_> { /// How many times the window went out of sight. fn times_put_away(&mut self) -> u32 { - let count = self.0.tree().text("window-put-away").and_then(|count| count.parse().ok()); - count.expect("the e2e flavour counts the window put away") + times(&self.0.tree()).expect("the e2e flavour counts the window put away") } } +fn times(tree: &Tree) -> Option { + tree.text("window-put-away").and_then(|count| count.parse().ok()) +} + /// The areas' switch beside the theme's button, above the notes' header. fn switch_in_the_bar(tree: &Tree) -> bool { let top = |id| tree.find(id).and_then(Node::bounds).map(|bounds| bounds.y0);