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.annotation.StringRes; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.pm.PackageManager; 25 import android.provider.Settings; 26 27 import com.android.settings.R; 28 29 import org.junit.Before; 30 import org.junit.Test; 31 import org.junit.runner.RunWith; 32 import org.robolectric.ParameterizedRobolectricTestRunner; 33 import org.robolectric.RuntimeEnvironment; 34 import org.robolectric.Shadows; 35 import org.robolectric.shadows.ShadowPackageManager; 36 37 import java.util.Arrays; 38 import java.util.Collection; 39 40 @RunWith(ParameterizedRobolectricTestRunner.class) 41 public class PowerMenuPreferenceControllerSummaryTest { 42 43 private static final String KEY_GESTURE_POWER_MENU = "gesture_power_menu"; 44 private static final String CONTROLS_ENABLED = Settings.Secure.CONTROLS_ENABLED; 45 private static final String CONTROLS_FEATURE = PackageManager.FEATURE_CONTROLS; 46 private static final String CARDS_ENABLED = Settings.Secure.GLOBAL_ACTIONS_PANEL_ENABLED; 47 private static final String CARDS_AVAILABLE = Settings.Secure.GLOBAL_ACTIONS_PANEL_AVAILABLE; 48 49 @ParameterizedRobolectricTestRunner.Parameters( 50 name = "ctrls available={0}, ctrls enabled={1}, cards available={2}, cards enabled={3}") data()51 public static Collection data() { 52 return Arrays.asList(new Object[][]{ 53 // controls available, controls enabled, cards available, cards enabled, summary 54 {false, false, false, false, R.string.power_menu_none}, 55 {false, false, false, true, R.string.power_menu_none}, 56 {false, false, true, false, R.string.power_menu_none}, 57 {false, false, true, true, R.string.power_menu_cards_passes}, 58 {false, true, false, false, R.string.power_menu_none}, 59 {false, true, false, true, R.string.power_menu_none}, 60 {false, true, true, false, R.string.power_menu_none}, 61 {false, true, true, true, R.string.power_menu_cards_passes}, 62 {true, false, false, false, R.string.power_menu_none}, 63 {true, false, false, true, R.string.power_menu_none}, 64 {true, false, true, false, R.string.power_menu_none}, 65 {true, false, true, true, R.string.power_menu_cards_passes}, 66 {true, true, false, false, R.string.power_menu_device_controls}, 67 {true, true, false, true, R.string.power_menu_device_controls}, 68 {true, true, true, false, R.string.power_menu_device_controls}, 69 {true, true, true, true, R.string.power_menu_cards_passes_device_controls} 70 }); 71 } 72 73 private Context mContext; 74 private PowerMenuPreferenceController mController; 75 private ShadowPackageManager mShadowPackageManager; 76 77 private boolean mControlsAvailable; 78 private boolean mControlsEnabled; 79 private boolean mCardsAvailable; 80 private boolean mCardsEnabled; 81 private @StringRes int mSummaryRes; 82 PowerMenuPreferenceControllerSummaryTest( boolean controlsAvailable, boolean controlsEnabled, boolean cardsAvailable, boolean cardsEnabled, @StringRes int summaryRes)83 public PowerMenuPreferenceControllerSummaryTest( 84 boolean controlsAvailable, 85 boolean controlsEnabled, 86 boolean cardsAvailable, 87 boolean cardsEnabled, 88 @StringRes int summaryRes) { 89 mControlsAvailable = controlsAvailable; 90 mControlsEnabled = controlsEnabled; 91 mCardsAvailable = cardsAvailable; 92 mCardsEnabled = cardsEnabled; 93 mSummaryRes = summaryRes; 94 } 95 96 @Before setUp()97 public void setUp() { 98 mContext = RuntimeEnvironment.application; 99 mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager()); 100 mController = new PowerMenuPreferenceController(mContext, KEY_GESTURE_POWER_MENU); 101 } 102 103 @Test getSummary_possiblyAvailableAndEnabled()104 public void getSummary_possiblyAvailableAndEnabled() { 105 mShadowPackageManager.setSystemFeature(CONTROLS_FEATURE, mControlsAvailable); 106 ContentResolver cr = mContext.getContentResolver(); 107 Settings.Secure.putInt(cr, CONTROLS_ENABLED, mControlsEnabled ? 1 : 0); 108 Settings.Secure.putInt(cr, CARDS_AVAILABLE, mCardsAvailable ? 1 : 0); 109 Settings.Secure.putInt(cr, CARDS_ENABLED, mCardsEnabled ? 1 : 0); 110 111 assertThat(mController.getSummary()).isEqualTo(mContext.getText(mSummaryRes)); 112 } 113 } 114