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:
Testing the creation of FSM states through the
StateFactory, including the ability to override default state implementations with custom test subclasses.Verifying FSM transitions across various playback and advertisement states triggered by inputs (
Inputenums), ensuring expected states are reached.Confirming singleton behavior of state instances created by the
StateFactory.Handling error conditions and invalid state creation.
Providing custom test states that extend the actual FSM playback states for testing override functionality.
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:**
@RunWith(JUnit4.class)— Specifies the JUnit4 test runner.
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:
Instantiates a
StateFactory.Overrides default state creation to use custom test subclasses (
TestMoviePlayingState,TestMakingAdCallState, etc.).Instantiates
playerFsmas a subclass ofFsmPlayerImperialwith overriddeninitializeState()returningMoviePlayingState.class.
**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:
Requests states from the factory for various FSM states.
Asserts that the created instances are of the custom test subclass types.
Tests ability to override state creation dynamically.
Verifies correct behavior for states both before and after overriding.
**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:**
After
MAKE_AD_CALL, FSM should be inMakingAdCallState.After
AD_RECEIVED, FSM should be inReceiveAdState.After
SHOW_ADS, FSM should be inAdPlayingState.After
AD_CLICK, FSM should be inVastAdInteractionSandBoxState.After
VPAID_MANIFEST, FSM should be inVpaidState.After
VPAID_FINISH, FSM should return toMoviePlayingState.After
MOVIE_FINISH, FSM should be inFinishState.
**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:**
Repeatedly tests normal ad playback flow.
Confirms FSM handles repeated transitions and state re-entries correctly.
Ensures
FinishStateis reached at the end of the playback sequence.
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
StateFactory Override Mechanism:
The test setup overrides the default FSM states by registering custom test subclasses withStateFactory. This enables injecting mock or test-specific behavior and verifying that the factory indeed produces the overridden types.FSM Player Subclassing:
playerFsmis instantiated as an anonymous subclass ofFsmPlayerImperialwith overriddeninitializeState()method to start the FSM inMoviePlayingState. This is critical to control the initial FSM state for consistent testing.State Singleton Pattern:
The factory ensures that only one instance of each state class is created and reused, preventing unnecessary object creation and promoting state consistency.FSM Transitions Tested via
transit(Input)Calls:
The FSM is driven by sendingInputenum events, and after each event, assertions are made on the current active state. This simulates real playback and ad event sequences.Error Handling:
Tests confirm that invalid operations such as creating states of unsupported classes or unexpected input sequences result in expected exceptions or graceful state recoveries.
Interaction With Other System Components
StateFactory (
com.tubitv.media.fsm.concrete.factory.StateFactory):
Used to create and manage FSM states. This test verifies its flexibility and correctness.FSM Player (
FsmPlayer,FsmPlayerImperial):
Core FSM implementation controlling playback state transitions. The test validates its behavior under different event sequences.FSM States (
MoviePlayingState,MakingAdCallState, etc.):
Represents discrete states of playback and ad logic. The test ensures the FSM player transitions among these states correctly.Input Events (
Inputenum):
Defines events that trigger FSM transitions, simulating playback actions or user interactions.JUnit Testing Framework:
Provides the testing infrastructure with annotations like@Test,@Before, and assertions to validate state correctness.
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:
Creating or overriding FSM states in the
StateFactory.Initializing the FSM player (
playerFsm).Driving the FSM with input events using
playerFsm.transit(Input.EVENT).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:
The creation and override mechanism of FSM states via
StateFactory.The correctness of FSM player transitions across multiple playback and advertisement-related states.
The FSM's handling of typical playback flows, including VPAID ads and error recovery.
The singleton nature of state instances to ensure resource efficiency.
Error conditions such as invalid state creation.
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.