1 /* 2 * Copyright (C) 2023 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.dream; 18 19 import static com.android.settingslib.dream.DreamBackend.COMPLICATION_TYPE_HOME_CONTROLS; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.provider.Settings; 27 import android.util.ArraySet; 28 29 import androidx.preference.PreferenceScreen; 30 import androidx.preference.SwitchPreference; 31 import androidx.test.core.app.ApplicationProvider; 32 33 import com.android.settingslib.dream.DreamBackend; 34 35 import org.junit.After; 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 import org.mockito.Answers; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.RobolectricTestRunner; 43 import org.robolectric.annotation.Config; 44 import org.robolectric.shadow.api.Shadow; 45 import org.robolectric.shadows.ShadowContentResolver; 46 import org.robolectric.shadows.ShadowSettings; 47 48 @RunWith(RobolectricTestRunner.class) 49 @Config(shadows = {ShadowSettings.ShadowSecure.class}) 50 public class DreamHomeControlsPreferenceControllerTest { 51 52 private Context mContext; 53 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 54 private PreferenceScreen mScreen; 55 private DreamHomeControlsPreferenceController mController; 56 private SwitchPreference mPreference; 57 private DreamBackend mBackend; 58 private ShadowContentResolver mShadowContentResolver; 59 60 @Before setUp()61 public void setUp() { 62 MockitoAnnotations.initMocks(this); 63 mContext = ApplicationProvider.getApplicationContext(); 64 mShadowContentResolver = Shadow.extract(mContext.getContentResolver()); 65 mBackend = new DreamBackend(mContext); 66 mController = new DreamHomeControlsPreferenceController(mContext, mBackend); 67 mPreference = new SwitchPreference(mContext); 68 mPreference.setKey(mController.getPreferenceKey()); 69 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 70 mController.displayPreference(mScreen); 71 72 // Make home controls supported by default 73 mBackend.setSupportedComplications( 74 new ArraySet<>(new Integer[]{COMPLICATION_TYPE_HOME_CONTROLS})); 75 } 76 77 @After tearDown()78 public void tearDown() { 79 ShadowSettings.ShadowSecure.reset(); 80 } 81 82 @Test testSetChecked_setTrue_enablesSetting()83 public void testSetChecked_setTrue_enablesSetting() { 84 setControlsEnabledOnLockscreen(true); 85 mBackend.setHomeControlsEnabled(false); 86 assertThat(mBackend.getEnabledComplications()) 87 .doesNotContain(COMPLICATION_TYPE_HOME_CONTROLS); 88 89 mController.setChecked(true); 90 assertThat(mBackend.getEnabledComplications()) 91 .contains(COMPLICATION_TYPE_HOME_CONTROLS); 92 } 93 94 @Test testSetChecked_setFalse_disablesSetting()95 public void testSetChecked_setFalse_disablesSetting() { 96 setControlsEnabledOnLockscreen(true); 97 mBackend.setHomeControlsEnabled(true); 98 assertThat(mBackend.getEnabledComplications()) 99 .contains(COMPLICATION_TYPE_HOME_CONTROLS); 100 101 mController.setChecked(false); 102 assertThat(mBackend.getEnabledComplications()) 103 .doesNotContain(COMPLICATION_TYPE_HOME_CONTROLS); 104 } 105 106 @Test testIsChecked_returnsFalse()107 public void testIsChecked_returnsFalse() { 108 setControlsEnabledOnLockscreen(true); 109 mBackend.setHomeControlsEnabled(false); 110 assertThat(mController.isChecked()).isFalse(); 111 } 112 113 @Test testIsChecked_returnsTrue()114 public void testIsChecked_returnsTrue() { 115 setControlsEnabledOnLockscreen(true); 116 mBackend.setHomeControlsEnabled(true); 117 assertThat(mBackend.getEnabledComplications()) 118 .contains(COMPLICATION_TYPE_HOME_CONTROLS); 119 assertThat(mController.isChecked()).isTrue(); 120 } 121 122 @Test testIsChecked_lockScreenDisabled_returnsFalse()123 public void testIsChecked_lockScreenDisabled_returnsFalse() { 124 setControlsEnabledOnLockscreen(false); 125 mBackend.setHomeControlsEnabled(true); 126 assertThat(mBackend.getEnabledComplications()) 127 .doesNotContain(COMPLICATION_TYPE_HOME_CONTROLS); 128 assertThat(mController.isChecked()).isFalse(); 129 } 130 setControlsEnabledOnLockscreen(boolean enabled)131 private void setControlsEnabledOnLockscreen(boolean enabled) { 132 Settings.Secure.putInt( 133 mContext.getContentResolver(), 134 Settings.Secure.LOCKSCREEN_SHOW_CONTROLS, 135 enabled ? 1 : 0); 136 } 137 } 138