|
| 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 | +using static System.Windows.Forms.ButtonInternal.ButtonBaseAdapter; |
| 7 | + |
| 8 | +namespace System.Windows.Forms.Tests; |
| 9 | + |
| 10 | +public class ButtonPopupAdapterTests : IDisposable |
| 11 | +{ |
| 12 | + public ButtonBase button { get; } |
| 13 | + internal ButtonPopupAdapter adapter { get; } |
| 14 | + |
| 15 | + public ButtonPopupAdapterTests() |
| 16 | + { |
| 17 | + button = new TestButtonBase(); |
| 18 | + adapter = new(button); |
| 19 | + } |
| 20 | + |
| 21 | + public void Dispose() => button.Dispose(); |
| 22 | + |
| 23 | + public static TheoryData<CheckState> PaintStates => new() |
| 24 | + { |
| 25 | + CheckState.Unchecked, |
| 26 | + CheckState.Checked, |
| 27 | + CheckState.Indeterminate |
| 28 | + }; |
| 29 | + |
| 30 | + private class TestButtonBase : ButtonBase |
| 31 | + { |
| 32 | + public bool IsDefaultSet { get; set; } |
| 33 | + public Rectangle ClientRectangleValue { get; set; } = new(0, 0, 100, 30); |
| 34 | + |
| 35 | + internal override Rectangle OverChangeRectangle => ClientRectangleValue; |
| 36 | + |
| 37 | + internal override ButtonBaseAdapter CreateStandardAdapter() => new ButtonPopupAdapter(this); |
| 38 | + |
| 39 | + internal override StringFormat CreateStringFormat() => new StringFormat(); |
| 40 | + |
| 41 | + internal override TextFormatFlags CreateTextFormatFlags() => TextFormatFlags.Default; |
| 42 | + } |
| 43 | + |
| 44 | + [WinFormsTheory] |
| 45 | + [MemberData(nameof(PaintStates))] |
| 46 | + public void PaintUp_DoesNotThrow(CheckState state) |
| 47 | + { |
| 48 | + using Bitmap bitmap = new(100, 30); |
| 49 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 50 | + PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30)); |
| 51 | + |
| 52 | + Exception? exception = Record.Exception(() => adapter.PaintUp(e, state)); |
| 53 | + |
| 54 | + exception.Should().BeNull(); |
| 55 | + } |
| 56 | + |
| 57 | + [WinFormsTheory] |
| 58 | + [MemberData(nameof(PaintStates))] |
| 59 | + public void PaintOver_DoesNotThrow(CheckState state) |
| 60 | + { |
| 61 | + using Bitmap bitmap = new(100, 30); |
| 62 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 63 | + PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30)); |
| 64 | + |
| 65 | + Exception? exception = Record.Exception(() => adapter.PaintOver(e, state)); |
| 66 | + |
| 67 | + exception.Should().BeNull(); |
| 68 | + } |
| 69 | + |
| 70 | + [WinFormsTheory] |
| 71 | + [MemberData(nameof(PaintStates))] |
| 72 | + public void PaintDown_DoesNotThrow(CheckState state) |
| 73 | + { |
| 74 | + using Bitmap bitmap = new(100, 30); |
| 75 | + using Graphics graphics = Graphics.FromImage(bitmap); |
| 76 | + PaintEventArgs e = new(graphics, new Rectangle(0, 0, 100, 30)); |
| 77 | + |
| 78 | + Exception? exception = Record.Exception(() => adapter.PaintDown(e, state)); |
| 79 | + |
| 80 | + exception.Should().BeNull(); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public void PaintPopupLayout_ReturnsLayoutOptions_WithExpectedBorderAndPadding() |
| 85 | + { |
| 86 | + Rectangle clientRectangle = new(0, 0, 100, 30); |
| 87 | + Padding padding = new(2); |
| 88 | + bool isDefault = false; |
| 89 | + using Font font = SystemFonts.DefaultFont; |
| 90 | + string text = "Test"; |
| 91 | + bool enabled = true; |
| 92 | + ContentAlignment textAlign = ContentAlignment.MiddleCenter; |
| 93 | + RightToLeft rightToLeft = RightToLeft.No; |
| 94 | + |
| 95 | + LayoutOptions layoutOptions = ButtonPopupAdapter.PaintPopupLayout( |
| 96 | + up: true, |
| 97 | + paintedBorder: 1, |
| 98 | + clientRectangle, |
| 99 | + padding, |
| 100 | + isDefault, |
| 101 | + font, |
| 102 | + text, |
| 103 | + enabled, |
| 104 | + textAlign, |
| 105 | + rightToLeft); |
| 106 | + |
| 107 | + layoutOptions.BorderSize.Should().Be(1); |
| 108 | + layoutOptions.PaddingSize.Should().Be(1); |
| 109 | + layoutOptions.HintTextUp.Should().BeFalse(); |
| 110 | + layoutOptions.TextOffset.Should().BeFalse(); |
| 111 | + layoutOptions.ShadowedText.Should().Be(SystemInformation.HighContrast); |
| 112 | + } |
| 113 | + |
| 114 | + [Theory] |
| 115 | + [InlineData(true, 0)] |
| 116 | + [InlineData(false, 2)] |
| 117 | + public void PaintPopupLayout_BorderAndPaddingSumIsTwo(bool up, int paintedBorder) |
| 118 | + { |
| 119 | + Rectangle clientRectangle = new(0, 0, 100, 30); |
| 120 | + Padding padding = new(0); |
| 121 | + bool isDefault = false; |
| 122 | + using Font font = SystemFonts.DefaultFont; |
| 123 | + string text = string.Empty; |
| 124 | + bool enabled = false; |
| 125 | + ContentAlignment textAlign = ContentAlignment.TopLeft; |
| 126 | + RightToLeft rightToLeft = RightToLeft.No; |
| 127 | + |
| 128 | + LayoutOptions layoutOptions = ButtonPopupAdapter.PaintPopupLayout( |
| 129 | + up, |
| 130 | + paintedBorder, |
| 131 | + clientRectangle, |
| 132 | + padding, |
| 133 | + isDefault, |
| 134 | + font, |
| 135 | + text, |
| 136 | + enabled, |
| 137 | + textAlign, |
| 138 | + rightToLeft); |
| 139 | + |
| 140 | + int sum = layoutOptions.BorderSize + layoutOptions.PaddingSize; |
| 141 | + |
| 142 | + sum.Should().Be(2); |
| 143 | + } |
| 144 | +} |
0 commit comments