Back to Repositories

Validating SinglePeriodTimeline Management in SmartTube

This test suite validates the functionality of SinglePeriodTimeline in ExoPlayer, focusing on timeline management and period position calculations for media playback. The tests ensure proper handling of dynamic windows, duration settings, and tag management.

Test Coverage Overview

The test suite provides comprehensive coverage of SinglePeriodTimeline functionality, including:

  • Dynamic window handling with known and unknown durations
  • Period position calculations and validations
  • Tag management and window property verification
  • Period index retrieval and validation

Implementation Analysis

The testing approach utilizes JUnit4 with AndroidJUnit4 runner for Android-specific testing context. The implementation employs setUp methods for test initialization and uses Truth assertions for validation. Tests are structured to verify both normal operations and edge cases of timeline management.

Technical Details

Testing tools and configuration include:

  • JUnit4 testing framework
  • AndroidJUnit4 test runner
  • Truth assertion library
  • Window and Period objects for timeline manipulation
  • C.TIME_UNSET constant for duration handling

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Clear test method naming convention
  • Proper test setup and initialization
  • Isolated test cases for specific functionality
  • Comprehensive edge case coverage
  • Effective use of assertion libraries

yuliskov/smarttube

exoplayer-amzn-2.10.6/library/core/src/test/java/com/google/android/exoplayer2/source/SinglePeriodTimelineTest.java

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

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

import android.util.Pair;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Timeline.Period;
import com.google.android.exoplayer2.Timeline.Window;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

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

  private Window window;
  private Period period;

  @Before
  public void setUp() throws Exception {
    window = new Window();
    period = new Period();
  }

  @Test
  public void testGetPeriodPositionDynamicWindowUnknownDuration() {
    SinglePeriodTimeline timeline = new SinglePeriodTimeline(C.TIME_UNSET, false, true);
    // Should return null with any positive position projection.
    Pair<Object, Long> position = timeline.getPeriodPosition(window, period, 0, C.TIME_UNSET, 1);
    assertThat(position).isNull();
    // Should return (0, 0) without a position projection.
    position = timeline.getPeriodPosition(window, period, 0, C.TIME_UNSET, 0);
    assertThat(position.first).isEqualTo(timeline.getUidOfPeriod(0));
    assertThat(position.second).isEqualTo(0);
  }

  @Test
  public void testGetPeriodPositionDynamicWindowKnownDuration() {
    long windowDurationUs = 1000;
    SinglePeriodTimeline timeline =
        new SinglePeriodTimeline(
            windowDurationUs,
            windowDurationUs,
            /* windowPositionInPeriodUs= */ 0,
            /* windowDefaultStartPositionUs= */ 0,
            /* isSeekable= */ false,
            /* isDynamic= */ true,
            /* tag= */ null);
    // Should return null with a positive position projection beyond window duration.
    Pair<Object, Long> position =
        timeline.getPeriodPosition(window, period, 0, C.TIME_UNSET, windowDurationUs + 1);
    assertThat(position).isNull();
    // Should return (0, duration) with a projection equal to window duration.
    position = timeline.getPeriodPosition(window, period, 0, C.TIME_UNSET, windowDurationUs);
    assertThat(position.first).isEqualTo(timeline.getUidOfPeriod(0));
    assertThat(position.second).isEqualTo(windowDurationUs);
    // Should return (0, 0) without a position projection.
    position = timeline.getPeriodPosition(window, period, 0, C.TIME_UNSET, 0);
    assertThat(position.first).isEqualTo(timeline.getUidOfPeriod(0));
    assertThat(position.second).isEqualTo(0);
  }

  @Test
  public void setNullTag_returnsNullTag_butUsesDefaultUid() {
    SinglePeriodTimeline timeline =
        new SinglePeriodTimeline(
            /* durationUs= */ C.TIME_UNSET,
            /* isSeekable= */ false,
            /* isDynamic= */ false,
            /* tag= */ null);

    assertThat(timeline.getWindow(/* windowIndex= */ 0, window, /* setTag= */ false).tag).isNull();
    assertThat(timeline.getWindow(/* windowIndex= */ 0, window, /* setTag= */ true).tag).isNull();
    assertThat(timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ false).id).isNull();
    assertThat(timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ true).id).isNull();
    assertThat(timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ false).uid).isNull();
    assertThat(timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ true).uid)
        .isNotNull();
  }

  @Test
  public void setTag_isUsedForWindowTag() {
    Object tag = new Object();
    SinglePeriodTimeline timeline =
        new SinglePeriodTimeline(
            /* durationUs= */ C.TIME_UNSET, /* isSeekable= */ false, /* isDynamic= */ false, tag);

    assertThat(timeline.getWindow(/* windowIndex= */ 0, window, /* setTag= */ false).tag).isNull();
    assertThat(timeline.getWindow(/* windowIndex= */ 0, window, /* setTag= */ true).tag)
        .isEqualTo(tag);
  }

  @Test
  public void getIndexOfPeriod_returnsPeriod() {
    SinglePeriodTimeline timeline =
        new SinglePeriodTimeline(
            /* durationUs= */ C.TIME_UNSET,
            /* isSeekable= */ false,
            /* isDynamic= */ false,
            /* tag= */ null);
    Object uid = timeline.getPeriod(/* periodIndex= */ 0, period, /* setIds= */ true).uid;

    assertThat(timeline.getIndexOfPeriod(uid)).isEqualTo(0);
    assertThat(timeline.getIndexOfPeriod(/* uid= */ null)).isEqualTo(C.INDEX_UNSET);
    assertThat(timeline.getIndexOfPeriod(/* uid= */ new Object())).isEqualTo(C.INDEX_UNSET);
  }
}