1 /*
2  * Copyright (C) 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 com.android.permissioncontroller
18 
19 import com.android.compatibility.common.util.SystemUtil.runShellCommand
20 import org.junit.rules.TestRule
21 import org.junit.runner.Description
22 import org.junit.runners.model.Statement
23 
24 /**
25  * Rule to disable the animations during a test
26  */
27 class DisableAnimationsRule : TestRule {
28     /** transition_animation_scale setting */
29     private val transitionAnimationScale = GlobalSetting("transition_animation_scale")
30 
31     /** window_animation_scale setting */
32     private val windowAnimationScale = GlobalSetting("window_animation_scale")
33 
34     /** animator_duration_scale setting */
35     private val animatorDurationScale = GlobalSetting("animator_duration_scale")
36 
applynull37     override fun apply(base: Statement, description: Description?): Statement {
38         return object : Statement() {
39             override fun evaluate() {
40                 transitionAnimationScale.zero()
41                 windowAnimationScale.zero()
42                 animatorDurationScale.zero()
43                 try {
44                     base.evaluate()
45                 } finally {
46                     transitionAnimationScale.restore()
47                     windowAnimationScale.restore()
48                     animatorDurationScale.restore()
49                 }
50             }
51         }
52     }
53 
54     /**
55      * Class representing a single global setting
56      */
57     private class GlobalSetting(val name: String) {
58         private val initialValue = runShellCommand("settings get global $name")
59 
60         /**
61          * Set setting to 0
62          */
zeronull63         fun zero() {
64             runShellCommand("settings put global $name 0")
65         }
66 
67         /**
68          * Restore original state of setting
69          */
restorenull70         fun restore() {
71             runShellCommand("settings put global $name $initialValue")
72         }
73     }
74 }