Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't clear images to avoid Glide not loading images #6543

Merged
merged 5 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ protected void onResourceCleared(@Nullable Drawable placeholder) {
public void onLoadFailed(@Nullable Drawable errorDrawable) {
callback.onComplete(false,
errorDrawable,
() -> view.post(
() -> requestManager.clear(MauiCustomViewTarget.this)
)
() -> { } // TODO: explicitly release image
);
}

Expand All @@ -43,9 +41,7 @@ public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? s
this.view.setImageDrawable(resource);
callback.onComplete(true,
resource,
() -> view.post(
() -> requestManager.clear(MauiCustomViewTarget.this)
)
() -> { } // TODO: explicitly release image
);
}
}
Binary file modified src/Core/src/maui.aar
Binary file not shown.
40 changes: 40 additions & 0 deletions src/Core/tests/DeviceTests/Handlers/Image/ImageHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,46 @@ public abstract partial class ImageHandlerTests<TImageHandler, TStub> : HandlerT
where TImageHandler : IImageHandler, new()
where TStub : StubBase, IImageStub, new()
{
[Theory(
#if IOS
Skip = "Test failing on IOS"
#endif
)]
[InlineData("#FF0000")]
[InlineData("#00FF00")]
[InlineData("#000000")]
public async Task UpdatingSourceUpdatesImageCorrectly(string colorHex)
{
// create files
var expectedColor = Color.FromArgb(colorHex);
var firstPath = BaseImageSourceServiceTests.CreateBitmapFile(100, 100, Colors.Blue);
var secondPath = BaseImageSourceServiceTests.CreateBitmapFile(100, 100, expectedColor);

await InvokeOnMainThreadAsync(async () =>
{
var image = new TStub { Width = 100, Height = 100 };
var handler = CreateHandler(image);
var platformView = GetPlatformImageView(handler);

await platformView.AttachAndRun(async () =>
{
// the first one works
image.Source = new FileImageSourceStub(firstPath);
handler.UpdateValue(nameof(IImage.Source));
await image.Wait();

await platformView.AssertContainsColor(Colors.Blue.ToPlatform());

// the second one does not
image.Source = new FileImageSourceStub(secondPath);
handler.UpdateValue(nameof(IImage.Source));
await image.Wait();

await platformView.AssertContainsColor(expectedColor.ToPlatform());
});
});
}

[Theory(
#if _ANDROID__
Skip = "Test failing on ANDROID"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace Microsoft.Maui.DeviceTests
{
public abstract partial class BaseImageSourceServiceTests
{
protected string CreateBitmapFile(int width, int height, Color color, string filename = null) =>
public static string CreateBitmapFile(int width, int height, Color color, string filename = null) =>
CreateBitmapFile(width, height, color.ToPlatform(), filename);

protected string CreateBitmapFile(int width, int height, AColor color, string filename = null)
public static string CreateBitmapFile(int width, int height, AColor color, string filename = null)
{
filename ??= Guid.NewGuid().ToString("N") + ".png";
if (!Path.IsPathRooted(filename))
Expand All @@ -30,10 +30,10 @@ protected string CreateBitmapFile(int width, int height, AColor color, string fi
return filename;
}

protected Stream CreateBitmapStream(int width, int height, Color color) =>
public static Stream CreateBitmapStream(int width, int height, Color color) =>
CreateBitmapStream(width, height, color.ToPlatform());

protected Stream CreateBitmapStream(int width, int height, AColor color)
public static Stream CreateBitmapStream(int width, int height, AColor color)
{
using var bitmap = CreateBitmap(width, height, color);

Expand All @@ -47,10 +47,10 @@ protected Stream CreateBitmapStream(int width, int height, AColor color)
return stream;
}

protected ABitmap CreateBitmap(int width, int height, Color color) =>
public static ABitmap CreateBitmap(int width, int height, Color color) =>
CreateBitmap(width, height, color.ToPlatform());

protected ABitmap CreateBitmap(int width, int height, AColor color)
public static ABitmap CreateBitmap(int width, int height, AColor color)
{
var bitmap = ABitmap.CreateBitmap(width, height, ABitmap.Config.Argb8888);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ namespace Microsoft.Maui.DeviceTests
{
public abstract partial class BaseImageSourceServiceTests
{
protected string CreateBitmapFile(int width, int height, Color color, string filename = null) =>
public static string CreateBitmapFile(int width, int height, Color color, string filename = null) =>
CreateBitmapFile(width, height, color.ToPlatform(), filename);

protected string CreateBitmapFile(int width, int height, UIColor color, string filename = null)
public static string CreateBitmapFile(int width, int height, UIColor color, string filename = null)
{
filename ??= Guid.NewGuid().ToString("N") + ".png";
if (!Path.IsPathRooted(filename))
Expand All @@ -31,10 +31,10 @@ protected string CreateBitmapFile(int width, int height, UIColor color, string f
return filename;
}

protected Stream CreateBitmapStream(int width, int height, Color color) =>
public static Stream CreateBitmapStream(int width, int height, Color color) =>
CreateBitmapStream(width, height, color.ToPlatform());

protected Stream CreateBitmapStream(int width, int height, UIColor color)
public static Stream CreateBitmapStream(int width, int height, UIColor color)
{
using var bitmap = CreateBitmap(width, height, color);

Expand All @@ -49,10 +49,10 @@ protected Stream CreateBitmapStream(int width, int height, UIColor color)
return stream;
}

protected UIImage CreateBitmap(int width, int height, Color color) =>
public static UIImage CreateBitmap(int width, int height, Color color) =>
CreateBitmap(width, height, color.ToPlatform());

protected UIImage CreateBitmap(int width, int height, UIColor color)
public static UIImage CreateBitmap(int width, int height, UIColor color)
{
var rect = new CGRect(0, 0, width, height);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Threading.Tasks;
using Android.Graphics.Drawables;
using Microsoft.Maui.DeviceTests.Stubs;
using Xunit;
using Color = Microsoft.Maui.Graphics.Color;
Expand Down Expand Up @@ -34,12 +33,8 @@ public async Task GetDrawableAsyncWithResource(string filename, string colorHex)

using var result = await service.GetDrawableAsync(imageSource, MauiProgram.DefaultContext);

var bitmapDrawable = Assert.IsType<BitmapDrawable>(result.Value);

var bitmap = bitmapDrawable.Bitmap;

var expectedColor = Color.FromArgb(colorHex);
bitmap.AssertColorAtCenter(expectedColor.ToPlatform());
result.Value.AssertColorAtCenter(expectedColor.ToPlatform());
}

[Theory]
Expand All @@ -57,21 +52,7 @@ public async Task GetDrawableAsyncWithFile(string colorHex)

using var result = await service.GetDrawableAsync(imageSource, MauiProgram.DefaultContext);

var bitmapDrawable = Assert.IsType<BitmapDrawable>(result.Value);

var bitmap = bitmapDrawable.Bitmap;

bitmap.AssertColorAtCenter(expectedColor.ToPlatform());
}

async Task<Drawable> GetDrawable(IImageSourceService service, IImageSource imageSource)
{
var tcsDrawable = new TaskCompletionSource<Drawable>();

// get an image
var result1 = await service.GetDrawableAsync(imageSource, MauiProgram.DefaultContext).ConfigureAwait(false);

return await tcsDrawable.Task.ConfigureAwait(false);
result.Value.AssertColorAtCenter(expectedColor.ToPlatform());
}
}
}
31 changes: 27 additions & 4 deletions src/TestUtils/src/DeviceTests/AssertionExtensions.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading;
using System.Threading.Tasks;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.Text;
using Android.Views;
using Android.Widget;
Expand Down Expand Up @@ -75,10 +76,24 @@ public static Task AttachAndRun(this AView view, Action action) =>
view.AttachAndRun(() =>
{
action();
return Task.FromResult(true);
});

public static Task<T> AttachAndRun<T>(this AView view, Func<T> action) =>
view.AttachAndRun(() =>
{
var result = action();
return Task.FromResult(result);
});

public static Task AttachAndRun(this AView view, Func<Task> action) =>
view.AttachAndRun(async () =>
{
await action();
return true;
});

public static async Task<T> AttachAndRun<T>(this AView view, Func<T> action)
public static async Task<T> AttachAndRun<T>(this AView view, Func<Task<T>> action)
{
if (view.Parent is WrapperView wrapper)
view = wrapper;
Expand Down Expand Up @@ -116,14 +131,13 @@ public static async Task<T> AttachAndRun<T>(this AView view, Func<T> action)
return await Run(view, action);
}

static async Task<T> Run(AView view, Func<T> action)
static async Task<T> Run(AView view, Func<Task<T>> action)
{
await Task.WhenAll(
WaitForLayout(view),
Wait(() => view.Width > 0 && view.Height > 0));

var result = action();
return result;
return await action();
}
}

Expand Down Expand Up @@ -151,6 +165,15 @@ public static Bitmap AssertColorAtPoint(this Bitmap bitmap, AColor expectedColor
return bitmap;
}

public static Bitmap AssertColorAtCenter(this Drawable drawable, AColor expectedColor)
{
var bitmapDrawable = Assert.IsType<BitmapDrawable>(drawable);
var bitmap = bitmapDrawable.Bitmap;
Assert.NotNull(bitmap);

return bitmap!.AssertColorAtCenter(expectedColor);
}

public static Bitmap AssertColorAtCenter(this Bitmap bitmap, AColor expectedColor)
{
return bitmap.AssertColorAtPoint(expectedColor, bitmap.Width / 2, bitmap.Height / 2);
Expand Down
31 changes: 25 additions & 6 deletions src/TestUtils/src/DeviceTests/AssertionExtensions.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using CoreAnimation;
using CoreGraphics;
using Foundation;
using Microsoft.Maui.Platform;
using ObjCRuntime;
using UIKit;
using Xunit;
using Xunit.Sdk;
Expand All @@ -28,11 +26,32 @@ public static string CreateColorError(this UIImage bitmap, string message)
return $"{message}. This is what it looked like:<img>{imageAsString}</img>";
}

// TODO: attach this view to the UI if anything breaks
public static Task AttachAndRun(this UIView view, Action action)
public static Task AttachAndRun(this UIView view, Action action) =>
view.AttachAndRun(() =>
{
action();
return Task.FromResult(true);
});

public static Task<T> AttachAndRun<T>(this UIView view, Func<T> action) =>
view.AttachAndRun(() =>
{
var result = action();
return Task.FromResult(result);
});

public static Task AttachAndRun(this UIView view, Func<Task> action) =>
view.AttachAndRun(async () =>
{
await action();
return true;
});

// TODO: Actually attach this view to the UI if anything breaks.
public static Task<T> AttachAndRun<T>(this UIView view, Func<Task<T>> action)
{
action();
return Task.CompletedTask;
var result = action();
return result;
}

public static Task<UIImage> ToUIImage(this UIView view)
Expand Down