Skip to content

Commit

Permalink
chore: ComboBoxHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Aug 21, 2024
1 parent 768facc commit 9e61a04
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.Threading.Tasks;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Tests.Enterprise;
using Private.Infrastructure;
using Uno.UI.RuntimeTests.MUX.Helpers;
using Windows.Foundation;
using static Private.Infrastructure.TestServices;

namespace Microsoft.UI.Xaml.Tests.Common;

internal class ComboBoxHelper
internal static class ComboBoxHelper
{
public enum OpenMethod
{
Expand All @@ -30,36 +32,34 @@ public enum CloseMethod
Programmatic
};

//public static async Task FocusComboBoxIfNecessary(ComboBox comboBox)
//{
// var comboBoxGotFocusEvent = std.make_shared<Event>();
// var gotFocusRegistration = CreateSafeEventRegistration(ComboBox, GotFocus);
// gotFocusRegistration.Attach(comboBox, [&]()

// {
// LOG_OUTPUT(L"[ComboBox]: Got Focus Event Fired.");
// comboBoxGotFocusEvent->Set();
// });

// bool alreadyHasFocus = false;
// RunOnUIThread([&]()
public static async Task FocusComboBoxIfNecessary(ComboBox comboBox)
{
var comboBoxGotFocusEvent = new Event();
var gotFocusRegistration = CreateSafeEventRegistration<ComboBox, RoutedEventHandler>("GotFocus");
gotFocusRegistration.Attach(comboBox, (s, e) =>
{
LOG_OUTPUT("[ComboBox]: Got Focus Event Fired.");
comboBoxGotFocusEvent.Set();
});

// {
// alreadyHasFocus = comboBox->FocusState != xaml.FocusState.Unfocused;
// comboBox->Focus(xaml.FocusState.Keyboard);
// });
bool alreadyHasFocus = false;
await RunOnUIThread(() =>
{
alreadyHasFocus = comboBox.FocusState != FocusState.Unfocused;
comboBox.Focus(FocusState.Keyboard);
});

// if (!alreadyHasFocus)
// {
// comboBoxGotFocusEvent->WaitForDefault();
// }
//}
if (!alreadyHasFocus)
{
await comboBoxGotFocusEvent.WaitForDefault();
}
}

public static async Task OpenComboBox(ComboBox comboBox, OpenMethod openMethod)
{
var comboBoxOpenedEvent = std.make_shared<Event>();
var openedRegistration = CreateSafeEventRegistration(ComboBox, DropDownOpened);
openedRegistration.Attach(comboBox, [&](){ comboBoxOpenedEvent->Set(); });
var comboBoxOpenedEvent = new Event();
var openedRegistration = CreateSafeEventRegistration<ComboBox, EventHandler<object>>("DropDownOpened");
openedRegistration.Attach(comboBox, (s, e) => { comboBoxOpenedEvent.Set(); });

if (openMethod == OpenMethod.Mouse)
{
Expand All @@ -71,12 +71,12 @@ public static async Task OpenComboBox(ComboBox comboBox, OpenMethod openMethod)
}
else if (openMethod == OpenMethod.Keyboard)
{
FocusComboBoxIfNecessary(comboBox);
await FocusComboBoxIfNecessary(comboBox);
TestServices.KeyboardHelper.PressKeySequence(" ");
}
else if (openMethod == OpenMethod.Gamepad)
{
FocusComboBoxIfNecessary(comboBox);
await FocusComboBoxIfNecessary(comboBox);
CommonInputHelper.Accept(InputDevice.Gamepad);
}
else if (openMethod == OpenMethod.Programmatic)
Expand All @@ -88,7 +88,7 @@ await RunOnUIThread(() =>
}
await TestServices.WindowHelper.WaitForIdle();

comboBoxOpenedEvent->WaitForDefault();
await comboBoxOpenedEvent.WaitForDefault();
}

public static async Task CloseComboBox(ComboBox comboBox)
Expand All @@ -98,13 +98,13 @@ public static async Task CloseComboBox(ComboBox comboBox)

public static async Task CloseComboBox(ComboBox comboBox, CloseMethod closeMethod)
{
var dropDownClosedEvent = std.make_shared<Event>();
var dropDownClosedRegistration = CreateSafeEventRegistration(ComboBox, DropDownClosed);
dropDownClosedRegistration.Attach(comboBox, [&](){ dropDownClosedEvent->Set(); });
var dropDownClosedEvent = new Event();
var dropDownClosedRegistration = CreateSafeEventRegistration<ComboBox, EventHandler<object>>("DropDownClosed");
dropDownClosedRegistration.Attach(comboBox, (s, e) => { dropDownClosedEvent.Set(); });

if (closeMethod == CloseMethod.Touch || closeMethod == CloseMethod.Mouse)
{
Rect dropdownBounds = GetBoundsOfOpenDropdown(comboBox);
Rect dropdownBounds = await GetBoundsOfOpenDropdown(comboBox);
int outsideBuffer = 10; // Tap at least this far away from the dropdown in order to ensure that it closes.
var closeTapPoint = new Point(dropdownBounds.X + dropdownBounds.Width + outsideBuffer, dropdownBounds.Y + dropdownBounds.Height + outsideBuffer);

Expand All @@ -129,39 +129,37 @@ public static async Task CloseComboBox(ComboBox comboBox, CloseMethod closeMetho
{
await RunOnUIThread(() =>
{
comboBox->IsDropDownOpen = false;
comboBox.IsDropDownOpen = false;
});
}

dropDownClosedEvent->WaitForDefault();
Private.Infrastructure.await TestServices.WindowHelper.WaitForIdle();
await dropDownClosedEvent.WaitForDefault();
await TestServices.WindowHelper.WaitForIdle();

dropDownClosedRegistration.Detach();
}

static async Rect GetBoundsOfOpenDropdown(xaml.DependencyObject^ element)
private static async Task<Rect> GetBoundsOfOpenDropdown(DependencyObject element)
{
Rect dropdownBounds = { };
Rect dropdownBounds = new();

await RunOnUIThread(() =>
{
var dropdownScrollViewer = safe_cast < ScrollViewer ^> (TreeHelper.GetVisualChildByNameFromOpenPopups(L"ScrollViewer", element));
WEX.Common.Throw.IfFalse(dropdownScrollViewer != nullptr, E_FAIL, L"DropDown not found.");
dropdownBounds = ControlHelper.GetBounds(dropdownScrollViewer);
LOG_OUTPUT(L"dropdownBounds: (%f, %f, %f, %f)", dropdownBounds.X, dropdownBounds.Y, dropdownBounds.Width, dropdownBounds.Height);
await RunOnUIThread(async () =>
{
var dropdownScrollViewer = (ScrollViewer)(TreeHelper.GetVisualChildByNameFromOpenPopups("ScrollViewer", element));
Assert.IsNotNull(dropdownScrollViewer, "DropDown not found.");
dropdownBounds = await ControlHelper.GetBounds(dropdownScrollViewer);
});
Private.Infrastructure.await TestServices.WindowHelper.WaitForIdle();
LOG_OUTPUT("dropdownBounds: (%f, %f, %f, %f)", dropdownBounds.X, dropdownBounds.Y, dropdownBounds.Width, dropdownBounds.Height);
});
await TestServices.WindowHelper.WaitForIdle();

return dropdownBounds;
}

// Verify the selected Index on the ComboBox.
static void VerifySelectedIndex(ComboBox comboBox, int expected)
public static async Task VerifySelectedIndex(ComboBox comboBox, int expected)
{
SelectorHelper.VerifySelectedIndex(comboBox, expected);
await SelectorHelper.VerifySelectedIndex(comboBox, expected);
}

// Uses touch to select the ComboBoxItem with the specified index.
Expand All @@ -172,19 +170,18 @@ public static async Task SelectItemWithTap(ComboBox comboBox, int index)
await OpenComboBox(comboBox, OpenMethod.Touch);
await TestServices.WindowHelper.WaitForIdle();

Event selectionChangedEvent;
var selectionChangedRegistration = CreateSafeEventRegistration(ComboBox, SelectionChanged);
selectionChangedRegistration.Attach(comboBox, [&](){ selectionChangedEvent.Set(); });

ComboBoxItem comboBoxItemToSelect;
RunOnUIThread([&]()
Event selectionChangedEvent = new();
var selectionChangedRegistration = CreateSafeEventRegistration<ComboBox, SelectionChangedEventHandler>("SelectionChanged");
selectionChangedRegistration.Attach(comboBox, (s, e) => selectionChangedEvent.Set());

{
comboBoxItemToSelect = safe_cast < ComboBoxItem ^> (comboBox->ContainerFromIndex(index));
ComboBoxItem comboBoxItemToSelect = null;
await RunOnUIThread(() =>
{
comboBoxItemToSelect = (ComboBoxItem)comboBox.ContainerFromIndex(index);
THROW_IF_NULL(comboBoxItemToSelect);
});

TestServices.InputHelper.Tap(comboBoxItemToSelect);
selectionChangedEvent.WaitForDefault();
await selectionChangedEvent.WaitForDefault();
}
}
21 changes: 21 additions & 0 deletions src/Uno.UI.RuntimeTests/IntegrationTests/common/SelectorHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.UI.Xaml.Controls.Primitives;
using static Private.Infrastructure.TestServices;

namespace Microsoft.UI.Xaml.Tests.Common;

internal static class SelectorHelper
{
public static async Task VerifySelectedIndex(Selector selector, int expected)
{
await RunOnUIThread(() =>
{
LOG_OUTPUT("Selected Index = %d", selector.SelectedIndex);
VERIFY_ARE_EQUAL(expected, selector.SelectedIndex);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static void ScrollMouseWheel(CalendarView cv, int i)
}

public static void LeftMouseClick(UIElement element) => Tap(element);
public static void LeftMouseClick(Point point) => throw new NotSupportedException("Not supported yet.");

public static void PenBarrelTap(FrameworkElement pElement)
{
Expand Down

0 comments on commit 9e61a04

Please sign in to comment.