|
| 1 | +using System; |
| 2 | +using GeneralUpdate.Core.Download; |
| 3 | +using GeneralUpdate.Core.Download.Models; |
| 4 | +using GeneralUpdate.Core.Event; |
| 5 | +using Xunit; |
| 6 | + |
| 7 | +namespace CoreTest.Event; |
| 8 | + |
| 9 | +public class EventListenerTests |
| 10 | +{ |
| 11 | + private class TestListener : IUpdateEventListener |
| 12 | + { |
| 13 | + public int AllDownloadCompletedCalls; |
| 14 | + public int DownloadCompletedCalls; |
| 15 | + public int DownloadErrorCalls; |
| 16 | + public int DownloadStatisticsCalls; |
| 17 | + public int UpdateInfoCalls; |
| 18 | + public int ExceptionCalls; |
| 19 | + public int ProgressCalls; |
| 20 | + |
| 21 | + public void OnAllDownloadCompleted(MultiAllDownloadCompletedEventArgs args) |
| 22 | + => AllDownloadCompletedCalls++; |
| 23 | + |
| 24 | + public void OnDownloadCompleted(MultiDownloadCompletedEventArgs args) |
| 25 | + => DownloadCompletedCalls++; |
| 26 | + |
| 27 | + public void OnDownloadError(MultiDownloadErrorEventArgs args) |
| 28 | + => DownloadErrorCalls++; |
| 29 | + |
| 30 | + public void OnDownloadStatistics(MultiDownloadStatisticsEventArgs args) |
| 31 | + => DownloadStatisticsCalls++; |
| 32 | + |
| 33 | + public void OnUpdateInfo(UpdateInfoEventArgs args) |
| 34 | + => UpdateInfoCalls++; |
| 35 | + |
| 36 | + public void OnException(ExceptionEventArgs args) |
| 37 | + => ExceptionCalls++; |
| 38 | + |
| 39 | + public void OnProgress(DownloadProgress progress) |
| 40 | + => ProgressCalls++; |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public void EventManager_AddListener_And_Dispatch() |
| 45 | + { |
| 46 | + var listener = new TestListener(); |
| 47 | + EventManager.Instance.AddListener<MultiAllDownloadCompletedEventArgs>((s, e) => listener.OnAllDownloadCompleted(e)); |
| 48 | + |
| 49 | + var args = new MultiAllDownloadCompletedEventArgs(true, Array.Empty<(object, string)>()); |
| 50 | + EventManager.Instance.Dispatch(this, args); |
| 51 | + |
| 52 | + Assert.Equal(1, listener.AllDownloadCompletedCalls); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public void EventManager_RemoveListener_StopsDispatch() |
| 57 | + { |
| 58 | + var listener = new TestListener(); |
| 59 | + Action<object, MultiDownloadCompletedEventArgs> handler = (s, e) => listener.OnDownloadCompleted(e); |
| 60 | + |
| 61 | + EventManager.Instance.AddListener(handler); |
| 62 | + EventManager.Instance.RemoveListener(handler); |
| 63 | + |
| 64 | + var args = new MultiDownloadCompletedEventArgs(new object(), true); |
| 65 | + EventManager.Instance.Dispatch(this, args); |
| 66 | + |
| 67 | + Assert.Equal(0, listener.DownloadCompletedCalls); |
| 68 | + } |
| 69 | + |
| 70 | + [Fact] |
| 71 | + public void EventManager_MultipleListeners_AllCalled() |
| 72 | + { |
| 73 | + var listener1 = new TestListener(); |
| 74 | + var listener2 = new TestListener(); |
| 75 | + |
| 76 | + EventManager.Instance.AddListener<ExceptionEventArgs>((s, e) => listener1.OnException(e)); |
| 77 | + EventManager.Instance.AddListener<ExceptionEventArgs>((s, e) => listener2.OnException(e)); |
| 78 | + |
| 79 | + var args = new ExceptionEventArgs(new Exception("test"), "test message"); |
| 80 | + EventManager.Instance.Dispatch(this, args); |
| 81 | + |
| 82 | + Assert.Equal(1, listener1.ExceptionCalls); |
| 83 | + Assert.Equal(1, listener2.ExceptionCalls); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void ProgressEventArgs_Constructor() |
| 88 | + { |
| 89 | + var progress = new DownloadProgress("asset.zip", 500, 1000, 50.0, DownloadStatus.Downloading); |
| 90 | + var args = new ProgressEventArgs(progress); |
| 91 | + |
| 92 | + Assert.Same(progress, args.Progress); |
| 93 | + Assert.Equal("asset.zip", args.Progress.AssetName); |
| 94 | + Assert.Equal(500, args.Progress.BytesDownloaded); |
| 95 | + Assert.Equal(1000, args.Progress.TotalBytes); |
| 96 | + Assert.Equal(50.0, args.Progress.Percentage); |
| 97 | + Assert.Equal(DownloadStatus.Downloading, args.Progress.Status); |
| 98 | + } |
| 99 | + |
| 100 | + [Fact] |
| 101 | + public void EventArgs_Types_Constructed() |
| 102 | + { |
| 103 | + var allDone = new MultiAllDownloadCompletedEventArgs(true, Array.Empty<(object, string)>()); |
| 104 | + Assert.True(allDone.IsAllDownloadCompleted); |
| 105 | + |
| 106 | + var downloadDone = new MultiDownloadCompletedEventArgs(new object(), true); |
| 107 | + Assert.NotNull(downloadDone.Version); |
| 108 | + Assert.True(downloadDone.IsComplated); |
| 109 | + |
| 110 | + var ex = new Exception("boom"); |
| 111 | + var err = new MultiDownloadErrorEventArgs(ex, new object()); |
| 112 | + Assert.Same(ex, err.Exception); |
| 113 | + |
| 114 | + var stats = new MultiDownloadStatisticsEventArgs(new object(), TimeSpan.FromSeconds(1), "1MB/s", 1000, 500, 50.0); |
| 115 | + Assert.Equal("1MB/s", stats.Speed); |
| 116 | + Assert.Equal(1000, stats.TotalBytesToReceive); |
| 117 | + } |
| 118 | +} |
0 commit comments