|
| 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.Windows.Forms.Design.Tests.Mocks; |
| 7 | +using Moq; |
| 8 | + |
| 9 | +namespace System.Windows.Forms.Design.Tests; |
| 10 | + |
| 11 | +public class BindingNavigatorDesignerTests : IDisposable |
| 12 | +{ |
| 13 | + private readonly Mock<IDesignerHost> _designerHostMock = new(); |
| 14 | + private readonly Mock<IServiceProvider> _serviceProviderMock = new(); |
| 15 | + private readonly Mock<IComponentChangeService> _componentChangeServiceMock = new(); |
| 16 | + private readonly Mock<DesignerTransaction> _mockTransaction = new(MockBehavior.Loose); |
| 17 | + private Mock<ISite>? _siteMock; |
| 18 | + private readonly BindingNavigatorDesigner _designer = new(); |
| 19 | + private readonly BindingNavigator _bindingNavigator = new(); |
| 20 | + |
| 21 | + public BindingNavigatorDesignerTests() |
| 22 | + { |
| 23 | + _bindingNavigator = MockMinimalControl(); |
| 24 | + _designer.Initialize(_bindingNavigator); |
| 25 | + } |
| 26 | + |
| 27 | + public void Dispose() |
| 28 | + { |
| 29 | + _bindingNavigator?.Dispose(); |
| 30 | + _designer?.Dispose(); |
| 31 | + } |
| 32 | + |
| 33 | + private BindingNavigator MockMinimalControl() |
| 34 | + { |
| 35 | + _siteMock = MockSite.CreateMockSiteWithDesignerHost(_designerHostMock.Object, MockBehavior.Loose); |
| 36 | + _siteMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object); |
| 37 | + _siteMock.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object); |
| 38 | + |
| 39 | + _serviceProviderMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object); |
| 40 | + _serviceProviderMock.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object); |
| 41 | + |
| 42 | + _designerHostMock.Setup(h => h.RootComponent).Returns(_bindingNavigator); |
| 43 | + _designerHostMock.Setup(h => h.CreateTransaction(It.IsAny<string>())).Returns(_mockTransaction.Object); |
| 44 | + _designerHostMock.Setup(h => h.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object); |
| 45 | + |
| 46 | + Mock<IContainer> containerMock = new(); |
| 47 | + _designerHostMock.Setup(h => h.Container).Returns(containerMock.Object); |
| 48 | + |
| 49 | + _bindingNavigator.Site = _siteMock.Object; |
| 50 | + return _bindingNavigator; |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public void InitializeNewComponent_ShouldCallAddStandardItems() |
| 55 | + { |
| 56 | + Dictionary<string, object> defaultValues = new(); |
| 57 | + |
| 58 | + _designer.InitializeNewComponent(defaultValues); |
| 59 | + |
| 60 | + _bindingNavigator.Items.Count.Should().Be(11); |
| 61 | + _bindingNavigator.ShowItemToolTips.Should().BeTrue(); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void InitializeNewComponent_ShouldSiteAllItems() |
| 66 | + { |
| 67 | + Mock<IContainer> containerMock = new(); |
| 68 | + _designerHostMock.Setup(h => h.Container).Returns(containerMock.Object); |
| 69 | + |
| 70 | + Dictionary<string, object> defaultValues = new(); |
| 71 | + |
| 72 | + _designer.InitializeNewComponent(defaultValues); |
| 73 | + |
| 74 | + containerMock.Verify(c => c.Add(It.IsAny<IComponent>(), It.IsAny<string>()), Times.AtLeastOnce); |
| 75 | + } |
| 76 | + |
| 77 | + public static IEnumerable<object[]> NavigationItemTestData() |
| 78 | + { |
| 79 | + yield return new object[] |
| 80 | + { |
| 81 | + (Action<BindingNavigator, ToolStripItem>)((nav, item) => nav.MoveFirstItem = item), |
| 82 | + (Func<BindingNavigator, ToolStripItem?>)(nav => nav.MoveFirstItem) |
| 83 | + }; |
| 84 | + yield return new object[] |
| 85 | + { |
| 86 | + (Action<BindingNavigator, ToolStripItem>)((nav, item) => nav.MovePreviousItem = item), |
| 87 | + (Func<BindingNavigator, ToolStripItem?>)(nav => nav.MovePreviousItem) |
| 88 | + }; |
| 89 | + yield return new object[] |
| 90 | + { |
| 91 | + (Action<BindingNavigator, ToolStripItem>)((nav, item) => nav.MoveNextItem = item), |
| 92 | + (Func<BindingNavigator, ToolStripItem?>)(nav => nav.MoveNextItem) |
| 93 | + }; |
| 94 | + yield return new object[] |
| 95 | + { |
| 96 | + (Action<BindingNavigator, ToolStripItem>)((nav, item) => nav.MoveLastItem = item), |
| 97 | + (Func<BindingNavigator, ToolStripItem?>)(nav => nav.MoveLastItem) |
| 98 | + }; |
| 99 | + yield return new object[] |
| 100 | + { |
| 101 | + (Action<BindingNavigator, ToolStripItem>)((nav, item) => nav.PositionItem = item), |
| 102 | + (Func<BindingNavigator, ToolStripItem?>)(nav => nav.PositionItem) |
| 103 | + }; |
| 104 | + yield return new object[] |
| 105 | + { |
| 106 | + (Action<BindingNavigator, ToolStripItem>)((nav, item) => nav.CountItem = item), |
| 107 | + (Func<BindingNavigator, ToolStripItem?>)(nav => nav.CountItem) |
| 108 | + }; |
| 109 | + yield return new object[] |
| 110 | + { |
| 111 | + (Action<BindingNavigator, ToolStripItem>)((nav, item) => nav.AddNewItem = item), |
| 112 | + (Func<BindingNavigator, ToolStripItem?>)(nav => nav.AddNewItem) |
| 113 | + }; |
| 114 | + yield return new object[] |
| 115 | + { |
| 116 | + (Action<BindingNavigator, ToolStripItem>)((nav, item) => nav.DeleteItem = item), |
| 117 | + (Func<BindingNavigator, ToolStripItem?>)(nav => nav.DeleteItem) |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + [Theory] |
| 122 | + [MemberData(nameof(NavigationItemTestData))] |
| 123 | + public void ComponentChangeService_ComponentRemoved_ShouldSetNavigationItemToNull( |
| 124 | + Action<BindingNavigator, ToolStripItem> setter, |
| 125 | + Func<BindingNavigator, ToolStripItem?> getter) |
| 126 | + { |
| 127 | + using ToolStripButton item = new(); |
| 128 | + setter(_bindingNavigator, item); |
| 129 | + |
| 130 | + getter(_bindingNavigator).Should().Be(item); |
| 131 | + |
| 132 | + ComponentEventArgs args = new(item); |
| 133 | + _designer.TestAccessor().Dynamic.ComponentChangeService_ComponentRemoved(null, args); |
| 134 | + |
| 135 | + getter(_bindingNavigator).Should().BeNull(); |
| 136 | + } |
| 137 | + |
| 138 | + [Fact] |
| 139 | + public void ComponentChangeService_ComponentRemovedOtherComponent_ShouldNotChangeProperties() |
| 140 | + { |
| 141 | + using ToolStripButton deleteItem = new(); |
| 142 | + using ToolStripButton otherItem = new(); |
| 143 | + _bindingNavigator.DeleteItem = deleteItem; |
| 144 | + |
| 145 | + ComponentEventArgs args = new(otherItem); |
| 146 | + _designer.TestAccessor().Dynamic.ComponentChangeService_ComponentRemoved(null, args); |
| 147 | + |
| 148 | + _bindingNavigator.DeleteItem.Should().Be(deleteItem); |
| 149 | + } |
| 150 | + |
| 151 | + [Fact] |
| 152 | + public void ComponentChangeService_ComponentChangedDifferentComponent_ShouldIgnoreChange() |
| 153 | + { |
| 154 | + using ToolStripLabel countItem = new() { Text = "Original format" }; |
| 155 | + using ToolStripLabel otherItem = new() { Text = "Other text" }; |
| 156 | + _bindingNavigator.CountItem = countItem; |
| 157 | + _bindingNavigator.CountItemFormat = "Original format"; |
| 158 | + |
| 159 | + PropertyDescriptor? textProperty = TypeDescriptor.GetProperties(otherItem)["Text"]; |
| 160 | + ComponentChangedEventArgs args = new(otherItem, textProperty, "old text", "Other text"); |
| 161 | + |
| 162 | + _designer.TestAccessor().Dynamic.ComponentChangeService_ComponentChanged(null, args); |
| 163 | + |
| 164 | + _bindingNavigator.CountItemFormat.Should().Be("Original format"); |
| 165 | + } |
| 166 | + |
| 167 | + [Fact] |
| 168 | + public void ComponentChangeService_ComponentChangedDifferentProperty_ShouldIgnoreChange() |
| 169 | + { |
| 170 | + using ToolStripLabel countItem = new() { Text = "Original format" }; |
| 171 | + _bindingNavigator.CountItem = countItem; |
| 172 | + _bindingNavigator.CountItemFormat = "Original format"; |
| 173 | + |
| 174 | + PropertyDescriptor? visibleProperty = TypeDescriptor.GetProperties(countItem)["Visible"]; |
| 175 | + ComponentChangedEventArgs args = new(countItem, visibleProperty, false, true); |
| 176 | + |
| 177 | + _designer.TestAccessor().Dynamic.ComponentChangeService_ComponentChanged(null, args); |
| 178 | + |
| 179 | + _bindingNavigator.CountItemFormat.Should().Be("Original format"); |
| 180 | + } |
| 181 | +} |
0 commit comments