use super::child_id;
use crate::*;
use arc_tokens::{Accent, Spacing, ThemeMode, install_arc_theme};
use gpui_kit::{
App, Context, ElementId, InteractiveElement as _, IntoElement, MouseButton, ParentElement,
Render, Styled, TestAppContext, TestSupportExt as _, Window, div, px,
test::{TestEventExt as _, TestQueryExt as _, TestWindowExt as _},
};
use std::{cell::RefCell, rc::Rc};
#[derive(Clone, Copy, Debug)]
enum Kind {
Command,
Notification,
Upload,
Otp,
Empty,
Page,
Comparison,
Changelog,
}
struct Host {
kind: Kind,
events: Rc<RefCell<Vec<String>>>,
disabled: bool,
}
struct InstantDoneHost;
impl Render for InstantDoneHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.w(px(560.0))
.p(Spacing::S2)
.child(
FileUpload::new("instant-upload").on_upload(|request, w, cx| {
request.finish(Ok(()), w, cx);
}),
)
}
}
impl Render for Host {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
let events = self.events.clone();
let element = match self.kind {
Kind::Command => CommandPalette::new("block")
.items([
CommandItem::new("a", "Open project").group("Workspace"),
CommandItem::new("b", "Settings").group("Settings"),
])
.on_select(move |item, _, _| events.borrow_mut().push(item.id.to_string()))
.into_any_element(),
Kind::Notification => NotificationCenter::new("block")
.notifications([
NotificationItem::new("a", "Ready", "now").description("Details"),
NotificationItem::new("b", "Earlier", "1h").read(true),
])
.on_read_change(move |i, read, _, _| {
events.borrow_mut().push(format!("{}:{read}", i.id))
})
.into_any_element(),
Kind::Upload => FileUpload::new("block")
.disabled(self.disabled)
.value([FileUploadItem::new(
"a",
"report.pdf",
1024,
"application/pdf",
)])
.on_change(move |items, _, _| events.borrow_mut().push(items.len().to_string()))
.into_any_element(),
Kind::Otp => OtpInput::new("block", "Code")
.length(4)
.disabled(self.disabled)
.on_change(move |v, _, _| events.borrow_mut().push(v.to_string()))
.into_any_element(),
Kind::Empty => EmptyStates::new("block")
.on_action(move |scene, _, _| events.borrow_mut().push(format!("{scene:?}")))
.into_any_element(),
Kind::Page => PageHeader::new("block")
.issues([PageIssue::new("a", "Fix layout")])
.files([PageFile::new("doc", "Notes", "Document", "1 KB")])
.project_url("https://northline.example/projects/checkout-redesign")
.on_notice(move |m, _, _| events.borrow_mut().push(m.to_string()))
.into_any_element(),
Kind::Comparison => ComparisonTable::new("block")
.columns([
ComparisonColumn {
id: "a".into(),
name: "Arc".into(),
caption: None,
highlight: true,
},
ComparisonColumn {
id: "b".into(),
name: "Other".into(),
caption: None,
highlight: false,
},
])
.sections([ComparisonSection {
id: "features".into(),
title: "Features".into(),
rows: vec![
ComparisonRow {
id: "same".into(),
feature: "Same".into(),
hint: None,
values: vec![true.into(), true.into()],
},
ComparisonRow {
id: "different".into(),
feature: "Different".into(),
hint: None,
values: vec![true.into(), false.into()],
},
],
}])
.cta("Try", Some("Done".into()), move |_, _| {
events.borrow_mut().push("cta".into())
})
.into_any_element(),
Kind::Changelog => {
let log = events.clone();
ChangelogFeed::new("block")
.entries([
ChangelogEntry::new("a", "November", "New feature")
.detail("Details")
.media(ChangelogMedia::Code {
file: "webhooks.ts".into(),
code: "const event = verify(body);\n\nif (event.type === \"order.paid\") {\n await fulfil(event.data.order);\n}\n\n// A complete multi-line example\nexport { event };".into(),
}),
ChangelogEntry::new("b", "October", "Bug fix").kind(ChangelogKind::Fixed),
])
.on_subscribe(move |email, _, _| log.borrow_mut().push(email.to_string()))
.into_any_element()
}
};
div().w(px(560.0)).p(Spacing::S2).child(element)
}
}
fn id(name: &str) -> ElementId {
child_id(&ElementId::Name("block".into()), name.to_owned())
}
fn nested(parent: &str, child: &str) -> ElementId {
child_id(&id(parent), child.to_owned())
}
fn setup(cx: &mut TestAppContext, mode: ThemeMode) {
cx.update(|cx: &mut App| {
gpui_kit::init(cx);
install_arc_theme(mode, Accent::Violet, cx);
arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Reduce, cx);
});
}
struct SizedHost {
kind: Kind,
width: f32,
}
impl Render for SizedHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
let element = match self.kind {
Kind::Command => CommandPalette::new("sized-command")
.items([
CommandItem::new("a", "Open project").group("Workspace"),
CommandItem::new("b", "Settings").group("Settings"),
])
.into_any_element(),
Kind::Notification => NotificationCenter::new("sized-notifications").into_any_element(),
Kind::Upload => FileUpload::new("sized-upload").into_any_element(),
Kind::Otp => OtpInput::new("sized-otp", "Code").into_any_element(),
Kind::Empty => EmptyStates::new("sized-empty").into_any_element(),
Kind::Page => PageHeader::new("sized-page").into_any_element(),
Kind::Comparison => ComparisonTable::new("sized-comparison").into_any_element(),
Kind::Changelog => ChangelogFeed::new("sized-changelog")
.entries([
ChangelogEntry::new("a", "November", "New feature")
.date("Nov 18")
.version("4.12.0")
.kind(ChangelogKind::New)
.detail("First detail"),
ChangelogEntry::new("b", "October", "Bug fix")
.date("Oct 9")
.version("4.11.0")
.kind(ChangelogKind::Fixed),
])
.into_any_element(),
};
div()
.id("sized-host")
.test_support()
.w(px(self.width))
.child(element)
}
}
#[gpui_kit::test]
fn blocks_render_at_narrow_and_wide_widths_across_theme_roundtrip(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (host, cx) = cx.add_window_view(|_, _| SizedHost {
kind: Kind::Otp,
width: 280.0,
});
for motion in [
arc_motion::ReducedMotionChoice::Allow,
arc_motion::ReducedMotionChoice::Reduce,
] {
for mode in [ThemeMode::Light, ThemeMode::Dark, ThemeMode::Light] {
for width in [280.0, 960.0] {
for kind in [
Kind::Command,
Kind::Notification,
Kind::Upload,
Kind::Otp,
Kind::Empty,
Kind::Page,
Kind::Comparison,
Kind::Changelog,
] {
host.update(cx, |s, cx| {
s.kind = kind;
s.width = width;
install_arc_theme(mode, Accent::Violet, cx);
arc_motion::set_reduced_motion(motion, cx);
cx.notify();
});
for _ in 0..3 {
cx.update(|w, cx| {
w.render_frame(cx);
assert_eq!(w.find("sized-host").bounds().size.width, px(width));
let elements = w.elements();
assert!(elements.len() > 1, "{kind:?} must expose child elements");
for element in elements {
let bounds = element.bounds();
for value in [
f32::from(bounds.origin.x),
f32::from(bounds.origin.y),
f32::from(bounds.size.width),
f32::from(bounds.size.height),
] {
assert!(value.is_finite(), "{kind:?}: {bounds:?}");
}
assert!(bounds.size.width >= px(0.0));
assert!(bounds.size.height >= px(0.0));
}
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(80));
}
}
}
}
}
}
struct NarrowHost;
impl Render for NarrowHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().w(px(280.0)).child(PageHeader::new("block"))
}
}
#[gpui_kit::test]
fn page_actions_fold_into_overflow_when_narrow(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| NarrowHost);
cx.update(|w, cx| {
w.render_frame(cx);
assert!(w.try_find(id("follow")).is_none());
assert!(w.try_find(id("share")).is_none());
w.click(child_id(&id("more"), "trigger"), cx);
w.render_frame(cx);
let target = w.find_by_label("Follow").bounds().center();
w.pointer_down(target, MouseButton::Left, cx);
w.pointer_up(target, MouseButton::Left, cx);
w.render_frame(cx);
w.click(child_id(&id("more"), "trigger"), cx);
w.render_frame(cx);
w.find_by_label("Unfollow");
});
}
#[gpui_kit::test]
fn page_overview_exposes_milestones_and_activity(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Dark);
let (_, cx) = cx.add_window_view(|_, _| NarrowHost);
cx.update(|w, cx| {
w.render_frame(cx);
assert!(w.try_find(id("milestone-Single review step")).is_some());
assert!(w.try_find(id("activity-a1")).is_some());
assert_eq!(w.find(id("tab-Issues")).label(), Some("Issues · 0"));
});
}
#[gpui_kit::test]
fn comparison_filter_is_uncontrolled_and_cta_changes_label(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Comparison,
events,
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
assert!(w.try_find(id("row-same")).is_some());
w.click(id("differences"), cx);
w.render_frame(cx);
assert!(w.try_find(id("row-same")).is_none());
assert!(w.try_find(id("row-different")).is_some());
w.click(id("differences"), cx);
w.render_frame(cx);
assert!(w.try_find(id("row-same")).is_some());
w.click(id("cta"), cx);
w.render_frame(cx);
assert_eq!(w.find(id("cta")).label(), Some("Done"));
});
assert_eq!(&*log.borrow(), &["cta"]);
}
#[gpui_kit::test]
fn page_tabs_archive_and_issue_completion_update_state(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Dark);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Page,
events,
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(child_id(&id("more"), "trigger"), cx);
w.render_frame(cx);
let target = w.find_by_label("Archive project").bounds().center();
w.pointer_down(target, MouseButton::Left, cx);
w.pointer_up(target, MouseButton::Left, cx);
w.render_frame(cx);
w.click(child_id(&id("more"), "trigger"), cx);
w.render_frame(cx);
w.find_by_label("Restore project");
w.click(id("tab-Issues"), cx);
w.render_frame(cx);
w.click(id("check-a"), cx);
w.render_frame(cx);
assert_eq!(w.find(id("check-a")).disabled(), Some(true));
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(420));
cx.update(|w, cx| {
w.render_frame(cx);
assert!(w.try_find(id("check-a")).is_none());
assert!(w.try_find(id("restore-issues")).is_some());
w.click(id("restore-issues"), cx);
w.render_frame(cx);
assert!(w.try_find(id("check-a")).is_some());
w.click(id("tab-Overview"), cx);
w.render_frame(cx);
});
assert_eq!(log.borrow().len(), 2);
assert_eq!(
log.borrow().last().map(String::as_str),
Some("a marked done")
);
}
#[gpui_kit::test]
fn page_follow_create_share_files_and_archive_guard(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Page,
events: Rc::default(),
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("follow"), cx);
assert_eq!(w.find(id("follow")).label(), Some("Unfollow project"));
w.click(id("add-issue"), cx);
w.render_frame(cx);
assert!(w.try_find(id("check-local-issue-1")).is_some());
w.click(id("share"), cx);
w.render_frame(cx);
assert_eq!(w.find(id("share")).label(), Some("Sharing…"));
assert_eq!(w.find(id("share")).disabled(), Some(true));
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(750));
cx.update(|w, cx| {
w.render_frame(cx);
assert!(w.try_find(id("update-local-update-2")).is_some());
assert_eq!(w.find(id("share")).label(), Some("Shared"));
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(1900));
cx.update(|w, cx| {
w.render_frame(cx);
assert_eq!(w.find(id("share")).label(), Some("Share update"));
w.click(id("tab-Files"), cx);
w.render_frame(cx);
assert!(w.try_find(id("file-doc")).is_some());
let open_menu = |w: &mut gpui_kit::Window, cx: &mut gpui_kit::App| {
w.click(child_id(&id("more"), "trigger"), cx);
w.render_frame(cx);
};
open_menu(w, cx);
let target = w.find_by_label("Archive project").bounds().center();
w.pointer_down(target, MouseButton::Left, cx);
w.pointer_up(target, MouseButton::Left, cx);
w.render_frame(cx);
assert_eq!(w.find(id("add-issue")).disabled(), Some(true));
assert_eq!(w.find(id("share")).disabled(), Some(true));
open_menu(w, cx);
let target = w.find_by_label("Restore project").bounds().center();
w.pointer_down(target, MouseButton::Left, cx);
w.pointer_up(target, MouseButton::Left, cx);
w.render_frame(cx);
assert_ne!(w.find(id("share")).disabled(), Some(true));
});
}
#[gpui_kit::test]
fn changelog_multi_filter_month_and_default_open_are_reachable(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Changelog,
events: Rc::default(),
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
assert_eq!(
w.find(nested("entry-a", "toggle")).label(),
Some("Collapse")
);
w.click(id("filter-Fixed"), cx);
w.render_frame(cx);
assert!(w.try_find(nested("entry-a", "toggle")).is_none());
assert!(w.try_find(nested("entry-b", "toggle")).is_some());
w.click(id("filter-New"), cx);
w.render_frame(cx);
assert!(w.try_find(nested("entry-a", "toggle")).is_some());
w.click(id("month-October"), cx);
w.render_frame(cx);
assert!(w.try_find(nested("entry-b", "toggle")).is_some());
assert!(w.try_find(nested("entry-a", "media-code")).is_some());
w.click(nested("entry-a", "toggle"), cx);
w.render_frame(cx);
assert_eq!(w.find(nested("entry-a", "toggle")).label(), Some("Details"));
assert!(w.try_find(nested("entry-a", "media-code")).is_none());
});
}
#[gpui_kit::test]
fn changelog_subscribe_validates_confirms_and_undoes(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Changelog,
events,
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("subscribe"), cx);
w.render_frame(cx);
w.click(id("subscribe-email"), cx);
w.input("bad", cx);
w.click(id("subscribe-submit"), cx);
w.render_frame(cx);
assert!(w.try_find(id("subscribe-note")).is_some());
w.click(id("subscribe-email"), cx);
w.press("ctrl-a", cx);
w.input("you@example.com", cx);
w.click(id("subscribe-submit"), cx);
w.render_frame(cx);
assert!(w.try_find(id("subscribe-undo")).is_some());
w.click(id("subscribe-undo"), cx);
w.render_frame(cx);
assert!(w.try_find(id("subscribe")).is_some());
});
assert_eq!(&*log.borrow(), &["you@example.com"]);
}
#[gpui_kit::test]
fn changelog_month_pill_hugs_selected_button_and_row_click_expands(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (host, cx) = cx.add_window_view(|_, _| SizedHost {
kind: Kind::Changelog,
width: 960.0,
});
let root = ElementId::from("sized-changelog");
let pill_quad = |w: &gpui_kit::Window, cx: &gpui_kit::App| {
use arc_tokens::ActiveArcTheme as _;
let border = cx.arc_theme().colors.border;
w.painted_quads()
.into_iter()
.find(|q| {
q.border_color == border
&& q.border_widths.top.as_f32() > 0.0
&& q.corner_radii.top_left.as_f32() > w.scale_factor() * 10.0
&& q.bounds.size.height.as_f32() / w.scale_factor() <= 30.0
})
.expect("必须绘制月份 pill")
};
cx.update(|w, cx| {
w.render_frame(cx);
w.render_frame(cx);
let month_nov = w.find(child_id(&root, "month-November")).bounds();
let q = pill_quad(w, cx);
let scale = w.scale_factor();
assert!(
(q.bounds.origin.x.as_f32() / scale - f32::from(month_nov.origin.x)).abs() <= 0.6
&& (q.bounds.size.width.as_f32() / scale - f32::from(month_nov.size.width)).abs()
<= 0.6,
"pill 必须贴合选中月份按钮:quad={:?}, btn={:?}",
q.bounds,
month_nov
);
w.click(child_id(&root, "month-October"), cx);
w.render_frame(cx);
w.render_frame(cx);
let month_oct = w.find(child_id(&root, "month-October")).bounds();
let q = pill_quad(w, cx);
assert!(
(q.bounds.origin.x.as_f32() / scale - f32::from(month_oct.origin.x)).abs() <= 0.6,
"pill 必须滑到新选中的月份按钮:quad={:?}, btn={:?}",
q.bounds,
month_oct
);
let entry_a = child_id(&root, "entry-a");
let entry_toggle = child_id(&entry_a, "toggle");
w.click(entry_a.clone(), cx);
w.render_frame(cx);
assert_eq!(w.find(entry_toggle.clone()).label(), Some("Details"));
w.click(entry_a, cx);
w.render_frame(cx);
assert_eq!(w.find(entry_toggle).label(), Some("Collapse"));
});
let _ = host;
}
#[gpui_kit::test]
fn changelog_details_stack_below_summary_and_keep_natural_height(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Changelog,
events: Rc::default(),
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.render_frame(cx);
let row = w.find(id("entry-a")).bounds();
let body = w.find(nested("entry-a", "body")).bounds();
let detail = w.find(nested("entry-a", "details")).bounds();
let panel = w.find(nested("entry-a", "panel")).bounds();
let code = w.find(nested("entry-a", "media-code")).bounds();
assert!(
detail.origin.y >= row.origin.y + row.size.height - px(0.5),
"详情不得并排在摘要右侧:row={row:?}, detail={detail:?}"
);
assert!(
(detail.origin.x - body.origin.x).abs() <= px(0.5),
"详情左侧与摘要对齐"
);
assert!(
panel.size.height >= detail.size.height - px(0.5),
"展开面板必须容纳自然高度"
);
assert!(
panel.size.height > px(160.0),
"多行附件不应限制为旧的 160px"
);
assert!(
code.origin.y + code.size.height <= panel.origin.y + panel.size.height,
"代码附件不得被裁掉"
);
w.click(id("entry-a"), cx);
w.render_frame(cx);
assert!(w.try_find(nested("entry-a", "details")).is_none());
let collapsed = w.find(id("entry-a")).bounds();
let next = w.find(id("entry-b")).bounds();
assert!(
next.origin.y >= collapsed.origin.y + collapsed.size.height,
"收起后仍为纵向列表"
);
});
}
#[gpui_kit::test]
fn empty_art_stage_and_action_remain_centered_in_all_scenes(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Empty,
events: Rc::default(),
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.render_frame(cx);
for index in 0..4 {
w.click(id(&format!("scene-{index}")), cx);
w.render_frame(cx);
w.render_frame(cx);
let root = w.find(ElementId::from("block")).bounds();
let art = w.find(id("artwork")).bounds();
let action = w.find(id("action")).bounds();
let copy = w.find(id("copy")).bounds();
assert!((action.top() - copy.bottom() - Spacing::S4).abs() < px(0.5));
assert!((copy.top() - art.bottom() - Spacing::S5).abs() < px(0.5));
assert!((art.size.width - px(360.0)).abs() < px(0.5));
assert!((art.size.height - art.size.width * (216.0 / 320.0)).abs() < px(0.5));
assert!((art.center().x - root.center().x).abs() < px(0.5));
assert!(
(action.center().x - root.center().x).abs() < px(0.5),
"底部动作必须居中"
);
}
});
}
#[gpui_kit::test]
fn empty_highlight_matches_selected_tab_across_widths_and_themes(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (host, cx) = cx.add_window_view(|_, _| SizedHost {
kind: Kind::Empty,
width: 960.0,
});
let root = ElementId::from("sized-empty");
for mode in [ThemeMode::Light, ThemeMode::Dark, ThemeMode::Light] {
for width in [960.0, 420.0, 280.0, 240.0, 960.0] {
host.update(cx, |s, cx| {
s.width = width;
install_arc_theme(mode, Accent::Violet, cx);
cx.notify();
});
cx.update(|w, cx| {
w.render_frame(cx);
w.render_frame(cx);
});
let clickable: &[usize] = if width >= 300.0 {
&[0, 1, 3, 2]
} else {
&[0, 1]
};
for &index in clickable {
cx.update(|w, cx| {
let selected = child_id(&root, format!("scene-{index}"));
w.click(selected.clone(), cx);
w.render_frame(cx);
let tab = w.find(selected).bounds();
let mut highlights = w
.painted_quads()
.into_iter()
.filter(|q| {
(q.corner_radii.top_left.as_f32() / w.scale_factor() - 9.0).abs() < 0.1
&& q.border_widths.top.as_f32() > 0.0
})
.map(|q| q.bounds)
.collect::<Vec<_>>();
highlights.dedup();
assert_eq!(highlights.len(), 1, "只能有一枚选中底板");
let q = highlights[0];
let scale = w.scale_factor();
for (actual, expected) in [
(q.origin.x.as_f32() / scale, f32::from(tab.origin.x)),
(q.origin.y.as_f32() / scale, f32::from(tab.origin.y)),
(q.size.width.as_f32() / scale, f32::from(tab.size.width)),
(q.size.height.as_f32() / scale, f32::from(tab.size.height)),
] {
assert!(
(actual - expected).abs() <= 0.5 / scale,
"底板必须贴合一个标签:mode={mode:?}, width={width}, index={index}, quad={q:?}, tab={tab:?}"
);
}
assert_eq!(tab.size.height, px(30.0));
if width == 960.0 {
let first = w.find(child_id(&root, "scene-0")).bounds();
let last = w.find(child_id(&root, "scene-3")).bounds();
assert!(
(first.size.width - last.size.width).abs() <= px(0.5),
"宽容器四列应等宽:{first:?}, {last:?}"
);
}
});
}
}
}
}
#[gpui_kit::test]
fn empty_tabs_keyboard_wraps_and_reveals_single_selection(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Dark);
let (_, cx) = cx.add_window_view(|_, _| SizedHost {
kind: Kind::Empty,
width: 240.0,
});
let root = ElementId::from("sized-empty");
cx.update(|w, cx| {
w.render_frame(cx);
w.render_frame(cx);
w.click(child_id(&root, "scene-0"), cx);
w.render_frame(cx);
});
for (key, selected) in [
("end", 3),
("right", 0),
("left", 3),
("home", 0),
("right", 1),
] {
cx.update(|w, cx| {
w.press(key, cx);
w.render_frame(cx);
w.render_frame(cx);
for index in 0..4 {
let tab = w.find(child_id(&root, format!("scene-{index}")));
assert_eq!(tab.selected(), Some(index == selected));
assert_eq!(tab.focused(), Some(index == selected));
assert_eq!(tab.bounds().size.height, px(30.0), "标签不能换行");
}
let tab = w
.find(child_id(&root, format!("scene-{selected}")))
.bounds();
let panel = w.find(child_id(&root, "tabs")).bounds();
assert!(
tab.origin.x >= panel.origin.x && tab.right() <= panel.right(),
"键盘选中项必须滚入可视区:{tab:?}, {panel:?}"
);
});
}
}
#[gpui_kit::test]
fn empty_highlight_glides_from_current_tab_and_settles(cx: &mut TestAppContext) {
fn highlight(w: &Window) -> gpui_kit::Bounds<gpui_kit::Pixels> {
let q = w
.painted_quads()
.into_iter()
.find(|q| {
(q.corner_radii.top_left.as_f32() / w.scale_factor() - 9.0).abs() < 0.1
&& q.border_widths.top.as_f32() > 0.0
})
.expect("必须绘制高亮底板");
let scale = w.scale_factor();
gpui_kit::bounds(
gpui_kit::point(
px(q.bounds.origin.x.as_f32() / scale),
px(q.bounds.origin.y.as_f32() / scale),
),
gpui_kit::size(
px(q.bounds.size.width.as_f32() / scale),
px(q.bounds.size.height.as_f32() / scale),
),
)
}
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| SizedHost {
kind: Kind::Empty,
width: 960.0,
});
let root = ElementId::from("sized-empty");
cx.update(|w, cx| {
arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Allow, cx);
w.render_frame(cx);
w.render_frame(cx);
let first = w.find(child_id(&root, "scene-0")).bounds();
w.click(child_id(&root, "scene-3"), cx);
w.render_frame(cx);
assert!((highlight(w).origin.x - first.origin.x).abs() <= px(0.5));
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(80));
cx.update(|w, cx| {
w.render_frame(cx);
let from = w.find(child_id(&root, "scene-0")).bounds();
let to = w.find(child_id(&root, "scene-3")).bounds();
let moving = highlight(w);
assert!(
moving.origin.x > from.origin.x && moving.origin.x < to.origin.x,
"中途帧必须在两标签之间:moving={moving:?}, from={from:?}, to={to:?}"
);
assert!(
moving.size.width >= from.size.width.min(to.size.width) - px(0.5)
&& moving.size.width <= from.size.width.max(to.size.width) + px(0.5),
"共享布局只能在两个单项宽度之间插值:{moving:?}, {from:?}, {to:?}"
);
w.click(child_id(&root, "scene-1"), cx);
w.render_frame(cx);
assert!((highlight(w).origin.x - moving.origin.x).abs() <= px(0.5));
});
cx.executor()
.advance_clock(std::time::Duration::from_secs(2));
cx.update(|w, cx| {
w.render_frame(cx);
let target = w.find(child_id(&root, "scene-1")).bounds();
assert!((highlight(w).origin.x - target.origin.x).abs() <= px(0.5));
assert!((highlight(w).size.width - target.size.width).abs() <= px(0.5));
});
}
#[gpui_kit::test]
fn page_indicator_hugs_selected_tab_across_widths_and_themes(cx: &mut TestAppContext) {
use arc_tokens::ActiveArcTheme as _;
setup(cx, ThemeMode::Light);
let (host, cx) = cx.add_window_view(|_, _| SizedHost {
kind: Kind::Page,
width: 960.0,
});
let root = ElementId::from("sized-page");
for mode in [ThemeMode::Light, ThemeMode::Dark, ThemeMode::Light] {
for width in [960.0, 320.0, 960.0] {
host.update(cx, |s, cx| {
s.width = width;
install_arc_theme(mode, Accent::Violet, cx);
cx.notify();
});
cx.update(|w, cx| {
w.render_frame(cx);
w.render_frame(cx);
});
for index in 0..4 {
let label = ["Overview", "Issues", "Updates", "Files"][index];
cx.update(|w, cx| {
w.click(child_id(&root, format!("tab-{label}")), cx);
w.render_frame(cx);
let tab = w.find(child_id(&root, format!("tab-{label}"))).bounds();
let accent = cx.arc_theme().colors.accent;
let mut lines = w
.painted_quads()
.into_iter()
.filter(|q| {
q.background == accent.into()
&& q.border_widths.top.as_f32() == 0.0
&& q.bounds.size.height.as_f32() / w.scale_factor() <= 3.0
})
.map(|q| q.bounds)
.collect::<Vec<_>>();
lines.dedup();
assert_eq!(lines.len(), 1, "只能有一枚指示线:{lines:?}");
let q = lines[0];
let scale = w.scale_factor();
let expected = gpui_kit::bounds(
gpui_kit::point(
px(f32::from(tab.origin.x) + 10.0),
px(f32::from(tab.origin.y) + f32::from(tab.size.height) - 2.0),
),
gpui_kit::size(px(f32::from(tab.size.width) - 20.0), px(2.0)),
);
for (actual, want) in [
(q.origin.x.as_f32() / scale, f32::from(expected.origin.x)),
(q.origin.y.as_f32() / scale, f32::from(expected.origin.y)),
(
q.size.width.as_f32() / scale,
f32::from(expected.size.width),
),
(
q.size.height.as_f32() / scale,
f32::from(expected.size.height),
),
] {
assert!(
(actual - want).abs() <= 0.5 / scale,
"指示线必须贴合激活 tab(左右内缩 10px):mode={mode:?}, width={width}, tab={label}, quad={q:?}, expected={expected:?}"
);
}
assert_eq!(q.size.height.as_f32(), 2.0 * scale);
});
}
}
}
}
#[gpui_kit::test]
fn empty_scene_actions_complete_and_scene_switch_cancels_old_work(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Dark);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Empty,
events,
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("action"), cx);
w.render_frame(cx);
assert_eq!(w.find(id("action")).label(), Some("Restore filters"));
w.click(id("scene-1"), cx);
w.render_frame(cx);
w.click(id("action"), cx);
w.render_frame(cx);
assert_eq!(w.find(id("action")).disabled(), Some(true));
w.click(id("scene-1"), cx);
w.render_frame(cx);
assert_eq!(
w.find(id("action")).disabled(),
Some(true),
"重选当前标签不能取消动作"
);
w.click(id("scene-2"), cx);
w.render_frame(cx);
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(1600));
cx.update(|w, cx| {
w.render_frame(cx);
assert_eq!(w.find(id("action")).label(), Some("Check for mail"));
});
assert_eq!(&*log.borrow(), &["Search", "Offline"]);
}
#[gpui_kit::test]
fn empty_inbox_recheck_preserves_completed_title_and_action_width(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Empty,
events: Rc::default(),
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("scene-2"), cx);
w.render_frame(cx);
let before = w.find(id("action")).bounds().size.width;
w.click(id("action"), cx);
w.render_frame(cx);
assert_eq!(w.find(id("action")).label(), Some("Check for mail"));
assert_eq!(w.find(id("action")).bounds().size.width, before);
assert_eq!(w.find(id("status")).label(), Some("All caught up"));
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(1200));
cx.update(|w, cx| {
w.render_frame(cx);
assert_eq!(w.find(id("status")).label(), Some("Still all caught up"));
let before = w.find(id("action")).bounds().size.width;
w.click(id("action"), cx);
w.render_frame(cx);
assert_eq!(w.find(id("status")).label(), Some("Still all caught up"));
assert_eq!(w.find(id("action")).label(), Some("Check again"));
assert_eq!(w.find(id("action")).bounds().size.width, before);
assert_eq!(w.find(id("action")).disabled(), Some(true));
});
}
#[gpui_kit::test]
fn page_condenses_on_scroll_and_expands_back_to_top(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Page,
events: Rc::default(),
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
assert!(w.try_find(id("compact")).is_none());
let body = w.find(id("body")).bounds();
w.scroll(
id("body"),
gpui_kit::ScrollDelta::Pixels(gpui_kit::point(px(0.0), px(-40.0))),
cx,
);
w.render_frame(cx);
w.render_frame(cx);
let condensed = w.find(id("body")).bounds();
assert!(
condensed.origin.y < body.origin.y,
"收紧后 intro 必须收起:before={body:?}, after={condensed:?}"
);
assert_eq!(
w.find(id("compact")).label(),
Some("Checkout redesign, back to top")
);
w.click(id("compact"), cx);
w.render_frame(cx);
w.render_frame(cx);
let expanded = w.find(id("body")).bounds();
assert!(
(expanded.origin.y - body.origin.y).abs() <= px(1.0),
"点击 compact 必须回到顶部展开:before={body:?}, after={expanded:?}"
);
assert!(w.try_find(id("compact")).is_none());
});
}
#[gpui_kit::test]
fn page_indicator_glides_between_tabs_and_settles(cx: &mut TestAppContext) {
fn line(w: &Window, cx: &App) -> gpui_kit::Bounds<gpui_kit::Pixels> {
use arc_tokens::ActiveArcTheme as _;
let accent = cx.arc_theme().colors.accent;
let q = w
.painted_quads()
.into_iter()
.find(|q| {
q.background == accent.into()
&& q.border_widths.top.as_f32() == 0.0
&& q.bounds.size.height.as_f32() / w.scale_factor() <= 3.0
})
.expect("必须绘制指示线");
let scale = w.scale_factor();
gpui_kit::bounds(
gpui_kit::point(
px(q.bounds.origin.x.as_f32() / scale),
px(q.bounds.origin.y.as_f32() / scale),
),
gpui_kit::size(
px(q.bounds.size.width.as_f32() / scale),
px(q.bounds.size.height.as_f32() / scale),
),
)
}
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| SizedHost {
kind: Kind::Page,
width: 960.0,
});
let root = ElementId::from("sized-page");
cx.update(|w, cx| {
arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Allow, cx);
w.render_frame(cx);
w.render_frame(cx);
let first = w.find(child_id(&root, "tab-Overview")).bounds();
w.click(child_id(&root, "tab-Files"), cx);
w.render_frame(cx);
let moving = line(w, cx);
assert!(
(moving.origin.x.as_f32() - (f32::from(first.origin.x) + 10.0)).abs() <= 0.5,
"滑行必须从当前 tab 开始:{moving:?}, first={first:?}"
);
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(80));
cx.update(|w, cx| {
w.render_frame(cx);
let from = w.find(child_id(&root, "tab-Overview")).bounds();
let to = w.find(child_id(&root, "tab-Files")).bounds();
let moving = line(w, cx);
assert!(
moving.origin.x.as_f32() > f32::from(from.origin.x)
&& moving.origin.x.as_f32() + moving.size.width.as_f32()
< f32::from(to.origin.x) + f32::from(to.size.width),
"中途帧必须在两 tab 之间:moving={moving:?}, from={from:?}, to={to:?}"
);
});
cx.executor()
.advance_clock(std::time::Duration::from_secs(2));
cx.update(|w, cx| {
w.render_frame(cx);
let to = w.find(child_id(&root, "tab-Files")).bounds();
let settled = line(w, cx);
assert!((settled.origin.x.as_f32() - (f32::from(to.origin.x) + 10.0)).abs() <= 0.5);
assert!(
(settled.size.width.as_f32() - (f32::from(to.size.width) - 20.0)).abs() <= 0.5,
"收敛宽度必须等于 Files tab 宽度减左右 20px:{settled:?}, {to:?}"
);
});
}
#[gpui_kit::test]
fn otp_focus_ring_matches_one_measured_slot_after_focus_and_resize(cx: &mut TestAppContext) {
use arc_tokens::ActiveArcTheme as _;
setup(cx, ThemeMode::Light);
let (host, cx) = cx.add_window_view(|_, _| SizedHost {
kind: Kind::Otp,
width: 960.0,
});
let otp_id = ElementId::from("sized-otp");
for mode in [ThemeMode::Light, ThemeMode::Dark, ThemeMode::Light] {
for width in [960.0, 240.0, 280.0, 960.0] {
host.update(cx, |s, cx| {
s.width = width;
install_arc_theme(mode, Accent::Violet, cx);
cx.notify();
});
cx.update(|w, cx| w.render_frame(cx));
for index in [0, 3, 5, 1] {
cx.update(|w, cx| {
let slot_id = child_id(&otp_id, format!("slot-{index}"));
w.click(slot_id.clone(), cx);
w.render_frame(cx);
let slot = w.find(slot_id).bounds();
let color = cx.arc_theme().colors.accent_strong;
let mut rings = w
.painted_quads()
.into_iter()
.filter(|q| q.border_color == color && q.border_widths.top.as_f32() > 0.0)
.map(|q| q.bounds)
.collect::<Vec<_>>();
rings.dedup();
assert_eq!(rings.len(), 1, "整组只能绘制一枚焦点环:{rings:?}");
let ring = rings[0];
let scale = w.scale_factor();
for (actual, expected) in [
(ring.origin.x.as_f32() / scale, f32::from(slot.origin.x)),
(ring.origin.y.as_f32() / scale, f32::from(slot.origin.y)),
(ring.size.width.as_f32() / scale, f32::from(slot.size.width)),
(
ring.size.height.as_f32() / scale,
f32::from(slot.size.height),
),
] {
assert!(
(actual - expected).abs() <= 0.5 / scale,
"焦点环必须贴合单槽:{ring:?}, {slot:?}"
);
}
assert!(slot.size.width <= px(42.0));
assert_eq!(slot.size.height, px(48.0));
});
}
}
}
}
#[gpui_kit::test]
fn otp_ring_glides_only_within_group_and_resizes_without_lag(cx: &mut TestAppContext) {
use arc_tokens::ActiveArcTheme as _;
fn ring(w: &Window, cx: &App) -> gpui_kit::Bounds<gpui_kit::Pixels> {
let color = cx.arc_theme().colors.accent_strong;
let q = w
.painted_quads()
.into_iter()
.find(|q| {
q.border_color.h == color.h
&& q.border_color.s == color.s
&& q.border_color.l == color.l
&& q.border_color.a > 0.0
&& q.border_widths.top.as_f32() > 0.0
})
.expect("可见焦点环");
let scale = w.scale_factor();
gpui_kit::bounds(
gpui_kit::point(
px(q.bounds.origin.x.as_f32() / scale),
px(q.bounds.origin.y.as_f32() / scale),
),
gpui_kit::size(
px(q.bounds.size.width.as_f32() / scale),
px(q.bounds.size.height.as_f32() / scale),
),
)
}
setup(cx, ThemeMode::Dark);
let (host, cx) = cx.add_window_view(|_, _| SizedHost {
kind: Kind::Otp,
width: 960.0,
});
let otp_id = ElementId::from("sized-otp");
cx.update(|w, cx| {
arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Allow, cx);
w.render_frame(cx);
w.click(child_id(&otp_id, "slot-4"), cx);
w.render_frame(cx);
});
cx.executor()
.advance_clock(std::time::Duration::from_secs(1));
cx.update(|w, cx| {
w.render_frame(cx);
let slot = w.find(child_id(&otp_id, "slot-4")).bounds();
assert_eq!(ring(w, cx), slot, "从组外进入不应从首槽滑来");
w.click(child_id(&otp_id, "slot-1"), cx);
w.render_frame(cx);
assert_eq!(ring(w, cx), slot, "组内换焦点从原位置起步");
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(80));
cx.update(|w, cx| {
w.render_frame(cx);
let from = w.find(child_id(&otp_id, "slot-4")).bounds();
let to = w.find(child_id(&otp_id, "slot-1")).bounds();
let moving = ring(w, cx);
assert!(moving.origin.x < from.origin.x && moving.origin.x > to.origin.x);
assert_eq!(moving.size.width, to.size.width, "滑行时也不能跨槽变宽");
});
host.update(cx, |s, cx| {
s.width = 240.0;
cx.notify();
});
cx.update(|w, cx| {
w.render_frame(cx);
let slot = w.find(child_id(&otp_id, "slot-1")).bounds();
assert!((ring(w, cx).origin.x - slot.origin.x).abs() <= px(0.25));
assert!((ring(w, cx).size.width - slot.size.width).abs() <= px(0.5));
w.blur(cx);
w.render_frame(cx);
assert!(
(ring(w, cx).origin.x - slot.origin.x).abs() <= px(0.25),
"离开组时原地淡出"
);
});
cx.executor()
.advance_clock(std::time::Duration::from_secs(1));
cx.update(|w, cx| {
w.render_frame(cx);
let color = cx.arc_theme().colors.accent_strong;
assert!(!w.painted_quads().iter().any(|q| q.border_color == color));
});
}
#[gpui_kit::test]
fn otp_paste_navigation_and_backspace_preserve_value(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Otp,
events,
disabled: false,
});
cx.update(|w, cx| w.render_frame(cx));
cx.update(|w, cx| {
w.click(id("slot-0"), cx);
cx.write_to_clipboard(gpui_kit::ClipboardItem::new_string("12x3".to_owned()));
w.press("ctrl-v", cx);
});
assert_eq!(log.borrow().last().map(String::as_str), Some("123"));
cx.update(|w, cx| {
w.render_frame(cx);
w.press("backspace", cx);
});
assert_eq!(log.borrow().last().map(String::as_str), Some("12"));
cx.update(|w, cx| {
w.render_frame(cx);
w.press("left", cx);
w.input("9", cx);
});
assert!(log.borrow().last().is_some_and(|v| v.contains('9')));
}
#[gpui_kit::test]
fn otp_typing_replaces_filled_slots_and_disabled_rejects_input(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Dark);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (host, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Otp,
events,
disabled: false,
});
cx.update(|w, cx| w.render_frame(cx));
cx.update(|w, cx| w.click(id("slot-0"), cx));
for c in ["1", "2", "3", "4"] {
cx.update(|w, cx| w.input(c, cx));
}
assert_eq!(log.borrow().last().map(String::as_str), Some("1234"));
cx.update(|w, cx| {
w.press("left", cx);
w.input("9", cx);
});
assert_eq!(log.borrow().last().map(String::as_str), Some("1294"));
host.update(cx, |s, cx| {
s.disabled = true;
cx.notify();
});
let before = log.borrow().len();
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("slot-1"), cx);
w.input("8", cx);
w.press("backspace", cx);
});
assert_eq!(log.borrow().len(), before);
}
#[gpui_kit::test]
fn otp_delete_clears_current_slot_and_ignores_empty_slot(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Otp,
events,
disabled: false,
});
cx.update(|w, cx| w.render_frame(cx));
cx.update(|w, cx| w.click(id("slot-0"), cx));
for c in ["1", "2", "3"] {
cx.update(|w, cx| w.input(c, cx));
}
assert_eq!(log.borrow().last().map(String::as_str), Some("123"));
cx.update(|w, cx| {
w.render_frame(cx);
w.press("delete", cx);
});
assert_eq!(log.borrow().last().map(String::as_str), Some("123"));
cx.update(|w, cx| {
w.render_frame(cx);
w.press("left", cx);
w.press("delete", cx);
});
assert_eq!(log.borrow().last().map(String::as_str), Some("12"));
cx.update(|w, cx| {
w.render_frame(cx);
w.press("delete", cx);
});
assert_eq!(log.borrow().last().map(String::as_str), Some("12"));
}
#[gpui_kit::test]
fn upload_remove_emits_controlled_list_and_disabled_blocks_action(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Dark);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (host, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Upload,
events,
disabled: true,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(nested("file-a", "remove"), cx);
});
assert!(log.borrow().is_empty());
host.update(cx, |s, cx| {
s.disabled = false;
cx.notify();
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(nested("file-a", "remove"), cx);
});
assert_eq!(&*log.borrow(), &["0"]);
}
#[gpui_kit::test]
fn notification_trigger_and_panel_geometry_match_upstream(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Notification,
events: Rc::default(),
disabled: false,
});
for mode in [ThemeMode::Light, ThemeMode::Dark, ThemeMode::Light] {
cx.update(|w, cx| {
install_arc_theme(mode, Accent::Violet, cx);
w.render_frame(cx);
let trigger = w.find(id("trigger")).bounds();
assert_eq!(trigger.size, gpui_kit::size(px(42.0), px(42.0)));
let bell = w.find(id("bell"));
assert!(bell.visible());
assert_eq!(bell.bounds().size, gpui_kit::size(px(19.0), px(19.0)));
assert_eq!(bell.bounds().center(), trigger.center());
assert!(w.try_find(id("badge")).is_some());
assert!(w.try_find(id("panel")).is_none());
w.click(id("trigger"), cx);
w.render_frame(cx);
w.render_frame(cx);
let panel = w.find(id("panel")).bounds();
assert_eq!(panel.size.width, px(424.0));
assert!(panel.size.height > px(200.0), "panel collapsed: {panel:?}");
assert!(panel.size.height <= px(590.0));
assert!(panel.origin.y >= trigger.origin.y + trigger.size.height);
w.click(nested("item-a", "toggle"), cx);
w.render_frame(cx);
let main = w.find(nested("item-a", "main")).bounds();
let icon = w.find(nested("item-a", "event-icon"));
assert!(icon.visible());
assert_eq!(icon.bounds().size, gpui_kit::size(px(18.0), px(18.0)));
assert!(main.contains(&icon.bounds().center()));
let details = w.find(nested("item-a", "details")).bounds();
assert!(
details.origin.y >= main.origin.y + main.size.height,
"details must be below, not squeezed beside the row: {main:?}, {details:?}"
);
assert!(main.size.width > px(300.0));
w.click(nested("item-a", "toggle"), cx);
w.render_frame(cx);
w.click(id("close"), cx);
w.render_frame(cx);
});
}
}
struct NotificationOverflowHost;
impl Render for NotificationOverflowHost {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div().w(px(960.0)).p(Spacing::S6).child(
NotificationCenter::new("block").open(true).notifications((0..30).map(|i|
NotificationItem::new(format!("n{i}"), "A very long notification title that must stay inside the panel", "12m")
.description("A long preview that should truncate instead of pushing the timestamp outside the panel.")
.read(i % 2 == 0))))
}
}
#[gpui_kit::test]
fn notification_long_list_scrolls_without_expanding_the_panel(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| NotificationOverflowHost);
cx.update(|w, cx| {
for _ in 0..3 {
w.render_frame(cx);
}
let panel = w.find(id("panel")).bounds();
assert_eq!(panel.size.width, px(424.0));
assert!(panel.size.height <= px(590.0));
assert!(panel.origin.x >= px(0.0));
let list = w.find(id("list")).bounds();
assert!(list.size.height < panel.size.height);
let first = w.find(nested("item-n0", "main")).bounds();
assert!(first.origin.x >= panel.origin.x);
assert!(first.origin.x + first.size.width <= panel.origin.x + panel.size.width);
assert!(w.try_find(id("badge")).is_some());
let before = first.origin.y;
w.scroll(
id("list"),
gpui_kit::ScrollDelta::Pixels(gpui_kit::point(px(0.0), px(-180.0))),
cx,
);
w.render_frame(cx);
assert!(w.find(nested("item-n0", "main")).bounds().origin.y < before);
});
cx.simulate_resize(gpui_kit::size(px(360.0), px(640.0)));
cx.update(|w, cx| {
for _ in 0..3 {
w.render_frame(cx);
}
let panel = w.find(id("panel")).bounds();
assert!(panel.size.width <= px(336.0));
assert!(panel.origin.x >= px(0.0));
assert!(panel.origin.x + panel.size.width <= px(360.0));
assert!(w.find(id("mark-all")).bounds().size.width <= px(32.0));
});
}
#[gpui_kit::test]
fn notification_badge_clears_and_escape_reopens_the_same_inbox(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Dark);
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Notification,
events: Rc::default(),
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("trigger"), cx);
w.render_frame(cx);
w.click(id("mark-all"), cx);
w.render_frame(cx);
assert!(w.try_find(id("badge")).is_none());
w.click(id("unread"), cx);
w.render_frame(cx);
assert!(w.try_find(id("empty")).is_some());
w.press("escape", cx);
w.render_frame(cx);
assert!(w.try_find(id("panel")).is_none());
w.click(id("trigger"), cx);
w.render_frame(cx);
w.click(id("view-all"), cx);
w.render_frame(cx);
assert!(w.try_find(nested("item-a", "toggle")).is_some());
assert!(w.try_find(nested("item-b", "toggle")).is_some());
w.click(id("clear-read"), cx);
w.render_frame(cx);
assert!(w.try_find(id("empty")).is_some());
assert!(
w.find(id("bell")).visible(),
"clearing the list must not remove the bell"
);
w.click(id("close"), cx);
w.render_frame(cx);
assert!(
w.find(id("bell")).visible(),
"closing the empty panel must keep the bell"
);
assert!(w.try_find(id("badge")).is_none());
});
}
#[gpui_kit::test]
fn notifications_individual_read_and_dismiss_are_reachable(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Dark);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Notification,
events,
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("trigger"), cx);
w.click(nested("item-a", "toggle"), cx);
w.render_frame(cx);
w.click(nested("item-a", "read"), cx);
w.render_frame(cx);
assert_eq!(
w.find(nested("item-a", "read")).label(),
Some("Mark unread")
);
w.click(nested("item-a", "dismiss"), cx);
w.render_frame(cx);
assert!(w.try_find(nested("item-a", "toggle")).is_none());
w.click(id("close"), cx);
w.render_frame(cx);
assert!(w.try_find(id("panel")).is_none());
});
assert_eq!(&*log.borrow(), &["a:true"]);
}
#[gpui_kit::test]
fn notification_panel_fades_out_before_the_layer_leaves(cx: &mut TestAppContext) {
cx.update(|cx: &mut App| {
gpui_kit::init(cx);
install_arc_theme(ThemeMode::Light, Accent::Violet, cx);
arc_motion::set_reduced_motion(arc_motion::ReducedMotionChoice::Allow, cx);
});
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Notification,
events: Rc::default(),
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("trigger"), cx);
w.render_frame(cx);
assert!(w.try_find(id("panel")).is_some());
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(300));
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("close"), cx);
w.render_frame(cx);
assert!(
w.try_find(id("panel")).is_some(),
"panel should stay mounted while its exit curve runs"
);
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(240));
cx.update(|w, cx| {
w.render_frame(cx);
w.render_frame(cx);
assert!(w.try_find(id("panel")).is_none());
});
}
#[gpui_kit::test]
fn blocks_rapid_kind_switches_stay_finite(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (host, cx) = cx.add_window_view(|_, _| SizedHost {
kind: Kind::Otp,
width: 960.0,
});
for cycle in 0..6 {
for (kind, mode) in [
(Kind::Command, ThemeMode::Dark),
(Kind::Notification, ThemeMode::Light),
(Kind::Changelog, ThemeMode::Dark),
(Kind::Page, ThemeMode::Light),
(Kind::Comparison, ThemeMode::Dark),
(Kind::Empty, ThemeMode::Light),
(Kind::Upload, ThemeMode::Dark),
(Kind::Otp, ThemeMode::Light),
] {
host.update(cx, |s, cx| {
s.kind = kind;
install_arc_theme(mode, Accent::Violet, cx);
cx.notify();
});
cx.update(|w, cx| {
w.render_frame(cx);
let elements = w.elements();
for element in elements {
let bounds = element.bounds();
assert!(
f32::from(bounds.size.width).is_finite()
&& f32::from(bounds.size.height).is_finite(),
"cycle {cycle} {kind:?}: {bounds:?}"
);
}
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(40));
cx.update(|w, cx| w.render_frame(cx));
}
}
}
#[gpui_kit::test]
fn command_palette_survives_rapid_query_flip_flop_and_reopen(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Command,
events,
disabled: false,
});
cx.update(|w, cx| w.render_frame(cx));
for cycle in 0..6 {
cx.update(|w, cx| {
w.click(id("input"), cx);
w.input(if cycle % 2 == 0 { "Open" } else { "Settings" }, cx);
w.render_frame(cx);
let visible = w.elements().len();
assert!(visible > 1, "cycle {cycle} lost all elements");
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(30));
}
cx.update(|w, cx| {
w.click(id("clear"), cx);
w.render_frame(cx);
assert!(w.try_find(id("clear")).is_none());
w.input("project", cx);
w.render_frame(cx);
});
cx.update(|w, cx| {
w.press("enter", cx);
w.render_frame(cx);
});
assert_eq!(&*log.borrow(), &["a"]);
}
#[gpui_kit::test]
fn page_follow_share_rapid_toggles_stay_consistent(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Page,
events: Rc::default(),
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
for _ in 0..5 {
w.click(id("follow"), cx);
}
w.render_frame(cx);
assert_eq!(w.find(id("follow")).label(), Some("Unfollow project"));
w.click(id("share"), cx);
w.render_frame(cx);
assert_eq!(w.find(id("share")).disabled(), Some(true));
});
cx.update(|w, cx| {
w.click(id("follow"), cx);
w.click(id("follow"), cx);
w.click(id("share"), cx);
w.render_frame(cx);
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(800));
cx.update(|w, cx| {
w.render_frame(cx);
assert_eq!(w.find(id("share")).label(), Some("Shared"));
assert_eq!(w.find(id("follow")).label(), Some("Unfollow project"));
});
cx.executor()
.advance_clock(std::time::Duration::from_millis(1900));
cx.update(|w, cx| {
w.render_frame(cx);
assert_eq!(w.find(id("share")).label(), Some("Share update"));
});
}
#[gpui_kit::test]
fn notification_and_upload_rapid_flip_flop_keeps_counts_consistent(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Notification,
events,
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("trigger"), cx);
w.render_frame(cx);
w.click(nested("item-a", "toggle"), cx);
w.render_frame(cx);
for _ in 0..5 {
w.click(nested("item-a", "read"), cx);
w.render_frame(cx);
}
assert_eq!(
w.find(nested("item-a", "read")).label(),
Some("Mark unread")
);
assert_eq!(
&*log.borrow(),
&["a:true", "a:false", "a:true", "a:false", "a:true"]
);
});
}
#[gpui_kit::test]
fn notifications_open_read_filter_and_clear(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (_, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Notification,
events,
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(id("trigger"), cx);
w.render_frame(cx);
w.click(id("mark-all"), cx);
w.render_frame(cx);
w.click(id("unread"), cx);
w.render_frame(cx);
w.click(id("all"), cx);
w.render_frame(cx);
w.click(id("clear-read"), cx);
w.render_frame(cx);
assert!(w.try_find(id("mark-all")).is_none());
});
assert_eq!(&*log.borrow(), &["a:true"]);
}
#[gpui_kit::test]
fn upload_done_label_waits_for_the_progress_spring(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let (_, cx) = cx.add_window_view(|_, _| InstantDoneHost);
cx.update(|w, cx| {
w.render_frame(cx);
assert!(
w.try_find(child_id(
&ElementId::Name("instant-upload".into()),
"dropzone"
))
.is_some()
);
});
}
#[gpui_kit::test]
fn upload_row_visibility_follows_the_controlled_list(cx: &mut TestAppContext) {
setup(cx, ThemeMode::Light);
let events = Rc::new(RefCell::new(vec![]));
let log = events.clone();
let (host, cx) = cx.add_window_view(|_, _| Host {
kind: Kind::Upload,
events,
disabled: false,
});
cx.update(|w, cx| {
w.render_frame(cx);
assert!(w.try_find(nested("file-a", "remove")).is_some());
assert!(w.try_find(nested("file-a", "status-slot")).is_some());
w.click(nested("file-a", "remove"), cx);
w.render_frame(cx);
});
assert_eq!(&*log.borrow(), &["0"]);
cx.update(|w, cx| {
w.render_frame(cx);
assert!(w.try_find(nested("file-a", "remove")).is_some());
});
cx.update(|w, _| {
assert_eq!(w.find(id("dropzone")).role(), Some(gpui_kit::Role::Button));
});
host.update(cx, |s, cx| {
s.disabled = true;
cx.notify();
});
cx.update(|w, cx| {
w.render_frame(cx);
w.click(nested("file-a", "remove"), cx);
w.render_frame(cx);
});
assert_eq!(&*log.borrow(), &["0"]);
}