MediaTests.cs 3.51 KB
Newer Older
Martin Finkel's avatar
Martin Finkel committed
1 2
using System;
using System.IO;
3
using System.Linq;
Martin Finkel's avatar
Martin Finkel committed
4 5
using NUnit.Framework;
using VideoLAN.LibVLC;
6
using Media = VideoLAN.LibVLC.Media;
Martin Finkel's avatar
Martin Finkel committed
7

8
namespace LibVLCSharp.Tests
Martin Finkel's avatar
Martin Finkel committed
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
{
    [TestFixture]
    public class MediaTests
    {
        [Test]
        public void CreateMedia()
        {
            var instance = new Instance();

            var media = new Media(instance, Path.GetTempFileName(), Media.FromType.FromPath);

            Assert.AreNotEqual(IntPtr.Zero, media.NativeReference);
        }

        [Test]
        public void CreateMediaFail()
        {
            Assert.Throws<ArgumentNullException>(() => new Media(null, Path.GetTempFileName(), Media.FromType.FromPath));
            Assert.Throws<ArgumentNullException>(() => new Media(new Instance(), string.Empty, Media.FromType.FromPath));
        }

        [Test]
        public void ReleaseMedia()
        {
            var media = new Media(new Instance(), Path.GetTempFileName(), Media.FromType.FromPath);

            media.Dispose();

            Assert.AreEqual(IntPtr.Zero, media.NativeReference);
        }

        [Test]
        public void CreateMediaFromStream()
        {
            var media = new Media(new Instance(), new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));
            Assert.AreNotEqual(IntPtr.Zero, media.NativeReference);
        }

        [Test]
        public void AddOption()
        {
            var media = new Media(new Instance(), new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));
            media.AddOption("-sout-all");
        }
53 54 55 56 57 58

        [Test]
        public void CreateRealMedia()
        {
            var instance = new Instance();
            var media = new Media(instance, RealMediaPath, Media.FromType.FromPath);
59
            
60 61
            Assert.False(media.IsParsed);
            media.Parse();
62 63

            //await media.ParseAsync();
64 65 66 67 68 69 70 71 72 73 74 75
            Assert.True(media.IsParsed);
            Assert.NotZero(media.Duration);
            Assert.NotZero(media.Tracks.First().Data.Audio.Channels);
            Assert.AreEqual(Media.MediaParsedStatus.Done, media.ParsedStatus);
            Assert.AreEqual(Media.MediaType.File, media.Type);
        }

        string RealMediaPath
        {
            get
            {
                var dir = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
76 77 78
                //var binDir = Path.Combine(dir, "..\\..\\..\\");
                var files = Directory.GetFiles(dir);
                return files.First(f => f.Contains("Klang"));
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
            }
        }

        [Test]
        public void Duplicate()
        {
            var media = new Media(new Instance(), new FileStream(Path.GetTempFileName(), FileMode.OpenOrCreate));
            var duplicate = media.Duplicate();
            Assert.AreNotEqual(duplicate.NativeReference, media.NativeReference);
        }

        [Test]
        public void CreateMediaFromFileStream()
        {
            // TODO: fix this.
94 95 96
            var media = new Media(new Instance(), new FileStream(RealMediaPath, FileMode.Open, FileAccess.Read, FileShare.Read));
            media.Parse();
            Assert.NotZero(media.Tracks.First().Data.Audio.Channels);
97 98 99 100 101 102 103 104 105 106 107
        }

        [Test]
        public void SetMetadata()
        {
            var media = new Media(new Instance(), Path.GetTempFileName(), Media.FromType.FromPath);
            const string test = "test";
            media.SetMeta(Media.MetadataType.ShowName, test);
            Assert.True(media.SaveMeta());
            Assert.AreEqual(test, media.Meta(Media.MetadataType.ShowName));
        }
Martin Finkel's avatar
Martin Finkel committed
108 109
    }
}