Skip to content

Commit

Permalink
#7320 Fixing flaky ComboBox unit test (#7323)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkazennov authored Jun 22, 2022
1 parent e96089e commit ef0c855
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -36,102 +35,5 @@ await RunSingleControlTestAsync<ComboBox>((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<Form, ComboBoxWithSelectCounter, Task> 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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ef0c855

Please sign in to comment.