1 /*
2  * Copyright (C) 2022 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.gestures;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.spy;
23 import static org.mockito.Mockito.when;
24 
25 import android.app.Application;
26 import android.content.res.Resources;
27 import android.text.TextUtils;
28 
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceScreen;
31 import androidx.test.core.app.ApplicationProvider;
32 
33 import com.android.settings.R;
34 import com.android.settingslib.widget.FooterPreference;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.robolectric.RobolectricTestRunner;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class LongPressPowerFooterPreferenceControllerTest {
43 
44     private Application mContext;
45     private Resources mResources;
46     private Preference mPreference;
47     private LongPressPowerFooterPreferenceController mController;
48 
49     @Before
setUp()50     public void setUp() {
51         mContext = spy(ApplicationProvider.getApplicationContext());
52         mResources = spy(mContext.getResources());
53         when(mContext.getResources()).thenReturn(mResources);
54         mPreference = new FooterPreference(mContext);
55         mController = new LongPressPowerFooterPreferenceController(mContext, "test_key");
56 
57         PreferenceScreen mScreen = mock(PreferenceScreen.class);
58         when(mScreen.findPreference("test_key")).thenReturn(mPreference);
59         mController.displayPreference(mScreen);
60     }
61 
62     @Test
updateState_longPressPowerForPowerMenu_hidesPreference()63     public void updateState_longPressPowerForPowerMenu_hidesPreference() {
64         PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext);
65 
66         mController.updateState(mPreference);
67 
68         assertThat(mPreference.isVisible()).isFalse();
69     }
70 
71     @Test
updateState_longPressPowerForAssistant_showsPreference()72     public void updateState_longPressPowerForAssistant_showsPreference() {
73         PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext);
74 
75         mController.updateState(mPreference);
76 
77         assertThat(mPreference.isVisible()).isTrue();
78     }
79 
80     @Test
updateState_notEligible_showsPreference()81     public void updateState_notEligible_showsPreference() {
82         PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext);
83 
84         mController.updateState(mPreference);
85 
86         assertThat(mPreference.isVisible()).isTrue();
87     }
88 
89     @Test
updateState_hushGestureEnabled_includesPreventRingingHint()90     public void updateState_hushGestureEnabled_includesPreventRingingHint() {
91         when(mResources.getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled))
92                 .thenReturn(true);
93 
94         mController.updateState(mPreference);
95 
96         assertThat(mPreference.getSummary().toString())
97                 .isEqualTo(
98                         TextUtils.concat(
99                                 mContext.getString(R.string.power_menu_power_volume_up_hint),
100                                 "\n\n",
101                                 mContext.getString(
102                                         R.string.power_menu_power_prevent_ringing_hint)));
103     }
104 
105     @Test
updateState_hushGestureDisabled_doesNotIncludePreventRingingHint()106     public void updateState_hushGestureDisabled_doesNotIncludePreventRingingHint() {
107         when(mResources.getBoolean(com.android.internal.R.bool.config_volumeHushGestureEnabled))
108                 .thenReturn(false);
109 
110         mController.updateState(mPreference);
111 
112         assertThat(mPreference.getSummary().toString())
113                 .isEqualTo(mContext.getString(R.string.power_menu_power_volume_up_hint));
114     }
115 }
116