• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.settings.accessibility;
18 
19 import static com.android.settings.accessibility.DisableAnimationsPreferenceController.ANIMATION_OFF_VALUE;
20 import static com.android.settings.accessibility.DisableAnimationsPreferenceController.ANIMATION_ON_VALUE;
21 import static com.android.settings.accessibility.DisableAnimationsPreferenceController.TOGGLE_ANIMATION_TARGETS;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import android.content.Context;
26 import android.os.Looper;
27 import android.provider.Settings;
28 
29 import androidx.preference.PreferenceManager;
30 import androidx.preference.PreferenceScreen;
31 import androidx.preference.SwitchPreference;
32 import androidx.test.core.app.ApplicationProvider;
33 import androidx.test.ext.junit.runners.AndroidJUnit4;
34 
35 import com.android.settings.core.BasePreferenceController;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 
41 @RunWith(AndroidJUnit4.class)
42 public class DisableAnimationsPreferenceControllerTest {
43 
44     private static final String TEST_PREFERENCE_KEY = "disable_animation";
45     private final Context mContext = ApplicationProvider.getApplicationContext();
46 
47     private PreferenceScreen mScreen;
48     private SwitchPreference mPreference;
49     private DisableAnimationsPreferenceController mController;
50 
51     @Before
setUp()52     public void setUp() {
53         if (Looper.myLooper() == null) {
54             Looper.prepare();
55         }
56         PreferenceManager preferenceManager = new PreferenceManager(mContext);
57         mScreen = preferenceManager.createPreferenceScreen(mContext);
58         final SwitchPreference preference = new SwitchPreference(mContext);
59         preference.setKey(TEST_PREFERENCE_KEY);
60         mScreen.addPreference(preference);
61 
62         mController = new DisableAnimationsPreferenceController(mContext, TEST_PREFERENCE_KEY);
63         mController.displayPreference(mScreen);
64         mPreference = mScreen.findPreference(TEST_PREFERENCE_KEY);
65     }
66 
67     @Test
getAvailabilityStatus_shouldReturnAvailable()68     public void getAvailabilityStatus_shouldReturnAvailable() {
69         assertThat(mController.getAvailabilityStatus()).isEqualTo(
70                 BasePreferenceController.AVAILABLE);
71     }
72 
73     @Test
isChecked_enabledAnimation_shouldReturnFalse()74     public void isChecked_enabledAnimation_shouldReturnFalse() {
75         setAnimationScale(ANIMATION_ON_VALUE);
76 
77         mController.updateState(mPreference);
78 
79         assertThat(mController.isChecked()).isFalse();
80         assertThat(mPreference.isChecked()).isFalse();
81     }
82 
83     @Test
isChecked_disabledAnimation_shouldReturnTrue()84     public void isChecked_disabledAnimation_shouldReturnTrue() {
85         setAnimationScale(ANIMATION_OFF_VALUE);
86 
87         mController.updateState(mPreference);
88 
89         assertThat(mController.isChecked()).isTrue();
90         assertThat(mPreference.isChecked()).isTrue();
91     }
92 
93     @Test
setChecked_disabledAnimation_shouldDisableAnimationTargets()94     public void setChecked_disabledAnimation_shouldDisableAnimationTargets() {
95         mController.setChecked(true);
96 
97         for (String animationSetting : TOGGLE_ANIMATION_TARGETS) {
98             final float value = Settings.Global.getFloat(mContext.getContentResolver(),
99                     animationSetting, /* def= */ -1.0f);
100             assertThat(Float.compare(value, ANIMATION_OFF_VALUE)).isEqualTo(0);
101         }
102     }
103 
104     @Test
setChecked_enabledAnimation_shouldEnableAnimationTargets()105     public void setChecked_enabledAnimation_shouldEnableAnimationTargets() {
106         mController.setChecked(false);
107 
108         for (String animationSetting : TOGGLE_ANIMATION_TARGETS) {
109             final float value = Settings.Global.getFloat(mContext.getContentResolver(),
110                     animationSetting, /* def= */ -1.0f);
111             assertThat(Float.compare(value, ANIMATION_ON_VALUE)).isEqualTo(0);
112         }
113     }
114 
115     @Test
onStart_enabledAnimation_shouldReturnFalse()116     public void onStart_enabledAnimation_shouldReturnFalse() {
117         mController.onStart();
118 
119         setAnimationScale(ANIMATION_ON_VALUE);
120 
121         assertThat(mController.isChecked()).isFalse();
122         assertThat(mPreference.isChecked()).isFalse();
123     }
124 
125     @Test
onStart_disabledAnimation_shouldReturnTrue()126     public void onStart_disabledAnimation_shouldReturnTrue() {
127         mController.onStart();
128 
129         setAnimationScale(ANIMATION_OFF_VALUE);
130 
131         assertThat(mController.isChecked()).isTrue();
132         assertThat(mPreference.isChecked()).isTrue();
133     }
134 
135     @Test
onStop_shouldNotUpdateTargets()136     public void onStop_shouldNotUpdateTargets() {
137         mPreference.setChecked(true);
138         mController.onStart();
139         mController.onStop();
140 
141         setAnimationScale(ANIMATION_ON_VALUE);
142 
143         assertThat(mPreference.isChecked()).isTrue();
144     }
145 
setAnimationScale(float newValue)146     private void setAnimationScale(float newValue) {
147         for (String animationPreference : TOGGLE_ANIMATION_TARGETS) {
148             Settings.Global.putFloat(mContext.getContentResolver(), animationPreference,
149                     newValue);
150         }
151     }
152 }