From 1290d00a4f157786c63301b6fefb0fcae549a807 Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Tue, 21 Mar 2023 15:57:13 -0400 Subject: [PATCH 1/6] Fixed Android's StreamImageSourceService.LoadDrawableAsync() Transition back to the calling thread context (which should theoretically be the main thread). Fixes issue #14052 --- .../StreamImageSourceService.Android.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/src/ImageSources/StreamImageSourceService/StreamImageSourceService.Android.cs b/src/Core/src/ImageSources/StreamImageSourceService/StreamImageSourceService.Android.cs index d31045b4fd73..c42c1be75922 100644 --- a/src/Core/src/ImageSources/StreamImageSourceService/StreamImageSourceService.Android.cs +++ b/src/Core/src/ImageSources/StreamImageSourceService/StreamImageSourceService.Android.cs @@ -21,7 +21,7 @@ public partial class StreamImageSourceService Stream? stream = null; try { - stream = await streamImageSource.GetStreamAsync(cancellationToken).ConfigureAwait(false); + stream = await streamImageSource.GetStreamAsync(cancellationToken); var callback = new ImageLoaderCallback(); From 8574886cc9df5777913786cae8ddf8b2dc6a1605 Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Tue, 21 Mar 2023 15:58:16 -0400 Subject: [PATCH 2/6] Try harder in the Android backend to load images in the main (UI) thread --- .../microsoft/maui/glide/MauiCustomViewTarget.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/MauiCustomViewTarget.java b/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/MauiCustomViewTarget.java index 765afdb5060b..2a8a579e0df6 100644 --- a/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/MauiCustomViewTarget.java +++ b/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/MauiCustomViewTarget.java @@ -27,7 +27,7 @@ public MauiCustomViewTarget(@NonNull ImageView view, ImageLoaderCallback callbac @Override protected void onResourceCleared(@Nullable Drawable placeholder) { - this.view.setImageDrawable(placeholder); + post(() -> this.view.setImageDrawable(placeholder)); } @Override @@ -46,11 +46,13 @@ public void onResourceReady(@NonNull Drawable resource, @Nullable Transition { + // set the image + this.view.setImageDrawable(resource); - // trigger the callback out of this target - post(() -> callback.onComplete(true, resource, this::clear)); + // trigger the callback out of this target + callback.onComplete(true, resource, this::clear); + }); } private void post(Runnable runnable) { From e1eb4f901f443a876aaffc35a31f8abdcb5d9c5e Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Mon, 27 Mar 2023 13:36:53 -0400 Subject: [PATCH 3/6] Reverted the onResourceCleared() use of post() --- .../java/com/microsoft/maui/glide/MauiCustomViewTarget.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/MauiCustomViewTarget.java b/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/MauiCustomViewTarget.java index 2a8a579e0df6..1a1f8427f241 100644 --- a/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/MauiCustomViewTarget.java +++ b/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/MauiCustomViewTarget.java @@ -27,7 +27,7 @@ public MauiCustomViewTarget(@NonNull ImageView view, ImageLoaderCallback callbac @Override protected void onResourceCleared(@Nullable Drawable placeholder) { - post(() -> this.view.setImageDrawable(placeholder)); + this.view.setImageDrawable(placeholder); } @Override From 83580c624d23d3aa98508384ebb64f440badaf1d Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Thu, 4 May 2023 17:25:11 -0400 Subject: [PATCH 4/6] Added test case --- .../DeviceTests/Controls.DeviceTests.csproj | 3 +++ .../DeviceTests/Elements/Image/ImageTests.cs | 24 ++++++++++++++++++ .../Resources/Images/red-embedded.png | Bin 0 -> 310 bytes 3 files changed, 27 insertions(+) create mode 100644 src/Controls/tests/DeviceTests/Resources/Images/red-embedded.png diff --git a/src/Controls/tests/DeviceTests/Controls.DeviceTests.csproj b/src/Controls/tests/DeviceTests/Controls.DeviceTests.csproj index 5313b587c229..3b134ca8cb9a 100644 --- a/src/Controls/tests/DeviceTests/Controls.DeviceTests.csproj +++ b/src/Controls/tests/DeviceTests/Controls.DeviceTests.csproj @@ -19,6 +19,9 @@ + + + diff --git a/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.cs b/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.cs index ad021d8e89a3..ac3096293b89 100644 --- a/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.Maui.Controls; @@ -46,5 +47,28 @@ await InvokeOnMainThreadAsync(async () => await handler.ToPlatform().AssertContainsColor(Colors.Red, MauiContext); }); } + + [Fact] + public async Task ImageSetFromStreamRenders() + { + SetupBuilder(); + var layout = new VerticalStackLayout(); + + using var stream = GetType().Assembly.GetManifestResourceStream("red-embedded.png"); + + var image = new Image + { + Source = ImageSource.FromStream(() => stream) + }; + + layout.Add(image); + + await InvokeOnMainThreadAsync(async () => + { + var handler = CreateHandler(layout); + await image.Wait(); + await handler.ToPlatform().AssertContainsColor(Colors.Red, MauiContext); + }); + } } } \ No newline at end of file diff --git a/src/Controls/tests/DeviceTests/Resources/Images/red-embedded.png b/src/Controls/tests/DeviceTests/Resources/Images/red-embedded.png new file mode 100644 index 0000000000000000000000000000000000000000..3587e2727c39ea9e72ec19be09c1a8bceaf9097c GIT binary patch literal 310 zcmeAS@N?(olHy`uVBq!ia0vp^DIm!lvI6;>1s;*b3=AqSK$x*9nNFVdQ&MBb@0LN;1H~;_u literal 0 HcmV?d00001 From 09aa288c778d7e9f3c3e102ffb6a09086289e4ca Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Mon, 22 May 2023 10:48:58 -0400 Subject: [PATCH 5/6] Move the test to be Android-specific to avoid iOS failures. --- .../Elements/Image/ImageTests.Android.cs | 23 +++++++++++++++++++ .../DeviceTests/Elements/Image/ImageTests.cs | 23 ------------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.Android.cs b/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.Android.cs index f18eb2fd64e4..8d0a875dd3f4 100644 --- a/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.Android.cs +++ b/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.Android.cs @@ -37,6 +37,29 @@ await InvokeOnMainThreadAsync(async () => await bitmapDrawable1.Bitmap.AssertNotEqualAsync(bitmapDrawable2.Bitmap); }); } + + [Fact] + public async Task ImageSetFromStreamRenders() + { + SetupBuilder(); + var layout = new VerticalStackLayout(); + + using var stream = GetType().Assembly.GetManifestResourceStream("red-embedded.png"); + + var image = new Image + { + Source = ImageSource.FromStream(() => stream) + }; + + layout.Add(image); + + await InvokeOnMainThreadAsync(async () => + { + var handler = CreateHandler(layout); + await image.Wait(); + await handler.ToPlatform().AssertContainsColor(Colors.Red, MauiContext); + }); + } } // This subclass of memory stream is deliberately set up to trick Glide into using the cached image diff --git a/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.cs b/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.cs index ac3096293b89..cc2865011bab 100644 --- a/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.cs @@ -47,28 +47,5 @@ await InvokeOnMainThreadAsync(async () => await handler.ToPlatform().AssertContainsColor(Colors.Red, MauiContext); }); } - - [Fact] - public async Task ImageSetFromStreamRenders() - { - SetupBuilder(); - var layout = new VerticalStackLayout(); - - using var stream = GetType().Assembly.GetManifestResourceStream("red-embedded.png"); - - var image = new Image - { - Source = ImageSource.FromStream(() => stream) - }; - - layout.Add(image); - - await InvokeOnMainThreadAsync(async () => - { - var handler = CreateHandler(layout); - await image.Wait(); - await handler.ToPlatform().AssertContainsColor(Colors.Red, MauiContext); - }); - } } } \ No newline at end of file From 9bb80058fd4ee0f6154a594e8906e7d6a494f771 Mon Sep 17 00:00:00 2001 From: Jeffrey Stedfast Date: Mon, 22 May 2023 11:27:58 -0400 Subject: [PATCH 6/6] fixed the build --- .../tests/DeviceTests/Elements/Image/ImageTests.Android.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.Android.cs b/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.Android.cs index 8d0a875dd3f4..d7e98d037434 100644 --- a/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.Android.cs +++ b/src/Controls/tests/DeviceTests/Elements/Image/ImageTests.Android.cs @@ -6,6 +6,8 @@ using System.Threading.Tasks; using Android.Graphics.Drawables; using Microsoft.Maui.Controls; +using Microsoft.Maui.Graphics; +using Microsoft.Maui.Handlers; using Xunit; namespace Microsoft.Maui.DeviceTests