Skip to content

Commit

Permalink
Merge pull request #17633 from unoplatform/dev/dr/wasmPointers
Browse files Browse the repository at this point in the history
feat(wasm): Enable managed pointers on wasm
  • Loading branch information
dr1rrb authored Aug 16, 2024
2 parents abd7376 + 92edbfa commit 3651fcc
Show file tree
Hide file tree
Showing 82 changed files with 1,714 additions and 1,698 deletions.
10 changes: 10 additions & 0 deletions src/SamplesApp/SamplesApp.UITests/Extensions/AppExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Uno.UITest;
using Uno.UITest.Helpers;
using Uno.UITest.Helpers.Queries;
Expand All @@ -13,6 +15,14 @@ namespace SamplesApp.UITests.Extensions
{
public static class AppExtensions
{
#if !IS_RUNTIME_UI_TESTS
public static async Task<FileInfo> TakeScreenshotAsync(this IApp app, string title)
=> app.Screenshot(title);

public static async ValueTask DragCoordinatesAsync(this IApp app, float fromX, float fromY, float toX, float toY, CancellationToken ct = default)
=> app.DragCoordinates(fromX, fromY, toX, toY);
#endif

public static void DragCoordinates(this IApp app, PointF from, PointF to) => app.DragCoordinates(from.X, from.Y, to.X, to.Y);

#if !IS_RUNTIME_UI_TESTS
Expand Down
6 changes: 6 additions & 0 deletions src/SamplesApp/SamplesApp.UITests/SampleControlUITestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ private void WriteSystemLogs(string fileName)
}
}

public async ValueTask<ScreenshotInfo> TakeScreenshotAsync(string stepName, bool? ignoreInSnapshotCompare = null)
=> TakeScreenshot(stepName, ignoreInSnapshotCompare);

