Ely Office: six offline-first desktop apps in Rust and GPUI
ElyDoc, ElyTable, ElyShow (LibreOfficeKit), ElyPDF (MuPDF), ElyTree (Git), ElyMark (Milkdown and CodeMirror in CEF). ElyMail and ElyAgent follow.
This commit is contained in:
817 files changed
+143101
No files matched your search
@@ -0,0 +1,91 @@
|
||||
use super::*;
|
||||
use ely_platform::clipboard::{self, Clip};
|
||||
use gpui::TestAppContext;
|
||||
|
||||
const BOOK: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
|
||||
<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
|
||||
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
|
||||
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0"
|
||||
xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.spreadsheet">
|
||||
<office:body><office:spreadsheet><table:table table:name="S"><table:table-row>
|
||||
<table:table-cell office:value-type="float" office:value="2"><text:p>2</text:p></table:table-cell>
|
||||
<table:table-cell table:formula="of:=[.A1]*3" office:value-type="float" office:value="6"><text:p>6</text:p></table:table-cell>
|
||||
</table:table-row></table:table></office:spreadsheet></office:body>
|
||||
</office:document>"#;
|
||||
|
||||
fn flush(cx: &TestAppContext) {
|
||||
for _ in 0..80 {
|
||||
cx.executor().tick();
|
||||
}
|
||||
}
|
||||
|
||||
fn goto(view: &Entity<LokView>, cx: &mut TestAppContext, cell: &str) {
|
||||
cx.update(|app| {
|
||||
let view = view.read(app);
|
||||
let mut worker = view.worker.lock().expect("the worker lock");
|
||||
worker
|
||||
.run(&view.doc, "cell.goto", &[cell])
|
||||
.expect("the cell");
|
||||
});
|
||||
}
|
||||
|
||||
/// The workbook as flat XML.
|
||||
fn saved(view: &Entity<LokView>, cx: &mut TestAppContext) -> String {
|
||||
let out = tempfile::tempdir()
|
||||
.expect("a temp dir")
|
||||
.keep()
|
||||
.join("out.fods");
|
||||
cx.update(|app| {
|
||||
let view = view.read(app);
|
||||
let mut worker = view.worker.lock().expect("the worker lock");
|
||||
view.doc
|
||||
.save_as(&mut worker, &format!("file://{}", out.display()), "fods")
|
||||
.expect("the save works");
|
||||
});
|
||||
std::fs::read_to_string(&out).expect("the save reads")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn values_and_formulas_paste_alone_from_the_views_copy() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
if std::env::var_os("ELY_CLIPBOARD_TEST").is_none() {
|
||||
eprintln!("SKIP: set ELY_CLIPBOARD_TEST=1; this replaces the clipboard");
|
||||
return;
|
||||
}
|
||||
let mut cx = TestAppContext::single();
|
||||
let (view, _) = cx.update(|app| live_view_of(app, &install, "book.fods", BOOK));
|
||||
goto(&view, &mut cx, "B1");
|
||||
cx.update(|app| {
|
||||
view.update(app, |view, _| {
|
||||
view.send(crate::view::input::Input::Copy { cut: false })
|
||||
})
|
||||
});
|
||||
flush(&cx);
|
||||
for (cell, id) in [("C1", "paste.values"), ("D1", "paste.formulas")] {
|
||||
goto(&view, &mut cx, cell);
|
||||
cx.update(|app| view.update(app, |view, _| view.paste_only(id)));
|
||||
flush(&cx);
|
||||
}
|
||||
let xml = saved(&view, &mut cx);
|
||||
assert_eq!(xml.matches("table:formula=").count(), 2, "C1 holds 6 alone");
|
||||
assert!(xml.contains("of:=[.C1]*3"), "D1 keeps the formula");
|
||||
let copy = cx.update(|app| {
|
||||
let read = clipboard::read().expect("the clipboard reads");
|
||||
let Some(Clip::Html { html, .. }) = read else {
|
||||
panic!("the copy lands as html");
|
||||
};
|
||||
view.read(app).holds_copy(&html)
|
||||
});
|
||||
assert!(copy, "the view knows its own copy");
|
||||
// Copies made elsewhere carry no mark of this view.
|
||||
let other = "<table><tr><td>6</td></tr></table>";
|
||||
assert!(cx.update(|app| !view.read(app).holds_copy(other)));
|
||||
let foreign = "<td>6</td><!--ely-copy:1-999-->";
|
||||
assert!(
|
||||
cx.update(|app| !view.read(app).holds_copy(foreign)),
|
||||
"another copy's mark"
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
use super::*;
|
||||
use gpui::TestAppContext;
|
||||
|
||||
/// Steps the scheduler until `done` or the budget ends.
|
||||
fn settle(cx: &TestAppContext, done: impl Fn(&App) -> bool) -> bool {
|
||||
for _ in 0..60 {
|
||||
for _ in 0..20 {
|
||||
cx.executor().tick();
|
||||
}
|
||||
if cx.update(|app| done(app)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn run_reaches_the_kernel_and_state_comes_back() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = TestAppContext::single();
|
||||
let (view, _) = cx.update(|app| live_view(app, &install));
|
||||
cx.update(|app| {
|
||||
view.update(app, |view, _| {
|
||||
assert!(
|
||||
view.run("format.dialog", &[]).is_err(),
|
||||
"unknown ids fail fast"
|
||||
);
|
||||
assert!(
|
||||
view.run("format.size", &[]).is_err(),
|
||||
"missing values fail fast"
|
||||
);
|
||||
view.commit(None, "a");
|
||||
view.run("edit.select_all", &[]).expect("select all queues");
|
||||
view.run("format.bold", &[]).expect("bold queues");
|
||||
view.run("format.size", &["20"]).expect("size queues");
|
||||
})
|
||||
});
|
||||
let landed = settle(&cx, |app| {
|
||||
let view = view.read(app);
|
||||
view.state(".uno:Bold") == Some("true")
|
||||
&& view.state(".uno:FontHeight") == Some("20")
|
||||
&& view.a11y.text == "aHello 世界"
|
||||
});
|
||||
assert!(landed, "the kernel state reaches the view");
|
||||
cx.update(|app| {
|
||||
let view = view.read(app);
|
||||
assert_eq!(
|
||||
view.a11y.text, "aHello 世界",
|
||||
"the a11y text follows the paragraph"
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_kernel_dialog_reaches_apps_as_an_error() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = TestAppContext::single();
|
||||
let body = crate::client::tests::input::FODS;
|
||||
let (view, _) = cx.update(|app| live_view_of(app, &install, "in.fods", body));
|
||||
let errors = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
|
||||
cx.update(|app| {
|
||||
let errors = errors.clone();
|
||||
app.subscribe(&view, move |_, error: &LokError, _| {
|
||||
errors.borrow_mut().push(error.0.clone())
|
||||
})
|
||||
.detach();
|
||||
view.update(app, |view, _| {
|
||||
view.key_down(&gpui::Keystroke::parse("backspace").expect("backspace"));
|
||||
});
|
||||
});
|
||||
let reported = settle(&cx, |_| !errors.borrow().is_empty());
|
||||
assert!(reported, "the blocked dialog reaches subscribers");
|
||||
assert!(
|
||||
errors.borrow()[0].contains("Delete Contents"),
|
||||
"{:?}",
|
||||
errors.borrow()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dead_worker_reaches_apps_while_idle() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = TestAppContext::single();
|
||||
let (view, shared) = cx.update(|app| live_view(app, &install));
|
||||
let errors = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
|
||||
cx.update(|app| {
|
||||
let errors = errors.clone();
|
||||
app.subscribe(&view, move |_, error: &LokError, _| {
|
||||
errors.borrow_mut().push(error.0.clone())
|
||||
})
|
||||
.detach();
|
||||
});
|
||||
let pid = shared.lock().expect("the worker lock").child.id();
|
||||
std::process::Command::new("kill")
|
||||
.args(["-9", &pid.to_string()])
|
||||
.status()
|
||||
.expect("kill runs");
|
||||
let reported = settle(&cx, |_| !errors.borrow().is_empty());
|
||||
assert!(reported, "the idle watch reports the dead engine");
|
||||
assert!(
|
||||
errors.borrow()[0].contains("engine stopped"),
|
||||
"{:?}",
|
||||
errors.borrow()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_handle_drag_resizes_the_selected_image() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = TestAppContext::single();
|
||||
let (view, _) = cx.update(|app| live_view(app, &install));
|
||||
let dir = tempfile::tempdir().expect("a temp dir");
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(300, 200, image::Rgba([0, 0, 255, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.expect("the png encodes");
|
||||
let path = dir.path().join("blue.png");
|
||||
std::fs::write(&path, png).expect("the png writes");
|
||||
let url = crate::file_url(&path);
|
||||
cx.update(|app| view.update(app, |view, _| view.run("image.insert", &[&url])))
|
||||
.expect("the insert queues");
|
||||
assert!(
|
||||
settle(&cx, |app| view.read(app).graphic.is_some()),
|
||||
"the image is selected"
|
||||
);
|
||||
let frame = cx.update(|app| view.read(app).graphic).expect("selected");
|
||||
let corner = super::super::handles::points(frame)[7];
|
||||
let to = (corner.0 - frame.2 / 3, corner.1 - frame.3 / 3);
|
||||
cx.update(|app| {
|
||||
view.update(app, |view, _| {
|
||||
assert!(
|
||||
view.grab(view.window_point(corner)),
|
||||
"the corner handle grabs"
|
||||
);
|
||||
view.grip = view
|
||||
.grip
|
||||
.map(|grip| super::super::handles::Grip { to, ..grip });
|
||||
assert_eq!(
|
||||
view.frame(),
|
||||
Some(super::super::handles::dragged(frame, 7, to))
|
||||
);
|
||||
let grip = view.grip.take().expect("held");
|
||||
view.send(super::super::input::Input::Drag {
|
||||
from: grip.from,
|
||||
to: grip.to,
|
||||
});
|
||||
})
|
||||
});
|
||||
let resized = settle(&cx, |app| {
|
||||
view.read(app)
|
||||
.graphic
|
||||
.is_some_and(|now| now.2 < frame.2 && now.3 < frame.3)
|
||||
});
|
||||
assert!(resized, "the kernel resized the image");
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_fitted_slide_fills_the_viewport_centred() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = gpui::TestAppContext::single();
|
||||
cx.update(|app| {
|
||||
let deck = crate::client::tests::show::DECK;
|
||||
let (view, _) = live_view_of(app, &install, "deck.fodp", deck);
|
||||
view.update(app, |view, cx| {
|
||||
view.fit_page(cx);
|
||||
let bounds = Bounds {
|
||||
origin: point(px(10.0), px(20.0)),
|
||||
size: size(px(800.0), px(600.0)),
|
||||
};
|
||||
view.prepare(bounds);
|
||||
let (w, h) = view.doc_size;
|
||||
let want = (800.0 * 15.0 / w as f32).min(600.0 * 15.0 / h as f32) * 0.94;
|
||||
assert!(
|
||||
(view.geom.zoom - want).abs() < 0.001,
|
||||
"{} vs {want}",
|
||||
view.geom.zoom
|
||||
);
|
||||
assert!(
|
||||
view.spare.0 > 0.0 && view.spare.1 > 0.0,
|
||||
"centred: {:?}",
|
||||
view.spare
|
||||
);
|
||||
let corner = view.window_point((0, 0));
|
||||
assert_eq!(view.doc_point(corner), (0, 0), "points map both ways");
|
||||
// A cursor far out must not move a fitted page.
|
||||
view.reach = (w * 3, h * 3);
|
||||
view.prepare(bounds);
|
||||
assert_eq!(view.window_point((0, 0)), corner, "the page stays put");
|
||||
let far = view.window_point((w, h));
|
||||
assert!(
|
||||
f32::from(far.y) < 620.0 && f32::from(far.x) < 810.0,
|
||||
"the page fits"
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_resized_slide_reaches_the_view_and_still_fits() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = gpui::TestAppContext::single();
|
||||
let view = cx.update(|app| {
|
||||
let deck = crate::client::tests::show::DECK;
|
||||
live_view_of(app, &install, "deck.fodp", deck).0
|
||||
});
|
||||
cx.update(|app| {
|
||||
view.update(app, |view, cx| {
|
||||
view.fit_page(cx);
|
||||
view.run("slide.size", &["300000", "300000", "true"])
|
||||
.expect("sent");
|
||||
})
|
||||
});
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
|
||||
while cx.update(|app| view.read(app).doc_size.0 < 100_000) {
|
||||
assert!(
|
||||
std::time::Instant::now() < deadline,
|
||||
"the view never read the new size"
|
||||
);
|
||||
if !cx.executor().tick() {
|
||||
std::thread::sleep(std::time::Duration::from_millis(20));
|
||||
}
|
||||
}
|
||||
cx.update(|app| {
|
||||
view.update(app, |view, _| {
|
||||
let bounds = Bounds {
|
||||
origin: point(px(0.0), px(0.0)),
|
||||
size: size(px(800.0), px(600.0)),
|
||||
};
|
||||
view.prepare(bounds);
|
||||
let (w, h) = view.doc_size;
|
||||
let far = view.window_point((w, h));
|
||||
assert!(
|
||||
f32::from(far.x) <= 800.0 && f32::from(far.y) <= 600.0,
|
||||
"a 3 m page fits: {far:?}"
|
||||
);
|
||||
})
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,271 @@
|
||||
use super::*;
|
||||
use crate::client::tests::input::paragraphs;
|
||||
use ely_platform::clipboard::{self, Clip};
|
||||
use gpui::{InputHandler, Keystroke, TestAppContext, Window};
|
||||
|
||||
/// Steps the scheduler so queued input reaches LOK.
|
||||
fn flush(cx: &TestAppContext) {
|
||||
for _ in 0..80 {
|
||||
cx.executor().tick();
|
||||
}
|
||||
}
|
||||
|
||||
/// The document's paragraphs, saved through the shared worker.
|
||||
fn saved(view: &Entity<LokView>, cx: &mut TestAppContext) -> Vec<String> {
|
||||
let out = tempfile::tempdir()
|
||||
.expect("a temp dir")
|
||||
.keep()
|
||||
.join("out.fodt");
|
||||
cx.update(|app| {
|
||||
let view = view.read(app);
|
||||
let mut worker = view.worker.lock().expect("the worker lock");
|
||||
view.doc
|
||||
.save_as(&mut worker, &format!("file://{}", out.display()), "fodt")
|
||||
.expect("the save works");
|
||||
});
|
||||
paragraphs(&std::fs::read_to_string(&out).expect("the save reads"))
|
||||
}
|
||||
|
||||
fn typed(key: &str) -> Keystroke {
|
||||
let mut stroke = Keystroke::parse(key).expect("a keystroke");
|
||||
stroke.key_char = Some(key.to_string());
|
||||
stroke
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typing_and_composing_reach_the_document() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let mut cx = TestAppContext::single();
|
||||
let (view, _) = cx.update(|app| live_view(app, &install));
|
||||
cx.update(|app| {
|
||||
view.update(app, |view, _| {
|
||||
assert!(!view.key_down(&typed("a")), "text waits for the IME");
|
||||
view.commit(None, "ab");
|
||||
view.compose(None, "n", None);
|
||||
view.compose(None, "ni", None);
|
||||
view.compose(None, "你", None);
|
||||
assert_eq!(view.marked.as_ref().map(|m| m.text.as_str()), Some("你"));
|
||||
view.commit(None, "你");
|
||||
assert!(view.marked.is_none());
|
||||
view.compose(None, "x", None);
|
||||
view.compose(None, "", None);
|
||||
assert!(view.marked.is_none(), "an empty composition cancels");
|
||||
assert!(view.key_down(&Keystroke::parse("enter").expect("enter")));
|
||||
})
|
||||
});
|
||||
flush(&cx);
|
||||
assert_eq!(saved(&view, &mut cx), ["ab你", "Hello 世界"]);
|
||||
}
|
||||
|
||||
struct Blank;
|
||||
|
||||
impl gpui::Render for Blank {
|
||||
fn render(&mut self, _: &mut Window, _: &mut gpui::Context<Self>) -> impl gpui::IntoElement {
|
||||
gpui::Empty
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_platform_ime_callbacks_compose_in_utf16() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let mut cx = TestAppContext::single();
|
||||
let (view, _) = cx.update(|app| live_view(app, &install));
|
||||
let window = cx.add_window(|_, _| Blank);
|
||||
cx.update_window(window.into(), |_, window, app| {
|
||||
let bounds = Bounds::default();
|
||||
let mut ime = gpui::ElementInputHandler::new(bounds, view.clone());
|
||||
ime.replace_and_mark_text_in_range(None, "nihao", Some(2..2), window, app);
|
||||
let selected = ime
|
||||
.selected_text_range(false, window, app)
|
||||
.expect("a selection");
|
||||
assert_eq!(selected.range, 2..2, "the IME caret sits inside");
|
||||
let mut adjusted = None;
|
||||
let head = ime.text_for_range(0..2, &mut adjusted, window, app);
|
||||
assert_eq!(head.as_deref(), Some("ni"));
|
||||
assert_eq!(adjusted, Some(0..2));
|
||||
assert_eq!(ime.marked_text_range(window, app), Some(0..5));
|
||||
ime.replace_and_mark_text_in_range(Some(0..2), "你", Some(1..1), window, app);
|
||||
let marked = view.read(app).marked.as_ref().map(|m| m.text.clone());
|
||||
assert_eq!(marked.as_deref(), Some("你hao"));
|
||||
let selected = ime.selected_text_range(false, window, app).map(|s| s.range);
|
||||
assert_eq!(selected, Some(1..1));
|
||||
ime.replace_text_in_range(Some(1..4), "好", window, app);
|
||||
assert_eq!(
|
||||
ime.marked_text_range(window, app),
|
||||
None,
|
||||
"the commit ends it"
|
||||
);
|
||||
ime.replace_and_mark_text_in_range(None, "x", None, window, app);
|
||||
ime.unmark_text(window, app);
|
||||
})
|
||||
.expect("the window lives");
|
||||
flush(&cx);
|
||||
assert_eq!(saved(&view, &mut cx), ["你好xHello 世界"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_cut_keeps_the_text_when_the_clipboard_fails() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let mut cx = TestAppContext::single();
|
||||
let (view, shared) = cx.update(|app| live_view(app, &install));
|
||||
let doc = cx.update(|app| view.read(app).doc.clone());
|
||||
{
|
||||
let mut worker = shared.lock().expect("the worker lock");
|
||||
worker
|
||||
.run(&doc, "edit.select_all", &[])
|
||||
.expect("select all");
|
||||
let failed = crate::view::clip::copy_out(&mut worker, &doc, true, |_| {
|
||||
Err(std::io::Error::other("the clipboard refused"))
|
||||
});
|
||||
assert!(failed.is_err(), "the write failure surfaces");
|
||||
assert!(
|
||||
!worker.copy(&doc).expect("copy runs").is_empty(),
|
||||
"the selection stays"
|
||||
);
|
||||
}
|
||||
assert_eq!(saved(&view, &mut cx), ["Hello 世界"], "the text stays");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_drag_selects_and_events_carry_the_cursor() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = TestAppContext::single();
|
||||
let (view, _) = cx.update(|app| live_view(app, &install));
|
||||
cx.update(|app| {
|
||||
view.update(app, |view, _| {
|
||||
let none = gpui::Modifiers::default();
|
||||
let y = px(view.geom.px_of(1500));
|
||||
view.mouse(0, point(px(view.geom.px_of(1500)), y), 1, 1, &none);
|
||||
view.mouse(2, point(px(view.geom.px_of(4000)), y), 1, 1, &none);
|
||||
view.mouse(1, point(px(view.geom.px_of(4000)), y), 1, 1, &none);
|
||||
})
|
||||
});
|
||||
let mut selected = false;
|
||||
for _ in 0..40 {
|
||||
flush(&cx);
|
||||
selected = cx.update(|app| !view.read(app).selection.is_empty());
|
||||
if selected {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert!(selected, "the drag selection reaches the view");
|
||||
cx.update(|app| {
|
||||
let view = view.read(app);
|
||||
let bounds = view.cursor_bounds().expect("the cursor arrived");
|
||||
assert!(bounds.size.height > px(0.0), "the caret has height");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_dead_document_surfaces_an_error() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = TestAppContext::single();
|
||||
let (view, shared) = cx.update(|app| live_view(app, &install));
|
||||
let errors = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
|
||||
cx.update(|app| {
|
||||
let errors = errors.clone();
|
||||
app.subscribe(&view, move |_, error: &LokError, _| {
|
||||
errors.borrow_mut().push(error.0.clone())
|
||||
})
|
||||
.detach();
|
||||
let doc = view.read(app).doc.clone();
|
||||
shared
|
||||
.lock()
|
||||
.expect("the worker lock")
|
||||
.close_doc(doc)
|
||||
.expect("the doc closes");
|
||||
view.update(app, |view, _| {
|
||||
view.key_down(&Keystroke::parse("enter").expect("enter"));
|
||||
});
|
||||
});
|
||||
flush(&cx);
|
||||
let errors = errors.borrow();
|
||||
assert!(
|
||||
errors
|
||||
.iter()
|
||||
.any(|error| error.contains("no such document")),
|
||||
"the failure reaches subscribers: {errors:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_clipboard_moves_html_tables_and_images() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
if std::env::var_os("ELY_CLIPBOARD_TEST").is_none() {
|
||||
eprintln!("SKIP: set ELY_CLIPBOARD_TEST=1; this replaces the clipboard");
|
||||
return;
|
||||
}
|
||||
let mut cx = TestAppContext::single();
|
||||
let (view, _) = cx.update(|app| live_view(app, &install));
|
||||
let primary = if cfg!(target_os = "macos") {
|
||||
"cmd-a"
|
||||
} else {
|
||||
"ctrl-a"
|
||||
};
|
||||
cx.update(|app| {
|
||||
view.update(app, |view, _| {
|
||||
view.key_down(&Keystroke::parse(primary).expect("select all"));
|
||||
view.send(crate::view::input::Input::Copy { cut: false });
|
||||
})
|
||||
});
|
||||
flush(&cx);
|
||||
let Some(Clip::Html { html, text }) = clipboard::read().expect("the clipboard reads") else {
|
||||
panic!("the copy lands as html");
|
||||
};
|
||||
assert!(
|
||||
html.contains("Hello") && text == "Hello 世界",
|
||||
"{html} / {text}"
|
||||
);
|
||||
|
||||
clipboard::write(&Clip::Html {
|
||||
html: "<table><tr><td>left</td><td>right</td></tr></table>".into(),
|
||||
text: "left\tright".into(),
|
||||
})
|
||||
.expect("the table writes");
|
||||
cx.update(|app| view.update(app, |view, _| view.send(crate::view::input::Input::Paste)));
|
||||
flush(&cx);
|
||||
let text = saved(&view, &mut cx);
|
||||
assert!(
|
||||
text.contains(&"left".to_string()) && text.contains(&"right".to_string()),
|
||||
"{text:?}"
|
||||
);
|
||||
|
||||
let mut png = Vec::new();
|
||||
image::RgbaImage::from_pixel(4, 4, image::Rgba([255, 0, 0, 255]))
|
||||
.write_to(&mut std::io::Cursor::new(&mut png), image::ImageFormat::Png)
|
||||
.expect("the png encodes");
|
||||
clipboard::write(&Clip::Png(png)).expect("the image writes");
|
||||
cx.update(|app| view.update(app, |view, _| view.send(crate::view::input::Input::Paste)));
|
||||
flush(&cx);
|
||||
let out = tempfile::tempdir()
|
||||
.expect("a temp dir")
|
||||
.keep()
|
||||
.join("img.fodt");
|
||||
cx.update(|app| {
|
||||
let view = view.read(app);
|
||||
let mut worker = view.worker.lock().expect("the worker lock");
|
||||
view.doc
|
||||
.save_as(&mut worker, &format!("file://{}", out.display()), "fodt")
|
||||
.expect("the save works");
|
||||
});
|
||||
let xml = std::fs::read_to_string(&out).expect("the save reads");
|
||||
assert!(xml.contains("<draw:image"), "the image lands");
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
use super::*;
|
||||
|
||||
/// A sheet whose last value sits 100,000 rows down.
|
||||
const TALL: &str = r#"<?xml version="1.0" encoding="UTF-8"?>
|
||||
<office:document xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"
|
||||
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"
|
||||
xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" office:version="1.3" office:mimetype="application/vnd.oasis.opendocument.spreadsheet">
|
||||
<office:body><office:spreadsheet><table:table table:name="S">
|
||||
<table:table-row><table:table-cell><text:p>top</text:p></table:table-cell></table:table-row>
|
||||
<table:table-row table:number-rows-repeated="99998"><table:table-cell/></table:table-row>
|
||||
<table:table-row><table:table-cell><text:p>bottom</text:p></table:table-cell></table:table-row>
|
||||
</table:table></office:spreadsheet></office:body>
|
||||
</office:document>"#;
|
||||
|
||||
#[test]
|
||||
fn a_tall_sheet_paints_and_keeps_only_tiles_near_the_screen() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = gpui::TestAppContext::single();
|
||||
cx.update(|app| {
|
||||
let (view, _) = live_view_of(app, &install, "tall.fods", TALL);
|
||||
view.update(app, |view, cx| {
|
||||
let bounds = Bounds {
|
||||
origin: point(px(0.0), px(0.0)),
|
||||
size: size(px(800.0), px(600.0)),
|
||||
};
|
||||
let side = view.geom.tile_pixels() as usize;
|
||||
let painted = |generation| {
|
||||
Ok(crate::Tile {
|
||||
generation,
|
||||
pixels: vec![0u8; side * side * 4],
|
||||
})
|
||||
};
|
||||
// 800 by 600 needs 5 by 4 tiles.
|
||||
view.prepare(bounds);
|
||||
let top = view.on_screen.clone();
|
||||
assert!(!top.is_empty() && top.len() <= 20, "{} tiles", top.len());
|
||||
for key in &top {
|
||||
view.finish_tile(*key, view.geom, painted(view.generation), cx);
|
||||
}
|
||||
assert_eq!(view.tile_count(), top.len(), "the top paints");
|
||||
view.scroll_by(0.0, -3.0e6, cx);
|
||||
view.prepare(bounds);
|
||||
let bottom = view.on_screen.clone();
|
||||
assert!(bottom.len() <= 20, "{} tiles at the bottom", bottom.len());
|
||||
assert!(
|
||||
bottom.iter().all(|key| key.1 > 1000),
|
||||
"far down: {bottom:?}"
|
||||
);
|
||||
assert!(
|
||||
top.iter().all(|key| !view.tiles.contains_key(key)),
|
||||
"the top's tiles are gone"
|
||||
);
|
||||
assert!(
|
||||
view.fetching.len() <= 20 + top.len(),
|
||||
"only shown tiles fetch"
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
use super::*;
|
||||
use gpui::TestAppContext;
|
||||
|
||||
/// Steps the scheduler until `done` or ten seconds pass.
|
||||
fn settle(cx: &TestAppContext, done: impl Fn(&App) -> bool) -> bool {
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
|
||||
while std::time::Instant::now() < deadline {
|
||||
if !cx.executor().tick() {
|
||||
cx.executor()
|
||||
.advance_clock(std::time::Duration::from_millis(20));
|
||||
}
|
||||
if cx.update(|app| done(app)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// The view's 800 by 600 pixel area.
|
||||
fn area() -> Bounds<Pixels> {
|
||||
Bounds {
|
||||
origin: point(px(0.0), px(0.0)),
|
||||
size: size(px(800.0), px(600.0)),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_view_paints_the_app_theme_and_back() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = TestAppContext::single();
|
||||
let (view, worker) = cx.update(|app| live_view(app, &install));
|
||||
let doc = cx.update(|app| view.read(app).doc.clone());
|
||||
let mut shades = Vec::new();
|
||||
for dark in [true, false] {
|
||||
cx.update(|app| view.update(app, |view, _| view.follow_theme(dark)));
|
||||
assert!(
|
||||
settle(&cx, |app| view.read(app).settled()),
|
||||
"the theme goes out"
|
||||
);
|
||||
let mut worker = worker.lock().expect("the worker");
|
||||
let tile = doc
|
||||
.paint_tile(&mut worker, (1440, 1440, 3840, 3840), (64, 64))
|
||||
.expect("a tile");
|
||||
let sum: u64 = tile
|
||||
.pixels
|
||||
.chunks_exact(4)
|
||||
.map(|px| u64::from(px[0]) + u64::from(px[1]) + u64::from(px[2]))
|
||||
.sum();
|
||||
shades.push(sum / (64 * 64 * 3));
|
||||
}
|
||||
assert!(
|
||||
shades[0] < 100 && shades[1] > 200,
|
||||
"dark then light: {shades:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_tile_reports_and_retries_on_a_move() {
|
||||
let Some(install) = crate::client::tests::install() else {
|
||||
eprintln!("SKIP: no pinned LOK build");
|
||||
return;
|
||||
};
|
||||
let cx = TestAppContext::single();
|
||||
let (view, _) = cx.update(|app| live_view(app, &install));
|
||||
let errors = std::rc::Rc::new(std::cell::RefCell::new(Vec::new()));
|
||||
cx.update(|app| {
|
||||
let errors = errors.clone();
|
||||
app.subscribe(&view, move |_, error: &LokError, _| {
|
||||
errors.borrow_mut().push(error.0.clone())
|
||||
})
|
||||
.detach();
|
||||
view.update(app, |view, cx| {
|
||||
let geom = view.geom;
|
||||
view.prepare(area());
|
||||
view.fetching.remove(&(0, 0));
|
||||
view.finish_tile((0, 0), geom, Err(io::Error::other("no paint")), cx);
|
||||
assert!(view.failed.contains(&((0, 0), view.generation)));
|
||||
view.prepare(area());
|
||||
assert!(!view.fetching.contains(&(0, 0)), "no retry storm in place");
|
||||
view.scroll_by(0.0, -40.0, cx);
|
||||
view.prepare(area());
|
||||
assert!(view.fetching.contains(&(0, 0)), "a move retries the tile");
|
||||
});
|
||||
});
|
||||
assert!(
|
||||
settle(&cx, |_| !errors.borrow().is_empty()),
|
||||
"the failure reaches the app"
|
||||
);
|
||||
assert!(
|
||||
errors.borrow()[0].contains("no paint"),
|
||||
"{:?}",
|
||||
errors.borrow()
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user