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 android.view.accessibility.AccessibilityManager.AUTOCLICK_DELAY_DEFAULT;
20 
21 import static com.android.settings.accessibility.AccessibilityUtil.State.OFF;
22 import static com.android.settings.accessibility.AccessibilityUtil.State.ON;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import android.content.Context;
27 import android.provider.Settings;
28 
29 import androidx.test.core.app.ApplicationProvider;
30 
31 import com.android.settings.R;
32 import com.android.settings.core.BasePreferenceController;
33 
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.robolectric.RobolectricTestRunner;
38 
39 /** Tests for {@link AutoclickPreferenceController}. */
40 @RunWith(RobolectricTestRunner.class)
41 public class AutoclickPreferenceControllerTest {
42 
43     private final Context mContext = ApplicationProvider.getApplicationContext();
44     private AutoclickPreferenceController mController;
45 
46     @Before
setUp()47     public void setUp() {
48         mController = new AutoclickPreferenceController(mContext, "auto_click");
49     }
50 
51     @Test
getAvailabilityStatus_shouldReturnAvailableUnsearchable()52     public void getAvailabilityStatus_shouldReturnAvailableUnsearchable() {
53         assertThat(mController.getAvailabilityStatus())
54                 .isEqualTo(BasePreferenceController.AVAILABLE);
55     }
56 
57     @Test
getSummary_disabledAutoclick_shouldReturnOffSummary()58     public void getSummary_disabledAutoclick_shouldReturnOffSummary() {
59         setAutoClickEnabled(false);
60 
61         assertThat(mController.getSummary().toString())
62                 .isEqualTo(mContext.getText(R.string.autoclick_disabled));
63     }
64 
65     @Test
getSummary_enabledAutoclick_shouldReturnOnSummary()66     public void getSummary_enabledAutoclick_shouldReturnOnSummary() {
67         setAutoClickEnabled(true);
68         setAutoClickDelayed(AUTOCLICK_DELAY_DEFAULT);
69 
70 
71         assertThat(mController.getSummary().toString())
72                 .isEqualTo(AutoclickUtils.getAutoclickDelaySummary(
73                         mContext,
74                         R.string.accessibilty_autoclick_preference_subtitle_medium_delay,
75                         AUTOCLICK_DELAY_DEFAULT).toString());
76     }
77 
setAutoClickEnabled(boolean enabled)78     private void setAutoClickEnabled(boolean enabled) {
79         Settings.Secure.putInt(mContext.getContentResolver(),
80                 Settings.Secure.ACCESSIBILITY_AUTOCLICK_ENABLED, enabled ? ON : OFF);
81     }
82 
setAutoClickDelayed(int delayedInMs)83     private void setAutoClickDelayed(int delayedInMs) {
84         Settings.Secure.putInt(mContext.getContentResolver(),
85                 Settings.Secure.ACCESSIBILITY_AUTOCLICK_DELAY, delayedInMs);
86     }
87 }
88