Back to Repositories

Testing Media Extractor Factory Implementation in SmartTube

This test suite validates the DefaultExtractorsFactory functionality in ExoPlayer’s core library, ensuring proper initialization and creation of media format extractors. It verifies the factory’s ability to instantiate the correct set of extractors for various media formats.

Test Coverage Overview

The test coverage focuses on verifying the DefaultExtractorsFactory’s ability to create the complete set of supported media extractors. Key functionality includes:

  • Validation of all expected extractor types
  • Verification of no duplicate extractors
  • Confirmation of exact matching between created and expected extractor classes

Implementation Analysis

The testing approach uses JUnit with AndroidJUnit4 runner for Android-specific testing context. The implementation employs Truth assertions for precise verification of collection contents and uniqueness checks. The test creates a factory instance and validates its output against a predefined set of expected extractor classes.

Technical Details

Testing tools and configuration include:

  • JUnit test framework
  • AndroidJUnit4 test runner
  • Google Truth assertion library
  • ArrayList for dynamic collection handling
  • Class comparison for type verification

Best Practices Demonstrated

The test demonstrates several quality testing practices:

  • Single responsibility principle in test method design
  • Clear test method naming convention
  • Comprehensive verification of both content and uniqueness
  • Use of appropriate assertion libraries for collection comparison
  • Well-structured test organization with setup and verification phases

yuliskov/smarttube

exoplayer-amzn-2.10.6/library/core/src/test/java/com/google/android/exoplayer2/extractor/DefaultExtractorsFactoryTest.java

            
/*
 * Copyright (C) 2016 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.extractor;

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

import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.extractor.amr.AmrExtractor;
import com.google.android.exoplayer2.extractor.flv.FlvExtractor;
import com.google.android.exoplayer2.extractor.mkv.MatroskaExtractor;
import com.google.android.exoplayer2.extractor.mp3.Mp3Extractor;
import com.google.android.exoplayer2.extractor.mp4.FragmentedMp4Extractor;
import com.google.android.exoplayer2.extractor.mp4.Mp4Extractor;
import com.google.android.exoplayer2.extractor.ogg.OggExtractor;
import com.google.android.exoplayer2.extractor.ts.Ac3Extractor;
import com.google.android.exoplayer2.extractor.ts.Ac4Extractor;
import com.google.android.exoplayer2.extractor.ts.AdtsExtractor;
import com.google.android.exoplayer2.extractor.ts.PsExtractor;
import com.google.android.exoplayer2.extractor.ts.TsExtractor;
import com.google.android.exoplayer2.extractor.wav.WavExtractor;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Unit test for {@link DefaultExtractorsFactory}. */
@RunWith(AndroidJUnit4.class)
public final class DefaultExtractorsFactoryTest {

  @Test
  public void testCreateExtractors_returnExpectedClasses() {
    DefaultExtractorsFactory defaultExtractorsFactory = new DefaultExtractorsFactory();

    Extractor[] extractors = defaultExtractorsFactory.createExtractors();
    List<Class> listCreatedExtractorClasses = new ArrayList<>();
    for (Extractor extractor : extractors) {
      listCreatedExtractorClasses.add(extractor.getClass());
    }

    Class[] expectedExtractorClassses =
        new Class[] {
          MatroskaExtractor.class,
          FragmentedMp4Extractor.class,
          Mp4Extractor.class,
          Mp3Extractor.class,
          AdtsExtractor.class,
          Ac3Extractor.class,
          TsExtractor.class,
          FlvExtractor.class,
          OggExtractor.class,
          PsExtractor.class,
          WavExtractor.class,
          AmrExtractor.class,
          Ac4Extractor.class
        };

    assertThat(listCreatedExtractorClasses).containsNoDuplicates();
    assertThat(listCreatedExtractorClasses).containsExactlyElementsIn(expectedExtractorClassses);
  }
}