|
| 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 Moq; |
| 8 | + |
| 9 | +namespace System.Windows.Forms.Design.Tests; |
| 10 | + |
| 11 | +public class StatusCommandUITests |
| 12 | +{ |
| 13 | + private readonly Mock<IServiceProvider> _mockServiceProvider; |
| 14 | + private readonly Mock<IMenuCommandService> _mockMenuCommandService; |
| 15 | + private readonly Mock<MenuCommand> _mockMenuCommand; |
| 16 | + private readonly StatusCommandUI _statusCommandUI; |
| 17 | + |
| 18 | + public StatusCommandUITests() |
| 19 | + { |
| 20 | + _mockServiceProvider = new(); |
| 21 | + _mockMenuCommandService = new(); |
| 22 | + _mockMenuCommand = new(null!, null!); |
| 23 | + |
| 24 | + _mockServiceProvider |
| 25 | + .Setup(sp => sp.GetService(typeof(IMenuCommandService))) |
| 26 | + .Returns(_mockMenuCommandService.Object); |
| 27 | + |
| 28 | + _mockMenuCommandService |
| 29 | + .Setup(mcs => mcs.FindCommand(MenuCommands.SetStatusRectangle)) |
| 30 | + .Returns(_mockMenuCommand.Object); |
| 31 | + |
| 32 | + _statusCommandUI = new(_mockServiceProvider.Object); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public void SetStatusInformation_WithNullComponent_ShouldNotInvokeCommand() |
| 37 | + { |
| 38 | + _statusCommandUI.SetStatusInformation(selectedComponent: null); |
| 39 | + _mockMenuCommand.Verify(mc => mc.Invoke(It.IsAny<Rectangle>()), Times.Never); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public void SetStatusInformation_WithControlComponent_ShouldInvokeWithOriginalBounds() |
| 44 | + { |
| 45 | + using Control control = new() { Bounds = new(10, 20, 30, 40) }; |
| 46 | + TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(control), new(10, 20, 30, 40)); |
| 47 | + control.IsHandleCreated.Should().BeFalse(); |
| 48 | + } |
| 49 | + |
| 50 | + [Fact] |
| 51 | + public void SetStatusInformation_WithCustomComponentHavingBounds_ShouldInvokeWithOriginalBounds() |
| 52 | + { |
| 53 | + using CustomComponent component = new() { Bounds = new(10, 20, 30, 40) }; |
| 54 | + TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(component), new(10, 20, 30, 40)); |
| 55 | + } |
| 56 | + |
| 57 | + [Fact] |
| 58 | + public void SetStatusInformation_WithComponentWithoutBounds_ShouldInvokeWithEmptyRectangle() |
| 59 | + { |
| 60 | + using Component component = new(); |
| 61 | + TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(component), Rectangle.Empty); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void SetStatusInformation_WithRectangle_ShouldInvokeWithSameRectangle() |
| 66 | + { |
| 67 | + Rectangle bounds = new(10, 20, 30, 40); |
| 68 | + TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(bounds), bounds); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public void SetStatusInformation_WithEmptyRectangle_ShouldInvokeWithEmptyRectangle() |
| 73 | + { |
| 74 | + Rectangle bounds = Rectangle.Empty; |
| 75 | + TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(bounds), bounds); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public void SetStatusInformation_WithControlAndLocation_ShouldInvokeWithUpdatedBounds() |
| 80 | + { |
| 81 | + using Control control = new() { Bounds = new(10, 20, 30, 40) }; |
| 82 | + Point location = new(50, 60); |
| 83 | + TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(control, location), new(50, 60, 30, 40)); |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + public void SetStatusInformation_WithComponentHavingBoundsAndLocation_ShouldInvokeWithUpdatedBounds() |
| 88 | + { |
| 89 | + using CustomComponent component = new() { Bounds = new(10, 20, 30, 40) }; |
| 90 | + Point location = new(50, 60); |
| 91 | + TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(component, location), new(50, 60, 30, 40)); |
| 92 | + } |
| 93 | + |
| 94 | + [Fact] |
| 95 | + public void SetStatusInformation_WithComponentHavingLocationButNoBounds_ShouldInvokeWithEmptyRectangle() |
| 96 | + { |
| 97 | + using Component component = new(); |
| 98 | + Point location = new(50, 60); |
| 99 | + _statusCommandUI.SetStatusInformation(component, location); |
| 100 | + _mockMenuCommand.Verify(cmd => cmd.Invoke(new Rectangle(50, 60, 0, 0)), Times.Once); |
| 101 | + } |
| 102 | + |
| 103 | + [Fact] |
| 104 | + public void SetStatusInformation_WithLocationAsEmpty_ShouldNotChangeBounds() |
| 105 | + { |
| 106 | + using Control control = new() { Bounds = new(10, 20, 30, 40) }; |
| 107 | + Point location = Point.Empty; |
| 108 | + TestSetStatusInformation(() => _statusCommandUI.SetStatusInformation(control, location), new(10, 20, 30, 40)); |
| 109 | + } |
| 110 | + |
| 111 | + [Fact] |
| 112 | + public void SetStatusInformation_NullComponentWithLocation_ShouldNotInvokeCommand() |
| 113 | + { |
| 114 | + _statusCommandUI.SetStatusInformation(null, new Point(10, 10)); |
| 115 | + _mockMenuCommand.Verify(cmd => cmd.Invoke(It.IsAny<Rectangle>()), Times.Never); |
| 116 | + } |
| 117 | + |
| 118 | + private void TestSetStatusInformation(Action action, Rectangle expectedRectangle) |
| 119 | + { |
| 120 | + action.Invoke(); |
| 121 | + VerifyInvoke(expectedRectangle); |
| 122 | + } |
| 123 | + |
| 124 | + private void VerifyInvoke(Rectangle expectedRectangle) |
| 125 | + { |
| 126 | + _mockMenuCommand.Verify(mc => mc.Invoke(It.Is<Rectangle>(r => |
| 127 | + r.X == expectedRectangle.X |
| 128 | + && r.Y == expectedRectangle.Y |
| 129 | + && r.Width == expectedRectangle.Width |
| 130 | + && r.Height == expectedRectangle.Height)), Times.Once); |
| 131 | + } |
| 132 | + |
| 133 | + private class CustomComponent : Component |
| 134 | + { |
| 135 | + public Rectangle Bounds { get; set; } |
| 136 | + } |
| 137 | +} |
0 commit comments