|
| 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.ComponentModel.Design.Serialization; |
| 7 | +using System.Windows.Forms.Design.Behavior; |
| 8 | +using Moq; |
| 9 | + |
| 10 | +namespace System.Windows.Forms.Design.Tests; |
| 11 | +public class StandardMenuStripVerbTests : IDisposable |
| 12 | +{ |
| 13 | + private readonly Mock<IDesignerHost> _designerHostMock = new(); |
| 14 | + private readonly Mock<IServiceProvider> _serviceProviderMock = new(); |
| 15 | + private readonly Mock<ISelectionService> _selectionServiceMock = new(); |
| 16 | + private readonly Mock<IComponentChangeService> _componentChangeServiceMock = new(); |
| 17 | + private readonly Mock<ISite> _siteMock = new(); |
| 18 | + private readonly Mock<DesignerTransaction> _mockTransaction = new(MockBehavior.Loose); |
| 19 | + |
| 20 | + private readonly DesignerFrame _designerFrame; |
| 21 | + private readonly SelectionManager _selectionManager; |
| 22 | + private readonly BehaviorService _behaviorService; |
| 23 | + private readonly DesignerActionUIService _designerActionUIService; |
| 24 | + |
| 25 | + private readonly ToolStripDesigner _designer = new(); |
| 26 | + private readonly ParentControlDesigner _parentControlDesigner = new(); |
| 27 | + private readonly MenuStrip _menuStrip = new(); |
| 28 | + |
| 29 | + public StandardMenuStripVerbTests() |
| 30 | + { |
| 31 | + _siteMock.Setup(s => s.GetService(typeof(INestedContainer))).Returns(new Mock<IContainer>().Object); |
| 32 | + _siteMock.Setup(s => s.Container).Returns(new Mock<ServiceContainer>().Object as IContainer); |
| 33 | + _siteMock.Setup(s => s.GetService(typeof(UndoEngine))).Returns(null!); |
| 34 | + _siteMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object); |
| 35 | + _siteMock.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object); |
| 36 | + |
| 37 | + _designerFrame = new(_siteMock.Object); |
| 38 | + _behaviorService = new(_serviceProviderMock.Object, _designerFrame); |
| 39 | + _siteMock.Setup(s => s.GetService(typeof(BehaviorService))).Returns(_behaviorService); |
| 40 | + _siteMock.Setup(s => s.GetService(typeof(ToolStripAdornerWindowService))).Returns(null!); |
| 41 | + _siteMock.Setup(s => s.GetService(typeof(DesignerActionService))).Returns(new Mock<DesignerActionService>(_siteMock.Object).Object); |
| 42 | + |
| 43 | + Mock<INameCreationService> nameCreationServiceMock = new(); |
| 44 | + nameCreationServiceMock.Setup(n => n.IsValidName(It.IsAny<string>())).Returns(true); |
| 45 | + _siteMock.Setup(s => s.GetService(typeof(INameCreationService))).Returns(nameCreationServiceMock.Object); |
| 46 | + _designerActionUIService = new(_siteMock.Object); |
| 47 | + _siteMock.Setup(s => s.GetService(typeof(DesignerActionUIService))).Returns(_designerActionUIService); |
| 48 | + |
| 49 | + _serviceProviderMock.Setup(s => s.GetService(typeof(IDesignerHost))).Returns(_designerHostMock.Object); |
| 50 | + _serviceProviderMock.Setup(s => s.GetService(typeof(IComponentChangeService))).Returns(new Mock<IComponentChangeService>().Object); |
| 51 | + |
| 52 | + _designerHostMock.Setup(h => h.RootComponent).Returns(_menuStrip); |
| 53 | + _designerHostMock.Setup(h => h.CreateTransaction(It.IsAny<string>())).Returns(_mockTransaction.Object); |
| 54 | + _designerHostMock.Setup(h => h.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object); |
| 55 | + _designerHostMock.Setup(h => h.AddService(typeof(ToolStripKeyboardHandlingService), It.IsAny<object>())); |
| 56 | + _designerHostMock.Setup(h => h.AddService(typeof(ISupportInSituService), It.IsAny<object>())); |
| 57 | + _designerHostMock.Setup(h => h.AddService(typeof(DesignerActionService), It.IsAny<object>())); |
| 58 | + _designerHostMock.Setup(h => h.GetDesigner(_menuStrip)).Returns(_parentControlDesigner); |
| 59 | + _designerHostMock.Setup(h => h.AddService(typeof(DesignerActionUIService), It.IsAny<object>())); |
| 60 | + Mock<ToolStripMenuItem> toolStripMenuItemMock = new(); |
| 61 | + using ToolStripDropDownMenu toolStripDropDownMenu = new(); |
| 62 | + toolStripMenuItemMock.Object.DropDown = toolStripDropDownMenu; |
| 63 | + |
| 64 | + string[] menuItemImageNames = |
| 65 | + [ |
| 66 | + "file", "new", "open", "save", "saveAs", "print", "printPreview", "cut", "copy", "paste", "undo", "redo", |
| 67 | + "delete", "selectAll", "edit", "exit", "saveAs", "saveAll", "tool", "tools", "customize", "options", "help", |
| 68 | + "contents", "index", "search", "about", "cut", "copy", "Paste", "undo", "redo" |
| 69 | + ]; |
| 70 | + |
| 71 | + foreach (string item in menuItemImageNames) |
| 72 | + { |
| 73 | + _designerHostMock.Setup(h => h.CreateComponent(typeof(ToolStripMenuItem), item + "ToolStripMenuItem")).Returns(toolStripMenuItemMock.Object); |
| 74 | + } |
| 75 | + |
| 76 | + _designerHostMock.Setup(h => h.CreateComponent(typeof(ToolStripSeparator), "toolStripSeparator")).Returns(new Mock<ToolStripSeparator>().Object); |
| 77 | + _selectionServiceMock.Setup(s => s.GetComponentSelected(_menuStrip)).Returns(true); |
| 78 | + _siteMock.Setup(s => s.GetService(typeof(ISelectionService))).Returns(_selectionServiceMock.Object); |
| 79 | + _designerHostMock.Setup(h => h.AddService(typeof(ISelectionService), _selectionServiceMock.Object)); |
| 80 | + _serviceProviderMock.Setup(s => s.GetService(typeof(ISelectionService))).Returns(_selectionServiceMock.Object); |
| 81 | + _selectionManager = new(_serviceProviderMock.Object, _behaviorService!); |
| 82 | + _siteMock.Setup(s => s.GetService(typeof(SelectionManager))).Returns(_selectionManager); |
| 83 | + |
| 84 | + _menuStrip.Site = _siteMock.Object; |
| 85 | + |
| 86 | + _designer.Initialize(_menuStrip); |
| 87 | + _parentControlDesigner.Initialize(_menuStrip); |
| 88 | + |
| 89 | + IComponent[] components = []; |
| 90 | + ComponentCollection componentCollection = new(components); |
| 91 | + _selectionServiceMock.Setup(s => s.GetSelectedComponents()).Returns(components); |
| 92 | + |
| 93 | + Mock<IContainer> containerMock = new(); |
| 94 | + containerMock.Setup(c => c.Components).Returns(componentCollection); |
| 95 | + _designerHostMock.Setup(d => d.Container).Returns(containerMock.Object); |
| 96 | + } |
| 97 | + |
| 98 | + public void Dispose() |
| 99 | + { |
| 100 | + _menuStrip?.Dispose(); |
| 101 | + _parentControlDesigner.Dispose(); |
| 102 | + _behaviorService?.Dispose(); |
| 103 | + _selectionManager?.Dispose(); |
| 104 | + _designerFrame.Dispose(); |
| 105 | + _designer.Dispose(); |
| 106 | + } |
| 107 | + |
| 108 | + [WinFormsFact] |
| 109 | + public void StandardMenuStripVerb_Ctor() |
| 110 | + { |
| 111 | + StandardMenuStripVerb standardMenuStripVerb = new(_designer); |
| 112 | + standardMenuStripVerb.Should().BeOfType<StandardMenuStripVerb>(); |
| 113 | + ToolStripDesigner toolStripDesigner = standardMenuStripVerb.TestAccessor().Dynamic._designer; |
| 114 | + toolStripDesigner.Should().Be(_designer); |
| 115 | + } |
| 116 | + |
| 117 | + [WinFormsFact] |
| 118 | + public void Ctor_ThrowsIfDesignerIsNull() |
| 119 | + { |
| 120 | + Action action = () => new StandardMenuStripVerb(null); |
| 121 | + action.Should().Throw<ArgumentNullException>(); |
| 122 | + } |
| 123 | + |
| 124 | + [WinFormsFact] |
| 125 | + public void Ctor_ThrowsIfComponentOrSiteIsNull() |
| 126 | + { |
| 127 | + Action action = () => |
| 128 | + { |
| 129 | + ToolStripDesigner toolStripDesigner = new(); |
| 130 | + toolStripDesigner.Initialize(new MenuStrip()); |
| 131 | + |
| 132 | + new StandardMenuStripVerb(toolStripDesigner); |
| 133 | + }; |
| 134 | + |
| 135 | + action.Should().Throw<InvalidOperationException>(); |
| 136 | + } |
| 137 | + |
| 138 | + [WinFormsFact] |
| 139 | + public void Ctor_AssignsHostAndChangeServiceIfAvailable() |
| 140 | + { |
| 141 | + StandardMenuStripVerb standardMenuStripVerb = new(_designer); |
| 142 | + IDesignerHost host = standardMenuStripVerb.TestAccessor().Dynamic._host; |
| 143 | + IComponentChangeService changeService = standardMenuStripVerb.TestAccessor().Dynamic._changeService; |
| 144 | + |
| 145 | + host.Should().BeSameAs(_designerHostMock.Object); |
| 146 | + changeService.Should().BeSameAs(_componentChangeServiceMock.Object); |
| 147 | + } |
| 148 | +} |
0 commit comments