-
Notifications
You must be signed in to change notification settings - Fork 1k
Adds coverage for ComponentTray
#13396
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
Closed
ricardobossan
wants to merge
6
commits into
dotnet:main
from
ricardobossan:Issue_10773_Adds_Coverage_To_ComponentTray
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cc8f20b
- Adds coverage for `ComponentTray`
07bd572
Adds more tests for public methods and properties
e182fdd
Removes test that fails on the CI and add field to be disposed
6a24151
Removes test that doesn't thrown on the CI
ac2b08a
Adds some tests
2ad5df2
Removes empty line
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
238 changes: 238 additions & 0 deletions
238
...em.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/ComponentTrayTests.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,238 @@ | ||
// 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 System.Drawing; | ||
using System.Drawing.Design; | ||
using Moq; | ||
|
||
namespace System.Windows.Forms.Design.Tests; | ||
|
||
public class ComponentTrayTests : IDisposable | ||
{ | ||
private readonly ComponentTray _componentTray; | ||
private readonly Component _component = new Mock<ContextMenuStrip>().Object; | ||
|
||
public ComponentTrayTests() | ||
{ | ||
_componentTray = new(new Mock<IDesigner>().Object, MockServiceProvider().Object); | ||
_componentTray.AddComponent(_component); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_component.Dispose(); | ||
_componentTray.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void Constructor_Default() | ||
{ | ||
ComponentTray componentTray = new(new Mock<IDesigner>().Object, MockServiceProvider().Object); | ||
componentTray.Should().BeOfType<ComponentTray>(); | ||
componentTray.Visible.Should().BeTrue(); | ||
componentTray.AutoScroll.Should().BeTrue(); | ||
componentTray.AllowDrop.Should().BeTrue(); | ||
componentTray.Text.Should().Be("ComponentTray"); | ||
componentTray.Controls.Count.Should().Be(0); | ||
componentTray.ComponentCount.Should().Be(0); | ||
componentTray.AutoArrange.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void AutoArrange_SetsPropertiesProperly() | ||
{ | ||
_componentTray.AutoArrange = true; | ||
_componentTray.AutoArrange.Should().BeTrue(); | ||
_componentTray.ShowLargeIcons = true; | ||
_componentTray.ShowLargeIcons.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public void AddComponent_AddsComponent_DoesNotThrow() | ||
{ | ||
Action action = () => _componentTray.AddComponent(new Mock<Component>().Object); | ||
|
||
action.Should().NotThrow<Exception>(); | ||
} | ||
|
||
[Fact] | ||
public void CreateComponentFromTool_DoesNotThrow() | ||
{ | ||
Action action = () => _componentTray.CreateComponentFromTool(new Mock<ToolboxItem>().Object); | ||
|
||
action.Should().NotThrow(); | ||
} | ||
|
||
[Fact] | ||
public void GetNextComponent_WithOneComponent_ReturnsNull() => | ||
_componentTray.GetNextComponent(_component, true).Should().BeNull(); | ||
|
||
[Fact] | ||
public void GetNextComponent_WithOneAddedComponent_ReturnsIt() => | ||
_componentTray.GetNextComponent(new Mock<Component>().Object, true).Should().Be(_component); | ||
|
||
[Fact] | ||
public void GetNextComponent_WithSameComponentAdded_ReturnsNull() => | ||
_componentTray.GetNextComponent(_component, true).Should().BeNull(); | ||
|
||
[Fact] | ||
public void GetNextComponent_WhenAddedDifferentThenSameThenNew_ReturnsTheFirstAddedOne() | ||
{ | ||
Mock<Component> sameComponentMock = new(); | ||
_componentTray.AddComponent(sameComponentMock.Object); | ||
Mock<Component> lastComponentMock = new(); | ||
_componentTray.AddComponent(lastComponentMock.Object); | ||
_componentTray.GetNextComponent(sameComponentMock.Object, true).Should().Be(_component); | ||
} | ||
|
||
[Fact] | ||
public void GetLocation_ReturnsLocation() => _componentTray.GetLocation(new Mock<ContextMenuStrip>().Object).Should().Be(new Point() { X = 0, Y = 0 }); | ||
|
||
[Fact] | ||
public void IsTrayComponent_WithoutAComponentView_ReturnsFalse() => | ||
_componentTray.IsTrayComponent(_component).Should().BeFalse(); | ||
|
||
[Fact] | ||
public void RemoveComponent_DoesNotThrow() | ||
{ | ||
Action action = () => | ||
{ | ||
_componentTray.RemoveComponent(_component); | ||
}; | ||
|
||
action.Should().NotThrow<Exception>(); | ||
} | ||
|
||
[Fact] | ||
public void SetLocation_DoesNotThrow() | ||
{ | ||
Action action = () => _componentTray.SetLocation(_component, new(0, 0)); | ||
action.Should().NotThrow<Exception>(); | ||
} | ||
|
||
private Mock<IServiceProvider> MockServiceProvider() | ||
{ | ||
var designerHostMock = new Mock<IDesignerHost>(); | ||
Dictionary<Type, object> serviceMap = new() | ||
{ | ||
{ typeof(IExtenderProviderService), new Mock<IExtenderProviderService>().Object}, | ||
{ typeof(IDesignerHost), designerHostMock.Object}, | ||
{ typeof(IEventHandlerService), new Mock<IEventHandlerService>().Object }, | ||
{ typeof(IMenuCommandService), new Mock<IMenuCommandService>().Object}, | ||
{ typeof(IComponentChangeService), new Mock<IComponentChangeService>().Object}, | ||
{ typeof(ISelectionService), new Mock<ISelectionService>().Object}, | ||
}; | ||
|
||
Mock<IServiceProvider> serviceProviderMock = new(); | ||
|
||
foreach (KeyValuePair<Type, object> service in serviceMap) | ||
{ | ||
serviceProviderMock | ||
.Setup(s => s.GetService(service.Key)) | ||
.Returns(service.Value); | ||
} | ||
|
||
return serviceProviderMock; | ||
} | ||
|
||
[Fact] | ||
public void AddComponent_WithValidComponent_AddsToTray() | ||
{ | ||
using Component component = new(); | ||
int initialCount = _componentTray.ComponentCount; | ||
|
||
_componentTray.AddComponent(component); | ||
|
||
_componentTray.ComponentCount.Should().Be(initialCount + 1); | ||
} | ||
|
||
[Fact] | ||
public void AddComponent_WithNonDisplayableComponent_DoesNotAddToTray() | ||
{ | ||
using Component component = new MyCustomComponent(); | ||
Mock<ISelectionUIService> selectionUISvcMock = new(); | ||
Mock<IServiceProvider> serviceProviderMock = MockServiceProvider(); | ||
serviceProviderMock | ||
.Setup(s => s.GetService(typeof(ISelectionUIService))) | ||
.Returns(selectionUISvcMock.Object); | ||
|
||
ComponentTray componentTray = new(new Mock<IDesigner>().Object, serviceProviderMock.Object); | ||
componentTray.AddComponent(component); | ||
|
||
componentTray.ComponentCount.Should().Be(0); | ||
} | ||
|
||
[DesignTimeVisible(false)] | ||
public class MyCustomComponent : Component | ||
{ | ||
} | ||
|
||
[Fact] | ||
public void AddComponent_WhenHostIsLoading_DoesNotPositionControl() | ||
{ | ||
Mock<IDesignerHost> hostMock = new(); | ||
hostMock.Setup(h => h.Loading).Returns(true); | ||
|
||
Mock<IServiceProvider> serviceProviderMock = MockServiceProvider(); | ||
serviceProviderMock | ||
.Setup(s => s.GetService(typeof(IDesignerHost))) | ||
.Returns(hostMock.Object); | ||
|
||
ComponentTray componentTray = new(new Mock<IDesigner>().Object, serviceProviderMock.Object); | ||
Mock<Component> mockComponent = new(); | ||
int initialCount = componentTray.ComponentCount; | ||
|
||
componentTray.AddComponent(mockComponent.Object); | ||
|
||
componentTray.ComponentCount.Should().Be(initialCount + 1); | ||
} | ||
|
||
[Fact] | ||
public void AutoArrange_TogglesCorrectly() | ||
{ | ||
_componentTray.AutoArrange = true; | ||
_componentTray.AutoArrange.Should().BeTrue(); | ||
|
||
_componentTray.AutoArrange = false; | ||
_componentTray.AutoArrange.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ShowLargeIcons_TogglesCorrectly() | ||
{ | ||
_componentTray.ShowLargeIcons = true; | ||
_componentTray.ShowLargeIcons.Should().BeTrue(); | ||
|
||
_componentTray.ShowLargeIcons = false; | ||
_componentTray.ShowLargeIcons.Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void ComponentCount_ReturnsCorrectCount() | ||
{ | ||
int initialCount = _componentTray.ComponentCount; | ||
|
||
using Component newComponent = new(); | ||
_componentTray.AddComponent(newComponent); | ||
|
||
_componentTray.ComponentCount.Should().Be(initialCount + 1); | ||
} | ||
|
||
[Fact] | ||
public void IsTrayComponent_WithComponentNotInTray_ReturnsFalse() | ||
{ | ||
Mock<IComponent> mockComponent = new(); | ||
_componentTray.IsTrayComponent(mockComponent.Object).Should().BeFalse(); | ||
} | ||
|
||
[Fact] | ||
public void CreateComponentFromTool_WithValidTool_DoesNotThrow() | ||
{ | ||
Mock<ToolboxItem> mockToolboxItem = new(); | ||
Action action = () => _componentTray.CreateComponentFromTool(mockToolboxItem.Object); | ||
|
||
action.Should().NotThrow(); | ||
} | ||
} |
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.