DialogTests.cs 4.63 KB
Newer Older
Martin Finkel's avatar
Martin Finkel committed
1
using System.Threading.Tasks;
Martin Finkel's avatar
Martin Finkel committed
2
using LibVLCSharp.Shared;
Martin Finkel's avatar
Martin Finkel committed
3 4 5 6 7
using NUnit.Framework;

namespace LibVLCSharp.Tests
{
    [TestFixture]
8
    public class DialogTests : BaseSetup
Martin Finkel's avatar
Martin Finkel committed
9 10 11 12 13 14
    {
        const string UrlRequireAuth = "http://httpbin.org/basic-auth/user/passwd";
        const string Username = "username";
        const string Password = "password";

        [Test]
Martin Finkel's avatar
Martin Finkel committed
15 16
        [Retry(3)]
        [Ignore("requires network calls that may fail when run from CI")]
Martin Finkel's avatar
Martin Finkel committed
17 18 19 20
        public async Task PostLogin()
        {
            var tcs = new TaskCompletionSource<bool>();

21
            _libVLC.SetDialogHandlers((title, text) => Task.CompletedTask,
Martin Finkel's avatar
Martin Finkel committed
22 23 24 25 26
                (dialog, title, text, username, store, token) =>
                {
                    // show UI dialog
                    // On "OK" call PostLogin
                    dialog.PostLogin(Username, Password, false);
Martin Finkel's avatar
Martin Finkel committed
27
                    tcs.TrySetResult(true);
Martin Finkel's avatar
Martin Finkel committed
28 29 30 31 32 33
                    return Task.CompletedTask;
                },
                (dialog, title, text, type, cancelText, actionText, secondActionText, token) => Task.CompletedTask,
                (dialog, title, text, indeterminate, position, cancelText, token) => Task.CompletedTask,
                (dialog, position, text) => Task.CompletedTask);

34
            var mp = new MediaPlayer(_libVLC)
Martin Finkel's avatar
Martin Finkel committed
35
            {
36
                Media = new Media(_libVLC, UrlRequireAuth, FromType.FromLocation)
Martin Finkel's avatar
Martin Finkel committed
37 38 39 40 41 42 43 44 45
            };

            mp.Play();

            await tcs.Task;
            Assert.True(tcs.Task.Result);
        }

        [Test]
Martin Finkel's avatar
Martin Finkel committed
46 47
        [Retry(3)]
        [Ignore("requires network calls that may fail when run from CI")]
48
        public async Task ShouldThrowIfPostLoginsTwice()
Martin Finkel's avatar
Martin Finkel committed
49 50 51
        {
            var tcs = new TaskCompletionSource<bool>();

52
            _libVLC.SetDialogHandlers((title, text) => Task.CompletedTask,
Martin Finkel's avatar
Martin Finkel committed
53 54 55 56
                (dialog, title, text, username, store, token) =>
                {
                    dialog.PostLogin(Username, Password, false);
                    Assert.Throws<VLCException>(() => dialog.PostLogin(Username, Password, false), "Calling method on dismissed Dialog instance");
Martin Finkel's avatar
Martin Finkel committed
57
                    tcs.TrySetResult(true);
Martin Finkel's avatar
Martin Finkel committed
58 59 60 61 62
                    return Task.CompletedTask;
                },
                (dialog, title, text, type, cancelText, actionText, secondActionText, token) => Task.CompletedTask,
                (dialog, title, text, indeterminate, position, cancelText, token) => Task.CompletedTask,
                (dialog, position, text) => Task.CompletedTask);
63

64
            var mp = new MediaPlayer(_libVLC)
65
            {
66
                Media = new Media(_libVLC, UrlRequireAuth, FromType.FromLocation)
67 68 69 70 71 72 73 74 75
            };

            mp.Play();

            await tcs.Task;
            Assert.True(tcs.Task.Result);
        }

        [Test]
Martin Finkel's avatar
Martin Finkel committed
76 77
        [Retry(3)]
        [Ignore("requires network calls that may fail when run from CI")]
78 79 80 81
        public async Task ShouldNotThrowAndReturnFalseIfDimissingTwice()
        {
            var tcs = new TaskCompletionSource<bool>();

82
            _libVLC.SetDialogHandlers((title, text) => Task.CompletedTask,
83 84 85 86 87 88
                (dialog, title, text, username, store, token) =>
                {
                    var result = dialog.Dismiss();
                    Assert.IsTrue(result);
                    result = dialog.Dismiss();
                    Assert.IsFalse(result);
Martin Finkel's avatar
Martin Finkel committed
89
                    tcs.TrySetResult(true);
90 91 92 93 94
                    return Task.CompletedTask;
                },
                (dialog, title, text, type, cancelText, actionText, secondActionText, token) => Task.CompletedTask,
                (dialog, title, text, indeterminate, position, cancelText, token) => Task.CompletedTask,
                (dialog, position, text) => Task.CompletedTask);
Martin Finkel's avatar
Martin Finkel committed
95

96
            var mp = new MediaPlayer(_libVLC)
Martin Finkel's avatar
Martin Finkel committed
97
            {
98
                Media = new Media(_libVLC, UrlRequireAuth, FromType.FromLocation)
Martin Finkel's avatar
Martin Finkel committed
99 100 101 102 103 104 105 106 107 108 109
            };

            mp.Play();

            await tcs.Task;
            Assert.True(tcs.Task.Result);
        }

        [Test]
        public void ShouldUnsetDialogHandlersWhenInstanceDisposed()
        {
110
            _libVLC.SetDialogHandlers((title, text) => Task.CompletedTask,
Martin Finkel's avatar
Martin Finkel committed
111 112 113 114 115
                (dialog, title, text, username, store, token) => Task.CompletedTask,
                (dialog, title, text, type, cancelText, actionText, secondActionText, token) => Task.CompletedTask,
                (dialog, title, text, indeterminate, position, cancelText, token) => Task.CompletedTask,
                (dialog, position, text) => Task.CompletedTask);

116
            Assert.True(_libVLC.DialogHandlersSet);
Martin Finkel's avatar
Martin Finkel committed
117

118
            _libVLC.Dispose();
Martin Finkel's avatar
Martin Finkel committed
119

120
            Assert.False(_libVLC.DialogHandlersSet);
Martin Finkel's avatar
Martin Finkel committed
121 122 123
        }
    }
}