/*
 * Copyright 2021 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;

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

import android.os.Bundle;
import android.os.RemoteException;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;

/** Unit tests for {@link PlaybackException}. */
@RunWith(AndroidJUnit4.class)
public class PlaybackExceptionTest {

  @Test
  public void roundTripViaBundle_yieldsEqualInstance() {
    PlaybackException before =
        new PlaybackException(
            /* message= */ "test",
            /* cause= */ new IOException(/* message= */ "io"),
            PlaybackException.ERROR_CODE_IO_FILE_NOT_FOUND);
    PlaybackException after = PlaybackException.CREATOR.fromBundle(before.toBundle());
    assertPlaybackExceptionsAreEqual(before, after);
  }

  // Backward compatibility tests.
  // The following tests prevent accidental modifications which break communication with older
  // ExoPlayer versions hosted in other processes.

  @Test
  public void bundle_producesExpectedException() {
    IOException expectedCause = new IOException("cause message");
    PlaybackException expectedException =
        new PlaybackException(
            "message",
            expectedCause,
            PlaybackException.ERROR_CODE_AUDIO_TRACK_INIT_FAILED,
            /* timestampMs= */ 1000);

    Bundle bundle = new Bundle();
    bundle.putInt("0", 5001); // Error code
    bundle.putLong("1", 1000); // Timestamp.
    bundle.putString("2", "message");
    bundle.putString("3", expectedCause.getClass().getName());
    bundle.putString("4", "cause message");

    assertPlaybackExceptionsAreEqual(
        expectedException, PlaybackException.CREATOR.fromBundle(bundle));
  }

  @Test
  public void exception_producesExpectedBundle() {
    IllegalStateException cause = new IllegalStateException("cause message");
    PlaybackException exception =
        new PlaybackException(
            "message",
            cause,
            PlaybackException.ERROR_CODE_DECODING_FAILED,
            /* timestampMs= */ 2000);

    Bundle bundle = exception.toBundle();
    assertThat(bundle.getInt("0")).isEqualTo(4002); // Error code.
    assertThat(bundle.getLong("1")).isEqualTo(2000); // Timestamp.
    assertThat(bundle.getString("2")).isEqualTo("message");
    assertThat(bundle.getString("3")).isEqualTo(cause.getClass().getName());
    assertThat(bundle.getString("4")).isEqualTo("cause message");
  }

  @Test
  public void bundleWithUnexpectedCause_producesRemoteExceptionCause() {
    RemoteException expectedCause = new RemoteException("cause message");
    PlaybackException expectedException =
        new PlaybackException(
            "message",
            expectedCause,
            PlaybackException.ERROR_CODE_AUDIO_TRACK_INIT_FAILED,
            /* timestampMs= */ 1000);

    Bundle bundle = new Bundle();
    bundle.putInt("0", 5001); // Error code
    bundle.putLong("1", 1000); // Timestamp.
    bundle.putString("2", "message");
    bundle.putString("3", "invalid cause class name");
    bundle.putString("4", "cause message");

    assertPlaybackExceptionsAreEqual(
        expectedException, PlaybackException.CREATOR.fromBundle(bundle));
  }

  // Internal methods.

  private static void assertPlaybackExceptionsAreEqual(PlaybackException a, PlaybackException b) {
    assertThat(a).hasMessageThat().isEqualTo(b.getMessage());
    assertThat(a.errorCode).isEqualTo(b.errorCode);
    assertThat(a.timestampMs).isEqualTo(b.timestampMs);
    assertThat(a.getCause().getClass()).isSameInstanceAs(b.getCause().getClass());
    assertThat(a).hasCauseThat().hasMessageThat().isEqualTo(b.getCause().getMessage());
  }
}

@@ -1430,18 +1430,23 @@
 nsAreEqu
-al
+ivalent
 (before,
@@ -2289,34 +2289,39 @@
 ExceptionsAreEqu
-al
+ivalent
 (%0A        expect
@@ -3788,18 +3788,23 @@
 nsAreEqu
-al
+ivalent
 (%0A      
@@ -3956,11 +3956,23 @@
 eEqu
-al(
+ivalent(%0A      
 Play

Use the term equivalent instead of equal in assertion