Skip to content

Commit

Permalink
test(SamplesApp): update Basic_Shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Jan 27, 2022
1 parent 8d9e9e4 commit 702fb13
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:win="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:skia="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
x:Name="_basicShapesTestRoot">
Expand Down Expand Up @@ -38,6 +39,10 @@
Content="Generate screenshots"
Click="GenerateScreenshots"
Margin="5,0,0,0" />
<skia:Button
Content="Generate screenshots"
Click="GenerateScreenshots"
Margin="5,0,0,0" />
<TextBox
x:Name="_idInput"
Margin="5,0,0,0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,22 @@ private void Update()

private async void GenerateScreenshots(object sender, RoutedEventArgs e)
{
#if WINDOWS_UWP
#if __SKIA__
// Workaround to avoid issue #7829
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, GenerateScreenshots);
#else
await GenerateScreenshots();
#endif
}
private async
#if __SKIA__
void
#else
Task
#endif
GenerateScreenshots()
{
#if WINDOWS_UWP || __SKIA__
_root.Visibility = Visibility.Collapsed;

var folder = await new FolderPicker { FileTypeFilter = { "*" } }.PickSingleFolderAsync();
Expand All @@ -271,100 +286,58 @@ private async void GenerateScreenshots(object sender, RoutedEventArgs e)
var alteratorsMap = _stretches.SelectMany(stretch => _sizes.Select(size => new[] { stretch, size })).ToArray();

foreach (var shape in _shapes)
foreach (var alterators in alteratorsMap)
{
var fileName = shape.Name + "_" + string.Join("_", alterators.Select(a => a.Id)) + ".png";
var grid = BuildHoriVertTestGridForScreenshot(shape, alterators);
_testZone.Child = grid;
await Task.Yield();
foreach (var alterators in alteratorsMap)
{
var fileName = shape.Name + "_" + string.Join("_", alterators.Select(a => a.Id)) + ".png";
var grid = BuildHoriVertTestGridForScreenshot(shape, alterators);
_testZone.Child = grid;
await Task.Yield();

var renderer = new RenderTargetBitmap();
await renderer.RenderAsync(grid);
var renderer = new RenderTargetBitmap();
await renderer.RenderAsync(grid);

var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (var output = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, output);
encoder.SetSoftwareBitmap(SoftwareBitmap.CreateCopyFromBuffer(await renderer.GetPixelsAsync(), BitmapPixelFormat.Bgra8, renderer.PixelWidth, renderer.PixelHeight));
await encoder.FlushAsync();
await output.FlushAsync();
var file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (var output = await file.OpenAsync(FileAccessMode.ReadWrite))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, output);
encoder.SetSoftwareBitmap(SoftwareBitmap.CreateCopyFromBuffer(await renderer.GetPixelsAsync(), BitmapPixelFormat.Bgra8, renderer.PixelWidth, renderer.PixelHeight));
await encoder.FlushAsync();
await output.FlushAsync();
}
}
}

_root.Visibility = Visibility.Visible;
_testZone.Child = null;
#endif
}

#if __IOS__ // This is the base work for a fix in the UITests in order to highly increase Screenshot speed. It will be removed by https://github.com/unoplatform/Uno.UITest/issues/39
private static byte[] RenderAsPng(FrameworkElement elt)
{
UIImage img;
try
{
UIGraphics.BeginImageContextWithOptions(new Size(elt.ActualWidth, elt.ActualHeight), true, UIScreen.MainScreen.Scale);
var ctx = UIGraphics.GetCurrentContext();
ctx.SetFillColor(Colors.White);
elt.Layer.RenderInContext(ctx);
img = UIGraphics.GetImageFromCurrentImageContext();
}
finally
{
UIGraphics.EndImageContext();
}

using (img)
{
return img.AsPNG().ToArray();
}
}
#elif __ANDROID__
private static byte[] RenderAsPng(FrameworkElement elt)
{
Android.Graphics.Bitmap b = Android.Graphics.Bitmap.CreateBitmap((int)ViewHelper.LogicalToPhysicalPixels(elt.ActualWidth), (int)ViewHelper.LogicalToPhysicalPixels(elt.ActualHeight), Android.Graphics.Bitmap.Config.Argb8888);
Android.Graphics.Canvas c = new Android.Graphics.Canvas(b);
var view = elt as Android.Views.View;

view.Layout(0, 0, (int)ViewHelper.LogicalToPhysicalPixels(elt.ActualWidth), (int)ViewHelper.LogicalToPhysicalPixels(elt.ActualHeight));
c.DrawColor(Android.Graphics.Color.White);
view.Draw(c);
using var stream = new MemoryStream();
b.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 100, stream);

return stream.ToArray();
}
#else
private static byte[] RenderAsPng(FrameworkElement elt)
=> throw new NotImplementedException("Not supported yet on this platform");
#endif

public string RunTests(string testNames)
{
TestResult = "";

var tests = testNames.Split(new [] {';'}, StringSplitOptions.RemoveEmptyEntries);
var tests = testNames.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
var id = Guid.NewGuid().ToString("N");

_ = ((DependencyObject)this).Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => _ = RunTestsCore(tests));
_ = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => _ = RunTestsCore(tests));

return id;

async Task RunTestsCore(string[] strings)
{
var result = new StringBuilder();
result.AppendLine(id);

var renderer = new RenderTargetBitmap();
foreach (var test in strings)
{
result.Append(test);
result.Append(';');
try
{
var elt = await RenderById(test);
var testResult = RenderAsPng(elt);
await renderer.RenderAsync(elt); ;
var testResult = await renderer.GetPixelsAsync();

result.Append("SUCCESS;");
result.Append(Convert.ToBase64String(testResult));
result.Append(Convert.ToBase64String(testResult.ToArray()));
}
catch (Exception e)
{
Expand Down

0 comments on commit 702fb13

Please sign in to comment.