|
| 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.Collections; |
| 5 | +using System.ComponentModel; |
| 6 | +using System.ComponentModel.Design; |
| 7 | +using System.Drawing; |
| 8 | +using Moq; |
| 9 | + |
| 10 | +namespace System.Windows.Forms.Design.Tests; |
| 11 | + |
| 12 | +public class FlowLayoutPanelDesignerTests : IDisposable |
| 13 | +{ |
| 14 | + private readonly Mock<IServiceProvider> _serviceProviderMock; |
| 15 | + private readonly Mock<IDesignerHost> _designerHostMock; |
| 16 | + private readonly Mock<ISelectionService> _selectionServiceMock; |
| 17 | + private readonly FlowLayoutPanelDesigner _designer; |
| 18 | + private readonly FlowLayoutPanel _flowLayoutPanel; |
| 19 | + |
| 20 | + public FlowLayoutPanelDesignerTests() |
| 21 | + { |
| 22 | + _serviceProviderMock = new(); |
| 23 | + _designerHostMock = new(); |
| 24 | + _selectionServiceMock = new(); |
| 25 | + |
| 26 | + _serviceProviderMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object); |
| 27 | + _serviceProviderMock.Setup(s => s.GetService(typeof(ISelectionService))).Returns(_selectionServiceMock.Object); |
| 28 | + |
| 29 | + _flowLayoutPanel = new() { Site = new Mock<ISite>().Object }; |
| 30 | + _designer = new(); |
| 31 | + _designer.Initialize(_flowLayoutPanel); |
| 32 | + } |
| 33 | + |
| 34 | + public void Dispose() |
| 35 | + { |
| 36 | + _designer.Dispose(); |
| 37 | + _flowLayoutPanel.Dispose(); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public void Initialize_ShouldSetInheritedReadOnlyAttribute_WhenFlowLayoutPanelIsInheritedReadOnly() |
| 42 | + { |
| 43 | + using Control childControl = new() { Site = new Mock<ISite>().Object }; |
| 44 | + _flowLayoutPanel.Controls.Add(childControl); |
| 45 | + |
| 46 | + TypeDescriptor.AddAttributes(childControl, InheritanceAttribute.InheritedReadOnly); |
| 47 | + |
| 48 | + TypeDescriptor.GetAttributes(childControl)[typeof(InheritanceAttribute)] |
| 49 | + .Should().BeEquivalentTo(InheritanceAttribute.InheritedReadOnly); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void Initialize_ShouldBeNotInheritedAttribute_WhenFlowLayoutPanelIsNotInheritedReadOnly() |
| 54 | + { |
| 55 | + using Control childControl = new() { Site = new Mock<ISite>().Object }; |
| 56 | + _flowLayoutPanel.Controls.Add(childControl); |
| 57 | + |
| 58 | + TypeDescriptor.GetAttributes(childControl)[typeof(InheritanceAttribute)] |
| 59 | + .Should().NotBeEquivalentTo(InheritanceAttribute.InheritedReadOnly); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void PreFilterProperties_ShouldModifyFlowDirectionProperty() |
| 64 | + { |
| 65 | + Hashtable properties = new() |
| 66 | + { |
| 67 | + { nameof(FlowLayoutPanel.FlowDirection), TypeDescriptor.CreateProperty(typeof(FlowLayoutPanel), nameof(FlowLayoutPanel.FlowDirection), typeof(FlowDirection)) } |
| 68 | + }; |
| 69 | + |
| 70 | + _designer.TestAccessor().Dynamic.PreFilterProperties(properties); |
| 71 | + |
| 72 | + properties[nameof(FlowLayoutPanel.FlowDirection)].Should().NotBeNull(); |
| 73 | + properties[nameof(FlowLayoutPanel.FlowDirection)].Should().BeAssignableTo<PropertyDescriptor>(); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void AllowSetChildIndexOnDrop_ShouldReturnFalse() => |
| 78 | + _designer.AllowSetChildIndexOnDrop.Should().BeFalse(); |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public void AllowGenericDragBox_ShouldReturnFalse() |
| 82 | + { |
| 83 | + bool result = _designer.TestAccessor().Dynamic.AllowGenericDragBox; |
| 84 | + |
| 85 | + result.Should().BeFalse(); |
| 86 | + } |
| 87 | + |
| 88 | + [Theory] |
| 89 | + [InlineData(FlowDirection.LeftToRight, true)] |
| 90 | + [InlineData(FlowDirection.RightToLeft, true)] |
| 91 | + [InlineData(FlowDirection.TopDown, false)] |
| 92 | + [InlineData(FlowDirection.BottomUp, false)] |
| 93 | + public void HorizontalFlow_ShouldReturnCorrectValueBasedOnFlowDirection(FlowDirection flowDirection, bool expected) |
| 94 | + { |
| 95 | + _flowLayoutPanel.FlowDirection = flowDirection; |
| 96 | + |
| 97 | + bool result = _designer.TestAccessor().Dynamic.HorizontalFlow; |
| 98 | + |
| 99 | + result.Should().Be(expected); |
| 100 | + } |
| 101 | + |
| 102 | + [Theory] |
| 103 | + [InlineData(FlowDirection.LeftToRight, FlowDirection.RightToLeft)] |
| 104 | + [InlineData(FlowDirection.RightToLeft, FlowDirection.LeftToRight)] |
| 105 | + [InlineData(FlowDirection.TopDown, FlowDirection.TopDown)] |
| 106 | + [InlineData(FlowDirection.BottomUp, FlowDirection.BottomUp)] |
| 107 | + public void RTLTranslateFlowDirection_ShouldTranslateCorrectly(FlowDirection input, FlowDirection expected) |
| 108 | + { |
| 109 | + _flowLayoutPanel.RightToLeft = RightToLeft.Yes; |
| 110 | + |
| 111 | + FlowDirection result = _designer.TestAccessor().Dynamic.RTLTranslateFlowDirection(input); |
| 112 | + |
| 113 | + result.Should().Be(expected); |
| 114 | + } |
| 115 | + |
| 116 | + [Theory] |
| 117 | + [InlineData(RightToLeft.Yes, true)] |
| 118 | + [InlineData(RightToLeft.No, false)] |
| 119 | + public void IsRtl_ShouldReturnCorrectValueBasedOnRightToLeft(RightToLeft rightToLeft, bool expected) |
| 120 | + { |
| 121 | + _flowLayoutPanel.RightToLeft = rightToLeft; |
| 122 | + |
| 123 | + bool result = _designer.TestAccessor().Dynamic.IsRtl; |
| 124 | + |
| 125 | + result.Should().Be(expected); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void GetMarginBounds_ShouldReturnCorrectRectangle() |
| 130 | + { |
| 131 | + using Control control = new() |
| 132 | + { |
| 133 | + Bounds = new Rectangle(10, 20, 30, 40), |
| 134 | + Margin = new Padding(5, 6, 7, 8) |
| 135 | + }; |
| 136 | + |
| 137 | + Rectangle result = _designer.TestAccessor().Dynamic.GetMarginBounds(control); |
| 138 | + |
| 139 | + result.Should().Be(new Rectangle(5, 14, 42, 54)); |
| 140 | + } |
| 141 | + |
| 142 | + [Fact] |
| 143 | + public void OnDragEnter_ShouldInitializeDragState() |
| 144 | + { |
| 145 | + DragEventArgs dragEventArgs = new( |
| 146 | + new DataObject(), |
| 147 | + 0, |
| 148 | + 0, |
| 149 | + 0, |
| 150 | + DragDropEffects.Copy, |
| 151 | + DragDropEffects.Copy); |
| 152 | + |
| 153 | + _designer.TestAccessor().Dynamic.OnDragEnter(dragEventArgs); |
| 154 | + |
| 155 | + int insertionIndex = _designer.TestAccessor().Dynamic._insertionIndex; |
| 156 | + insertionIndex.Should().Be(-1); |
| 157 | + |
| 158 | + Point lastMouseLocation = _designer.TestAccessor().Dynamic._lastMouseLocation; |
| 159 | + lastMouseLocation.Should().Be(Point.Empty); |
| 160 | + } |
| 161 | + |
| 162 | + [Fact] |
| 163 | + public void OnDragLeave_ShouldClearDragState() |
| 164 | + { |
| 165 | + _designer.TestAccessor().Dynamic.OnDragLeave(EventArgs.Empty); |
| 166 | + |
| 167 | + int insertionIndex = _designer.TestAccessor().Dynamic._insertionIndex; |
| 168 | + insertionIndex.Should().Be(-1); |
| 169 | + } |
| 170 | + |
| 171 | + [Fact] |
| 172 | + public void OnDragOver_ShouldUpdateInsertionIndex() |
| 173 | + { |
| 174 | + DragEventArgs dragEventArgs = new( |
| 175 | + new DataObject(), |
| 176 | + 0, |
| 177 | + 10, |
| 178 | + 10, |
| 179 | + DragDropEffects.Copy, |
| 180 | + DragDropEffects.Copy); |
| 181 | + |
| 182 | + _designer.TestAccessor().Dynamic.OnDragOver(dragEventArgs); |
| 183 | + |
| 184 | + int insertionIndex = _designer.TestAccessor().Dynamic._insertionIndex; |
| 185 | + insertionIndex.Should().Be(-1); |
| 186 | + } |
| 187 | + |
| 188 | + [Fact] |
| 189 | + public void OnDragDrop_ShouldReorderControls() |
| 190 | + { |
| 191 | + using Control control1 = new(); |
| 192 | + using Control control2 = new(); |
| 193 | + _flowLayoutPanel.Controls.Add(control1); |
| 194 | + _flowLayoutPanel.Controls.Add(control2); |
| 195 | + DragEventArgs dragEventArgs = new( |
| 196 | + new DataObject(), |
| 197 | + 0, |
| 198 | + 0, |
| 199 | + 0, |
| 200 | + DragDropEffects.Move, |
| 201 | + DragDropEffects.Move); |
| 202 | + |
| 203 | + _designer.TestAccessor().Dynamic.OnDragDrop(dragEventArgs); |
| 204 | + |
| 205 | + _flowLayoutPanel.Controls[0].Should().Be(control1); |
| 206 | + _flowLayoutPanel.Controls[1].Should().Be(control2); |
| 207 | + } |
| 208 | +} |
0 commit comments