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 static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.ContentResolver;
25 import android.content.Context;
26 import android.content.pm.PackageManager;
27 import android.provider.Settings;
28 
29 import com.android.internal.widget.LockPatternUtils;
30 import com.android.settings.core.BasePreferenceController;
31 import com.android.settings.testutils.FakeFeatureFactory;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.robolectric.ParameterizedRobolectricTestRunner;
39 import org.robolectric.RuntimeEnvironment;
40 import org.robolectric.Shadows;
41 import org.robolectric.shadows.ShadowPackageManager;
42 
43 import java.util.Arrays;
44 import java.util.Collection;
45 
46 @RunWith(ParameterizedRobolectricTestRunner.class)
47 public class PowerMenuPrivacyPreferenceControllerAvailabilityTest {
48 
49     private static final String CONTROLS_ENABLED = Settings.Secure.CONTROLS_ENABLED;
50     private static final String CONTROLS_FEATURE = PackageManager.FEATURE_CONTROLS;
51     private static final String CARDS_ENABLED = Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED;
52     private static final String CARDS_AVAILABLE = Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE;
53 
54     @ParameterizedRobolectricTestRunner.Parameters(
55             name = "ctrls available={0}, ctrls enabled={1}, cards available={2}, cards enabled={3}")
data()56     public static Collection data() {
57         return Arrays.asList(new Object[][]{
58                 // controls available, controls enabled, cards available, cards enabled, available
59                 {false, false, false, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
60                 {false, false, false, true, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
61                 {false, false, true, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
62                 {false, false, true, true, BasePreferenceController.AVAILABLE},
63                 {false, true, false, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
64                 {false, true, false, true, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
65                 {false, true, true, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
66                 {false, true, true, true, BasePreferenceController.AVAILABLE},
67                 {true, false, false, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
68                 {true, false, false, true, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
69                 {true, false, true, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
70                 {true, false, true, true, BasePreferenceController.AVAILABLE},
71                 {true, true, false, false, BasePreferenceController.AVAILABLE},
72                 {true, true, false, true, BasePreferenceController.AVAILABLE},
73                 {true, true, true, false, BasePreferenceController.AVAILABLE},
74                 {true, true, true, true, BasePreferenceController.AVAILABLE}
75         });
76     }
77 
78     private Context mContext;
79     private PowerMenuPrivacyPreferenceController mController;
80     private ShadowPackageManager mShadowPackageManager;
81 
82     @Mock
83     private LockPatternUtils mLockPatternUtils;
84 
85     private boolean mControlsAvailable;
86     private boolean mControlsEnabled;
87     private boolean mCardsAvailable;
88     private boolean mCardsEnabled;
89     private int mAvailable;
90 
PowerMenuPrivacyPreferenceControllerAvailabilityTest( boolean controlsAvailable, boolean controlsEnabled, boolean cardsAvailable, boolean cardsEnabled, int available)91     public PowerMenuPrivacyPreferenceControllerAvailabilityTest(
92             boolean controlsAvailable,
93             boolean controlsEnabled,
94             boolean cardsAvailable,
95             boolean cardsEnabled,
96             int available) {
97         mControlsAvailable = controlsAvailable;
98         mControlsEnabled = controlsEnabled;
99         mCardsAvailable = cardsAvailable;
100         mCardsEnabled = cardsEnabled;
101         mAvailable = available;
102     }
103 
104     @Before
setUp()105     public void setUp() {
106         MockitoAnnotations.initMocks(this);
107         mContext = RuntimeEnvironment.application;
108         mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
109         FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest();
110 
111         // For these tests we assume the device has a secure lock.
112         when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
113                 .thenReturn(mLockPatternUtils);
114         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
115 
116         mController = new PowerMenuPrivacyPreferenceController(mContext, "TEST_KEY");
117     }
118 
119     @Test
getAvailabilityStatus_possiblyAvailableAndEnabled()120     public void getAvailabilityStatus_possiblyAvailableAndEnabled() {
121         mShadowPackageManager.setSystemFeature(CONTROLS_FEATURE, mControlsAvailable);
122         ContentResolver cr = mContext.getContentResolver();
123         Settings.Secure.putInt(cr, CONTROLS_ENABLED, mControlsEnabled ? 1 : 0);
124         Settings.Secure.putInt(cr, CARDS_AVAILABLE, mCardsAvailable ? 1 : 0);
125         Settings.Secure.putInt(cr, CARDS_ENABLED, mCardsEnabled ? 1 : 0);
126 
127         assertThat(mController.getAvailabilityStatus()).isEqualTo(mAvailable);
128     }
129 }
130