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.Mockito.spy; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 import android.content.res.Resources; 26 27 import androidx.test.core.app.ApplicationProvider; 28 29 import com.android.settings.R; 30 import com.android.settings.core.BasePreferenceController; 31 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.robolectric.RobolectricTestRunner; 36 37 @RunWith(RobolectricTestRunner.class) 38 public class PowerMenuPreferenceControllerTest { 39 private Context mContext; 40 private Resources mResources; 41 private PowerMenuPreferenceController mController; 42 43 private static final String KEY_GESTURE_POWER_MENU = "gesture_power_menu"; 44 45 @Before setUp()46 public void setUp() { 47 mContext = spy(ApplicationProvider.getApplicationContext()); 48 mResources = spy(mContext.getResources()); 49 mController = new PowerMenuPreferenceController(mContext, KEY_GESTURE_POWER_MENU); 50 51 when(mContext.getResources()).thenReturn(mResources); 52 when(mResources.getBoolean( 53 com.android.internal.R.bool 54 .config_longPressOnPowerForAssistantSettingAvailable)) 55 .thenReturn(true); 56 when(mResources.getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior)) 57 .thenReturn(1); // Default to power menu 58 } 59 60 @Test getAvailabilityStatus_settingsAvailable_returnsAvailable()61 public void getAvailabilityStatus_settingsAvailable_returnsAvailable() { 62 when(mResources.getBoolean( 63 com.android.internal.R.bool 64 .config_longPressOnPowerForAssistantSettingAvailable)) 65 .thenReturn(true); 66 when(mResources.getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior)) 67 .thenReturn(1); // Default to power menu 68 69 assertThat(mController.getAvailabilityStatus()) 70 .isEqualTo(BasePreferenceController.AVAILABLE); 71 } 72 73 @Test getAvailabilityStatus_settingsNotAvailable_returnsNotAvailable()74 public void getAvailabilityStatus_settingsNotAvailable_returnsNotAvailable() { 75 when(mResources.getBoolean( 76 com.android.internal.R.bool 77 .config_longPressOnPowerForAssistantSettingAvailable)) 78 .thenReturn(false); 79 when(mResources.getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior)) 80 .thenReturn(1); // Default to power menu 81 82 assertThat(mController.getAvailabilityStatus()) 83 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE); 84 } 85 86 @Test getAvailabilityStatus_longPressPowerSettingNotAvailable_returnsNotAvailable()87 public void getAvailabilityStatus_longPressPowerSettingNotAvailable_returnsNotAvailable() { 88 when(mResources.getBoolean( 89 com.android.internal.R.bool 90 .config_longPressOnPowerForAssistantSettingAvailable)) 91 .thenReturn(true); 92 when(mResources.getInteger(com.android.internal.R.integer.config_longPressOnPowerBehavior)) 93 .thenReturn(3); // Default to power off (unsupported setup) 94 95 assertThat(mController.getAvailabilityStatus()) 96 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE); 97 } 98 99 @Test getSummary_longPressPowerToAssistant_returnsNotAvailable()100 public void getSummary_longPressPowerToAssistant_returnsNotAvailable() { 101 PowerMenuSettingsUtils.setLongPressPowerForAssistant(mContext); 102 103 assertThat(mController.getSummary().toString()) 104 .isEqualTo( 105 mContext.getString(R.string.power_menu_summary_long_press_for_assistant)); 106 } 107 108 @Test getSummary_longPressPowerToPowerMenu_returnsNotAvailable()109 public void getSummary_longPressPowerToPowerMenu_returnsNotAvailable() { 110 PowerMenuSettingsUtils.setLongPressPowerForPowerMenu(mContext); 111 112 assertThat(mController.getSummary().toString()) 113 .isEqualTo( 114 mContext.getString(R.string.power_menu_summary_long_press_for_power_menu)); 115 } 116 } 117