diff --git a/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/ComboBoxTests.cs b/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/ComboBoxTests.cs index f5b341abc6b..f19a5bbd2af 100644 --- a/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/ComboBoxTests.cs +++ b/src/System.Windows.Forms/tests/IntegrationTests/UIIntegrationTests/ComboBoxTests.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System.Text; using Xunit; using Xunit.Abstractions; @@ -36,102 +35,5 @@ await RunSingleControlTestAsync((form, comboBox) => return Task.CompletedTask; }); } - - [WinFormsFact] - public async Task ComboBox_Select_Item_By_UpArrowKeyAsync() - { - int expectedKeyPressesCount = 9; - await RunComboBoxTestAsync(async (form, comboBox) => - { - StringBuilder stringBuilder = new(); - - // Reset EventsCount because the control gets selected right after the creation. - comboBox.ResetEventsCount(); - for (int i = 0; i < expectedKeyPressesCount; i++) - { - await Task.Delay(100); - await InputSimulator.SendAsync( - form, - inputSimulator => inputSimulator.Keyboard - .Sleep(100) - .KeyPress(WindowsInput.Native.VirtualKeyCode.UP)); - - await Task.Delay(100); - stringBuilder.AppendLine($"i:{i + 1}, eventsCount:{comboBox.EventsCount}"); - } - - TestOutputHelper.WriteLine($"{stringBuilder}"); - Assert.Equal(expectedKeyPressesCount, comboBox.EventsCount); - }, - expectedKeyPressesCount, - selectedIndex: 9); - } - - [WinFormsFact] - public async Task ComboBox_Select_Item_By_DownArrowKeyAsync() - { - int expectedKeyPressesCount = 9; - await RunComboBoxTestAsync(async (form, comboBox) => - { - // Reset EventsCount because the control gets selected right after the creation. - comboBox.ResetEventsCount(); - for (int i = 0; i < expectedKeyPressesCount; i++) - { - await InputSimulator.SendAsync( - form, - inputSimulator => inputSimulator.Keyboard.KeyPress(WindowsInput.Native.VirtualKeyCode.DOWN)); - - Assert.Equal(i + 1, comboBox.EventsCount); - } - - Assert.Equal(expectedKeyPressesCount, comboBox.EventsCount); - }, - expectedKeyPressesCount, - selectedIndex: 0); - } - - private async Task RunComboBoxTestAsync(Func runTest, int numberOfItemsToAdd, int selectedIndex) - { - await RunSingleControlTestAsync( - testDriverAsync: runTest, - createControl: () => - { - ComboBoxWithSelectCounter control = new(); - control.AddItems(numberOfItemsToAdd); - control.SelectedIndex = selectedIndex; - return control; - }, - createForm: () => - { - return new() - { - Size = new(500, 300), - }; - }); - } - - private class ComboBoxWithSelectCounter : ComboBox - { - public int EventsCount { get; private set; } - - public void AddItems(int numToAdd) - { - for (int i = 0; i <= numToAdd; i++) - { - Items.Add($"Item {i}"); - } - } - - protected override void OnSelectedIndexChanged(EventArgs e) - { - base.OnSelectedIndexChanged(e); - EventsCount++; - } - - public void ResetEventsCount() - { - EventsCount = 0; - } - } } } diff --git a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs index 1529ef4c9dc..d23c8bb5e56 100644 --- a/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs +++ b/src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/ComboBoxTests.cs @@ -2271,6 +2271,31 @@ public void OwnerDrawComboBox_Verify_OnMeasureItem_Receives_Correct_Arguments() } } + [WinFormsTheory] + [InlineData(Keys.Up, 9, 9)] + [InlineData(Keys.Down, 9, 0)] + public void ComboBox_Select_Item_By_Key(Keys key, int expectedKeyPressesCount, int selectedIndex) + { + using ComboBoxWithSelectCounter comboBox = new(); + comboBox.AddItems(expectedKeyPressesCount+1); + comboBox.CreateControl(); + comboBox.SelectedIndex = selectedIndex; + comboBox.ResetEventsCount(); + + // https://docs.microsoft.com/windows/win32/inputdev/wm-keyup + // The MSDN page tells us what bits of lParam to use for each of the parameters. + // All we need to do is some bit shifting to assemble lParam. + nint keyCode = (nint)key; + nint lParam = 0x00000001 | keyCode << 16; + for (int i = 0; i < expectedKeyPressesCount; i++) + { + User32.SendMessageW(comboBox, User32.WM.KEYDOWN, keyCode, lParam); + User32.SendMessageW(comboBox, User32.WM.KEYUP, keyCode, lParam); + } + + Assert.Equal(expectedKeyPressesCount, comboBox.EventsCount); + } + private void InitializeItems(ComboBox comboBox, int numItems) { for (int i = 0; i < numItems; i++) @@ -2472,6 +2497,15 @@ public void ConfigureForCtrlBackspace(int cursorRelativeToEnd = 0) private class ComboBoxWithSelectCounter : ComboBox { public int EventsCount { get; private set; } + + public void AddItems(int numToAdd) + { + for (int i = 0; i < numToAdd; i++) + { + Items.Add($"Item {i}"); + } + } + protected override void OnSelectedIndexChanged(EventArgs e) { base.OnSelectedIndexChanged(e);