chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
 
namespace OpenTap.UnitTests
{
    [TestFixture]
    public class DefaultPictureDataProviderTest
    {
        public class TestPictureProvider : IPictureDataProvider
        {
            public const string Format = "Test Format";
            public const string Source = "NonFileNameSource";
            
            // Go after DefaultPictureDataProvider
            public double Order => 999;
 
            Task<Stream> IPictureDataProvider.GetStream(IPicture picture)
            {
                if (picture.Source != Source) return null;
                var stream = new MemoryStream();
                stream.Write(new byte[] {1,2,3,4,5,6,7,8,9,10}, 0, 10);
                return Task.FromResult<Stream>(stream);
            }
 
            public Task<string> GetFormat(IPicture picture)
            {
                if (picture.Source != Source) return null;
                return Task.FromResult(Format);
            }
        }
 
 
        [Test]
        public async Task TestFilePicture()
        {
            var dir = Path.GetDirectoryName(typeof(DefaultPictureDataProviderTest).Assembly.Location);
            var source = Path.Combine(dir, "Resources/TestImg.png");
 
            var pic = new Picture() {Source = source, Description = "Test Picture"};
 
            {
                // Test file name and file type
                Assert.AreEqual("png", await pic.GetFormat());
            }
 
            var expectedBytes = File.ReadAllBytes(source);
 
            using (var picStream = await pic.GetStream())
            {
                using (var memoryStream = new MemoryStream())
                {
                    picStream.CopyTo(memoryStream);
                    CollectionAssert.AreEqual(expectedBytes, memoryStream.ToArray());
                }
            }
        }
 
        [Test]
        public async Task TestNoFileExtension()
        {
            var source = "abc";
            var content = "def";
            if (File.Exists(source) == false)
                File.WriteAllText(source, content);
 
            var pic = new Picture() {Source = source, Description = "No File Extensions"};
            
            Assert.IsNull(await pic.GetFormat());
            Assert.IsNull(await pic.GetStream());
        }
 
        [Test]
        public async Task TestWebPicture()
        {
            var dir = Path.GetDirectoryName(typeof(DefaultPictureDataProviderTest).Assembly.Location);
            var filename = Path.Combine(dir, "Resources/TestImg.png");
            var fileContent = File.ReadAllBytes(filename);
            
            var listener = new TcpListener(IPAddress.Loopback, 0);
            listener.Start();
            var source = $"http://{IPAddress.Loopback}:{((IPEndPoint)listener.LocalEndpoint).Port}/file.png";
            TapThread.Start(() =>
            {
                for (int i = 0; i < 2; i++)
                {
                    using var client = listener.AcceptTcpClient();
                    using var stream = client.GetStream();
                    var header = $"HTTP/1.0 200 OK\r\nContent-Length: {fileContent.Length}\r\n\r\n";
                    stream.Write(Encoding.UTF8.GetBytes(header), 0, header.Length);
                    stream.Write(fileContent, 0, fileContent.Length);
                    // Ensure the client has finished downloading the resource before this thread's resources are disposed
                    TapThread.Sleep(TimeSpan.FromSeconds(1));
                }
            });
 
            var pic = new Picture() {Source = source, Description = "Test Picture"};
 
            // Ensure filenames and types are correct
            Assert.AreEqual("png", await pic.GetFormat());
 
            byte[] expectedBytes;
            {
                // Read the actual data
                var client = new HttpClient();
                var req = new HttpRequestMessage(HttpMethod.Get, source);
                var data = client.SendAsync(req).Result;
                expectedBytes = data.Content.ReadAsByteArrayAsync().Result;
                CollectionAssert.AreEqual(fileContent, expectedBytes, "Files are not equal");
            }
 
            using (var picStream = await pic.GetStream())
            {
                using (var memoryStream = new MemoryStream())
                {
                    picStream.CopyTo(memoryStream);
                    CollectionAssert.AreEqual(expectedBytes, memoryStream.ToArray());
                }
            }
            
            listener.Stop();
        }
 
        [Test]
        public async Task TestOrder()
        {
            var source = TestPictureProvider.Source;
            var pic = new Picture() {Source = source, Description = "Non-existent"};
            
            Assert.AreEqual(TestPictureProvider.Format, await pic.GetFormat());
 
            var bytes = await pic.GetStream() as MemoryStream;
            CollectionAssert.AreEqual(new byte[]{1,2,3,4,5,6,7,8,9,10}, bytes.ToArray());
 
        }
    }
}