-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Windows Image device tests (#20167)
- Loading branch information
1 parent
dbf7aad
commit 744f951
Showing
7 changed files
with
235 additions
and
20 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
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
26 changes: 26 additions & 0 deletions
26
src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.Windows.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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using System; | ||
using Microsoft.UI.Xaml.Media.Imaging; | ||
using WImage = Microsoft.UI.Xaml.Controls.Image; | ||
using WStretch = Microsoft.UI.Xaml.Media.Stretch; | ||
|
||
namespace Microsoft.Maui.DeviceTests | ||
{ | ||
public partial class ImageHandlerTests<TImageHandler, TStub> | ||
{ | ||
WImage GetPlatformImageView(IImageHandler imageHandler) => | ||
imageHandler.PlatformView; | ||
|
||
bool GetNativeIsAnimationPlaying(IImageHandler imageHandler) => | ||
GetPlatformImageView(imageHandler).Source is BitmapImage bitmapImage && bitmapImage.IsPlaying; | ||
|
||
Aspect GetNativeAspect(IImageHandler imageHandler) => | ||
GetPlatformImageView(imageHandler).Stretch switch | ||
{ | ||
WStretch.Uniform => Aspect.AspectFit, | ||
WStretch.UniformToFill => Aspect.AspectFill, | ||
WStretch.Fill => Aspect.Fill, | ||
WStretch.None => Aspect.Center, | ||
_ => throw new ArgumentOutOfRangeException("Stretch") | ||
}; | ||
} | ||
} |
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
94 changes: 94 additions & 0 deletions
94
src/Core/tests/DeviceTests/Services/ImageSource/BaseImageSourceServiceTests.Windows.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 |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using System; | ||
using System.IO; | ||
using System.Runtime.InteropServices.WindowsRuntime; | ||
using System.Text; | ||
using Microsoft.Maui.Devices; | ||
using Microsoft.Maui.Graphics; | ||
using Microsoft.Maui.Storage; | ||
using Microsoft.UI.Xaml.Media.Imaging; | ||
using Windows.Graphics.Imaging; | ||
using Xunit; | ||
using WColor = Windows.UI.Color; | ||
|
||
namespace Microsoft.Maui.DeviceTests | ||
{ | ||
public abstract partial class BaseImageSourceServiceTests | ||
{ | ||
public static string CreateBitmapFile(int width, int height, Color color, string filename = null) => | ||
CreateBitmapFile(width, height, color.ToWindowsColor(), filename); | ||
|
||
public static string CreateBitmapFile(int width, int height, WColor color, string filename = null) | ||
{ | ||
filename ??= Guid.NewGuid().ToString("N") + ".png"; | ||
if (!Path.IsPathRooted(filename)) | ||
{ | ||
filename = Path.Combine(FileSystem.CacheDirectory, Guid.NewGuid().ToString("N"), filename); | ||
} | ||
var dir = Path.GetDirectoryName(filename); | ||
Directory.CreateDirectory(dir); | ||
|
||
using var src = CreateBitmapStream(width, height, color); | ||
using var dst = File.Create(filename); | ||
src.CopyTo(dst); | ||
|
||
return filename; | ||
} | ||
|
||
public static Stream CreateBitmapStream(int width, int height, Color color) => | ||
CreateBitmapStream(width, height, color.ToWindowsColor()); | ||
|
||
public static Stream CreateBitmapStream(int width, int height, WColor color) | ||
{ | ||
var bitmap = CreateBitmap(width, height, color); | ||
|
||
var stream = new MemoryStream(); | ||
|
||
var encoder = BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream.AsRandomAccessStream()).GetAwaiter().GetResult(); | ||
|
||
encoder.SetPixelData( | ||
BitmapPixelFormat.Bgra8, | ||
BitmapAlphaMode.Ignore, | ||
(uint)bitmap.PixelWidth, | ||
(uint)bitmap.PixelHeight, | ||
96, | ||
96, | ||
bitmap.PixelBuffer.ToArray()); | ||
|
||
stream.Position = 0; | ||
|
||
return stream; | ||
} | ||
|
||
public static WriteableBitmap CreateBitmap(int width, int height, Color color) => | ||
CreateBitmap(width, height, color.ToWindowsColor()); | ||
|
||
public static WriteableBitmap CreateBitmap(int width, int height, WColor color) | ||
{ | ||
var bitmap = new WriteableBitmap(width, height); | ||
|
||
using (var stream = bitmap.PixelBuffer.AsStream()) | ||
{ | ||
var pixels = new byte[width * height * 4]; | ||
|
||
for (var y = 0; y < height; y++) | ||
{ | ||
for (var x = 0; x < width; x++) | ||
{ | ||
var index = (y * width + x) * 4; | ||
|
||
pixels[index + 0] = color.B; | ||
pixels[index + 1] = color.G; | ||
pixels[index + 2] = color.R; | ||
pixels[index + 3] = color.A; | ||
} | ||
} | ||
|
||
stream.Write(pixels, 0, pixels.Length); | ||
} | ||
|
||
bitmap.Invalidate(); | ||
|
||
return bitmap; | ||
} | ||
} | ||
} |
Oops, something went wrong.