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
#[allow(unused_imports)]
use crate::{
    run_daemon,
    utils::{AUDIO, BASE, BLUETOOTH, DBUS_PATH, WIRELESS},
};
use dbus::{
    arg::{AppendAll, ReadAll},
    blocking::Connection,
};
#[cfg(test)]
use re_set_lib::audio::audio_structures::Sink;
#[allow(unused_imports)]
use re_set_lib::audio::audio_structures::{InputStream, OutputStream, Source};
use std::{
    hint,
    sync::{
        atomic::{AtomicU16, Ordering},
        Once,
    },
};
#[allow(unused_imports)]
use std::{thread, time::Duration};
use tokio::runtime;

#[allow(dead_code)]
static START_DAEMON: Once = Once::new();
static COUNTER: AtomicU16 = AtomicU16::new(0);

#[allow(dead_code)]
fn call_session_dbus_method<
    I: AppendAll + Sync + Send + 'static,
    O: ReadAll + Sync + Send + 'static,
>(
    function: &str,
    proxy_name: &str,
    params: I,
) -> Result<O, dbus::Error> {
    let conn = Connection::new_session();
    let conn = conn.unwrap();
    let proxy = conn.with_proxy(BASE, DBUS_PATH, Duration::from_millis(2000));
    let result: Result<O, dbus::Error> = proxy.method_call(proxy_name, function, params);
    result
}

#[allow(dead_code)]
fn setup() {
    if COUNTER.fetch_add(1, Ordering::SeqCst) < 1 {
        thread::spawn(|| {
            let rt = runtime::Runtime::new().expect("Failed to create runtime");
            rt.spawn(run_daemon());
            while COUNTER.load(Ordering::SeqCst) != 0 {
                hint::spin_loop();
            }
            rt.shutdown_background();
        });
    };
}

#[tokio::test]
async fn test_check() {
    setup();
    thread::sleep(Duration::from_millis(1000));
    let res = call_session_dbus_method::<(), ()>("Check", BASE, ());
    COUNTER.fetch_sub(1, Ordering::SeqCst);
    assert!(res.is_ok());
}

#[tokio::test]
async fn test_audio_listener() {
    setup();
    thread::sleep(Duration::from_millis(1000));
    let res = call_session_dbus_method::<(), ()>("StartAudioListener", AUDIO, ());
    COUNTER.fetch_sub(1, Ordering::SeqCst);
    assert!(res.is_ok());
}

#[tokio::test]
async fn test_wireless_listener() {
    setup();
    thread::sleep(Duration::from_millis(1000));
    let res = call_session_dbus_method::<(), ()>("StartNetworkListener", WIRELESS, ());
    COUNTER.fetch_sub(1, Ordering::SeqCst);
    assert!(res.is_ok());
}

#[tokio::test]
async fn test_bluetooth_listener() {
    setup();
    thread::sleep(Duration::from_millis(1000));
    let res = call_session_dbus_method::<(u32,), ()>("StartBluetoothListener", BLUETOOTH, (5,));
    COUNTER.fetch_sub(1, Ordering::SeqCst);
    assert!(res.is_ok());
}

#[tokio::test]
async fn test_get_sinks() {
    setup();
    thread::sleep(Duration::from_millis(1000));
    let res = call_session_dbus_method::<(), (Vec<Sink>,)>("ListSinks", AUDIO, ());
    COUNTER.fetch_sub(1, Ordering::SeqCst);
    assert!(res.is_ok());
}

#[tokio::test]
async fn test_get_default_sink() {
    setup();
    thread::sleep(Duration::from_millis(1000));
    let res = call_session_dbus_method::<(), (Sink,)>("GetDefaultSink", AUDIO, ());
    COUNTER.fetch_sub(1, Ordering::SeqCst);
    assert!(res.is_ok());
}

#[tokio::test]
async fn test_get_default_source() {
    setup();
    thread::sleep(Duration::from_millis(1000));
    let res = call_session_dbus_method::<(), (Source,)>("GetDefaultSource", AUDIO, ());
    COUNTER.fetch_sub(1, Ordering::SeqCst);
    assert!(res.is_ok());
}

#[tokio::test]
async fn test_get_sources() {
    setup();
    thread::sleep(Duration::from_millis(1000));
    let res = call_session_dbus_method::<(), (Vec<Source>,)>("ListSources", AUDIO, ());
    COUNTER.fetch_sub(1, Ordering::SeqCst);
    assert!(res.is_ok());
}

#[tokio::test]
async fn test_get_input_streams() {
    setup();
    thread::sleep(Duration::from_millis(1000));
    let res = call_session_dbus_method::<(), (Vec<InputStream>,)>("ListInputStreams", AUDIO, ());
    COUNTER.fetch_sub(1, Ordering::SeqCst);
    assert!(res.is_ok());
}

#[tokio::test]
async fn test_get_output_streams() {
    setup();
    thread::sleep(Duration::from_millis(1000));
    let res = call_session_dbus_method::<(), (Vec<OutputStream>,)>("ListOutputStreams", AUDIO, ());
    COUNTER.fetch_sub(1, Ordering::SeqCst);
    assert!(res.is_ok());
}