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.dream; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Mockito.when; 22 23 import android.content.Context; 24 import android.provider.Settings; 25 26 import androidx.preference.PreferenceScreen; 27 import androidx.test.core.app.ApplicationProvider; 28 29 import com.android.settingslib.dream.DreamBackend; 30 import com.android.settingslib.widget.MainSwitchPreference; 31 32 import org.junit.After; 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Answers; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.RobolectricTestRunner; 40 import org.robolectric.annotation.Config; 41 import org.robolectric.shadow.api.Shadow; 42 import org.robolectric.shadows.ShadowContentResolver; 43 import org.robolectric.shadows.ShadowSettings; 44 45 @RunWith(RobolectricTestRunner.class) 46 @Config(shadows = {ShadowSettings.ShadowSecure.class}) 47 public class DreamMainSwitchPreferenceControllerTest { 48 49 private Context mContext; 50 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 51 private PreferenceScreen mScreen; 52 private DreamMainSwitchPreferenceController mController; 53 private MainSwitchPreference mPreference; 54 private DreamBackend mBackend; 55 private ShadowContentResolver mShadowContentResolver; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mContext = ApplicationProvider.getApplicationContext(); 61 mShadowContentResolver = Shadow.extract(mContext.getContentResolver()); 62 mBackend = DreamBackend.getInstance(mContext); 63 mController = new DreamMainSwitchPreferenceController(mContext, "key"); 64 mPreference = new MainSwitchPreference(mContext); 65 mPreference.setKey(mController.getPreferenceKey()); 66 when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference); 67 mController.displayPreference(mScreen); 68 } 69 70 @After tearDown()71 public void tearDown() { 72 ShadowSettings.ShadowSecure.reset(); 73 mController.onStop(); 74 } 75 76 @Test testIsChecked_returnsFalse()77 public void testIsChecked_returnsFalse() { 78 mBackend.setEnabled(false); 79 assertThat(mController.isChecked()).isFalse(); 80 } 81 82 @Test testIsChecked_returnsTrue()83 public void testIsChecked_returnsTrue() { 84 mBackend.setEnabled(true); 85 assertThat(mController.isChecked()).isTrue(); 86 } 87 88 @Test testSetChecked_setFalse_disablesSetting()89 public void testSetChecked_setFalse_disablesSetting() { 90 mBackend.setEnabled(true); 91 mController.setChecked(false); 92 assertThat(mBackend.isEnabled()).isFalse(); 93 } 94 95 @Test testSetChecked_setTrue_enablesSetting()96 public void testSetChecked_setTrue_enablesSetting() { 97 mBackend.setEnabled(false); 98 mController.setChecked(true); 99 assertThat(mBackend.isEnabled()).isTrue(); 100 } 101 102 @Test testIsSliceable_returnsFalse()103 public void testIsSliceable_returnsFalse() { 104 assertThat(mController.isSliceable()).isFalse(); 105 } 106 107 @Test testRegisterAndUnregister()108 public void testRegisterAndUnregister() { 109 mController.onStart(); 110 assertThat(mShadowContentResolver.getContentObservers( 111 Settings.Secure.getUriFor(Settings.Secure.SCREENSAVER_ENABLED))).hasSize(1); 112 113 mController.onStop(); 114 assertThat(mShadowContentResolver.getContentObservers( 115 Settings.Secure.getUriFor(Settings.Secure.SCREENSAVER_ENABLED))).isEmpty(); 116 } 117 118 @Test testUpdateState()119 public void testUpdateState() { 120 mController.onStart(); 121 122 mBackend.setEnabled(true); 123 triggerOnChangeListener(); 124 assertThat(mPreference.isChecked()).isTrue(); 125 126 mBackend.setEnabled(false); 127 triggerOnChangeListener(); 128 assertThat(mPreference.isChecked()).isFalse(); 129 130 mBackend.setEnabled(true); 131 triggerOnChangeListener(); 132 assertThat(mPreference.isChecked()).isTrue(); 133 } 134 triggerOnChangeListener()135 private void triggerOnChangeListener() { 136 mShadowContentResolver.getContentObservers( 137 Settings.Secure.getUriFor(Settings.Secure.SCREENSAVER_ENABLED)) 138 .forEach(contentObserver -> contentObserver.onChange(false)); 139 } 140 } 141