Files
craft-libs/crates/dialogs/tests/it/files.rs
T
aiongg 20e0bcf93b dialogs-w2: net change of 3 commits
- dialogs-w2: written by an agent
- Integration: ParamRows keeps dialogs-w1's preview_colors beside the patterns rows (dialogs-w1 7a195af4a)
- dialogs-w2: review fixes (review-1)
2026-11-15 04:46:07 +00:00

105 lines
3.6 KiB
Rust

//! The path field and the picker ticket it keeps, against a file service that records its requests.
use craft_dialogs::ui::PathField;
use craft_host::{AppInfo, FileError, Files, Host, PickAnswer, PickId, PickRequest, Picked};
use craft_ui::contract::Bind;
use craft_ui::testing::{Fixture, trial};
use std::cell::{Cell, RefCell};
use std::path::{Path, PathBuf};
use std::rc::Rc;
use std::sync::{Arc, Mutex};
/// Records every request; answers are put in by the test.
#[derive(Default)]
struct StubFiles {
state: Mutex<StubState>,
}
#[derive(Default)]
struct StubState {
requests: Vec<PickRequest>,
answered: Vec<PickAnswer>,
}
impl StubFiles {
/// Answers ticket `id`; a ticket this service never issued is answered as a request of another feature.
fn answer(&self, id: PickId, picked: Picked) {
let mut state = self.state.lock().expect("the stub is not poisoned");
let request = usize::try_from(id.0 - 1).ok().and_then(|at| state.requests.get(at).cloned()).unwrap_or_else(|| PickRequest::open("other"));
state.answered.push(PickAnswer { id, request, picked });
}
}
impl Files for StubFiles {
fn pick(&self, request: PickRequest) -> PickId {
let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
state.requests.push(request);
PickId(state.requests.len() as u64)
}
fn take_answer(&self, id: PickId) -> Option<Picked> {
let mut state = self.state.lock().unwrap_or_else(|e| e.into_inner());
let at = state.answered.iter().position(|a| a.id == id)?;
Some(state.answered.remove(at).picked)
}
fn forget(&self, id: PickId) {
self.state.lock().unwrap_or_else(|e| e.into_inner()).answered.retain(|a| a.id != id);
}
fn pick_pending(&self) -> bool {
false
}
fn read(&self, _path: &Path) -> Result<Vec<u8>, FileError> {
Err(FileError::Unsupported)
}
fn write(&self, _path: &Path, _bytes: &[u8]) -> Result<(), FileError> {
Err(FileError::Unsupported)
}
fn last_folder(&self, _purpose: &str) -> Option<PathBuf> {
None
}
}
fn host_with(files: &Arc<StubFiles>) -> Arc<Host> {
Arc::new(Host::builder(AppInfo { id: "test", name: "Test", version: "1.0" }).files(files.clone()).build())
}
fn paths(names: &[&str]) -> Vec<PathBuf> {
names.iter().map(|n| PathBuf::from(format!("/photos/{n}"))).collect()
}
/// The path field's Browse queues a pick through the file service, and the answer lands in the field's path once.
#[trial]
fn path_field_browse_fills_the_path() {
let files = Arc::new(StubFiles::default());
let path = Rc::new(RefCell::new(String::new()));
let sets = Rc::new(Cell::new(0usize));
let ticket = Rc::new(Cell::new(None::<PickId>));
let (held, counted, issued) = (Rc::clone(&path), Rc::clone(&sets), Rc::clone(&ticket));
let mut fixture = Fixture::new(move |ui| {
let mut path = held.borrow_mut();
let out = PathField::new("PDF file", Bind::bound(&mut *path), PickRequest::open("import")).show(ui);
if out.browsing.is_some() {
issued.set(out.browsing);
}
if out.edit.is_some() {
counted.set(counted.get() + 1);
}
});
fixture.host(host_with(&files));
fixture.settle();
let browse = fixture.by_label("Browse…").expect("the Browse button");
fixture.click(&browse);
fixture.settle();
sets.set(0);
files.answer(ticket.get().expect("Browse issued a ticket"), Picked::Paths(paths(&["brochure.pdf"])));
fixture.idle(3);
assert_eq!(*path.borrow(), "/photos/brochure.pdf");
assert_eq!(sets.get(), 1, "the answer lands once");
}