|
| 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.Reflection; |
| 8 | +using System.Windows.Forms.Design.Behavior; |
| 9 | +using Microsoft.CodeAnalysis; |
| 10 | +using Moq; |
| 11 | + |
| 12 | +namespace System.Windows.Forms.Design.Tests; |
| 13 | + |
| 14 | +public sealed class ToolStripEditorManagerTests : IDisposable |
| 15 | +{ |
| 16 | + private readonly BehaviorService _behaviorService; |
| 17 | + private readonly Mock<IDesignerHost> _mockDesignerHost; |
| 18 | + private readonly Mock<IComponent> _mockComponent; |
| 19 | + private readonly Mock<ISite> _mockSite; |
| 20 | + private readonly Mock<IServiceProvider> _mockServiceProvider; |
| 21 | + private readonly ToolStripEditorManager _editorManager; |
| 22 | + private readonly Control _editorControl; |
| 23 | + private readonly Rectangle _bounds; |
| 24 | + private readonly object _toolStripEditorControl; |
| 25 | + private readonly Type _toolStripEditorControlType; |
| 26 | + private readonly ConstructorInfo _constructor; |
| 27 | + private readonly ToolStripItem _toolStripItem; |
| 28 | + private readonly Mock<DesignerFrame> _mockDesignerFrame; |
| 29 | + |
| 30 | + public ToolStripEditorManagerTests() |
| 31 | + { |
| 32 | + _mockServiceProvider = new(); |
| 33 | + _mockSite = new(); |
| 34 | + _mockDesignerHost = new(); |
| 35 | + _mockComponent = new(); |
| 36 | + _mockDesignerFrame = new(_mockSite.Object) { CallBase = true }; |
| 37 | + _behaviorService = new(_mockServiceProvider.Object, _mockDesignerFrame.Object); |
| 38 | + _toolStripItem = new ToolStripButton("Sample Button"); |
| 39 | + |
| 40 | + _mockComponent.Setup(c => c.Site).Returns(_mockSite.Object); |
| 41 | + _mockSite.Setup(s => s.GetService(typeof(BehaviorService))).Returns(_behaviorService); |
| 42 | + _mockSite.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_mockDesignerHost.Object); |
| 43 | + _editorManager = new(_mockComponent.Object); |
| 44 | + |
| 45 | + _editorControl = new(); |
| 46 | + _bounds = new(10, 20, 100, 200); |
| 47 | + |
| 48 | + _toolStripEditorControlType = typeof(ToolStripEditorManager).GetNestedType("ToolStripEditorControl", BindingFlags.NonPublic) |
| 49 | + ?? throw new InvalidOperationException("ToolStripEditorControl type not found."); |
| 50 | + |
| 51 | + _constructor = _toolStripEditorControlType?.GetConstructor([typeof(Control), typeof(Rectangle)]) |
| 52 | + ?? throw new InvalidOperationException("Constructor for ToolStripEditorControl not found."); |
| 53 | + |
| 54 | + _toolStripEditorControl = _constructor.Invoke([_editorControl, _bounds]); |
| 55 | + } |
| 56 | + |
| 57 | + public void Dispose() |
| 58 | + { |
| 59 | + _behaviorService.Dispose(); |
| 60 | + _editorControl.Dispose(); |
| 61 | + _toolStripItem.Dispose(); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void Constructor_InitializesBehaviorServiceAndDesignerHost() |
| 66 | + { |
| 67 | + _editorManager.Should().BeOfType<ToolStripEditorManager>(); |
| 68 | + |
| 69 | + BehaviorService? behaviorService = _editorManager.TestAccessor().Dynamic._behaviorService; |
| 70 | + behaviorService.Should().Be(_behaviorService); |
| 71 | + |
| 72 | + IDesignerHost? designerHost = _editorManager.TestAccessor().Dynamic._designerHost; |
| 73 | + designerHost.Should().Be(_mockDesignerHost.Object); |
| 74 | + } |
| 75 | + |
| 76 | + [WinFormsFact] |
| 77 | + public void ActivateEditor_ShouldNotAddNewEditor_WhenItemIsNull() |
| 78 | + { |
| 79 | + Action action = () => _editorManager.ActivateEditor(null); |
| 80 | + action.Should().NotThrow(); |
| 81 | + } |
| 82 | + |
| 83 | + [WinFormsFact] |
| 84 | + public void ActivateEditor_ShouldReturn_WhenItemIsSameAsCurrentItem() |
| 85 | + { |
| 86 | + _editorManager.TestAccessor().Dynamic._behaviorService = _behaviorService; |
| 87 | + _editorManager.TestAccessor().Dynamic._currentItem = _toolStripItem; |
| 88 | + |
| 89 | + Action action = () => _editorManager.ActivateEditor(_toolStripItem); |
| 90 | + action.Should().NotThrow(); |
| 91 | + |
| 92 | + ToolStripItem currentItem = _editorManager.TestAccessor().Dynamic._currentItem; |
| 93 | + currentItem.Should().Be(_toolStripItem); |
| 94 | + } |
| 95 | + |
| 96 | + [WinFormsFact] |
| 97 | + public void ActivateEditor_ShouldDeactivateCurrentEditor_WhenEditorIsNotNull() |
| 98 | + { |
| 99 | + _editorManager.TestAccessor().Dynamic._behaviorService = _behaviorService; |
| 100 | + _editorManager.TestAccessor().Dynamic._editor = _toolStripEditorControl; |
| 101 | + _editorManager.TestAccessor().Dynamic._itemDesigner = new Mock<ToolStripItemDesigner>().Object; |
| 102 | + _editorManager.TestAccessor().Dynamic._currentItem = new ToolStripButton(); |
| 103 | + |
| 104 | + _editorManager.ActivateEditor(null); |
| 105 | + |
| 106 | + _behaviorService.AdornerWindowControl.Controls.Cast<Control>().Should().NotContain((Control)_toolStripEditorControl); |
| 107 | + |
| 108 | + ToolStripTemplateNode editorUI = _editorManager.TestAccessor().Dynamic._editorUI; |
| 109 | + editorUI.Should().BeNull(); |
| 110 | + |
| 111 | + object? editor = _editorManager.TestAccessor().Dynamic._editor; |
| 112 | + editor.Should().BeNull(); |
| 113 | + |
| 114 | + ToolStripItem currentItem = _editorManager.TestAccessor().Dynamic._currentItem; |
| 115 | + currentItem.Should().BeNull(); |
| 116 | + |
| 117 | + bool? isEditorActive = _editorManager.TestAccessor().Dynamic._itemDesigner.IsEditorActive; |
| 118 | + isEditorActive.Should().BeFalse(); |
| 119 | + } |
| 120 | + |
| 121 | + [WinFormsFact] |
| 122 | + public void ActivateEditor_ShouldAddNewEditor_WhenItemIsNotNull() |
| 123 | + { |
| 124 | + Mock<ToolStrip> mockToolStrip = new(); |
| 125 | + Mock<IComponent> mockComponent = mockToolStrip.As<IComponent>(); |
| 126 | + Mock<ISite> mockSite = new(); |
| 127 | + Mock<IDesignerHost> mockDesignerHost = new(); |
| 128 | + Mock<DesignSurface> mockDesignSurface = new(); |
| 129 | + mockComponent.Setup(c => c.Site).Returns(mockSite.Object); |
| 130 | + mockSite.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(mockDesignerHost.Object); |
| 131 | + mockSite.Setup(s => s.GetService(typeof(DesignSurface))).Returns(mockDesignSurface.Object); |
| 132 | + Mock<Control> mockRootComponent = new(); |
| 133 | + mockDesignerHost.Setup(dh => dh.RootComponent).Returns(mockRootComponent.Object); |
| 134 | + |
| 135 | + Mock<ToolStripItemDesigner> mockToolStripItemDesigner = new(); |
| 136 | + Mock<ToolStripTemplateNode> mockToolStripTemplateNode = new(MockBehavior.Default, mockComponent.Object, "SampleText"); |
| 137 | + FieldInfo? centerLabelField = typeof(ToolStripTemplateNode).GetField("_centerLabel", BindingFlags.NonPublic | BindingFlags.Instance); |
| 138 | + centerLabelField?.SetValue(mockToolStripTemplateNode.Object, new ToolStripLabel("Test")); |
| 139 | + mockToolStripItemDesigner.Setup(d => d.Editor).Returns(mockToolStripTemplateNode.Object); |
| 140 | + _mockDesignerHost.Setup(dh => dh.GetDesigner(It.IsAny<ToolStripItem>())).Returns(mockToolStripItemDesigner.Object); |
| 141 | + |
| 142 | + _editorManager.ActivateEditor(_toolStripItem); |
| 143 | + |
| 144 | + ToolStripItem currentItem = _editorManager.TestAccessor().Dynamic._currentItem; |
| 145 | + currentItem.Should().Be(_toolStripItem); |
| 146 | + |
| 147 | + ToolStripTemplateNode editorUI = _editorManager.TestAccessor().Dynamic._editorUI; |
| 148 | + editorUI.Should().Be(mockToolStripTemplateNode.Object); |
| 149 | + |
| 150 | + ToolStripItemDesigner itemDesigner = _editorManager.TestAccessor().Dynamic._itemDesigner; |
| 151 | + itemDesigner.Should().Be(mockToolStripItemDesigner.Object); |
| 152 | + |
| 153 | + _mockDesignerHost.Verify(dh => dh.GetDesigner(_toolStripItem), Times.Once); |
| 154 | + mockToolStripItemDesigner.Object.IsEditorActive.Should().BeTrue(); |
| 155 | + } |
| 156 | + |
| 157 | + [Fact] |
| 158 | + public void CloseManager_ShouldNotThrowException() |
| 159 | + { |
| 160 | + Action action = ToolStripEditorManager.CloseManager; |
| 161 | + action.Should().NotThrow(); |
| 162 | + } |
| 163 | + |
| 164 | + [WinFormsFact] |
| 165 | + public void OnEditorResize_ShouldInvalidateAndUpdateBounds() |
| 166 | + { |
| 167 | + _editorManager.TestAccessor().Dynamic._editor = _toolStripEditorControl; |
| 168 | + _editorManager.TestAccessor().Dynamic.OnEditorResize(_editorManager, EventArgs.Empty); |
| 169 | + |
| 170 | + Rectangle _editorManagerBounds = _editorManager.TestAccessor().Dynamic._lastKnownEditorBounds; |
| 171 | + _editorManagerBounds.X.Should().Be(_bounds.X); |
| 172 | + _editorManagerBounds.Y.Should().Be(_bounds.Y); |
| 173 | + } |
| 174 | + |
| 175 | + [Fact] |
| 176 | + public void ToolStripEditorControl_Constructor_InitializesProperties() |
| 177 | + { |
| 178 | + VerifyProperty("Bounds1", _toolStripEditorControl, _bounds); |
| 179 | + VerifyProperty(nameof(Location), _toolStripEditorControl, new Point(_bounds.X, _bounds.Y)); |
| 180 | + VerifyProperty(nameof(Text), _toolStripEditorControl, "InSituEditorWrapper"); |
| 181 | + VerifyProperty(nameof(Size), _toolStripEditorControl, new Size(_editorControl.Size.Width, _editorControl.Size.Height)); |
| 182 | + _toolStripEditorControlType?.GetProperty("Controls")?.GetValue(_toolStripEditorControl).Should().BeOfType<Control.ControlCollection>(); |
| 183 | + } |
| 184 | + |
| 185 | + private void VerifyProperty<T>(string propertyName, object targetObject, T expectedValue) |
| 186 | + { |
| 187 | + PropertyInfo? propertyInfo = targetObject.TestAccessor().Dynamic.GetType().GetProperty(propertyName); |
| 188 | + object? propertyValue = propertyInfo?.GetValue(targetObject); |
| 189 | + propertyValue?.Should().Be(expectedValue); |
| 190 | + } |
| 191 | + |
| 192 | + [WinFormsFact] |
| 193 | + public void ToolStripEditorControl_Bounds1_Setter_UpdatesBounds() |
| 194 | + { |
| 195 | + Rectangle newBounds = new(30, 40, 150, 250); |
| 196 | + PropertyInfo? boundsProperty = _toolStripEditorControlType?.GetProperty("Bounds1"); |
| 197 | + |
| 198 | + boundsProperty.Should().NotBeNull(); |
| 199 | + if (boundsProperty is not null) |
| 200 | + { |
| 201 | + boundsProperty.SetValue(_toolStripEditorControl, newBounds); |
| 202 | + Rectangle? actualBounds = boundsProperty.GetValue(_toolStripEditorControl) as Rectangle?; |
| 203 | + actualBounds.Should().Be(newBounds); |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments