ExoPlayerFactoryTest.java


Overview

`ExoPlayerFactoryTest.java` is a **JUnit4 test class** designed to validate the behavior and correctness of the Finite State Machine (FSM) player and the `StateFactory` used within the Tubitv media playback system. This file focuses primarily on:

The FSM player under test (`FsmPlayerImperial`) manages media playback and advertisement logic by transitioning between states such as `MoviePlayingState`, `MakingAdCallState`, `ReceiveAdState`, `AdPlayingState`, `VpaidState`, and others. This test class ensures that these transitions happen correctly in response to playback events and user interactions.


Detailed Class and Method Documentation

Class: ExoPlayerFactoryTest

**Package:** `com.tubitv.demo`

**Description:** Test suite for the FSM playback state factory and player FSM behavior. It ensures that `StateFactory` produces correct states and that the FSM transitions properly through typical ad and playback flows, including handling of VPAID ads and error scenarios.

**Annotations:**


Fields

Name

Type

Description

`stateFactory`

`StateFactory`

Factory used for creating FSM states.

`playerFsm`

`FsmPlayer`

The FSM player instance under test.


Method: setup()

**Signature:**

@Before
public void setup()

**Description:** Sets up the test environment before each test method runs. It:

**Usage Example:** Automatically invoked before each test case by JUnit.


Method: testCustomClass()

**Signature:**

@Test
public void testCustomClass()

**Description:** Tests whether the `StateFactory` correctly creates custom state subclasses instead of the default FSM states. It:

**Usage Example:**

State state = stateFactory.createState(MoviePlayingState.class);
assertThat(state instanceof TestMoviePlayingState, is(true));

Method: testFSMFlowWithVpaid()

**Signature:**

@Test
public void testFSMFlowWithVpaid()

**Description:** Simulates an FSM playback flow involving VPAID ads, verifying correct state transitions for inputs such as `INITIALIZE`, `MAKE_AD_CALL`, `AD_RECEIVED`, `SHOW_ADS`, `AD_CLICK`, `VPAID_MANIFEST`, `VPAID_FINISH`, `MOVIE_FINISH`, etc.

**Key Assertions:**

**Usage Example:**

playerFsm.transit(Input.INITIALIZE);
playerFsm.transit(Input.MAKE_AD_CALL);
assertTrue(playerFsm.getCurrentState() instanceof MakingAdCallState);

Method: testFSMFlowWithNoVpaid()

**Signature:**

@Test
public void testFSMFlowWithNoVpaid()

**Description:** Tests FSM transitions through multiple ad playback cycles (10 iterations) without preroll ads, including ad calls, ad receipt, ad playing, ad clicks, returning back to player, transitioning to VPAID states, and finally finishing the movie.

**Highlights:**


Method: testErrorFlow()

**Signature:**

@Test
public void testErrorFlow()

**Description:** Tests FSM behavior when unexpected or erroneous inputs occur out of normal sequence, ensuring FSM recovers gracefully and returns to the `MoviePlayingState`.

**Example:** After completing normal playback flow, sends inputs like `AD_CLICK`, `BACK_TO_PLAYER_FROM_VAST_AD`, `AD_FINISH`, and another `AD_CLICK`, then asserts FSM is back in `MoviePlayingState`.


Method: testWrongStateCreating()

**Signature:**

@Test(expected = IllegalStateException.class)
public void testWrongStateCreating()

**Description:** Verifies that creating a state instance for a class not extending `State` (e.g., `String.class`) throws an `IllegalStateException`, enforcing type safety in the `StateFactory`.


Method: testSingleton()

**Signature:**

@Test
public void testSingleton()

**Description:** Tests that `StateFactory` returns the same instance (singleton) for multiple requests of the same state class, ensuring state object reuse and efficient memory usage.


Inner Classes: Custom Test States

Custom subclasses of the FSM states designed to test override functionality in `StateFactory`. They do not add additional behavior but serve as distinct types for assertion.

Class Name

Extends

Purpose

`TestMoviePlayingState`

