2024-04-19 01:36:11 +02:00
|
|
|
use std::panic;
|
|
|
|
|
|
|
|
use futures::{stream::StreamExt, SinkExt};
|
2024-05-02 20:59:30 +02:00
|
|
|
use mpvipc::{parse_event_property, Mpv, MpvDataType, MpvExt, Property};
|
2024-04-19 01:36:11 +02:00
|
|
|
use serde_json::json;
|
|
|
|
use test_log::test;
|
|
|
|
use tokio::{net::UnixStream, task::JoinHandle};
|
|
|
|
use tokio_util::codec::{Framed, LinesCodec, LinesCodecError};
|
|
|
|
|
|
|
|
fn test_socket(
|
|
|
|
answers: Vec<(bool, String)>,
|
|
|
|
) -> (UnixStream, JoinHandle<Result<(), LinesCodecError>>) {
|
|
|
|
let (socket, server) = UnixStream::pair().unwrap();
|
|
|
|
let join_handle = tokio::spawn(async move {
|
|
|
|
let mut framed = Framed::new(socket, LinesCodec::new());
|
|
|
|
for (unsolicited, answer) in answers {
|
|
|
|
if !unsolicited {
|
|
|
|
framed.next().await;
|
|
|
|
}
|
|
|
|
framed.send(answer).await?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
(server, join_handle)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test(tokio::test)]
|
|
|
|
async fn test_observe_event_successful() {
|
|
|
|
let (server, join_handle) = test_socket(vec![
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
json!({ "request_id": 0, "error": "success" }).to_string(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
false,
|
|
|
|
json!({ "request_id": 0, "error": "success" }).to_string(),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
true,
|
|
|
|
json!({ "data": 64.0, "event": "property-change", "id": 1, "name": "volume" })
|
|
|
|
.to_string(),
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let mpv = Mpv::connect_socket(server).await.unwrap();
|
|
|
|
|
|
|
|
mpv.observe_property(1, "volume").await.unwrap();
|
|
|
|
|
|
|
|
let mpv2 = mpv.clone();
|
|
|
|
tokio::spawn(async move {
|
|
|
|
let event = mpv2.get_event_stream().await.next().await.unwrap().unwrap();
|
|
|
|
|
2024-05-02 20:59:30 +02:00
|
|
|
let data = match parse_event_property(event) {
|
|
|
|
Ok((_, Property::Unknown { name, data })) => {
|
2024-04-19 01:36:11 +02:00
|
|
|
assert_eq!(name, "volume");
|
|
|
|
data
|
|
|
|
}
|
2024-05-02 20:59:30 +02:00
|
|
|
Ok((_, property)) => panic!("{:?}", property),
|
|
|
|
Err(err) => panic!("{:?}", err),
|
2024-04-19 01:36:11 +02:00
|
|
|
};
|
|
|
|
match data {
|
2024-05-03 16:11:31 +02:00
|
|
|
Some(MpvDataType::Double(data)) => assert_eq!(data, 64.0),
|
|
|
|
Some(data) => panic!("Unexpected value: {:?}", data),
|
|
|
|
None => panic!("No data"),
|
2024-04-19 01:36:11 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
mpv.set_property("volume", 64.0).await.unwrap();
|
|
|
|
|
|
|
|
join_handle.await.unwrap().unwrap();
|
|
|
|
}
|