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