|
| 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.ComponentModel; |
| 5 | +using System.ComponentModel.Design; |
| 6 | +using System.Drawing; |
| 7 | +using System.Drawing.Design; |
| 8 | +using System.Reflection; |
| 9 | +using System.Windows.Forms.Design.Behavior; |
| 10 | +using Moq; |
| 11 | + |
| 12 | +namespace System.Windows.Forms.Design.Tests; |
| 13 | +public class ParentControlDesignerTests : IDisposable |
| 14 | +{ |
| 15 | + private readonly ParentControlDesigner _designer; |
| 16 | + private readonly Control _control; |
| 17 | + |
| 18 | + public ParentControlDesignerTests() |
| 19 | + { |
| 20 | + _designer = new(); |
| 21 | + _control = new(); |
| 22 | + _designer.Initialize(_control); |
| 23 | + } |
| 24 | + |
| 25 | + public void Dispose() |
| 26 | + { |
| 27 | + _control.Dispose(); |
| 28 | + _designer.Dispose(); |
| 29 | + } |
| 30 | + |
| 31 | + [Fact] |
| 32 | + public void SnapLines_ReturnsExpectedSnapLines() |
| 33 | + { |
| 34 | + IList<SnapLine> snapLines = _designer.SnapLines.Cast<SnapLine>().ToList(); |
| 35 | + |
| 36 | + snapLines.Should().NotBeNull(); |
| 37 | + snapLines.Should().BeOfType<List<SnapLine>>(); |
| 38 | + ((List<SnapLine>)snapLines).Should().NotBeEmpty(); |
| 39 | + ((List<SnapLine>)snapLines).Should().OnlyHaveUniqueItems(); |
| 40 | + |
| 41 | + ((List<SnapLine>)snapLines).Should() |
| 42 | + .Contain(sl => sl.SnapLineType == SnapLineType.Top) |
| 43 | + .And.Contain(sl => sl.SnapLineType == SnapLineType.Bottom) |
| 44 | + .And.Contain(sl => sl.SnapLineType == SnapLineType.Left) |
| 45 | + .And.Contain(sl => sl.SnapLineType == SnapLineType.Right); |
| 46 | + } |
| 47 | + |
| 48 | + [Fact] |
| 49 | + public void CanParent_ControlDesigner_ReturnsExpected() |
| 50 | + { |
| 51 | + Mock<ControlDesigner> mockControlDesigner = new(); |
| 52 | + Mock<Control> mockControl = new(); |
| 53 | + mockControlDesigner.Setup(cd => cd.Control).Returns(mockControl.Object); |
| 54 | + |
| 55 | + _designer.CanParent(mockControlDesigner.Object).Should().BeTrue(); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void CanParent_Control_ReturnsExpected() |
| 60 | + { |
| 61 | + using TestControl testControl = new() { ContainsControl = false }; |
| 62 | + |
| 63 | + _designer.CanParent(testControl).Should().BeTrue(); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void GetGlyphs_SelectionTypeSelected_AddsContainerSelectorGlyph() |
| 68 | + { |
| 69 | + Mock<IServiceProvider> mockServiceProvider = new(); |
| 70 | + Mock<ISite> mockSite = new(); |
| 71 | + Mock<IDesignerHost> mockDesignerHost = new(); |
| 72 | + Mock<IComponentChangeService> mockChangeService = new(); |
| 73 | + _control.Site = mockSite.Object; |
| 74 | + mockSite.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(mockDesignerHost.Object); |
| 75 | + mockSite.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(mockChangeService.Object); |
| 76 | + |
| 77 | + Mock<DesignerFrame> mockDesignerFrame = new(mockSite.Object) { CallBase = true }; |
| 78 | + BehaviorService behaviorService = new(mockServiceProvider.Object, mockDesignerFrame.Object); |
| 79 | + mockServiceProvider.Setup(sp => sp.GetService(typeof(BehaviorService))).Returns(() => behaviorService); |
| 80 | + mockSite.Setup(s => s.GetService(typeof(BehaviorService))).Returns(behaviorService); |
| 81 | + |
| 82 | + _designer.TestAccessor().Dynamic._behaviorService = behaviorService; |
| 83 | + _designer.TestAccessor().Dynamic._host = mockDesignerHost.Object; |
| 84 | + _designer.TestAccessor().Dynamic._changeService = mockChangeService.Object; |
| 85 | + |
| 86 | + GlyphSelectionType selectionType = GlyphSelectionType.Selected; |
| 87 | + |
| 88 | + GlyphCollection glyphs = _designer.GetGlyphs(selectionType); |
| 89 | + |
| 90 | + glyphs.Should().BeOfType<GlyphCollection>(); |
| 91 | + glyphs.OfType<ContainerSelectorGlyph>().Count().Should().Be(1); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void InitializeNewComponent_WithNullDefaultValues_DoesNotThrow() |
| 96 | + { |
| 97 | + _designer.Invoking(d => d.InitializeNewComponent(null)).Should().NotThrow(); |
| 98 | + } |
| 99 | + |
| 100 | + [WinFormsFact] |
| 101 | + public void InitializeNewComponent_ShouldReparentControls_WhenAllowControlLassoIsTrue() |
| 102 | + { |
| 103 | + Mock<IDesignerHost> mockDesignerHost = new(); |
| 104 | + Mock<ISite> mockSite = new(); |
| 105 | + Mock<ISelectionService> mockSelectionService = new(); |
| 106 | + Mock<IComponentChangeService> mockChangeService = new(); |
| 107 | + |
| 108 | + using TestControl testComponent = new(); |
| 109 | + using TestControl parentComponent = new(); |
| 110 | + |
| 111 | + mockSite.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(mockDesignerHost.Object); |
| 112 | + mockSite.Setup(s => s.GetService(typeof(ISelectionService))).Returns(mockSelectionService.Object); |
| 113 | + mockSite.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(mockChangeService.Object); |
| 114 | + testComponent.Site = mockSite.Object; |
| 115 | + parentComponent.Site = mockSite.Object; |
| 116 | + |
| 117 | + mockDesignerHost.Setup(h => h.GetDesigner(parentComponent)).Returns(_designer); |
| 118 | + |
| 119 | + _designer.Initialize(testComponent); |
| 120 | + _designer.TestAccessor().Dynamic._changeService = mockChangeService.Object; |
| 121 | + |
| 122 | + Dictionary<string, object> defaultValues = new() |
| 123 | + { |
| 124 | + { "Size", new Size(100, 100) }, |
| 125 | + { "Location", new Point(10, 10) }, |
| 126 | + { "Parent", parentComponent } |
| 127 | + }; |
| 128 | + |
| 129 | + DisableDragDrop(testComponent); |
| 130 | + DisableDragDrop(parentComponent); |
| 131 | + |
| 132 | + _designer.InitializeNewComponent(defaultValues); |
| 133 | + |
| 134 | + mockDesignerHost.Verify(h => h.GetDesigner(parentComponent), Times.AtMost(2)); |
| 135 | + } |
| 136 | + |
| 137 | + public class TestControl : Control |
| 138 | + { |
| 139 | + public bool ContainsControl { get; set; } |
| 140 | + |
| 141 | + public new bool Contains(Control ctl) |
| 142 | + { |
| 143 | + return ContainsControl; |
| 144 | + } |
| 145 | + |
| 146 | + public new ControlCollection Controls => base.Controls; |
| 147 | + } |
| 148 | + |
| 149 | + private void DisableDragDrop(Control control) |
| 150 | + { |
| 151 | + if (control is null) |
| 152 | + return; |
| 153 | + |
| 154 | + control.AllowDrop = false; |
| 155 | + foreach (Control child in control.Controls) |
| 156 | + { |
| 157 | + DisableDragDrop(child); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + [Fact] |
| 162 | + public void AllowSetChildIndexOnDrop_ReturnsTrue() |
| 163 | + { |
| 164 | + _designer.AllowSetChildIndexOnDrop.Should().BeTrue(); |
| 165 | + } |
| 166 | + |
| 167 | + [Fact] |
| 168 | + public void CanAddComponent_ReturnsTrue() |
| 169 | + { |
| 170 | + Mock<IComponent> mockComponent = new(); |
| 171 | + _designer.CanAddComponent(mockComponent.Object).Should().BeTrue(); |
| 172 | + } |
| 173 | + |
| 174 | + [Fact] |
| 175 | + public void EnableDragRect_ReturnsTrue() |
| 176 | + { |
| 177 | + bool enableDragRect = _designer.TestAccessor().Dynamic.EnableDragRect; |
| 178 | + enableDragRect.Should().BeTrue(); |
| 179 | + } |
| 180 | + |
| 181 | + [Fact] |
| 182 | + public void GridSize_DefaultValue_ReturnsExpected() |
| 183 | + { |
| 184 | + Size gridSize = _designer.TestAccessor().Dynamic.GridSize; |
| 185 | + gridSize.Should().Be(new Size(8, 8)); |
| 186 | + } |
| 187 | + |
| 188 | + [Fact] |
| 189 | + public void GridSize_SetValue_UpdatesGridSize() |
| 190 | + { |
| 191 | + Size newSize = new Size(10, 10); |
| 192 | + _designer.TestAccessor().Dynamic.GridSize = newSize; |
| 193 | + Size gridSize = _designer.TestAccessor().Dynamic.GridSize; |
| 194 | + gridSize.Should().Be(newSize); |
| 195 | + } |
| 196 | + |
| 197 | + [Fact] |
| 198 | + public void GridSize_SetValue_InvalidSize_ThrowsArgumentException() |
| 199 | + { |
| 200 | + Action action = () => _designer.TestAccessor().Dynamic.GridSize = new Size(1, 1); |
| 201 | + action.Should().Throw<TargetInvocationException>() |
| 202 | + .WithInnerException<ArgumentException>() |
| 203 | + .WithMessage("*'GridSize'*"); |
| 204 | + |
| 205 | + action = () => _designer.TestAccessor().Dynamic.GridSize = new Size(201, 201); |
| 206 | + action.Should().Throw<TargetInvocationException>() |
| 207 | + .WithInnerException<ArgumentException>() |
| 208 | + .WithMessage("*'GridSize'*"); |
| 209 | + } |
| 210 | + |
| 211 | + [Fact] |
| 212 | + public void MouseDragTool_DefaultValue_ReturnsNull() |
| 213 | + { |
| 214 | + ToolboxItem mouseDragTool = _designer.TestAccessor().Dynamic.MouseDragTool; |
| 215 | + mouseDragTool.Should().BeNull(); |
| 216 | + } |
| 217 | + |
| 218 | + [Fact] |
| 219 | + public void MouseDragTool_SetValue_ReturnsExpected() |
| 220 | + { |
| 221 | + ToolboxItem toolboxItem = new(typeof(Button)); |
| 222 | + _designer.TestAccessor().Dynamic._mouseDragTool = toolboxItem; |
| 223 | + ToolboxItem mouseDragTool = _designer.TestAccessor().Dynamic.MouseDragTool; |
| 224 | + mouseDragTool.Should().Be(toolboxItem); |
| 225 | + } |
| 226 | + |
| 227 | + [Fact] |
| 228 | + public void GetParentForComponent_ReturnsControl() |
| 229 | + { |
| 230 | + Mock<IComponent> mockComponent = new(); |
| 231 | + Control parentControl = _designer.TestAccessor().Dynamic.GetParentForComponent(mockComponent.Object); |
| 232 | + parentControl.Should().Be(_control); |
| 233 | + } |
| 234 | +} |
0 commit comments