TestNavigationEventHandler


public final class TestNavigationEventHandler<T extends NavigationEventInfo> extends NavigationEventHandler


A test implementation of NavigationEventHandler that records received events and invocation counts.

This class is primarily used in tests to verify that specific navigation event callbacks are triggered as expected. It captures the NavigationEvent objects and counts how many times each callback is fired.

Parameters
<T extends NavigationEventInfo>

The NavigationEventInfo type this callback handles.

Summary

Public constructors

<T extends NavigationEventInfo> TestNavigationEventHandler(
    @NonNull T currentInfo,
    @NonNull List<@NonNull T> backInfo,
    @NonNull List<@NonNull T> forwardInfo,
    boolean isForwardEnabled,
    @NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onForwardStarted,
    @NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onForwardProgressed,
    @NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onForwardCancelled,
    @NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onForwardCompleted,
    boolean isBackEnabled,
    @NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onBackStarted,
    @NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onBackProgressed,
    @NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onBackCancelled,
    @NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onBackCompleted
)

Public methods

final int

Number of times onBackCancelled has been invoked.

final int

Number of times onBackCompleted has been invoked.

final @NonNull List<@NonNull NavigationEvent>

All events received by onBackProgressed.

final int

Number of times onBackProgressed has been invoked.

final @NonNull List<@NonNull NavigationEvent>

All events received by onBackStarted.

final int

Number of times onBackStarted has been invoked.

final int

Number of times onForwardCancelled has been invoked.

final int

Number of times onForwardCompleted has been invoked.

final @NonNull List<@NonNull NavigationEvent>

All events received by onForwardProgressed.

final int

Number of times onForwardProgressed has been invoked.

final @NonNull List<@NonNull NavigationEvent>

All events received by onForwardStarted.

final int

Number of times onForwardStarted has been invoked.

Protected methods

void

Override this to handle the cancellation of a back navigation event.

void

Override this to handle the completion of a back navigation event.

void

Override this to handle the progress of an ongoing back navigation event.

void

Override this to handle the beginning of a back navigation event.

void

Override this to handle the cancellation of a forward navigation event.

void

Override this to handle the completion of a forward navigation event.

void

Override this to handle the progress of an ongoing forward navigation event.

void

Override this to handle the beginning of a forward navigation event.

Inherited methods

From androidx.navigationevent.NavigationEventHandler
final @NonNull List<@NonNull T>

Contextual information describing the application's back state for this handler.

final @NonNull T

The contextual information representing the active destination for this handler.

final @NonNull List<@NonNull T>

Contextual information describing the application's forward state for this handler.

final @NonNull NavigationEventTransitionState

The current transition state of this specific handler (e.g., Idle or InProgress).

final boolean

Controls whether this handler is active and should be considered for back event dispatching.

final boolean

Controls whether this handler is active and should be considered for forward event dispatching.

final void

Removes this handler from the NavigationEventDispatcher it is registered with.

final void
setBackEnabled(boolean isBackEnabled)

Controls whether this handler is active and should be considered for back event dispatching.

final void
setForwardEnabled(boolean isForwardEnabled)

Controls whether this handler is active and should be considered for forward event dispatching.

final void
setInfo(
    @NonNull T currentInfo,
    @NonNull List<@NonNull T> backInfo,
    @NonNull List<@NonNull T> forwardInfo
)

Sets the directional navigation context for this handler.

Public constructors

TestNavigationEventHandler

public <T extends NavigationEventInfo> TestNavigationEventHandler(
    @NonNull T currentInfo,
    @NonNull List<@NonNull T> backInfo,
    @NonNull List<@NonNull T> forwardInfo,
    boolean isForwardEnabled,
    @NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onForwardStarted,
    @NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onForwardProgressed,
    @NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onForwardCancelled,
    @NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onForwardCompleted,
    boolean isBackEnabled,
    @NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onBackStarted,
    @NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onBackProgressed,
    @NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onBackCancelled,
    @NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onBackCompleted
)
Parameters
<T extends NavigationEventInfo>

The NavigationEventInfo type this callback handles.

@NonNull T currentInfo

Initial current navigation info.

@NonNull List<@NonNull T> backInfo

Initial back stack info list. Defaults to empty.

@NonNull List<@NonNull T> forwardInfo

Initial forward stack info list. Defaults to empty.

boolean isForwardEnabled

Determines if forward callbacks should process events. Defaults to true.

@NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onForwardStarted

Optional lambda to execute when onForwardStarted is called.

@NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onForwardProgressed

Optional lambda to execute when onForwardProgressed is called.

@NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onForwardCancelled

Optional lambda to execute when onForwardCancelled is called.

@NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onForwardCompleted

Optional lambda to execute when onForwardCompleted is called.

boolean isBackEnabled

Determines if back callbacks should process events. Defaults to true.

@NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onBackStarted

Optional lambda to execute when onBackStarted is called.

@NonNull Function2<@NonNull TestNavigationEventHandler<@NonNull T>, @NonNull NavigationEventUnit> onBackProgressed

Optional lambda to execute when onBackProgressed is called.

@NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onBackCancelled

Optional lambda to execute when onBackCancelled is called.

@NonNull Function1<@NonNull TestNavigationEventHandler<@NonNull T>, Unit> onBackCompleted

Optional lambda to execute when onBackCompleted is called.

Public methods

getOnBackCancelledInvocations

Added in 1.0.0-alpha09
public final int getOnBackCancelledInvocations()

Number of times onBackCancelled has been invoked.

getOnBackCompletedInvocations

Added in 1.0.0-alpha09
public final int getOnBackCompletedInvocations()

Number of times onBackCompleted has been invoked.

getOnBackProgressedEvents

Added in 1.0.0-alpha09
public final @NonNull List<@NonNull NavigationEventgetOnBackProgressedEvents()

All events received by onBackProgressed.

getOnBackProgressedInvocations

Added in 1.0.0-alpha09
public final int getOnBackProgressedInvocations()

Number of times onBackProgressed has been invoked.

getOnBackStartedEvents

Added in 1.0.0-alpha09
public final @NonNull List<@NonNull NavigationEventgetOnBackStartedEvents()

All events received by onBackStarted.

getOnBackStartedInvocations

Added in 1.0.0-alpha09
public final int getOnBackStartedInvocations()

Number of times onBackStarted has been invoked.

getOnForwardCancelledInvocations

Added in 1.0.0-alpha09
public final int getOnForwardCancelledInvocations()

Number of times onForwardCancelled has been invoked.

getOnForwardCompletedInvocations

Added in 1.0.0-alpha09
public final int getOnForwardCompletedInvocations()

Number of times onForwardCompleted has been invoked.

getOnForwardProgressedEvents

Added in 1.0.0-alpha09
public final @NonNull List<@NonNull NavigationEventgetOnForwardProgressedEvents()

All events received by onForwardProgressed.

getOnForwardProgressedInvocations

Added in 1.0.0-alpha09
public final int getOnForwardProgressedInvocations()

Number of times onForwardProgressed has been invoked.

getOnForwardStartedEvents

Added in 1.0.0-alpha09
public final @NonNull List<@NonNull NavigationEventgetOnForwardStartedEvents()

All events received by onForwardStarted.

getOnForwardStartedInvocations

Added in 1.0.0-alpha09
public final int getOnForwardStartedInvocations()

Number of times onForwardStarted has been invoked.

Protected methods

onBackCancelled

protected void onBackCancelled()

Override this to handle the cancellation of a back navigation event.

This is called when the user cancels the navigation action, signaling that the UI should return to its original state.

onBackCompleted

protected void onBackCompleted()

Override this to handle the completion of a back navigation event.

This is called when the user commits to the back navigation action, signaling that the navigation should be finalized.

The default implementation throws an UnsupportedOperationException. Any handler that can be completed must override this method to handle the navigation.

onBackProgressed

protected void onBackProgressed(@NonNull NavigationEvent event)

Override this to handle the progress of an ongoing back navigation event.

This is called repeatedly during a gesture-driven back navigation to update the UI in real-time based on the user's input.

Parameters
@NonNull NavigationEvent event

The NavigationEvent containing progress information.

onBackStarted

protected void onBackStarted(@NonNull NavigationEvent event)

Override this to handle the beginning of a back navigation event.

This is called when a user action initiates a back navigation. It's the ideal place to prepare UI elements for a transition.

Parameters
@NonNull NavigationEvent event

The NavigationEvent that triggered this handler.

onForwardCancelled

protected void onForwardCancelled()

Override this to handle the cancellation of a forward navigation event.

This is called when the user cancels the navigation action, signaling that the UI should return to its original state.

onForwardCompleted

protected void onForwardCompleted()

Override this to handle the completion of a forward navigation event.

This is called when the user commits to the forward navigation action, signaling that the navigation should be finalized.

The default implementation throws an UnsupportedOperationException. Any handler that can be completed must override this method to handle the navigation.

onForwardProgressed

protected void onForwardProgressed(@NonNull NavigationEvent event)

Override this to handle the progress of an ongoing forward navigation event.

This is called repeatedly during a gesture-driven forward navigation to update the UI in real-time based on the user's input.

Parameters
@NonNull NavigationEvent event

The NavigationEvent containing progress information.

onForwardStarted

protected void onForwardStarted(@NonNull NavigationEvent event)

Override this to handle the beginning of a forward navigation event.

This is called when a user action initiates a forward navigation. It's the ideal place to prepare UI elements for a transition.

Parameters
@NonNull NavigationEvent event

The NavigationEvent that triggered this handler.