use std::{
collections::HashMap,
sync::{
Arc, Mutex,
atomic::{AtomicUsize, Ordering},
},
time::{Duration, Instant},
};
use crossbeam_channel::{Receiver, Sender};
use nsh_base::dialog;
use nsh_core::{
ApproxTextEngine, AttributedText, Command, EventKind, EventPayload, MountBatch, MountItem,
Points, ROOT_ID, Size, StatusBarSetting, StatusBarStyle, TextEngine, TextLayout,
desktop::{
DesktopCommand, Display, DisplayId, WindowEvent, WindowMode, WindowPlacement, WindowState,
},
element::{AssetFile, AssetKind, Registry, Root, desktop::window},
geometry::{Bounds, point, size},
platform::{
Idiom, Languages, Lifecycle, Locale, LocaleSettings, Os, TemperatureUnit, TimeZone,
},
prelude::*,
};
use nsh_engine::{App, Assets, Engine, EngineConfig, Environment, MountSink};
struct FrameSink {
batches: Sender<MountBatch>,
frames: Arc<AtomicUsize>,
}
impl MountSink for FrameSink {
fn submit(&self, batch: MountBatch) {
let _ = self.batches.send(batch);
}
fn request_frame(&self) {
self.frames.fetch_add(1, Ordering::SeqCst);
}
}
fn config(app: App) -> EngineConfig {
EngineConfig {
app,
viewport: Size::new(px(390.0), px(844.0)),
trace: false,
content_culling: false,
network: None,
websocket: None,
refuse_connections: None,
filesystem: None,
assets: None,
environment: Environment::default(),
header_heights: (nsh_core::Points(44.0), nsh_core::Points(96.0)),
limits: nsh_engine::Limits::default(),
ui_sets: Vec::new(),
runtime: None,
support_dir: None,
cache: nsh_engine::CacheOptions::off(),
storage: None,
clock: Default::default(),
context: Default::default(),
pinned: Default::default(),
clipboard: None,
host: Default::default(),
picker: None,
audio: None,
windows: None,
login_item: None,
data_dir: None,
}
}
fn start<C: Component>(environment: Environment) -> (Engine, Receiver<MountBatch>, Arc<AtomicUsize>)
where
C::Props: Default,
{
let (tx, rx) = crossbeam_channel::unbounded();
let frames = Arc::new(AtomicUsize::new(0));
let config = EngineConfig {
environment,
..config(App::Rust(Root::of::<C>()))
};
let sink = FrameSink {
batches: tx,
frames: frames.clone(),
};
let engine = Engine::start(config, || ApproxTextEngine, sink).unwrap();
(engine, rx, frames)
}
fn until(rx: &Receiver<MountBatch>, until: &str) -> (Vec<String>, Vec<bool>, Option<u32>) {
let deadline = Instant::now() + Duration::from_secs(5);
let (mut texts, mut directions, mut host) = (Vec::new(), Vec::new(), None);
while let Ok(batch) = rx.recv_deadline(deadline) {
for item in &batch.items {
match item {
MountItem::Text { text, .. } => texts.push(text.text.clone()),
MountItem::LayoutDirection { rtl } => directions.push(*rtl),
MountItem::Insert {
parent: nsh_core::ROOT_ID,
child,
..
} => host = Some(*child),
_ => {}
}
}
if texts.iter().any(|t| t == until) {
break;
}
}
(texts, directions, host)
}
struct Fonts(ApproxTextEngine, Arc<Mutex<Vec<Vec<u8>>>>);
impl TextEngine for Fonts {
type Prepared = <ApproxTextEngine as TextEngine>::Prepared;
fn prepare(&self, text: &AttributedText) -> Self::Prepared {
self.0.prepare(text)
}
fn measure(&self, prepared: &Self::Prepared, max_width: Option<Points>) -> Size<Points> {
self.0.measure(prepared, max_width)
}
fn layout(&self, prepared: &Self::Prepared, size: Size<Points>) -> TextLayout {
self.0.layout(prepared, size)
}
fn register_font(&self, data: &[u8]) -> Result<(), String> {
self.1.lock().unwrap().push(data.to_vec());
Ok(())
}
}
static FILES: &[AssetFile] = &[
AssetFile {
uri: "asset:/@app/fonts/Script.ttf",
source: Some("/nowhere/fonts/Script.ttf"),
kind: AssetKind::Font,
},
AssetFile {
uri: "asset:/@app/logo.png",
source: Some("/nowhere/logo.png"),
kind: AssetKind::Image,
},
];
struct CrateAssets(Arc<Mutex<Vec<String>>>);
impl Assets for CrateAssets {
fn read(&self, uri: &str) -> Result<Vec<u8>, String> {
self.0.lock().unwrap().push(uri.to_owned());
Ok(uri.as_bytes().to_vec())
}
}
#[component]
fn Label() -> impl IntoElement {
view().child(text("ready").font_family("Script"))
}
#[test]
fn the_fonts_ui_sets_ship_are_registered_before_the_first_render() {
let (tx, rx) = crossbeam_channel::unbounded();
let read = Arc::new(Mutex::new(Vec::new()));
let fonts = Arc::new(Mutex::new(Vec::new()));
let mut set = Registry::ui_set("app/native");
set.assets(FILES);
let config = EngineConfig {
assets: Some(Arc::new(CrateAssets(read.clone()))),
ui_sets: vec![set],
..config(App::Rust(Root::of::<Label>()))
};
let registered = fonts.clone();
let _engine = Engine::start(
config,
move || Fonts(ApproxTextEngine, registered),
FrameSink {
batches: tx,
frames: Arc::default(),
},
)
.unwrap();
until(&rx, "ready");
assert_eq!(*read.lock().unwrap(), ["asset:/@app/fonts/Script.ttf"]);
assert_eq!(
*fonts.lock().unwrap(),
[b"asset:/@app/fonts/Script.ttf".to_vec()]
);
}
#[test]
fn a_font_the_host_registers_is_registered_before_the_first_render() {
let (tx, rx) = crossbeam_channel::unbounded();
let fonts = Arc::new(Mutex::new(Vec::new()));
let registered = fonts.clone();
let _engine = Engine::start_with(
config(App::Rust(Root::of::<Label>())),
move || Fonts(ApproxTextEngine, registered),
FrameSink {
batches: tx,
frames: Arc::default(),
},
|control| control.register_font(b"host-font".to_vec()),
)
.unwrap();
until(&rx, "ready");
assert_eq!(*fonts.lock().unwrap(), [b"host-font".to_vec()]);
}
#[component]
fn Frames() -> impl IntoElement {
let frames = signal(0);
let called = signal(false);
let start = Instant::now();
request_animation_frame(move |at| called.set(at >= start));
spawn(async move {
for _ in 0..3 {
next_frame().await;
frames.update(|f| *f += 1);
}
});
view().child(text(move || {
format!("{} frames, called {}", frames.get(), called.get())
}))
}
#[test]
fn tasks_wait_for_display_frames() {
let (engine, rx, frames) = start::<Frames>(Environment::default());
until(&rx, "0 frames, called false");
let deadline = Instant::now() + Duration::from_secs(5);
while frames.load(Ordering::SeqCst) == 0 && Instant::now() < deadline {
std::thread::sleep(Duration::from_millis(5));
}
assert_eq!(frames.load(Ordering::SeqCst), 1);
for n in 1..=3 {
engine.control().frame();
let (texts, ..) = until(&rx, &format!("{n} frames, called true"));
assert_eq!(
texts.last().map(String::as_str),
Some(format!("{n} frames, called true").as_str())
);
}
std::thread::sleep(Duration::from_millis(100));
assert_eq!(frames.load(Ordering::SeqCst), 3);
}
enum ToMain {
Batch(MountBatch),
Task(nsh_engine::MainThreadTask),
}
struct MainThreadSink(Sender<ToMain>);
impl MountSink for MainThreadSink {
fn submit(&self, batch: MountBatch) {
let _ = self.0.send(ToMain::Batch(batch));
}
fn run_on_main_thread(&self, task: nsh_engine::MainThreadTask) {
let _ = self.0.send(ToMain::Task(task));
}
}
#[component]
fn OnMain() -> impl IntoElement {
let shown = signal(String::from("asking"));
spawn(async move {
shown.set("calling".into());
let name = run_on_main_thread(|| std::thread::current().name().map(str::to_owned))
.await
.ok()
.flatten()
.unwrap_or_default();
let failed = run_on_main_thread(|| -> u8 { panic!("no AppKit here") }).await;
shown.set(format!("{name} {}", failed.is_err()));
});
text(move || shown.get())
}
#[test]
fn functions_run_on_the_platforms_main_thread_and_answer_the_engine() {
let (tx, rx) = crossbeam_channel::unbounded();
let (main_tx, main_rx) = crossbeam_channel::unbounded::<ToMain>();
let order = Arc::new(Mutex::new(Vec::new()));
let seen = order.clone();
std::thread::Builder::new()
.name("ui".into())
.spawn(move || {
for message in main_rx {
match message {
ToMain::Batch(batch) => {
for item in &batch.items {
if let MountItem::Text { text, .. } = item {
seen.lock().unwrap().push(text.text.clone());
}
}
let _ = tx.send(batch);
}
ToMain::Task(task) => {
seen.lock().unwrap().push("task".into());
task.run();
}
}
}
})
.unwrap();
let engine = Engine::start(
config(App::Rust(Root::of::<OnMain>())),
|| ApproxTextEngine,
MainThreadSink(main_tx),
)
.unwrap();
let (texts, ..) = until(&rx, "ui true");
assert_eq!(texts.last().map(String::as_str), Some("ui true"));
let order = order.lock().unwrap();
let calling = order.iter().position(|t| t == "calling").unwrap();
let task = order.iter().position(|t| t == "task").unwrap();
assert!(calling < task, "{order:?}");
drop(engine);
}
#[component]
fn Direction() -> impl IntoElement {
view().flex_1().child(text("direction"))
}
#[test]
fn the_layout_direction_follows_the_language() {
let (engine, rx, _) = start::<Direction>(Environment::default());
let (_, directions, _) = until(&rx, "direction");
assert_eq!(directions, [false]);
let directions_after = |environment: Environment| {
engine.control().set_environment(environment);
let deadline = Instant::now() + Duration::from_secs(5);
while let Ok(batch) = rx.recv_deadline(deadline) {
let found = batch.items.iter().find_map(|item| match item {
MountItem::LayoutDirection { rtl } => Some(*rtl),
_ => None,
});
if found.is_some() {
return found;
}
}
None
};
let rtl = Environment {
rtl: true,
..Environment::default()
};
assert_eq!(directions_after(rtl), Some(true));
assert_eq!(directions_after(Environment::default()), Some(false));
}
#[component]
fn Device() -> impl IntoElement {
view().child(text(|| {
let (major, minor) = os_version();
format!(
"{} {major}.{minor} {:?} ×{} {} {}",
os().name(),
idiom(),
pixel_ratio(),
round_to_nearest_pixel(px(10.3)).0,
locale(),
)
}))
}
#[test]
fn components_read_the_pixel_ratio_the_device_and_the_locale() {
let environment = Environment {
pixel_ratio: 3.0,
os: Os::Android,
os_version: (34, 0),
idiom: Idiom::Pad,
locale: Locale::parse("de_DE").unwrap(),
..Environment::default()
};
let (engine, rx, _) = start::<Device>(environment);
let shown = "android 34.0 Pad ×3 10.333333 de-DE";
let (texts, ..) = until(&rx, shown);
assert!(texts.iter().any(|t| t == shown), "{texts:?}");
engine.control().set_environment(Environment {
pixel_ratio: 2.0,
..environment
});
let shown = "android 34.0 Pad ×2 10.5 de-DE";
let (texts, ..) = until(&rx, shown);
assert!(texts.iter().any(|t| t == shown), "{texts:?}");
engine.control().set_environment(Environment {
pixel_ratio: 2.0,
locale: Locale::parse("zh-Hans_CN").unwrap(),
..environment
});
let shown = "android 34.0 Pad ×2 10.5 zh-Hans-CN";
let (texts, ..) = until(&rx, shown);
assert!(texts.iter().any(|t| t == shown), "{texts:?}");
}
#[component]
fn InUse() -> impl IntoElement {
let runs = signal(0);
effect(move || {
let _ = lifecycle();
runs.update(|r| *r += 1);
});
view().child(text(move || {
format!("{} after {} runs", lifecycle().name(), runs.get())
}))
}
#[test]
fn components_follow_whether_the_app_is_in_use() {
let (engine, rx, _) = start::<InUse>(Environment::default());
until(&rx, "active after 1 runs");
for (state, shown) in [
(Lifecycle::Inactive, "inactive after 2 runs"),
(Lifecycle::Background, "background after 3 runs"),
(Lifecycle::Active, "active after 4 runs"),
] {
engine.control().set_environment(Environment {
lifecycle: state,
..Environment::default()
});
let (texts, ..) = until(&rx, shown);
assert_eq!(texts.last().map(|t| t.as_ref()), Some(shown));
}
engine.control().set_environment(Environment {
font_scale: 1.5,
..Environment::default()
});
std::thread::sleep(Duration::from_millis(100));
let texts: Vec<_> = rx
.try_iter()
.flat_map(|batch| batch.items)
.filter_map(|item| match item {
MountItem::Text { text, .. } => Some(text.text.clone()),
_ => None,
})
.collect();
assert!(
texts.iter().all(|t| t == "active after 4 runs"),
"{texts:?}"
);
}
#[component]
fn Downloaded() -> impl IntoElement {
let family = signal(String::from("none"));
spawn(async move {
let data =
include_bytes!("../../../../tests/compat/cases/layout/fonts/Pacifico-Regular.woff2");
let shown = match register_font(data.to_vec()).await {
Ok(name) => name,
Err(e) => format!("failed: {e}"),
};
family.set(shown);
});
view().child(text(move || format!("Family: {}", family.get())))
}
#[test]
fn a_component_registers_a_font_while_the_app_runs() {
let (_engine, rx, _) = start::<Downloaded>(Environment::default());
until(&rx, "Family: Pacifico");
}
#[component]
fn Greeting() -> impl IntoElement {
view().child(text(move || {
let languages = locale_settings().languages;
let words = languages
.iter()
.find_map(|l| match l.as_str().split('-').next() {
Some("de") => Some("Willkommen"),
Some("fr") => Some("Bienvenue"),
_ => None,
})
.unwrap_or("Welcome");
format!("{words} ({})", languages.as_slice().len())
}))
}
#[test]
fn components_follow_the_preferred_languages() {
let (engine, rx, _) = start::<Greeting>(Environment::default());
until(&rx, "Welcome (1)");
for (tags, shown) in [
(&["ja-JP", "fr-FR", "de-DE"][..], "Bienvenue (3)"),
(&["de_AT", "en-US"][..], "Willkommen (2)"),
] {
let mut env = Environment::default();
env.locale_settings.languages = Languages::parse(tags.iter().copied()).unwrap();
engine.control().set_environment(env);
let (texts, ..) = until(&rx, shown);
assert_eq!(texts.last().map(|t| t.as_ref()), Some(shown));
}
}
#[component]
fn Forecast() -> impl IntoElement {
let clock_runs = signal(0);
let hour_cycle = computed(|| locale_settings().hour_cycle);
effect(move || {
let _ = hour_cycle.get();
clock_runs.update(|r| *r += 1);
});
view().child(text(move || {
let settings = locale_settings();
let time = if settings.hour_cycle.twelve_hour() {
"3 PM"
} else {
"15:00"
};
let temperature = match settings.temperature_unit {
TemperatureUnit::Celsius => "20 °C",
TemperatureUnit::Fahrenheit => "68 °F",
};
format!(
"{time}, {temperature}, weeks from {}, {} units, clock ran {}",
settings.first_day_of_week.number(),
settings.measurement_system.name(),
clock_runs.get()
)
}))
}
#[test]
fn components_follow_the_regional_preferences() {
let (engine, rx, _) = start::<Forecast>(Environment::default());
until(&rx, "3 PM, 68 °F, weeks from 7, us units, clock ran 1");
let mut celsius = LocaleSettings::default();
celsius.temperature_unit = TemperatureUnit::Celsius;
let germany = LocaleSettings::of(Locale::parse("de-DE").unwrap());
for (settings, shown) in [
(celsius, "3 PM, 20 °C, weeks from 7, us units, clock ran 1"),
(
germany,
"15:00, 20 °C, weeks from 1, metric units, clock ran 2",
),
] {
engine.control().set_environment(Environment {
locale_settings: settings,
..Environment::default()
});
let (texts, ..) = until(&rx, shown);
assert_eq!(texts.last().map(|t| t.as_ref()), Some(shown));
}
}
#[component]
fn Zoned() -> impl IntoElement {
let runs = signal(0);
effect(move || {
let _ = locale_settings().time_zone;
runs.update(|r| *r += 1);
});
view().child(text(move || {
format!("{} after {} runs", locale_settings().time_zone, runs.get())
}))
}
#[test]
fn components_follow_the_time_zone() {
let (engine, rx, _) = start::<Zoned>(Environment::default());
until(&rx, "UTC after 1 runs");
for (zone, shown) in [
("Europe/Berlin", "Europe/Berlin after 2 runs"),
(
"America/Argentina/Buenos_Aires",
"America/Argentina/Buenos_Aires after 3 runs",
),
] {
let mut env = Environment::default();
env.locale_settings.time_zone = TimeZone::parse(zone).unwrap();
engine.control().set_environment(env);
let (texts, ..) = until(&rx, shown);
assert_eq!(texts.last().map(|t| t.as_ref()), Some(shown));
}
}
#[component]
fn Services() -> impl IntoElement {
let note = element_ref();
let open = signal(false);
view()
.flex_1()
.child(text("Announce").on_press(|_: &()| announce_for_accessibility("Saved")))
.child(text("Focus").on_press(move |_: &()| note.set_accessibility_focus()))
.child(text("Open").on_press(move |_: &()| open.set(true)))
.child(text("The note").element_ref(¬e))
.child(dynamic(move || {
open.get().then(|| {
dialog()
.status_bar(|| {
StatusBarSetting::default()
.foreground(StatusBarStyle::Light)
.hidden(true)
})
.child(text("Close").on_press(move |_: &()| open.set(false)))
})
}))
}
#[test]
fn components_speak_move_the_reader_and_set_the_status_bar() {
let (engine, rx, _) = start::<Services>(Environment::default());
let mut texts = HashMap::new();
let mut step = |press: Option<&str>| {
if let Some(label) = press {
engine
.control()
.dispatch_event(texts[label], EventKind::Press, EventPayload::None);
}
let mut commands = Vec::new();
while let Ok(batch) = rx.recv_timeout(Duration::from_millis(300)) {
for item in batch.items {
match item {
MountItem::Text { id, text, .. } => {
texts.insert(text.text.clone(), id);
}
MountItem::Command { id, command } => commands.push((id, command)),
_ => {}
}
}
}
commands
};
let status_bars = |commands: &[(u32, Command)]| -> Vec<(StatusBarStyle, bool)> {
commands
.iter()
.filter_map(|(_, c)| match c {
Command::StatusBar(config) => Some((config.style, config.hidden)),
_ => None,
})
.collect()
};
assert_eq!(status_bars(&step(None)), []);
assert_eq!(
step(Some("Announce")),
[(ROOT_ID, Command::Announce("Saved".into()))]
);
let focused = step(Some("Focus"));
assert_eq!(
focused.iter().map(|c| &c.1).collect::<Vec<_>>(),
[&Command::AccessibilityFocus]
);
assert_eq!(
status_bars(&step(Some("Open"))),
[(StatusBarStyle::Light, true)]
);
assert_eq!(
status_bars(&step(Some("Close"))),
[(StatusBarStyle::Default, false)]
);
assert_eq!(focused[0].0, texts["The note"]);
}
#[component]
fn AppLanguage() -> impl IntoElement {
view()
.flex_1()
.child(text("Chinese").on_press(|_: &()| {
nsh_core::i18n::set_locale(Some(Locale::parse("zh-Hant-TW").unwrap()))
}))
.child(text("System").on_press(|_: &()| nsh_core::i18n::set_locale(None)))
}
#[test]
fn the_app_language_reaches_the_platform_state_words() {
let (engine, rx, _) = start::<AppLanguage>(Environment::default());
let mut texts = HashMap::new();
let mut step = |press: Option<&str>| {
if let Some(label) = press {
engine
.control()
.dispatch_event(texts[label], EventKind::Press, EventPayload::None);
}
let mut sent = Vec::new();
while let Ok(batch) = rx.recv_timeout(Duration::from_millis(300)) {
for item in batch.items {
match item {
MountItem::Text { id, text, .. } => {
texts.insert(text.text.clone(), id);
}
MountItem::Command {
id,
command: Command::TextLocale(locale),
} => {
assert_eq!(id, ROOT_ID);
sent.push(locale.to_string());
}
_ => {}
}
}
}
sent
};
assert_eq!(step(None), Vec::<String>::new());
assert_eq!(step(Some("Chinese")), ["zh-Hant-TW"]);
assert_eq!(step(Some("Chinese")), Vec::<String>::new());
assert_eq!(step(Some("System")), ["en-US"]);
engine.control().set_environment(Environment {
locale: Locale::parse("ja-JP").unwrap(),
..Environment::default()
});
assert_eq!(step(None), ["ja-JP"]);
assert_eq!(step(Some("Chinese")), ["zh-Hant-TW"]);
}
#[component]
fn WindowInfo() -> impl IntoElement {
window().id("main").content(window_info_text)
}
#[component]
fn WindowInfoText() -> impl IntoElement {
view().child(text(move || {
let displays = nsh_core::element::desktop::displays().len();
match nsh_core::element::desktop::window_state() {
Some(state) => {
let mode = match state.mode {
WindowMode::Normal => "windowed",
WindowMode::Maximized => "maximized",
WindowMode::FullScreen => "fullscreen",
};
format!(
"x {} {mode} minimized {} displays {displays}",
state.bounds.origin.x.0, state.minimized
)
}
None => format!("no window, displays {displays}"),
}
}))
}
fn window_state_at(x: f32, mode: WindowMode) -> WindowState {
let bounds = Bounds::new(point(px(x), px(0.0)), size(px(800.0), px(600.0)));
WindowState::new(bounds, WindowPlacement::new(mode, bounds))
}
fn display(id: u64, x: f32) -> Display {
let bounds = Bounds::new(point(px(x), px(0.0)), size(px(1440.0), px(900.0)));
Display::new(DisplayId(id), bounds, bounds, x == 0.0)
}
fn last_text(rx: &Receiver<MountBatch>, last: &mut String) -> String {
while let Ok(batch) = rx.try_recv() {
for item in &batch.items {
if let MountItem::Text { text, .. } = item {
last.clone_from(&text.text);
}
}
}
last.clone()
}
fn advance(engine: &Engine, ms: u64) {
let (tx, rx) = crossbeam_channel::bounded(1);
engine
.control()
.advance_clock(Duration::from_millis(ms), move || {
let _ = tx.send(());
});
rx.recv_timeout(Duration::from_secs(10)).unwrap();
}
#[test]
fn components_read_their_windows_state_and_the_displays() {
let (tx, rx) = crossbeam_channel::unbounded();
let config = EngineConfig {
environment: Environment {
os: Os::MacOs,
..Environment::default()
},
clock: nsh_core::time::Clock::manual(
std::time::SystemTime::UNIX_EPOCH + Duration::from_secs(1_767_225_600),
),
..config(App::Rust(Root::of::<WindowInfo>()))
};
let sink = FrameSink {
batches: tx,
frames: Arc::new(AtomicUsize::new(0)),
};
let engine = Engine::start_with(
config,
|| ApproxTextEngine,
sink,
|control| {
control.set_displays(vec![display(1, 0.0), display(2, 1440.0)]);
},
)
.unwrap();
advance(&engine, 0);
let mut first = Vec::new();
while let Ok(batch) = rx.try_recv() {
first.extend(batch.items);
}
let mut last = first
.iter()
.rev()
.find_map(|item| match item {
MountItem::Text { text, .. } => Some(text.text.clone()),
_ => None,
})
.unwrap_or_default();
assert_eq!(last, "no window, displays 2");
let window = first
.iter()
.find_map(|item| match item {
MountItem::Command {
id,
command: Command::Desktop(command),
} if matches!(&**command, DesktopCommand::OpenWindow(_)) => Some(*id),
_ => None,
})
.expect("the window opens");
let report = |state: WindowState| {
engine.control().dispatch_event(
window,
EventKind::Window,
EventPayload::Window(Box::new(WindowEvent::State(Box::new(state)))),
);
};
report(window_state_at(0.0, WindowMode::Normal));
advance(&engine, 0);
assert_eq!(
last_text(&rx, &mut last),
"x 0 windowed minimized false displays 2"
);
report(window_state_at(20.0, WindowMode::Normal));
advance(&engine, 0);
assert_eq!(
last_text(&rx, &mut last),
"x 20 windowed minimized false displays 2"
);
report(window_state_at(0.0, WindowMode::Maximized));
advance(&engine, 0);
assert_eq!(
last_text(&rx, &mut last),
"x 0 maximized minimized false displays 2"
);
let mut minimized = window_state_at(0.0, WindowMode::Maximized);
minimized.minimized = true;
report(minimized);
advance(&engine, 0);
assert_eq!(
last_text(&rx, &mut last),
"x 0 maximized minimized true displays 2"
);
engine.control().set_displays(vec![display(1, 0.0)]);
advance(&engine, 0);
assert_eq!(
last_text(&rx, &mut last),
"x 0 maximized minimized true displays 1"
);
}
struct FakeLoginItem(Mutex<bool>);
impl nsh_core::desktop::LoginItemBackend for FakeLoginItem {
fn status(&self) -> std::io::Result<nsh_core::desktop::LoginItemStatus> {
Ok(match *self.0.lock().unwrap() {
true => nsh_core::desktop::LoginItemStatus::Enabled,
false => nsh_core::desktop::LoginItemStatus::Disabled,
})
}
fn set(&self, enabled: bool) -> std::io::Result<nsh_core::desktop::LoginItemStatus> {
*self.0.lock().unwrap() = enabled;
self.status()
}
}
#[component]
fn LoginItemInfo() -> impl IntoElement {
use nsh_core::element::desktop::{launch_at_login, set_launch_at_login};
let shown = signal(String::from("login"));
spawn(async move {
let read = launch_at_login().await;
let set = set_launch_at_login(false).await;
shown.set(format!("login {read} {set:?}"));
});
view().child(text(move || shown.get()))
}
#[test]
fn a_rust_app_reads_and_changes_launch_at_login() {
let shown = |backend: Option<Arc<dyn nsh_core::desktop::LoginItemBackend>>, expected| {
let (tx, rx) = crossbeam_channel::unbounded();
let config = EngineConfig {
login_item: backend,
..config(App::Rust(Root::of::<LoginItemInfo>()))
};
let sink = FrameSink {
batches: tx,
frames: Arc::default(),
};
let _engine = Engine::start(config, || ApproxTextEngine, sink).unwrap();
let (texts, ..) = until(&rx, expected);
texts.last().cloned().unwrap_or_default()
};
let backend = Arc::new(FakeLoginItem(Mutex::new(true)));
let expected = "login true Ok(())";
assert_eq!(shown(Some(backend), expected), expected);
let expected = "login false Err(Unavailable)";
assert_eq!(shown(None, expected), expected);
}
#[component]
fn FpsSwitch() -> impl IntoElement {
view()
.child(text(move || format!("fps {}", fps_enabled())))
.child(text("Measure").on_press(|_: &()| set_fps_enabled(true)))
}
#[test]
fn components_turn_the_performance_monitor_on_and_follow_it() {
let (engine, rx, _) = start::<FpsSwitch>(Environment::default());
let mut press = None;
let deadline = Instant::now() + Duration::from_secs(5);
while press.is_none() {
let batch = rx.recv_deadline(deadline).expect("the first render");
for item in batch.items {
if let MountItem::Text { id, text, .. } = item
&& text.text == "Measure"
{
press = Some(id);
}
}
}
engine
.control()
.dispatch_event(press.unwrap(), EventKind::Press, EventPayload::None);
let mut asked = false;
while !asked {
let batch = rx.recv_deadline(deadline).expect("the command");
asked = batch.items.iter().any(|item| {
matches!(
item,
MountItem::Command {
id: ROOT_ID,
command: Command::PerfMonitor(true)
}
)
});
}
engine
.control()
.set_perf_monitor(Some(Arc::new(nsh_engine::perf::PerfMonitor::new())));
until(&rx, "fps true");
engine.control().set_perf_monitor(None);
let (texts, ..) = until(&rx, "fps false");
assert_eq!(texts.last().map(String::as_str), Some("fps false"));
}
#[test]
fn a_rust_apps_main_window_takes_its_launch_options() {
use nsh_core::desktop::DesktopCommand;
let root = Root::of::<Label>().main_window_json(
r#"{"title":"Notes","titleBar":{"style":"hidden","height":40},"defaultSize":{"width":900,"height":600},"restoreBounds":true}"#,
);
let options = root
.main_window_ref()
.cloned()
.expect("the window's options");
assert_eq!(options.attributes.title.as_deref(), Some("Notes"));
assert!(options.restore_bounds);
for (os, idiom) in [
(Os::MacOs, Idiom::Desktop),
(Os::Web, Idiom::Desktop),
(Os::Ios, Idiom::Phone),
(Os::Android, Idiom::Phone),
] {
let config = EngineConfig {
environment: Environment {
os,
idiom,
..Environment::default()
},
..config(App::Rust(root.clone()))
};
assert_eq!(config.app.main_window(), Some(&options));
let (tx, rx) = crossbeam_channel::unbounded();
let _engine = Engine::start(
config,
|| ApproxTextEngine,
FrameSink {
batches: tx,
frames: Arc::default(),
},
)
.unwrap();
let batch = rx
.recv_timeout(Duration::from_secs(5))
.expect("the first frame");
let set = batch.items.into_iter().find_map(|item| match item {
MountItem::Command {
id: ROOT_ID,
command: Command::Desktop(command),
} => match *command {
DesktopCommand::SetAttributes(attributes) => Some(attributes),
_ => None,
},
_ => None,
});
if os.is_desktop() || os == Os::Web {
let set = set.unwrap_or_else(|| panic!("{os:?}: the main window's attributes"));
assert_eq!(set.title.as_deref(), Some("Notes"), "{os:?}");
} else {
assert_eq!(set, None, "{os:?}");
}
}
}