Skip to content

Commit d4f85a9

Browse files
Refactored unit tests. Moved common logic of keyboard's simulation into the helper class.
1 parent 30b3dda commit d4f85a9

File tree

4 files changed

+47
-85
lines changed

4 files changed

+47
-85
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using static Interop;
2+
using System.Windows.Forms;
3+
4+
namespace System
5+
{
6+
public static class KeyboardHelper
7+
{
8+
public static void SimulateKeyDown(Control control, Keys key)
9+
{
10+
(nint keyCode, nint lParam) = GetKeyParameters(key);
11+
User32.SendMessageW(control, User32.WM.KEYDOWN, keyCode, lParam);
12+
}
13+
14+
public static void SimulateKeyPress(Control control, Keys key)
15+
{
16+
(nint keyCode, nint lParam) = GetKeyParameters(key);
17+
User32.SendMessageW(control, User32.WM.KEYDOWN, keyCode, lParam);
18+
User32.SendMessageW(control, User32.WM.KEYUP, keyCode, lParam);
19+
}
20+
21+
private static (nint keyCode, nint lParam) GetKeyParameters(Keys key)
22+
{
23+
// https://docs.microsoft.com/windows/win32/inputdev/wm-keyup
24+
// The MSDN page tells us what bits of lParam to use for each of the parameters.
25+
// All we need to do is some bit shifting to assemble lParam
26+
// lParam = repeatCount | (scanCode << 16)
27+
nint keyCode = (nint)key;
28+
nint lParam = 0x00000001 | keyCode << 16;
29+
return (keyCode, lParam);
30+
}
31+
}
32+
}

