|
| 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 | +using System.Drawing; |
| 5 | +using System.Windows.Forms.VisualStyles; |
| 6 | + |
| 7 | +namespace System.Windows.Forms.Tests; |
| 8 | + |
| 9 | +public class ButtonRendererTests |
| 10 | +{ |
| 11 | + public static TheoryData<PushButtonState> ButtonStates => new() |
| 12 | + { |
| 13 | + PushButtonState.Normal, |
| 14 | + PushButtonState.Hot, |
| 15 | + PushButtonState.Pressed, |
| 16 | + PushButtonState.Disabled, |
| 17 | + PushButtonState.Default |
| 18 | + }; |
| 19 | + |
| 20 | + [WinFormsTheory] |
| 21 | + [MemberData(nameof(ButtonStates))] |
| 22 | + public void DrawButton_DoesNotThrow(PushButtonState state) |
| 23 | + { |
| 24 | + using Bitmap bitmap = new(50, 20); |
| 25 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 26 | + Rectangle bounds = new(0, 0, 50, 20); |
| 27 | + |
| 28 | + Exception? exception = Record.Exception(() => ButtonRenderer.DrawButton(graphics, bounds, state)); |
| 29 | + exception.Should().BeNull(); |
| 30 | + } |
| 31 | + |
| 32 | + [WinFormsTheory] |
| 33 | + [MemberData(nameof(ButtonStates))] |
| 34 | + public void DrawButton_IDeviceContext_DoesNotThrow(PushButtonState state) |
| 35 | + { |
| 36 | + using Bitmap bitmap = new(60, 25); |
| 37 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 38 | + Rectangle bounds = new(0, 0, 60, 25); |
| 39 | + |
| 40 | + ButtonRenderer.RenderMatchingApplicationState = false; |
| 41 | + Exception? exception = Record.Exception(() => ButtonRenderer.DrawButton(graphics, bounds, state)); |
| 42 | + exception.Should().BeNull(); |
| 43 | + |
| 44 | + ButtonRenderer.RenderMatchingApplicationState = true; |
| 45 | + Exception? exception2 = Record.Exception(() => ButtonRenderer.DrawButton(graphics, bounds, state)); |
| 46 | + exception2.Should().BeNull(); |
| 47 | + } |
| 48 | + |
| 49 | + [WinFormsTheory] |
| 50 | + [InlineData(PushButtonState.Normal, true)] |
| 51 | + [InlineData(PushButtonState.Normal, false)] |
| 52 | + [InlineData(PushButtonState.Hot, true)] |
| 53 | + [InlineData(PushButtonState.Hot, false)] |
| 54 | + [InlineData(PushButtonState.Pressed, true)] |
| 55 | + [InlineData(PushButtonState.Pressed, false)] |
| 56 | + [InlineData(PushButtonState.Disabled, true)] |
| 57 | + [InlineData(PushButtonState.Disabled, false)] |
| 58 | + [InlineData(PushButtonState.Default, true)] |
| 59 | + [InlineData(PushButtonState.Default, false)] |
| 60 | + public void DrawButton_Focused_DoesNotThrow(PushButtonState state, bool focused) |
| 61 | + { |
| 62 | + using Bitmap bitmap = new(80, 30); |
| 63 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 64 | + Rectangle bounds = new(0, 0, 80, 30); |
| 65 | + |
| 66 | + Exception? exception = Record.Exception(() => ButtonRenderer.DrawButton(graphics, bounds, focused, state)); |
| 67 | + exception.Should().BeNull(); |
| 68 | + } |
| 69 | + |
| 70 | + [WinFormsTheory] |
| 71 | + [InlineData(PushButtonState.Normal, "Test", true)] |
| 72 | + [InlineData(PushButtonState.Normal, "Test", false)] |
| 73 | + [InlineData(PushButtonState.Normal, null, true)] |
| 74 | + [InlineData(PushButtonState.Normal, null, false)] |
| 75 | + [InlineData(PushButtonState.Hot, "Test", true)] |
| 76 | + [InlineData(PushButtonState.Hot, "Test", false)] |
| 77 | + [InlineData(PushButtonState.Hot, null, true)] |
| 78 | + [InlineData(PushButtonState.Hot, null, false)] |
| 79 | + [InlineData(PushButtonState.Pressed, "Test", true)] |
| 80 | + [InlineData(PushButtonState.Pressed, "Test", false)] |
| 81 | + [InlineData(PushButtonState.Pressed, null, true)] |
| 82 | + [InlineData(PushButtonState.Pressed, null, false)] |
| 83 | + [InlineData(PushButtonState.Disabled, "Test", true)] |
| 84 | + [InlineData(PushButtonState.Disabled, "Test", false)] |
| 85 | + [InlineData(PushButtonState.Disabled, null, true)] |
| 86 | + [InlineData(PushButtonState.Disabled, null, false)] |
| 87 | + [InlineData(PushButtonState.Default, "Test", true)] |
| 88 | + [InlineData(PushButtonState.Default, "Test", false)] |
| 89 | + [InlineData(PushButtonState.Default, null, true)] |
| 90 | + [InlineData(PushButtonState.Default, null, false)] |
| 91 | + public void DrawButton_TextFontFocused_DoesNotThrow(PushButtonState state, string? buttonText, bool focused) |
| 92 | + { |
| 93 | + using Bitmap bitmap = new(100, 40); |
| 94 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 95 | + Rectangle bounds = new(0, 0, 100, 40); |
| 96 | + using Font font = new("Segoe UI", 10); |
| 97 | + |
| 98 | + Exception? exception = Record.Exception(() => |
| 99 | + ButtonRenderer.DrawButton(graphics, bounds, buttonText, font, focused, state)); |
| 100 | + exception.Should().BeNull(); |
| 101 | + } |
| 102 | + |
| 103 | + [WinFormsTheory] |
| 104 | + [InlineData(PushButtonState.Normal, "Sample", TextFormatFlags.Default, true)] |
| 105 | + [InlineData(PushButtonState.Normal, "Sample", TextFormatFlags.Default, false)] |
| 106 | + [InlineData(PushButtonState.Normal, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 107 | + [InlineData(PushButtonState.Normal, "Button", TextFormatFlags.SingleLine, false)] |
| 108 | + [InlineData(PushButtonState.Hot, "Sample", TextFormatFlags.Default, true)] |
| 109 | + [InlineData(PushButtonState.Hot, "Sample", TextFormatFlags.Default, false)] |
| 110 | + [InlineData(PushButtonState.Hot, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 111 | + [InlineData(PushButtonState.Hot, "Button", TextFormatFlags.SingleLine, false)] |
| 112 | + [InlineData(PushButtonState.Pressed, "Sample", TextFormatFlags.Default, true)] |
| 113 | + [InlineData(PushButtonState.Pressed, "Sample", TextFormatFlags.Default, false)] |
| 114 | + [InlineData(PushButtonState.Pressed, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 115 | + [InlineData(PushButtonState.Pressed, "Button", TextFormatFlags.SingleLine, false)] |
| 116 | + [InlineData(PushButtonState.Disabled, "Sample", TextFormatFlags.Default, true)] |
| 117 | + [InlineData(PushButtonState.Disabled, "Sample", TextFormatFlags.Default, false)] |
| 118 | + [InlineData(PushButtonState.Disabled, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 119 | + [InlineData(PushButtonState.Disabled, "Button", TextFormatFlags.SingleLine, false)] |
| 120 | + [InlineData(PushButtonState.Default, "Sample", TextFormatFlags.Default, true)] |
| 121 | + [InlineData(PushButtonState.Default, "Sample", TextFormatFlags.Default, false)] |
| 122 | + [InlineData(PushButtonState.Default, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 123 | + [InlineData(PushButtonState.Default, "Button", TextFormatFlags.SingleLine, false)] |
| 124 | + public void DrawButton_TextFontFlagsFocused_DoesNotThrow( |
| 125 | + PushButtonState state, string? buttonText, TextFormatFlags flags, bool focused) |
| 126 | + { |
| 127 | + using Bitmap bitmap = new(120, 50); |
| 128 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 129 | + Rectangle bounds = new(0, 0, 120, 50); |
| 130 | + using Font font = new("Arial", 12); |
| 131 | + |
| 132 | + Exception? exception = Record.Exception(() => |
| 133 | + ButtonRenderer.DrawButton(graphics, bounds, buttonText, font, flags, focused, state)); |
| 134 | + exception.Should().BeNull(); |
| 135 | + } |
| 136 | + |
| 137 | + [WinFormsTheory] |
| 138 | + [InlineData(PushButtonState.Normal, true)] |
| 139 | + [InlineData(PushButtonState.Normal, false)] |
| 140 | + [InlineData(PushButtonState.Hot, true)] |
| 141 | + [InlineData(PushButtonState.Hot, false)] |
| 142 | + [InlineData(PushButtonState.Pressed, true)] |
| 143 | + [InlineData(PushButtonState.Pressed, false)] |
| 144 | + [InlineData(PushButtonState.Disabled, true)] |
| 145 | + [InlineData(PushButtonState.Disabled, false)] |
| 146 | + [InlineData(PushButtonState.Default, true)] |
| 147 | + [InlineData(PushButtonState.Default, false)] |
| 148 | + public void DrawButton_ImageImageBoundsFocused_DoesNotThrow(PushButtonState state, bool focused) |
| 149 | + { |
| 150 | + using Bitmap bitmap = new(120, 50); |
| 151 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 152 | + Rectangle bounds = new(0, 0, 120, 50); |
| 153 | + |
| 154 | + using Bitmap image = new(32, 32); |
| 155 | + Rectangle imageBounds = new(10, 10, 32, 32); |
| 156 | + |
| 157 | + Exception? exception = Record.Exception(() => |
| 158 | + ButtonRenderer.DrawButton(graphics, bounds, image, imageBounds, focused, state)); |
| 159 | + exception.Should().BeNull(); |
| 160 | + } |
| 161 | + |
| 162 | + [WinFormsTheory] |
| 163 | + [InlineData(PushButtonState.Normal, "Icon", true)] |
| 164 | + [InlineData(PushButtonState.Normal, "Icon", false)] |
| 165 | + [InlineData(PushButtonState.Normal, null, true)] |
| 166 | + [InlineData(PushButtonState.Normal, null, false)] |
| 167 | + [InlineData(PushButtonState.Hot, "Icon", true)] |
| 168 | + [InlineData(PushButtonState.Hot, "Icon", false)] |
| 169 | + [InlineData(PushButtonState.Hot, null, true)] |
| 170 | + [InlineData(PushButtonState.Hot, null, false)] |
| 171 | + [InlineData(PushButtonState.Pressed, "Icon", true)] |
| 172 | + [InlineData(PushButtonState.Pressed, "Icon", false)] |
| 173 | + [InlineData(PushButtonState.Pressed, null, true)] |
| 174 | + [InlineData(PushButtonState.Pressed, null, false)] |
| 175 | + [InlineData(PushButtonState.Disabled, "Icon", true)] |
| 176 | + [InlineData(PushButtonState.Disabled, "Icon", false)] |
| 177 | + [InlineData(PushButtonState.Disabled, null, true)] |
| 178 | + [InlineData(PushButtonState.Disabled, null, false)] |
| 179 | + [InlineData(PushButtonState.Default, "Icon", true)] |
| 180 | + [InlineData(PushButtonState.Default, "Icon", false)] |
| 181 | + [InlineData(PushButtonState.Default, null, true)] |
| 182 | + [InlineData(PushButtonState.Default, null, false)] |
| 183 | + public void DrawButton_TextFontImageImageBoundsFocused_DoesNotThrow( |
| 184 | + PushButtonState state, string? buttonText, bool focused) |
| 185 | + { |
| 186 | + using Bitmap bitmap = new(140, 60); |
| 187 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 188 | + Rectangle bounds = new(0, 0, 140, 60); |
| 189 | + using Font font = new("Tahoma", 11); |
| 190 | + using Bitmap image = new(24, 24); |
| 191 | + Rectangle imageBounds = new(20, 18, 24, 24); |
| 192 | + |
| 193 | + Exception? exception = Record.Exception(() => |
| 194 | + ButtonRenderer.DrawButton(graphics, bounds, buttonText, font, image, imageBounds, focused, state)); |
| 195 | + exception.Should().BeNull(); |
| 196 | + } |
| 197 | + |
| 198 | + [WinFormsTheory] |
| 199 | + [InlineData(PushButtonState.Normal, "Combo", TextFormatFlags.Default, true)] |
| 200 | + [InlineData(PushButtonState.Normal, "Combo", TextFormatFlags.Default, false)] |
| 201 | + [InlineData(PushButtonState.Normal, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 202 | + [InlineData(PushButtonState.Normal, "Button", TextFormatFlags.SingleLine, false)] |
| 203 | + [InlineData(PushButtonState.Hot, "Combo", TextFormatFlags.Default, true)] |
| 204 | + [InlineData(PushButtonState.Hot, "Combo", TextFormatFlags.Default, false)] |
| 205 | + [InlineData(PushButtonState.Hot, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 206 | + [InlineData(PushButtonState.Hot, "Button", TextFormatFlags.SingleLine, false)] |
| 207 | + [InlineData(PushButtonState.Pressed, "Combo", TextFormatFlags.Default, true)] |
| 208 | + [InlineData(PushButtonState.Pressed, "Combo", TextFormatFlags.Default, false)] |
| 209 | + [InlineData(PushButtonState.Pressed, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 210 | + [InlineData(PushButtonState.Pressed, "Button", TextFormatFlags.SingleLine, false)] |
| 211 | + [InlineData(PushButtonState.Disabled, "Combo", TextFormatFlags.Default, true)] |
| 212 | + [InlineData(PushButtonState.Disabled, "Combo", TextFormatFlags.Default, false)] |
| 213 | + [InlineData(PushButtonState.Disabled, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 214 | + [InlineData(PushButtonState.Disabled, "Button", TextFormatFlags.SingleLine, false)] |
| 215 | + [InlineData(PushButtonState.Default, "Combo", TextFormatFlags.Default, true)] |
| 216 | + [InlineData(PushButtonState.Default, "Combo", TextFormatFlags.Default, false)] |
| 217 | + [InlineData(PushButtonState.Default, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 218 | + [InlineData(PushButtonState.Default, "Button", TextFormatFlags.SingleLine, false)] |
| 219 | + public void DrawButton_TextFontFlagsImageImageBoundsFocused_DoesNotThrow( |
| 220 | + PushButtonState state, |
| 221 | + string? buttonText, |
| 222 | + TextFormatFlags flags, |
| 223 | + bool focused) |
| 224 | + { |
| 225 | + using Bitmap bitmap = new(160, 70); |
| 226 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 227 | + Rectangle bounds = new(0, 0, 160, 70); |
| 228 | + using Font font = new("Verdana", 13); |
| 229 | + using Bitmap image = new(32, 32); |
| 230 | + Rectangle imageBounds = new(30, 20, 32, 32); |
| 231 | + |
| 232 | + Exception? exception = Record.Exception(() => |
| 233 | + ButtonRenderer.DrawButton(graphics, bounds, buttonText, font, flags, image, imageBounds, focused, state)); |
| 234 | + exception.Should().BeNull(); |
| 235 | + } |
| 236 | + |
| 237 | + [WinFormsTheory] |
| 238 | + [InlineData(PushButtonState.Normal, "Advanced", TextFormatFlags.Default, true)] |
| 239 | + [InlineData(PushButtonState.Normal, "Advanced", TextFormatFlags.Default, false)] |
| 240 | + [InlineData(PushButtonState.Normal, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 241 | + [InlineData(PushButtonState.Normal, "Button", TextFormatFlags.SingleLine, false)] |
| 242 | + [InlineData(PushButtonState.Hot, "Advanced", TextFormatFlags.Default, true)] |
| 243 | + [InlineData(PushButtonState.Hot, "Advanced", TextFormatFlags.Default, false)] |
| 244 | + [InlineData(PushButtonState.Hot, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 245 | + [InlineData(PushButtonState.Hot, "Button", TextFormatFlags.SingleLine, false)] |
| 246 | + [InlineData(PushButtonState.Pressed, "Advanced", TextFormatFlags.Default, true)] |
| 247 | + [InlineData(PushButtonState.Pressed, "Advanced", TextFormatFlags.Default, false)] |
| 248 | + [InlineData(PushButtonState.Pressed, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 249 | + [InlineData(PushButtonState.Pressed, "Button", TextFormatFlags.SingleLine, false)] |
| 250 | + [InlineData(PushButtonState.Disabled, "Advanced", TextFormatFlags.Default, true)] |
| 251 | + [InlineData(PushButtonState.Disabled, "Advanced", TextFormatFlags.Default, false)] |
| 252 | + [InlineData(PushButtonState.Disabled, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 253 | + [InlineData(PushButtonState.Disabled, "Button", TextFormatFlags.SingleLine, false)] |
| 254 | + [InlineData(PushButtonState.Default, "Advanced", TextFormatFlags.Default, true)] |
| 255 | + [InlineData(PushButtonState.Default, "Advanced", TextFormatFlags.Default, false)] |
| 256 | + [InlineData(PushButtonState.Default, null, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, true)] |
| 257 | + [InlineData(PushButtonState.Default, "Button", TextFormatFlags.SingleLine, false)] |
| 258 | + public void DrawButton_IDeviceContext_TextFontFlagsImageImageBoundsFocused_DoesNotThrow( |
| 259 | + PushButtonState state, |
| 260 | + string? buttonText, |
| 261 | + TextFormatFlags flags, |
| 262 | + bool focused) |
| 263 | + { |
| 264 | + using Bitmap bitmap = new(180, 80); |
| 265 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 266 | + Rectangle bounds = new(0, 0, 180, 80); |
| 267 | + using Font font = new("Calibri", 14); |
| 268 | + using Bitmap image = new(40, 40); |
| 269 | + Rectangle imageBounds = new(40, 30, 40, 40); |
| 270 | + |
| 271 | + ButtonRenderer.RenderMatchingApplicationState = false; |
| 272 | + Exception? exception = Record.Exception(() => |
| 273 | + ButtonRenderer.DrawButton(graphics, bounds, buttonText, font, flags, image, imageBounds, focused, state)); |
| 274 | + exception.Should().BeNull(); |
| 275 | + |
| 276 | + ButtonRenderer.RenderMatchingApplicationState = true; |
| 277 | + Exception? exception2 = Record.Exception(() => |
| 278 | + ButtonRenderer.DrawButton(graphics, bounds, buttonText, font, flags, image, imageBounds, focused, state)); |
| 279 | + exception2.Should().BeNull(); |
| 280 | + } |
| 281 | +} |
0 commit comments