-
Notifications
You must be signed in to change notification settings - Fork 1k
Issue 10773 add coverage for ToolStripInSituService #13297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Tanya-Solyanik
merged 5 commits into
dotnet:main
from
ricardobossan:Issue_10773_Add_Coverage_For_ToolStripInSituService
Apr 16, 2025
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
194 changes: 194 additions & 0 deletions
194
...s.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ToolStripInSituServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.ComponentModel; | ||
using System.ComponentModel.Design; | ||
using Moq; | ||
|
||
namespace System.Windows.Forms.Design.Tests; | ||
|
||
public sealed class ToolStripInSituServiceTests : IDisposable | ||
{ | ||
private readonly Mock<IServiceProvider> _mockServiceProvider; | ||
private readonly Mock<IDesignerHost> _mockDesignerHost; | ||
private readonly Mock<IComponentChangeService> _mockComponentChangeService; | ||
private readonly Mock<ToolStripDesigner> _mockToolStripDesigner; | ||
private readonly Mock<ToolStripItemDesigner> _mockToolStripItemDesigner; | ||
private readonly Mock<ISelectionService> _mockSelectionService; | ||
private readonly Mock<ToolStripKeyboardHandlingService> _mockToolStripKeyboardHandlingService; | ||
private readonly ToolStripInSituService _inSituService; | ||
private bool _isInSituServiceDisposed; | ||
|
||
public ToolStripInSituServiceTests() | ||
{ | ||
_mockServiceProvider = new(); | ||
_mockDesignerHost = new(); | ||
_mockComponentChangeService = new(); | ||
_mockToolStripDesigner = new(); | ||
_mockToolStripItemDesigner = new(); | ||
_mockSelectionService = new(); | ||
_mockToolStripKeyboardHandlingService = new(_mockServiceProvider.Object); | ||
|
||
_mockServiceProvider.Setup(sp => sp.GetService(typeof(IDesignerHost))).Returns(_mockDesignerHost.Object); | ||
_mockDesignerHost.Setup(dh => dh.GetService(typeof(IComponentChangeService))).Returns(_mockComponentChangeService.Object); | ||
_mockServiceProvider.Setup(sp => sp.GetService(typeof(ISelectionService))).Returns(_mockSelectionService.Object); | ||
_mockServiceProvider.Setup(sp => sp.GetService(typeof(ToolStripKeyboardHandlingService))).Returns(_mockToolStripKeyboardHandlingService.Object); | ||
|
||
_inSituService = new(_mockServiceProvider.Object); | ||
_inSituService.TestAccessor().Dynamic._toolDesigner = _mockToolStripDesigner.Object; | ||
_inSituService.TestAccessor().Dynamic._toolItemDesigner = _mockToolStripItemDesigner.Object; | ||
_inSituService.TestAccessor().Dynamic._componentChangeService = _mockComponentChangeService.Object; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (!_isInSituServiceDisposed) | ||
{ | ||
_inSituService.Dispose(); | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Dispose_DisposesToolDesigner() | ||
{ | ||
object toolDesignerValue = _inSituService.TestAccessor().Dynamic._toolDesigner; | ||
toolDesignerValue.Should().NotBeNull(); | ||
|
||
_inSituService.Dispose(); | ||
_isInSituServiceDisposed = true; | ||
|
||
toolDesignerValue = _inSituService.TestAccessor().Dynamic._toolDesigner; | ||
toolDesignerValue.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Dispose_DisposesToolItemDesigner() | ||
{ | ||
object toolItemDesignerValue = _inSituService.TestAccessor().Dynamic._toolItemDesigner; | ||
toolItemDesignerValue.Should().NotBeNull(); | ||
|
||
_inSituService.Dispose(); | ||
_isInSituServiceDisposed = true; | ||
|
||
toolItemDesignerValue = _inSituService.TestAccessor().Dynamic._toolItemDesigner; | ||
toolItemDesignerValue.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void Dispose_UnsubscribesFromComponentChangeService() | ||
{ | ||
object componentChangeServiceValue = _inSituService.TestAccessor().Dynamic._componentChangeService; | ||
componentChangeServiceValue.Should().NotBeNull(); | ||
|
||
_inSituService.Dispose(); | ||
_isInSituServiceDisposed = true; | ||
|
||
componentChangeServiceValue = _inSituService.TestAccessor().Dynamic._componentChangeService; | ||
componentChangeServiceValue.Should().BeNull(); | ||
} | ||
|
||
[Fact] | ||
public void ToolStripKeyBoardService_ReturnsServiceInstance() | ||
{ | ||
object toolStripKeyBoardService = _inSituService.TestAccessor().Dynamic.ToolStripKeyBoardService; | ||
|
||
toolStripKeyBoardService.Should().BeAssignableTo<ToolStripKeyboardHandlingService>(); | ||
ricardobossan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
toolStripKeyBoardService.Should().Be(_mockToolStripKeyboardHandlingService.Object); | ||
} | ||
|
||
[Fact] | ||
public void GetEditWindow_ReturnsZero_WhenNoDesignerIsNotNull() | ||
{ | ||
IntPtr result = _inSituService.GetEditWindow(); | ||
result.Should().Be(IntPtr.Zero); | ||
} | ||
|
||
[Fact] | ||
public void OnComponentRemoved_RemovesService_WhenNoToolStripPresent() | ||
{ | ||
Mock<IContainer> mockContainer = new(); | ||
ComponentCollection componentCollection = new(Array.Empty<IComponent>()); | ||
|
||
mockContainer.Setup(c => c.Components).Returns(componentCollection); | ||
_mockDesignerHost.Setup(dh => dh.Container).Returns(mockContainer.Object); | ||
_mockServiceProvider.Setup(sp => sp.GetService(typeof(ISupportInSituService))).Returns(_inSituService); | ||
|
||
using Component component = new(); | ||
_inSituService.TestAccessor().Dynamic.OnComponentRemoved(null, new ComponentEventArgs(component)); | ||
|
||
_mockDesignerHost.Verify(dh => dh.RemoveService(typeof(ISupportInSituService)), Times.Once); | ||
} | ||
|
||
[Fact] | ||
public void OnComponentRemoved_DoesNotRemoveService_WhenToolStripPresent() | ||
{ | ||
Mock<ToolStrip> mockToolStrip = new(); | ||
Mock<IContainer> mockContainer = new(); | ||
ComponentCollection realComponentCollection = new([mockToolStrip.Object]); | ||
|
||
mockContainer.Setup(c => c.Components).Returns(realComponentCollection); | ||
_mockDesignerHost.Setup(dh => dh.Container).Returns(mockContainer.Object); | ||
_mockServiceProvider.Setup(sp => sp.GetService(typeof(ISupportInSituService))).Returns(_inSituService); | ||
|
||
using Component component = new(); | ||
_inSituService.TestAccessor().Dynamic.OnComponentRemoved(null, new ComponentEventArgs(component)); | ||
|
||
_mockDesignerHost.Verify(dh => dh.RemoveService(typeof(ISupportInSituService)), Times.Never); | ||
} | ||
|
||
[Fact] | ||
public void IgnoreMessages_ReturnsFalse_WhenSelectionServiceIsNull() | ||
{ | ||
_mockServiceProvider.Setup(sp => sp.GetService(typeof(ISelectionService))).Returns(null!); | ||
bool result = _inSituService.IgnoreMessages; | ||
result.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void IgnoreMessages_ReturnsFalse_WhenDesignerHostIsNull() | ||
{ | ||
_mockServiceProvider.Setup(sp => sp.GetService(typeof(IDesignerHost))).Returns(null!); | ||
bool result = _inSituService.IgnoreMessages; | ||
result.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void IgnoreMessages_WhenPrimarySelectionIsNotIComponentAndSelectedDesignerControlIsNull_ReturnsFalse() | ||
ricardobossan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
_inSituService.TestAccessor().Dynamic._toolDesigner = _mockToolStripDesigner.Object; | ||
bool result = _inSituService.IgnoreMessages; | ||
result.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void IgnoreMessages_WhenComponentIsMenuStrip_ReturnsTrue() | ||
{ | ||
Mock<MenuStrip> menuStripMock = new(); | ||
Mock<ToolStrip> toolStripMock = new(); | ||
_mockSelectionService.Setup(ss => ss.PrimarySelection).Returns(menuStripMock.Object); | ||
_mockDesignerHost.Setup(dh => dh.GetDesigner(menuStripMock.Object)).Returns(_mockToolStripDesigner.Object); | ||
bool result = _inSituService.IgnoreMessages; | ||
result.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void IgnoreMessages_WhenComponentIsToolStripMenuItem_ReturnsTrue() | ||
{ | ||
Mock<ToolStripMenuItem> toolStripMenuItemMock = new(); | ||
Mock<DesignerToolStripControlHost> designerToolStripControlHostMock = new(toolStripMenuItemMock.Object); | ||
_mockSelectionService.Setup(ss => ss.PrimarySelection).Returns(toolStripMenuItemMock.Object); | ||
_mockDesignerHost.Setup(dh => dh.GetDesigner(toolStripMenuItemMock.Object)).Returns(_mockToolStripItemDesigner.Object); | ||
bool result = _inSituService.IgnoreMessages; | ||
result.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void HandleKeyChar_WhenToolItemDesignerIsNotMenuDesigner_CallsShowEditNode() | ||
{ | ||
_inSituService.TestAccessor().Dynamic._toolDesigner = null; | ||
|
||
_inSituService.HandleKeyChar(); | ||
|
||
_mockToolStripItemDesigner.Verify(d => d.ShowEditNode(false), Times.Once); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.