Use PackageManager.FEATURE_CONTROLS to decide availability

Using this features, some of the Preference in Power Menu Settings can
change their availability and their summary.

Test: Robotest Settings
Fixes: 157244528
Change-Id: I704438dda181aa6347c3f168ac5ef6bd16148993
This commit is contained in:
Fabian Kozynski
2020-05-28 13:49:40 -04:00
parent 5c7f6d1b98
commit d0d07ddceb
9 changed files with 348 additions and 137 deletions

View File

@@ -12056,6 +12056,9 @@
<!-- Power menu setting privacy show controls [CHAR LIMIT=NONE] --> <!-- Power menu setting privacy show controls [CHAR LIMIT=NONE] -->
<string name="power_menu_privacy_show_controls">Show controls when locked</string> <string name="power_menu_privacy_show_controls">Show controls when locked</string>
<!-- Power menu setting privacy show cards [CHAR LIMIT=NONE] -->
<string name="power_menu_privacy_show_cards">Show cards when locked</string>
<!-- Power menu setting privacy hide all [CHAR LIMIT=NONE] --> <!-- Power menu setting privacy hide all [CHAR LIMIT=NONE] -->
<string name="power_menu_privacy_hide">Hide cards and controls when locked</string> <string name="power_menu_privacy_hide">Hide cards and controls when locked</string>

View File

@@ -17,6 +17,7 @@
package com.android.settings.gestures; package com.android.settings.gestures;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils; import android.text.TextUtils;
@@ -37,7 +38,9 @@ public class DeviceControlsPreferenceController extends GesturePreferenceControl
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
return AVAILABLE; boolean available = mContext.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CONTROLS);
return available ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
} }
@Override @Override

View File

@@ -17,6 +17,7 @@
package com.android.settings.gestures; package com.android.settings.gestures;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings; import android.provider.Settings;
import com.android.settings.R; import com.android.settings.R;
@@ -37,15 +38,15 @@ public class PowerMenuPreferenceController extends BasePreferenceController {
@Override @Override
public CharSequence getSummary() { public CharSequence getSummary() {
boolean controlsEnabled = Settings.Secure.getInt(mContext.getContentResolver(), boolean controlsVisible = isControlsAvailable()
CONTROLS_ENABLED_SETTING, 1) == 1; && Settings.Secure.getInt(mContext.getContentResolver(),
boolean cardsEnabled = Settings.Secure.getInt(mContext.getContentResolver(), CONTROLS_ENABLED_SETTING, 1) == 1;
CARDS_ENABLED_SETTING, 0) == 1; boolean cardsVisible = isCardsAvailable()
boolean cardsVisible = cardsEnabled && Settings.Secure.getInt(mContext.getContentResolver(), && Settings.Secure.getInt(mContext.getContentResolver(),
CARDS_AVAILABLE_SETTING, 0) == 1; CARDS_ENABLED_SETTING, 0) == 1;
if (controlsEnabled && cardsVisible) { if (controlsVisible && cardsVisible) {
return mContext.getText(R.string.power_menu_cards_passes_device_controls); return mContext.getText(R.string.power_menu_cards_passes_device_controls);
} else if (controlsEnabled) { } else if (controlsVisible) {
return mContext.getText(R.string.power_menu_device_controls); return mContext.getText(R.string.power_menu_device_controls);
} else if (cardsVisible) { } else if (cardsVisible) {
return mContext.getText(R.string.power_menu_cards_passes); return mContext.getText(R.string.power_menu_cards_passes);
@@ -56,6 +57,15 @@ public class PowerMenuPreferenceController extends BasePreferenceController {
@Override @Override
public int getAvailabilityStatus() { public int getAvailabilityStatus() {
return AVAILABLE; return isCardsAvailable() || isControlsAvailable() ? AVAILABLE : CONDITIONALLY_UNAVAILABLE;
}
private boolean isControlsAvailable() {
return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CONTROLS);
}
private boolean isCardsAvailable() {
return Settings.Secure.getInt(mContext.getContentResolver(),
CARDS_AVAILABLE_SETTING, 0) == 1;
} }
} }

View File

@@ -18,6 +18,7 @@ package com.android.settings.gestures;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.os.UserHandle; import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
@@ -57,13 +58,20 @@ public class PowerMenuPrivacyPreferenceController extends TogglePreferenceContro
public CharSequence getSummary() { public CharSequence getSummary() {
boolean cardsAvailable = Settings.Secure.getInt(mContext.getContentResolver(), boolean cardsAvailable = Settings.Secure.getInt(mContext.getContentResolver(),
CARDS_AVAILABLE_KEY, 0) != 0; CARDS_AVAILABLE_KEY, 0) != 0;
boolean controlsAvailable = isControlsAvailable();
final int res; final int res;
if (!isSecure()) { if (!isSecure()) {
res = R.string.power_menu_privacy_not_secure; res = R.string.power_menu_privacy_not_secure;
} else if (cardsAvailable) { } else if (cardsAvailable && controlsAvailable) {
res = R.string.power_menu_privacy_show; res = R.string.power_menu_privacy_show;
} else { } else if (!cardsAvailable && controlsAvailable) {
res = R.string.power_menu_privacy_show_controls; res = R.string.power_menu_privacy_show_controls;
} else if (cardsAvailable) {
res = R.string.power_menu_privacy_show_cards;
} else {
// In this case, neither cards nor controls are available. This preference should not
// be accessible as the power menu setting is not accessible
return "";
} }
return mContext.getText(res); return mContext.getText(res);
} }
@@ -87,7 +95,7 @@ public class PowerMenuPrivacyPreferenceController extends TogglePreferenceContro
boolean cardsAvailable = Settings.Secure.getInt(resolver, CARDS_AVAILABLE_KEY, 0) != 0; boolean cardsAvailable = Settings.Secure.getInt(resolver, CARDS_AVAILABLE_KEY, 0) != 0;
boolean cardsEnabled = Settings.Secure.getInt(resolver, CARDS_ENABLED_KEY, 0) != 0; boolean cardsEnabled = Settings.Secure.getInt(resolver, CARDS_ENABLED_KEY, 0) != 0;
boolean controlsEnabled = Settings.Secure.getInt(resolver, CONTROLS_ENABLED_KEY, 1) != 0; boolean controlsEnabled = Settings.Secure.getInt(resolver, CONTROLS_ENABLED_KEY, 1) != 0;
return (cardsAvailable && cardsEnabled) || controlsEnabled; return (cardsAvailable && cardsEnabled) || (isControlsAvailable() && controlsEnabled);
} }
private boolean isSecure() { private boolean isSecure() {
@@ -97,4 +105,8 @@ public class PowerMenuPrivacyPreferenceController extends TogglePreferenceContro
int userId = UserHandle.myUserId(); int userId = UserHandle.myUserId();
return utils.isSecure(userId); return utils.isSecure(userId);
} }
private boolean isControlsAvailable() {
return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CONTROLS);
}
} }

