Skip to content

Commit

Permalink
Add missing await call for color match test (dotnet#11016)
Browse files Browse the repository at this point in the history
* Add missing await call for color match test

* - remove duplicated ValidateHasColor code

* - fix type

* - remove async suffix

* - clean up extension methods

* - add alpha channel to tests

* - move BoxView tests to controls from core

* - skip tests

* - skip some more android tests

* - tizen

* - skip skip skip
  • Loading branch information
PureWeen authored Dec 13, 2022
1 parent 3cf3624 commit b6c3b53
Show file tree
Hide file tree
Showing 65 changed files with 262 additions and 638 deletions.
3 changes: 3 additions & 0 deletions src/Controls/tests/DeviceTests/ControlsHandlerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,5 +393,8 @@ protected IToolbar GetToolbar(IElementHandler handler)
.SingleOrDefault(x => x.Toolbar != null)
?.Toolbar;
}

protected Task ValidateHasColor<THandler>(IView view, Color color, Action action = null) =>
ValidateHasColor(view, color, typeof(THandler), action);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;

namespace Microsoft.Maui.DeviceTests
{
public partial class BoxViewTests
{
MauiShapeView GetNativeBoxView(ShapeViewHandler boxViewViewHandler) =>
boxViewViewHandler.PlatformView;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Graphics.Win2D;
using Microsoft.Maui.Handlers;

namespace Microsoft.Maui.DeviceTests
{
public partial class BoxViewTests
{
W2DGraphicsView GetNativeBoxView(ShapeViewHandler boxViewHandler) =>
boxViewHandler.PlatformView;
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Hosting;
using Xunit;


namespace Microsoft.Maui.DeviceTests
{
[Category(TestCategory.BoxView)]
public partial class BoxViewHandlerTests : CoreHandlerTestBase<ShapeViewHandler, BoxViewStub>
public partial class BoxViewTests : ControlsHandlerTestBase
{
[Theory(DisplayName = "BoxView Initializes Correctly")]
[InlineData(0xFF0000)]
[InlineData(0x00FF00)]
[InlineData(0x0000FF)]
[InlineData(0xFFFF0000)]
[InlineData(0xFF00FF00)]
[InlineData(0xFF0000FF)]
public async Task BoxViewInitializesCorrectly(uint color)
{
var expected = Color.FromUint(color);

var boxView = new BoxViewStub()
var boxView = new BoxView()
{
Color = expected,
Height = 100,
Width = 200
HeightRequest = 100,
WidthRequest = 200
};

await ValidateHasColor(boxView, expected);
await ValidateHasColor(boxView, expected, typeof(ShapeViewHandler));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Threading.Tasks;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;

namespace Microsoft.Maui.DeviceTests
{
public partial class BoxViewTests
{
MauiShapeView GetNativeBoxView(ShapeViewHandler boxViewHandler) =>
boxViewHandler.PlatformView;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,5 @@ public partial class CheckBoxTests
{
AppCompatCheckBox GetNativeCheckBox(CheckBoxHandler checkBoxHandler) =>
checkBoxHandler.PlatformView;

Task ValidateHasColor(ICheckBox checkBox, Color color, Action action = null)
{
return InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<CheckBoxHandler>(checkBox);
var nativeSwitch = GetNativeCheckBox(handler);
action?.Invoke();
nativeSwitch.AssertContainsColor(color);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,5 @@ public partial class CheckBoxTests
{
CheckBox GetNativeCheckBox(CheckBoxHandler checkBoxHandler) =>
checkBoxHandler.PlatformView;

Task ValidateHasColor(ICheckBox checkBox, Color color, Action action = null)
{
return InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<CheckBoxHandler>(checkBox);
var nativeSwitch = GetNativeCheckBox(handler);
action?.Invoke();
nativeSwitch.AssertContainsColor(color);
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Xunit;

namespace Microsoft.Maui.DeviceTests
Expand All @@ -23,7 +24,7 @@ public async Task UpdatingCheckBoxBackgroundColorUpdatesBackground(string colorS

checkBox.BackgroundColor = color;

await ValidateHasColor(checkBox, color);
await ValidateHasColor<CheckBoxHandler>(checkBox, color);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,5 @@ public partial class CheckBoxTests
{
MauiCheckBox GetNativeCheckBox(CheckBoxHandler checkBoxHandler) =>
checkBoxHandler.PlatformView;

Task ValidateHasColor(ICheckBox checkBox, Color color, Action action = null)
{
return InvokeOnMainThreadAsync(() =>
{
var handler = CreateHandler<CheckBoxHandler>(checkBox);
var nativeSwitch = GetNativeCheckBox(handler);
action?.Invoke();
nativeSwitch.AssertContainsColor(color);
});
}
}
}
4 changes: 2 additions & 2 deletions src/Controls/tests/DeviceTests/Elements/Frame/FrameTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public async Task BasicFrameTest()
};

var labelFrame =
await InvokeOnMainThreadAsync(() =>
frame.ToPlatform(MauiContext).AttachAndRun(async () =>
await InvokeOnMainThreadAsync(async () =>
await frame.ToPlatform(MauiContext).AttachAndRun(async () =>
{
(frame as IView).Measure(300, 300);
(frame as IView).Arrange(new Graphics.Rect(0, 0, 300, 300));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public async Task FormattedStringSpanTextHasCorrectColorWhenChanges()

formattedLabel.TextColor = expected;

await ValidateHasColor(formattedLabel, expected);
await ValidateHasColor<LabelHandler>(formattedLabel, expected);
}

TextView GetPlatformLabel(LabelHandler labelHandler) =>
Expand All @@ -46,15 +46,5 @@ TextUtils.TruncateAt GetPlatformLineBreakMode(LabelHandler labelHandler) =>

int GetPlatformMaxLines(LabelHandler labelHandler) =>
GetPlatformLabel(labelHandler).MaxLines;

Task ValidateHasColor(Label label, Color color, Action action = null)
{
return InvokeOnMainThreadAsync(async () =>
{
var labelHandler = CreateHandler<LabelHandler>(label);
action?.Invoke();
await labelHandler.PlatformView.AssertContainsColor(color);
});
}
}
}
6 changes: 3 additions & 3 deletions src/Controls/tests/DeviceTests/Elements/Label/LabelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ await InvokeOnMainThreadAsync(async () =>
var normalHandler = CreateHandler<LabelHandler>(normalLabel);
var normalBitmap = await normalHandler.PlatformView.ToBitmap();

await normalBitmap.AssertEqual(formattedBitmap);
await normalBitmap.AssertEqualAsync(formattedBitmap);
});
}

Expand Down Expand Up @@ -306,7 +306,7 @@ await InvokeOnMainThreadAsync(async () =>

var updatedBitmap = await updatedHandler.PlatformView.ToBitmap();

await updatedBitmap.AssertEqual(initialBitmap);
await updatedBitmap.AssertEqualAsync(initialBitmap);
});

static FormattedString GetFormattedString() =>
Expand Down Expand Up @@ -363,7 +363,7 @@ await InvokeOnMainThreadAsync(async () =>
var normalHandler = CreateHandler<LabelHandler>(normalLabel);
var normalBitmap = await normalHandler.PlatformView.ToBitmap();

await normalBitmap.AssertEqual(formattedBitmap);
await normalBitmap.AssertEqualAsync(formattedBitmap);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ await InvokeOnMainThreadAsync(async () =>
return updatingHandler.PlatformView.ToBitmap();
});

await initialBitmap.AssertEqual(updatingBitmap);
await initialBitmap.AssertEqualAsync(updatingBitmap);
});

static void CreateLayout(Type layoutType, out Layout layout, out Label label)
Expand Down
1 change: 1 addition & 0 deletions src/Controls/tests/DeviceTests/TestCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public static class TestCategory
public const string Accessibility = "Accessibility";
public const string Application = "Application";
public const string Behavior = "Behavior";
public const string BoxView = "BoxView";
public const string Button = "Button";
public const string CheckBox = "CheckBox";
public const string Compatibility = "Compatibility";
Expand Down
4 changes: 4 additions & 0 deletions src/Core/src/Platform/Android/MauiTextView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ public class MauiTextView : AppCompatTextView
{
public MauiTextView(Context context) : base(context)
{
this.ViewAttachedToWindow += MauiTextView_ViewAttachedToWindow;
}

private void MauiTextView_ViewAttachedToWindow(object? sender, ViewAttachedToWindowEventArgs e)
{
}

internal event EventHandler<LayoutChangedEventArgs>? LayoutChanged;
Expand Down
18 changes: 18 additions & 0 deletions src/Core/tests/DeviceTests.Shared/HandlerTests/HandlerTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,29 @@ protected TCustomHandler CreateHandler<THandler, TCustomHandler>(IElement elemen
#if PLATFORM
protected IPlatformViewHandler CreateHandler(IElement view, Type handlerType)
{
if (view.Handler is IPlatformViewHandler t)
return t;

var handler = (IPlatformViewHandler)Activator.CreateInstance(handlerType);
InitializeViewHandler(view, handler, MauiContext);
return handler;

}

protected Task ValidateHasColor(IView view, Color color, Type handlerType, Action action = null)
{
#if !TIZEN
return InvokeOnMainThreadAsync(async () =>
{
var plaformView = CreateHandler(view, handlerType).ToPlatform();
action?.Invoke();
await plaformView.AssertContainsColor(color);
});
#else
throw new NotImplementedException();
#endif
}

#endif

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ async protected Task ValidateUnrelatedPropertyUnaffected<TValue>(

Assert.Equal(initialNativeVal, newNativeVal);
}

protected Task ValidateHasColor(IView view, Color color, Action action = null) =>
ValidateHasColor(view, color, typeof(THandler), action);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@ ProgressBar GetNativeActivityIndicator(ActivityIndicatorHandler activityIndicato
bool GetNativeIsRunning(ActivityIndicatorHandler activityIndicatorHandler) =>
GetNativeActivityIndicator(activityIndicatorHandler).Visibility == ViewStates.Visible;

Task ValidateHasColor(IActivityIndicator activityIndicator, Color color, Action action = null)
{
return InvokeOnMainThreadAsync(() =>
{
var nativeActivityIndicator = GetNativeActivityIndicator(CreateHandler(activityIndicator));
action?.Invoke();
nativeActivityIndicator.AssertContainsColor(color);
});
}

[Theory(DisplayName = "Visibility is set correctly")]
[InlineData(Visibility.Visible)]
[InlineData(Visibility.Collapsed)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,5 @@ ProgressRing GetNativeActivityIndicator(ActivityIndicatorHandler activityIndicat

bool GetNativeIsRunning(ActivityIndicatorHandler activityIndicatorHandler) =>
GetNativeActivityIndicator(activityIndicatorHandler).IsActive;

Task ValidateHasColor(IActivityIndicator activityIndicator, Color color, Action action = null)
{
return InvokeOnMainThreadAsync(() =>
{
var nativeActivityIndicator = GetNativeActivityIndicator(CreateHandler(activityIndicator));
action?.Invoke();
nativeActivityIndicator.AssertContainsColor(color);
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public async Task IsRunningInitializesCorrectly(bool isRunning)
await ValidatePropertyInitValue(activityIndicator, () => activityIndicator.IsRunning, GetNativeIsRunning, activityIndicator.IsRunning);
}

[Fact(DisplayName = "Background Updates Correctly")]
[Fact(DisplayName = "Background Updates Correctly",
Skip = "This test is currently invalid https://github.com/dotnet/maui/issues/11948")]
public async Task BackgroundUpdatesCorrectly()
{
var activityIndicator = new ActivityIndicatorStub()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,5 @@ UIActivityIndicatorView GetNativeActivityIndicator(ActivityIndicatorHandler acti

bool GetNativeIsRunning(ActivityIndicatorHandler activityIndicatorHandler) =>
GetNativeActivityIndicator(activityIndicatorHandler).IsAnimating;

Task ValidateHasColor(IActivityIndicator activityIndicator, Color color, Action action = null)
{
return InvokeOnMainThreadAsync(() =>
{
var nativeActivityIndicator = GetNativeActivityIndicator(CreateHandler(activityIndicator));
action?.Invoke();
nativeActivityIndicator.AssertContainsColor(color);
});
}
}
}
Loading

0 comments on commit b6c3b53

Please sign in to comment.