1 /*
2 * Copyright 2024 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.collection.mutableIntObjectMapOf
20 import androidx.compose.ui.node.RootForTest
21
22 /**
23 * Provides storage of test related entities that must be accessible by anything other than
24 * [ComposeUiTest] and friends, for example the [InputDispatcher] or the implementation of some
25 * assertions and actions.
26 */
27 class TestContext internal constructor(internal val testOwner: TestOwner) {
28
29 /**
30 * Stores the [InputDispatcherState] of each [RootForTest]. The state will be restored in an
31 * [InputDispatcher] when it is created for an owner that has a state stored. To avoid leaking
32 * the [RootForTest], the [identityHashCode] of the root is used as the key instead of the
33 * actual object.
34 */
35 internal val states = mutableIntObjectMapOf<InputDispatcherState>()
36
37 /** Platform specific additions to the [TestContext]. */
38 internal val platform = createPlatformTestContext()
39 }
40
41 /** Factory method to create a [PlatformTestContext] */
createPlatformTestContextnull42 internal expect fun createPlatformTestContext(): PlatformTestContext
43
44 /**
45 * An extension to [TestContext] that allows us to add platform specific context to [TestContext].
46 *
47 * For example, on Android it contains a ComposeAccessibilityValidator that is used by Android's
48 * implementation of [tryPerformAccessibilityChecks].
49 */
50 internal expect class PlatformTestContext
51