mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-12-05 11:15:01 +03:00
282 lines
9.9 KiB
Rust
282 lines
9.9 KiB
Rust
//! Offscreen checks for the settings navigation and editor font size slider.
|
|
|
|
use affine_desktop::{DesktopAppState, DesktopShellView, palette_for, register_desktop_fonts};
|
|
use anyhow::{Context as _, Result};
|
|
use ui::{
|
|
AnyWindowHandle, AppContext, Application, ApplicationHandle, Bounds, HeadlessPlatform,
|
|
HeadlessWindow, Keystroke, MouseButton, MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels,
|
|
PlatformInput, Point, Rgba, SnapshotRect, WindowBounds, WindowHandle, WindowOptions, point, px,
|
|
size,
|
|
};
|
|
use ui_tokens::{DARK_THEME, LIGHT_THEME, ThemeMode};
|
|
|
|
const THEMES: [ThemeMode; 2] = [ThemeMode::Light, ThemeMode::Dark];
|
|
const EDITOR: &str = "desktop.settings.editor";
|
|
const SHORTCUTS: &str = "desktop.settings.shortcuts";
|
|
const SLIDER: &str = "desktop.settings.font.size";
|
|
const THUMB: &str = "desktop.settings.font.size.thumb";
|
|
const THUMB_SIZE: f32 = 14.0;
|
|
|
|
#[test]
|
|
fn settings_navigation_and_font_slider_match_web_pixels() -> Result<()> {
|
|
for theme in THEMES {
|
|
let desktop = Desktop::open_editor_settings(theme)?;
|
|
let editor = desktop.bounds(EDITOR)?;
|
|
let shortcuts = desktop.bounds(SHORTCUTS)?;
|
|
let track = desktop.bounds(SLIDER)?;
|
|
let thumb = desktop.bounds(THUMB)?;
|
|
let (x, y) = nav_sample(shortcuts);
|
|
let rest = desktop.image()?.get_pixel(x, y).0;
|
|
|
|
desktop.move_to(point(px(x as f32 + 0.5), px(y as f32 + 0.5)), None);
|
|
let image = desktop.image()?;
|
|
|
|
let hover = Rgba::from(palette_for(theme).hover_color);
|
|
let mut expected = rest;
|
|
for (channel, source) in expected.iter_mut().zip([hover.r, hover.g, hover.b]) {
|
|
*channel =
|
|
(source * 255.0 * hover.a + f32::from(*channel) * (1.0 - hover.a)).round() as u8;
|
|
}
|
|
assert_ne!(expected, rest, "{theme:?} hover changes the surface");
|
|
for item in [editor, shortcuts] {
|
|
let (x, y) = nav_sample(item);
|
|
assert_eq!(
|
|
image.get_pixel(x, y).0,
|
|
expected,
|
|
"{theme:?} navigation item"
|
|
);
|
|
}
|
|
|
|
assert_eq!(track.height, 28.0, "{theme:?} slider hit area");
|
|
assert_eq!([thumb.width, thumb.height], [THUMB_SIZE; 2]);
|
|
assert_eq!(thumb.y + thumb.height / 2.0, track.y + track.height / 2.0);
|
|
let tokens = match theme {
|
|
ThemeMode::Light => &LIGHT_THEME,
|
|
ThemeMode::Dark => &DARK_THEME,
|
|
};
|
|
let color = |token| {
|
|
let c = tokens
|
|
.color(token)
|
|
.with_context(|| format!("resolve {token}"))?;
|
|
Ok::<_, anyhow::Error>([c.r, c.g, c.b, c.a])
|
|
};
|
|
let fill = color("--affine-v2-icon-primary")?;
|
|
let border = color("--affine-v2-layer-insideBorder-border")?;
|
|
let y = (track.y + track.height / 2.0) as u32;
|
|
let center_x = (thumb.x + thumb.width / 2.0) as u32;
|
|
assert_eq!(image.get_pixel(center_x, y).0, fill, "{theme:?} thumb");
|
|
let track_rows = |x, color| {
|
|
(track.y as u32..(track.y + track.height) as u32)
|
|
.filter(|&row| image.get_pixel(x, row).0 == color)
|
|
.collect::<Vec<_>>()
|
|
};
|
|
let filled = track_rows((track.x + THUMB_SIZE) as u32, fill);
|
|
let unfilled = track_rows((track.x + track.width - THUMB_SIZE) as u32, border);
|
|
assert_eq!(filled.len(), 1, "{theme:?} filled track thickness");
|
|
assert_eq!(
|
|
unfilled, filled,
|
|
"{theme:?} unfilled track thickness and center"
|
|
);
|
|
assert!(filled[0].abs_diff(y) <= 1, "{theme:?} track center");
|
|
assert_ne!(image.get_pixel(track.x as u32, filled[0]).0, fill);
|
|
assert_ne!(
|
|
image
|
|
.get_pixel((track.x + track.width - 1.0) as u32, filled[0])
|
|
.0,
|
|
border
|
|
);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn font_slider_maps_clicks_drags_and_keys_to_exact_sizes() -> Result<()> {
|
|
for theme in THEMES {
|
|
let desktop = Desktop::open_editor_settings(theme)?;
|
|
let track = desktop.bounds(SLIDER)?;
|
|
let at_size = |size: u8| {
|
|
let fraction = f32::from(size - 12) / 12.0;
|
|
point(
|
|
px(track.x + THUMB_SIZE / 2.0 + (track.width - THUMB_SIZE) * fraction),
|
|
px(track.y + track.height / 2.0),
|
|
)
|
|
};
|
|
for size in 12..=24 {
|
|
let p = at_size(size);
|
|
desktop.click(p);
|
|
assert_eq!(desktop.font_size()?, size, "{theme:?} click");
|
|
let thumb = desktop.bounds(THUMB)?;
|
|
assert!((thumb.x + thumb.width / 2.0 - f32::from(p.x)).abs() <= 1.0);
|
|
}
|
|
|
|
let start = at_size(24);
|
|
desktop
|
|
.window
|
|
.dispatch_input(PlatformInput::MouseDown(MouseDownEvent {
|
|
button: MouseButton::Left,
|
|
position: start,
|
|
modifiers: Default::default(),
|
|
click_count: 1,
|
|
first_mouse: false,
|
|
}));
|
|
desktop.move_to(at_size(22), Some(MouseButton::Left));
|
|
for (p, size) in [
|
|
(at_size(19), 19),
|
|
(point(px(track.x - 10.0), start.y), 12),
|
|
(point(px(track.x + track.width + 10.0), start.y), 24),
|
|
] {
|
|
desktop.move_to(p, Some(MouseButton::Left));
|
|
assert_eq!(desktop.font_size()?, size, "{theme:?} drag");
|
|
}
|
|
desktop
|
|
.window
|
|
.dispatch_input(PlatformInput::MouseUp(MouseUpEvent {
|
|
button: MouseButton::Left,
|
|
position: at_size(24),
|
|
modifiers: Default::default(),
|
|
click_count: 1,
|
|
}));
|
|
desktop.move_to(at_size(18), None);
|
|
assert_eq!(desktop.font_size()?, 24, "{theme:?} drag released");
|
|
|
|
for (key, size) in [
|
|
("right", 24),
|
|
("left", 23),
|
|
("down", 22),
|
|
("up", 23),
|
|
("home", 12),
|
|
("left", 12),
|
|
("end", 24),
|
|
] {
|
|
desktop.app.update(|cx| {
|
|
AnyWindowHandle::from(desktop.handle).update(cx, |_, window, cx| {
|
|
window.dispatch_keystroke(Keystroke::parse(key).expect("valid key"), cx);
|
|
})
|
|
})?;
|
|
desktop.frame();
|
|
assert_eq!(desktop.font_size()?, size, "{theme:?} {key}");
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "writes offscreen evidence to SETTINGS_EVIDENCE_DIR"]
|
|
fn capture_settings_editor() -> Result<()> {
|
|
let dir = std::path::PathBuf::from(
|
|
std::env::var_os("SETTINGS_EVIDENCE_DIR").context("set SETTINGS_EVIDENCE_DIR")?,
|
|
);
|
|
std::fs::create_dir_all(&dir)?;
|
|
for (theme, name) in [(ThemeMode::Light, "light"), (ThemeMode::Dark, "dark")] {
|
|
let desktop = Desktop::open_editor_settings(theme)?;
|
|
let (x, y) = nav_sample(desktop.bounds(SHORTCUTS)?);
|
|
desktop.move_to(point(px(x as f32 + 0.5), px(y as f32 + 0.5)), None);
|
|
desktop
|
|
.image()?
|
|
.save(dir.join(format!("settings-editor-{name}.png")))?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn nav_sample(bounds: SnapshotRect) -> (u32, u32) {
|
|
(
|
|
(bounds.x + bounds.width - 16.0) as u32,
|
|
(bounds.y + bounds.height / 2.0) as u32,
|
|
)
|
|
}
|
|
|
|
struct Desktop {
|
|
platform: std::rc::Rc<HeadlessPlatform>,
|
|
app: ApplicationHandle,
|
|
handle: WindowHandle<DesktopShellView>,
|
|
window: HeadlessWindow,
|
|
}
|
|
|
|
impl Desktop {
|
|
fn open_editor_settings(theme: ThemeMode) -> Result<Self> {
|
|
let mut state = DesktopAppState::seed_showcase(theme, "2026-12-03");
|
|
state.open_doc_in_active_pane("doc-arch");
|
|
state.open_settings();
|
|
let platform = HeadlessPlatform::new(1280, 800, 1.0);
|
|
let app = Application::with_platform(platform.clone()).run_embedded(|_| {});
|
|
let handle = app.update(|cx| {
|
|
register_desktop_fonts(cx)?;
|
|
cx.open_window(
|
|
WindowOptions {
|
|
window_bounds: Some(WindowBounds::Windowed(Bounds::new(
|
|
point(px(0.0), px(0.0)),
|
|
size(px(1280.0), px(800.0)),
|
|
))),
|
|
..WindowOptions::default()
|
|
},
|
|
|_, cx| cx.new(|cx| DesktopShellView::new(state, cx)),
|
|
)
|
|
})?;
|
|
let window = platform
|
|
.windows()
|
|
.into_iter()
|
|
.next()
|
|
.context("desktop window")?;
|
|
let desktop = Self {
|
|
platform,
|
|
app,
|
|
handle,
|
|
window,
|
|
};
|
|
desktop.frame();
|
|
let editor = desktop.bounds(EDITOR)?;
|
|
desktop.click(point(
|
|
px(editor.x + editor.width / 2.0),
|
|
px(editor.y + editor.height / 2.0),
|
|
));
|
|
Ok(desktop)
|
|
}
|
|
|
|
fn frame(&self) {
|
|
self.window.request_frame();
|
|
self.platform.run_until_parked();
|
|
}
|
|
|
|
fn bounds(&self, id: &str) -> Result<SnapshotRect> {
|
|
self.app.update(|cx| {
|
|
self.handle.update(cx, |_, window, _| {
|
|
window
|
|
.frame_snapshot()
|
|
.elements
|
|
.get(id)
|
|
.map(|element| element.bounds)
|
|
.with_context(|| format!("find {id}"))
|
|
})
|
|
})?
|
|
}
|
|
|
|
fn image(&self) -> Result<image::RgbaImage> {
|
|
self.app.update(|cx| {
|
|
self.handle
|
|
.update(cx, |_, window, _| window.render_to_image())
|
|
})?
|
|
}
|
|
|
|
fn font_size(&self) -> Result<u8> {
|
|
self.app.update(|cx| {
|
|
self.handle
|
|
.update(cx, |shell, _, _| shell.state.editor_font.size())
|
|
})
|
|
}
|
|
|
|
fn click(&self, position: Point<Pixels>) {
|
|
self.window.simulate_click(position);
|
|
self.frame();
|
|
}
|
|
|
|
fn move_to(&self, position: Point<Pixels>, pressed_button: Option<MouseButton>) {
|
|
self.window
|
|
.dispatch_input(PlatformInput::MouseMove(MouseMoveEvent {
|
|
position,
|
|
pressed_button,
|
|
modifiers: Default::default(),
|
|
}));
|
|
self.frame();
|
|
}
|
|
}
|