Skip to content

Commit

Permalink
refactors seektest and adds more formats
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdsk committed Oct 4, 2023
1 parent ea1fcc7 commit ca3c29d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 28 deletions.
52 changes: 52 additions & 0 deletions tests/seek.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::io::BufReader;
use std::path::Path;
use std::time::Duration;

use rodio::source::SeekNotSupported;

// hound wav decoder does not support seeking
#[cfg(feature = "hound")]
#[test]
fn seek_not_supported_returns_err() {
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();

let file = std::fs::File::open("assets/music.wav").unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
let res = sink.try_seek(Duration::from_secs(5));

let Err(rodio::source::SeekNotSupported { source }) = res else {
panic!("result of try_seek should be error SourceNotSupported")
};

assert!(source.starts_with("rodio::decoder::wav::WavDecoder"));
}

fn play_and_seek(asset_path: &Path) -> Result<(), SeekNotSupported> {
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();

let file = std::fs::File::open(asset_path).unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
sink.try_seek(Duration::from_secs(2))
}

#[test]
fn seek_returns_err_if_unsupported() {
let formats = ["mp3", "wav", "ogg", "flac", "m4a"].into_iter();
#[cfg(not(feature = "symphonia"))]
let supported = [true, false, false, false, false].into_iter();
#[cfg(feature = "symphonia")]
let supported = [true, true, true, true, true].into_iter();
#[cfg(not(feature = "symphonia"))]
let decoder = ["minimp3", "hound", "lewton", "claxon", "_"].into_iter();
#[cfg(feature = "symphonia")]
let decoder = ["symphonia"].into_iter().cycle();

for ((format, supported), decoder) in formats.zip(supported).zip(decoder) {
println!("trying: {format} by {decoder}, should support seek: {supported}");
let asset = Path::new("assets/music").with_extension(format);
let res = play_and_seek(&asset);
assert_eq!(res.is_ok(), supported);
}
}
28 changes: 0 additions & 28 deletions tests/seek_error.rs

This file was deleted.

0 comments on commit ca3c29d

Please sign in to comment.