Skip to content

Commit c200807

Browse files
committed
Add unit tests for ToolStripKeyboardHandlingService.cs file
1 parent f812cb6 commit c200807

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 Moq;
6+
7+
namespace System.Windows.Forms.Design.Tests;
8+
9+
public class ToolStripKeyboardHandlingServiceTests
10+
{
11+
private readonly Mock<ISelectionService> _selectionServiceMock;
12+
private readonly Mock<IDesignerHost> _designerHostMock;
13+
private readonly Mock<IComponentChangeService> _componentChangeServiceMock;
14+
private readonly DummyServiceProvider _provider;
15+
16+
public ToolStripKeyboardHandlingServiceTests()
17+
{
18+
_selectionServiceMock = new();
19+
_designerHostMock = new();
20+
_componentChangeServiceMock = new();
21+
22+
_provider = new(type =>
23+
type == typeof(ISelectionService) ? _selectionServiceMock.Object :
24+
type == typeof(IDesignerHost) ? _designerHostMock.Object :
25+
type == typeof(IComponentChangeService) ? _componentChangeServiceMock.Object :
26+
null);
27+
28+
_designerHostMock.Setup(h => h.GetService(typeof(IComponentChangeService))).Returns(_componentChangeServiceMock.Object);
29+
}
30+
31+
private class DummyServiceProvider : IServiceProvider
32+
{
33+
private readonly Func<Type, object?> _serviceResolver;
34+
35+
public DummyServiceProvider(Func<Type, object?> serviceResolver)
36+
{
37+
_serviceResolver = serviceResolver;
38+
}
39+
40+
public object? GetService(Type serviceType) => _serviceResolver(serviceType);
41+
}
42+
43+
[Fact]
44+
public void Ctor_InitializesAndSubscribesToEvents()
45+
{
46+
ToolStripKeyboardHandlingService service = new(_provider);
47+
48+
_selectionServiceMock.VerifyAdd(s => s.SelectionChanging += It.IsAny<EventHandler>(), Times.Once());
49+
_selectionServiceMock.VerifyAdd(s => s.SelectionChanged += It.IsAny<EventHandler>(), Times.Once());
50+
_componentChangeServiceMock.VerifyAdd(s => s.ComponentRemoved += It.IsAny<ComponentEventHandler>(), Times.Once());
51+
}
52+
53+
[Fact]
54+
public void AddCommands_DoesNotThrow_WhenNoMenuService()
55+
{
56+
ToolStripKeyboardHandlingService service = new(_provider);
57+
service.AddCommands();
58+
}
59+
60+
[Fact]
61+
public void RestoreCommands_DoesNotThrow_WhenNoMenuService()
62+
{
63+
ToolStripKeyboardHandlingService service = new(_provider);
64+
service.RestoreCommands();
65+
}
66+
67+
[Fact]
68+
public void RemoveCommands_DoesNotThrow_WhenNoMenuService()
69+
{
70+
ToolStripKeyboardHandlingService service = new(_provider);
71+
service.RemoveCommands();
72+
}
73+
74+
[Fact]
75+
public void OnContextMenu_ReturnsTrue_WhenTemplateNodeActive()
76+
{
77+
ToolStripKeyboardHandlingService service = new(_provider)
78+
{
79+
TemplateNodeActive = true
80+
};
81+
service.OnContextMenu(10, 10).Should().BeTrue();
82+
}
83+
84+
[Fact]
85+
public void OnContextMenu_ReturnsFalse_WhenNotTemplateNodeActive()
86+
{
87+
ToolStripKeyboardHandlingService service = new(_provider)
88+
{
89+
TemplateNodeActive = false
90+
};
91+
service.OnContextMenu(10, 10).Should().BeFalse();
92+
}
93+
94+
[Fact]
95+
public void ProcessKeySelect_DoesNotThrow_WhenNoSelectionService()
96+
{
97+
ToolStripKeyboardHandlingService service = new(_provider);
98+
service.ProcessKeySelect(false);
99+
}
100+
101+
[Fact]
102+
public void ProcessUpDown_DoesNotThrow_WhenNoSelectionService()
103+
{
104+
ToolStripKeyboardHandlingService service = new(_provider);
105+
service.ProcessUpDown(false);
106+
}
107+
108+
[Fact]
109+
public void RotateTab_DoesNotThrow_WhenNoSelectionService()
110+
{
111+
ToolStripKeyboardHandlingService service = new(_provider);
112+
service.RotateTab(false);
113+
}
114+
}

0 commit comments

Comments
 (0)