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

Add a check for the surface if it is valid #55277

Merged
merged 6 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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.
mahmuttaskiran marked this conversation as resolved.
Show resolved Hide resolved
final Canvas canvas = mock(Canvas.class);
wrapper.draw(canvas);

// Verify.
mahmuttaskiran marked this conversation as resolved.
Show resolved Hide resolved
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.
mahmuttaskiran marked this conversation as resolved.
Show resolved Hide resolved
wrapper.draw(canvas);

// Verify.
mahmuttaskiran marked this conversation as resolved.
Show resolved Hide resolved
verify(canvas, times(1)).drawColor(Color.TRANSPARENT, android.graphics.PorterDuff.Mode.CLEAR);
}

@Test
public void focusChangeListener_hasFocus() {
final ViewTreeObserver viewTreeObserver = mock(ViewTreeObserver.class);
Expand Down