FSM Unit Tests
Purpose
The FSM Unit Tests verify the correctness and reliability of the Finite State Machine (FSM) that governs media playback and ad integration within the player system. Specifically, they ensure that the FSM transitions through appropriate states in response to playback events, ad retrieval outcomes, user interactions, and error conditions. This testing subtopic addresses the critical need to validate the complex state logic controlling content and advertisement playback, including support for VPAID ads and preroll/midroll ad flows.
Functionality
This subtopic encompasses a suite of unit tests that simulate FSM state transitions by injecting input events and asserting the resulting FSM state. The tests cover:
State Initialization and Transition Sequences: Confirming the FSM starts in the correct initial state (e.g.,
FetchCuePointStateorMoviePlayingState) and flows through states likeMakingAdCallState,ReceiveAdState,AdPlayingState,VpaidState, andFinishStatebased on inputs.Ad Playback Variants: Testing flows with and without preroll ads and with VPAID ad manifests, validating seamless transitions between content playback and different ad states.
Error and Edge Case Handling: Ensuring that unexpected inputs or error conditions lead to appropriate fallback states, such as returning to content playback or moving to a finish state.
State Factory Behavior: Verifying that the
StateFactorycorrectly creates singleton instances of states and supports overriding state implementations for testing purposes.Custom State Implementation Support: Demonstrating that the FSM infrastructure can substitute custom state subclasses, facilitating extensibility and test isolation.
These tests simulate user and system events by calling the FSM’s `transit()` method with defined `Input` enums and checking the current FSM state using assertions.
Example Interaction Snippet
playerFsm.transit(Input.INITIALIZE);
assertTrue(playerFsm.getCurrentState() instanceof FetchCuePointState);
playerFsm.transit(Input.HAS_PREROLL_AD);
assertTrue(playerFsm.getCurrentState() instanceof MakingPrerollAdCallState);
playerFsm.transit(Input.PRE_ROLL_AD_RECEIVED);
assertTrue(playerFsm.getCurrentState() instanceof AdPlayingState);
playerFsm.transit(Input.VPAID_MANIFEST);
assertTrue(playerFsm.getCurrentState() instanceof VpaidState);
This snippet illustrates transitioning from initialization to preroll ad fetching, ad playing, and then to VPAID ad playback.
Integration with the Parent Topic and Other Subtopics
Under the broader topic of **Logging and Testing**, FSM Unit Tests provide a focused validation layer for the FSM playback logic that orchestrates the media player’s core behavior. While the parent topic includes logging utilities and instrumentation tests for overall system reliability, this subtopic uniquely concentrates on functional correctness of the FSM’s state transitions and error handling.
This testing ensures the FSM layer — which integrates with ad and cue point management, playback controllers, and VPAID interfaces — behaves predictably. It complements:
Logging Utilities by verifying the FSM emits expected state changes that logs can capture.
Instrumentation Tests by providing fast, isolated validation of FSM logic without requiring full device or UI interactions.
Dependency Injection Setup by leveraging injected
StateFactoryinstances to test state creation and overrides, supporting modular test configurations.
By validating FSM behavior independently, this subtopic helps prevent playback regressions and facilitates confident enhancements to playback and ad logic.
Diagram: FSM Unit Test State Transition Flow
The following flowchart visualizes a typical FSM state transition sequence exercised by the unit tests, highlighting key states and input triggers:
flowchart TD
Init[Initialize FSM] --> FetchCue[FetchCuePointState]
FetchCue -->|No Preroll Ad| MoviePlay[MoviePlayingState]
FetchCue -->|Has Preroll Ad| PreRollCall[MakingPrerollAdCallState]
PreRollCall --> PreRollAd[AdPlayingState]
PreRollAd -->|Next Ad| PreRollAd
PreRollAd -->|VPAID Manifest| VpaidState[VpaidState]
VpaidState -->|VPAID Finish| MoviePlay
MoviePlay --> MakeAdCall[MakingAdCallState]
MakeAdCall --> ReceiveAd[ReceiveAdState]
ReceiveAd --> ShowAd[AdPlayingState]
ShowAd -->|Next Ad| ShowAd
ShowAd -->|Ad Click| VastSandbox[VastAdInteractionSandBoxState]
VastSandbox -->|Back to Player| ShowAd
ShowAd -->|VPAID Manifest| VpaidState
MoviePlay -->|Movie Finish| FinishState
FinishState[FinishState] --> End[End]
This diagram represents the core states and transitions tested, including preroll ads, midroll ad calls, VPAID ad playback, user interactions (ad clicks), and completion. It emphasizes the sequence of inputs leading the FSM through ad and content playback cycles.
By rigorously exercising these state flows and factory behaviors, the FSM Unit Tests provide a foundation for a robust, maintainable playback engine that integrates content and advanced ad experiences seamlessly.