Back to Repositories

Testing ByteArrayUploadDataProvider Operations in SmartTube

This test suite validates the ByteArrayUploadDataProvider class in ExoPlayer’s Cronet extension, focusing on data upload functionality and buffer handling. The tests ensure proper implementation of data reading, length calculation, and rewind operations for byte array uploads.

Test Coverage Overview

The test suite provides comprehensive coverage of ByteArrayUploadDataProvider functionality:

  • Length verification of upload data
  • Full buffer reading operations
  • Partial buffer reading with multiple iterations
  • Rewind functionality and data consistency
  • Edge cases in buffer management

Implementation Analysis

The testing approach utilizes JUnit4 with AndroidJUnit4 runner, implementing Mockito for dependency isolation. The tests follow AAA (Arrange-Act-Assert) pattern with clear setup methods and mock interactions. ByteBuffer operations are thoroughly validated using both full and partial read scenarios.

Technical Details

Testing infrastructure includes:

  • JUnit4 testing framework
  • Mockito for mocking UploadDataSink
  • AndroidJUnit4 test runner
  • Truth assertion library for readable assertions
  • ByteBuffer for memory management

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test initialization using @Before
  • Clear test method naming conventions
  • Isolated test cases with specific assertions
  • Mock verification for interaction testing
  • Efficient test data management with byte arrays

yuliskov/smarttube

exoplayer-amzn-2.10.6/extensions/cronet/src/test/java/com/google/android/exoplayer2/ext/cronet/ByteArrayUploadDataProviderTest.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.ext.cronet;

import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.chromium.net.UploadDataSink;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

/** Tests for {@link ByteArrayUploadDataProvider}. */
@RunWith(AndroidJUnit4.class)
public final class ByteArrayUploadDataProviderTest {

  private static final byte[] TEST_DATA = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

  @Mock private UploadDataSink mockUploadDataSink;
  private ByteBuffer byteBuffer;
  private ByteArrayUploadDataProvider byteArrayUploadDataProvider;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    byteBuffer = ByteBuffer.allocate(TEST_DATA.length);
    byteArrayUploadDataProvider = new ByteArrayUploadDataProvider(TEST_DATA);
  }

  @Test
  public void testGetLength() {
    assertThat(byteArrayUploadDataProvider.getLength()).isEqualTo(TEST_DATA.length);
  }

  @Test
  public void testReadFullBuffer() throws IOException {
    byteArrayUploadDataProvider.read(mockUploadDataSink, byteBuffer);
    assertThat(byteBuffer.array()).isEqualTo(TEST_DATA);
  }

  @Test
  public void testReadPartialBuffer() throws IOException {
    byte[] firstHalf = Arrays.copyOf(TEST_DATA, TEST_DATA.length / 2);
    byte[] secondHalf = Arrays.copyOfRange(TEST_DATA, TEST_DATA.length / 2, TEST_DATA.length);
    byteBuffer = ByteBuffer.allocate(TEST_DATA.length / 2);
    // Read half of the data.
    byteArrayUploadDataProvider.read(mockUploadDataSink, byteBuffer);
    assertThat(byteBuffer.array()).isEqualTo(firstHalf);

    // Read the second half of the data.
    byteBuffer.rewind();
    byteArrayUploadDataProvider.read(mockUploadDataSink, byteBuffer);
    assertThat(byteBuffer.array()).isEqualTo(secondHalf);
    verify(mockUploadDataSink, times(2)).onReadSucceeded(false);
  }

  @Test
  public void testRewind() throws IOException {
    // Read all the data.
    byteArrayUploadDataProvider.read(mockUploadDataSink, byteBuffer);
    assertThat(byteBuffer.array()).isEqualTo(TEST_DATA);

    // Rewind and make sure it can be read again.
    byteBuffer.clear();
    byteArrayUploadDataProvider.rewind(mockUploadDataSink);
    byteArrayUploadDataProvider.read(mockUploadDataSink, byteBuffer);
    assertThat(byteBuffer.array()).isEqualTo(TEST_DATA);
    verify(mockUploadDataSink).onRewindSucceeded();
  }
}