#[cfg(target_os = "macos")]
mod checks {
use std::cell::{Cell, RefCell};
use std::rc::Rc;
use mitsuami_appkit::{AppKitBackend, AppKitHandle, BackendOptions, NativeView};
use mitsuami_core::services::{Alert, Menu, MenuBar, MenuItem, MenuRole, OpenFile, ServiceError, Shortcut};
use mitsuami_core::{Key, Size, Ui, View};
use objc2::rc::Retained;
use objc2::runtime::AnyObject;
use objc2_app_kit::{
NSApplication, NSButton, NSControlStateValueOff, NSControlStateValueOn, NSEventModifierFlags, NSMenu,
NSMenuItem, NSSavePanel, NSView, NSWindow, NSWindowDidBecomeMainNotification,
NSWindowDidResignMainNotification,
};
use objc2_foundation::{NSDate, NSNotificationCenter, NSRunLoop};
pub struct Fixture {
pub ui: Ui,
pub handle: AppKitHandle,
}
pub fn fixture() -> Fixture {
let mtm = mitsuami_appkit::init_for_tests();
let options = BackendOptions { show_windows: false, private_clipboard: true, ..BackendOptions::default() };
let backend = AppKitBackend::new(mtm, options);
let handle = backend.handle();
Fixture { ui: Ui::new(backend), handle }
}
fn pump(ui: &Ui) {
for _ in 0..10 {
let until = NSDate::dateWithTimeIntervalSinceNow(0.02);
NSRunLoop::currentRunLoop().runUntilDate(&until);
ui.tick();
}
}
fn pump_until(ui: &Ui, done: impl Fn() -> bool) {
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
while !done() && std::time::Instant::now() < deadline {
pump(ui);
}
}
fn find_button(view: &NSView, title: &str) -> Option<Retained<NSButton>> {
for sub in view.subviews().iter() {
if let Some(button) = sub.downcast_ref::<NSButton>()
&& button.title().to_string() == title
{
return Some(button.retain());
}
if let Some(found) = find_button(&sub, title) {
return Some(found);
}
}
None
}
fn sheet_of(window: &NSWindow) -> Retained<NSWindow> {
window.attachedSheet().expect("a sheet is attached to the window")
}
use objc2::Message;
pub fn clipboard_round_trips(f: &Fixture) {
let written = Rc::new(RefCell::new(None));
let read = Rc::new(RefCell::new(None));
let (w, r) = (written.clone(), read.clone());
let write = f.ui.set_clipboard_text("mitsuami ✓");
let ui = f.ui.clone();
f.ui.spawn_local(async move {
*w.borrow_mut() = Some(write.await);
*r.borrow_mut() = ui.clipboard_text().await;
});
f.ui.tick();
assert_eq!(*written.borrow(), Some(Ok(())));
assert_eq!(read.borrow().as_deref(), Some("mitsuami ✓"));
}
pub fn menus_are_installed_and_activate(f: &Fixture) {
let chosen = Rc::new(Cell::new(0));
let c = chosen.clone();
f.ui.set_menu(
MenuBar::new()
.menu(
Menu::new("File")
.item(
MenuItem::new("New").on_select(move || c.set(c.get() + 1)).shortcut(Shortcut::primary('n')),
)
.item(MenuItem::new("Unavailable").enabled(false))
.item(MenuItem::new("Move to Trash").shortcut(Shortcut::primary(Key::Backspace)))
.item(MenuItem::new("Enclosing Folder").shortcut(Shortcut::primary(Key::Up))),
)
.menu(Menu::new("View").item(MenuItem::new("Zoom"))),
);
let mtm = objc2::MainThreadMarker::new().unwrap();
let bar = NSApplication::sharedApplication(mtm).mainMenu().expect("a main menu");
let titles: Vec<String> = bar.itemArray().iter().map(|i| i.title().to_string()).collect();
assert_eq!(&titles[1..], ["File", "Edit", "View"], "app menu, File, Edit, then the rest");
let file = bar.itemAtIndex(1).and_then(|i| i.submenu()).expect("File submenu");
let new = file.itemAtIndex(0).unwrap();
assert_eq!(new.keyEquivalent().to_string(), "n");
assert!(new.keyEquivalentModifierMask().contains(NSEventModifierFlags::Command));
assert!(new.isEnabled());
assert!(!file.itemAtIndex(1).unwrap().isEnabled());
let trash = file.itemAtIndex(2).unwrap();
assert_eq!(trash.keyEquivalent().to_string(), "\u{8}");
assert_eq!(trash.keyEquivalentModifierMask(), NSEventModifierFlags::Command);
assert_eq!(file.itemAtIndex(3).unwrap().keyEquivalent().to_string(), "\u{f700}");
file.performActionForItemAtIndex(0);
f.ui.tick();
assert_eq!(chosen.get(), 1, "the handler ran");
}
fn main_menu() -> Retained<NSMenu> {
let mtm = objc2::MainThreadMarker::new().unwrap();
NSApplication::sharedApplication(mtm).mainMenu().expect("a main menu")
}
fn titles(menu: &NSMenu) -> Vec<String> {
let visible = |title: String| title.chars().filter(|c| !('\u{2066}'..='\u{2069}').contains(c)).collect();
menu.itemArray()
.iter()
.map(|i| if i.isSeparatorItem() { "-".into() } else { visible(i.title().to_string()) })
.collect()
}
fn submenu(menu: &NSMenu, title: &str) -> Retained<NSMenu> {
menu.itemWithTitle(&objc2_foundation::NSString::from_str(title)).and_then(|i| i.submenu()).expect(title)
}
fn item(menu: &NSMenu, title: &str) -> Retained<NSMenuItem> {
menu.itemWithTitle(&objc2_foundation::NSString::from_str(title)).expect(title)
}
pub fn submenus_check_marks_and_roles(f: &Fixture) {
let settings = Rc::new(Cell::new(0));
let s = settings.clone();
let sidebar = mitsuami_reactive::signal(true);
let zoom = mitsuami_reactive::signal(1);
f.ui.set_menu(
MenuBar::new()
.menu(
Menu::new("File")
.submenu(Menu::new("Open Recent").item(MenuItem::new("notes.txt")))
.separator()
.item(
MenuItem::new("Preferences").role(MenuRole::Settings).on_select(move || s.set(s.get() + 1)),
),
)
.menu(
Menu::new("View")
.item(MenuItem::new("Show Sidebar").bind(sidebar))
.separator()
.item(MenuItem::new("Small").radio((zoom, 0)))
.item(MenuItem::new("Large").radio((zoom, 1))),
)
.menu(Menu::new("Help").item(MenuItem::new("About the app").role(MenuRole::About))),
);
let bar = main_menu();
let name = objc2_foundation::NSProcessInfo::processInfo().processName().to_string();
let app_menu = bar.itemAtIndex(0).and_then(|i| i.submenu()).unwrap();
assert_eq!(
titles(&app_menu),
[
format!("About {name}"),
"-".into(),
"Settings…".into(),
"-".into(),
format!("Hide {name}"),
"-".into(),
format!("Quit {name}")
],
"roles go to the app menu, with AppKit's titles"
);
assert_eq!(item(&app_menu, "Settings…").keyEquivalent().to_string(), ",");
assert_eq!(titles(&bar)[1..], ["File", "Edit", "View"], "Help went with its only item");
let file = submenu(&bar, "File");
assert_eq!(titles(&file), ["Open Recent"], "and File lost the separator before Settings");
assert_eq!(titles(&submenu(&file, "Open Recent")), ["notes.txt"]);
let view = submenu(&bar, "View");
let state = |title: &str| {
submenu(&main_menu(), "View").itemWithTitle(&objc2_foundation::NSString::from_str(title)).unwrap().state()
};
assert_eq!(state("Show Sidebar"), NSControlStateValueOn);
assert_eq!((state("Small"), state("Large")), (NSControlStateValueOff, NSControlStateValueOn));
view.performActionForItemAtIndex(0);
view.performActionForItemAtIndex(2);
f.ui.tick();
assert!(!sidebar.get_untracked());
assert_eq!(zoom.get_untracked(), 0);
assert_eq!(state("Show Sidebar"), NSControlStateValueOff, "the menu follows the signal");
assert_eq!((state("Small"), state("Large")), (NSControlStateValueOn, NSControlStateValueOff));
app_menu.performActionForItemAtIndex(2);
f.ui.tick();
assert_eq!(settings.get(), 1, "Settings… runs the app's item");
let checked = Rc::new(Cell::new(0));
let c = checked.clone();
f.ui.set_menu(
MenuBar::new()
.menu(
Menu::new(name.clone())
.item(MenuItem::new("About the app").role(MenuRole::About))
.item(MenuItem::new("Check for Updates…").on_select(move || c.set(c.get() + 1)))
.separator()
.item(MenuItem::new("Preferences").role(MenuRole::Settings)),
)
.menu(Menu::new("View").item(MenuItem::new("Show Sidebar"))),
);
let bar = main_menu();
let app_menu = bar.itemAtIndex(0).and_then(|i| i.submenu()).unwrap();
assert_eq!(
titles(&app_menu),
[
format!("About {name}"),
"-".into(),
"Settings…".into(),
"-".into(),
"Check for Updates…".into(),
"-".into(),
format!("Hide {name}"),
"-".into(),
format!("Quit {name}")
]
);
assert_eq!(titles(&bar)[1..], ["Edit", "View"], "and isn't a menu of its own");
app_menu.performActionForItemAtIndex(4);
f.ui.tick();
assert_eq!(checked.get(), 1);
f.ui.set_menu(MenuBar::new());
}
pub fn window_menus_are_there_while_it_is_main(f: &Fixture) {
let window = f.ui.create_window("editor", Size::new(400.0, 300.0));
f.ui.set_menu(MenuBar::new().menu(Menu::new("File").item(MenuItem::new("New"))));
f.ui.set_window_menu(
window,
MenuBar::new()
.menu(Menu::new("File").item(MenuItem::new("Export…")))
.menu(Menu::new("Format").item(MenuItem::new("Bold"))),
);
f.ui.tick();
assert_eq!(titles(&main_menu())[1..], ["File", "Edit"], "not main: only the app's menus");
let ns_window = f.handle.ns_window(window).unwrap();
let posted = ns_window.clone();
let view = NativeView::appkit(move |cx| {
let center = NSNotificationCenter::defaultCenter();
unsafe { center.postNotificationName_object(NSWindowDidBecomeMainNotification, Some(&posted)) };
NSView::new(cx.mtm())
})
.build(&f.ui);
f.ui.append_child(window, view);
pump(&f.ui);
assert_eq!(titles(&main_menu())[1..], ["File", "Edit", "Format"]);
assert_eq!(titles(&submenu(&main_menu(), "File")), ["New", "-", "Export…"], "its File joins the app's");
let center = NSNotificationCenter::defaultCenter();
unsafe { center.postNotificationName_object(NSWindowDidResignMainNotification, Some(&ns_window)) };
pump(&f.ui);
assert_eq!(titles(&main_menu())[1..], ["File", "Edit"]);
f.ui.destroy(window);
f.ui.set_menu(MenuBar::new());
f.ui.tick();
}
pub fn alerts_are_answered_through_their_sheet(f: &Fixture) {
let window = f.ui.create_window("alert host", Size::new(400.0, 300.0));
f.ui.tick();
let answer = Rc::new(RefCell::new(None));
let a = answer.clone();
let alert = Alert::new("Proceed?").button("Yes").destructive_button("Delete").button("No");
let reply = f.ui.alert(Some(window), alert);
f.ui.spawn_local(async move { *a.borrow_mut() = Some(reply.await) });
pump(&f.ui);
let ns_window = f.handle.ns_window(window).unwrap();
let content = sheet_of(&ns_window).contentView().unwrap();
let delete = find_button(&content, "Delete").expect("the Delete button");
assert!(delete.hasDestructiveAction(), "Delete is destructive");
assert!(!find_button(&content, "Yes").unwrap().hasDestructiveAction(), "Yes isn't");
let no = find_button(&content, "No").expect("the No button");
unsafe { no.performClick(None::<&AnyObject>) };
pump(&f.ui);
assert_eq!(*answer.borrow(), Some(2));
f.ui.destroy(window);
f.ui.tick();
}
pub fn open_panels_report_cancellation(f: &Fixture) {
let window = f.ui.create_window("panel host", Size::new(600.0, 400.0));
f.ui.tick();
let answer = Rc::new(RefCell::new(Some(Some(vec![]))));
let a = answer.clone();
let reply = f.ui.open_file(Some(window), OpenFile::new().multiple());
f.ui.spawn_local(async move { *a.borrow_mut() = Some(reply.await) });
pump(&f.ui);
let ns_window = f.handle.ns_window(window).unwrap();
let sheet = sheet_of(&ns_window);
let panel = sheet.downcast::<NSSavePanel>().expect("an open panel");
unsafe { panel.cancel(None) };
pump(&f.ui);
assert_eq!(*answer.borrow(), Some(None), "cancelled");
f.ui.destroy(window);
f.ui.tick();
}
pub fn folder_panels_can_make_folders(f: &Fixture) {
let window = f.ui.create_window("panel host", Size::new(600.0, 400.0));
f.ui.tick();
for (request, folders) in [(OpenFile::new().directories(), true), (OpenFile::new(), false)] {
let reply = f.ui.open_file(Some(window), request);
f.ui.spawn_local(async move {
reply.await;
});
pump(&f.ui);
let ns_window = f.handle.ns_window(window).unwrap();
let panel = sheet_of(&ns_window).downcast::<NSSavePanel>().expect("an open panel");
assert_eq!(
panel.canCreateDirectories(),
folders,
"New Folder on a {} panel",
if folders { "folder" } else { "file" }
);
unsafe { panel.cancel(None) };
pump(&f.ui);
}
f.ui.destroy(window);
f.ui.tick();
}
pub fn sheets_wait_for_their_window(_: &Fixture) {
let mtm = mitsuami_appkit::init_for_tests();
let options = BackendOptions { show_windows: true, private_clipboard: true, ..BackendOptions::default() };
let backend = AppKitBackend::new(mtm, options);
let handle = backend.handle();
let ui = Ui::new(backend);
let window = ui.create_window("asks at start", Size::new(600.0, 400.0));
ui.tick();
let ns_window = handle.ns_window(window).expect("the window is made");
assert!(!ns_window.isVisible(), "not shown yet");
let answer = Rc::new(Cell::new(None));
let a = answer.clone();
let reply = ui.alert(Some(window), Alert::new("Choose a folder?").button("Choose").button("Later"));
ui.spawn_local(async move { a.set(Some(reply.await)) });
pump(&ui);
handle.show_pending_windows();
pump(&ui);
let sheet = sheet_of(&ns_window);
let screen = ns_window.screen().expect("on a screen").visibleFrame();
let shown = ns_window.frame();
assert!(ns_window.isVisible(), "the window is on screen");
let centre = |r: objc2_foundation::NSRect| r.origin.x + r.size.width / 2.0;
assert!(
(centre(shown) - centre(screen)).abs() < 2.0,
"the window is centred, not at x={} on a screen from x={}",
shown.origin.x,
screen.origin.x
);
ns_window.endSheet_returnCode(&sheet, objc2_app_kit::NSAlertSecondButtonReturn);
pump(&ui);
assert_eq!(answer.get(), Some(1), "the alert answered its second button");
let after = ns_window.frame();
assert_eq!((after.origin.x, after.origin.y), (shown.origin.x, shown.origin.y), "the window stays where it was");
ui.destroy(window);
ui.tick();
}
pub fn sheets_end_with_their_window(f: &Fixture) {
let first = f.ui.create_window("alert host", Size::new(400.0, 300.0));
let second = f.ui.create_window("panel host", Size::new(600.0, 400.0));
f.ui.tick();
let answers = Rc::new(RefCell::new((None, None)));
let (a, b) = (answers.clone(), answers.clone());
let alert = f.ui.alert(Some(first), Alert::new("Proceed?").button("Yes").button("No"));
let open = f.ui.open_file(Some(second), OpenFile::new());
f.ui.spawn_local(async move { a.borrow_mut().0 = Some(alert.await) });
f.ui.spawn_local(async move { b.borrow_mut().1 = Some(open.await) });
pump(&f.ui);
sheet_of(&f.handle.ns_window(first).unwrap());
sheet_of(&f.handle.ns_window(second).unwrap());
f.ui.destroy(first);
f.ui.destroy(second);
f.ui.tick();
pump_until(&f.ui, || *answers.borrow() == (Some(1), Some(None)));
assert_eq!(*answers.borrow(), (Some(1), Some(None)));
}
pub fn trash_moves_files_to_the_users_trash(f: &Fixture) {
let name = format!("mitsuami-trash-check-{}.txt", std::process::id());
let file = std::env::temp_dir().join(&name);
std::fs::write(&file, b"trash me").unwrap();
let answer = Rc::new(RefCell::new(None));
let a = answer.clone();
let reply = f.ui.trash(None, vec![file.clone()]);
f.ui.spawn_local(async move { *a.borrow_mut() = Some(reply.await) });
pump_until(&f.ui, || answer.borrow().is_some());
assert_eq!(*answer.borrow(), Some(Ok(())));
assert!(!file.exists(), "the file left its folder");
let trashed = std::env::home_dir().unwrap().join(".Trash").join(&name);
assert!(trashed.exists(), "it's in the trash, where Put Back finds it");
std::fs::remove_file(trashed).unwrap();
let a = answer.clone();
*answer.borrow_mut() = None;
let reply = f.ui.trash(None, vec![file]);
f.ui.spawn_local(async move { *a.borrow_mut() = Some(reply.await) });
pump_until(&f.ui, || answer.borrow().is_some());
assert!(matches!(*answer.borrow(), Some(Err(ServiceError::Failed(_)))), "{:?}", answer.borrow());
}
}
#[cfg(target_os = "macos")]
fn main() {
use std::panic::{AssertUnwindSafe, catch_unwind};
type Check = (&'static str, fn(&checks::Fixture));
let checks: [Check; 10] = [
("clipboard_round_trips", checks::clipboard_round_trips),
("menus_are_installed_and_activate", checks::menus_are_installed_and_activate),
("submenus_check_marks_and_roles", checks::submenus_check_marks_and_roles),
("window_menus_are_there_while_it_is_main", checks::window_menus_are_there_while_it_is_main),
("alerts_are_answered_through_their_sheet", checks::alerts_are_answered_through_their_sheet),
("open_panels_report_cancellation", checks::open_panels_report_cancellation),
("folder_panels_can_make_folders", checks::folder_panels_can_make_folders),
("sheets_end_with_their_window", checks::sheets_end_with_their_window),
("sheets_wait_for_their_window", checks::sheets_wait_for_their_window),
("trash_moves_files_to_the_users_trash", checks::trash_moves_files_to_the_users_trash),
];
let filter: Vec<String> = std::env::args().skip(1).filter(|a| !a.starts_with('-')).collect();
let fixture = checks::fixture();
let mut failed = 0;
println!("\nrunning {} tests", checks.len());
for (name, check) in checks {
if !filter.is_empty() && !filter.iter().any(|f| name.contains(f.as_str())) {
continue;
}
let ok = catch_unwind(AssertUnwindSafe(|| check(&fixture))).is_ok();
println!("test {name} ... {}", if ok { "ok" } else { "FAILED" });
failed += usize::from(!ok);
}
println!("\ntest result: {}. {failed} failed\n", if failed == 0 { "ok" } else { "FAILED" });
std::process::exit(if failed == 0 { 0 } else { 101 });
}
#[cfg(not(target_os = "macos"))]
fn main() {}