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 getPartiallyAllowedUpdateType()

Returns the type of update that's permitted or null, if no limitations exist.

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(int 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 setPartiallyAllowedUpdateType(Integer allowedUpdate)

Simulates conditions where only the in-app update type you specify, such as flexible or immediate, is permitted.

void setTotalBytesToDownload(long totalBytesToDownload)

Sets the size of an available update in bytes.

void setUpdateAvailable(int availableVersionCode)

Declares that an update is available and provides its version code.

void setUpdateNotAvailable()

Declares that no updates are available.

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.

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

From class java.lang.Object
From interface com.google.android.play.core.appupdate.AppUpdateManager

Public constructors

FakeAppUpdateManager

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

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, int, Activity, int) and is in the DOWNLOADED state.

Returns
Task<Void>

downloadCompletes

void downloadCompletes ()

Simulates the download completing.

This method call works only after downloadStarts().

downloadFails

void downloadFails ()

Simulates a download failure.

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

downloadStarts

void downloadStarts ()

Simulates the download starting.

This method call works only after calling userAcceptsUpdate().

getAppUpdateInfo

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>

getPartiallyAllowedUpdateType

Integer getPartiallyAllowedUpdateType ()

Returns the type of update that's permitted or null, if no limitations exist.

Returns
Integer

getTypeForUpdateInProgress

Integer getTypeForUpdateInProgress ()

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

Returns
Integer

installCompletes

void installCompletes ()

Simulates the download completing.

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

installFails

void installFails ()

Simulates an update install failure.

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

isConfirmationDialogVisible

boolean isConfirmationDialogVisible ()

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

Returns
boolean

isImmediateFlowVisible

boolean isImmediateFlowVisible ()

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

Returns
boolean

isInstallSplashScreenVisible

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

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

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

void setClientVersionStalenessDays (int clientVersionStaleness)

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

This method call only has effect if ERROR(/this#isUpdateAvailable) is true.

Parameters
clientVersionStaleness int: The number of days client has known about the fake update.

setInstallErrorCode

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 NO_ERROR.

Parameters
installErrorCode int: The error code you want to simulate.

setPartiallyAllowedUpdateType

void setPartiallyAllowedUpdateType (Integer allowedUpdate)

Simulates conditions where only the in-app update type you specify, such as flexible or immediate, is permitted.

This call also sets the error code to NO_ERROR_PARTIALLY_ALLOWED to simulate this limitation.

Parameters
allowedUpdate Integer: The AppUpdateType you want to permit.

setTotalBytesToDownload

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

void setUpdateAvailable (int availableVersionCode)

Declares that an update is available and provides its version code.

Parameters
availableVersionCode int: The version code of the fake update.

setUpdateNotAvailable

void setUpdateNotAvailable ()

Declares that no updates are available.

startUpdateFlowForResult

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.

For example, you can use it for androidx.fragment.app.Fragment:

startUpdateFlowForResult(info, type, fragment::startIntentSenderForResult, requestCode);
 

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

Similar to startActivityForResult(Intent, int), this method is run "for result". The result from the flow's Activity can be handled by the onActivityResult of the object that would normally receive an activity result when the starter's startIntentSenderForResult is called. The result is tagged with the provided requestCode.

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 and parameter documentation, see startUpdateFlowForResult(AppUpdateInfo, int, Activity, int).

Parameters
appUpdateInfo AppUpdateInfo

appUpdateType int

starter IntentSenderForResultStarter

requestCode int

Returns
boolean

startUpdateFlowForResult

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

Starts the desired update flow.

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

Similar to startActivityForResult(Intent, int), this method is run "for result". The result from the flow's Activity can be handled by the provided activity's ERROR(/Activity#onActivityResult) and is tagged with the provided requestCode.

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().

In the flexible flow, the Activity returns right after user confirmation with one of the following values:

  • RESULT_OK: The user accepted the request to update.
  • RESULT_CANCELED: The user denied the request to update.
  • RESULT_IN_APP_UPDATE_FAILED: Something failed during the request for user confirmation. For example, the user terminates the app before responding to the request.

In the immediate flow, the Activity returns one of the following values:

  • RESULT_OK: The user accepted and the update succeeded (which, in practice, your app never should never receive because it already updated).
  • RESULT_CANCELED: The user denied or canceled the update.
  • RESULT_IN_APP_UPDATE_FAILED: The flow failed either during the user confirmation, the download, or the installation.

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

appUpdateType int: A value from AppUpdateType that determines the type of update flow.

activity Activity: the Activity that will be used for ERROR(/Activity#onActivityResult)

requestCode int: the request code that will be used for ERROR(/Activity#onActivityResult)

Returns
boolean Specifies whether the requested update has started.

unregisterListener

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

void userAcceptsUpdate ()

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

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

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

userCancelsDownload

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

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 RESULT_CANCELED is also received by the calling activity's ERROR(/android.app.Activity#onActivityResult).