View File

@@ -19,6 +19,7 @@ package com.android.settings.gestures;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings; import android.provider.Settings;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
@@ -28,12 +29,15 @@ import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPackageManager;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class DeviceControlsPreferenceControllerTest { public class DeviceControlsPreferenceControllerTest {
private Context mContext; private Context mContext;
private DeviceControlsPreferenceController mController; private DeviceControlsPreferenceController mController;
private ShadowPackageManager mShadowPackageManager;
private static final String KEY_GESTURE_PANEL = "gesture_device_controls"; private static final String KEY_GESTURE_PANEL = "gesture_device_controls";
private static final String ENABLED_SETTING = private static final String ENABLED_SETTING =
@@ -42,6 +46,7 @@ public class DeviceControlsPreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
mController = new DeviceControlsPreferenceController(mContext, KEY_GESTURE_PANEL); mController = new DeviceControlsPreferenceController(mContext, KEY_GESTURE_PANEL);
} }
@@ -60,11 +65,21 @@ public class DeviceControlsPreferenceControllerTest {
} }
@Test @Test
public void getAvailabilityStatus_panelAvailable() { public void getAvailabilityStatus_hasSystemFeature_panelAvailable() {
mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
assertThat(mController.getAvailabilityStatus()) assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.AVAILABLE); .isEqualTo(BasePreferenceController.AVAILABLE);
} }
@Test
public void getAvailabilityStatus_hasntSystemFeature_panelUnsupported() {
mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, false);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
}
@Test @Test
public void isSliceable_correctKey() { public void isSliceable_correctKey() {
final DeviceControlsPreferenceController controller = final DeviceControlsPreferenceController controller =

View File

@@ -0,0 +1,113 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.gestures;
import static com.google.common.truth.Truth.assertThat;
import android.annotation.StringRes;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings;
import com.android.settings.R;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.ParameterizedRobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPackageManager;
import java.util.Arrays;
import java.util.Collection;
@RunWith(ParameterizedRobolectricTestRunner.class)
public class PowerMenuPreferenceControllerSummaryTest {
private static final String KEY_GESTURE_POWER_MENU = "gesture_power_menu";
private static final String CONTROLS_ENABLED = Settings.Secure.CONTROLS_ENABLED;
private static final String CONTROLS_FEATURE = PackageManager.FEATURE_CONTROLS;
private static final String CARDS_ENABLED = Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED;
private static final String CARDS_AVAILABLE = Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE;
@ParameterizedRobolectricTestRunner.Parameters(
name = "ctrls available={0}, ctrls enabled={1}, cards available={2}, cards enabled={3}")
public static Collection data() {
return Arrays.asList(new Object[][]{
// controls available, controls enabled, cards available, cards enabled, summary
{false, false, false, false, R.string.power_menu_none},
{false, false, false, true, R.string.power_menu_none},
{false, false, true, false, R.string.power_menu_none},
{false, false, true, true, R.string.power_menu_cards_passes},
{false, true, false, false, R.string.power_menu_none},
{false, true, false, true, R.string.power_menu_none},
{false, true, true, false, R.string.power_menu_none},
{false, true, true, true, R.string.power_menu_cards_passes},
{true, false, false, false, R.string.power_menu_none},
{true, false, false, true, R.string.power_menu_none},
{true, false, true, false, R.string.power_menu_none},
{true, false, true, true, R.string.power_menu_cards_passes},
{true, true, false, false, R.string.power_menu_device_controls},
{true, true, false, true, R.string.power_menu_device_controls},
{true, true, true, false, R.string.power_menu_device_controls},
{true, true, true, true, R.string.power_menu_cards_passes_device_controls}
});
}
private Context mContext;
private PowerMenuPreferenceController mController;
private ShadowPackageManager mShadowPackageManager;
private boolean mControlsAvailable;
private boolean mControlsEnabled;
private boolean mCardsAvailable;
private boolean mCardsEnabled;
private @StringRes int mSummaryRes;
public PowerMenuPreferenceControllerSummaryTest(
boolean controlsAvailable,
boolean controlsEnabled,
boolean cardsAvailable,
boolean cardsEnabled,
@StringRes int summaryRes) {
mControlsAvailable = controlsAvailable;
mControlsEnabled = controlsEnabled;
mCardsAvailable = cardsAvailable;
mCardsEnabled = cardsEnabled;
mSummaryRes = summaryRes;
}
@Before
public void setUp() {
mContext = RuntimeEnvironment.application;
mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
mController = new PowerMenuPreferenceController(mContext, KEY_GESTURE_POWER_MENU);
}
@Test
public void getSummary_possiblyAvailableAndEnabled() {
mShadowPackageManager.setSystemFeature(CONTROLS_FEATURE, mControlsAvailable);
ContentResolver cr = mContext.getContentResolver();
Settings.Secure.putInt(cr, CONTROLS_ENABLED, mControlsEnabled ? 1 : 0);
Settings.Secure.putInt(cr, CARDS_AVAILABLE, mCardsAvailable ? 1 : 0);
Settings.Secure.putInt(cr, CARDS_ENABLED, mCardsEnabled ? 1 : 0);
assertThat(mController.getSummary()).isEqualTo(mContext.getText(mSummaryRes));
}
}

View File

@@ -19,118 +19,71 @@ package com.android.settings.gestures;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings; import android.provider.Settings;
import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPackageManager;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class PowerMenuPreferenceControllerTest { public class PowerMenuPreferenceControllerTest {
private Context mContext; private Context mContext;
private PowerMenuPreferenceController mController; private PowerMenuPreferenceController mController;
private ShadowPackageManager mShadowPackageManager;
private static final String KEY_GESTURE_POWER_MENU = "gesture_power_menu"; private static final String KEY_GESTURE_POWER_MENU = "gesture_power_menu";
private static final String CONTROLS_ENABLED = Settings.Secure.CONTROLS_ENABLED; private static final String CONTROLS_ENABLED = Settings.Secure.CONTROLS_ENABLED;
private static final String CONTROLS_FEATURE = PackageManager.FEATURE_CONTROLS;
private static final String CARDS_ENABLED = Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED; private static final String CARDS_ENABLED = Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED;
private static final String CARDS_AVAILABLE = Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE; private static final String CARDS_AVAILABLE = Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
mController = new PowerMenuPreferenceController(mContext, KEY_GESTURE_POWER_MENU); mController = new PowerMenuPreferenceController(mContext, KEY_GESTURE_POWER_MENU);
} }
@Test @Test
public void getAvailabilityStatus_available() { public void getAvailabilityStatus_bothAvailable_available() {
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 1);
mShadowPackageManager.setSystemFeature(CONTROLS_FEATURE, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo( assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE); BasePreferenceController.AVAILABLE);
} }
@Test @Test
public void getSummary_allDisabled() { public void getAvailabilityStatus_onlyCardsAvailable_available() {
Settings.Secure.putInt(mContext.getContentResolver(), CONTROLS_ENABLED, 0);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_ENABLED, 0);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 0);
assertThat(mController.getSummary())
.isEqualTo(mContext.getText(R.string.power_menu_none));
}
@Test
public void getSummary_onlyControlsEnabled() {
Settings.Secure.putInt(mContext.getContentResolver(), CONTROLS_ENABLED, 1);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_ENABLED, 0);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 0);
assertThat(mController.getSummary())
.isEqualTo(mContext.getText(R.string.power_menu_device_controls));
}
@Test
public void getSummary_onlyCardsEnabled_notAvailable() {
Settings.Secure.putInt(mContext.getContentResolver(), CONTROLS_ENABLED, 0);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_ENABLED, 1);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 0);
assertThat(mController.getSummary())
.isEqualTo(mContext.getText(R.string.power_menu_none));
}
@Test
public void getSummary_cardsAvailable_notEnabled() {
Settings.Secure.putInt(mContext.getContentResolver(), CONTROLS_ENABLED, 0);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_ENABLED, 0);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 1); Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 1);
mShadowPackageManager.setSystemFeature(CONTROLS_FEATURE, false);
assertThat(mController.getSummary()) assertThat(mController.getAvailabilityStatus()).isEqualTo(
.isEqualTo(mContext.getText(R.string.power_menu_none)); BasePreferenceController.AVAILABLE);
} }
@Test @Test
public void getSummary_allEnabled_cardsNotAvailable() { public void getAvailabilityStatus_onlyControlsAvailable_available() {
Settings.Secure.putInt(mContext.getContentResolver(), CONTROLS_ENABLED, 1);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_ENABLED, 1);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 0); Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 0);
mShadowPackageManager.setSystemFeature(CONTROLS_FEATURE, true);
assertThat(mController.getSummary()) assertThat(mController.getAvailabilityStatus()).isEqualTo(
.isEqualTo(mContext.getText(R.string.power_menu_device_controls)); BasePreferenceController.AVAILABLE);
} }
@Test @Test
public void getSummary_controlsEnabled_cardsDisabledAvailable() { public void getAvailabilityStatus_bothUnavailable_unavailable() {
Settings.Secure.putInt(mContext.getContentResolver(), CONTROLS_ENABLED, 1); Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 0);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_ENABLED, 0); mShadowPackageManager.setSystemFeature(CONTROLS_FEATURE, false);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 1);
assertThat(mController.getSummary()) assertThat(mController.getAvailabilityStatus()).isEqualTo(
.isEqualTo(mContext.getText(R.string.power_menu_device_controls)); BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
}
@Test
public void getSummary_controlsDisabled() {
Settings.Secure.putInt(mContext.getContentResolver(), CONTROLS_ENABLED, 0);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_ENABLED, 1);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 1);
assertThat(mController.getSummary())
.isEqualTo(mContext.getText(R.string.power_menu_cards_passes));
}
@Test
public void getSummary_allEnabled() {
Settings.Secure.putInt(mContext.getContentResolver(), CONTROLS_ENABLED, 1);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_ENABLED, 1);
Settings.Secure.putInt(mContext.getContentResolver(), CARDS_AVAILABLE, 1);
assertThat(mController.getSummary())
.isEqualTo(mContext.getText(R.string.power_menu_cards_passes_device_controls));
} }
} }

