Test your app with autofill

Some apps, such as password managers, can fill out the views in other apps with
data previously provided by the user. These apps that fill out other apps are
called autofill services. Most apps work with autofill services without any
changes. However, you can optimize your app to ensure it works as best as it can
with autofill services. After you optimize your app, you should test it to make
sure it works as intended with autofill services.

To better understand how to test your app with autofill services, you should
understand how autofill services and clients fit in the Autofill Framework. The
following are the high-level components of the framework:

Autofill services, such as password manager apps, which save and store the
user information that can be used in views across multiple apps. An autofill
service should be installed on the device and specified as the active autofill
service in the device settings.

Android System, which defines the workflow and provides the infrastructure
that makes services and clients work together. For a detailed explanation of
the autofill workflow, see AutofillService
and AutofillManager.

Autofill clients, which are the apps that provide the views that need to be
filled out or that hold the user's data. In most cases, this is the app that
you want to test with autofill services.

You should use an emulator or a physical device running Android 8.0 (API level
26) or higher to test your app. For more information about how to create an
emulator, see Create and Manage Virtual
Devices.

Install an autofill service

Before you can test your app with autofill, you need to install another app that
provides autofill services. You could use third-party app for this purpose, but
it's easier to use a sample autofill service to test your app. If you use a
sample app you don't need to sign up for any third-party services.

You can use the Android Autofill Framework
sample to test your
app with autofill services. The sample app provides an autofill service and
client Activities that you can use to test the
workflow before using it with your app. The sample app is referenced throughout
this article.

After you have installed the app, you should enable the autofill service in the
system settings. You can enable the service in Settings > System >
Languages & input > Advanced > Input assistance > Autofill
service.

Analyze data requirements

To test your app with the autofill service, the service needs to have data that
it can use to fill out your app. The service also needs to understand what type
of data is expected in the views of your app. For example, if your app has a
view that expects a username, the service should have a dataset that contains a
username and some mechanism to know that the view expects such data.

You should let the service know what type of data is expected in your views by
setting the android:autofillHints
attribute. Some services use sophisticated heuristics to determine the type of
data, but others, such as the sample app, rely on the developer to provide this
information. Your app works better with autofill services if you set the
android:autofillHints attribute in the views that are relevant for autofill.

Run your test

After you have analyzed the data requirements, you can run your test, which
includes saving test data in the autofill service and triggering autofill in
your app.

Save data in the service

The following steps show how to save data in the autofill service that is
currently active:

Open an app that has a view that expects the type of data that you want to
use in your test. The sample
app provides UI
with views that expect several types of data, such as credit card number and
username.

Tap the view that holds the type of data you need and type a value in it. You
usually have to submit the form before the service attempts to save the data.
For example, a login form requires you to provide username and password and
tap a sign in or submit button.

Android System displays a dialog that asks for your permission to save the
data. The dialog shows the name of the service that is currently active.
Verify that this is the service that you want to use in your test.

Tap Save.

If Android doesn't display the permission dialog, or if the service is not the
one you want to use in your test, then check that the service is currently
active in system settings.

Trigger autofill in your app

The following steps show how to trigger autofill in your app:

Open your app and go to the activity that has the views you want to test.

Tap the view that should be filled out.

Android System should display the autofill UI, which contains the
datasets that can fill the view, as shown in the screenshot below.

Tap the dataset that contains the data that you want to use. The view should
display the data previously stored in the service.

Autofill UI displaying available datasets.

If Android doesn't display the autofill UI, you can try the following
troubleshooting options:

Check that the views in your app use the right value in the android:autofillHints attribute. For a
list of possible values for the attribute, see the constants prefixed with
AUTOFILL_HINT in the View class.

Check that the android:importantForAutofill attribute is set to other than no on the view
that should be filled out or to other than noExcludeDescendants on the view or
one of its parents.