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 com.android.settings.accessibility;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.provider.Settings;
23 
24 import androidx.preference.ListPreference;
25 import androidx.test.core.app.ApplicationProvider;
26 
27 import com.android.settings.R;
28 import com.android.settings.core.BasePreferenceController;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.robolectric.RobolectricTestRunner;
34 
35 /** Tests for {@link SelectLongPressTimeoutPreferenceController}. */
36 @RunWith(RobolectricTestRunner.class)
37 public class SelectLongPressTimeoutPreferenceControllerTest {
38     private static final int SHORT_VALUE = 400;
39     private static final int MEDIUM_VALUE = 1000;
40     private static final int LONG_VALUE = 1500;
41     private static final int INVALID_VALUE = 0;
42 
43     private final Context mContext = ApplicationProvider.getApplicationContext();
44     private SelectLongPressTimeoutPreferenceController mController;
45     private ListPreference mPreference;
46 
47     @Before
setUp()48     public void setUp() {
49         mController = new SelectLongPressTimeoutPreferenceController(mContext, "press_timeout");
50         mPreference = new ListPreference(mContext);
51         mPreference.setEntries(R.array.long_press_timeout_selector_titles);
52         mPreference.setEntryValues(R.array.long_press_timeout_selector_values);
53         mPreference.setSummary("%s");
54     }
55 
56     @Test
getAvailabilityStatus_shouldReturnAvailable()57     public void getAvailabilityStatus_shouldReturnAvailable() {
58         assertThat(mController.getAvailabilityStatus())
59                 .isEqualTo(BasePreferenceController.AVAILABLE);
60     }
61 
62     @Test
updateState_toShortTimeout_shouldReturnShortSummary()63     public void updateState_toShortTimeout_shouldReturnShortSummary() {
64         Settings.Secure.putInt(mContext.getContentResolver(),
65                 Settings.Secure.LONG_PRESS_TIMEOUT, SHORT_VALUE);
66         mController.updateState(mPreference);
67 
68         assertThat(mPreference.getSummary().toString()).isEqualTo("Short");
69     }
70 
71     @Test
updateState_toMediumTimeout_shouldReturnMediumSummary()72     public void updateState_toMediumTimeout_shouldReturnMediumSummary() {
73         Settings.Secure.putInt(mContext.getContentResolver(),
74                 Settings.Secure.LONG_PRESS_TIMEOUT, MEDIUM_VALUE);
75         mController.updateState(mPreference);
76 
77         assertThat(mPreference.getSummary().toString()).isEqualTo("Medium");
78     }
79 
80     @Test
updateState_toLongTimeout_shouldReturnLongSummary()81     public void updateState_toLongTimeout_shouldReturnLongSummary() {
82         Settings.Secure.putInt(mContext.getContentResolver(),
83                 Settings.Secure.LONG_PRESS_TIMEOUT, LONG_VALUE);
84         mController.updateState(mPreference);
85 
86         assertThat(mPreference.getSummary().toString()).isEqualTo("Long");
87     }
88 
89     @Test
updateState_toInvalidTimeout_shouldReturnEmptySummary()90     public void updateState_toInvalidTimeout_shouldReturnEmptySummary() {
91         Settings.Secure.putInt(mContext.getContentResolver(),
92                 Settings.Secure.LONG_PRESS_TIMEOUT, INVALID_VALUE);
93         mController.updateState(mPreference);
94 
95         assertThat(mPreference.getSummary().toString()).isEmpty();
96     }
97 }
98