1 /*
2 * Copyright (C) 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 com.android.compose.test
18
19 import com.android.compose.animation.scene.SceneKey
20 import com.android.compose.animation.scene.SceneTransitionLayoutImpl
21 import com.android.compose.animation.scene.content.state.TransitionState.Transition
22 import com.android.mechanics.GestureContext
23 import kotlinx.coroutines.CompletableDeferred
24
25 /** A [Transition.ChangeScene] for tests that will be finished once [finish] is called. */
26 abstract class TestSceneTransition(
27 fromScene: SceneKey,
28 toScene: SceneKey,
29 replacedTransition: Transition?,
30 ) : Transition.ChangeScene(fromScene, toScene, replacedTransition) {
31 private val finishCompletable = CompletableDeferred<Unit>()
32
runnull33 override suspend fun run() {
34 finishCompletable.await()
35 }
36
37 /** Finish this transition. */
finishnull38 fun finish() {
39 finishCompletable.complete(Unit)
40 }
41 }
42
43 /** A utility to easily create a [TestSceneTransition] in tests. */
transitionnull44 fun transition(
45 from: SceneKey,
46 to: SceneKey,
47 current: () -> SceneKey = { to },
<lambda>null48 progress: () -> Float = { 0f },
<lambda>null49 progressVelocity: () -> Float = { 0f },
<lambda>null50 previewProgress: () -> Float = { 0f },
<lambda>null51 previewProgressVelocity: () -> Float = { 0f },
<lambda>null52 isInPreviewStage: () -> Boolean = { false },
<lambda>null53 interruptionProgress: () -> Float = { 0f },
54 isInitiatedByUserInput: Boolean = false,
55 isUserInputOngoing: Boolean = false,
56 onFreezeAndAnimate: ((TestSceneTransition) -> Unit)? = null,
57 replacedTransition: Transition? = null,
58 gestureContext: GestureContext? = null,
59 ): TestSceneTransition {
60 return object : TestSceneTransition(from, to, replacedTransition) {
61 override val currentScene: SceneKey
62 get() = current()
63
64 override val progress: Float
65 get() = progress()
66
67 override val progressVelocity: Float
68 get() = progressVelocity()
69
70 override val previewProgress: Float
71 get() = previewProgress()
72
73 override val previewProgressVelocity: Float
74 get() = previewProgressVelocity()
75
76 override val isInPreviewStage: Boolean
77 get() = isInPreviewStage()
78
79 override val isInitiatedByUserInput: Boolean = isInitiatedByUserInput
80 override val isUserInputOngoing: Boolean = isUserInputOngoing
81 override val gestureContext: GestureContext? = gestureContext
82
freezeAndAnimateToCurrentStatenull83 override fun freezeAndAnimateToCurrentState() {
84 if (onFreezeAndAnimate != null) {
85 onFreezeAndAnimate(this)
86 } else {
87 finish()
88 }
89 }
90
interruptionProgressnull91 override fun interruptionProgress(layoutImpl: SceneTransitionLayoutImpl): Float {
92 return interruptionProgress()
93 }
94 }
95 }
96