View File

@@ -0,0 +1,129 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.gestures;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.when;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings;
import com.android.internal.widget.LockPatternUtils;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.testutils.FakeFeatureFactory;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.ParameterizedRobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPackageManager;
import java.util.Arrays;
import java.util.Collection;
@RunWith(ParameterizedRobolectricTestRunner.class)
public class PowerMenuPrivacyPreferenceControllerAvailabilityTest {
private static final String CONTROLS_ENABLED = Settings.Secure.CONTROLS_ENABLED;
private static final String CONTROLS_FEATURE = PackageManager.FEATURE_CONTROLS;
private static final String CARDS_ENABLED = Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED;
private static final String CARDS_AVAILABLE = Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE;
@ParameterizedRobolectricTestRunner.Parameters(
name = "ctrls available={0}, ctrls enabled={1}, cards available={2}, cards enabled={3}")
public static Collection data() {
return Arrays.asList(new Object[][]{
// controls available, controls enabled, cards available, cards enabled, available
{false, false, false, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
{false, false, false, true, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
{false, false, true, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
{false, false, true, true, BasePreferenceController.AVAILABLE},
{false, true, false, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
{false, true, false, true, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
{false, true, true, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
{false, true, true, true, BasePreferenceController.AVAILABLE},
{true, false, false, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
{true, false, false, true, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
{true, false, true, false, BasePreferenceController.DISABLED_DEPENDENT_SETTING},
{true, false, true, true, BasePreferenceController.AVAILABLE},
{true, true, false, false, BasePreferenceController.AVAILABLE},
{true, true, false, true, BasePreferenceController.AVAILABLE},
{true, true, true, false, BasePreferenceController.AVAILABLE},
{true, true, true, true, BasePreferenceController.AVAILABLE}
});
}
private Context mContext;
private PowerMenuPrivacyPreferenceController mController;
private ShadowPackageManager mShadowPackageManager;
@Mock
private LockPatternUtils mLockPatternUtils;
private boolean mControlsAvailable;
private boolean mControlsEnabled;
private boolean mCardsAvailable;
private boolean mCardsEnabled;
private int mAvailable;
public PowerMenuPrivacyPreferenceControllerAvailabilityTest(
boolean controlsAvailable,
boolean controlsEnabled,
boolean cardsAvailable,
boolean cardsEnabled,
int available) {
mControlsAvailable = controlsAvailable;
mControlsEnabled = controlsEnabled;
mCardsAvailable = cardsAvailable;
mCardsEnabled = cardsEnabled;
mAvailable = available;
}
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest();
// For these tests we assume the device has a secure lock.
when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
.thenReturn(mLockPatternUtils);
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
mController = new PowerMenuPrivacyPreferenceController(mContext, "TEST_KEY");
}
@Test
public void getAvailabilityStatus_possiblyAvailableAndEnabled() {
mShadowPackageManager.setSystemFeature(CONTROLS_FEATURE, mControlsAvailable);
ContentResolver cr = mContext.getContentResolver();
Settings.Secure.putInt(cr, CONTROLS_ENABLED, mControlsEnabled ? 1 : 0);
Settings.Secure.putInt(cr, CARDS_AVAILABLE, mCardsAvailable ? 1 : 0);
Settings.Secure.putInt(cr, CARDS_ENABLED, mCardsEnabled ? 1 : 0);
assertThat(mController.getAvailabilityStatus()).isEqualTo(mAvailable);
}
}

View File

@@ -26,6 +26,7 @@ import static org.mockito.Mockito.when;
import android.content.ContentResolver; import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager;
import android.provider.Settings; import android.provider.Settings;
import androidx.preference.Preference; import androidx.preference.Preference;
@@ -42,6 +43,8 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.shadows.ShadowPackageManager;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class PowerMenuPrivacyPreferenceControllerTest { public class PowerMenuPrivacyPreferenceControllerTest {
@@ -55,6 +58,7 @@ public class PowerMenuPrivacyPreferenceControllerTest {
private Context mContext; private Context mContext;
private ContentResolver mContentResolver; private ContentResolver mContentResolver;
private ShadowPackageManager mShadowPackageManager;
private PowerMenuPrivacyPreferenceController mController; private PowerMenuPrivacyPreferenceController mController;
@Mock @Mock
@@ -66,6 +70,8 @@ public class PowerMenuPrivacyPreferenceControllerTest {
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
mContentResolver = mContext.getContentResolver(); mContentResolver = mContext.getContentResolver();
FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest(); FakeFeatureFactory featureFactory = FakeFeatureFactory.setupForTest();
when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext)) when(featureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
@@ -119,82 +125,50 @@ public class PowerMenuPrivacyPreferenceControllerTest {
} }
@Test @Test
public void getSummary_cardsAvailable_isPower_menu_privacy_showString() { public void getSummary_cardsControlsAvailable_isPower_menu_privacy_showString() {
Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1); Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
assertThat(mController.getSummary()).isEqualTo( assertThat(mController.getSummary()).isEqualTo(
mContext.getText(R.string.power_menu_privacy_show)); mContext.getText(R.string.power_menu_privacy_show));
} }
@Test @Test
public void getSummary_cardsUnavailable_isPower_menu_privacy_show_controlsString() { public void
getSummary_cardsUnavailableControlsAvailable_isPower_menu_privacy_show_controlsString() {
Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 0); Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 0);
mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
assertThat(mController.getSummary()).isEqualTo( assertThat(mController.getSummary()).isEqualTo(
mContext.getText(R.string.power_menu_privacy_show_controls)); mContext.getText(R.string.power_menu_privacy_show_controls));
} }
@Test
public void
getSummary_cardsAvailableControlsUnavailable_isPower_menu_privacy_show_cardsString() {
Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, false);
assertThat(mController.getSummary()).isEqualTo(
mContext.getText(R.string.power_menu_privacy_show_cards));
}
@Test @Test
public void updateState_onPreferenceRefreshed_preferenceEnabledAndSummaryChanged() { public void updateState_onPreferenceRefreshed_preferenceEnabledAndSummaryChanged() {
mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
mController.updateState(mPreference); mController.updateState(mPreference);
verify(mPreference).setEnabled(anyBoolean()); verify(mPreference).setEnabled(anyBoolean());
verify(mPreference, atLeastOnce()).setSummary(mController.getSummary()); verify(mPreference, atLeastOnce()).setSummary(mController.getSummary());
} }
@Test
public void getAvailabilityStatus_allOff_returnsDisabled() {
Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
Settings.Secure.putInt(mContentResolver, CARDS_ENABLED_KEY, 0);
Settings.Secure.putInt(mContentResolver, CONTROLS_ENABLED_KEY, 0);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.DISABLED_DEPENDENT_SETTING);
}
@Test
public void getAvailabilityStatus_cardsUnavailableControlsOff_returnsDisabled() {
Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 0);
Settings.Secure.putInt(mContentResolver, CONTROLS_ENABLED_KEY, 0);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.DISABLED_DEPENDENT_SETTING);
}
@Test
public void testAvailabilityStatus_cardsOnControlsOff_returnsAvailable() {
Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
Settings.Secure.putInt(mContentResolver, CARDS_ENABLED_KEY, 1);
Settings.Secure.putInt(mContentResolver, CONTROLS_ENABLED_KEY, 0);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_cardsOffControlsOn_returnsAvailable() {
Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
Settings.Secure.putInt(mContentResolver, CARDS_ENABLED_KEY, 0);
Settings.Secure.putInt(mContentResolver, CONTROLS_ENABLED_KEY, 1);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE);
}
@Test
public void getAvailabilityStatus_allOn_returnsAvailable() {
Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
Settings.Secure.putInt(mContentResolver, CARDS_ENABLED_KEY, 1);
Settings.Secure.putInt(mContentResolver, CONTROLS_ENABLED_KEY, 1);
assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE);
}
@Test @Test
public void getAvailabilityStatus_allOnNotSecure_returnsDisabled() { public void getAvailabilityStatus_allOnNotSecure_returnsDisabled() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false); when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1); Settings.Secure.putInt(mContentResolver, CARDS_AVAILABLE_KEY, 1);
Settings.Secure.putInt(mContentResolver, CARDS_ENABLED_KEY, 1); Settings.Secure.putInt(mContentResolver, CARDS_ENABLED_KEY, 1);
Settings.Secure.putInt(mContentResolver, CONTROLS_ENABLED_KEY, 1); Settings.Secure.putInt(mContentResolver, CONTROLS_ENABLED_KEY, 1);
@@ -205,9 +179,8 @@ public class PowerMenuPrivacyPreferenceControllerTest {
@Test @Test
public void getAvailabilityStatus_controlsDeletedSecure_retursAvailable() { public void getAvailabilityStatus_controlsDeletedSecure_retursAvailable() {
when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
Settings.Secure.putString(mContentResolver, CONTROLS_ENABLED_KEY, null); Settings.Secure.putString(mContentResolver, CONTROLS_ENABLED_KEY, null);
mShadowPackageManager.setSystemFeature(PackageManager.FEATURE_CONTROLS, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo( assertThat(mController.getAvailabilityStatus()).isEqualTo(
BasePreferenceController.AVAILABLE); BasePreferenceController.AVAILABLE);