|
| 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.Design; |
| 5 | +using System.Drawing; |
| 6 | +using Moq; |
| 7 | + |
| 8 | +namespace System.Windows.Forms.Design.Tests; |
| 9 | + |
| 10 | +public class StandardCommandToolStripMenuItemTests : IDisposable |
| 11 | +{ |
| 12 | + private readonly Mock<IServiceProvider> _serviceProviderMock; |
| 13 | + private readonly Mock<IMenuCommandService> _menuCommandServiceMock; |
| 14 | + private readonly CommandID _commandID; |
| 15 | + private readonly MenuCommand _menuCommand; |
| 16 | + private readonly StandardCommandToolStripMenuItem _item; |
| 17 | + |
| 18 | + public StandardCommandToolStripMenuItemTests() |
| 19 | + { |
| 20 | + _serviceProviderMock = new(); |
| 21 | + _menuCommandServiceMock = new(); |
| 22 | + _commandID = new(Guid.NewGuid(), 1); |
| 23 | + _menuCommand = new((sender, e) => { }, _commandID); |
| 24 | + |
| 25 | + _serviceProviderMock.Setup(sp => sp.GetService(typeof(IMenuCommandService))) |
| 26 | + .Returns(_menuCommandServiceMock.Object); |
| 27 | + _menuCommandServiceMock.Setup(mcs => mcs.FindCommand(_commandID)) |
| 28 | + .Returns(_menuCommand); |
| 29 | + |
| 30 | + _item = new(_commandID, "Test Text", "TestImage", _serviceProviderMock.Object); |
| 31 | + } |
| 32 | + |
| 33 | + public void Dispose() => _item.Dispose(); |
| 34 | + |
| 35 | + [WinFormsFact] |
| 36 | + public void Constructor_InitializesPropertiesCorrectly() |
| 37 | + { |
| 38 | + _item.MenuService?.FindCommand(_commandID)?.CommandID.Should().Be(_commandID); |
| 39 | + string name = _item.TestAccessor().Dynamic._name; |
| 40 | + |
| 41 | + _item.Text.Should().Be("Test Text"); |
| 42 | + name.Should().BeSameAs("TestImage"); |
| 43 | + } |
| 44 | + |
| 45 | + [WinFormsFact] |
| 46 | + public void RefreshItem_UpdatesProperties() |
| 47 | + { |
| 48 | + _menuCommand.Visible = false; |
| 49 | + _menuCommand.Enabled = false; |
| 50 | + _menuCommand.Checked = true; |
| 51 | + |
| 52 | + _item.RefreshItem(); |
| 53 | + |
| 54 | + _item.Visible.Should().BeFalse(); |
| 55 | + _item.Enabled.Should().BeFalse(); |
| 56 | + _item.Checked.Should().BeTrue(); |
| 57 | + } |
| 58 | + |
| 59 | + [WinFormsFact] |
| 60 | + public void MenuService_RetrievesMenuCommandService() |
| 61 | + { |
| 62 | + IMenuCommandService? menuService = _item.MenuService; |
| 63 | + |
| 64 | + menuService.Should().Be(_menuCommandServiceMock.Object); |
| 65 | + } |
| 66 | + |
| 67 | + [WinFormsFact] |
| 68 | + public void Image_ReturnsCachedImageOnSubsequentAccesses() |
| 69 | + { |
| 70 | + Image? firstAccessImage = _item.Image; |
| 71 | + Image? secondAccessImage = _item.Image; |
| 72 | + |
| 73 | + firstAccessImage.Should().BeSameAs(secondAccessImage); |
| 74 | + } |
| 75 | + |
| 76 | + [WinFormsFact] |
| 77 | + public void Item_OnClick_WithNullMenuCommand() |
| 78 | + { |
| 79 | + Action action = _item.PerformClick; |
| 80 | + action.Should().NotThrow(); |
| 81 | + } |
| 82 | + |
| 83 | + [WinFormsFact] |
| 84 | + public void OnClick_InvokesGlobalCommand_WhenMenuCommandIsNull() |
| 85 | + { |
| 86 | + _menuCommandServiceMock.Setup(mcs => mcs.FindCommand(_commandID)) |
| 87 | + .Returns((MenuCommand?)null); |
| 88 | + _menuCommandServiceMock.Setup(mcs => mcs.GlobalInvoke(_commandID)) |
| 89 | + .Returns(true); |
| 90 | + |
| 91 | + StandardCommandToolStripMenuItem item = new(_commandID, "Test Text", "TestImage", _serviceProviderMock.Object); |
| 92 | + item.PerformClick(); |
| 93 | + |
| 94 | + _menuCommandServiceMock.Verify(mcs => mcs.GlobalInvoke(_commandID), Times.Once); |
| 95 | + } |
| 96 | +} |
0 commit comments