-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(SampleChooserViewModel): Skia GenerateBitmap
- Loading branch information
1 parent
62287b3
commit f2db194
Showing
4 changed files
with
135 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 123 additions & 15 deletions
138
.../SamplesApp.UnitTests.Shared/Controls/UITests/Presentation/SampleChooserViewModel.skia.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters