Skip to content

Commit

Permalink
#478 #479 bitmap tests and fix for BitmapLoader.Create
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvreony committed May 20, 2020
1 parent 0f73e25 commit bcc2239
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Task<IBitmap> LoadFromResource(string resource, float? desiredWidth, floa
/// <inheritdoc />
public IBitmap Create(float width, float height)
{
return new BitmapSourceBitmap(new WriteableBitmap((int)width, (int)height, 96, 96, PixelFormats.Default, null));
return new BitmapSourceBitmap(new WriteableBitmap((int)width, (int)height, 96, 96, PixelFormats.Pbgra32, null));
}

private static void WithInit(BitmapImage source, Action<BitmapImage> block)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,14 @@ public Task<IBitmap> LoadFromResource(string resource, float? desiredWidth, floa
/// <inheritdoc />
public IBitmap Create(float width, float height)
{
return new BitmapSourceBitmap(new WriteableBitmap((int)width, (int)height, 96, 96, PixelFormats.Default, null));
/*
* Taken from MSDN:
*
* The preferred values for pixelFormat are Bgr32 and Pbgra32.
* These formats are natively supported and do not require a format conversion.
* Other pixelFormat values require a format conversion for each frame update, which reduces performance.
*/
return new BitmapSourceBitmap(new WriteableBitmap((int)width, (int)height, 96, 96, PixelFormats.Rgb24, null));
}

private static void WithInit(BitmapImage source, Action<BitmapImage> block)
Expand Down
76 changes: 73 additions & 3 deletions src/Splat.Tests/BitmapLoaderTests.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Xunit;

#if !NETSTANDARD && !NETCOREAPP2
namespace Splat.Tests
{
#if ANDROID
/// <summary>
/// Unit Tests for the Bitmap Loader.
/// </summary>
public sealed class BitmapLoaderTests
{
/// <summary>
/// Test data for the Load Suceeds Unit Test.
/// </summary>
public static TheoryData<Func<Stream>> LoadSucceedsTestData = new TheoryData<Func<Stream>>
{
GetPngStream,
GetJpegStream,
GetBitmapStream,
};

/// <summary>
/// Test to ensure the bitmap loader initializes properly.
/// </summary>
Expand All @@ -20,9 +31,68 @@ public sealed class BitmapLoaderTests
[Fact]
public void ReturnsInstance()
{
var instance = Splat.BitmapLoader.Current;
var instance = new Splat.PlatformBitmapLoader();
Assert.NotNull(instance);
}

/// <summary>
/// Test to ensure creating a default bitmap succeeds on all platforms.
/// </summary>
[Fact]
public void Create_Succeeds()
{
var instance = new Splat.PlatformBitmapLoader();
var result = instance.Create(1, 1);

Assert.NotNull(result);
}

/// <summary>
/// Test to ensure loading a bitmap succeeds on all platforms.
/// </summary>
/// <param name="getStream">Function to load a file stream.</param>
[Theory]
[MemberData(nameof(LoadSucceedsTestData))]
public void Load_Succeeds(Func<Stream> getStream)
{
var instance = new Splat.PlatformBitmapLoader();

using (var sourceStream = getStream())
{
var result = instance.Load(
sourceStream,
640,
480);

Assert.NotNull(result);
}
}

private static Stream GetBitmapStream()
{
var cwd = Directory.GetCurrentDirectory();
var path = Path.Combine(cwd, "splat-logo.bmp");
return GetStream(path);
}

private static Stream GetJpegStream()
{
var cwd = Directory.GetCurrentDirectory();
var path = Path.Combine(cwd, "splat-logo.jpg");
return GetStream(path);
}

private static Stream GetPngStream()
{
var cwd = Directory.GetCurrentDirectory();
var path = Path.Combine(cwd, "splat-logo.png");
return GetStream(path);
}

private static Stream GetStream(string path)
{
return File.OpenRead(path);
}
}
#endif
}
#endif
9 changes: 9 additions & 0 deletions src/Splat.Tests/Splat.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
</ItemGroup>

<ItemGroup>
<None Update="splat-logo.bmp">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="splat-logo.jpg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="splat-logo.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="xunit.runner.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Binary file added src/Splat.Tests/splat-logo.bmp
Binary file not shown.
Binary file added src/Splat.Tests/splat-logo.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Splat.Tests/splat-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bcc2239

Please sign in to comment.