Skip to content

Commit bd2cedf

Browse files
committed
Add basic tests validating fix in #10428
1 parent b6914d7 commit bd2cedf

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed

src/Microsoft.DotNet.Wpf/tests/UnitTests/PresentationCore.Tests/PresentationCore.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
<PackageReference Include="FluentAssertions" Version="$(FluentAssertionsVersion)" />
3434
<PackageReference Include="System.Configuration.ConfigurationManager" Version="$(SystemConfigurationConfigurationManagerPackageVersion)" />
3535
<PackageReference Include="System.Formats.Nrbf" Version="$(SystemFormatsNrbfVersion)" />
36+
<PackageReference Include="$(SystemIOPackagingPackage)" Version="$(SystemIOPackagingVersion)" />
3637
<PackageReference Include="System.Runtime.Serialization.Formatters" Version="$(SystemRuntimeSerializationFormattersPackageVersion)" />
37-
<PackageReference Include="System.Drawing.Common" Version="$(SystemDrawingCommonVersion)" />
38+
<PackageReference Include="$(SystemDrawingCommonPackage)" Version="$(SystemDrawingCommonVersion)" />
3839
<PackageReference Include="System.Private.Windows.Core.TestUtilities" Version="$(SystemPrivateWindowsCoreTestUtilitiesVersion)" />
3940
</ItemGroup>
4041

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace System.Windows.Media.Imaging;
5+
6+
public sealed class BitmapImageTests
7+
{
8+
private static readonly byte[] s_png120DPI1x1 = [0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
9+
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,
10+
0xDE, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xAE, 0xCE, 0x1C, 0xE9, 0x00, 0x00,
11+
0x00, 0x04, 0x67, 0x41, 0x4D, 0x41, 0x00, 0x00, 0xB1, 0x8F, 0x0B, 0xFC, 0x61, 0x05, 0x00, 0x00,
12+
0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x12, 0x74, 0x00, 0x00, 0x12, 0x74, 0x01, 0xDE,
13+
0x66, 0x1F, 0x78, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x18, 0x57, 0x63, 0x60, 0x60,
14+
0x60, 0x00, 0x00, 0x00, 0x04, 0x00, 0x01, 0x5C, 0xCD, 0xFF, 0x69, 0x00, 0x00, 0x00, 0x00, 0x49,
15+
0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82];
16+
17+
[WpfFact]
18+
public void Initialization_StreamSource_Invalid_ThrowsNotSupportedException()
19+
{
20+
using MemoryStream stream = new("invalid image"u8.ToArray());
21+
BitmapImage image = new BitmapImage();
22+
23+
// InitializeInit
24+
25+
image.BeginInit();
26+
27+
image.StreamSource = stream;
28+
image.CacheOption = BitmapCacheOption.OnLoad;
29+
30+
Assert.Throws<NotSupportedException>(image.EndInit);
31+
}
32+
33+
[WpfFact]
34+
public void Initialization_StreamSource_PNG_Succeeds()
35+
{
36+
using MemoryStream stream = new(s_png120DPI1x1);
37+
38+
BitmapImage image = new BitmapImage();
39+
40+
// InitializeInit
41+
42+
image.BeginInit();
43+
44+
image.CacheOption = BitmapCacheOption.OnLoad;
45+
image.StreamSource = stream;
46+
47+
image.EndInit();
48+
image.Freeze();
49+
50+
// Check sources
51+
Assert.Null(image.UriSource);
52+
Assert.NotNull(image.StreamSource);
53+
54+
// It is 1x1 at 120 DPI
55+
Assert.Equal(1, image.PixelWidth);
56+
Assert.Equal(1, image.PixelHeight);
57+
}
58+
59+
[WpfFact]
60+
public void Initialization_UriSource_Invalid_ThrowsNotSupportedException()
61+
{
62+
string tempFile = Path.GetTempFileName();
63+
64+
try
65+
{
66+
File.WriteAllBytes(tempFile, "invalid image"u8.ToArray());
67+
68+
BitmapImage image = new BitmapImage();
69+
70+
// InitializeInit (with OnLoad, so the file handle is closed)
71+
72+
image.BeginInit();
73+
74+
image.CacheOption = BitmapCacheOption.OnLoad;
75+
image.UriSource = new Uri(tempFile);
76+
77+
Assert.Throws<NotSupportedException>(image.EndInit);
78+
}
79+
finally
80+
{
81+
File.Delete(tempFile);
82+
}
83+
}
84+
85+
[WpfFact]
86+
public void Initialization_UriSource_PNG_Succeeds()
87+
{
88+
string tempFile = Path.GetTempFileName();
89+
90+
try
91+
{
92+
File.WriteAllBytes(tempFile, s_png120DPI1x1);
93+
94+
BitmapImage image = new BitmapImage();
95+
96+
// InitializeInit (with OnLoad, so the file handle is closed)
97+
98+
image.BeginInit();
99+
100+
image.CacheOption = BitmapCacheOption.OnLoad;
101+
image.UriSource = new Uri(tempFile);
102+
103+
image.EndInit();
104+
image.Freeze();
105+
106+
// Check sources
107+
Assert.NotNull(image.UriSource);
108+
Assert.Null(image.StreamSource);
109+
110+
// It is 1x1 at 120 DPI
111+
Assert.Equal(1, image.PixelWidth);
112+
Assert.Equal(1, image.PixelHeight);
113+
}
114+
finally
115+
{
116+
File.Delete(tempFile);
117+
}
118+
}
119+
}

0 commit comments

Comments
 (0)