src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2282,15 +2282,9 @@ public void ComboBox_Select_Item_By_Key(Keys key, int expectedKeyPressesCount, i
22822282
comboBox.SelectedIndex = selectedIndex;
22832283
comboBox.ResetEventsCount();
22842284

2285-
// https://docs.microsoft.com/windows/win32/inputdev/wm-keyup
2286-
// The MSDN page tells us what bits of lParam to use for each of the parameters.
2287-
// All we need to do is some bit shifting to assemble lParam.
2288-
nint keyCode = (nint)key;
2289-
nint lParam = 0x00000001 | keyCode << 16;
22902285
for (int i = 0; i < expectedKeyPressesCount; i++)
22912286
{
2292-
User32.SendMessageW(comboBox, User32.WM.KEYDOWN, keyCode, lParam);
2293-
User32.SendMessageW(comboBox, User32.WM.KEYUP, keyCode, lParam);
2287+
KeyboardHelper.SimulateKeyPress(comboBox, key);
22942288
}
22952289

22962290
Assert.Equal(expectedKeyPressesCount, comboBox.EventsCount);

src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ListViewGroup.ListViewGroupAccessibleObjectTests.cs

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -759,44 +759,24 @@ public void ListViewGroupAccessibleObject_ExpandCollapseStateFromKeyboard_Return
759759
Assert.True(listView.IsHandleCreated);
760760
Assert.NotEqual(IntPtr.Zero, listView.Handle);
761761

762-
// https://docs.microsoft.com/windows/win32/inputdev/wm-keyup
763-
// The MSDN page tells us what bits of lParam to use for each of the parameters.
764-
// All we need to do is some bit shifting to assemble lParam
765-
// lParam = repeatCount | (scanCode << 16)
766-
nint keyCode = (nint)Keys.Up;
767-
nint lParam = 0x00000001 | keyCode << 16;
768-
User32.SendMessageW(listView, User32.WM.KEYDOWN, keyCode, lParam);
769-
User32.SendMessageW(listView, User32.WM.KEYUP, keyCode, lParam);
770-
771-
keyCode = (nint)Keys.Left;
772-
lParam = 0x00000001 | keyCode << 16;
773-
User32.SendMessageW(listView, User32.WM.KEYDOWN, keyCode, lParam);
774-
User32.SendMessageW(listView, User32.WM.KEYUP, keyCode, lParam);
762+
KeyboardHelper.SimulateKeyPress(listView, Keys.Up);
763+
KeyboardHelper.SimulateKeyPress(listView, Keys.Left);
775764

776765
Assert.Equal(ListViewGroupCollapsedState.Collapsed, listViewGroup.GetNativeCollapsedState());
777766
Assert.Equal(ExpandCollapseState.Collapsed, listViewGroup.AccessibilityObject.ExpandCollapseState);
778767

779-
keyCode = (nint)Keys.Left;
780-
lParam = 0x00000001 | keyCode << 16;
781-
User32.SendMessageW(listView, User32.WM.KEYDOWN, keyCode, lParam);
782-
User32.SendMessageW(listView, User32.WM.KEYUP, keyCode, lParam);
768+
KeyboardHelper.SimulateKeyPress(listView, Keys.Left);
783769

784770
// The second left key pressing should not change Collapsed state
785771
Assert.Equal(ListViewGroupCollapsedState.Collapsed, listViewGroup.GetNativeCollapsedState());
786772
Assert.Equal(ExpandCollapseState.Collapsed, listViewGroup.AccessibilityObject.ExpandCollapseState);
787773

788-
keyCode = (nint)Keys.Right;
789-
lParam = 0x00000001 | keyCode << 16;
790-
User32.SendMessageW(listView, User32.WM.KEYDOWN, keyCode, lParam);
791-
User32.SendMessageW(listView, User32.WM.KEYUP, keyCode, lParam);
774+
KeyboardHelper.SimulateKeyPress(listView, Keys.Right);
792775

793776
Assert.Equal(ListViewGroupCollapsedState.Expanded, listViewGroup.GetNativeCollapsedState());
794777
Assert.Equal(ExpandCollapseState.Expanded, listViewGroup.AccessibilityObject.ExpandCollapseState);
795778

796-
keyCode = (nint)Keys.Right;
797-
lParam = 0x00000001 | keyCode << 16;
798-
User32.SendMessageW(listView, User32.WM.KEYDOWN, keyCode, lParam);
799-
User32.SendMessageW(listView, User32.WM.KEYUP, keyCode, lParam);
779+
KeyboardHelper.SimulateKeyPress(listView, Keys.Right);
800780

801781
// The second right key pressing should not change Expanded state
802782
Assert.Equal(ListViewGroupCollapsedState.Expanded, listViewGroup.GetNativeCollapsedState());
@@ -829,12 +809,12 @@ public void ListViewGroupAccessibleObject_GroupCollapsedStateChanged_IsExpected_
829809
listView.GroupCollapsedStateChanged += (_, e) => eventGroupIndices.Add(e.GroupIndex);
830810

831811
// Navigate to the second group
832-
SimulateKeyPress(Keys.Down);
812+
KeyboardHelper.SimulateKeyPress(listView, Keys.Down);
833813

834814
if (firstGroupSate == ListViewGroupCollapsedState.Collapsed)
835815
{
836816
// This action is necessary to navigate to the second group correctly in this specific case.
837-
SimulateKeyPress(Keys.Up);
817+
KeyboardHelper.SimulateKeyPress(listView, Keys.Up);
838818
}
839819

840820
// Simulate multiple selection of several groups to test a specific case,
@@ -844,7 +824,7 @@ public void ListViewGroupAccessibleObject_GroupCollapsedStateChanged_IsExpected_
844824
item3.Selected = true;
845825

846826
// Simulate the second group collapse action via keyboard.
847-
SimulateKeyPress(Keys.Left);
827+
KeyboardHelper.SimulateKeyPress(listView, Keys.Left);
848828

849829
Assert.Equal(firstGroupSate, group1.GetNativeCollapsedState());
850830
Assert.Equal(firstGroupSate, group1.CollapsedState);
@@ -862,18 +842,6 @@ public void ListViewGroupAccessibleObject_GroupCollapsedStateChanged_IsExpected_
862842
// are still selected after keyboard navigation simulations.
863843
Assert.Equal(3, listView.SelectedItems.Count);
864844
Assert.True(listView.IsHandleCreated);
865-
866-
void SimulateKeyPress(Keys key)
867-
{
868-
// https://docs.microsoft.com/windows/win32/inputdev/wm-keyup
869-
// The MSDN page tells us what bits of lParam to use for each of the parameters.
870-
// All we need to do is some bit shifting to assemble lParam
871-
// lParam = repeatCount | (scanCode << 16)
872-
nint keyCode = (nint)key;
873-
nint lParam = 0x00000001 | keyCode << 16;
874-
User32.SendMessageW(listView, User32.WM.KEYDOWN, keyCode, lParam);
875-
User32.SendMessageW(listView, User32.WM.KEYUP, keyCode, lParam);
876-
}
877845
}
878846

879847
[WinFormsTheory]

src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ListViewTests.cs

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4486,14 +4486,8 @@ public unsafe void ListView_WmReflectNotify_LVN_KEYDOWN_SpaceKey_HasCheckBoxes_W
44864486
item1.Selected = selectItems;
44874487
item2.Selected = selectItems;
44884488

4489-
// https://docs.microsoft.com/windows/win32/inputdev/wm-keydown
4490-
// The MSDN page tells us what bits of lParam to use for each of the parameters.
4491-
// All we need to do is some bit shifting to assemble lParam
4492-
// lParam = repeatCount | (scanCode << 16)
4493-
uint keyCode = (uint)Keys.Space;
4494-
uint lParam = (0x00000001 | keyCode << 16);
4489+
KeyboardHelper.SimulateKeyDown(control, Keys.Space);
44954490

