|
| 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.ButtonInternal; |
| 6 | + |
| 7 | +namespace System.Windows.Forms.Tests; |
| 8 | + |
| 9 | +public class CheckBoxBaseAdapterTests : IDisposable |
| 10 | +{ |
| 11 | + private TestCheckBox? _checkBox; |
| 12 | + |
| 13 | + private (TestCheckBoxBaseAdapter, TestCheckBox) CreateAdapter(CheckState checkState) |
| 14 | + { |
| 15 | + _checkBox = new TestCheckBox |
| 16 | + { |
| 17 | + CheckState = checkState |
| 18 | + }; |
| 19 | + |
| 20 | + TestCheckBoxBaseAdapter adapter = new(_checkBox); |
| 21 | + |
| 22 | + return (adapter, _checkBox); |
| 23 | + } |
| 24 | + |
| 25 | + public void Dispose() => _checkBox?.Dispose(); |
| 26 | + |
| 27 | + private class TestCheckBox : CheckBox |
| 28 | + { |
| 29 | + public new bool Enabled |
| 30 | + { |
| 31 | + get => base.Enabled; |
| 32 | + set => base.Enabled = value; |
| 33 | + } |
| 34 | + |
| 35 | + public new CheckState CheckState |
| 36 | + { |
| 37 | + get => base.CheckState; |
| 38 | + set => base.CheckState = value; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private class TestCheckBoxBaseAdapter : CheckBoxBaseAdapter |
| 43 | + { |
| 44 | + public TestCheckBoxBaseAdapter(ButtonBase control) : base(control) { } |
| 45 | + |
| 46 | + public void CallDrawCheckFlat( |
| 47 | + PaintEventArgs e, |
| 48 | + LayoutData layout, |
| 49 | + Color checkColor, |
| 50 | + Color checkBackground, |
| 51 | + Color checkBorder, |
| 52 | + ColorData colors) |
| 53 | + => DrawCheckFlat(e, layout, checkColor, checkBackground, checkBorder, colors); |
| 54 | + |
| 55 | + protected override LayoutOptions Layout(PaintEventArgs e) |
| 56 | + => new LayoutOptions(); |
| 57 | + |
| 58 | + protected override ButtonBaseAdapter CreateButtonAdapter() |
| 59 | + => new StubButtonBaseAdapter(Control); |
| 60 | + |
| 61 | + private class StubButtonBaseAdapter : ButtonBaseAdapter |
| 62 | + { |
| 63 | + public StubButtonBaseAdapter(ButtonBase control) : base(control) { } |
| 64 | + internal override void PaintUp(PaintEventArgs e, CheckState state) { } |
| 65 | + internal override void PaintDown(PaintEventArgs e, CheckState state) { } |
| 66 | + internal override void PaintOver(PaintEventArgs e, CheckState state) { } |
| 67 | + protected override LayoutOptions Layout(PaintEventArgs e) => new LayoutOptions(); |
| 68 | + } |
| 69 | + |
| 70 | + internal override void PaintOver(PaintEventArgs e, CheckState state) |
| 71 | + { |
| 72 | + } |
| 73 | + |
| 74 | + internal override void PaintDown(PaintEventArgs e, CheckState state) |
| 75 | + { |
| 76 | + } |
| 77 | + |
| 78 | + internal override void PaintUp(PaintEventArgs e, CheckState state) |
| 79 | + { |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + private static ButtonBaseAdapter.LayoutData CreateLayoutData(bool dotNetOneButtonCompat = false) |
| 84 | + { |
| 85 | + ButtonBaseAdapter.LayoutData layout = new( |
| 86 | + new ButtonBaseAdapter.LayoutOptions |
| 87 | + { |
| 88 | + DotNetOneButtonCompat = dotNetOneButtonCompat |
| 89 | + }) |
| 90 | + { |
| 91 | + CheckBounds = new Rectangle(2, 2, 11, 11) |
| 92 | + }; |
| 93 | + |
| 94 | + return layout; |
| 95 | + } |
| 96 | + |
| 97 | + private static ButtonBaseAdapter.ColorData CreateColorData(Graphics graphics) |
| 98 | + { |
| 99 | + ButtonBaseAdapter.ColorOptions options = new( |
| 100 | + graphics, |
| 101 | + Color.Black, |
| 102 | + Color.White |
| 103 | + ); |
| 104 | + |
| 105 | + ButtonBaseAdapter.ColorData colorData = new(options) |
| 106 | + { |
| 107 | + ButtonFace = Color.LightGray, |
| 108 | + ButtonShadow = Color.Gray, |
| 109 | + Highlight = Color.Yellow |
| 110 | + }; |
| 111 | + |
| 112 | + return colorData; |
| 113 | + } |
| 114 | + |
| 115 | + [WinFormsTheory] |
| 116 | + [InlineData(CheckState.Unchecked, false)] |
| 117 | + [InlineData(CheckState.Checked, false)] |
| 118 | + [InlineData(CheckState.Indeterminate, false)] |
| 119 | + [InlineData(CheckState.Unchecked, true)] |
| 120 | + [InlineData(CheckState.Checked, true)] |
| 121 | + [InlineData(CheckState.Indeterminate, true)] |
| 122 | + public void DrawCheckFlat_DoesNotThrow(CheckState checkState, bool dotNetOneButtonCompat) |
| 123 | + { |
| 124 | + (TestCheckBoxBaseAdapter adapter, TestCheckBox checkBox) = CreateAdapter(checkState); |
| 125 | + using Bitmap bitmap = new(20, 20); |
| 126 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 127 | + using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 20, 20)); |
| 128 | + ButtonBaseAdapter.LayoutData layout = CreateLayoutData(dotNetOneButtonCompat); |
| 129 | + Color checkColor = Color.Black; |
| 130 | + Color checkBackground = Color.White; |
| 131 | + Color checkBorder = Color.Blue; |
| 132 | + ButtonBaseAdapter.ColorData colors = CreateColorData(graphics); |
| 133 | + |
| 134 | + Exception? ex = Record.Exception(() => |
| 135 | + adapter.CallDrawCheckFlat(e, layout, checkColor, checkBackground, checkBorder, colors)); |
| 136 | + |
| 137 | + ex.Should().BeNull(); |
| 138 | + } |
| 139 | + |
| 140 | + [WinFormsTheory] |
| 141 | + [InlineData(false, CheckState.Unchecked, false, 0)] |
| 142 | + [InlineData(false, CheckState.Unchecked, true, 1)] |
| 143 | + [InlineData(true, CheckState.Indeterminate, true, 2)] |
| 144 | + public void DrawCheckBackground_CoversAllBranches( |
| 145 | + bool controlEnabled, |
| 146 | + CheckState checkState, |
| 147 | + bool disabledColors, |
| 148 | + int expectedBranch) |
| 149 | + { |
| 150 | + Rectangle bounds = new(1, 2, 3, 4); |
| 151 | + using Bitmap bitmap = new(10, 10); |
| 152 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 153 | + Color checkBackground = expectedBranch == 2 ? SystemColors.Window : Color.Red; |
| 154 | + |
| 155 | + Exception? ex = Record.Exception(() => |
| 156 | + CheckBoxBaseAdapter.DrawCheckBackground( |
| 157 | + controlEnabled, |
| 158 | + checkState, |
| 159 | + graphics, |
| 160 | + bounds, |
| 161 | + checkBackground, |
| 162 | + disabledColors)); |
| 163 | + |
| 164 | + ex.Should().BeNull(); |
| 165 | + } |
| 166 | + |
| 167 | + [WinFormsTheory] |
| 168 | + [InlineData(CheckState.Checked, true, true)] |
| 169 | + [InlineData(CheckState.Checked, false, true)] |
| 170 | + [InlineData(CheckState.Indeterminate, true, true)] |
| 171 | + [InlineData(CheckState.Indeterminate, false, true)] |
| 172 | + [InlineData(CheckState.Unchecked, true, false)] |
| 173 | + public void DrawCheckOnly_Protected_DoesNotThrow( |
| 174 | + CheckState checkState, |
| 175 | + bool enabled, |
| 176 | + bool isChecked) |
| 177 | + { |
| 178 | + (TestCheckBoxBaseAdapter adapter, TestCheckBox checkBox) = CreateAdapter(checkState); |
| 179 | + checkBox.Enabled = enabled; |
| 180 | + checkBox.Checked = isChecked; |
| 181 | + |
| 182 | + using Bitmap bitmap = new(20, 20); |
| 183 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 184 | + using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 20, 20)); |
| 185 | + ButtonBaseAdapter.LayoutData layout = CreateLayoutData(); |
| 186 | + ButtonBaseAdapter.ColorData colors = CreateColorData(graphics); |
| 187 | + Color checkColor = Color.Black; |
| 188 | + |
| 189 | + Exception? ex = Record.Exception(() => |
| 190 | + adapter.TestAccessor().Dynamic.DrawCheckOnly( |
| 191 | + e, |
| 192 | + layout, |
| 193 | + colors, |
| 194 | + checkColor)); |
| 195 | + |
| 196 | + ex.Should().BeNull(); |
| 197 | + } |
| 198 | + |
| 199 | + [WinFormsFact] |
| 200 | + public void DrawPopupBorder_DoesNotThrow_AndInflatesRectangle() |
| 201 | + { |
| 202 | + using Bitmap bitmap = new(20, 20); |
| 203 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 204 | + Rectangle original = new(2, 2, 10, 10); |
| 205 | + |
| 206 | + ButtonBaseAdapter.ColorData colors = CreateColorData(graphics); |
| 207 | + |
| 208 | + Rectangle result = CheckBoxBaseAdapter.DrawPopupBorder(graphics, original, colors); |
| 209 | + |
| 210 | + Rectangle expected = original; |
| 211 | + expected.Inflate(-1, -1); |
| 212 | + |
| 213 | + result.Should().Be(expected); |
| 214 | + } |
| 215 | + |
| 216 | + [WinFormsFact] |
| 217 | + public void DrawPopupBorder_PaintEventArgs_DoesNotThrow_AndInflatesRectangle() |
| 218 | + { |
| 219 | + using Bitmap bitmap = new(20, 20); |
| 220 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 221 | + Rectangle original = new(3, 3, 8, 8); |
| 222 | + |
| 223 | + ButtonBaseAdapter.ColorData colors = CreateColorData(graphics); |
| 224 | + |
| 225 | + using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 20, 20)); |
| 226 | + Rectangle result = CheckBoxBaseAdapter.DrawPopupBorder(e, original, colors); |
| 227 | + |
| 228 | + Rectangle expected = original; |
| 229 | + expected.Inflate(-1, -1); |
| 230 | + |
| 231 | + result.Should().Be(expected); |
| 232 | + } |
| 233 | + |
| 234 | + [WinFormsFact] |
| 235 | + public void DrawPopupBorder_HDC_DoesNotThrow_AndInflatesRectangle() |
| 236 | + { |
| 237 | + using Bitmap bitmap = new(20, 20); |
| 238 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 239 | + Rectangle original = new(4, 4, 6, 6); |
| 240 | + |
| 241 | + ButtonBaseAdapter.ColorData colors = CreateColorData(graphics); |
| 242 | + |
| 243 | + IntPtr hdcPtr = graphics.GetHdc(); |
| 244 | + try |
| 245 | + { |
| 246 | + Rectangle result = CheckBoxBaseAdapter.DrawPopupBorder( |
| 247 | + new HDC(hdcPtr), |
| 248 | + original, |
| 249 | + colors); |
| 250 | + |
| 251 | + Rectangle expected = original; |
| 252 | + expected.Inflate(-1, -1); |
| 253 | + |
| 254 | + result.Should().Be(expected); |
| 255 | + } |
| 256 | + finally |
| 257 | + { |
| 258 | + graphics.ReleaseHdc(hdcPtr); |
| 259 | + } |
| 260 | + } |
| 261 | + |
| 262 | + [Theory] |
| 263 | + [InlineData(null, true, 2, 2, 10, 10)] |
| 264 | + [InlineData("", true, 2, 2, 10, 10)] |
| 265 | + [InlineData(null, false, 2, 2, 10, 10)] |
| 266 | + [InlineData("", false, 2, 2, 10, 10)] |
| 267 | + public void AdjustFocusRectangle_SetsFocusAsExpected( |
| 268 | + string? text, |
| 269 | + bool autoSize, |
| 270 | + int checkX, int checkY, int checkW, int checkH) |
| 271 | + { |
| 272 | + (TestCheckBoxBaseAdapter adapter, TestCheckBox checkBox) = CreateAdapter(CheckState.Unchecked); |
| 273 | + checkBox.Text = text; |
| 274 | + checkBox.AutoSize = autoSize; |
| 275 | + |
| 276 | + ButtonBaseAdapter.LayoutData layout = new(new ButtonBaseAdapter.LayoutOptions()) |
| 277 | + { |
| 278 | + CheckBounds = new Rectangle(checkX, checkY, checkW, checkH), |
| 279 | + Field = new Rectangle(5, 5, 20, 20) |
| 280 | + }; |
| 281 | + |
| 282 | + adapter.TestAccessor().Dynamic.AdjustFocusRectangle(layout); |
| 283 | + |
| 284 | + if (string.IsNullOrEmpty(text)) |
| 285 | + { |
| 286 | + if (autoSize) |
| 287 | + { |
| 288 | + Rectangle expected = Rectangle.Inflate(layout.CheckBounds, -2, -2); |
| 289 | + layout.Focus.Should().Be(expected); |
| 290 | + } |
| 291 | + else |
| 292 | + { |
| 293 | + layout.Focus.Should().Be(new Rectangle(5, 5, 20, 20)); |
| 294 | + } |
| 295 | + } |
| 296 | + else |
| 297 | + { |
| 298 | + layout.Focus.Should().Be(default); |
| 299 | + } |
| 300 | + } |
| 301 | +} |
0 commit comments