1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.compose.ui.test
18 
19 import androidx.compose.ui.test.internal.JvmDefaultWithCompatibility
20 
21 /**
22  * Represents a resource of an application under test which can cause asynchronous background work
23  * to happen during test execution (e.g. an http request in response to a button click).
24  *
25  * By default, all interactions from the test with the compose tree (finding nodes, performing
26  * gestures, making assertions) will be synchronized with pending work in Compose's internals (such
27  * as applying state changes, recomposing, measuring, etc). This ensures that the UI is in a stable
28  * state when the interactions are performed, so that e.g. no pending recompositions are still
29  * scheduled that could potentially change the UI. However, any asynchronous work that is not done
30  * through one of Compose's mechanisms won't be included in the default synchronization. For such
31  * work, test authors can create an [IdlingResource] and register it into the test with
32  * [registerIdlingResource][androidx.compose.ui.test.junit4.ComposeTestRule
33  * .registerIdlingResource], and the interaction will wait for that resource to become idle prior to
34  * performing it.
35  */
36 @JvmDefaultWithCompatibility
37 interface IdlingResource {
38     /**
39      * Whether or not the [IdlingResource] is idle when reading this value. This should always be
40      * called from the main thread, which is why it should be lightweight and fast.
41      *
42      * If one idling resource returns `false`, the synchronization system will keep polling all
43      * idling resources until they are all idle.
44      */
45     val isIdleNow: Boolean
46 
47     /**
48      * Returns diagnostics that explain why the idling resource is busy, or `null` if the resource
49      * is not busy. Default implementation returns `null`.
50      */
getDiagnosticMessageIfBusynull51     fun getDiagnosticMessageIfBusy(): String? = null
52 }
53