Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable running compat unit tests for iOS & Android #27114

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions eng/cake/dotnet.cake
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ Task("dotnet-test")
"**/Resizetizer.UnitTests.csproj",
"**/Graphics.Tests.csproj",
"**/Compatibility.Core.UnitTests.csproj",
"**/Compatibility.iOS.UnitTests.csproj",
"**/Compatibility.Android.UnitTests.csproj",
};

var success = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ public void SecondaryToolbarItemsDontChangeColor()
SetupToolBar(settings, MauiContext);
AToolBar aToolBar = settings.ToolBar;
IMenuItem menuItem = settings.MenuItemsCreated.First();

#pragma warning disable XAOBS001 // Type or member is obsolete
MenuItemImpl menuItemImpl = (MenuItemImpl)menuItem;
#pragma warning restore XAOBS001 // Type or member is obsolete
Assert.IsNotNull(menuItemImpl, "menuItem is not of type MenuItemImpl");

if (menuItemImpl.TitleFormatted is SpannableString tf)
Expand Down Expand Up @@ -202,9 +203,11 @@ public void Layout(int width = 800, int height = 200)
public Color TintColor;
public List<IMenuItem> MenuItemsCreated;

#pragma warning disable XAOBS001 // Type or member is obsolete
public IEnumerable<ActionMenuItemView> TextViews =>
ToolBar.GetChildrenOfType<ActionMenuItemView>()
.OrderBy(x => x.Text);
#pragma warning restore XAOBS001 // Type or member is obsolete
}
}
}
4 changes: 4 additions & 0 deletions src/Compatibility/Core/tests/iOS/BackgroundColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ static IEnumerable TestCases
{
get
{
#pragma warning disable CS0618 // Type or member is obsolete
foreach (var element in BasicViews
.Where(e => !(e is Label) && !(e is BoxView) && !(e is Frame)))
#pragma warning restore CS0618 // Type or member is obsolete
{
element.BackgroundColor = Colors.AliceBlue;
yield return new TestCaseData(element)
Expand All @@ -38,7 +40,9 @@ public async Task BackgroundColorConsistent(VisualElement element)
[Description("Frame background color should match renderer background color")]
public async Task FrameBackgroundColorConsistent()
{
#pragma warning disable CS0618 // Type or member is obsolete
var frame = new Frame { BackgroundColor = Colors.AliceBlue };
#pragma warning restore CS0618 // Type or member is obsolete
var expectedColor = frame.BackgroundColor.ToPlatform();
var screenshot = await GetRendererProperty(frame, (ver) => ver.NativeView.ToBitmap(), requiresLayout: true);
screenshot.AssertColorAtCenter(expectedColor);
Expand Down
2 changes: 2 additions & 0 deletions src/Compatibility/Core/tests/iOS/BackgroundTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ static LinearGradientBrush LinearGradientBrush
[Description("Frame background should match renderer background")]
public async Task FrameLinearGradientBrushConsistent()
{
#pragma warning disable CS0618 // Type or member is obsolete
var frame = new Frame { HeightRequest = 50, WidthRequest = 50, Background = LinearGradientBrush };
#pragma warning restore CS0618 // Type or member is obsolete
var screenshot = await GetRendererProperty(frame, (ver) => ver.NativeView.ToBitmap(), requiresLayout: true);

var screenshotHeight = (int)screenshot.Size.Height;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,4 @@
<ProjectReference Include="..\..\..\Core\src\Compatibility.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\..\ControlGallery\src\UITests.Shared\Utilities\NumericExtensions.cs" />
<Compile Include="..\..\..\ControlGallery\src\UITests.Shared\Utilities\ParsingUtils.cs" />
</ItemGroup>

</Project>
3 changes: 2 additions & 1 deletion src/Compatibility/Core/tests/iOS/CornerRadiusTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ public async Task ButtonCornerRadius()
public async Task FrameCornerRadius()
{
var backgroundColor = Colors.CadetBlue;

#pragma warning disable CS0618 // Type or member is obsolete
var frame = new Frame
#pragma warning restore CS0618 // Type or member is obsolete
{
HeightRequest = 100,
WidthRequest = 200,
Expand Down
8 changes: 6 additions & 2 deletions src/Compatibility/Core/tests/iOS/FrameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ public class FrameTests : PlatformTestFixture
public async Task ReusingFrameRendererDoesCauseOverlapWithPreviousContent()
{
ContentPage page = new ContentPage();
#pragma warning disable CS0618 // Type or member is obsolete
Frame frame1 = new Frame()
#pragma warning restore CS0618 // Type or member is obsolete
{
Content = new Label()
{
Expand All @@ -35,8 +37,9 @@ await page.Dispatcher.DispatchAsync(() =>
using (var renderer = GetRenderer(frame1))
{
var frameRenderer = GetRenderer(frame1);

#pragma warning disable CS0618 // Type or member is obsolete
Frame frame2 = new Frame()
#pragma warning restore CS0618 // Type or member is obsolete
{
Content = new Label()
{
Expand Down Expand Up @@ -70,8 +73,9 @@ await page.Dispatcher.DispatchAsync(() =>

var uILabel = (UILabel)labelRenderer.NativeView.Subviews[0];
Assert.AreEqual("I am frame 2", uILabel.Text);

#pragma warning disable CS0618 // Type or member is obsolete
Frame frameWithButton = new Frame()
#pragma warning restore CS0618 // Type or member is obsolete
{
Content = new Button()
{
Expand Down
2 changes: 2 additions & 0 deletions src/Compatibility/Core/tests/iOS/PlatformTestFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ protected static IEnumerable<View> BasicViews
yield return new DatePicker { };
yield return new Editor { };
yield return new Entry { };
#pragma warning disable CS0618 // Type or member is obsolete
yield return new Frame { };
#pragma warning restore CS0618 // Type or member is obsolete
yield return new Image { };
yield return new ImageButton { };
yield return new Label { };
Expand Down
14 changes: 6 additions & 8 deletions src/Compatibility/Core/tests/iOS/RotationTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Collections;
using System.Threading.Tasks;
using NUnit.Framework;
using static Microsoft.Maui.Controls.Compatibility.UITests.NumericExtensions;
using static Microsoft.Maui.Controls.Compatibility.UITests.ParsingUtils;

namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS.UnitTests
{
Expand Down Expand Up @@ -50,8 +48,8 @@ static IEnumerable RotationCases
public async Task RotationXConsistent(View view)
{
var transform = await GetRendererProperty(view, r => r.NativeView.Layer.Transform, requiresLayout: true);
var actual = ParseCATransform3D(transform.ToString());
var expected = CalculateRotationMatrixForDegrees((float)view.RotationX, UITests.Axis.X);
var actual = ParsingUtils.ParseCATransform3D(transform.ToString());
var expected = NumericExtensions.CalculateRotationMatrixForDegrees((float)view.RotationX, Axis.X);
Assert.That(actual, Is.EqualTo(expected));
}

Expand All @@ -60,8 +58,8 @@ public async Task RotationXConsistent(View view)
public async Task RotationYConsistent(View view)
{
var transform = await GetRendererProperty(view, r => r.NativeView.Layer.Transform, requiresLayout: true);
var actual = ParseCATransform3D(transform.ToString());
var expected = CalculateRotationMatrixForDegrees((float)view.RotationY, UITests.Axis.Y);
var actual = ParsingUtils.ParseCATransform3D(transform.ToString());
var expected = NumericExtensions.CalculateRotationMatrixForDegrees((float)view.RotationY, Axis.Y);
Assert.That(actual, Is.EqualTo(expected));
}

Expand All @@ -70,8 +68,8 @@ public async Task RotationYConsistent(View view)
public async Task RotationConsistent(View view)
{
var transform = await GetRendererProperty(view, r => r.NativeView.Layer.Transform, requiresLayout: true);
var actual = ParseCATransform3D(transform.ToString());
var expected = CalculateRotationMatrixForDegrees((float)view.Rotation, UITests.Axis.Z);
var actual = ParsingUtils.ParseCATransform3D(transform.ToString());
var expected = NumericExtensions.CalculateRotationMatrixForDegrees((float)view.Rotation, Axis.Z);
Assert.That(actual, Is.EqualTo(expected));
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/Compatibility/Core/tests/iOS/ScaleTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System.Collections;
using System.Threading.Tasks;
using NUnit.Framework;
using static Microsoft.Maui.Controls.Compatibility.UITests.NumericExtensions;
using static Microsoft.Maui.Controls.Compatibility.UITests.ParsingUtils;

namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS.UnitTests
{
Expand All @@ -26,8 +24,8 @@ static IEnumerable ScaleCases
public async Task ScaleConsistent(View view)
{
var transform = await GetRendererProperty(view, r => r.NativeView.Layer.Transform, requiresLayout: true);
var actual = ParseCATransform3D(transform.ToString());
var expected = BuildScaleMatrix((float)view.Scale);
var actual = ParsingUtils.ParseCATransform3D(transform.ToString());
var expected = NumericExtensions.BuildScaleMatrix((float)view.Scale);
Assert.That(actual, Is.EqualTo(expected));
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/Compatibility/Core/tests/iOS/ShapeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using NUnit.Framework;
using ObjCRuntime;
using UIKit;
using static Microsoft.Maui.Controls.Compatibility.UITests.NumericExtensions;
using static Microsoft.Maui.Controls.Compatibility.UITests.ParsingUtils;
using CategoryAttribute = NUnit.Framework.CategoryAttribute;

namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS.UnitTests
Expand Down
168 changes: 168 additions & 0 deletions src/Compatibility/Core/tests/iOS/Utilities/NumericExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
using System;

namespace Microsoft.Maui.Controls.Compatibility.Platform.iOS.UnitTests
{
internal class Matrix : Object
{
public double M00, M01, M02, M03;
public double M10, M11, M12, M13;
public double M20, M21, M22, M23;
public double M30, M31, M32, M33;

public void Log()
{

//Logger.LogLine ();

//Logger.LogLine (string.Format ("{0,-3}, {1,-3}, {2,-3}, {3,-3}", M00, M01, M02, M03));
//Logger.LogLine (string.Format ("{0,-3}, {1,-3}, {2,-3}, {3,-3}", M10, M11, M12, M13));
//Logger.LogLine (string.Format ("{0,-3}, {1,-3}, {2,-3}, {3,-3}", M20, M21, M22, M23));
//Logger.LogLine (string.Format ("{0,-3}, {1,-3}, {2,-3}, {3,-3}", M30, M31, M32, M33));

//Logger.LogLine ();
}

public override bool Equals(object obj)
{
if (obj == null)
return false;

var transform = obj as Matrix;
if (transform is null)
return false;

const double tolerance = 0.01;
bool result =
Math.Abs(M00 - transform.M00) < tolerance &&
Math.Abs(M01 - transform.M01) < tolerance &&
Math.Abs(M02 - transform.M02) < tolerance &&
Math.Abs(M03 - transform.M03) < tolerance &&
Math.Abs(M10 - transform.M10) < tolerance &&
Math.Abs(M11 - transform.M11) < tolerance &&
Math.Abs(M12 - transform.M12) < tolerance &&
Math.Abs(M13 - transform.M13) < tolerance &&
Math.Abs(M20 - transform.M20) < tolerance &&
Math.Abs(M21 - transform.M21) < tolerance &&
Math.Abs(M22 - transform.M22) < tolerance &&
Math.Abs(M23 - transform.M23) < tolerance &&
Math.Abs(M30 - transform.M30) < tolerance &&
Math.Abs(M31 - transform.M31) < tolerance &&
Math.Abs(M32 - transform.M32) < tolerance &&
Math.Abs(M33 - transform.M33) < tolerance;

return result;
}

public override int GetHashCode()
{
return 0;
}
}

internal enum Axis
{
X,
Y,
Z
}

internal static class NumericExtensions
{
public static double ToRadians(this float val)
{
return (Math.PI / 180.0) * val;
}

public static Matrix CalculateRotationMatrixForDegrees(float degrees, Axis rotationAxis)
{
var angle = degrees.ToRadians();

var transform = new Matrix();
if (rotationAxis == Axis.X)
{
transform.M00 = 1;
transform.M01 = 0;
transform.M02 = 0;
transform.M03 = 0;
transform.M10 = 0;
transform.M11 = (float)Math.Cos(angle);
transform.M12 = (float)Math.Sin(angle);
transform.M13 = 0;
transform.M20 = 0;
transform.M21 = -(float)Math.Sin(angle);
transform.M22 = (float)Math.Cos(angle);
transform.M23 = 0;
transform.M30 = 0;
transform.M31 = 0;
transform.M32 = 0;
transform.M33 = 1;
}
else if (rotationAxis == Axis.Y)
{
transform.M00 = (float)Math.Cos(angle);
transform.M01 = 0;
transform.M02 = -(float)Math.Sin(angle);
transform.M03 = 0;
transform.M10 = 0;
transform.M11 = 1;
transform.M12 = 0;
transform.M13 = 0;
transform.M20 = (float)Math.Sin(angle);
transform.M21 = 0;
transform.M22 = (float)Math.Cos(angle);
transform.M23 = 0;
transform.M30 = 0;
transform.M31 = 0;
transform.M32 = 0;
transform.M33 = 1;
}
else
{
transform.M00 = (float)Math.Cos(angle);
transform.M01 = (float)Math.Sin(angle);
transform.M02 = 0;
transform.M03 = 0;
transform.M10 = -(float)Math.Sin(angle);
transform.M11 = (float)Math.Cos(angle);
transform.M12 = 0;
transform.M13 = 0;
transform.M20 = 0;
transform.M21 = 0;
transform.M22 = 1;
transform.M23 = 0;
transform.M30 = 0;
transform.M31 = 0;
transform.M32 = 0;
transform.M33 = 1;
}

return transform;
}

public static Matrix BuildScaleMatrix(float scale)
{
var transform = new Matrix();

transform.M00 = scale;
transform.M01 = 0;
transform.M02 = 0;
transform.M03 = 0;
transform.M10 = 0;
transform.M11 = scale;
transform.M12 = 0;
transform.M13 = 0;
transform.M20 = 0;
transform.M21 = 0;
transform.M22 = scale;
transform.M23 = 0;
transform.M30 = 0;
transform.M31 = 0;
transform.M32 = 0;
transform.M33 = 1;

return transform;
}

}

}
Loading
Loading