|
| 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 Moq; |
| 5 | +using System.ComponentModel; |
| 6 | +using System.ComponentModel.Design; |
| 7 | + |
| 8 | +namespace System.Windows.Forms.Design.Tests; |
| 9 | + |
| 10 | +public sealed class ToolStripContainerActionListTests : IDisposable |
| 11 | +{ |
| 12 | + private readonly ToolStripContainer _toolStripContainer; |
| 13 | + private readonly Mock<IDesignerHost> _designerHostMock; |
| 14 | + private readonly Mock<ISite> _siteMock; |
| 15 | + private readonly ToolStripContainerActionList _actionList; |
| 16 | + |
| 17 | + public ToolStripContainerActionListTests() |
| 18 | + { |
| 19 | + _toolStripContainer = new(); |
| 20 | + _designerHostMock = new(); |
| 21 | + _siteMock = new(); |
| 22 | + _siteMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object); |
| 23 | + _toolStripContainer.Site = _siteMock.Object; |
| 24 | + _actionList = new(_toolStripContainer); |
| 25 | + } |
| 26 | + |
| 27 | + public void Dispose() |
| 28 | + { |
| 29 | + _designerHostMock.Reset(); |
| 30 | + _toolStripContainer.Dispose(); |
| 31 | + _siteMock.Reset(); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void SetDockToForm_SetsDockToFill() |
| 36 | + { |
| 37 | + _actionList.SetDockToForm(); |
| 38 | + _toolStripContainer.Dock.Should().Be(DockStyle.Fill); |
| 39 | + } |
| 40 | + |
| 41 | + [Fact] |
| 42 | + public void SetDockToForm_AddsToolStripContainerToRootComponent() |
| 43 | + { |
| 44 | + using Control rootComponent = new(); |
| 45 | + _designerHostMock.Setup(dh => dh.RootComponent).Returns(rootComponent); |
| 46 | + _actionList.SetDockToForm(); |
| 47 | + rootComponent.Controls.Cast<Control>().Should().Contain(_toolStripContainer); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void ReparentControls_ReparentsControlsToContentPanel() |
| 52 | + { |
| 53 | + using Control rootComponent = new(); |
| 54 | + using Control childControl1 = new(); |
| 55 | + using Control childControl2 = new(); |
| 56 | + rootComponent.Controls.Add(_toolStripContainer); |
| 57 | + rootComponent.Controls.Add(childControl1); |
| 58 | + rootComponent.Controls.Add(childControl2); |
| 59 | + |
| 60 | + _designerHostMock.Setup(dh => dh.RootComponent).Returns(rootComponent); |
| 61 | + Mock<IComponentChangeService> componentChangeServiceMock = new(); |
| 62 | + _siteMock.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(componentChangeServiceMock.Object); |
| 63 | + |
| 64 | + _actionList.ReparentControls(); |
| 65 | + |
| 66 | + _toolStripContainer.ContentPanel.Controls.Cast<Control>().Should().Contain(childControl1); |
| 67 | + _toolStripContainer.ContentPanel.Controls.Cast<Control>().Should().Contain(childControl2); |
| 68 | + rootComponent.Controls.Cast<Control>().Should().NotContain(childControl1); |
| 69 | + rootComponent.Controls.Cast<Control>().Should().NotContain(childControl2); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void ReparentControls_DoesNotReparentInheritedControls() |
| 74 | + { |
| 75 | + using Control rootComponent = new(); |
| 76 | + using Control inheritedControl = new(); |
| 77 | + TypeDescriptor.AddAttributes(inheritedControl, new InheritanceAttribute(InheritanceLevel.InheritedReadOnly)); |
| 78 | + rootComponent.Controls.Add(_toolStripContainer); |
| 79 | + rootComponent.Controls.Add(inheritedControl); |
| 80 | + |
| 81 | + _designerHostMock.Setup(dh => dh.RootComponent).Returns(rootComponent); |
| 82 | + Mock<IComponentChangeService> componentChangeServiceMock = new(); |
| 83 | + _siteMock.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(componentChangeServiceMock.Object); |
| 84 | + |
| 85 | + _actionList.ReparentControls(); |
| 86 | + |
| 87 | + rootComponent.Controls.Cast<Control>().Should().Contain(inheritedControl); |
| 88 | + _toolStripContainer.ContentPanel.Controls.Cast<Control>().Should().NotContain(inheritedControl); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void TopVisible_Get_ReturnsCorrectValue() |
| 93 | + { |
| 94 | + bool topVisible = _actionList.TopVisible; |
| 95 | + topVisible.Should().Be(_toolStripContainer.TopToolStripPanelVisible); |
| 96 | + } |
| 97 | + |
| 98 | + [Fact] |
| 99 | + public void TopVisible_Set_ChangesValue() |
| 100 | + { |
| 101 | + bool newValue = !_toolStripContainer.TopToolStripPanelVisible; |
| 102 | + _actionList.TopVisible = newValue; |
| 103 | + _toolStripContainer.TopToolStripPanelVisible.Should().Be(newValue); |
| 104 | + } |
| 105 | + |
| 106 | + [Fact] |
| 107 | + public void BottomVisible_Get_ReturnsCorrectValue() |
| 108 | + { |
| 109 | + bool bottomVisible = _actionList.BottomVisible; |
| 110 | + bottomVisible.Should().Be(_toolStripContainer.BottomToolStripPanelVisible); |
| 111 | + } |
| 112 | + |
| 113 | + [Fact] |
| 114 | + public void BottomVisible_Set_ChangesValue() |
| 115 | + { |
| 116 | + bool newValue = !_toolStripContainer.BottomToolStripPanelVisible; |
| 117 | + _actionList.BottomVisible = newValue; |
| 118 | + _toolStripContainer.BottomToolStripPanelVisible.Should().Be(newValue); |
| 119 | + } |
| 120 | + |
| 121 | + [Fact] |
| 122 | + public void LeftVisible_Get_ReturnsCorrectValue() |
| 123 | + { |
| 124 | + bool leftVisible = _actionList.LeftVisible; |
| 125 | + leftVisible.Should().Be(_toolStripContainer.LeftToolStripPanelVisible); |
| 126 | + } |
| 127 | + |
| 128 | + [Fact] |
| 129 | + public void LeftVisible_Set_ChangesValue() |
| 130 | + { |
| 131 | + bool newValue = !_toolStripContainer.LeftToolStripPanelVisible; |
| 132 | + _actionList.LeftVisible = newValue; |
| 133 | + _toolStripContainer.LeftToolStripPanelVisible.Should().Be(newValue); |
| 134 | + } |
| 135 | + |
| 136 | + [Fact] |
| 137 | + public void RightVisible_Get_ReturnsCorrectValue() |
| 138 | + { |
| 139 | + bool rightVisible = _actionList.RightVisible; |
| 140 | + rightVisible.Should().Be(_toolStripContainer.RightToolStripPanelVisible); |
| 141 | + } |
| 142 | + |
| 143 | + [Fact] |
| 144 | + public void RightVisible_Set_ChangesValue() |
| 145 | + { |
| 146 | + bool newValue = !_toolStripContainer.RightToolStripPanelVisible; |
| 147 | + _actionList.RightVisible = newValue; |
| 148 | + _toolStripContainer.RightToolStripPanelVisible.Should().Be(newValue); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact] |
| 152 | + public void GetSortedActionItems_ReturnsCorrectItems_WhenDockNotFilled() |
| 153 | + { |
| 154 | + DesignerActionItemCollection items = _actionList.GetSortedActionItems(); |
| 155 | + List<string> displayNames = items.Cast<DesignerActionItem>() |
| 156 | + .Select(i => i.DisplayName ?? string.Empty) |
| 157 | + .ToList(); |
| 158 | + |
| 159 | + displayNames.Should().Contain(new[] |
| 160 | + { |
| 161 | + SR.ToolStripContainerActionList_Top, |
| 162 | + SR.ToolStripContainerActionList_Bottom, |
| 163 | + SR.ToolStripContainerActionList_Left, |
| 164 | + SR.ToolStripContainerActionList_Right, |
| 165 | + SR.DesignerShortcutDockInForm |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + [Fact] |
| 170 | + public void GetSortedActionItems_ReturnsCorrectItems_WhenDockFilled() |
| 171 | + { |
| 172 | + PropertyDescriptor dockProp = TypeDescriptor.GetProperties(_toolStripContainer)["Dock"]!; |
| 173 | + dockProp.SetValue(_toolStripContainer, DockStyle.Fill); |
| 174 | + |
| 175 | + DesignerActionItemCollection items = _actionList.GetSortedActionItems(); |
| 176 | + List<string> displayNames = items.Cast<DesignerActionItem>() |
| 177 | + .Select(i => i.DisplayName ?? string.Empty) |
| 178 | + .ToList(); |
| 179 | + |
| 180 | + displayNames.Should().Contain(new[] |
| 181 | + { |
| 182 | + SR.ToolStripContainerActionList_Top, |
| 183 | + SR.ToolStripContainerActionList_Bottom, |
| 184 | + SR.ToolStripContainerActionList_Left, |
| 185 | + SR.ToolStripContainerActionList_Right |
| 186 | + }); |
| 187 | + } |
| 188 | + |
| 189 | + [Fact] |
| 190 | + public void GetSortedActionItems_ReturnsCorrectItems_WhenProvideReparent() |
| 191 | + { |
| 192 | + using Control rootComponent = new(); |
| 193 | + using Control childControl = new(); |
| 194 | + rootComponent.Controls.Add(_toolStripContainer); |
| 195 | + rootComponent.Controls.Add(childControl); |
| 196 | + |
| 197 | + _designerHostMock.Setup(dh => dh.RootComponent).Returns(rootComponent); |
| 198 | + TypeDescriptor.GetProperties(_toolStripContainer)["Dock"]?.SetValue(_toolStripContainer, DockStyle.Fill); |
| 199 | + |
| 200 | + DesignerActionItemCollection items = _actionList.GetSortedActionItems(); |
| 201 | + List<string> displayNames = items.Cast<DesignerActionItem>() |
| 202 | + .Select(i => i.DisplayName ?? string.Empty) |
| 203 | + .ToList(); |
| 204 | + |
| 205 | + displayNames.Should().Contain(new[] |
| 206 | + { |
| 207 | + SR.ToolStripContainerActionList_Top, |
| 208 | + SR.ToolStripContainerActionList_Bottom, |
| 209 | + SR.ToolStripContainerActionList_Left, |
| 210 | + SR.ToolStripContainerActionList_Right, |
| 211 | + SR.DesignerShortcutReparentControls |
| 212 | + }); |
| 213 | + } |
| 214 | +} |
0 commit comments