|
| 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 Moq; |
| 7 | + |
| 8 | +namespace System.Windows.Forms.Design.Tests; |
| 9 | + |
| 10 | +public sealed class ToolStripInSituServiceTests : IDisposable |
| 11 | +{ |
| 12 | + private readonly Mock<IServiceProvider> _mockServiceProvider; |
| 13 | + private readonly Mock<IDesignerHost> _mockDesignerHost; |
| 14 | + private readonly Mock<IComponentChangeService> _mockComponentChangeService; |
| 15 | + private readonly Mock<ToolStripDesigner> _mockToolStripDesigner; |
| 16 | + private readonly Mock<ToolStripItemDesigner> _mockToolStripItemDesigner; |
| 17 | + private readonly Mock<ISelectionService> _mockSelectionService; |
| 18 | + private readonly Mock<ToolStripKeyboardHandlingService> _mockToolStripKeyboardHandlingService; |
| 19 | + private readonly ToolStripInSituService _inSituService; |
| 20 | + private bool _isInSituServiceDisposed; |
| 21 | + |
| 22 | + public ToolStripInSituServiceTests() |
| 23 | + { |
| 24 | + _mockServiceProvider = new(); |
| 25 | + _mockDesignerHost = new(); |
| 26 | + _mockComponentChangeService = new(); |
| 27 | + _mockToolStripDesigner = new(); |
| 28 | + _mockToolStripItemDesigner = new(); |
| 29 | + _mockSelectionService = new(); |
| 30 | + _mockToolStripKeyboardHandlingService = new(_mockServiceProvider.Object); |
| 31 | + |
| 32 | + _mockServiceProvider.Setup(sp => sp.GetService(typeof(IDesignerHost))).Returns(_mockDesignerHost.Object); |
| 33 | + _mockDesignerHost.Setup(dh => dh.GetService(typeof(IComponentChangeService))).Returns(_mockComponentChangeService.Object); |
| 34 | + _mockServiceProvider.Setup(sp => sp.GetService(typeof(ISelectionService))).Returns(_mockSelectionService.Object); |
| 35 | + _mockServiceProvider.Setup(sp => sp.GetService(typeof(ToolStripKeyboardHandlingService))).Returns(_mockToolStripKeyboardHandlingService.Object); |
| 36 | + |
| 37 | + _inSituService = new(_mockServiceProvider.Object); |
| 38 | + _inSituService.TestAccessor().Dynamic._toolDesigner = _mockToolStripDesigner.Object; |
| 39 | + _inSituService.TestAccessor().Dynamic._toolItemDesigner = _mockToolStripItemDesigner.Object; |
| 40 | + _inSituService.TestAccessor().Dynamic._componentChangeService = _mockComponentChangeService.Object; |
| 41 | + } |
| 42 | + |
| 43 | + public void Dispose() |
| 44 | + { |
| 45 | + if (!_isInSituServiceDisposed) |
| 46 | + { |
| 47 | + _inSituService.Dispose(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + [Fact] |
| 52 | + public void Dispose_DisposesToolDesigner() |
| 53 | + { |
| 54 | + _inSituService.Dispose(); |
| 55 | + _isInSituServiceDisposed = true; |
| 56 | + |
| 57 | + object toolDesignerValue = _inSituService.TestAccessor().Dynamic._toolDesigner; |
| 58 | + toolDesignerValue.Should().BeNull(); |
| 59 | + } |
| 60 | + |
| 61 | + [Fact] |
| 62 | + public void Dispose_DisposesToolItemDesigner() |
| 63 | + { |
| 64 | + _inSituService.Dispose(); |
| 65 | + _isInSituServiceDisposed = true; |
| 66 | + |
| 67 | + object toolItemDesignerValue = _inSituService.TestAccessor().Dynamic._toolItemDesigner; |
| 68 | + toolItemDesignerValue.Should().BeNull(); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void Dispose_UnsubscribesFromComponentChangeService() |
| 73 | + { |
| 74 | + _inSituService.Dispose(); |
| 75 | + _isInSituServiceDisposed = true; |
| 76 | + |
| 77 | + object componentChangeServiceValue = _inSituService.TestAccessor().Dynamic._componentChangeService; |
| 78 | + componentChangeServiceValue.Should().BeNull(); |
| 79 | + } |
| 80 | + |
| 81 | + [Fact] |
| 82 | + public void ToolStripKeyBoardService_ReturnsServiceInstance() |
| 83 | + { |
| 84 | + object toolStripKeyBoardService = _inSituService.TestAccessor().Dynamic.ToolStripKeyBoardService; |
| 85 | + |
| 86 | + toolStripKeyBoardService.Should().NotBeNull(); |
| 87 | + toolStripKeyBoardService.Should().BeAssignableTo<ToolStripKeyboardHandlingService>(); |
| 88 | + toolStripKeyBoardService.Should().Be(_mockToolStripKeyboardHandlingService.Object); |
| 89 | + } |
| 90 | + |
| 91 | + [Fact] |
| 92 | + public void IgnoreMessages_ReturnsFalse_WhenSelectionServiceIsNull() |
| 93 | + { |
| 94 | + _mockServiceProvider.Setup(sp => sp.GetService(typeof(ISelectionService))).Returns(null!); |
| 95 | + bool result = _inSituService.IgnoreMessages; |
| 96 | + result.Should().BeFalse(); |
| 97 | + } |
| 98 | + |
| 99 | + [Fact] |
| 100 | + public void IgnoreMessages_ReturnsFalse_WhenDesignerHostIsNull() |
| 101 | + { |
| 102 | + _mockServiceProvider.Setup(sp => sp.GetService(typeof(IDesignerHost))).Returns(null!); |
| 103 | + bool result = _inSituService.IgnoreMessages; |
| 104 | + result.Should().BeFalse(); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public void GetEditWindow_ReturnsZero_WhenNoDesignerIsNotNull() |
| 109 | + { |
| 110 | + IntPtr result = _inSituService.GetEditWindow(); |
| 111 | + result.Should().Be(IntPtr.Zero); |
| 112 | + } |
| 113 | + |
| 114 | + [Fact] |
| 115 | + public void OnComponentRemoved_RemovesService_WhenNoToolStripPresent() |
| 116 | + { |
| 117 | + Mock<IContainer> mockContainer = new(); |
| 118 | + ComponentCollection componentCollection = new(Array.Empty<IComponent>()); |
| 119 | + |
| 120 | + mockContainer.Setup(c => c.Components).Returns(componentCollection); |
| 121 | + _mockDesignerHost.Setup(dh => dh.Container).Returns(mockContainer.Object); |
| 122 | + _mockServiceProvider.Setup(sp => sp.GetService(typeof(ISupportInSituService))).Returns(_inSituService); |
| 123 | + |
| 124 | + _inSituService.TestAccessor().Dynamic.OnComponentRemoved(null, new ComponentEventArgs(new Component())); |
| 125 | + |
| 126 | + _mockDesignerHost.Verify(dh => dh.RemoveService(typeof(ISupportInSituService)), Times.Once); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public void OnComponentRemoved_DoesNotRemoveService_WhenToolStripPresent() |
| 131 | + { |
| 132 | + Mock<ToolStrip> mockToolStrip = new(); |
| 133 | + Mock<IContainer> mockContainer = new(); |
| 134 | + ComponentCollection realComponentCollection = new([mockToolStrip.Object]); |
| 135 | + |
| 136 | + mockContainer.Setup(c => c.Components).Returns(realComponentCollection); |
| 137 | + _mockDesignerHost.Setup(dh => dh.Container).Returns(mockContainer.Object); |
| 138 | + _mockServiceProvider.Setup(sp => sp.GetService(typeof(ISupportInSituService))).Returns(_inSituService); |
| 139 | + |
| 140 | + _inSituService.TestAccessor().Dynamic.OnComponentRemoved(null, new ComponentEventArgs(new Component())); |
| 141 | + |
| 142 | + _mockDesignerHost.Verify(dh => dh.RemoveService(typeof(ISupportInSituService)), Times.Never); |
| 143 | + } |
| 144 | +} |
0 commit comments