FakeAppUpdateManager

public class FakeAppUpdateManager
extends Object implements AppUpdateManager

java.lang.Object
   ↳ com.google.android.play.core.appupdate.testing.FakeAppUpdateManager


A fake implementation of the AppUpdateManager.

This implementation is completely self-contained in the library and does not interact with the Play Store. For this reason, no UI is shown and no update is performed on the device. You can use this API to test your support for in-app updates, such as initiating update flows, checking download progress, and handling failure scenarios.

Keep in mind, you should use this API for only unit-tests and early development iterations. It is not intended for full stack integration tests, which you can conduct by uploading the latest version of your app to Play and sideloading an older version of your app that supports in-app updates onto your test device.

Summary

Public constructors

FakeAppUpdateManager(Context context)

Creates a fake instance of AppUpdateManager, which you can use for testing.

Public methods

Task<Void> completeUpdate()

For a flexible update flow, triggers the completion of the update.

void downloadCompletes()

Simulates the download completing.

void downloadFails()

Simulates a download failure.

void downloadStarts()

Simulates the download starting.

Task<AppUpdateInfo> getAppUpdateInfo()

Requests the update availability for the current app, an intent to start an update flow, and, if applicable, the state of updates currently in progress.

Integer getTypeForUpdateInProgress()

Returns the type of update that's currently in progress or null, if no update is in progress.

void installCompletes()

Simulates the download completing.

void installFails()

Simulates an update install failure.

boolean isConfirmationDialogVisible()

Returns whether the user confirmation dialog of the flexible update is visible to the user.

boolean isImmediateFlowVisible()

Returns whether the user confirmation screen of the immediate update is visible to the user.

boolean isInstallSplashScreenVisible()

Returns whether the splash screen is visible to the user.

void registerListener(InstallStateUpdatedListener listener)

Registers a listener for this app that receives state changes for self-update operations.

void setBytesDownloaded(long bytesDownloaded)

Simulates progress of the download by setting the number of bytes already downloaded.

void setClientVersionStalenessDays(Integer clientVersionStaleness)

Sets the staleness of the version installed on user's device.

void setInstallErrorCode(int installErrorCode)

Sets an error code which is returned by certain update flow API calls.

void setTotalBytesToDownload(long totalBytesToDownload)

Sets the size of an available update in bytes.

void setUpdateAvailable(int availableVersionCode, int appUpdateType)

Declares that an update is available but only appUpdateType is allowed to execute it.

void setUpdateAvailable(int availableVersionCode)

Declares that an update is available and both the flexible and immediate flow can be used to execute it.

void setUpdateNotAvailable()

Declares that no updates are available.

void setUpdatePriority(int updatePriority)

Sets the priority of the available update.

boolean startUpdateFlowForResult(AppUpdateInfo appUpdateInfo, int appUpdateType, IntentSenderForResultStarter starter, int requestCode)

Starts the desired update flow.

boolean startUpdateFlowForResult(AppUpdateInfo appUpdateInfo, int appUpdateType, Activity activity, int requestCode)

Starts the desired update flow.

boolean startUpdateFlowForResult(AppUpdateInfo appUpdateInfo, ActivityResultLauncher<IntentSenderRequest> activityResultLauncher, AppUpdateOptions options)

Starts the desired update flow.

void unregisterListener(InstallStateUpdatedListener listener)

Unregisters a listener you previously registered using registerListener(InstallStateUpdatedListener).

void userAcceptsUpdate()

Simulates that a user has accepted an update from the update confirmation dialog.

void userCancelsDownload()

Simulates the user canceling the download via the Play UI.

void userRejectsUpdate()

Simulates that a user has declined an update from the update confirmation dialog.

Inherited methods

Public constructors

FakeAppUpdateManager

public FakeAppUpdateManager (Context context)

Creates a fake instance of AppUpdateManager, which you can use for testing.

Note: This class should be used as a test-scoped singleton in unit tests and as an app-wide singleton during app development.

Parameters
context Context

Public methods

completeUpdate

public Task<Void> completeUpdate ()

For a flexible update flow, triggers the completion of the update.

