Repository: flutter/flutter
File: engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java (Java)

Code before:
    try {
      FlutterRenderer.debugForceSurfaceProducerGlTextures = true;
      FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
      TextureRegistry.SurfaceProducer producer = flutterRenderer.createSurfaceProducer();

      assertTrue(producer.handlesCropAndRotation());
    } finally {
      FlutterRenderer.debugForceSurfaceProducerGlTextures = false;
    }
  }

  @Test
  @Config(sdk = 29)
  public void createSurfaceProducer_usesSurfaceTextureWhenHardwareBufferDefect() {
    // Setup: Simulate a Huawei device on API 29 where HardwareBuffer has known defects.
    // See: https://github.com/flutter/flutter/issues/166481
    ShadowBuild.setManufacturer("HUAWEI");

    // Execute the behavior under test.
    FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
    TextureRegistry.SurfaceProducer producer = flutterRenderer.createSurfaceProducer();

    // Verify: Should return SurfaceTextureSurfaceProducer instead of ImageReaderSurfaceProducer.
    assertTrue(
        "Expected SurfaceTextureSurfaceProducer on Huawei API <= 29 due to HardwareBuffer defect causing video playback failures",
        producer instanceof FlutterRenderer.SurfaceTextureSurfaceProducer);
  }

  @Test
  public void ImageReaderSurfaceProducerDoesNotCropOrRotate() {
    FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
    TextureRegistry.SurfaceProducer producer = flutterRenderer.createSurfaceProducer();

    assertFalse(producer.handlesCropAndRotation());
  }

  @Test
  @SuppressWarnings({"deprecation", "removal"})
  public void ImageReaderSurfaceProducerIsCleanedUpOnTrimMemory() {
    FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
    TextureRegistry.SurfaceProducer producer =
        flutterRenderer.createSurfaceProducer(TextureRegistry.SurfaceLifecycle.resetInBackground);

    // Create and set a mock callback.
    TextureRegistry.SurfaceProducer.Callback callback =
        mock(TextureRegistry.SurfaceProducer.Callback.class);
    producer.setCallback(callback);

    // Trim memory.
    flutterRenderer.onTrimMemory(TRIM_MEMORY_BACKGROUND);


Review comment (performance):
Looks like you're missing an import

```sh
/b/s/w/ir/cache/builder/engine/src/flutter/shell/platform/android/test/io/flutter/embedding/engine/renderer/FlutterRendererTest.java:842: error: cannot find symbol
          producer instanceof FlutterRenderer.SurfaceTextureSurfaceProducer);
                                             ^
    symbol:   class SurfaceTextureSurfaceProducer
    location: class FlutterRenderer
  1 error
```

Code after:
    try {
      FlutterRenderer.debugForceSurfaceProducerGlTextures = true;
      FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
      TextureRegistry.SurfaceProducer producer = flutterRenderer.createSurfaceProducer();

      assertTrue(producer.handlesCropAndRotation());
    } finally {
      FlutterRenderer.debugForceSurfaceProducerGlTextures = false;
    }
  }

  @Test
  @Config(sdk = 29)
  public void createSurfaceProducer_usesSurfaceTextureWhenHardwareBufferDefect() {
    // Setup: Simulate a Huawei device on API 29 where HardwareBuffer has known defects.
    // See: https://github.com/flutter/flutter/issues/166481
    ShadowBuild.setManufacturer("HUAWEI");

    // Execute the behavior under test.
    FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
    TextureRegistry.SurfaceProducer producer = flutterRenderer.createSurfaceProducer();

    // Verify: Should return SurfaceTextureSurfaceProducer instead of ImageReaderSurfaceProducer.
    assertTrue(
        "Expected SurfaceTextureSurfaceProducer on Huawei API <= 29 due to HardwareBuffer defect causing video playback failures",
        producer instanceof SurfaceTextureSurfaceProducer);
  }

  @Test
  public void ImageReaderSurfaceProducerDoesNotCropOrRotate() {
    FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
    TextureRegistry.SurfaceProducer producer = flutterRenderer.createSurfaceProducer();

    assertFalse(producer.handlesCropAndRotation());
  }

  @Test
  @SuppressWarnings({"deprecation", "removal"})
  public void ImageReaderSurfaceProducerIsCleanedUpOnTrimMemory() {
    FlutterRenderer flutterRenderer = engineRule.getFlutterEngine().getRenderer();
    TextureRegistry.SurfaceProducer producer =
        flutterRenderer.createSurfaceProducer(TextureRegistry.SurfaceLifecycle.resetInBackground);

    // Create and set a mock callback.
    TextureRegistry.SurfaceProducer.Callback callback =
        mock(TextureRegistry.SurfaceProducer.Callback.class);
    producer.setCallback(callback);

    // Trim memory.
    flutterRenderer.onTrimMemory(TRIM_MEMORY_BACKGROUND);