public ScreenshotInfo TakeScreenshot(string stepName, bool? ignoreInSnapshotCompare = null)
=> TakeScreenshot(
stepName,
Expand All @@ -289,6 +292,9 @@ public ScreenshotInfo TakeScreenshot(string stepName, bool? ignoreInSnapshotComp
: null
);

public async ValueTask<ScreenshotInfo> TakeScreenshotAsync(string stepName, ScreenshotOptions options)
=> TakeScreenshot(stepName, options);

public ScreenshotInfo TakeScreenshot(string stepName, ScreenshotOptions options)
{
if (_app == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<DefineConstants Condition="$(UnoTargetFrameworkOverride.ToLowerInvariant().Contains('-ios'))">$(DefineConstants);TARGET_FRAMEWORK_OVERRIDE_IOS</DefineConstants>

<!-- Use 'GeneratedRegexAttribute' to generate the regular expression -->
<NoWarn>$(NoWarn);SYSLIB1045</NoWarn>
<NoWarn>$(NoWarn);SYSLIB1045;CS1998</NoWarn>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions src/SamplesApp/SamplesApp.UITests/TestFramework/ImageAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,27 @@ static ImageAssert()
#region Are[Almost]Equal
public static void AreEqual(ScreenshotInfo expected, ScreenshotInfo actual, IAppRect rect, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0)
=> AreEqualImpl(expected, rect.ToRectangle(), actual, rect.ToRectangle(), expectedToActualScale, tolerance, line);
public static async Task AreEqualAsync(ScreenshotInfo expected, ScreenshotInfo actual, IAppRect rect, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0)
=> AreEqualImpl(expected, rect.ToRectangle(), actual, rect.ToRectangle(), expectedToActualScale, tolerance, line);

public static void AreEqual(ScreenshotInfo expected, ScreenshotInfo actual, Rectangle? rect = null, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0)
=> AreEqualImpl(expected, rect ?? FirstQuadrant, actual, rect ?? FirstQuadrant, expectedToActualScale, tolerance, line);

public static async Task AreEqualAsync(ScreenshotInfo expected, ScreenshotInfo actual, Rectangle? rect = null, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0)
=> AreEqualImpl(expected, rect ?? FirstQuadrant, actual, rect ?? FirstQuadrant, expectedToActualScale, tolerance, line);

public static void AreEqual(ScreenshotInfo expected, IAppRect expectedRect, ScreenshotInfo actual, IAppRect actualRect, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0)
=> AreEqualImpl(expected, expectedRect.ToRectangle(), actual, actualRect.ToRectangle(), expectedToActualScale, tolerance, line);

public static async Task AreEqualAsync(ScreenshotInfo expected, IAppRect expectedRect, ScreenshotInfo actual, IAppRect actualRect, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0)
=> AreEqualImpl(expected, expectedRect.ToRectangle(), actual, actualRect.ToRectangle(), expectedToActualScale, tolerance, line);

public static void AreEqual(ScreenshotInfo expected, Rectangle expectedRect, ScreenshotInfo actual, Rectangle actualRect, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0)
=> AreEqualImpl(expected, expectedRect, actual, actualRect, expectedToActualScale, tolerance, line);

public static async Task AreEqualAsync(ScreenshotInfo expected, Rectangle expectedRect, ScreenshotInfo actual, Rectangle actualRect, double expectedToActualScale = 1, PixelTolerance tolerance = default, [CallerLineNumber] int line = 0)
=> AreEqualImpl(expected, expectedRect, actual, actualRect, expectedToActualScale, tolerance, line);

/// <summary>
/// Asserts that two screenshots are equal to each other inside the area of the nominated rect to within the given pixel error
/// (ie, the A, R, G, or B values cumulatively differ by more than the permitted error for any pixel).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,102 +1,250 @@
using NUnit.Framework;
#if IS_RUNTIME_UI_TESTS
#define CAN_ASSERT_CONTENT
#if HAS_RENDER_TARGET_BITMAP
#define CAN_ASSERT_IMAGE
#endif
using Microsoft/* UWP don't rename */.UI.Xaml.Controls;
using Uno.UI.RuntimeTests.Helpers;
#else
#define CAN_ASSERT_IMAGE
#endif

#if !CAN_ASSERT_CONTENT && !CAN_ASSERT_IMAGE
#error No way to assert the test result
#endif

using NUnit.Framework;
using SamplesApp.UITests.TestFramework;
using Uno.UITest;
using Uno.UITest.Helpers;
using Uno.UITest.Helpers.Queries;
using System.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Windows.Devices.Input;
using SamplesApp.UITests.Extensions;
using Uno.Testing;

namespace SamplesApp.UITests.Windows_UI_Xaml.DragAndDropTests
{
[TestFixture]
public partial class DragDrop_TreeView_Reorder_Automated : SampleControlUITestBase
{
const int _itemHeight = 70;
const int _offset = 30;
[Test]
[AutoRetry]
[ActivePlatforms(Platform.Browser)]
public void When_Dragging_TreeView_Item()
[InjectedPointer(PointerDeviceType.Mouse)]
#if !IS_RUNTIME_UI_TESTS
[Ignore("Flaky")]
#endif
public async Task When_Dragging_TreeView_Item()
{
Run("UITests.Windows_UI_Xaml.DragAndDrop.DragDrop_TreeView", skipInitialScreenshot: true);

var tv = _app.Marked("tv");
var bt0 = _app.Marked("bt0");
var bt1 = _app.Marked("bt1");
var bt2 = _app.Marked("bt2");
var bt3 = _app.Marked("bt3");
var focusbt = _app.Marked("focusbt");
var radio_disable = _app.Marked("radio_disable");

_app.WaitForElement(radio_disable);
_app.Tap(radio_disable);

_app.WaitForElement(tv);
_app.Tap(bt1);
_app.Tap(focusbt);
var case1 = _app.Screenshot("case1");

_app.Tap(bt2);
_app.Tap(focusbt);
var case2 = _app.Screenshot("case2");

_app.Tap(bt3);
_app.Tap(focusbt);
var case3 = _app.Screenshot("case3");

_app.Tap(bt0);
var tvBounds = _app.Query("tv").Single().Rect;
float fromX = tvBounds.X + 100;
float fromY = tvBounds.Y + _itemHeight * 3 + _offset;
float toX = tvBounds.X + 100;
float toY = tvBounds.Y + _offset;
_app.DragCoordinates(fromX, fromY, toX, toY);
_app.Tap(focusbt);
var result = _app.Screenshot("result1");
ImageAssert.AreEqual(case1, result);
await RunAsync("UITests.Windows_UI_Xaml.DragAndDrop.DragDrop_TreeView", skipInitialScreenshot: true);

var tv = App.Marked("tv");
var bt0 = App.Marked("bt0");
var bt1 = App.Marked("bt1");
var bt2 = App.Marked("bt2");
var bt3 = App.Marked("bt3");
var focusbt = App.Marked("focusbt");
var radio_disable = App.Marked("radio_disable");

App.WaitForElement(radio_disable);
App.Tap(radio_disable);

App.WaitForElement(tv);

#if CAN_ASSERT_IMAGE
App.Tap(bt1);
App.Tap(focusbt);
var case1 = await Screenshot("case1");

App.Tap(bt2);
App.Tap(focusbt);
var case2 = await Screenshot("ase2");

App.Tap(bt3);
App.Tap(focusbt);
var case3 = await Screenshot("case3");

App.Tap(bt0);
#endif
#if CAN_ASSERT_CONTENT
App.Tap(bt0);
await UITestHelper.WaitForIdle();
await UITestHelper.WaitForIdle();
await UITestHelper.WaitForIdle();

var treeView = (TreeView)App.Query(tv).Single().Element;
Assert.IsNotNull(treeView);
Assert.AreEqual(
""""
v aaa
- bbb
- ccc
- ddd
- eee
"""".Trim().Replace("\r\n", "\n"),
Dump(treeView));
#endif

var tvBounds = App.Query("tv").Single().Rect;
var fromX = tvBounds.X + 100;
var fromY = tvBounds.Y + _itemHeight * 3 + _offset;
var toX = tvBounds.X + 100;
var toY = tvBounds.Y + _offset;

await App.DragCoordinatesAsync(fromX, fromY, toX, toY);
App.Tap(focusbt);

#if CAN_ASSERT_IMAGE
var result = await Screenshot("result1");
await ImageAssert.AreEqualAsync(case1, result);
#endif
#if CAN_ASSERT_CONTENT
await UITestHelper.WaitForIdle();
Assert.AreEqual(
""""
v aaa
- bbb
- ddd
- ccc
- eee
"""".Trim().Replace("\r\n", "\n"),
Dump(treeView));
#endif

fromY = tvBounds.Y + _itemHeight * 1 + _offset;
toY = tvBounds.Y + _itemHeight * 4 + _offset;
_app.DragCoordinates(fromX, fromY, toX, toY);
_app.Tap(focusbt);
result = _app.Screenshot("result2");
ImageAssert.AreEqual(case2, result);
await App.DragCoordinatesAsync(fromX, fromY, toX, toY);
App.Tap(focusbt);

#if CAN_ASSERT_IMAGE
result = await Screenshot("result2");
await ImageAssert.AreEqualAsync(case2, result);
#endif
#if CAN_ASSERT_CONTENT
await UITestHelper.WaitForIdle();
Assert.AreEqual(
""""
v aaa
- ddd
- ccc
> eee
"""".Trim().Replace("\r\n", "\n"),
Dump(treeView));
#endif

fromY = tvBounds.Y + _itemHeight * 3 + _offset;
toY = tvBounds.Y + _itemHeight - 5;
_app.DragCoordinates(fromX, fromY, toX, toY);
_app.Tap(focusbt);
result = _app.Screenshot("result3");
ImageAssert.AreEqual(case3, result);
await App.DragCoordinatesAsync(fromX, fromY, toX, toY);
App.Tap(focusbt);

#if CAN_ASSERT_IMAGE
result = await Screenshot("result3");
await ImageAssert.AreEqualAsync(case3, result);
#endif
#if CAN_ASSERT_CONTENT
await UITestHelper.WaitForIdle();
Assert.AreEqual(
""""
v aaa
> eee
- ddd
- ccc
"""".Trim().Replace("\r\n", "\n"),
Dump(treeView));
#endif

#if CAN_ASSERT_IMAGE
#if IS_RUNTIME_UI_TESTS
async ValueTask<RawBitmap> Screenshot(string name) => await UITestHelper.ScreenShot((TreeView)App.Query(tv).Single().Element);
#else
async ValueTask<FileInfo> Screenshot(string name) => await App.TakeScreenshotAsync(name);
#endif
#endif
}

[Test]
[AutoRetry]
[ActivePlatforms(Platform.Browser)]
public void When_OnDragItemsCompleted()
[InjectedPointer(PointerDeviceType.Mouse)]
public async Task When_OnDragItemsCompleted()
{
Run("UITests.Windows_UI_Xaml.DragAndDrop.DragDrop_TreeView", skipInitialScreenshot: true);
var tv = _app.Marked("tv");
var bt0 = _app.Marked("bt0");
var radio_enable = _app.Marked("radio_enable");
var tb = _app.Marked("tb");

_app.WaitForElement(tv);

_app.WaitForElement(radio_enable);
_app.Tap(radio_enable);
_app.WaitForElement(bt0);
_app.Tap(bt0);

var tvBounds = _app.Query("tv").Single().Rect;
float fromX = tvBounds.X + 100;
float fromY = tvBounds.Y + _itemHeight * 3 + _offset;
float toX = tvBounds.X + 100;
float toY = tvBounds.Y + _offset;
_app.DragCoordinates(fromX, fromY, toX, toY);

string text = _app.GetText("tb").Trim();
await RunAsync("UITests.Windows_UI_Xaml.DragAndDrop.DragDrop_TreeView", skipInitialScreenshot: true);
var tv = App.Marked("tv");
var bt0 = App.Marked("bt0");
var radio_enable = App.Marked("radio_enable");
var tb = App.Marked("tb");

App.WaitForElement(tv);

App.WaitForElement(radio_enable);
App.Tap(radio_enable);
App.WaitForElement(bt0);
App.Tap(bt0);

#if IS_RUNTIME_UI_TESTS
await UITestHelper.WaitForIdle();
await UITestHelper.WaitForIdle();
await UITestHelper.WaitForIdle();
#endif

var tvBounds = App.Query("tv").Single().Rect;
var fromX = tvBounds.X + 100;
var fromY = tvBounds.Y + _itemHeight * 3 + _offset;
var toX = tvBounds.X + 100;
var toY = tvBounds.Y + _offset;
await App.DragCoordinatesAsync(fromX, fromY, toX, toY);

#if IS_RUNTIME_UI_TESTS
await UITestHelper.WaitForIdle();
await UITestHelper.WaitForIdle();
await UITestHelper.WaitForIdle();
#endif

string text = App.GetText("tb").Trim();
Assert.AreEqual("DragItemsCompleted is triggered", text);
}

#if CAN_ASSERT_CONTENT
private string Dump(TreeView tv)
{
var dump = new StringBuilder();
DumpCore(0, tv.RootNodes);
return dump.ToString().Trim().Replace("\r\n", "\n");

void DumpCore(int level, IList<TreeViewNode> nodes)
{
foreach (var node in nodes)
{
dump.Append(new string('\t', level));
switch (node)
{
case { Children: null or { Count: 0 } }:
dump.Append("- ");
dump.AppendLine(node.Content?.ToString() ?? "--null--");
break;

case { IsExpanded: false }:
dump.Append("> ");
dump.AppendLine(node.Content?.ToString() ?? "--null--");
break;

default:
dump.Append("v ");
dump.AppendLine(node.Content?.ToString() ?? "--null--");
DumpCore(level + 1, node.Children);
break;
}
}
}
}
#endif
}
}
Loading

0 comments on commit 3651fcc

Please sign in to comment.