Skip to content

Commit 6cb7a47

Browse files
authored
Add code coverage for TabOrder (#13181)
* Add code coverage for TabOrder Related #10773
1 parent 69e044f commit 6cb7a47

File tree

1 file changed

+239
-0
lines changed
  • src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design

1 file changed

+239
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
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 System.Globalization;
8+
using Moq;
9+
10+
namespace System.Windows.Forms.Design.Tests;
11+
12+
public class TabOrderTests : IDisposable
13+
{
14+
private readonly Mock<IDesignerHost> _mockHost;
15+
private readonly Mock<IUIService> _mockUIService;
16+
private readonly Mock<IOverlayService> _mockOverlayService;
17+
private readonly Mock<IHelpService> _mockHelpService;
18+
private readonly Mock<IMenuCommandService> _mockMenuCommandService;
19+
private readonly Mock<IEventHandlerService> _mockEventHandlerService;
20+
private readonly Mock<IComponentChangeService> _mockComponentChangeService;
21+
private readonly Font _dialogFont;
22+
private readonly TabOrder _tabOrder;
23+
24+
public Mock<IComponent> ComponentMock { get; }
25+
public Mock<Control> ControlMock { get; }
26+
27+
public TabOrderTests()
28+
{
29+
_mockHost = new();
30+
_mockUIService = new();
31+
_mockOverlayService = new();
32+
_mockHelpService = new();
33+
_mockMenuCommandService = new();
34+
_mockEventHandlerService = new();
35+
_mockComponentChangeService = new();
36+
37+
_mockHost.Setup(h => h.GetService(typeof(IUIService))).Returns(_mockUIService.Object);
38+
_mockHost.Setup(h => h.GetService(typeof(IOverlayService))).Returns(_mockOverlayService.Object);
39+
_mockHost.Setup(h => h.GetService(typeof(IHelpService))).Returns(_mockHelpService.Object);
40+
_mockHost.Setup(h => h.GetService(typeof(IMenuCommandService))).Returns(_mockMenuCommandService.Object);
41+
_mockHost.Setup(h => h.GetService(typeof(IEventHandlerService))).Returns(_mockEventHandlerService.Object);
42+
_mockHost.Setup(h => h.GetService(typeof(IComponentChangeService))).Returns(_mockComponentChangeService.Object);
43+
44+
_dialogFont = new("Arial", 8);
45+
_mockUIService.Setup(u => u.Styles["DialogFont"]).Returns(_dialogFont);
46+
47+
_tabOrder = new(_mockHost.Object);
48+
49+
ComponentMock = new();
50+
ControlMock = new();
51+
}
52+
53+
public void Dispose()
54+
{
55+
_dialogFont.Dispose();
56+
_tabOrder.Dispose();
57+
}
58+
59+
[WinFormsFact]
60+
public void TabOrder_Constructor_InitializesFieldsCorrectly()
61+
{
62+
dynamic accessor = _tabOrder.TestAccessor().Dynamic;
63+
64+
_tabOrder.Should().NotBeNull();
65+
((Font)accessor._tabFont).Should().Be(new Font(_dialogFont, FontStyle.Bold));
66+
((int)accessor._selSize).Should().Be(DesignerUtils.GetAdornmentDimensions(AdornmentType.GrabHandle).Width);
67+
((SolidBrush)accessor._highlightTextBrush).Color.Should().Be(SystemColors.HighlightText);
68+
((Pen)accessor._highlightPen).Color.Should().Be(SystemColors.Highlight);
69+
((string)accessor._decimalSep).Should().Be(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator);
70+
((object)accessor._commands).Should().NotBeNull();
71+
((object)accessor._newCommands).Should().NotBeNull();
72+
73+
_mockOverlayService.Verify(os => os.PushOverlay(_tabOrder), Times.Once);
74+
_mockHelpService.Verify(hs => hs.AddContextAttribute("Keyword", "TabOrderView", HelpKeywordType.FilterKeyword), Times.Once);
75+
_mockMenuCommandService.Verify(mcs => mcs.AddCommand(It.IsAny<MenuCommand>()), Times.Exactly(((MenuCommand[])accessor._newCommands).Length));
76+
_mockEventHandlerService.Verify(ehs => ehs.PushHandler(_tabOrder), Times.Once);
77+
_mockComponentChangeService.VerifyAdd(cs => cs.ComponentAdded += It.IsAny<ComponentEventHandler>(), Times.Once);
78+
_mockComponentChangeService.VerifyAdd(cs => cs.ComponentRemoved += It.IsAny<ComponentEventHandler>(), Times.Once);
79+
_mockComponentChangeService.VerifyAdd(cs => cs.ComponentChanged += It.IsAny<ComponentChangedEventHandler>(), Times.Once);
80+
81+
_tabOrder.CreateControl();
82+
_tabOrder.IsHandleCreated.Should().BeTrue();
83+
}
84+
85+
[WinFormsFact]
86+
public void OnMouseDoubleClick_DoesNotThrowException()
87+
{
88+
_tabOrder.CreateControl();
89+
_tabOrder.IsHandleCreated.Should().BeTrue();
90+
91+
Record.Exception(() => _tabOrder.OnMouseDoubleClick(ComponentMock.Object)).Should().BeNull();
92+
}
93+
94+
[WinFormsFact]
95+
public void OnMouseDown_SetsNextTabIndex_WhenCtlHoverIsNotNull()
96+
{
97+
_tabOrder.CreateControl();
98+
_tabOrder.IsHandleCreated.Should().BeTrue();
99+
100+
dynamic accessor = _tabOrder.TestAccessor().Dynamic;
101+
accessor._ctlHover = ControlMock.Object;
102+
103+
_tabOrder.OnMouseDown(ComponentMock.Object, MouseButtons.Left, 0, 0);
104+
105+
((object)accessor._ctlHover).Should().NotBeNull();
106+
accessor.SetNextTabIndex(ControlMock.Object);
107+
}
108+
109+
[WinFormsFact]
110+
public void OnMouseHover_DoesNotThrowException()
111+
{
112+
_tabOrder.CreateControl();
113+
_tabOrder.IsHandleCreated.Should().BeTrue();
114+
115+
Record.Exception(() => _tabOrder.OnMouseHover(ComponentMock.Object)).Should().BeNull();
116+
}
117+
118+
[WinFormsFact]
119+
public void OnMouseMove_SetsNewHoverControl()
120+
{
121+
_tabOrder.CreateControl();
122+
_tabOrder.IsHandleCreated.Should().BeTrue();
123+
124+
List<Control> tabControls = new() { ControlMock.Object };
125+
dynamic accessor = _tabOrder.TestAccessor().Dynamic;
126+
accessor._tabControls = tabControls;
127+
128+
_tabOrder.OnMouseMove(ComponentMock.Object, 10, 10);
129+
130+
((object)accessor._ctlHover).Should().Be(ControlMock.Object.ToString());
131+
}
132+
133+
[WinFormsFact]
134+
public void OnMouseUp_DoesNotThrowException()
135+
{
136+
_tabOrder.CreateControl();
137+
_tabOrder.IsHandleCreated.Should().BeTrue();
138+
139+
Record.Exception(() => _tabOrder.OnMouseUp(ComponentMock.Object, MouseButtons.Left)).Should().BeNull();
140+
}
141+
142+
[WinFormsFact]
143+
public void OnSetCursor_DoesNotThrowException()
144+
{
145+
_tabOrder.CreateControl();
146+
_tabOrder.IsHandleCreated.Should().BeTrue();
147+
148+
Record.Exception(() => _tabOrder.OnSetCursor(ComponentMock.Object)).Should().BeNull();
149+
}
150+
151+
[WinFormsFact]
152+
public void OnSetCursor_SetsAppropriateCursor()
153+
{
154+
_tabOrder.CreateControl();
155+
_tabOrder.IsHandleCreated.Should().BeTrue();
156+
157+
dynamic accessor = _tabOrder.TestAccessor().Dynamic;
158+
159+
_tabOrder.OnSetCursor(ComponentMock.Object);
160+
161+
Cursor.Current.Should().Be(Cursors.Default);
162+
163+
accessor._ctlHover = new Control();
164+
_tabOrder.OnSetCursor(ComponentMock.Object);
165+
Cursor.Current.Should().Be(Cursors.Cross);
166+
}
167+
168+
[WinFormsFact]
169+
public void OverrideInvoke_CommandExists_InvokesCommandAndReturnsTrue()
170+
{
171+
_tabOrder.CreateControl();
172+
_tabOrder.IsHandleCreated.Should().BeTrue();
173+
174+
CommandID commandID = new(Guid.NewGuid(), 1);
175+
MenuCommand menuCommand = new((sender, e) => { }, commandID);
176+
dynamic accessor = _tabOrder.TestAccessor().Dynamic;
177+
accessor._commands = new MenuCommand[]
178+
{
179+
menuCommand
180+
};
181+
182+
_tabOrder.OverrideInvoke(menuCommand).Should().BeTrue();
183+
menuCommand.Invoke();
184+
}
185+
186+
[WinFormsFact]
187+
public void OverrideInvoke_CommandDoesNotExist_ReturnsFalse()
188+
{
189+
_tabOrder.CreateControl();
190+
_tabOrder.IsHandleCreated.Should().BeTrue();
191+
192+
CommandID commandID = new(Guid.NewGuid(), 1);
193+
Mock<MenuCommand> mockCommand = new(null!, commandID);
194+
dynamic accessor = _tabOrder.TestAccessor().Dynamic;
195+
accessor._commands = new MenuCommand[]
196+
{
197+
new MenuCommand((sender, e) => { }, new CommandID(Guid.NewGuid(), 2))
198+
};
199+
200+
_tabOrder.OverrideInvoke(mockCommand.Object).Should().BeFalse();
201+
mockCommand.Verify(c => c.Invoke(), Times.Never);
202+
}
203+
204+
[WinFormsFact]
205+
public void OverrideStatus_CommandDoesNotExist_DisablesCommandAndReturnsTrue()
206+
{
207+
_tabOrder.CreateControl();
208+
_tabOrder.IsHandleCreated.Should().BeTrue();
209+
210+
CommandID commandID = new(Guid.NewGuid(), 1);
211+
Mock<MenuCommand> mockCommand = new(null!, commandID);
212+
dynamic accessor = _tabOrder.TestAccessor().Dynamic;
213+
accessor._commands = new MenuCommand[]
214+
{
215+
new MenuCommand((sender, e) => { }, new CommandID(Guid.NewGuid(), 2)) { Enabled = true }
216+
};
217+
218+
_tabOrder.OverrideStatus(mockCommand.Object).Should().BeTrue();
219+
mockCommand.Object.Enabled.Should().BeFalse();
220+
}
221+
222+
[WinFormsFact]
223+
public void OverrideStatus_TabOrderCommand_DisablesCommandAndReturnsTrue()
224+
{
225+
_tabOrder.CreateControl();
226+
_tabOrder.IsHandleCreated.Should().BeTrue();
227+
228+
CommandID commandID = StandardCommands.TabOrder;
229+
Mock<MenuCommand> mockCommand = new(null!, commandID);
230+
dynamic accessor = _tabOrder.TestAccessor().Dynamic;
231+
accessor._commands = new MenuCommand[]
232+
{
233+
new MenuCommand((sender, e) => { }, new CommandID(Guid.NewGuid(), 2)) { Enabled = true }
234+
};
235+
236+
_tabOrder.OverrideStatus(mockCommand.Object).Should().BeTrue();
237+
mockCommand.Object.Enabled.Should().BeFalse();
238+
}
239+
}

0 commit comments

Comments
 (0)