Back to Repositories

Validating ErrorInfo Parcelable Implementation in NewPipe

This test suite validates the error handling functionality in NewPipe’s Android application, specifically focusing on the ErrorInfo class’s parcelable implementation. The tests ensure proper error state preservation and restoration when errors are passed between Android components.

Test Coverage Overview

The test suite provides comprehensive coverage of the ErrorInfo class’s Parcelable implementation, focusing on error state serialization and deserialization.

  • Tests parcelable object creation and restoration
  • Validates stack trace preservation
  • Verifies user action preservation
  • Ensures service information retention
  • Confirms error message integrity

Implementation Analysis

The testing approach utilizes Android’s Parcel system to verify object serialization and deserialization. The implementation employs JUnit4 with AndroidJUnit4 runner for Android-specific testing capabilities.

The test creates an ErrorInfo instance with a ParsingException, writes it to a Parcel, then reads it back to verify state preservation across the parcelable operation.

Technical Details

  • Testing Framework: JUnit4 with AndroidJUnit4 runner
  • Android Components: Parcel system
  • Test Annotations: @LargeTest, @RunWith
  • Assert Methods: assertEquals, assertTrue
  • Error Types: ParsingException
  • Service Integration: YouTube service ID validation

Best Practices Demonstrated

The test demonstrates several testing best practices in Android development.

  • Proper resource cleanup with parcel.recycle()
  • Comprehensive state verification
  • Clear test method naming
  • Isolated test scenarios
  • Effective use of Android testing annotations

teamnewpipe/newpipe

app/src/androidTest/java/org/schabi/newpipe/error/ErrorInfoTest.java

            
package org.schabi.newpipe.error;

import android.os.Parcel;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.schabi.newpipe.R;
import org.schabi.newpipe.extractor.ServiceList;
import org.schabi.newpipe.extractor.exceptions.ParsingException;

import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Instrumented tests for {@link ErrorInfo}.
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ErrorInfoTest {

    @Test
    public void errorInfoTestParcelable() {
        final ErrorInfo info = new ErrorInfo(new ParsingException("Hello"),
                UserAction.USER_REPORT, "request", ServiceList.YouTube.getServiceId());
        // Obtain a Parcel object and write the parcelable object to it:
        final Parcel parcel = Parcel.obtain();
        info.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);
        final ErrorInfo infoFromParcel = (ErrorInfo) ErrorInfo.CREATOR.createFromParcel(parcel);

        assertTrue(Arrays.toString(infoFromParcel.getStackTraces())
                .contains(ErrorInfoTest.class.getSimpleName()));
        assertEquals(UserAction.USER_REPORT, infoFromParcel.getUserAction());
        assertEquals(ServiceList.YouTube.getServiceInfo().getName(),
                infoFromParcel.getServiceName());
        assertEquals("request", infoFromParcel.getRequest());
        assertEquals(R.string.parsing_error, infoFromParcel.getMessageStringId());

        parcel.recycle();
    }
}