TestCarContext
class TestCarContext : CarContext
| kotlin.Any | ||||
| ↳ | 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 |
|---|
|
A representation of a permission request including the permissions that were requested as well as the callback provided. |
Public functions |
|
|---|---|
java-static TestCarContext |
createCarContext(testContext: Context)Creates a |
Unit |
Requests to finish the car app. |
Any |
getCarService(name: String)Provides a car service by name. |
T |
<T> getCarService(serviceClass: Class<T!>)Returns a car service by class. |
FakeHost |
Retrieve the |
TestCarContext.PermissionRequestInfo? |
Returns a |
(Mutable)List<Intent!> |
Returns all |
Boolean |
Verifies if |
Unit |
requestPermissions(Requests the provided |
Unit |
reset()Resets the values tracked by this |
Unit |
startCarApp(intent: Intent)Starts a car app on the car screen. |
Inherited Constants |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Public functions
createCarContext
java-static fun createCarContext(testContext: Context): TestCarContext
Creates a TestCarContext to use for testing.
| Throws | |
|---|---|
java.lang.NullPointerException |
if |
finishCarApp
fun finishCarApp(): Unit
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
fun getCarService(name: String): Any
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 | |
|---|---|
name: String |
The name of the car service requested. This should be one of |
| Returns | |
|---|---|
Any |
The car service instance |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
java.lang.IllegalStateException |
if the service referred by |
java.lang.NullPointerException |
if |
getCarService
fun <T> getCarService(serviceClass: Class<T!>): T
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.
| Parameters | |
|---|---|
serviceClass: Class<T!> |
the class of the requested service |
| Throws | |
|---|---|
java.lang.IllegalArgumentException |
if |
java.lang.IllegalStateException |
if |
java.lang.NullPointerException |
if |
getLastPermissionRequestInfo
fun getLastPermissionRequestInfo(): TestCarContext.PermissionRequestInfo?
Returns a PermissionRequestInfo including the information with the last call made to requestPermissions, or null if no call was made.
getStartCarAppIntents
fun getStartCarAppIntents(): (Mutable)List<Intent!>
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
fun hasCalledFinishCarApp(): Boolean
Verifies if finishCarApp has been called.
requestPermissions
fun requestPermissions(
permissions: (Mutable)List<String!>,
executor: Executor,
listener: OnRequestPermissionsListener
): Unit
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 | |
|---|---|
permissions: (Mutable)List<String!> |
the runtime permissions to request from the user |
executor: Executor |
the executor that will be used for calling the |
listener: OnRequestPermissionsListener |
listener that will be notified when the user takes action on the permission request |
| Throws | |
|---|---|
java.lang.NullPointerException |
if any of |
startCarApp
fun startCarApp(intent: Intent): Unit
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 |