You should call this method to complete an update that has already been started via startUpdateFlowForResult(AppUpdateInfo, Activity, AppUpdateOptions, int) and is in the InstallStatus.DOWNLOADED state.

Returns
Task<Void>

downloadCompletes

public void downloadCompletes ()

Simulates the download completing.

This method call works only after downloadStarts().

downloadFails

public void downloadFails ()

Simulates a download failure.

This method call works only if the download of an update is pending or downloading.

downloadStarts

public void downloadStarts ()

Simulates the download starting.

This method call works only after calling userAcceptsUpdate().

getAppUpdateInfo

public Task<AppUpdateInfo> getAppUpdateInfo ()

Requests the update availability for the current app, an intent to start an update flow, and, if applicable, the state of updates currently in progress.

To learn more, read Check for update availability.

Returns
Task<AppUpdateInfo>

getTypeForUpdateInProgress

public Integer getTypeForUpdateInProgress ()

Returns the type of update that's currently in progress or null, if no update is in progress.

Returns
Integer

installCompletes

public void installCompletes ()

Simulates the download completing.

This method call works only after completeUpdate() has been triggered by the application.

installFails

public void installFails ()

Simulates an update install failure.

This method call works only if the update is already installing (after a call to completeUpdate()).

isConfirmationDialogVisible

public boolean isConfirmationDialogVisible ()

Returns whether the user confirmation dialog of the flexible update is visible to the user.

Returns
boolean

isImmediateFlowVisible

public boolean isImmediateFlowVisible ()

Returns whether the user confirmation screen of the immediate update is visible to the user.

Returns
boolean

isInstallSplashScreenVisible

public boolean isInstallSplashScreenVisible ()

Returns whether the splash screen is visible to the user. This occurs during update completion. In the real implementation, your app is reloaded with the new version.

Returns
boolean

registerListener

public void registerListener (InstallStateUpdatedListener listener)

Registers a listener for this app that receives state changes for self-update operations. Typically, you need to register a listener for only Flexible updates, and listeners should be unregistered using unregisterListener(InstallStateUpdatedListener) when you no longer need them.

To learn more, read Handle a flexible update.

Parameters
listener InstallStateUpdatedListener: the listener you want to register

setBytesDownloaded

public void setBytesDownloaded (long bytesDownloaded)

Simulates progress of the download by setting the number of bytes already downloaded.

This method call works only after calling setTotalBytesToDownload(long).

Parameters
bytesDownloaded long

setClientVersionStalenessDays

public void setClientVersionStalenessDays (Integer clientVersionStaleness)

Sets the staleness of the version installed on user's device.

This method call only has effect if update is available.

Parameters
clientVersionStaleness Integer: the number of days client has known about the fake update

setInstallErrorCode

public void setInstallErrorCode (int installErrorCode)

Sets an error code which is returned by certain update flow API calls.

The error code is returned calls to getAppUpdateInfo() and completeUpdate(). However, the error code might not be honored if a more severe workflow error occurs, such as if completeUpdate() you call before an update is downloaded.

The error code is persistent. So, you need reset the value by calling the same method and passing InstallErrorCode.NO_ERROR.

Parameters
installErrorCode int: the error code you want to simulate

setTotalBytesToDownload

public void setTotalBytesToDownload (long totalBytesToDownload)

Sets the size of an available update in bytes.

This method call works only after calling downloadStarts().

Parameters
totalBytesToDownload long: total size of the update in bytes

setUpdateAvailable

public void setUpdateAvailable (int availableVersionCode, 
                int appUpdateType)

Declares that an update is available but only appUpdateType is allowed to execute it.

Parameters
availableVersionCode int: the version code of the fake update

appUpdateType int

setUpdateAvailable

public void setUpdateAvailable (int availableVersionCode)

Declares that an update is available and both the flexible and immediate flow can be used to execute it.

Parameters
availableVersionCode int: the version code of the fake update

setUpdateNotAvailable

public void setUpdateNotAvailable ()

Declares that no updates are available.

setUpdatePriority

public void setUpdatePriority (int updatePriority)

Sets the priority of the available update.

This method call only has effect if update is available.

