nsh-engine 0.8.1

Engine thread that drives the JS host and the core and emits mount batches.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
//! The platform's services for Rust components on the engine: the fonts
//! the UI sets' crates ship are registered before the first render, tasks
//! wait for display frames, components set the layout direction and read
//! the pixel ratio, the device, the locale and whether the app is in use,
//! and their language reaches the platform's own words;
//! they speak, move the screen reader and set the status bar with their
//! dialogs; they read the desktop window's state, a move once it stops,
//! and the displays.

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};

/// Hands the batches to the test and counts the frames asked for.
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,
    }
}

/// A Rust app of `C`, its batches and the count of frames it asked for.
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)
}

/// The texts and layout directions the batches carry, until one shows
/// `until` (or 5 seconds passed).
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)
}

/// Records the fonts registered.
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(())
    }
}

/// The files of a crate: a font, an image.
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,
    },
];

/// Reads the font, and records what was read.
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");
    // Only the font is read (images load where they show).
    assert_eq!(*read.lock().unwrap(), ["asset:/@app/fonts/Script.ttf"]);
    assert_eq!(
        *fonts.lock().unwrap(),
        [b"asset:/@app/fonts/Script.ttf".to_vec()]
    );
}

/// A font the embedding app registers is in place before the first render.
#[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()]);
}

/// Counts display frames while a task awaits them.
#[component]
fn Frames() -> impl IntoElement {
    let frames = signal(0);
    // The frame `request_animation_frame` called back with.
    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");
    // The engine asked the platform for a frame (once until it comes).
    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())
        );
    }
    // Each frame asked for the next; none is asked for once the task ended.
    std::thread::sleep(Duration::from_millis(100));
    assert_eq!(frames.load(Ordering::SeqCst), 3);
}

/// What a platform's UI thread gets, in the order the engine sends it.
enum ToMain {
    Batch(MountBatch),
    Task(nsh_engine::MainThreadTask),
}

/// Sends batches and the app's functions for the main thread down one
/// channel, as a platform's UI thread gets them.
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));
    }
}

/// Asks the main thread for its name, and for a function that panics.
#[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"));
    // The batch of the turn that asked reaches the UI thread before the function.
    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);
}

/// A Rust app's root, laid out in the surface's direction.
#[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());
    // A Rust app's surface takes the platform's direction...
    let (_, directions, _) = until(&rx, "direction");
    assert_eq!(directions, [false]);
    // ...and a right-to-left language lays it out right to left, at once.
    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));
}

/// Shows the pixel ratio, the device and the locale.
#[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:?}");
    // The window moved to another screen: what reads the pixel ratio
    // follows.
    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:?}");
    // The user changed the language: what reads the locale follows.
    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:?}");
}

/// Counts its effect's runs while it shows whether the app is in use, as
/// work that pauses in the background does.
#[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));
    }
    // Another part of the environment changing runs nothing that read only
    // the lifecycle.
    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:?}"
    );
}

/// Registers a font it was given while the app runs and shows its family,
/// as an app that downloads its fonts does.
#[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");
}

/// Greets in the first of the user's languages it has, as an app's
/// translated text does.
#[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));
    }
}

/// Shows a time, a temperature and the week as the user reads them, and
/// counts the runs of an effect that reads the hour cycle alone through a
/// `computed`, as a clock's does.
#[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 [
        // Only the unit changes: the clock's effect does not run again.
        (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));
    }
}

/// Shows the time zone and counts its effect's runs, as a clock that
/// shows times in it does.
#[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));
    }
}

/// Speaks, moves the screen reader and opens a dialog
/// that sets the status bar.
#[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(&note))
        .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();
    // Presses the text `label` (none: only the first render) and returns
    // the commands that follow.
    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()
    };
    // Nothing sets the status bar until the dialog opens.
    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]
    );
    // The dialog's setting while it shows; the default again once it
    // closed.
    assert_eq!(
        status_bars(&step(Some("Open"))),
        [(StatusBarStyle::Light, true)]
    );
    assert_eq!(
        status_bars(&step(Some("Close"))),
        [(StatusBarStyle::Default, false)]
    );
    // The reader's focus went to the note's view.
    assert_eq!(focused[0].0, texts["The note"]);
}

/// Shows the app in Traditional Chinese, or the user's language again.
#[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();
    // Presses the text `label` (none: only the first render) and returns
    // the languages of the app's text sent to the platform since.
    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
    };
    // The user's English is the platform's own default: nothing is sent.
    assert_eq!(step(None), Vec::<String>::new());
    // The app picks its language whatever the user's (the system's English
    // stays the user's).
    assert_eq!(step(Some("Chinese")), ["zh-Hant-TW"]);
    assert_eq!(step(Some("Chinese")), Vec::<String>::new());
    assert_eq!(step(Some("System")), ["en-US"]);
    // The user's language changes while the app follows it.
    engine.control().set_environment(Environment {
        locale: Locale::parse("ja-JP").unwrap(),
        ..Environment::default()
    });
    assert_eq!(step(None), ["ja-JP"]);
    // The app's choice wins over the user's language.
    assert_eq!(step(Some("Chinese")), ["zh-Hant-TW"]);
}

/// Shows the window's x, mode and whether it is minimized, and how many
/// displays there are.
#[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}"),
        }
    }))
}

/// A window at `x`, 800 × 600, in `mode`'s mode.
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)
}

/// The last text the batches so far showed.
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()
}

/// Moves `engine`'s manual clock by `ms`, then waits until it got there.
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)),
    };
    // The displays sent before the start are there as the app first renders.
    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();
    // The window opened; the platform has reported no state yet.
    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");
    // Its state is the platform's, reported as it arrives.
    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"
    );
    // A change of mode or minimizing comes with the bounds of then.
    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"
    );
    // A display unplugged.
    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"
    );
}

/// A login item the system keeps on until it is turned off.
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()
    }
}

/// Reads whether the app launches at login, turns it off and shows both
/// answers.
#[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() {
    // The last text the app shows, once it is `expected` (or 5 seconds
    // passed).
    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);
    // Without one (a view embedded in another app, `nsh run`) it is off and
    // cannot be turned on.
    let expected = "login false Err(Unavailable)";
    assert_eq!(shown(None, expected), expected);
}

/// Shows whether the performance monitor measures and turns it on.
#[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);
            }
        }
    }
    // The app asks; the platform turns its monitor on.
    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)
                }
            )
        });
    }
    // What the app reads follows the monitor the platform installs, and
    // removes.
    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"));
}

/// A Rust app's `nsh.json` `"window"` (its root's, as the generated code
/// sets it) reaches the main window as a TSX app's bundle's does: its
/// attributes with the first frame, the options for the runners. A phone's
/// surface has no window: nothing is sent there.
#[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()))
        };
        // What the runners read before they make the window.
        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();
        // The first frame: the label, and the attributes where they go.
        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:?}");
        }
    }
}