4496-
User32.SendMessageW(control, User32.WM.KEYDOWN, (nint)keyCode, (nint)lParam);
44974491
Assert.Equal(selectItems ? 2 : 0, control.SelectedItems.Count);
44984492
Assert.Equal(!checkItem && selectItems && focusItem, item2.Checked);
44994493
}
@@ -4517,15 +4511,8 @@ public unsafe void ListView_WmReflectNotify_LVN_KEYDOWN_WithGroups_WithoutSelect
45174511
control.Groups.Add(group);
45184512
control.CreateControl();
45194513

4520-
// https://docs.microsoft.com/windows/win32/inputdev/wm-keydown
4521-
// The MSDN page tells us what bits of lParam to use for each of the parameters.
4522-
// All we need to do is some bit shifting to assemble lParam
4523-
// lParam = repeatCount | (scanCode << 16)
4524-
uint keyCode = (uint)key;
4525-
uint lParam = (0x00000001 | keyCode << 16);
4514+
KeyboardHelper.SimulateKeyDown(control, key);
45264515

4527-
// If control doesn't have selected items none will be focused.
4528-
User32.SendMessageW(control, User32.WM.KEYDOWN, (nint)keyCode, (nint)lParam);
45294516
Assert.Empty(control.SelectedIndices);
45304517
Assert.Null(control.FocusedItem);
45314518
Assert.Null(control.FocusedGroup);
@@ -4562,14 +4549,9 @@ public unsafe void ListView_WmReflectNotify_LVN_KEYDOWN_WithGroups_and_SelectedI
45624549

45634550
item2.Selected = true;
45644551

4565-
// https://docs.microsoft.com/windows/win32/inputdev/wm-keydown
4566-
// The MSDN page tells us what bits of lParam to use for each of the parameters.
4567-
// All we need to do is some bit shifting to assemble lParam
4568-
// lParam = repeatCount | (scanCode << 16)
4569-
uint keyCode = (uint)(key_s == "Keys.Down" ? Keys.Down : Keys.Up);
4570-
uint lParam = (0x00000001 | keyCode << 16);
4552+
var key = key_s == "Keys.Down" ? Keys.Down : Keys.Up;
4553+
KeyboardHelper.SimulateKeyDown(control, key);
45714554

4572-
User32.SendMessageW(control, User32.WM.KEYDOWN, (nint)keyCode, (nint)lParam);
45734555
Assert.False(control.GroupsEnabled);
45744556
Assert.True(control.Items.Count > 0);
45754557
int expectedGroupIndex = int.Parse(expectedGroupIndex_s);
@@ -4606,15 +4588,8 @@ public unsafe void ListView_VirtualMode_WmReflectNotify_LVN_KEYDOWN_WithGroups_D
46064588

46074589
control.CreateControl();
46084590

4609-
// https://docs.microsoft.com/windows/win32/inputdev/wm-keydown
4610-
// The MSDN page tells us what bits of lParam to use for each of the parameters.
4611-
// All we need to do is some bit shifting to assemble lParam
4612-
// lParam = repeatCount | (scanCode << 16)
4613-
uint keyCode = (uint)key;
4614-
uint lParam = (0x00000001 | keyCode << 16);
4591+
KeyboardHelper.SimulateKeyDown(control, key);
46154592

4616-
// Actually ListView in VirtualMode can't have Groups
4617-
User32.SendMessageW(control, User32.WM.KEYDOWN, (nint)keyCode, (nint)lParam);
46184593
Assert.Null(control.FocusedGroup);
46194594
}
46204595

@@ -4648,15 +4623,8 @@ public unsafe void ListView_VirtualMode_WmReflectNotify_LVN_KEYDOWN_EnabledCheck
46484623
item2.Checked = false;
46494624
control.FocusedItem = item1;
46504625

4651-
// https://docs.microsoft.com/windows/win32/inputdev/wm-keydown
4652-
// The MSDN page tells us what bits of lParam to use for each of the parameters.
4653-
// All we need to do is some bit shifting to assemble lParam
4654-
// lParam = repeatCount | (scanCode << 16)
4655-
uint keyCode = (uint)Keys.Space;
4656-
uint lParam = (0x00000001 | keyCode << 16);
4626+
KeyboardHelper.SimulateKeyDown(control, Keys.Space);
46574627

4658-
// Actually ListView in VirtualMode doesn't check items here
4659-
User32.SendMessageW(control, User32.WM.KEYDOWN, (nint)keyCode, (nint)lParam);
46604628
Assert.False(item2.Checked);
46614629
}
46624630

0 commit comments

Comments
 (0)