forked from dotnet/winforms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHelpers.cs
More file actions
233 lines (187 loc) · 8.29 KB
/
Copy pathHelpers.cs
File metadata and controls
233 lines (187 loc) · 8.29 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Drawing.Printing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Xunit;
using Xunit.Sdk;
namespace System.Drawing
{
public static class Helpers
{
public const string IsDrawingSupported = nameof(Helpers) + "." + nameof(GetIsDrawingSupported);
public const string IsWindowsOrAtLeastLibgdiplus6 = nameof(Helpers) + "." + nameof(GetIsWindowsOrAtLeastLibgdiplus6);
public const string RecentGdiplusIsAvailable = IsWindowsOrAtLeastLibgdiplus6;
public const string GdiPlusIsAvailableNotWindows7 = nameof(Helpers) + "." + nameof(GetGdiPlusIsAvailableNotWindows7);
public const string AnyInstalledPrinters = nameof(Helpers) + "." + nameof(IsAnyInstalledPrinters);
public const string WindowsRS3OrEarlier = nameof(Helpers) + "." + nameof(IsWindowsRS3OrEarlier);
public const string IsWindows = nameof(Helpers) + "." + nameof(GetIsWindows);
public static bool GetIsDrawingSupported() => PlatformDetection.IsDrawingSupported;
public static bool GetIsWindowsOrAtLeastLibgdiplus6()
{
if (!PlatformDetection.IsDrawingSupported)
{
return false;
}
if (PlatformDetection.IsWindows)
{
return true;
}
Version installedVersion;
try
{
installedVersion = new Version(GetLibgdiplusVersion());
}
catch (DllNotFoundException)
{
return false;
}
catch (EntryPointNotFoundException)
{
return false;
}
return installedVersion.Major >= 6;
}
public static bool GetIsWindows() => PlatformDetection.IsDrawingSupported && PlatformDetection.IsWindows;
public static bool IsNotUnix => PlatformDetection.IsWindows;
public static bool IsWindowsRS3OrEarlier => !PlatformDetection.IsWindows10Version1803OrGreater;
public static bool GetGdiPlusIsAvailableNotWindows7()
{
if (PlatformDetection.IsWindows7)
{
return false;
}
return GetIsDrawingSupported();
}
public static bool IsAnyInstalledPrinters()
{
return PrinterSettings.InstalledPrinters.Count > 0;
}
public static string GetTestBitmapPath(string fileName) => GetTestPath("bitmaps", fileName);
public static string GetTestFontPath(string fileName) => GetTestPath("fonts", fileName);
public static string GetTestColorProfilePath(string fileName) => GetTestPath("colorProfiles", fileName);
private static string GetTestPath(string directoryName, string fileName) => Path.Combine(AppContext.BaseDirectory, directoryName, fileName);
public static void VerifyBitmap(Bitmap bitmap, Color[][] colors)
{
for (int y = 0; y < colors.Length; y++)
{
for (int x = 0; x < colors[y].Length; x++)
{
Color expectedColor = Color.FromArgb(colors[y][x].ToArgb());
Color actualColor = bitmap.GetPixel(x, y);
if (expectedColor != actualColor)
{
throw GetBitmapEqualFailureException(bitmap, colors, x, y);
}
}
}
}
private static Exception GetBitmapEqualFailureException(Bitmap bitmap, Color[][] colors, int firstFailureX, int firstFailureY)
{
// Print out the whole bitmap to provide a view of the whole image, rather than just the difference between
// a single pixel.
var actualStringBuilder = new StringBuilder();
var expectedStringBuilder = new StringBuilder();
actualStringBuilder.AppendLine();
expectedStringBuilder.AppendLine();
for (int y = 0; y < bitmap.Height; y++)
{
for (int x = 0; x < bitmap.Width; x++)
{
PrintColor(actualStringBuilder, bitmap.GetPixel(x, y));
PrintColor(expectedStringBuilder, colors[y][x]);
if (x != bitmap.Width - 1)
{
actualStringBuilder.Append(", ");
expectedStringBuilder.Append(", ");
}
}
actualStringBuilder.AppendLine();
expectedStringBuilder.AppendLine();
}
return new AssertActualExpectedException(expectedStringBuilder.ToString(), actualStringBuilder.ToString(), $"Bitmaps were different at {firstFailureX}, {firstFailureY}.");
}
private static void PrintColor(StringBuilder stringBuilder, Color color)
{
stringBuilder.Append($"Color.FromArgb({color.A}, {color.R}, {color.G}, {color.B})");
}
public static Color EmptyColor => Color.FromArgb(0, 0, 0, 0);
private static Rectangle GetRectangle(RECT rect)
{
return new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
}
private const int MONITOR_DEFAULTTOPRIMARY = 1;
[DllImport("libgdiplus", ExactSpelling = true)]
internal static extern string GetLibgdiplusVersion();
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr MonitorFromWindow(IntPtr hWnd, int dwFlags);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO monitorInfo);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", ExactSpelling = true, SetLastError = true)]
internal static extern int GetGuiResources(IntPtr hProcess, uint flags);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr GetDC(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true)]
internal static extern IntPtr GetWindowDC(IntPtr hWnd);
public static Rectangle GetWindowDCRect(IntPtr hdc) => GetHWndRect(WindowFromDC(hdc));
public static Rectangle GetHWndRect(IntPtr hWnd)
{
if (hWnd == IntPtr.Zero)
{
return GetMonitorRectForWindow(hWnd);
}
var rect = new RECT();
GetClientRect(hWnd, ref rect);
return GetRectangle(rect);
}
private static Rectangle GetMonitorRectForWindow(IntPtr hWnd)
{
IntPtr hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);
Assert.NotEqual(IntPtr.Zero, hMonitor);
var info = new MONITORINFO();
info.cbSize = Marshal.SizeOf(info);
int result = GetMonitorInfo(hMonitor, ref info);
Assert.NotEqual(0, result);
return GetRectangle(info.rcMonitor);
}
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetClientRect(IntPtr hWnd, ref RECT lpRect);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr WindowFromDC(IntPtr hdc);
[StructLayout(LayoutKind.Sequential)]
private struct MONITORINFO
{
public int cbSize;
public RECT rcMonitor;
public RECT rcWork;
public int dwFlags;
}
[StructLayout(LayoutKind.Sequential)]
private struct RECT
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
public static void VerifyBitmapNotBlank(Bitmap bmp)
{
Color emptyColor = Color.FromArgb(0);
for (int y = 0; y < bmp.Height; y++)
{
for (int x = 0; x < bmp.Width; x++)
{
Color pixel = bmp.GetPixel(x, y);
if (!pixel.Equals(emptyColor))
{
return;
}
}
}
throw new XunitException("The entire image was blank.");
}
}
}