Skip to content

Commit

Permalink
test: Added Test for ShadowContainer re-layout issue (#797)
Browse files Browse the repository at this point in the history
Co-authored-by: Steve Bilogan <steve.bilogan@gmail.com>
Co-authored-by: Rafael Rosa <rafaelmendesrosa@gmail.com>
Co-authored-by: Rafael Rosa <116665025+rafael-rosa-knowcode@users.noreply.github.com>
  • Loading branch information
4 people authored Sep 5, 2023
1 parent a98095e commit 4c78e92
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/Uno.Toolkit.RuntimeTests/Tests/ShadowContainerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.UI.ViewManagement;
using FluentAssertions;

namespace Uno.Toolkit.RuntimeTests.Tests
{
Expand Down Expand Up @@ -172,6 +173,68 @@ public async Task Outer_Shadows(int offsetX, int offsetY, bool inner)
}
}
#endif

[TestMethod]
public async Task ShadowContainer_ReLayoutsAfterChangeInSize()
{
ShadowContainer.ClearCache();

var internalBorder = new Border { Height = 200, Width = 200, Background = new SolidColorBrush(Colors.Green) };
bool createdNewCanvas = false;

var shadowContainer = new ShadowContainer
{
Content = internalBorder,
Shadows = new ShadowCollection
{
new UI.Shadow
{
Color = Colors.Blue,
OffsetX = 10,
OffsetY = 10,
IsInner = false,
Opacity = 1,

}
}
};

UnitTestsUIContentHelper.Content = shadowContainer;

shadowContainer.SurfacePaintCompleted += SurfacePaintCompleted;

shadowContainer.Shadows.Add(new UI.Shadow
{
Color = Colors.Red,
OffsetX = 10,
OffsetY = 10,
IsInner = false,
Opacity = 1,
});

await UnitTestsUIContentHelper.WaitForIdle();
await UnitTestsUIContentHelper.WaitForLoaded(shadowContainer);
createdNewCanvas.Should().BeTrue();
createdNewCanvas = false;

// Changing the height of the children content of the shadow.
internalBorder.Height = 400;

await UnitTestsUIContentHelper.WaitForIdle();

createdNewCanvas.Should().BeTrue();

shadowContainer.SurfacePaintCompleted -= SurfacePaintCompleted; ;

void SurfacePaintCompleted(object? sender, ShadowContainer.SurfacePaintCompletedEventArgs args)
{
// OnSurfacePainted can be called several times, we must make sure that the canvas is regenerated at least once.
if (createdNewCanvas == false)
{
createdNewCanvas = args.CreatedNewCanvas;
}
}
}
}
}
#endif
3 changes: 3 additions & 0 deletions src/Uno.Toolkit.Skia.WinUI/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Uno.Toolkit.RuntimeTests")]
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ public partial class ShadowContainer
private ShadowPaintState? _lastPaintState;
private bool _isShadowDirty;

internal event EventHandler<SurfacePaintCompletedEventArgs>? SurfacePaintCompleted;

internal static void ClearCache()
{
Cache.Clear();
}

private void OnSurfacePaintCompleted(bool createdNewCanvas)
{
SurfacePaintCompleted?.Invoke(this, new SurfacePaintCompletedEventArgs(createdNewCanvas));
}

private bool NeedsPaint(ShadowPaintState state, out bool pixelRatioChanged)
{
var needsPaint = state != _lastPaintState;
Expand Down Expand Up @@ -84,6 +96,7 @@ private void OnSurfacePainted(object? sender, SKPaintSurfaceEventArgs e)
else
{
canvas.DrawImage(snapshot, SKPoint.Empty);
OnSurfacePaintCompleted(createdNewCanvas: false);
return;
}
}
Expand Down Expand Up @@ -120,6 +133,8 @@ private void OnSurfacePainted(object? sender, SKPaintSurfaceEventArgs e)
{
Cache.AddOrUpdate(key, e.Surface.Snapshot());
}

OnSurfacePaintCompleted(createdNewCanvas: true);
}

private static Color? GetBackgroundColor(Brush? background)
Expand Down Expand Up @@ -344,4 +359,14 @@ x is { } y &&
PixelRatio == y.PixelRatio &&
Shadows.SequenceEqual(y.Shadows);
}

public class SurfacePaintCompletedEventArgs : EventArgs
{
public bool CreatedNewCanvas { get; }

public SurfacePaintCompletedEventArgs(bool createdNewCanvas)
{
CreatedNewCanvas = createdNewCanvas;
}
}
}
10 changes: 10 additions & 0 deletions src/Uno.Toolkit.Skia.WinUI/Controls/Shadows/ShadowsCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ public bool Remove(string key)
return _shadowsCache.TryRemove(key, out var _);
}

internal void Clear()
{
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.Trace("Cleared [ShadowsCache]");
}

_shadowsCache.Clear();
}

private class CacheBucket
{
public CacheBucket(SKImage bitmap)
Expand Down

0 comments on commit 4c78e92

Please sign in to comment.