/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.media3.transformer.mh;

import static com.google.common.truth.Truth.assertThat;

import android.content.Context;
import androidx.media3.common.MimeTypes;
import androidx.media3.transformer.AndroidTestUtil;
import androidx.media3.transformer.TransformationRequest;
import androidx.media3.transformer.TransformationTestResult;
import androidx.media3.transformer.Transformer;
import androidx.media3.transformer.TransformerAndroidTestRunner;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Checks transcoding quality. */
@RunWith(AndroidJUnit4.class)
public final class TranscodeQualityTest {
  @Test
  public void transformWithDecodeEncode_ssimIsGreaterThan90Percent() throws Exception {
    Context context = ApplicationProvider.getApplicationContext();
    Transformer transformer =
        new Transformer.Builder(context)
            .setTransformationRequest(
                new TransformationRequest.Builder().setVideoMimeType(MimeTypes.VIDEO_H264).build())
            .setEncoderFactory(AndroidTestUtil.FORCE_ENCODE_ENCODER_FACTORY)
            .setRemoveAudio(true)
            .build();

    TransformationTestResult result =
        new TransformerAndroidTestRunner.Builder(context, transformer)
            .setCalculateSsim(true)
            .build()
            .run(
                /* testId= */ "transformWithDecodeEncode_ssim",
                AndroidTestUtil.MP4_ASSET_URI_STRING);

    assertThat(result.ssim).isGreaterThan(0.90);
  }

  @Test
  public void transcodeAvcToHevc_ssimIsGreaterThan90Percent() throws Exception {
    Context context = ApplicationProvider.getApplicationContext();
    String testId = "transcodeAvcToHevc_ssim";

    if (AndroidTestUtil.skipAndLogIfInsufficientCodecSupport(
        context,
        testId,
        /* decodingFormat= */ AndroidTestUtil.MP4_ASSET_FORMAT,
        /* encodingFormat= */ AndroidTestUtil.MP4_ASSET_FORMAT
            .buildUpon()
            .setSampleMimeType(MimeTypes.VIDEO_H265)
            .build())) {
      return;
    }

    Transformer transformer =
        new Transformer.Builder(context)
            .setTransformationRequest(
                new TransformationRequest.Builder().setVideoMimeType(MimeTypes.VIDEO_H265).build())
            .setRemoveAudio(true)
            .build();

    TransformationTestResult result =
        new TransformerAndroidTestRunner.Builder(context, transformer)
            .setCalculateSsim(true)
            .build()
            .run(testId, AndroidTestUtil.MP4_ASSET_URI_STRING);

    assertThat(result.ssim).isGreaterThan(0.90);
  }

  @Test
  public void transcodeAvcToAvc360p_ssimIsGreaterThan90Percent() throws Exception {
    Context context = ApplicationProvider.getApplicationContext();
    String testId = "transcodeAvcToAvc360p_ssim";

    // Note: We never skip this test as the input and output formats should be within CDD
    // requirements on all supported API versions.

    Transformer transformer =
        new Transformer.Builder(context)
            .setTransformationRequest(
                new TransformationRequest.Builder().setVideoMimeType(MimeTypes.VIDEO_H264).build())
            .setEncoderFactory(AndroidTestUtil.FORCE_ENCODE_ENCODER_FACTORY)
            .setRemoveAudio(true)
            .build();

    TransformationTestResult result =
        new TransformerAndroidTestRunner.Builder(context, transformer)
            .setCalculateSsim(true)
            .build()
            .run(testId, AndroidTestUtil.MP4_ASSET_WITH_INCREASING_TIMESTAMPS_360P_15S_URI_STRING);

    assertThat(result.ssim).isGreaterThan(0.90);
  }
}

@@ -1493,24 +1493,326 @@
 nContext();%0A
+    String testId = %22transformWithDecodeEncode_ssim%22;%0A%0A    if (AndroidTestUtil.skipAndLogIfInsufficientCodecSupport(%0A        context,%0A        testId,%0A        /* decodingFormat= */ AndroidTestUtil.MP4_ASSET_FORMAT,%0A        /* encodingFormat= */ AndroidTestUtil.MP4_ASSET_FORMAT)) %7B%0A      return;%0A    %7D%0A%0A
     Transfor
@@ -2330,88 +2330,15 @@
 run(
-%0A                /* testId= */ %22transformWithDecodeEncode_ssim%22,%0A               
+testId,
  And

Add device capability based test skipping.