Skip to content

Commit

Permalink
UI Test Improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
jknaudt21 committed Aug 15, 2023
1 parent 411b2a1 commit 45a3849
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@
</ScrollView>
</Grid>
<Label x:Name="events" AutomationId="DragEventsLabel" Text="EventsLabel"/>
<HorizontalStackLayout>
<Label Text="Drag Start: "/>
<Label x:Name="startLocation" AutomationId="StartDragEventLabel" Text="0,0"/>
</HorizontalStackLayout>
<HorizontalStackLayout>
<Label Text="Drag End: "/>
<Label x:Name="endLocation" AutomationId="EndDragEventLabel" Text="0,0"/>
</HorizontalStackLayout>
<!-- DragStarting Labels-->
<Label x:Name="dragStartRelativeSelf" AutomationId="DragStartRelativeSelf" Text="Drag Start relative to self:"/>
<Label x:Name="dragStartRelativeScreen" AutomationId="DragStartRelativeScreen" Text="Drag Start relative to screen:"/>
<Label x:Name="dragStartRelativeLabel" AutomationId="DragStartRelativeLabel" Text="Drag Start relative to this label:"/>
<!-- Drag Labels-->
<Label x:Name="dragRelativeDrop" AutomationId="DragRelativeDrop" Text="Drag relative to receiving layout:"/>
<Label x:Name="dragRelativeScreen" AutomationId="DragRelativeScreen" Text="Drag relative to screen:"/>
<Label x:Name="dragRelativeLabel" AutomationId="DragRelativeLabel" Text="Drag relative to this label:"/>
</StackLayout>
</ContentView>
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ private void OnDragStarting(object sender, DragStartingEventArgs e)

AddEvent(nameof(OnDragStarting));

startLocation.Text = $"{(int)e.GetPosition(null).Value.X},{(int)e.GetPosition(null).Value.X}";
dragStartRelativeSelf.Text = $"Drag Start relative to self: {(int)e.GetPosition(label).Value.X},{(int)e.GetPosition(label).Value.Y}";
dragStartRelativeScreen.Text = $"Drag Start relative to screen: {(int)e.GetPosition(null).Value.X},{(int)e.GetPosition(null).Value.Y}";
dragStartRelativeLabel.Text = $"Drag Start relative to this label: {(int)e.GetPosition(dragStartRelativeLabel).Value.X},{(int)e.GetPosition(dragStartRelativeLabel).Value.Y}";
}

private void OnDropCompleted(object sender, DropCompletedEventArgs e)
Expand Down Expand Up @@ -73,7 +75,9 @@ private void OnDragOver(object sender, DragEventArgs e)
_emittedDragOver = true;
}

endLocation.Text = $"{(int)e.GetPosition(null).Value.X},{(int)e.GetPosition(null).Value.X}";
dragRelativeDrop.Text = $"Drag relative to self: {(int)e.GetPosition(sl).Value.X},{(int)e.GetPosition(sl).Value.Y}";
dragRelativeScreen.Text = $"Drag relative to screen: {(int)e.GetPosition(null).Value.X},{(int)e.GetPosition(null).Value.Y}";
dragRelativeLabel.Text = $"Drag relative to receiving layout: {(int)e.GetPosition(dragRelativeLabel).Value.X},{(int)e.GetPosition(dragRelativeLabel).Value.Y}";
}

private void OnDragLeave(object sender, DragEventArgs e)
Expand Down
81 changes: 72 additions & 9 deletions src/Controls/tests/UITests/Tests/DragAndDropUITests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Maui.Controls.Sample;
using Microsoft.Maui.Appium;
using Microsoft.Maui.Controls.Xaml;
using Microsoft.Maui.Graphics;
using NUnit.Framework;
using TestUtils.Appium.UITests;
Expand Down Expand Up @@ -62,25 +63,87 @@ public void DragAndDropBetweenLayouts()
}