Parameters
updatePriority int: priority of the available update

startUpdateFlowForResult

public boolean startUpdateFlowForResult (AppUpdateInfo appUpdateInfo, 
                int appUpdateType, 
                IntentSenderForResultStarter starter, 
                int requestCode)

Starts the desired update flow.

This method should be called if you are starting the flow from a Component different from an Activity and you don't want to receive the onActivityResult call on the Activity.

This is identical to calling startUpdateFlowForResult(AppUpdateInfo, IntentSenderForResultStarter, AppUpdateOptions, int), with the AppUpdateOptions.defaultOptions(int) for the requested update flow. See its documentation for further information.

For a list of possible result values and parameter documentation, see startUpdateFlowForResult(AppUpdateInfo, IntentSenderForResultStarter, AppUpdateOptions, int).

Parameters
appUpdateInfo AppUpdateInfo

appUpdateType int

starter IntentSenderForResultStarter

requestCode int

Returns
boolean

startUpdateFlowForResult

public boolean startUpdateFlowForResult (AppUpdateInfo appUpdateInfo, 
                int appUpdateType, 
                Activity activity, 
                int requestCode)

Starts the desired update flow.

Starts a flow of the provided AppUpdateType.

This is identical to calling startUpdateFlowForResult(AppUpdateInfo, Activity, AppUpdateOptions, int), with the AppUpdateOptions.defaultOptions(int) for the requested update flow. See its documentation for further information.

For a list of possible result values and parameter documentation, see startUpdateFlowForResult(AppUpdateInfo, Activity, AppUpdateOptions, int).

Parameters
appUpdateInfo AppUpdateInfo

appUpdateType int

activity Activity

requestCode int

Returns
boolean

startUpdateFlowForResult

public boolean startUpdateFlowForResult (AppUpdateInfo appUpdateInfo, 
                ActivityResultLauncher<IntentSenderRequest> activityResultLauncher, 
                AppUpdateOptions options)

Starts the desired update flow.

This API uses the ActivityResultLauncher provided by Jetpack components to launch the update flow for result.

Starts a flow with the provided options, including the flow type. Requires an AppUpdateInfo, which you can receive from getAppUpdateInfo(). This method can only start update flow types that are allowed according to AppUpdateInfo.isUpdateTypeAllowed(AppUpdateOptions).

Each AppUpdateInfo instance can be used only in a single call to this method. If you need to call it multiple times - for instance, when retrying to start a flow in case of failure - you need to get a fresh AppUpdateInfo from getAppUpdateInfo().

For a list of possible result values, see startUpdateFlowForResult(AppUpdateInfo, Activity, AppUpdateOptions, int).

Parameters
appUpdateInfo AppUpdateInfo: an intent you use to start an update. You get this using getAppUpdateInfo().

activityResultLauncher ActivityResultLauncher: An ActivityResultLauncher to launch the update flow.

options AppUpdateOptions: the update options, including the type of flow.

Returns
boolean whether the requested update has started.

unregisterListener

public void unregisterListener (InstallStateUpdatedListener listener)

Unregisters a listener you previously registered using registerListener(InstallStateUpdatedListener).

To learn more, read Handle a flexible update.

Parameters
listener InstallStateUpdatedListener: the listener you want to unregister

userAcceptsUpdate

public void userAcceptsUpdate ()

Simulates that a user has accepted an update from the update confirmation dialog. The download is enqueued in InstallStatus.PENDING status.

This method call works only if isConfirmationDialogVisible() or isImmediateFlowVisible() is true.

In the real implementation, a Activity.RESULT_OK is also received by the calling activity's ERROR(/android.app.Activity#onActivityResult).

userCancelsDownload

public void userCancelsDownload ()

Simulates the user canceling the download via the Play UI.

This method call works only if the download of an update is pending or downloading.

userRejectsUpdate

public void userRejectsUpdate ()

Simulates that a user has declined an update from the update confirmation dialog.

This method call works only if isConfirmationDialogVisible() or isImmediateFlowVisible() is true.

In the real implementation, a Activity.RESULT_CANCELED is also received by the calling activity's ERROR(/android.app.Activity#onActivityResult).