# TestWearableButtonsProvider

Artifact: [androidx.wear:wear-input-testing](https://developer.android.com/jetpack/androidx/releases/wear) [View Source](https://cs.android.com/search?q=file:androidx/wear/input/testing/TestWearableButtonsProvider.java+class:androidx.wear.input.testing.TestWearableButtonsProvider) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/wear#1.0.0)

*** ** * ** ***


```
public class TestWearableButtonsProvider implements WearableButtonsProvider
```

<br />

*** ** * ** ***

A `https://developer.android.com/reference/androidx/wear/input/WearableButtonsProvider` suitable for use in tests.

This allows for explicitly specifying which buttons are available for testing, and their coordinates. It is intended to be used by passing in a map, mapping between the button keycode (typically in the set `https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_STEM_PRIMARY`, `https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_STEM_1`, `https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_STEM_2`, or `https://developer.android.com/reference/android/view/KeyEvent.html#KEYCODE_STEM_3`) and the location of the button. Take the following example:

```
    Mapbuttons = new HashMap<>();
    buttons.put(KEYCODE_STEM_1, new TestWearableButtonLocation(100, 100);

    TestWearableButtonsProvider provider = new TestWearableButtonsProvider(buttons);

    WearableButtons.setWearableButtonsProvider(provider);
```

## Summary

| ### Nested types |
|---|
| `public class https://developer.android.com/reference/androidx/wear/input/testing/TestWearableButtonsProvider.TestWearableButtonLocation` Class describing the location of a button on a wearable device. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/androidx/wear/input/testing/TestWearableButtonsProvider#TestWearableButtonsProvider(java.util.Map<java.lang.Integer,androidx.wear.input.testing.TestWearableButtonsProvider.TestWearableButtonLocation>)( @https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/Integer.html, https://developer.android.com/reference/androidx/wear/input/testing/TestWearableButtonsProvider.TestWearableButtonLocation> buttons )` Build a button provider, which will respond with the provided set of buttons. |

| ### Public methods |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html int[]` | `https://developer.android.com/reference/androidx/wear/input/testing/TestWearableButtonsProvider#getAvailableButtonKeyCodes(android.content.Context)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context)` Get the keycodes of available hardware buttons on device. |
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/os/Bundle.html` | `https://developer.android.com/reference/androidx/wear/input/testing/TestWearableButtonsProvider#getButtonInfo(android.content.Context,int)(@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context, int keycode)` Returns a bundle containing the metadata of a specific button. |

## Public constructors

### TestWearableButtonsProvider

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/wear#1.0.0)

```
public TestWearableButtonsProvider(
    @NonNull Map<Integer, TestWearableButtonsProvider.TestWearableButtonLocation> buttons
)
```

Build a button provider, which will respond with the provided set of buttons.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/java/util/Map.html<https://developer.android.com/reference/java/lang/Integer.html, https://developer.android.com/reference/androidx/wear/input/testing/TestWearableButtonsProvider.TestWearableButtonLocation> buttons` | The buttons returned by this provider. |

## Public methods

### getAvailableButtonKeyCodes

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/wear#1.0.0)

```
public @Nullable int[] getAvailableButtonKeyCodes(@NonNull Context context)
```

Get the keycodes of available hardware buttons on device. This function based on key's locations from system property. This count includes the primary stem key as well as any secondary stem keys available.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The context of the current activity |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/Nullable.html int[]` | An int array of available button keycodes, or null if no keycodes could be read. |

### getButtonInfo

Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/wear#1.0.0)

```
public @NonNull Bundle getButtonInfo(@NonNull Context context, int keycode)
```

Returns a bundle containing the metadata of a specific button. Currently, only location is supported. Use with and . The key will not be present if the information is not available for the requested keycode.

The location returned is a Cartesian coordinate where the bottom left corner of the screen is the origin. The unit of measurement is in pixels. The coordinates do not take rotation into account and assume that the device is in the standard upright position.

| Parameters |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/content/Context.html context` | The context of the current activity |
| `int keycode` | The keycode associated with the hardware button of interest |

| Returns |
|---|---|
| `@https://jspecify.dev/docs/api/org/jspecify/annotations/NonNull.html https://developer.android.com/reference/android/os/Bundle.html` | A `https://developer.android.com/reference/android/os/Bundle.html` containing the metadata for the given keycode |