Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

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 being shown and no update is really performed. The developer can simulate the most common user actions, download progress and failure scenarios.

It is designed to be used for unit-tests and early development iterations. It is not meant for full stack integration tests: the latter can be performed by uploading your app to Play and sideload an older version (modified so it can trigger an in-app update) onto your test device.

Summary

Public constructors

FakeAppUpdateManager(Context context)

Creates a fake instance of AppUpdateManager which can be used for testing.

Public methods

Task<Void> completeUpdate()

Triggers the completion of the update of the app for the flexible flow.

void downloadCompletes()

Simulates the download completing.

void downloadFails()

Simulates a download failure.

void downloadStarts()

Simulates the download starting.

Task<AppUpdateInfo> getAppUpdateInfo()

Requests to retrieve the package updatability state, any available intent to start the update flow and the current state of updates in progress (if any).

Integer getPartiallyAllowedUpdateType()

Returns which type of update is partially allowed or null if no update is partially allowed.

Integer getTypeForUpdateInProgress()

Returns which type of update is currently in progress or null if no update is in progress.

void installCompletes()

Simulates the download completing.

void installFails()

Simulates an 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 that will be alerted of state changes for self-update operations for this app.

void setInstallErrorCode(int installErrorCode)

Sets an error code which will be returned by the next API calls.

void setPartiallyAllowedUpdateType(Integer allowedUpdate)

In case the update is only partially allowed, which means that not all in-app update types are allowed by policy, the type of update which is exclusively allowed can be set here.

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, Activity activity, int requestCode)

Starts the desired update flow.

void unregisterListener(InstallStateUpdatedListener listener)

Unregisters a listener previously added using registerListener(InstallStateUpdatedListener).

void userAcceptsUpdate()

Simulates user click on the positive button in the update confirmation dialog.

void userCancelsDownload()

Simulates user canceling the download via the Play UI.

void userRejectsUpdate()

Simulates user click on the negative button in the currently active update flow.

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 can be used 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 ()

Triggers the completion of the update of the app for the flexible flow.

This should be called to complete an update that has been started via startUpdateFlowForResult(AppUpdateInfo, int, Activity, int) and is in DOWNLOADED status.

Returns
Task<Void>

downloadCompletes

void downloadCompletes ()

Simulates the download completing.

Works only after downloadStarts().

downloadFails

void downloadFails ()

Simulates a download failure.

Works only if the download of an update is pending or downloading.

downloadStarts

void downloadStarts ()

Simulates the download starting.

Works only after userAcceptsUpdate().

getAppUpdateInfo

Task<AppUpdateInfo> getAppUpdateInfo ()

Requests to retrieve the package updatability state, any available intent to start the update flow and the current state of updates in progress (if any).

Returns
Task<AppUpdateInfo>

getPartiallyAllowedUpdateType

Integer getPartiallyAllowedUpdateType ()

Returns which type of update is partially allowed or null if no update is partially allowed.

Returns
Integer

getTypeForUpdateInProgress

Integer getTypeForUpdateInProgress ()

Returns which type of update is currently in progress or null if no update is in progress.

Returns
Integer

installCompletes

void installCompletes ()

Simulates the download completing.

Works only after completeUpdate() has been triggered by the application.

installFails

void installFails ()

Simulates an install failure.

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 happens 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 that will be alerted of state changes for self-update operations for this app. Listeners should be subsequently unregistered using unregisterListener(InstallStateUpdatedListener).

Parameters
listener InstallStateUpdatedListener

setInstallErrorCode

void setInstallErrorCode (int installErrorCode)

Sets an error code which will be returned by the next API calls.

The error code is being honored by getAppUpdateInfo() and completeUpdate(); the error code is not being honored if a more severe workflow error happens, e.g. if completeUpdate() is called when the update is not downloaded yet.

The error code is persistent: to return to the default behaviour, you can reset the value by calling the same method with NO_ERROR.

Parameters
installErrorCode int

setPartiallyAllowedUpdateType

void setPartiallyAllowedUpdateType (Integer allowedUpdate)

In case the update is only partially allowed, which means that not all in-app update types are allowed by policy, the type of update which is exclusively allowed can be set here.

Also sets the error code to NO_ERROR_PARTIALLY_ALLOWED to enable the partial error behaviour.

Parameters
allowedUpdate Integer

setUpdateAvailable

void setUpdateAvailable (int availableVersionCode)

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

Parameters
availableVersionCode int

setUpdateNotAvailable

void setUpdateNotAvailable ()

Declares that no updates are available.

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

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

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

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

  • RESULT_OK if the user accepted and the update succeeded (which in practice will never be received, because your app has been updated in the meantime)
  • RESULT_CANCELED if the user denied or canceled the update
  • RESULT_IN_APP_UPDATE_FAILED if the flow failed (either during the user confirmation, the download or the installation)

Parameters
appUpdateInfo AppUpdateInfo

appUpdateType int

activity Activity

requestCode int

Returns
boolean whether the flow is allowed and has been started

unregisterListener

void unregisterListener (InstallStateUpdatedListener listener)

Unregisters a listener previously added using registerListener(InstallStateUpdatedListener).

Parameters
listener InstallStateUpdatedListener

userAcceptsUpdate

void userAcceptsUpdate ()

Simulates user click on the positive button in the update confirmation dialog. The download is enqueued in PENDING status.

Works only if isConfirmationDialogVisible() or isImmediateFlowVisible() is true.

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

userCancelsDownload

void userCancelsDownload ()

Simulates user canceling the download via the Play UI.

Works only if the download of an update is pending or downloading.

userRejectsUpdate

void userRejectsUpdate ()

Simulates user click on the negative button in the currently active update flow.

Works only if isConfirmationDialogVisible() or isImmediateFlowVisible() is true.

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