|
| 1 | +mod config; |
| 2 | + |
| 3 | +use std::collections::HashMap; |
| 4 | + |
| 5 | +use client::DbusClient; |
| 6 | +use zbus::zvariant::Value; |
| 7 | + |
| 8 | +#[test] |
| 9 | +fn test_client_capabilities() { |
| 10 | + let client = DbusClient::new(); |
| 11 | + let msg = client.call_method("GetClientCapabilities", &()).unwrap(); |
| 12 | + let body = msg.body(); |
| 13 | + let rsp: HashMap<String, Value> = body.deserialize().unwrap(); |
| 14 | + |
| 15 | + let capabilities = HashMap::from([ |
| 16 | + ("conditionalCreate", false), |
| 17 | + ("conditionalGet", false), |
| 18 | + ("hybridTransport", false), |
| 19 | + ("passkeyPlatformAuthenticator", false), |
| 20 | + ("userVerifyingPlatformAuthenticator", false), |
| 21 | + ("relatedOrigins", false), |
| 22 | + ("signalAllAcceptedCredentials", false), |
| 23 | + ("signalCurrentUserDetails", false), |
| 24 | + ("signalUnknownCredential", false), |
| 25 | + ]); |
| 26 | + for (key, expected) in capabilities.iter() { |
| 27 | + let value: &Value = rsp.get(*key).unwrap(); |
| 28 | + assert_eq!(*expected, value.try_into().unwrap()); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +mod client { |
| 33 | + use crate::config::{INTERFACE, PATH, SERVICE_DIR, SERVICE_NAME}; |
| 34 | + use gtk::gio::{TestDBus, TestDBusFlags}; |
| 35 | + use serde::Serialize; |
| 36 | + use zbus::{blocking::Connection, zvariant::DynamicType, Message}; |
| 37 | + |
| 38 | + fn init_test_dbus() -> TestDBus { |
| 39 | + let dbus = TestDBus::new(TestDBusFlags::NONE); |
| 40 | + |
| 41 | + // assumes this runs in root of Cargo project. |
| 42 | + let current_dir = std::env::current_dir().unwrap(); |
| 43 | + let service_dir = current_dir.join(SERVICE_DIR); |
| 44 | + println!("{:?}", service_dir); |
| 45 | + dbus.add_service_dir(service_dir.to_str().unwrap()); |
| 46 | + |
| 47 | + dbus.up(); |
| 48 | + dbus |
| 49 | + } |
| 50 | + |
| 51 | + pub(super) struct DbusClient { |
| 52 | + _bus: TestDBus, |
| 53 | + } |
| 54 | + |
| 55 | + impl DbusClient { |
| 56 | + pub fn new() -> Self { |
| 57 | + Self { |
| 58 | + _bus: init_test_dbus(), |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + pub fn call_method<B>(&self, method_name: &str, body: &B) -> zbus::Result<Message> |
| 63 | + where |
| 64 | + B: Serialize + DynamicType, |
| 65 | + { |
| 66 | + let connection = Connection::session().unwrap(); |
| 67 | + connection.call_method(Some(SERVICE_NAME), PATH, Some(INTERFACE), method_name, body) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments