5

In the doc's it say this about UiAutomation

Class for interacting with the device's UI by simulation user actions and introspection of the screen content. It relies on the platform accessibility APIs to introspect the screen and to perform some actions on the remote view tree. It also allows injecting of arbitrary raw input events simulating user interaction with keyboards and touch devices. One can think of a UiAutomation as a special type of AccessibilityService which does not provide hooks for the service life cycle and exposes other APIs that are useful for UI test automation.

The APIs exposed by this class are low-level to maximize flexibility when developing UI test automation tools and libraries. Generally, a UiAutomation client should be using a higher-level library or implement high-level functions. For example, performing a tap on the screen requires construction and injecting of a touch down and up events which have to be delivered to the system by a call to injectInputEvent(android.view.InputEvent, boolean).

The APIs exposed by this class operate across applications enabling a client to write tests that cover use cases spanning over multiple applications. For example, going to the settings application to change a setting and then interacting with another application whose behavior depends on that setting.

How exactly is UiAutomation different from a regular AccessibilityService, as it doesn't inherit from it in the source code.

public final class UiAutomation {
    private static final String LOG_TAG = UiAutomation.class.getSimpleName();
    // omitted the rest...
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Anton Toshik
  • 2,621
  • 2
  • 19
  • 42
  • `UiAutomation` does not *inherit* from AccessibilityService, it connects to the service, see https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/android/app/UiAutomation.java#293 – Diego Torres Milano Nov 14 '20 at 01:04
  • @DiegoTorresMilano can any application do the same 'connect to the service', or some there are some special privileges required? – Anton Toshik Nov 15 '20 at 13:53

1 Answers1

5

Accessibility Service:

should only be used to assist users with disabilities in using Android devices and apps. They run in the background and receive [...] AccessibilityEvents [...]. Such events denote some state transition in the user interface, for example, the focus has changed, a button has been clicked, etc. Such a service can optionally request the capability for querying the content of the active window.

This is a powerful tool that can give you access to basically everything that happens on your phone, hence the user needs to explicitly enable such service in the phone settings (generally under Accessibility/Installed Services).

UiAutomation:

Well you basically quoted the description in your question, but here you have a more detailed one:

In certain way UiAutomation acts as a decorated AccessibilityService (decorator design pattern.). You can verify it reading the source code of the methods getServiceInfo and setServiceInfo, which internally uses the connected service counterparts (check the imports for a quick look).

This is a wrapper that adds some functionality, for example it allows you to navigate the view hierarchy, etc, without needing to enable the accessibility service in the phone settings every single time you first run an instrumented test. This is achieved with aid of the android.app.Instumentation class and other testing framework components. The system can safely grant the needed permissions because an instrumentation can only be started through adb or from within a System App.

This is not really an AccessibilityService (Not even a real service), but it's probably called that way for simplicity and to draw attention to the underlaying environment.

Raymond Arteaga
  • 4,355
  • 19
  • 35
  • To my knowledge UiAutomation can't be used in regular applications, can you comment on the difference between using the accessability services via UiAutomation in a test case scenario, and having just an accessibility service directly. – Anton Toshik Nov 15 '20 at 13:56
  • The app "performing" your instrumented tests [your.package.name].test is a "regular" application. You can read android.app.Instrumentation source code which internally uses UiAutomation. – Raymond Arteaga Nov 15 '20 at 22:19
  • @RaymondArteaga the difference is that you can only start the instrumentation from `adb` and not from a normal Activity – Diego Torres Milano Nov 16 '20 at 03:04
  • Correct, You cannot blindly grant access to the accessibility service lightly, as it allows to intercept basically everything is happening in your pone. The system enforces the necessary permissions so you can only start an instrumentation from adb or SystemApps: https://stackoverflow.com/questions/14339216/how-to-start-instrumentation-project-programmatically-using-android-intent. – Raymond Arteaga Nov 16 '20 at 10:41
  • @RaymondArteaga could you comment on `and exposes other APIs that are useful for UI test automation` please, what does it give you that an Accessibility Service doesn't. The way I see it right now, they are the same it's just one has to be ran adhoc over adb the other one has to be opted in to once installed. – Anton Toshik Nov 16 '20 at 13:56
  • @AntonToshik the base Accessibility Service is integrated to the android platform just to provide accessibility services (duh :P), UI automation uses it internally, while being closely bound to an Instrumentation, which provides the appropriate permissions. It would be a pain having to grant the accessibility permissions manually each time you first run an instrumented test, but thanks to the UI Automation wrapper and the rest of the testing/permissions framework, it all becomes integrated for the purpose of testing. We can move this conversation to a chat if you want. – Raymond Arteaga Nov 16 '20 at 15:15
  • 1
    @RaymondArteaga no need to, I think your answer is good I am just trying to clear up things like: "some functionality", "unnecessary stuff", "necessary permissions". The purpose of this question was to clear up the confusion between the 2, these blanket statements just extend the original problem. If you could update the answer with the comments you made here, avoiding ambiguity I'll mark it complete. – Anton Toshik Nov 16 '20 at 23:02
  • Thanks for all the input :), it made this a better answer. – Raymond Arteaga Nov 17 '20 at 00:14
  • @RaymondArteaga last query from me, is there anything particular that an instrumenetation test will let you do, that an Accessability Service won't? – Anton Toshik Nov 17 '20 at 15:19
  • @AntonToshik basically everything inside androidx.test package is designed for you to test your app, specialy androidx.test.uiautomator which uses UiAutomation as a bridge to perform the needes actions. You can find a view that contains certain text, etc. – Raymond Arteaga Nov 17 '20 at 15:39
  • You can take a look at https://github.com/dtmilano/CulebraTester2-public that creates a service that uses UiAutomator and allows you to control the device using the functionality exposed. – Diego Torres Milano Nov 18 '20 at 05:58
  • @DiegoTorresMilano the point is not to have an example of how to do it, it's to find out what are the distinct differences - what does UiAutomation allow you to do that an AccessibilityService doesn't? – Anton Toshik Nov 23 '20 at 10:15