|
| 1 | +#if HAS_UNO |
| 2 | +using System; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Microsoft.UI.Xaml.Controls; |
| 5 | +using Microsoft.UI.Xaml.Media.Imaging; |
| 6 | +using Private.Infrastructure; |
| 7 | +using Uno.UI.RuntimeTests.Helpers; |
| 8 | +using static Private.Infrastructure.TestServices; |
| 9 | + |
| 10 | +namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls; |
| 11 | + |
| 12 | +[TestClass] |
| 13 | +[RunsOnUIThread] |
| 14 | +[PlatformCondition(ConditionMode.Include, RuntimeTestPlatforms.Skia)] |
| 15 | +public class Given_Image_Release |
| 16 | +{ |
| 17 | + private const string AssetUri = "ms-appx:///Assets/my500x200.jpg"; |
| 18 | + |
| 19 | + [TestMethod] |
| 20 | + [GitHubWorkItem("https://github.com/unoplatform/uno/issues/25114")] |
| 21 | + public async Task When_Unloaded_Then_Decoded_Image_Released() |
| 22 | + { |
| 23 | + // The source owns its decoded data only when the cache does not share it between sources. |
| 24 | + var cacheWasEnabled = FeatureConfiguration.Image.EnableBitmapImageCache; |
| 25 | + FeatureConfiguration.Image.EnableBitmapImageCache = false; |
| 26 | + |
| 27 | + try |
| 28 | + { |
| 29 | + var bitmap = new BitmapImage(new Uri(AssetUri)); |
| 30 | + var image = new Image { Width = 100, Height = 40, Source = bitmap }; |
| 31 | + |
| 32 | + await LoadAndWaitForOpened(image); |
| 33 | + Assert.IsTrue(bitmap.IsOpened, "Pre-condition: the bitmap is decoded while an Image shows it"); |
| 34 | + |
| 35 | + WindowHelper.WindowContent = null; |
| 36 | + await WindowHelper.WaitForIdle(); |
| 37 | + Assert.IsFalse(bitmap.IsOpened, "Nothing displays the bitmap once its only Image is unloaded, so the decoded data must be released"); |
| 38 | + |
| 39 | + // Showing it again decodes it again. |
| 40 | + await LoadAndWaitForOpened(image); |
| 41 | + Assert.IsTrue(bitmap.IsOpened, "An Image that is loaded again must decode its source again"); |
| 42 | + } |
| 43 | + finally |
| 44 | + { |
| 45 | + WindowHelper.WindowContent = null; |
| 46 | + FeatureConfiguration.Image.EnableBitmapImageCache = cacheWasEnabled; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + [TestMethod] |
| 51 | + [GitHubWorkItem("https://github.com/unoplatform/uno/issues/25114")] |
| 52 | + public async Task When_Source_Replaced_Then_Previous_Decoded_Image_Released() |
| 53 | + { |
| 54 | + var cacheWasEnabled = FeatureConfiguration.Image.EnableBitmapImageCache; |
| 55 | + FeatureConfiguration.Image.EnableBitmapImageCache = false; |
| 56 | + |
| 57 | + try |
| 58 | + { |
| 59 | + var first = new BitmapImage(new Uri(AssetUri)); |
| 60 | + var image = new Image { Width = 100, Height = 40, Source = first }; |
| 61 | + |
| 62 | + await LoadAndWaitForOpened(image); |
| 63 | + Assert.IsTrue(first.IsOpened, "Pre-condition: the first bitmap is decoded while the Image shows it"); |
| 64 | + |
| 65 | + var opened = TrackOpened(image); |
| 66 | + image.Source = new BitmapImage(new Uri(AssetUri)); |
| 67 | + await opened.WaitAsync(TimeSpan.FromSeconds(10)); |
| 68 | + |
| 69 | + Assert.IsFalse(first.IsOpened, "The replaced bitmap is shown by nothing, so its decoded data must be released"); |
| 70 | + } |
| 71 | + finally |
| 72 | + { |
| 73 | + WindowHelper.WindowContent = null; |
| 74 | + FeatureConfiguration.Image.EnableBitmapImageCache = cacheWasEnabled; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static async Task LoadAndWaitForOpened(Image image) |
| 79 | + { |
| 80 | + var opened = TrackOpened(image); |
| 81 | + await UITestHelper.Load(image); |
| 82 | + await opened.WaitAsync(TimeSpan.FromSeconds(10)); |
| 83 | + } |
| 84 | + |
| 85 | + private static Task TrackOpened(Image image) |
| 86 | + { |
| 87 | + var tcs = new TaskCompletionSource(); |
| 88 | + image.ImageOpened += (_, _) => tcs.TrySetResult(); |
| 89 | + image.ImageFailed += (_, e) => tcs.TrySetException(new Exception($"Failed to load the image: {e.ErrorMessage}")); |
| 90 | + return tcs.Task; |
| 91 | + } |
| 92 | +} |
| 93 | +#endif |
0 commit comments