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
/// Expands to implement the required methods for the [`crate::EventProperties`] trait.
/// This depends on the struct to have an `item` field of type `ObjectRef`.
///
/// ```ignore
/// impl_from_interface_event_enum_for_event!(TextCaretMovedEvent);
/// ```
///
/// Expands to:
///
/// ```ignore
/// impl EventProperties for TextCaretMovedEvent {
/// fn sender(&self) -> UniqueName<'_> {
/// self.item.name.as_ref()
/// }
/// fn path(&self) -> ObjectPath<'_> {
/// self.item.path.as_ref()
/// }
/// }
/// ```
macro_rules! impl_event_properties {
($type:ty) => {
impl EventProperties for $type {
fn sender(&self) -> UniqueName<'_> {
self.item.name.as_ref()
}
fn path(&self) -> ObjectPath<'_> {
self.item.path.as_ref()
}
}
};
}
/// Expands to a conversion given the enclosed event type and outer `Event` variant.
///
/// eg
/// ```ignore
/// impl_from_interface_event_enum_for_event!(ObjectEvents, Event::Object);
/// ```
/// expands to:
///
/// ```ignore
/// impl From<ObjectEvents> for Event {
/// fn from(event_variant: ObjectEvents) -> Event {
/// Event::Object(event_variant.into())
/// }
/// }
/// ```
macro_rules! impl_from_interface_event_enum_for_event {
($outer_type:ty, $outer_variant:path) => {
impl From<$outer_type> for Event {
fn from(event_variant: $outer_type) -> Event {
$outer_variant(event_variant.into())
}
}
};
}
/// Expands to a conversion given the enclosed event enum type and outer `Event` variant.
///
/// eg
/// ```ignore
/// impl_try_from_event_for_user_facing_event_type!(ObjectEvents, Event::Object);
/// ```
/// expands to:
///
/// ```ignore
/// impl TryFrom<Event> for ObjectEvents {
/// type Error = AtspiError;
/// fn try_from(generic_event: Event) -> Result<ObjectEvents, Self::Error> {
/// if let Event::Object(event_type) = generic_event {
/// Ok(event_type)
/// } else {
/// Err(AtspiError::Conversion("Invalid type"))
/// }
/// }
/// }
/// ```
macro_rules! impl_try_from_event_for_user_facing_event_type {
($outer_type:ty, $outer_variant:path) => {
impl TryFrom<Event> for $outer_type {
type Error = AtspiError;
fn try_from(generic_event: Event) -> Result<$outer_type, Self::Error> {
if let $outer_variant(event_type) = generic_event {
Ok(event_type)
} else {
Err(AtspiError::Conversion("Invalid type"))
}
}
}
};
}
/// Expands to a conversion given the user facing event type and outer `Event::Interface(<InterfaceEnum>)` variant.,
/// the enum type and outtermost variant.
///
/// ```ignore user facing type, enum type, outer variant
/// impl_from_user_facing_event_for_interface_event_enum!(StateChangedEvent, ObjectEvents, ObjectEvents::StateChanged);
/// ```
///
/// expands to:
///
/// ```ignore
/// impl From<StateChangedEvent> for ObjectEvents {
/// fn from(specific_event: StateChangedEvent) -> ObjectEvents {
/// ObjectEvents::StateChanged(specific_event)
/// }
/// }
/// ```
macro_rules! impl_from_user_facing_event_for_interface_event_enum {
($inner_type:ty, $outer_type:ty, $inner_variant:path) => {
impl From<$inner_type> for $outer_type {
fn from(specific_event: $inner_type) -> $outer_type {
$inner_variant(specific_event)
}
}
};
}
/// Expands to a conversion given two arguments,
/// 1. the user facing event type `(inner_type)`
/// which relies on a conversion to its interface variant enum type variant.
/// 2. the outer `Event::<Interface(<InterfaceEnum>)>` wrapper.,
/// the enum type and outtermost variant.
///
/// ```ignore user facing type, outer event variant
/// impl_from_user_facing_type_for_event_enum!(StateChangedEvent, Event::Object);
/// ```
///
/// expands to:
///
/// ```ignore
/// impl From<StateChangedEvent> for Event {
/// fn from(event_variant: StateChangedEvent) -> Event {
/// Event::Object(ObjectEvents::StateChanged(event_variant))
/// }
/// }
/// ```
macro_rules! impl_from_user_facing_type_for_event_enum {
($inner_type:ty, $outer_variant:path) => {
impl From<$inner_type> for Event {
fn from(event_variant: $inner_type) -> Event {
$outer_variant(event_variant.into())
}
}
};
}
/// Expands to a conversion given two arguments,
/// 1. the user facing event type `(inner_type)`
/// 2. the outer `Event::<Interface(<InterfaceEnum>)>` wrapper.
///
/// eg
/// ```ignore
/// impl_try_from_event_for_user_facing_type!(StateChangedEvent, ObjectEvents::StateChanged);
/// ```
/// expands to:
///
/// ```ignore
/// impl TryFrom<Event> for StateChangedEvent {
/// type Error = AtspiError;
/// fn try_from(generic_event: Event) -> Result<StateChangedEvent, Self::Error> {
/// if let Event::Object(ObjectEvents::StateChanged(specific_event)) = generic_event {
/// Ok(specific_event)
/// } else {
/// Err(AtspiError::Conversion("Invalid type"))
/// }
/// }
/// }
/// ```
macro_rules! impl_try_from_event_for_user_facing_type {
($inner_type:ty, $inner_variant:path, $outer_variant:path) => {
impl TryFrom<Event> for $inner_type {
type Error = AtspiError;
fn try_from(generic_event: Event) -> Result<$inner_type, Self::Error> {
if let $outer_variant($inner_variant(specific_event)) = generic_event {
Ok(specific_event)
} else {
Err(AtspiError::Conversion("Invalid type"))
}
}
}
};
}
/// Implements the `TryFrom` trait for a given event type.
/// Converts a user facing event type into a `zbus::Message`.
///
/// # Example
/// ```ignore
/// impl_to_dbus_message!(StateChangedEvent);
/// ```
/// expands to:
///
/// ```ignore
/// impl TryFrom<StateChangedEvent> for zbus::Message {
/// type Error = AtspiError;
/// fn try_from(event: StateChangedEvent) -> Result<Self, Self::Error> {
/// Ok(zbus::Message::signal(
/// event.path(),
/// StateChangedEvent::DBUS_INTERFACE,
/// StateChangedEvent::DBUS_MEMBER,
/// )?
/// .sender(event.sender())?
/// .build(&event.body())?)
/// }
/// }
///
macro_rules! impl_to_dbus_message {
($type:ty) => {
#[cfg(feature = "zbus")]
impl TryFrom<$type> for zbus::Message {
type Error = AtspiError;
fn try_from(event: $type) -> Result<Self, Self::Error> {
Ok(zbus::Message::signal(
event.path(),
<$type as BusProperties>::DBUS_INTERFACE,
<$type as BusProperties>::DBUS_MEMBER,
)?
.sender(event.sender().to_string())?
.build(&event.body())?)
}
}
};
}
/// Implements the `TryFrom` trait for a given event type.
/// Converts a `zbus::Message` into a user facing event type.
///
/// # Example
/// ```ignore
/// impl_from_dbus_message!(StateChangedEvent);
/// ```
/// expands to:
///
/// ```ignore
/// impl TryFrom<&zbus::Message> for StateChangedEvent {
/// type Error = AtspiError;
/// fn try_from(msg: &zbus::Message) -> Result<Self, Self::Error> {
/// if msg.header().interface().ok_or(AtspiError::MissingInterface)? != StateChangedEvent::DBUS_INTERFACE {
/// return Err(AtspiError::InterfaceMatch(format!("The interface {} does not match the signal's interface: {}",
/// msg.header().interface().unwrap(),
/// StateChangedEvent::DBUS_INTERFACE)));
/// }
/// if msg.header().member().ok_or(AtspiError::MissingMember)? != StateChangedEvent::DBUS_MEMBER {
/// return Err(AtspiError::MemberMatch(format!("The member {} does not match the signal's member: {}",
/// msg.header().member().unwrap(),
/// StateChangedEvent::DBUS_MEMBER)));
/// }
/// StateChangedEvent::from_message_parts(msg.try_into()?, msg.body().try_into()?)
/// }
/// }
/// ```
///
/// There is also a variant that can be used for events whose [`crate::events::BusProperties::Body`] is not
/// [`crate::events::EventBodyOwned`]. You can call this by setting the second parameter to `Explicit`.
macro_rules! impl_from_dbus_message {
($type:ty) => {
impl_from_dbus_message!($type, Auto);
};
($type:ty, Auto) => {
#[cfg(feature = "zbus")]
impl TryFrom<&zbus::Message> for $type {
type Error = AtspiError;
fn try_from(msg: &zbus::Message) -> Result<Self, Self::Error> {
let header = msg.header();
if header.interface().ok_or(AtspiError::MissingInterface)?
!= <$type as BusProperties>::DBUS_INTERFACE
{
return Err(AtspiError::InterfaceMatch(format!(
"The interface {} does not match the signal's interface: {}",
header.interface().unwrap(),
<$type as BusProperties>::DBUS_INTERFACE
)));
}
if header.member().ok_or(AtspiError::MissingMember)? != <$type>::DBUS_MEMBER {
return Err(AtspiError::MemberMatch(format!(
"The member {} does not match the signal's member: {}",
// unwrap is safe here because of guard above
header.member().unwrap(),
<$type as BusProperties>::DBUS_MEMBER
)));
}
<$type>::from_message_parts(msg.try_into()?, msg.try_into()?)
}
}
};
($type:ty, Explicit) => {
#[cfg(feature = "zbus")]
impl TryFrom<&zbus::Message> for $type {
type Error = AtspiError;
fn try_from(msg: &zbus::Message) -> Result<Self, Self::Error> {
let header = msg.header();
if header.interface().ok_or(AtspiError::MissingInterface)?
!= <$type as BusProperties>::DBUS_INTERFACE
{
return Err(AtspiError::InterfaceMatch(format!(
"The interface {} does not match the signal's interface: {}",
header.interface().unwrap(),
<$type as BusProperties>::DBUS_INTERFACE
)));
}
if header.member().ok_or(AtspiError::MissingMember)? != <$type>::DBUS_MEMBER {
return Err(AtspiError::MemberMatch(format!(
"The member {} does not match the signal's member: {}",
// unwrap is safe here because of guard above
header.member().unwrap(),
<$type as BusProperties>::DBUS_MEMBER
)));
}
<$type>::from_message_parts(
msg.try_into()?,
msg.body().deserialize::<<$type as BusProperties>::Body>()?,
)
}
}
};
}
// We decorate the macro with a `#[cfg(test)]` attribute.
// This prevents Clippy from complaining about the macro not being used.
// It is being used, but only in test mode.
//
/// Tests `Default` and `BusProperties::from_message_parts` for a given event struct.
///
/// Obtains a default for the given event struct.
/// Asserts that the path and sender are the default.
///
/// Breaks the struct down into item (the associated object) and body.
/// Then tests `BusProperties::from_message_parts` with the item and body.
#[cfg(test)]
macro_rules! generic_event_test_case {
($type:ty) => {
#[test]
fn generic_event_uses() {
let struct_event = <$type>::default();
assert_eq!(struct_event.path().as_str(), "/org/a11y/atspi/accessible/null");
assert_eq!(struct_event.sender().as_str(), ":0.0");
let item = struct_event.item.clone();
let body = struct_event.body();
let build_struct = <$type>::from_message_parts(item, body)
.expect("<$type as Default>'s parts should build a valid ObjectRef");
assert_eq!(struct_event, build_struct);
}
};
}
// We decorate the macro with a `#[cfg(test)]` attribute.
// This prevents Clippy from complaining about the macro not being used.
// It is being used, but only in test mode.
//
/// Tests conversion to and from the `Event` enum.
///
/// Obtains a default for the given event struct.
/// Converts the struct into the `Event` enum, wrapping the struct.
/// Converts the `Event` enum into the given event struct.
/// Asserts that the original struct and the converted struct are equal.
#[cfg(test)]
macro_rules! event_enum_test_case {
($type:ty) => {
#[test]
fn event_enum_conversion() {
let struct_event = <$type>::default();
let event = Event::from(struct_event.clone());
let struct_event_back = <$type>::try_from(event)
.expect("Should convert event enum into specific event type because it was created from it. Check the `impl_from_interface_event_enum_for_event` macro");
assert_eq!(struct_event, struct_event_back);
}
};
}
/// Tests transparency of the `EventTypeProperties` and `EventProperties` trait on the `Event` wrapper type.
///
/// Obtains a default for the given event struct.
/// Converts the struct into the `Event` enum, wrapping the struct.
/// Checks the equality of all four functions defined in the `EventTypeProiperties` and `EventProperties` traits:
///
/// - `member`
/// - `interface`
/// - `registry_string`
/// - `match_rule`
/// - `path`
/// - `sender`
///
/// It is imperitive that these items come through with no modifications from the wrappers.
///
#[cfg(test)]
macro_rules! event_enum_transparency_test_case {
($type:ty) => {
#[test]
fn event_enum_transparency_test_case() {
let specific_event = <$type>::default();
let generic_event = Event::from(specific_event.clone());
assert_eq!(
specific_event.member(),
generic_event.member(),
"DBus member strings do not match."
);
assert_eq!(
specific_event.interface(),
generic_event.interface(),
"Registry interfaces do not match."
);
assert_eq!(
specific_event.registry_string(),
generic_event.registry_string(),
"Registry strings do not match."
);
assert_eq!(
specific_event.match_rule(),
generic_event.match_rule(),
"Match rule strings do not match."
);
assert_eq!(specific_event.path(), generic_event.path(), "Pathsdo not match.");
assert_eq!(specific_event.sender(), generic_event.sender(), "Senders do not match.");
}
};
}
#[cfg(test)]
macro_rules! zbus_message_qtspi_test_case {
($type:ty, Auto) => {
#[cfg(feature = "zbus")]
#[test]
fn zbus_message_conversion_qtspi() {
// in the case that the body type is EventBodyOwned, we need to also check successful
// conversion from a QSPI-style body.
let ev = <$type>::default();
let qt: crate::events::EventBodyQT = ev.body().into();
let msg = zbus::Message::signal(
ev.path(),
ev.interface(),
ev.member(),
)
.unwrap()
.sender(":0.0")
.unwrap()
.build(&(qt,))
.unwrap();
<$type>::try_from(&msg).expect("Should be able to use an EventBodyQT for any type whose BusProperties::Body = EventBodyOwned");
}
};
($type:ty, Explicit) => {};
}
// We decorate the macro with a `#[cfg(test)]` attribute.
// This prevents Clippy from complaining about the macro not being used.
// It is being used, but only in test mode.
//
/// As of writing, this macro is expanded only once: in the `event_test_cases!` macro.
#[cfg(test)]
macro_rules! zbus_message_test_case {
($type:ty) => {
zbus_message_test_case!($type, Auto);
};
($type:ty, $extra:tt) => {
zbus_message_qtspi_test_case!($type, $extra);
#[cfg(feature = "zbus")]
#[test]
fn zbus_msg_conversion_to_specific_event_type() {
let struct_event = <$type>::default();
let msg: zbus::Message = zbus::Message::try_from(struct_event.clone())
.expect("Should convert a `$type::default()` into a message. Check the `impl_to_dbus_message` macro .");
let struct_event_back =
<$type>::try_from(&msg).expect("Should convert from `$type::default()` originated `Message` back into a specific event type. Check the `impl_from_dbus_message` macro.");
assert_eq!(struct_event, struct_event_back, "Events converted into a message and back must be the same");
}
#[cfg(feature = "zbus")]
#[test]
fn zbus_msg_conversion_to_event_enum_type() {
let struct_event = <$type>::default();
let msg: zbus::Message = zbus::Message::try_from(struct_event.clone()).expect("Should convert a `$type::default()` into a message. Check the `impl_to_dbus_message` macro .");
let event_enum_back =
Event::try_from(&msg).expect("Should convert a from `$type::default()` built `Message` into an event enum. Check the `impl_from_dbus_message` macro .");
let event_enum: Event = struct_event.into();
assert_eq!(event_enum, event_enum_back);
}
// make want to consider parameterized tests here, no need for fuzz testing, but one level lower than that may be nice
// try having a matching member, matching interface, path, or body type, but that has some other piece which is not right
#[cfg(feature = "zbus")]
#[test]
#[should_panic(expected = "Should panic")]
fn zbus_msg_conversion_failure_fake_msg() -> () {
let fake_msg = zbus::Message::signal(
"/org/a11y/sixtynine/fourtwenty",
"org.a11y.atspi.technically.valid",
"MadeUpMember",
)
.unwrap()
.sender(":0.0")
.unwrap()
.build(&())
.unwrap();
let event = <$type>::try_from(&fake_msg);
event.expect("Should panic");
}
#[cfg(feature = "zbus")]
#[test]
#[should_panic(expected = "Should panic")]
fn zbus_msg_conversion_failure_correct_interface() -> () {
let fake_msg = zbus::Message::signal(
"/org/a11y/sixtynine/fourtwenty",
<$type as BusProperties>::DBUS_INTERFACE,
"MadeUpMember",
)
.unwrap()
.sender(":0.0")
.unwrap()
.build(&())
.unwrap();
let event = <$type>::try_from(&fake_msg);
event.expect("Should panic");
}
#[cfg(feature = "zbus")]
#[test]
#[should_panic(expected = "Should panic")]
fn zbus_msg_conversion_failure_correct_interface_and_member() -> () {
let fake_msg = zbus::Message::signal(
"/org/a11y/sixtynine/fourtwenty",
<$type as BusProperties>::DBUS_INTERFACE,
<$type as BusProperties>::DBUS_MEMBER,
)
.unwrap()
.sender(":0.0")
.unwrap()
.build(&())
.unwrap();
let event = <$type>::try_from(&fake_msg);
event.expect("Should panic");
}
#[cfg(feature = "zbus")]
#[test]
#[should_panic(expected = "Should panic")]
fn zbus_msg_conversion_failure_correct_body() -> () {
let fake_msg = zbus::Message::signal(
"/org/a11y/sixtynine/fourtwenty",
"org.a11y.atspi.accessible.technically.valid",
"FakeMember",
)
.unwrap()
.sender(":0.0")
.unwrap()
.build(&<$type>::default().body())
.unwrap();
let event = <$type>::try_from(&fake_msg);
event.expect("Should panic");
}
#[cfg(feature = "zbus")]
#[test]
#[should_panic(expected = "Should panic")]
fn zbus_msg_conversion_failure_correct_body_and_member() -> () {
let fake_msg = zbus::Message::signal(
"/org/a11y/sixtynine/fourtwenty",
"org.a11y.atspi.accessible.technically.valid",
<$type as BusProperties>::DBUS_MEMBER,
)
.unwrap()
.sender(":0.0")
.unwrap()
.build(&<$type>::default().body())
.unwrap();
let event = <$type>::try_from(&fake_msg);
event.expect("Should panic");
}
};
}
/// Expands to five tests:
///
/// 1. `into_and_try_from_event`
/// 2. `zbus_msg_invalid_interface`
/// 3. `zbus_msg_invalid_member`
/// 4. `zbus_msg_invalid_member_and_interface`
/// 5. `zbus_msg_conversion`
///
/// # Examples
///
/// ```ignore
/// event_wrapper_test_cases!(MouseEvents, AbsEvent);
/// ```
/// In the macro, its first argument `$type` is the event enum type.
/// The second argument `$any_subtype` is the event struct type.
///
/// For each of the types, the macro will create a module with the name `events_tests_{foo}`
/// where `{foo}` is the snake case of the 'interface enum' name.
macro_rules! event_wrapper_test_cases {
($type:ty, $any_subtype:ty) => {
#[cfg(test)]
#[rename_item::rename(name($type), prefix = "events_tests_", case = "snake")]
mod foo {
use super::{$any_subtype, $type, Event, BusProperties};
#[test]
fn into_and_try_from_event() {
// Create a default event struct from its type's `Default::default()` impl.
let sub_type = <$any_subtype>::default();
// Wrap the event struct in the event enum
let mod_type = <$type>::from(sub_type);
// Wrap the inner event enum into the `Event` enum.
let event = Event::from(mod_type.clone());
// Unwrap the `Event` enum into the inner event enum.
let mod_type2 = <$type>::try_from(event.clone())
.expect("Should convert outer `Event` enum into interface enum because it was created from it. Check the `impl_try_from_event_for_user_facing_event_type` macro");
assert_eq!(
mod_type, mod_type2,
"Events were able to be parsed and encapsulated, but they have changed value"
);
}
#[cfg(feature = "zbus")]
#[test]
#[should_panic(expected = "Should panic")]
fn zbus_msg_invalid_interface() {
let fake_msg = zbus::Message::signal(
"/org/a11y/sixtynine/fourtwenty",
"org.a11y.atspi.technically.valid.lol",
<$any_subtype as BusProperties>::DBUS_MEMBER,
)
.unwrap()
.sender(":0.0")
.unwrap()
.build(&<$any_subtype>::default().body())
.unwrap();
// It is hard to see what eventually is tested here. Let's unravel it:
//
// Below we call `TryFrom<&zbus::Message> for $type` where `$type` the interface enum name. (eg. `MouseEvents`, `ObjectEvents`, etc.) and
// `mod_type` is an 'interface enum' variant (eg. `MouseEvents::Abs(AbsEvent)`).
// This conversion is found in the `/src/events/{iface_name}.rs`` file.
// This conversion in turn leans on the `impl_from_dbus_message` macro.
// In `MouseEvents::Abs(msg.try_into()?)`, it is the `msg.try_into()?` that should fail.
// The `msg.try_into()?` is provided through the `impl_from_dbus_message` macro.
let mod_type = <$type>::try_from(&fake_msg);
mod_type.expect("Should panic");
}
#[cfg(feature = "zbus")]
#[test]
#[should_panic(expected = "Should panic")]
fn zbus_msg_invalid_member() {
let fake_msg = zbus::Message::signal(
"/org/a11y/sixtynine/fourtwenty",
<$any_subtype as BusProperties>::DBUS_INTERFACE,
"FakeFunctionLol",
)
.unwrap()
.sender(":0.0")
.unwrap()
.build(&<$any_subtype>::default().body())
.unwrap();
// As above, the `msg.try_into()?` is provided through the `impl_from_dbus_message` macro.
let mod_type = <$type>::try_from(&fake_msg);
mod_type.expect("Should panic");
}
#[cfg(feature = "zbus")]
#[test]
#[should_panic(expected = "Should panic")]
fn zbus_msg_invalid_member_and_interface() {
let fake_msg = zbus::Message::signal(
"/org/a11y/sixtynine/fourtwenty",
"org.a11y.atspi.technically.allowed",
"FakeFunctionLol",
)
.unwrap()
.sender(":0.0")
.unwrap()
.build(&<$any_subtype>::default().body())
.unwrap();
// As above, the `msg.try_into()?` is provided through the `impl_from_dbus_message` macro.
let mod_type = <$type>::try_from(&fake_msg);
// Note that the non-matching interface is the first error, so the member match error is not reached.
mod_type.expect("Should panic");
}
#[cfg(feature = "zbus")]
#[test]
fn zbus_msg_conversion() {
let valid_msg = zbus::Message::signal(
"/org/a11y/sixtynine/fourtwenty",
<$any_subtype as BusProperties>::DBUS_INTERFACE,
<$any_subtype as BusProperties>::DBUS_MEMBER,
)
.unwrap()
.sender(":0.0")
.unwrap()
.build(&<$any_subtype>::default().body())
.unwrap();
// As above, the `msg.try_into()?` is provided through the `impl_from_dbus_message` macro.
let mod_type = <$type>::try_from(&valid_msg);
mod_type.expect("Should convert from `$any_subtype::default()` built `Message` back into a interface event enum variant wrapping an inner type. Check the `impl_from_dbus_message` macro.");
}
}
};
}
macro_rules! event_test_cases {
($type:ty) => {
event_test_cases!($type, Auto);
};
($type:ty, $qt:tt) => {
#[cfg(test)]
#[rename_item::rename(name($type), prefix = "event_tests_", case = "snake")]
mod foo {
use super::{$type, Event, BusProperties, EventProperties, EventTypeProperties};
generic_event_test_case!($type);
event_enum_test_case!($type);
zbus_message_test_case!($type, $qt);
event_enum_transparency_test_case!($type);
}
assert_impl_all!(
$type: Clone,
std::fmt::Debug,
serde::Serialize,
serde::Deserialize<'static>,
Default,
PartialEq,
Eq,
std::hash::Hash,
crate::EventProperties,
crate::EventTypeProperties,
crate::BusProperties,
);
#[cfg(feature = "zbus")]
assert_impl_all!(zbus::Message: TryFrom<$type>);
};
}