|
2 | 2 | #nullable enable |
3 | 3 |
|
4 | 4 | using System; |
| 5 | +using System.IO; |
| 6 | +using System.Net; |
| 7 | +using System.Net.Sockets; |
| 8 | +using System.Text; |
| 9 | +using System.Threading; |
5 | 10 | using System.Threading.Tasks; |
6 | 11 | using Microsoft.UI.Xaml.Controls; |
7 | 12 | using Microsoft.UI.Xaml.Media.Imaging; |
| 13 | +using Private.Infrastructure; |
8 | 14 | using Uno.UI.RuntimeTests.Helpers; |
| 15 | +using Windows.Storage; |
9 | 16 | using static Private.Infrastructure.TestServices; |
10 | 17 |
|
11 | 18 | namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Media_Imaging; |
@@ -64,5 +71,218 @@ private static async Task LoadAsync(Panel panel, Uri uri, int decodePixelWidth, |
64 | 71 |
|
65 | 72 | await tcs.Task.WaitAsync(TimeSpan.FromSeconds(10)); |
66 | 73 | } |
| 74 | + |
| 75 | + [TestMethod] |
| 76 | + [PlatformCondition(ConditionMode.Include, RuntimeTestPlatforms.SkiaDesktop)] |
| 77 | + [GitHubWorkItem("https://github.com/unoplatform/uno/issues/25096")] |
| 78 | + public async Task When_Shared_Download_And_First_Requester_Cancelled() |
| 79 | + { |
| 80 | + using var server = await GatedImageServer.StartAsync(); |
| 81 | + |
| 82 | + var first = new BitmapImage(server.Uri); |
| 83 | + var second = new BitmapImage(server.Uri); |
| 84 | + var firstResult = TrackOpen(first); |
| 85 | + var secondResult = TrackOpen(second); |
| 86 | + |
| 87 | + // Subscribing an Image is what opens the source; both join the same cached download. |
| 88 | + var firstImage = new Image { Source = first }; |
| 89 | + var secondImage = new Image { Source = second }; |
| 90 | + |
| 91 | + await WindowHelper.WaitFor(() => server.RequestCount >= 1, 5000, "the download never started"); |
| 92 | + |
| 93 | + // Cancels the first requester while the shared download is in flight (what a recycled container does). |
| 94 | + first.UriSource = null; |
| 95 | + |
| 96 | + server.ReleaseResponses(); |
| 97 | + |
| 98 | + Assert.IsTrue(await secondResult.WaitAsync(TimeSpan.FromSeconds(10)), "The second BitmapImage should open"); |
| 99 | + Assert.AreEqual(100, second.PixelWidth); |
| 100 | + Assert.AreEqual(1, server.RequestCount, "Both sources should share one download"); |
| 101 | + Assert.IsFalse(firstResult.IsCompleted, "The cancelled BitmapImage should raise neither ImageOpened nor ImageFailed"); |
| 102 | + |
| 103 | + GC.KeepAlive(firstImage); |
| 104 | + GC.KeepAlive(secondImage); |
| 105 | + } |
| 106 | + |
| 107 | + [TestMethod] |
| 108 | + [PlatformCondition(ConditionMode.Include, RuntimeTestPlatforms.SkiaDesktop)] |
| 109 | + [GitHubWorkItem("https://github.com/unoplatform/uno/issues/25096")] |
| 110 | + public async Task When_Only_Requester_Cancelled_Then_Same_Uri_Reopened() |
| 111 | + { |
| 112 | + using var server = await GatedImageServer.StartAsync(); |
| 113 | + |
| 114 | + var first = new BitmapImage(server.Uri); |
| 115 | + var firstImage = new Image { Source = first }; |
| 116 | + |
| 117 | + await WindowHelper.WaitFor(() => server.RequestCount >= 1, 5000, "the download never started"); |
| 118 | + |
| 119 | + first.UriSource = null; |
| 120 | + server.ReleaseResponses(); |
| 121 | + |
| 122 | + var second = new BitmapImage(server.Uri); |
| 123 | + var secondResult = TrackOpen(second); |
| 124 | + var secondImage = new Image { Source = second }; |
| 125 | + |
| 126 | + Assert.IsTrue(await secondResult.WaitAsync(TimeSpan.FromSeconds(10)), "A later BitmapImage for the same Uri should open"); |
| 127 | + Assert.AreEqual(100, second.PixelWidth); |
| 128 | + |
| 129 | + GC.KeepAlive(firstImage); |
| 130 | + GC.KeepAlive(secondImage); |
| 131 | + } |
| 132 | + |
| 133 | + [TestMethod] |
| 134 | + [PlatformCondition(ConditionMode.Include, RuntimeTestPlatforms.SkiaDesktop)] |
| 135 | + [GitHubWorkItem("https://github.com/unoplatform/uno/issues/25096")] |
| 136 | + public async Task When_Shared_Download_Failed_Then_Same_Uri_Reopened() |
| 137 | + { |
| 138 | + using var server = await GatedImageServer.StartAsync(); |
| 139 | + server.FailNextRequest = true; |
| 140 | + server.ReleaseResponses(); |
| 141 | + |
| 142 | + var first = new BitmapImage(server.Uri); |
| 143 | + var firstResult = TrackOpen(first); |
| 144 | + var firstImage = new Image { Source = first }; |
| 145 | + |
| 146 | + Assert.IsFalse(await firstResult.WaitAsync(TimeSpan.FromSeconds(10)), "The first BitmapImage should fail"); |
| 147 | + |
| 148 | + // The entry is dropped by a continuation on the load, so poll rather than assume it ran before ImageFailed. |
| 149 | + await TestHelper.RetryAssert( |
| 150 | + async () => Assert.IsNull(await BitmapImage.GetCachedImageDataTaskForTesting(server.Uri, null, null), "The failed load should have been dropped from the cache"), |
| 151 | + count: 200); |
| 152 | + |
| 153 | + var second = new BitmapImage(server.Uri); |
| 154 | + var secondResult = TrackOpen(second); |
| 155 | + var secondImage = new Image { Source = second }; |
| 156 | + |
| 157 | + Assert.IsTrue(await secondResult.WaitAsync(TimeSpan.FromSeconds(10)), "A failed download must not be cached: the next BitmapImage should download again and open"); |
| 158 | + Assert.AreEqual(100, second.PixelWidth); |
| 159 | + Assert.AreEqual(2, server.RequestCount, "The second BitmapImage should have downloaded again"); |
| 160 | + |
| 161 | + GC.KeepAlive(firstImage); |
| 162 | + GC.KeepAlive(secondImage); |
| 163 | + } |
| 164 | + |
| 165 | + private static Task<bool> TrackOpen(BitmapImage image) |
| 166 | + { |
| 167 | + var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously); |
| 168 | + image.ImageOpened += (_, _) => tcs.TrySetResult(true); |
| 169 | + image.ImageFailed += (_, _) => tcs.TrySetResult(false); |
| 170 | + return tcs.Task; |
| 171 | + } |
| 172 | + |
| 173 | + /// <summary> |
| 174 | + /// Minimal loopback HTTP server that sends the response headers immediately but holds the image bytes |
| 175 | + /// until <see cref="ReleaseResponses"/>, so a test can act while a download is deterministically in flight. |
| 176 | + /// With <see cref="FailNextRequest"/> set, the next request gets an HTTP 500 instead of the image. |
| 177 | + /// </summary> |
| 178 | + private sealed class GatedImageServer : IDisposable |
| 179 | + { |
| 180 | + private readonly TcpListener _listener; |
| 181 | + private readonly byte[] _body; |
| 182 | + private readonly TaskCompletionSource _release = new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 183 | + private int _requestCount; |
| 184 | + |
| 185 | + private GatedImageServer(TcpListener listener, byte[] body) |
| 186 | + { |
| 187 | + _listener = listener; |
| 188 | + _body = body; |
| 189 | + Uri = new Uri($"http://127.0.0.1:{((IPEndPoint)listener.LocalEndpoint).Port}/square100.png"); |
| 190 | + _ = AcceptLoopAsync(); |
| 191 | + } |
| 192 | + |
| 193 | + public static async Task<GatedImageServer> StartAsync() |
| 194 | + { |
| 195 | + var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/square100.png")); |
| 196 | + using var stream = await file.OpenStreamForReadAsync(); |
| 197 | + using var memory = new MemoryStream(); |
| 198 | + await stream.CopyToAsync(memory); |
| 199 | + |
| 200 | + var listener = new TcpListener(IPAddress.Loopback, 0); |
| 201 | + listener.Start(); |
| 202 | + return new GatedImageServer(listener, memory.ToArray()); |
| 203 | + } |
| 204 | + |
| 205 | + public Uri Uri { get; } |
| 206 | + |
| 207 | + public int RequestCount => Volatile.Read(ref _requestCount); |
| 208 | + |
| 209 | + public bool FailNextRequest { get; set; } |
| 210 | + |
| 211 | + public void ReleaseResponses() => _release.TrySetResult(); |
| 212 | + |
| 213 | + public void Dispose() |
| 214 | + { |
| 215 | + _release.TrySetResult(); |
| 216 | + _listener.Stop(); |
| 217 | + } |
| 218 | + |
| 219 | + private async Task AcceptLoopAsync() |
| 220 | + { |
| 221 | + try |
| 222 | + { |
| 223 | + while (true) |
| 224 | + { |
| 225 | + var client = await _listener.AcceptTcpClientAsync(); |
| 226 | + _ = HandleAsync(client); |
| 227 | + } |
| 228 | + } |
| 229 | + catch (ObjectDisposedException) |
| 230 | + { |
| 231 | + } |
| 232 | + catch (SocketException) |
| 233 | + { |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + private async Task HandleAsync(TcpClient client) |
| 238 | + { |
| 239 | + try |
| 240 | + { |
| 241 | + using var _ = client; |
| 242 | + using var stream = client.GetStream(); |
| 243 | + |
| 244 | + var request = new byte[8192]; |
| 245 | + var read = 0; |
| 246 | + while (!Encoding.ASCII.GetString(request, 0, read).Contains("\r\n\r\n")) |
| 247 | + { |
| 248 | + var count = await stream.ReadAsync(request, read, request.Length - read); |
| 249 | + if (count == 0) |
| 250 | + { |
| 251 | + return; |
| 252 | + } |
| 253 | + |
| 254 | + read += count; |
| 255 | + } |
| 256 | + |
| 257 | + Interlocked.Increment(ref _requestCount); |
| 258 | + |
| 259 | + if (FailNextRequest) |
| 260 | + { |
| 261 | + FailNextRequest = false; |
| 262 | + await stream.WriteAsync(Encoding.ASCII.GetBytes("HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\nConnection: close\r\n\r\n")); |
| 263 | + await stream.FlushAsync(); |
| 264 | + return; |
| 265 | + } |
| 266 | + |
| 267 | + var headers = $"HTTP/1.1 200 OK\r\nContent-Type: image/png\r\nContent-Length: {_body.Length}\r\nConnection: close\r\n\r\n"; |
| 268 | + await stream.WriteAsync(Encoding.ASCII.GetBytes(headers)); |
| 269 | + await stream.FlushAsync(); |
| 270 | + |
| 271 | + await _release.Task; |
| 272 | + |
| 273 | + await stream.WriteAsync(_body); |
| 274 | + await stream.FlushAsync(); |
| 275 | + } |
| 276 | + catch (IOException) |
| 277 | + { |
| 278 | + } |
| 279 | + catch (ObjectDisposedException) |
| 280 | + { |
| 281 | + } |
| 282 | + catch (SocketException) |
| 283 | + { |
| 284 | + } |
| 285 | + } |
| 286 | + } |
67 | 287 | } |
68 | 288 | #endif |
0 commit comments