[Test]
public void DragAndDropCoordinates()
public void DragStartEventCoordinates()
{
App.WaitForElement("TargetView");
App.EnterText("TargetView", "DragAndDropBetweenLayouts");
App.Tap("GoButton");

App.WaitForElement("Blue");
App.DragAndDrop("Blue", "Green");
App.WaitForElement("DragEventsLabel");

var textStartDrag = App.Query("StartDragEventLabel").First().Text;
var textEndDrag = App.Query("EndDragEventLabel").First().Text;
var dragStartRelativeToSelf = GetCoordinatesFromLabel(App.Query("DragStartRelativeSelf").First().Text);
var dragStartRelativeToScreen = GetCoordinatesFromLabel(App.Query("DragStartRelativeScreen").First().Text);
var dragStartRelativeToLabel = GetCoordinatesFromLabel(App.Query("DragStartRelativeLabel").First().Text);

Assert.NotNull(dragStartRelativeToSelf);
Assert.NotNull(dragStartRelativeToScreen);
Assert.NotNull(dragStartRelativeToLabel);

Assert.True(dragStartRelativeToSelf!.Value.X > 0 && dragStartRelativeToSelf!.Value.Y > 0);
Assert.True(dragStartRelativeToScreen!.Value.X > 0 && dragStartRelativeToScreen!.Value.Y > 0);

// The position of the drag relative to itself should be less than that relative to the screen
// There are other elements in the screen, plus the ContentView of the test has some margin
Assert.True(dragStartRelativeToSelf!.Value.X < dragStartRelativeToScreen!.Value.X);
Assert.True(dragStartRelativeToSelf!.Value.Y < dragStartRelativeToScreen!.Value.Y);

// Since the label is below the the box, the Y position of the drag relative to the label should be negative
Assert.True(dragStartRelativeToLabel!.Value.Y < 0);
}

[Test]
public void DragEventCoordinates()
{
App.WaitForElement("TargetView");
App.EnterText("TargetView", "DragAndDropBetweenLayouts");
App.Tap("GoButton");

App.WaitForElement("Blue");
App.DragAndDrop("Blue", "Green");

var dragRelativeToDrop = GetCoordinatesFromLabel(App.Query("DragRelativeDrop").First().Text);
var dragRelativeToScreen = GetCoordinatesFromLabel(App.Query("DragRelativeScreen").First().Text);
var dragRelativeToLabel = GetCoordinatesFromLabel(App.Query("DragRelativeLabel").First().Text);
var dragStartRelativeToScreen = GetCoordinatesFromLabel(App.Query("DragStartRelativeScreen").First().Text);

Assert.NotNull(dragRelativeToDrop);
Assert.NotNull(dragRelativeToScreen);
Assert.NotNull(dragRelativeToLabel);
Assert.NotNull(dragStartRelativeToScreen);


Assert.True(dragRelativeToDrop!.Value.X > 0 && dragRelativeToDrop!.Value.Y > 0);
Assert.True(dragRelativeToScreen!.Value.X > 0 && dragRelativeToScreen!.Value.Y > 0);

// The position of the drag relative to the drop location should be less than that relative to the screen
// There are other elements in the screen, plus the ContentView of the test has some margin
Assert.True(dragRelativeToDrop!.Value.X < dragRelativeToScreen!.Value.X);
Assert.True(dragRelativeToDrop!.Value.Y < dragRelativeToScreen!.Value.Y);

// Since the label is below the the box, the Y position of the drag relative to the label should be negative
Assert.True(dragRelativeToLabel!.Value.Y < 0);

// The drag is executed left to right, so the X value should be higher than where it started
Assert.True(dragRelativeToScreen!.Value.X > dragStartRelativeToScreen!.Value.X);
}

// Helper function to parse out the X and Y coordinates from text labels 'Drag position: (x),(y)'
Point? GetCoordinatesFromLabel(string? labelText)
{
if (labelText is null)
return null;

var i = labelText.IndexOf(':', StringComparison.Ordinal);

if (i == -1)
return null;

Point startPoint = new Point(float.Parse(textStartDrag.Split(",")[0]), float.Parse(textStartDrag.Split(",")[1]));
Point endPoint = new Point(float.Parse(textEndDrag.Split(",")[0]), float.Parse(textEndDrag.Split(",")[1]));
var coordinates = labelText[(i + 1)..].Split(",");
var x = int.Parse(coordinates[0]);
var y = int.Parse(coordinates[1]);

Assert.True(startPoint.X != 0 && startPoint.Y != 0);
Assert.True(endPoint.X != 0 && endPoint.Y != 0);
Assert.True(endPoint.Y < startPoint.Y); // Blue is below Green as of writing this test
return new Point(x, y);
}
}
}

0 comments on commit 45a3849

Please sign in to comment.