`MoviePlayingState`

Custom test substitute for movie playback state

`TestMakingAdCallState`

`MakingAdCallState`

Custom test substitute for making ad call state

`TestFinishState`

`FinishState`

Custom test substitute for finish state

`TestReceivedState`

`ReceiveAdState`

Custom test substitute for ad received state

`TestAdPlayingState`

`AdPlayingState`

Custom test substitute for ad playing state

`TestVastAdSandBox`

`VastAdInteractionSandBoxState`

Custom test substitute for vast ad interaction sandbox

`TestVpadState`

`VpaidState`

Custom test substitute for VPAID ad state

`TestFetchCuePointState`

`FetchCuePointState`

Custom test substitute for cue point fetching state

`TestMakingPrerollAdCallState`

`MakingPrerollAdCallState`

Custom test substitute for preroll ad call state


Important Implementation Details and Algorithms


Interaction With Other System Components

This file is an integral part of the testing suite that validates the FSM playback engine's robustness, which is central to the media playback system's operation within the Tubitv platform.


Usage Example

Typical usage in this test class involves:

  1. Creating or overriding FSM states in the StateFactory.

  2. Initializing the FSM player (playerFsm).

  3. Driving the FSM with input events using playerFsm.transit(Input.EVENT).

  4. Asserting the current FSM state after each input to verify correct transitions.

Example snippet:

playerFsm.transit(Input.INITIALIZE);
assertTrue(playerFsm.getCurrentState() instanceof MoviePlayingState);

playerFsm.transit(Input.MAKE_AD_CALL);
assertTrue(playerFsm.getCurrentState() instanceof MakingAdCallState);

playerFsm.transit(Input.AD_RECEIVED);
assertTrue(playerFsm.getCurrentState() instanceof ReceiveAdState);

Mermaid Class Diagram

The following diagram illustrates the key class structure within `ExoPlayerFactoryTest.java`, showing the main test class, its fields, methods, and inner custom test state classes that extend FSM states.

classDiagram
    class ExoPlayerFactoryTest {
        - StateFactory stateFactory
        - FsmPlayer playerFsm
        + void setup()
        + void testCustomClass()
        + void testFSMFlowWithVpaid()
        + void testFSMFlowWithNoVpaid()
        + void testErrorFlow()
        + void testWrongStateCreating()
        + void testSingleton()
    }

    %% Custom test state classes extending FSM states
    class TestMoviePlayingState {
    }
    class TestMakingAdCallState {
    }
    class TestFinishState {
    }
    class TestReceivedState {
    }
    class TestAdPlayingState {
    }
    class TestVastAdSandBox {
    }
    class TestVpadState {
    }
    class TestFetchCuePointState {
    }
    class TestMakingPrerollAdCallState {
    }

    %% Relationships
    ExoPlayerFactoryTest o-- TestMoviePlayingState : "extends MoviePlayingState"
    ExoPlayerFactoryTest o-- TestMakingAdCallState : "extends MakingAdCallState"
    ExoPlayerFactoryTest o-- TestFinishState : "extends FinishState"
    ExoPlayerFactoryTest o-- TestReceivedState : "extends ReceiveAdState"
    ExoPlayerFactoryTest o-- TestAdPlayingState : "extends AdPlayingState"
    ExoPlayerFactoryTest o-- TestVastAdSandBox : "extends VastAdInteractionSandBoxState"
    ExoPlayerFactoryTest o-- TestVpadState : "extends VpaidState"
    ExoPlayerFactoryTest o-- TestFetchCuePointState : "extends FetchCuePointState"
    ExoPlayerFactoryTest o-- TestMakingPrerollAdCallState : "extends MakingPrerollAdCallState"

Summary

`ExoPlayerFactoryTest.java` is a comprehensive unit test suite that validates:

This file plays a crucial role in ensuring the robustness and reliability of the media playback FSM system within the Tubitv platform, supporting smooth playback experiences and ad integration.


End of Documentation for ExoPlayerFactoryTest.java