#[cfg(not(target_os = "macos"))] fn main() {} #[cfg(target_os = "macos")] #[path = "../src/background_tab.rs"] mod background_tab; #[cfg(target_os = "macos")] #[link(name = "WebKit", kind = "framework")] unsafe extern "C" {} #[cfg(target_os = "macos")] fn main() { // nextest runs a harness-less binary as `--list --format terse [--ignored]`, then ` --exact --nocapture`. let args: Vec = std::env::args().collect(); if args.iter().any(|a| a == "--list") { if !args.iter().any(|a| a == "--ignored") { println!("background_tab_appkit: test"); } return; } use background_tab::Return; use objc2::{MainThreadMarker, msg_send, rc::Retained, runtime::AnyClass}; use objc2_app_kit::{ NSApplication, NSBackingStoreType, NSView, NSWindow, NSWindowDidBecomeKeyNotification, NSWindowStyleMask, }; use objc2_foundation::{NSNotificationCenter, NSPoint, NSRect, NSSize}; use std::{cell::Cell, rc::Rc}; let mtm = MainThreadMarker::new().expect("main thread"); let _app = NSApplication::sharedApplication(mtm); let rect = NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(400.0, 300.0)); let window = unsafe { NSWindow::initWithContentRect_styleMask_backing_defer( mtm.alloc(), rect, NSWindowStyleMask::Titled, NSBackingStoreType::Buffered, false, ) }; let class = AnyClass::get(c"WKWebView").expect("WebKit is linked"); let web: Retained = unsafe { msg_send![msg_send![class, alloc], initWithFrame: rect] }; window.contentView().unwrap().addSubview(&web); let view = background_tab::web_view(&window).expect("web view in content view"); view.removeFromSuperview(); assert!(view.window().is_none()); // Not asked for its process ids: on a web view that never loaded a page, WebKit asserts in // `_networkProcessIdentifier` (macOS 15), which a real tab never is. let reason = Rc::new(Cell::new(None)); let observers = { let (reason, window, view) = (reason.clone(), window.clone(), view.clone()); background_tab::on_return(&window.clone(), move |why| { background_tab::put_back(&window, &view); reason.set(Some(why)); }) }; unsafe { NSNotificationCenter::defaultCenter() .postNotificationName_object(NSWindowDidBecomeKeyNotification, Some(&window)); } assert_eq!(reason.get(), Some(Return::Key)); assert!(view.window().is_some_and(|w| Retained::as_ptr(&w) == Retained::as_ptr(&window))); for observer in &observers { unsafe { NSNotificationCenter::defaultCenter().removeObserver(observer.as_ref()) }; } println!("ok: re-attached synchronously inside the key notification"); }