Skip to content

Commit

Permalink
test(SampleChooserViewModel): Skia GenerateBitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
workgroupengineering committed Feb 1, 2022
1 parent 62287b3 commit f2db194
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public partial class SampleChooserViewModel

private Section _lastSection = Section.Library;
private readonly Stack<Section> _previousSections = new Stack<Section>();
private static readonly Windows.UI.Xaml.Media.SolidColorBrush _screenshotBackground =
new Windows.UI.Xaml.Media.SolidColorBrush(Windows.UI.Colors.White);

// A static instance used during UI Testing automation
public static SampleChooserViewModel Instance { get; private set; }
Expand Down Expand Up @@ -291,8 +293,10 @@ await Window.Current.Dispatcher.RunAsync(

private async Task RecordAllTestsInner(string folderName, CancellationToken ct, Action doneAction = null)
{
var timer = new global::System.Diagnostics.Stopwatch();
try
{
timer.Start();
#if TRACK_REFS
var initialInactiveStats = Uno.UI.DataBinding.BinderReferenceHolder.GetInactiveViewReferencesStats();
var initialActiveStats = Uno.UI.DataBinding.BinderReferenceHolder.GetReferenceStats();
Expand Down Expand Up @@ -411,6 +415,11 @@ from sample in category.SamplesContent
}
finally
{
timer.Stop();
if (_log.IsEnabled(LogLevel.Information))
{
_log.Info($"{nameof(RunRuntimeTests)} is finished in {timer.Elapsed}");
}
// Done action is needed as awaiting the task is not enough to determine the end of this method.
doneAction?.Invoke();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,151 @@
#if __WASM__
using SampleControl.Entities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
using System.Linq;
using Uno.Disposables;
using System.Reflection;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Uno.UI;
using Uno.UI.Samples.Controls;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Uno.Foundation.Logging;
using Windows.UI.Xaml.Media;
using Windows.Graphics.Imaging;
using Windows.Storage;
using Windows.Graphics.Display;
using Uno.UI.Extensions;
using System.Collections.Immutable;

namespace SampleControl.Presentation
{

public partial class SampleChooserViewModel
{
public void PrintViewHierarchy(UIElement vg, StringBuilder sb, int level = 0)
public void PrintViewHierarchy(UIElement c, StringBuilder sb, int level = 0)
{

var children = c.GetChildren().ToImmutableArray();
for (int i = 0; i < children.Length; i++)
{
var v = children[i];
var vElement = (FrameworkElement)v;
var desc = string.Concat(Enumerable.Repeat(" |", level)) + $" [{i + 1}/{children.Length}] {v.GetType().Name}";
if (vElement != null)
{
desc += $" -- ActualHeight:{vElement.ActualHeight}, ActualWidth:{vElement.ActualWidth}, Height:{vElement.Height}, Width:{vElement.Width}, DataContext:{vElement.DataContext?.GetType().FullName}";
var vTextBlock = vElement as TextBlock;
if (vTextBlock != null)
{
desc += $", Text: {vTextBlock.Text}";
}
}

sb.AppendLine(desc);
var childViewGroup = v as Control;
if (childViewGroup != null)
{
PrintViewHierarchy(childViewGroup, sb, level + 1);
}
}
}

private async Task DumpOutputFolderName(CancellationToken ct, string folderName)
{
var fullPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), folderName);
var folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(
folderName,
CreationCollisionOption.OpenIfExists
).AsTask(ct);

Console.WriteLine($"Output folder for tests: {fullPath}");
if (this.Log().IsEnabled(LogLevel.Debug))
{
this.Log().Debug($"Output folder for tests: {folder.Path}");
}
}

private async Task GenerateBitmap(CancellationToken ct, string folderName, string fileName, IFrameworkElement content)
{

FrameworkElement element;

if (content.Parent is FrameworkElement parent)
{
element = parent;
}
else
{
element = (FrameworkElement)content;
}

if (element is null)
throw new Exception("Invalid element");

(double oldMinWidth, double oldMinHeight, double oldWidth, double oldHeight)
= (element.MinWidth, element.MinHeight, element.Width, element.Height);
try
{
element.MinWidth = 400;
element.MinHeight = 400;
element.Width = 1024;
element.Height = 774;

var border = element.FindFirstChild<Border>();

if (border != null)
{
border.Background = _screenshotBackground;
}

element.InvalidateMeasure();
element.InvalidateArrange();
await Task.Yield();
element.Measure(new Windows.Foundation.Size(1024, 774));
element.Arrange(new Windows.Foundation.Rect(0, 0, 1024, 774));
await Task.Yield();

var bmp = new Windows.UI.Xaml.Media.Imaging.RenderTargetBitmap();

await bmp.RenderAsync(element).AsTask(ct);

var pixels = await bmp.GetPixelsAsync().AsTask(ct);

var folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(
folderName,
CreationCollisionOption.OpenIfExists
).AsTask(ct);

if (folder == null)
{
folder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName).AsTask(ct);
}

var file = await folder.CreateFileAsync(
fileName,
CreationCollisionOption.ReplaceExisting
).AsTask(ct);

using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite).AsTask(ct))
{
var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream).AsTask(ct);

encoder.SetPixelData(
BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Ignore,
(uint)bmp.PixelWidth,
(uint)bmp.PixelHeight,
DisplayInformation.GetForCurrentView().RawDpiX,
DisplayInformation.GetForCurrentView().RawDpiY,
pixels.ToArray()
);

await encoder.FlushAsync().AsTask(ct);
}
}
finally
{
(element.MinWidth, element.MinHeight, element.Width, element.Height) =
(oldMinWidth, oldMinHeight, oldWidth, oldHeight);
await Task.Yield();
}


}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if __SKIA__
#if __WASM__
using SampleControl.Entities;
using System;
using System.Collections;
Expand All @@ -22,8 +22,6 @@ namespace SampleControl.Presentation

public partial class SampleChooserViewModel
{
public static Action<string> TakeScreenShot;

public void PrintViewHierarchy(UIElement vg, StringBuilder sb, int level = 0)
{

Expand All @@ -38,9 +36,7 @@ private async Task DumpOutputFolderName(CancellationToken ct, string folderName)

private async Task GenerateBitmap(CancellationToken ct, string folderName, string fileName, IFrameworkElement content)
{
Directory.CreateDirectory(folderName);

TakeScreenShot(Path.Combine(folderName, fileName));

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Presentation\SampleChooserViewMode.Commands.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Presentation\SampleChooserViewMode.Properties.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Presentation\SampleChooserViewModel.wasm.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Presentation\SampleChooserViewModel.macOS.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Presentation\SampleChooserViewModel.Android.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Presentation\SampleChooserViewModel.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Presentation\SampleChooserViewModel.iOS.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Presentation\SampleChooserViewModel.netstd.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Presentation\SampleChooserViewModel.skia.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Presentation\SampleChooserViewModel.Uwa.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\UITests\Views\Behaviors\ListViewBaseCommand.cs" />
Expand Down

0 comments on commit f2db194

Please sign in to comment.