use crossbeam_channel::Receiver;
use nsh_core::{
EventKind, EventPayload, MountBatch, MountItem, ViewKind,
platform::{Os, SPLIT_VIEW_COMPONENT},
};
use nsh_engine::{Engine, Environment};
use crate::components::{shows, start_with, until};
const MAIL: &str = r#"import { mount, signal, Text } from "nsh";
import { type ColumnVisibility, createNavStack, NavPage, NavStack, SplitView } from "nsh/base";
const visibility = signal<ColumnVisibility>("all");
const reported = signal("none");
const mailboxes = createNavStack(["mailboxes"]);
const inbox = createNavStack(["inbox"]);
const message = createNavStack(["message"]);
// What the app takes: `detailOnly` and `all`, never `doubleColumn`.
const take = (next: ColumnVisibility) => {
reported.set(next);
if (next !== "doubleColumn") visibility.set(next);
};
mount(() => (
<SplitView
style={{ flex: 1 }}
columnVisibility={visibility()}
onColumnVisibilityChange={({ columnVisibility }) => take(columnVisibility)}
sidebar={<NavStack state={mailboxes}>{() => <NavPage title="Mailboxes"><Text>Mailboxes</Text></NavPage>}</NavStack>}
content={<NavStack state={inbox}>{() => <NavPage title="Inbox"><Text>Inbox</Text></NavPage>}</NavStack>}
detail={<NavStack state={message}>{() => <NavPage title="Message"><Text>{`Reported: ${reported()} Shown: ${visibility()}`}</Text></NavPage>}</NavStack>}
/>
));
"#;
fn start(os: Os, app: &str) -> (Engine, Receiver<MountBatch>) {
start_with(
app,
Environment {
os,
..Environment::default()
},
None,
None,
)
}
fn split_id(batches: &[MountBatch]) -> Option<u32> {
batches
.iter()
.flat_map(|b| &b.items)
.find_map(|item| match item {
MountItem::Component { id, name } if &**name == SPLIT_VIEW_COMPONENT => Some(*id),
_ => None,
})
}
fn prop<'a>(
batches: impl IntoIterator<Item = &'a MountBatch>,
id: u32,
key: &str,
) -> Option<serde_json::Value> {
batches
.into_iter()
.flat_map(|b| &b.items)
.filter_map(|item| match item {
MountItem::ComponentProps { id: c, props } if *c == id => Some(props),
_ => None,
})
.flatten()
.filter(|(name, _)| &**name == key)
.last()
.and_then(|(_, value)| value.clone())
}
fn stacks(batches: &[MountBatch]) -> Vec<u32> {
batches
.iter()
.flat_map(|b| &b.items)
.filter_map(|item| match item {
MountItem::Create {
id,
kind: ViewKind::Stack,
} => Some(*id),
_ => None,
})
.collect()
}
fn frame<'a>(batches: impl IntoIterator<Item = &'a MountBatch>, id: u32) -> Option<(f32, f32)> {
batches
.into_iter()
.flat_map(|b| &b.items)
.filter_map(|item| match item {
MountItem::Frame { id: f, frame } if *f == id => {
Some((frame.size.width.0, frame.size.height.0))
}
_ => None,
})
.last()
}
fn native(engine: &Engine, split: u32, name: &str, data: serde_json::Value) {
engine.control().dispatch_event(
split,
EventKind::NativeEvent,
EventPayload::Native {
name: name.into(),
data,
},
);
}
#[test]
fn ios_gets_a_split_view_controller_of_the_columns_stacks() {
let (engine, rx) = start(Os::Ios, MAIL);
let initial = until(&rx, |batch| shows(batch, "Reported: none Shown: all"));
let split = split_id(&initial).expect("the native split");
assert_eq!(prop(&initial, split, "columns"), Some("triple".into()));
assert_eq!(
prop(&initial, split, "columnVisibility"),
Some("all".into())
);
assert_eq!(prop(&initial, split, "revision"), Some(0.into()));
let made = stacks(&initial);
assert_eq!(made.len(), 3, "a stack per column");
native(
&engine,
split,
"layout",
serde_json::json!({
"sidebar": { "width": 320, "height": 700 },
"content": { "width": 320, "height": 700 },
"detail": { "width": 384, "height": 700 },
}),
);
let laid_out = until(&rx, |batch| frame([batch], made[0]) == Some((320.0, 700.0)));
assert_eq!(frame(&laid_out, made[0]), Some((320.0, 700.0)));
native(
&engine,
split,
"columnVisibilityChange",
"doubleColumn".into(),
);
let kept = until(&rx, |batch| {
prop([batch], split, "revision") == Some(1.into())
});
assert!(
kept.iter()
.any(|b| shows(b, "Reported: doubleColumn Shown: all")),
"reported, and the app kept all"
);
native(
&engine,
split,
"columnVisibilityChange",
"detailOnly".into(),
);
let taken = until(&rx, |batch| {
shows(batch, "Reported: detailOnly Shown: detailOnly")
});
assert_eq!(
prop(&taken, split, "columnVisibility"),
Some("detailOnly".into())
);
assert_eq!(prop(&taken, split, "revision"), None, "nothing shown again");
native(&engine, split, "compactColumnChange", 1.into());
let back = until(&rx, |batch| {
prop([batch], split, "revision") == Some(2.into())
});
assert!(
back.iter()
.any(|b| shows(b, "Reported: doubleColumn Shown: detailOnly")),
"the column the user went back to is reported"
);
for batch in initial.iter().chain(&laid_out).chain(&kept).chain(&taken) {
for item in &batch.items {
if let MountItem::Delete { id } = item {
assert!(!made.contains(id), "stack {id} deleted");
}
}
}
}
#[test]
fn elsewhere_the_columns_are_a_dock_areas() {
let app = format!("{MAIL}\nsetTimeout(() => visibility.set(\"detailOnly\"), 100);\n");
for os in [Os::Android, Os::MacOs, Os::Web] {
let (_engine, rx) = start(os, &app);
let batches = until(&rx, |batch| {
shows(batch, "Reported: none Shown: detailOnly")
});
assert_eq!(split_id(&batches), None, "{os:?}");
let made = stacks(&batches);
assert_eq!(made.len(), 3, "{os:?}: a stack per column");
for batch in &batches {
for item in &batch.items {
if let MountItem::Delete { id } = item {
assert!(!made.contains(id), "{os:?}: stack {id} deleted");
}
}
}
}
}