Back to Repositories

Testing AC3 Audio Frame Parsing in SmartTube

This test suite validates the Ac3Util functionality in ExoPlayer’s audio processing system, focusing on TrueHD syncframe parsing and audio sample count verification. The tests ensure accurate handling of both valid and invalid TrueHD audio frame headers.

Test Coverage Overview

The test suite provides targeted coverage of the Ac3Util class’s TrueHD syncframe parsing capabilities.

  • Tests parsing of valid TrueHD syncframe headers
  • Verifies handling of non-syncframe headers
  • Validates correct audio sample count extraction
  • Covers edge cases with predefined test data

Implementation Analysis

The implementation uses JUnit 4 with AndroidJUnit4 runner for Android-specific testing context. The tests employ a clear pattern of comparing expected values against parsed results using Truth assertions.

Test data is provided through hex-encoded byte arrays, representing real-world TrueHD frame headers for authentic testing scenarios.

Technical Details

  • JUnit 4 testing framework
  • AndroidJUnit4 test runner
  • Google Truth assertion library
  • Util.getBytesFromHexString for test data preparation
  • Constant test values for sample counts and headers

Best Practices Demonstrated

The test suite demonstrates excellent testing practices through clear test method naming, isolated test cases, and precise assertions.

  • Well-documented test constants
  • Separate test cases for positive and negative scenarios
  • Use of modern assertion library
  • Clear separation of test data and test logic

yuliskov/smarttube

exoplayer-amzn-2.10.6/library/core/src/test/java/com/google/android/exoplayer2/audio/Ac3UtilTest.java

            
/*
 * Copyright (C) 2018 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 com.google.android.exoplayer2.audio;

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

import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.util.Util;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Unit tests for {@link Ac3Util}. */
@RunWith(AndroidJUnit4.class)
public final class Ac3UtilTest {

  private static final int TRUEHD_SYNCFRAME_SAMPLE_COUNT = 40;
  private static final byte[] TRUEHD_SYNCFRAME_HEADER =
      Util.getBytesFromHexString("C07504D8F8726FBA0097C00FB7520000");
  private static final byte[] TRUEHD_NON_SYNCFRAME_HEADER =
      Util.getBytesFromHexString("A025048860224E6F6DEDB6D5B6DBAFE6");

  @Test
  public void testParseTrueHdSyncframeAudioSampleCount_nonSyncframe() {
    assertThat(Ac3Util.parseTrueHdSyncframeAudioSampleCount(TRUEHD_NON_SYNCFRAME_HEADER))
        .isEqualTo(0);
  }

  @Test
  public void testParseTrueHdSyncframeAudioSampleCount_syncframe() {
    assertThat(Ac3Util.parseTrueHdSyncframeAudioSampleCount(TRUEHD_SYNCFRAME_HEADER))
        .isEqualTo(TRUEHD_SYNCFRAME_SAMPLE_COUNT);
  }
}