mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Add a check for the surface if it is valid (flutter/engine#55277)
Fixes an issue where the Surface is not valid and the `draw` method is crashing. https://github.com/flutter/flutter/issues/155018 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
This commit is contained in:
parent
7242297f80
commit
92cc1aac8a
@ -165,7 +165,13 @@ public class PlatformViewWrapper extends FrameLayout {
|
||||
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.
|
||||
|
||||
@ -15,6 +15,7 @@ import android.annotation.TargetApi;
|
||||
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.view.accessibility.AccessibilityEvent;
|
||||
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,37 @@ public class PlatformViewWrapperTest {
|
||||
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);
|
||||
final Canvas canvas = mock(Canvas.class);
|
||||
wrapper.draw(canvas);
|
||||
|
||||
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);
|
||||
|
||||
wrapper.draw(canvas);
|
||||
|
||||
verify(canvas, times(1)).drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void focusChangeListener_hasFocus() {
|
||||
final ViewTreeObserver viewTreeObserver = mock(ViewTreeObserver.class);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user