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.gestures;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.provider.Settings;
24 
25 import com.android.settings.core.BasePreferenceController;
26 
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.robolectric.RobolectricTestRunner;
31 import org.robolectric.RuntimeEnvironment;
32 import org.robolectric.Shadows;
33 import org.robolectric.shadows.ShadowPackageManager;
34 
35 @RunWith(RobolectricTestRunner.class)
36 public class DeviceControlsPreferenceControllerTest {
37 
38     private Context mContext;
39     private DeviceControlsPreferenceController mController;
40     private ShadowPackageManager mShadowPackageManager;
41 
42     private static final String KEY_GESTURE_PANEL = "gesture_device_controls";
43     private static final String ENABLED_SETTING =
44             DeviceControlsPreferenceController.ENABLED_SETTING;
45 
46     @Before
setUp()47     public void setUp() {
48         mContext = RuntimeEnvironment.application;
49         mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
50         mController = new DeviceControlsPreferenceController(mContext, KEY_GESTURE_PANEL);
51     }
52 
53     @Test
testIsChecked_panelEnabled()54     public void testIsChecked_panelEnabled() {
55         Settings.Secure.putInt(
56                 mContext.getContentResolver(), ENABLED_SETTING, 1);
57         assertThat(mController.isChecked()).isTrue();
58     }
59 
60     @Test
testIsChecked_panelDisabled()61     public void testIsChecked_panelDisabled() {
62         Settings.Secure.putInt(
63                 mContext.getContentResolver(), ENABLED_SETTING, 0);
64         assertThat(mController.isChecked()).isFalse();
65     }
66 
67     @Test
getAvailabilityStatus_hasSystemFeature_panelAvailable()68     public void getAvailabilityStatus_hasSystemFeature_panelAvailable() {
69         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
70 
71         assertThat(mController.getAvailabilityStatus())
72                 .isEqualTo(BasePreferenceController.AVAILABLE);
73     }
74 
75     @Test
getAvailabilityStatus_hasntSystemFeature_panelUnsupported()76     public void getAvailabilityStatus_hasntSystemFeature_panelUnsupported() {
77         mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, false);
78 
79         assertThat(mController.getAvailabilityStatus())
80                 .isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
81     }
82 
83     @Test
isSliceable_correctKey()84     public void isSliceable_correctKey() {
85         final DeviceControlsPreferenceController controller =
86                 new DeviceControlsPreferenceController(mContext,
87                         DeviceControlsPreferenceController.TOGGLE_KEY);
88         assertThat(controller.isSliceable()).isTrue();
89     }
90 
91     @Test
isSliceable_incorrectKey()92     public void isSliceable_incorrectKey() {
93         final DeviceControlsPreferenceController controller =
94                 new DeviceControlsPreferenceController(mContext, "bad_key");
95         assertThat(controller.isSliceable()).isFalse();
96     }
97 
98     @Test
isPublicSlice_returnTrue()99     public void isPublicSlice_returnTrue() {
100         assertThat(mController.isPublicSlice()).isTrue();
101     }
102 }
103