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.util.ArraySet; 27 28 import androidx.preference.PreferenceScreen; 29 import androidx.preference.SwitchPreference; 30 import androidx.test.core.app.ApplicationProvider; 31 32 import com.android.settingslib.dream.DreamBackend; 33 34 import org.junit.After; 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Answers; 39 import org.mockito.Mock; 40 import org.mockito.MockitoAnnotations; 41 import org.robolectric.RobolectricTestRunner; 42 import org.robolectric.annotation.Config; 43 import org.robolectric.shadow.api.Shadow; 44 import org.robolectric.shadows.ShadowContentResolver; 45 import org.robolectric.shadows.ShadowSettings; 46 47 @RunWith(RobolectricTestRunner.class) 48 @Config(shadows = {ShadowSettings.ShadowSecure.class}) 49 public class DreamHomeControlsPreferenceControllerTest { 50 51 private Context mContext; 52 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 53 private PreferenceScreen mScreen; 54 private DreamHomeControlsPreferenceController mController; 55 private SwitchPreference mPreference; 56 private DreamBackend mBackend; 57 private ShadowContentResolver mShadowContentResolver; 58 59 @Before setUp()60 public void setUp() { 61 MockitoAnnotations.initMocks(this); 62 mContext = ApplicationProvider.getApplicationContext(); 63 mShadowContentResolver = Shadow.extract(mContext.getContentResolver()); 64 mBackend = new DreamBackend(mContext); 65 mController = new DreamHomeControlsPreferenceController(mContext, "key", mBackend); 66 mPreference = new SwitchPreference(mContext); 67 mPreference.setKey(mController.getPreferenceKey()); 68 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 69 mController.displayPreference(mScreen); 70 71 // Make home controls supported by default 72 mBackend.setSupportedComplications( 73 new ArraySet<>(new Integer[]{COMPLICATION_TYPE_HOME_CONTROLS})); 74 } 75 76 @After tearDown()77 public void tearDown() { 78 ShadowSettings.ShadowSecure.reset(); 79 } 80 81 @Test testSetChecked_setTrue_enablesSetting()82 public void testSetChecked_setTrue_enablesSetting() { 83 mBackend.setHomeControlsEnabled(false); 84 assertThat(mBackend.getEnabledComplications()) 85 .doesNotContain(COMPLICATION_TYPE_HOME_CONTROLS); 86 87 mController.setChecked(true); 88 assertThat(mBackend.getEnabledComplications()) 89 .contains(COMPLICATION_TYPE_HOME_CONTROLS); 90 } 91 92 @Test testSetChecked_setFalse_disablesSetting()93 public void testSetChecked_setFalse_disablesSetting() { 94 mBackend.setHomeControlsEnabled(true); 95 assertThat(mBackend.getEnabledComplications()) 96 .contains(COMPLICATION_TYPE_HOME_CONTROLS); 97 98 mController.setChecked(false); 99 assertThat(mBackend.getEnabledComplications()) 100 .doesNotContain(COMPLICATION_TYPE_HOME_CONTROLS); 101 } 102 103 @Test testIsChecked_returnsFalse()104 public void testIsChecked_returnsFalse() { 105 mBackend.setHomeControlsEnabled(false); 106 assertThat(mController.isChecked()).isFalse(); 107 } 108 109 @Test testIsChecked_returnsTrue()110 public void testIsChecked_returnsTrue() { 111 mBackend.setHomeControlsEnabled(true); 112 assertThat(mBackend.getEnabledComplications()) 113 .contains(COMPLICATION_TYPE_HOME_CONTROLS); 114 assertThat(mController.isChecked()).isTrue(); 115 } 116 } 117