From 3550482e816da95c92aae6117243bc4e8bd0f2dd Mon Sep 17 00:00:00 2001 From: "mahmut.taskiran" Date: Wed, 18 Sep 2024 16:32:18 +0400 Subject: [PATCH 1/5] Add a check for the surface if it is valid --- .../plugin/platform/PlatformViewWrapper.java | 6 +++ .../platform/PlatformViewWrapperTest.java | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java b/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java index 1b202c14cb535..1bce76fd09bbe 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformViewWrapper.java @@ -165,7 +165,13 @@ public void draw(Canvas canvas) { Log.e(TAG, "Platform view cannot be composed without a RenderTarget."); return; } + final Surface targetSurface = renderTarget.getSurface(); + if (!targetSurface.isValid()) { + Log.e(TAG, "Platform view cannot be composed without a valid RenderTarget surface."); + return; + } + final Canvas targetCanvas = targetSurface.lockHardwareCanvas(); if (targetCanvas == null) { // Cannot render right now. diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java index 64fd84f93743b..eb5431e4aceda 100644 --- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java +++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java @@ -15,6 +15,7 @@ import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; +import android.view.Surface; import android.view.View; import android.view.View.OnFocusChangeListener; import android.view.ViewGroup; @@ -23,6 +24,7 @@ import android.widget.FrameLayout; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; +import io.flutter.embedding.engine.renderer.FlutterRenderer; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; @@ -63,6 +65,41 @@ public void onDraw(Canvas canvas) { verify(canvas, times(1)).drawColor(Color.RED); } + @Test + public void draw_withoutValidSurface() { + FlutterRenderer.debugDisableSurfaceClear = true; + final Surface surface = mock(Surface.class); + when(surface.isValid()).thenReturn(false); + final PlatformViewRenderTarget renderTarget = mock(PlatformViewRenderTarget.class); + when(renderTarget.getSurface()).thenReturn(surface); + + final PlatformViewWrapper wrapper = new PlatformViewWrapper(ctx, renderTarget); + // Test. + final Canvas canvas = mock(Canvas.class); + wrapper.draw(canvas); + + // Verify. + verify(canvas, times(0)).drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR); + } + + @Test + public void draw_withValidSurface() { + FlutterRenderer.debugDisableSurfaceClear = true; + final Canvas canvas = mock(Canvas.class); + final Surface surface = mock(Surface.class); + when(surface.isValid()).thenReturn(true); + final PlatformViewRenderTarget renderTarget = mock(PlatformViewRenderTarget.class); + when(renderTarget.getSurface()).thenReturn(surface); + when(surface.lockHardwareCanvas()).thenReturn(canvas); + final PlatformViewWrapper wrapper = new PlatformViewWrapper(ctx, renderTarget); + + // Test. + wrapper.draw(canvas); + + // Verify. + verify(canvas, times(1)).drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR); + } + @Test public void focusChangeListener_hasFocus() { final ViewTreeObserver viewTreeObserver = mock(ViewTreeObserver.class); From 575c8da9867010f3a2fd5f4fc97eff56533cb1cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mahmut=20Ta=C5=9Fk=C4=B1ran?= Date: Tue, 24 Sep 2024 21:39:59 +0400 Subject: [PATCH 2/5] Update shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java Delete comments Co-authored-by: Jonah Williams --- .../test/io/flutter/plugin/platform/PlatformViewWrapperTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java index eb5431e4aceda..fa1c8e8fac373 100644 --- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java +++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java @@ -74,7 +74,6 @@ public void draw_withoutValidSurface() { when(renderTarget.getSurface()).thenReturn(surface); final PlatformViewWrapper wrapper = new PlatformViewWrapper(ctx, renderTarget); - // Test. final Canvas canvas = mock(Canvas.class); wrapper.draw(canvas); From 51438d730447ab8f04d22a70ec363a3b6a26d24d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mahmut=20Ta=C5=9Fk=C4=B1ran?= Date: Tue, 24 Sep 2024 21:40:18 +0400 Subject: [PATCH 3/5] Delete comments Co-authored-by: Jonah Williams --- .../test/io/flutter/plugin/platform/PlatformViewWrapperTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java index fa1c8e8fac373..4a9aa6149f6e5 100644 --- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java +++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java @@ -77,7 +77,6 @@ public void draw_withoutValidSurface() { final Canvas canvas = mock(Canvas.class); wrapper.draw(canvas); - // Verify. verify(canvas, times(0)).drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR); } From 2d26565ce1187c17f4f2ace9e7be5d3c6853e9c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mahmut=20Ta=C5=9Fk=C4=B1ran?= Date: Tue, 24 Sep 2024 21:40:29 +0400 Subject: [PATCH 4/5] Update shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java Co-authored-by: Jonah Williams --- .../test/io/flutter/plugin/platform/PlatformViewWrapperTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java index 4a9aa6149f6e5..f782d26f02d3c 100644 --- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java +++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java @@ -91,7 +91,6 @@ public void draw_withValidSurface() { when(surface.lockHardwareCanvas()).thenReturn(canvas); final PlatformViewWrapper wrapper = new PlatformViewWrapper(ctx, renderTarget); - // Test. wrapper.draw(canvas); // Verify. From af585cffcd4c1de848ace685e2399130c55136a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mahmut=20Ta=C5=9Fk=C4=B1ran?= Date: Tue, 24 Sep 2024 21:40:34 +0400 Subject: [PATCH 5/5] Update shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java Co-authored-by: Jonah Williams --- .../test/io/flutter/plugin/platform/PlatformViewWrapperTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java index f782d26f02d3c..11c3094f982e7 100644 --- a/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java +++ b/shell/platform/android/test/io/flutter/plugin/platform/PlatformViewWrapperTest.java @@ -93,7 +93,6 @@ public void draw_withValidSurface() { wrapper.draw(canvas); - // Verify. verify(canvas, times(1)).drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR); }