|
| 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 CheckBoxFlatAdapterTests : IDisposable |
| 10 | +{ |
| 11 | + private TestCheckBox? _checkBox; |
| 12 | + |
| 13 | + private (TestCheckBoxFlatAdapter, TestCheckBox) CreateAdapter(Appearance appearance, bool enabled) |
| 14 | + { |
| 15 | + _checkBox = new TestCheckBox |
| 16 | + { |
| 17 | + Appearance = appearance, |
| 18 | + Enabled = enabled |
| 19 | + }; |
| 20 | + |
| 21 | + TestCheckBoxFlatAdapter checkBoxFlatAdapter = new(_checkBox); |
| 22 | + |
| 23 | + return (checkBoxFlatAdapter, _checkBox); |
| 24 | + } |
| 25 | + |
| 26 | + public void Dispose() => _checkBox?.Dispose(); |
| 27 | + |
| 28 | + private class TestCheckBox : CheckBox |
| 29 | + { |
| 30 | + public new bool Enabled |
| 31 | + { |
| 32 | + get => base.Enabled; |
| 33 | + set => base.Enabled = value; |
| 34 | + } |
| 35 | + |
| 36 | + public new Appearance Appearance |
| 37 | + { |
| 38 | + get => base.Appearance; |
| 39 | + set => base.Appearance = value; |
| 40 | + } |
| 41 | + |
| 42 | + public new CheckState CheckState |
| 43 | + { |
| 44 | + get => base.CheckState; |
| 45 | + set => base.CheckState = value; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + private class TestCheckBoxFlatAdapter : CheckBoxFlatAdapter |
| 50 | + { |
| 51 | + public TestCheckBoxFlatAdapter(ButtonBase control) : base(control) { } |
| 52 | + |
| 53 | + public void CallPaintDown(PaintEventArgs e, CheckState state) => PaintDown(e, state); |
| 54 | + |
| 55 | + public bool PaintFlatWorkerCalled { get; private set; } |
| 56 | + |
| 57 | + protected void PaintFlatWorker() => PaintFlatWorkerCalled = true; |
| 58 | + } |
| 59 | + |
| 60 | + [WinFormsTheory] |
| 61 | + [InlineData(Appearance.Button, true)] |
| 62 | + [InlineData(Appearance.Button, false)] |
| 63 | + [InlineData(Appearance.Normal, true)] |
| 64 | + [InlineData(Appearance.Normal, false)] |
| 65 | + public void PaintDown_DoesNotThrow(Appearance appearance, bool enabled) |
| 66 | + { |
| 67 | + (TestCheckBoxFlatAdapter checkBoxFlatAdapter, TestCheckBox checkBox) = CreateAdapter(appearance, enabled); |
| 68 | + checkBox.CheckState = CheckState.Checked; |
| 69 | + |
| 70 | + using Bitmap bitmap = new(20, 20); |
| 71 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 72 | + using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 20, 20)); |
| 73 | + |
| 74 | + Exception? exception = Record.Exception(() => |
| 75 | + checkBoxFlatAdapter.CallPaintDown(e, checkBox.CheckState)); |
| 76 | + |
| 77 | + exception.Should().BeNull(); |
| 78 | + } |
| 79 | + |
| 80 | + [WinFormsTheory] |
| 81 | + [InlineData(Appearance.Button, true)] |
| 82 | + [InlineData(Appearance.Button, false)] |
| 83 | + [InlineData(Appearance.Normal, true)] |
| 84 | + [InlineData(Appearance.Normal, false)] |
| 85 | + public void PaintOver_DoesNotThrow(Appearance appearance, bool enabled) |
| 86 | + { |
| 87 | + (TestCheckBoxFlatAdapter checkBoxFlatAdapter, TestCheckBox checkBox) = CreateAdapter(appearance, enabled); |
| 88 | + checkBox.CheckState = CheckState.Indeterminate; |
| 89 | + |
| 90 | + using Bitmap bitmap = new(20, 20); |
| 91 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 92 | + using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 20, 20)); |
| 93 | + |
| 94 | + Exception? exception = Record.Exception(() => |
| 95 | + checkBoxFlatAdapter.PaintOver(e, checkBox.CheckState)); |
| 96 | + |
| 97 | + exception.Should().BeNull(); |
| 98 | + } |
| 99 | + |
| 100 | + [WinFormsTheory] |
| 101 | + [InlineData(Appearance.Button, true)] |
| 102 | + [InlineData(Appearance.Button, false)] |
| 103 | + [InlineData(Appearance.Normal, true)] |
| 104 | + [InlineData(Appearance.Normal, false)] |
| 105 | + public void PaintUp_DoesNotThrow(Appearance appearance, bool enabled) |
| 106 | + { |
| 107 | + (TestCheckBoxFlatAdapter checkBoxFlatAdapter, TestCheckBox checkBox) = CreateAdapter(appearance, enabled); |
| 108 | + checkBox.CheckState = CheckState.Unchecked; |
| 109 | + |
| 110 | + using Bitmap bitmap = new(20, 20); |
| 111 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 112 | + using PaintEventArgs e = new(graphics, new Rectangle(0, 0, 20, 20)); |
| 113 | + |
| 114 | + Exception? exception = Record.Exception(() => |
| 115 | + checkBoxFlatAdapter.PaintUp(e, checkBox.CheckState)); |
| 116 | + |
| 117 | + exception.Should().BeNull(); |
| 118 | + } |
| 119 | + |
| 120 | + [WinFormsFact] |
| 121 | + public void CreateButtonAdapter_ReturnsButtonFlatAdapter() |
| 122 | + { |
| 123 | + (TestCheckBoxFlatAdapter checkBoxFlatAdapter, _) = CreateAdapter(Appearance.Normal, true); |
| 124 | + |
| 125 | + ButtonBaseAdapter result = checkBoxFlatAdapter.TestAccessor().Dynamic.CreateButtonAdapter(); |
| 126 | + |
| 127 | + result.Should().NotBeNull(); |
| 128 | + result.Should().BeOfType<ButtonFlatAdapter>(); |
| 129 | + } |
| 130 | +} |
0 commit comments