From 78df126d5bda543f403e3a233a22a0f38cadd849 Mon Sep 17 00:00:00 2001 From: lilla28 Date: Fri, 3 May 2024 13:24:00 +0200 Subject: [PATCH] fix(icon-handling) - Add test for ToImageSource extension method to check if the icon handle is properly diposed --- Directory.Packages.props | 2 +- src/shell/dotnet/Shell.sln | 4 +- .../tests/Shell.Tests/Shell.Tests.csproj | 1 + .../Utilities/IconUtilities.Tests.cs | 45 +++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 src/shell/dotnet/tests/Shell.Tests/Utilities/IconUtilities.Tests.cs diff --git a/Directory.Packages.props b/Directory.Packages.props index ba634b7af..5ed0b46f3 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -59,4 +59,4 @@ - + \ No newline at end of file diff --git a/src/shell/dotnet/Shell.sln b/src/shell/dotnet/Shell.sln index aab923825..f563c0fb3 100644 --- a/src/shell/dotnet/Shell.sln +++ b/src/shell/dotnet/Shell.sln @@ -34,7 +34,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{DA examples\module-catalog.json = examples\module-catalog.json EndProjectSection EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiagnosticsExample", "..\..\..\examples\dotnet-diagnostics\DiagnosticsExample\DiagnosticsExample.csproj", "{24E64AFE-D2C5-487D-85F3-B0CE5AC0A128}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiagnosticsExample", "..\..\..\examples\dotnet-diagnostics\DiagnosticsExample\DiagnosticsExample.csproj", "{24E64AFE-D2C5-487D-85F3-B0CE5AC0A128}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9173857E-84BF-4F8B-906B-39BCF0A8915B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/src/shell/dotnet/tests/Shell.Tests/Shell.Tests.csproj b/src/shell/dotnet/tests/Shell.Tests/Shell.Tests.csproj index 7f94cb03a..00a38f434 100644 --- a/src/shell/dotnet/tests/Shell.Tests/Shell.Tests.csproj +++ b/src/shell/dotnet/tests/Shell.Tests/Shell.Tests.csproj @@ -10,6 +10,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/src/shell/dotnet/tests/Shell.Tests/Utilities/IconUtilities.Tests.cs b/src/shell/dotnet/tests/Shell.Tests/Utilities/IconUtilities.Tests.cs new file mode 100644 index 000000000..2b3d040fe --- /dev/null +++ b/src/shell/dotnet/tests/Shell.Tests/Utilities/IconUtilities.Tests.cs @@ -0,0 +1,45 @@ +// /* +// * Morgan Stanley makes this available to you under the Apache License, +// * Version 2.0 (the "License"). You may obtain a copy of the License at +// * +// * http://www.apache.org/licenses/LICENSE-2.0. +// * +// * See the NOTICE file distributed with this work for additional information +// * regarding copyright ownership. Unless required by applicable law or agreed +// * to in writing, software distributed under the License is distributed on an +// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express +// * or implied. See the License for the specific language governing permissions +// * and limitations under the License. +// */ + +using System.Drawing; +using FluentAssertions; + +namespace MorganStanley.ComposeUI.Shell.Utilities; + +public class IconUtilitiesTests +{ + [Fact] + public void Bitmap_handle_is_no_longer_held_after_it_has_been_disposed() + { + var icon = new Icon(SystemIcons.Exclamation, 40, 40); + var bitmap = icon.ToBitmap(); + + var result = bitmap.ToImageSource(); + + result.Should().NotBeNull(); + + bitmap.Dispose(); + icon.Dispose(); + + var action1 = () => bitmap.GetHbitmap(); + var action2 = () => bitmap.GetHicon(); + var action3 = () => icon.Handle; + var action4 = () => result; + + action1.Should().Throw(); + action2.Should().Throw(); + action3.Should().Throw(); + action4.Should().NotThrow(); + } +}