//! Real StatusNotifier/DBusMenu transport test on a private session bus. //! It does not claim a real desktop panel displays the icon. #![cfg(all(target_os = "linux", not(target_env = "ohos")))] use budget_capture::desktop::{Tray, TrayAction}; use std::{ collections::HashMap, sync::{mpsc, Arc}, time::Duration, }; #[test] fn native_dbus_menu_dispatches_all_actions_and_can_be_recreated() { if std::env::var("BUDGET_LIVE_TRAY_TEST").as_deref() != Ok("1") { return; } for _ in 0..2 { let (send, recv) = mpsc::channel(); let tray = Tray::new(Arc::new(move || { let _ = send.send(()); })) .unwrap(); futures_lite::future::block_on(async { let connection = zbus::Connection::session().await.unwrap(); let bus = zbus::fdo::DBusProxy::new(&connection).await.unwrap(); let prefix = format!("org.kde.StatusNotifierItem-{}-", std::process::id()); let names = bus.list_names().await.unwrap(); let name = names .iter() .find(|name| name.as_str().starts_with(&prefix)) .expect("native tray registered on D-Bus"); let menu = zbus::Proxy::new( &connection, name.as_str(), "/MenuBar", "com.canonical.dbusmenu", ) .await .unwrap(); let items: Vec<(i32, HashMap)> = menu .call( "GetGroupProperties", &(Vec::::new(), vec!["label".to_string()]), ) .await .unwrap(); for (label, expected) in [ ("Open Budget", TrayAction::Open), ("Capture product…", TrayAction::Capture), ("Minimize Budget", TrayAction::Minimize), ("Quit Budget", TrayAction::Quit), ] { let id = items .iter() .find_map(|(id, properties)| { let value = properties.get("label")?; (String::try_from(value.try_clone().ok()?).ok()?.as_str() == label) .then_some(*id) }) .expect("native menu label"); let _: () = menu .call( "Event", &(id, "clicked", zbus::zvariant::Value::from(0i32), 0u32), ) .await .unwrap(); recv.recv_timeout(Duration::from_secs(5)) .expect("UI wake notification"); assert_eq!(tray.drain(), vec![expected]); } connection.close().await.unwrap(); }); drop(tray); } }