1 /*
2  * Copyright (C) 2023 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 android.platform.test.rule
18 
19 import android.platform.uiautomator_helpers.DeviceHelpers.shell
20 import org.junit.rules.TestWatcher
21 import org.junit.runner.Description
22 
23 class DisableAnimationsRule: TestWatcher() {
24     private var prevAnimationState: AnimationState = defaultAnimationState
25     private var animationState: AnimationState
26         get() =
27             AnimationState(
28                 shell("settings get global transition_animation_scale").toFloatOrNull() ?: 1f,
29                 shell("settings get global window_animation_scale").toFloatOrNull() ?: 1f,
30                 shell("settings get global animator_duration_scale").toFloatOrNull() ?: 1f,
31             )
32         set(value) {
33             shell("settings put global transition_animation_scale ${value.transitions}")
34             shell("settings put global window_animation_scale ${value.windows}")
35             shell("settings put global animator_duration_scale ${value.animators}")
36         }
37 
startingnull38     override fun starting(description: Description) {
39         super.starting(description)
40         prevAnimationState = animationState
41         animationState = disabledAnimationState
42     }
43 
finishednull44     override fun finished(description: Description) {
45         super.finished(description)
46         animationState = prevAnimationState
47     }
48 
49     data class AnimationState(val transitions: Float, val windows: Float, val animators: Float)
50 
51     private companion object {
52         val disabledAnimationState = AnimationState(transitions = 0f, windows = 0f, animators = 0f)
53         val defaultAnimationState = AnimationState(transitions = 1f, windows = 1f, animators = 1f)
54     }
55 }