1 /*
2  * Copyright (C) 2019 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.provider.Settings;
27 
28 import androidx.preference.SwitchPreference;
29 
30 import com.android.settings.core.BasePreferenceController;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 
38 @RunWith(RobolectricTestRunner.class)
39 public class DisableAnimationsPreferenceControllerTest {
40 
41     private Context mContext;
42     private SwitchPreference mPreference;
43     private DisableAnimationsPreferenceController mController;
44 
45     @Before
setUp()46     public void setUp() {
47         mContext = RuntimeEnvironment.application;
48         mPreference = new SwitchPreference(mContext);
49         mController = new DisableAnimationsPreferenceController(mContext, "disable_animation");
50     }
51 
52     @Test
getAvailabilityStatus_shouldReturnAvailable()53     public void getAvailabilityStatus_shouldReturnAvailable() {
54         assertThat(mController.getAvailabilityStatus()).isEqualTo(
55                 BasePreferenceController.AVAILABLE);
56     }
57 
58     @Test
isChecked_enabledAnimation_shouldReturnFalse()59     public void isChecked_enabledAnimation_shouldReturnFalse() {
60         for (String animationPreference : TOGGLE_ANIMATION_TARGETS) {
61             Settings.Global.putString(mContext.getContentResolver(), animationPreference,
62                     ANIMATION_ON_VALUE);
63         }
64 
65         mController.updateState(mPreference);
66 
67         assertThat(mController.isChecked()).isFalse();
68         assertThat(mPreference.isChecked()).isFalse();
69     }
70 
71     @Test
isChecked_disabledAnimation_shouldReturnTrue()72     public void isChecked_disabledAnimation_shouldReturnTrue() {
73         for (String animationPreference : TOGGLE_ANIMATION_TARGETS) {
74             Settings.Global.putString(mContext.getContentResolver(), animationPreference,
75                     ANIMATION_OFF_VALUE);
76         }
77 
78         mController.updateState(mPreference);
79 
80         assertThat(mController.isChecked()).isTrue();
81         assertThat(mPreference.isChecked()).isTrue();
82     }
83 
84     @Test
setChecked_disabledAnimation_shouldDisableAnimationTargets()85     public void setChecked_disabledAnimation_shouldDisableAnimationTargets() {
86         mController.setChecked(true);
87 
88         for (String animationSetting : TOGGLE_ANIMATION_TARGETS) {
89             assertThat(Settings.Global.getString(mContext.getContentResolver(), animationSetting))
90                     .isEqualTo(ANIMATION_OFF_VALUE);
91         }
92     }
93 
94     @Test
setChecked_enabledAnimation_shouldEnableAnimationTargets()95     public void setChecked_enabledAnimation_shouldEnableAnimationTargets() {
96         mController.setChecked(false);
97 
98         for (String animationSetting : TOGGLE_ANIMATION_TARGETS) {
99             assertThat(Settings.Global.getString(mContext.getContentResolver(), animationSetting))
100                     .isEqualTo(ANIMATION_ON_VALUE);
101         }
102     }
103 }
104