Skip to content

Commit

Permalink
chore: adding textBox pointer test
Browse files Browse the repository at this point in the history
  • Loading branch information
Kunal22shah committed Nov 26, 2024
1 parent 58e477b commit 02e3c5d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using FluentAssertions;
using FluentAssertions.Execution;
using NUnit.Framework;
using SamplesApp.UITests.Extensions;
using SamplesApp.UITests.TestFramework;
using Uno.UITest;
using Uno.UITest.Helpers;
Expand Down Expand Up @@ -992,5 +993,25 @@ public void TextBox_Selection_IsReadOnly()
// Final verification of SelectedText
Assert.IsNotEmpty(readonlyTextBox.GetDependencyPropertyValue("SelectedText")?.ToString());
}

[Test]
[AutoRetry]
public void When_ClickAndDrag_SelectsDefaultText()
{

Run("UITests.Shared.Windows_UI_Input.PointersTests.TextBox_Pointer");

_app.WaitForElement("TestTextBox");

var textBoxRect = _app.GetPhysicalRect("TestTextBox");

var startPoint = textBoxRect.GetPointInRect(new System.Drawing.PointF(0.1f, 0.5f));
var endPoint = textBoxRect.GetPointInRect(new System.Drawing.PointF(0.9f, 0.5f));

_app.DragCoordinates(startPoint, endPoint);

var resultText = _app.GetText("ResultText");
Assert.AreEqual("Pointer Released - No text selected", resultText, "Dragging should not select text.");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Page x:Class="UITests.Shared.Windows_UI_Input.PointersTests.TextBoxPointer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:UITests.Shared.Windows_UI_Input.PointersTests"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<StackPanel>
<StackPanel Orientation="Horizontal"
Margin="0,20">
<Border x:Name="TestTextBoxBorder"
Background="LightGray"
Width="300"
Height="50">
<TextBox x:Name="TestTextBox"
Text="Default Text"
VerticalAlignment="Center"
HorizontalAlignment="Center" />
</Border>

<TextBlock x:Name="ResultText"
Text="No action yet"
Margin="10,0,0,0" />
</StackPanel>
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Uno.UI.Samples.Controls;
using Windows.Foundation;

namespace UITests.Shared.Windows_UI_Input.PointersTests
{
[Sample("Pointers",Description = "This sample tests an issue where dragging on a TextBox selects text incorrectly. To ensures that dragging across the TextBox does not select text unless explicitly selected.")]
public sealed partial class TextBoxPointer : Page
{
public TextBoxPointer()
{
this.InitializeComponent();
SetupPointerEvents();
}

private void SetupPointerEvents()
{
TestTextBoxBorder.PointerPressed += OnPointerPressed;
TestTextBoxBorder.PointerMoved += OnPointerMoved;
TestTextBoxBorder.PointerReleased += OnPointerReleased;
}

private bool _isDragging;
private Point? _initialPoint;

private void OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
_initialPoint = e.GetCurrentPoint(TestTextBoxBorder).Position;
_isDragging = false;
ResultText.Text = "Pointer Pressed";
}

private void OnPointerMoved(object sender, PointerRoutedEventArgs e)
{
if (_initialPoint is Point initialPoint)
{
var currentPoint = e.GetCurrentPoint(TestTextBoxBorder).Position;

if (Math.Abs(currentPoint.X - initialPoint.X) > 2 || Math.Abs(currentPoint.Y - initialPoint.Y) > 2)
{
_isDragging = true;
ResultText.Text = "Pointer Dragging";
}
}
}

private void OnPointerReleased(object sender, PointerRoutedEventArgs e)
{
if (_isDragging)
{
var selectedText = TestTextBox.SelectedText;
ResultText.Text = string.IsNullOrEmpty(selectedText)
? "Pointer Released - No text selected"
: $"Pointer Released - Selected Text: '{selectedText}'";
}
else
{
ResultText.Text = "Pointer Released - No drag detected";
}

_isDragging = false;
_initialPoint = null;
}
}
}

0 comments on commit 02e3c5d

Please sign in to comment.