Skip to content

Commit

Permalink
Automated test
Browse files Browse the repository at this point in the history
  • Loading branch information
hartez committed Mar 10, 2023
1 parent bb84de0 commit 0e132e4
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.Graphics.Drawables;
using Microsoft.Maui.Controls;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class ImageTests
{
const string coffeeBase64 = "iVBORw0KGgoAAAANSUhEUgAAADAAAAA4CAYAAAC7UXvqAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3gUJADAhwicxqAAAAB1pVFh0Q29tbWVudAAAAAAAQ3JlYXRlZCB3aXRoIEdJTVBkLmUHAAABTklEQVRo3u2ZvUoDQRSFv2g6FZ/A0p/G9PZpJCCWFr6BoAGfxsY3sTZgbAKRYCtBCwkh6aIQmyFGEJPZnd25Y86B6XaX882cvXOHAemHpm6UprXUZ2zlAPJGJHjEFCEBCEAAAlhtAEmSpLiqzLW5SfpPfh+oJpKQ3w5GaiUEIAABCEAAAhDAf++FpsuuwCT1CI0Nent13ehfYwbQNwjQ91mBnkGAJx+AlkGAts/DNb6vf6yMA1/iriHznSwb2a2h+NxkeWkTeDcw+2/ARlbypgGAi7ytRTui+XtgPW/+9oFRBPNDYDfUT3QCfJZofgI0QleC85IgPoCzosrZqWv0ijI/Ao6Lrsl7wGMB5h/ct0s7+FwBgwDGB8BlrMPUNnDtOkVf4123z2yFNFTJ8e4hUAeOXBR25syNgRfg2dX2O5/+JguA1RuahROsm/rY+gI8XGfJmDMlSQAAAABJRU5ErkJggg==";
const string booksBase64 = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAZdEVYdFNvZnR3YXJlAHBhaW50Lm5ldCA0LjAuMjHxIGmVAAAB5UlEQVR4Xu2XsW4UQRBEzxkiITIidGgSZIgsMKk/gj/ylzgkceiIFDmFiMwiIkQkIKAab0mtUa00Y/qsG1096en2ptatmdoNzhtjjDHGmBXewmv4A/4p9BM8heQMfob5HpLXlKRdjz3H3mP2vTiHP2E7uMpbSL7CNifteitRWRhniLMM0z6RbUhGsyxRGY03boinUA2qloxmWaKy7CHs5giqIdWS0SxLVJaNM3XjAqAaUi0ZzbJEZVkXALtxAVANqZaMZlmisqwLgN3sagG/FvMayWvK6Qv4AuN/kzCu1+5bc9oC4rf8BXz879sdcR1rkRE1IzttASfLZ/BukeRMzchOW0DwDL6HzOI61jL5b5XTFhBP/Bts81jLb0Obt05XQPvU1+TboLLsdAWop75mz73TFVCtC4DduACohlRLVFatC4DduACohlRLVFatC4DduACohlRLVFatC4DduACohlRLVFatC4DduACohlRLVFatC4DduACohlRLVFatC4Dd7H0BT+BvqAZVSlRWaZwlzjTEB6iGVUpUVmmcZZhX8DtUA6skKqsyzvAS3osX8Apuq4jLRZX9r7Hn2HucYad4BN/AG8jNfoSvYWR7w3PIAo5jYd84gCwgrvcSFmCMMcYY88BsNn8BZTc8uHKN4gQAAAAASUVORK5CYII=";

[Fact]
public async Task CachingDisabledForImagesLoadViaInputStreams()
{
await InvokeOnMainThreadAsync(async () => {
var image1 = ImageSource.FromStream(() => new TestMemoryStream(Convert.FromBase64String(coffeeBase64)));
var image2 = ImageSource.FromStream(() => new TestMemoryStream(Convert.FromBase64String(booksBase64)));

var platformImage1 = await image1.GetPlatformImageAsync(MauiContext);
var platformImage2 = await image2.GetPlatformImageAsync(MauiContext);

var bitmapDrawable1 = Assert.IsType<BitmapDrawable>(platformImage1.Value);
var bitmapDrawable2 = Assert.IsType<BitmapDrawable>(platformImage2.Value);

// If Glide is using caching for images loaded from streams, then the second image will
// match the first image (because the second image load attempt will return the cached image)

// So we assert that the images are _not_ equal to ensure that caching is _not_ turned on for
// image streams.
await bitmapDrawable1.Bitmap.AssertNotEqualAsync(bitmapDrawable2.Bitmap);
});
}
}

// This subclass of memory stream is deliberately set up to trick Glide into using the cached image
// after the first image is loaded. See https://github.com/dotnet/maui/issues/8676#issuecomment-1416183584 and
// https://github.com/dotnet/maui/pull/13111#issuecomment-1416214847 for context.
class TestMemoryStream : MemoryStream
{
public TestMemoryStream(byte[] data) : base(data) { }

public override string ToString()
{
return "TestMemoryStream";
}

public override int GetHashCode()
{
return 42;
}

public override bool Equals(object obj)
{
return true;
}
}
}
39 changes: 28 additions & 11 deletions src/TestUtils/src/DeviceTests/AssertionExtensions.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Platform;
using Xunit;
using Xunit.Sdk;
using AColor = Android.Graphics.Color;
using AView = Android.Views.View;
using SearchView = AndroidX.AppCompat.Widget.SearchView;
Expand Down Expand Up @@ -448,25 +449,41 @@ public static Task AssertEqualAsync(this Bitmap bitmap, Bitmap other)

Assert.Equal(new Size(bitmap.Width, bitmap.Height), new Size(other.Width, other.Height));

Assert.True(IsMatching(), CreateEqualError(bitmap, other, $"Images did not match."));
Assert.True(IsMatching(bitmap, other), CreateEqualError(bitmap, other, $"Images did not match."));

return Task.CompletedTask;
}

bool IsMatching()
static bool IsMatching(Bitmap bitmap1, Bitmap bitmap2)
{
for (int x = 0; x < bitmap1.Width; x++)
{
for (int x = 0; x < bitmap.Width; x++)
for (int y = 0; y < bitmap1.Height; y++)
{
for (int y = 0; y < bitmap.Height; y++)
{
var first = bitmap.ColorAtPoint(x, y, true);
var second = other.ColorAtPoint(x, y, true);
var first = bitmap1.ColorAtPoint(x, y, true);
var second = bitmap2.ColorAtPoint(x, y, true);

if (!first.IsEquivalent(second))
return false;
}
if (!first.IsEquivalent(second))
return false;
}
return true;
}

return true;
}

public static Task AssertNotEqualAsync(this Bitmap bitmap, Bitmap other)
{
Assert.NotNull(bitmap);
Assert.NotNull(other);

Assert.NotEqual(new Size(bitmap.Width, bitmap.Height), new Size(other.Width, other.Height));

if (IsMatching(bitmap, other))
{
throw new XunitException(CreateEqualError(bitmap, other, $"Images did not match."));
}

return Task.CompletedTask;
}

public static TextUtils.TruncateAt? ToPlatform(this LineBreakMode mode) =>
Expand Down

0 comments on commit 0e132e4

Please sign in to comment.