TestCarContext
public class TestCarContext extends CarContext
| java.lang.Object | ||||
| ↳ | android.content.Context | |||
| ↳ | android.content.ContextWrapper | |||
| ↳ | androidx.car.app.CarContext | |||
| ↳ | androidx.car.app.testing.TestCarContext |
The CarContext that is used for testing.
This class will return the test version of car services for tracking calls during testing.
It also allows retrieving the car services already cast to the testing class by calling getCarService with the class name of the test services:
testCarContext.getCarService(TestAppManager.class)
testCarContext.getCarService(TestNavigationManager.class)
testCarContext.getCarService(TestScreenManager.class)
Allows retrieving all Intents sent via startCarApp.
Summary
Nested types |
|---|
public class TestCarContext.PermissionRequestInfoA representation of a permission request including the permissions that were requested as well as the callback provided. |
Public methods |
|
|---|---|
static @NonNull TestCarContext |
createCarContext(@NonNull Context testContext)Creates a |
void |
Requests to finish the car app. |
@NonNull Object |
getCarService(@NonNull String name)Provides a car service by name. |
@NonNull T |
<T> getCarService(@NonNull Class<T> serviceClass)Returns a car service by class. |
@NonNull FakeHost |
Retrieve the |
@Nullable TestCarContext.PermissionRequestInfo |
Returns a |
@NonNull List<Intent> |
Returns all |
boolean |
Verifies if |
void |
requestPermissions(Requests the provided |
void |
reset()Resets the values tracked by this |
void |
startCarApp(@NonNull Intent intent)Starts a car app on the car screen. |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Inherited methods |
||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||
|
||||||||||||||||||
Public methods
createCarContext
public static @NonNull TestCarContext createCarContext(@NonNull Context testContext)
Creates a TestCarContext to use for testing.
| Throws | |
|---|---|
java.lang.NullPointerException |
if |
finishCarApp
public void finishCarApp()
Requests to finish the car app.
Call this when your app is done and should be closed. The Session corresponding to this CarContext will become State.DESTROYED.
At some point after this call, the OS will destroy your CarAppService.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
getCarService
public @NonNull Object getCarService(@NonNull String name)
Provides a car service by name.
The class of the returned object varies by the requested name.
Currently supported car services, and their respective classes, are:
- An
AppManagerfor communication between the app and the host. - A
NavigationManagerfor management of navigation updates. - A
ScreenManagerfor management ofScreens. - A
ConstraintManagerfor management of content limits. - A
CarHardwareManagerfor interacting with car hardware (e.g. sensors) data. - A
SuggestionManagerfor postingandroidx.car.app.suggestion.model.Suggestions.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
| Parameters | |
|---|---|
@NonNull String name |
The name of the car service requested. This should be one of |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
java.lang.IllegalStateException |
if the service referred by |
java.lang.NullPointerException |
if |
getCarService
public @NonNull T <T> getCarService(@NonNull Class<T> serviceClass)
Returns a car service by class.
See getCarService for a list of the supported car services.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
java.lang.IllegalStateException |
if |
java.lang.NullPointerException |
if |
getLastPermissionRequestInfo
public @Nullable TestCarContext.PermissionRequestInfo getLastPermissionRequestInfo()
Returns a PermissionRequestInfo including the information with the last call made to requestPermissions, or null if no call was made.
getStartCarAppIntents
public @NonNull List<Intent> getStartCarAppIntents()
Returns all Intents sent via startCarApp.
The Intents are stored in the order of when they were sent, where the first intent in the list, is the first intent sent.
The results will be stored until reset is called.
hasCalledFinishCarApp
public boolean hasCalledFinishCarApp()
Verifies if finishCarApp has been called.
requestPermissions
public void requestPermissions(
@NonNull List<String> permissions,
@NonNull Executor executor,
@NonNull OnRequestPermissionsListener listener
)
Requests the provided permissions from the user.
When the result is available, the listener provided will be called using the Executor provided.
This method should be called using a androidx.car.app.model.ParkedOnlyOnClickListener.
If this method is called while the host deems it is unsafe (for example, when the user is driving), the permission(s) will not be requested from the user.
If the Session is destroyed before the user accepts or rejects the permissions, the callback will not be executed.
CarAppActivity are not allowed and may be rejected during app submission. See CarAppActivity for more details.
| Parameters | |
|---|---|
@NonNull List<String> permissions |
the runtime permissions to request from the user |
@NonNull Executor executor |
the executor that will be used for calling the |
@NonNull OnRequestPermissionsListener listener |
listener that will be notified when the user takes action on the permission request |
| Throws | |
|---|---|
java.lang.NullPointerException |
if any of |
startCarApp
public void startCarApp(@NonNull Intent intent)
Starts a car app on the car screen.
The target application will get the Intent via onCreateScreen or onNewIntent.
Supported Intents:
An
Intentto navigate.- The action must be
ACTION_NAVIGATE. - The data URI scheme must be either a latitude,longitude pair, or a + separated string query as follows:
- 1) "geo:12.345,14.8767" for a latitude, longitude pair.
- 2) "geo:0,0?q=123+Main+St,+Seattle,+WA+98101" for an address.
- 3) "geo:0,0?q=a+place+name" for a place to search for.
An
Intentto make a phone call.- The
Intentmust be created as defined here. An
Intentto start this app in the car.- The component name of the intent must be the one for the
CarAppServicethat contains thisCarContext. If the component name is for a different component, the method will throw aSecurityException.
This method should not be called until the Lifecycle.State of the context's Session is at least CREATED.
| Throws | |
|---|---|
java.lang.SecurityException |
if the app attempts to start a different app explicitly or does not have permissions for the requested action |
androidx.car.app.HostException |
if the remote call fails. For example, if the intent cannot be handled by the car host. |
java.lang.NullPointerException |
if |