File: protocol_shared_retain_count.rs

package info (click to toggle)
rust-coreutils 0.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 505,620 kB
  • sloc: ansic: 103,594; asm: 28,570; sh: 8,910; python: 5,581; makefile: 472; cpp: 97; javascript: 72
file content (32 lines) | stat: -rw-r--r-- 1,049 bytes parent folder | download | duplicates (5)
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
//! Test that AnyProtocol objects have a shared retain count.
//!
//! Separate test because this will likely not work if other tests are running
//! at the same time.

use objc2::runtime::{AnyObject, NSObject, NSObjectProtocol};
use objc2::{Message, ProtocolType};

#[test]
#[cfg_attr(
    feature = "gnustep-1-7",
    ignore = "Protocols don't implement isKindOfClass: on GNUStep"
)]
#[cfg_attr(
    all(target_os = "macos", target_arch = "x86"),
    ignore = "protocols are not NSObject subclasses in the old runtime"
)]
fn protocol_has_shared_retain_count() {
    let obj: &AnyObject = <dyn NSObjectProtocol>::protocol().unwrap().as_ref();
    let obj = obj.downcast_ref::<NSObject>().unwrap();

    assert_eq!(obj.retainCount(), 1);
    let obj2 = obj.retain();
    assert_eq!(obj.retainCount(), 2);
    drop(obj2);
    assert_eq!(obj.retainCount(), 1);

    let obj2: &AnyObject = <dyn NSObjectProtocol>::protocol().unwrap().as_ref();
    assert_eq!(obj.retainCount(), 1);
    let _obj2 = obj2.retain();
    assert_eq!(obj.retainCount(), 2);
}