Add master toggle to enable/disable screen savers

The new control toggles the Settings.Secure.SCREENSAVER_ENABLED setting,
which controls if a screen saver is enabled. Also removed the "Never"
option from the WhenToDream picker, since this is now controlled via the
toggle.

Test: locally on device
Bug: 222360260
Change-Id: Ibcf52b5e2d1ad0c295f32c4c18bfc89082d074ce
This commit is contained in:
Lucas Silva
2022-03-03 17:39:50 -05:00
parent 3ccdcd433a
commit d9071dea25
11 changed files with 393 additions and 56 deletions

View File

@@ -0,0 +1,143 @@
/*
* Copyright (C) 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.dream;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.provider.Settings;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import com.android.settingslib.dream.DreamBackend;
import com.android.settingslib.widget.MainSwitchPreference;
import com.android.settingslib.widget.OnMainSwitchChangeListener;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowContentResolver;
import org.robolectric.shadows.ShadowSettings;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowSettings.ShadowSecure.class})
public class DreamMainSwitchPreferenceControllerTest {
private Context mContext;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PreferenceScreen mScreen;
private DreamMainSwitchPreferenceController mController;
private MainSwitchPreference mPreference;
private DreamBackend mBackend;
@Mock
private OnMainSwitchChangeListener mChangeListener;
private ShadowContentResolver mShadowContentResolver;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = ApplicationProvider.getApplicationContext();
mShadowContentResolver = Shadow.extract(mContext.getContentResolver());
mBackend = DreamBackend.getInstance(mContext);
mController = new DreamMainSwitchPreferenceController(mContext, "key");
mPreference = new MainSwitchPreference(mContext);
mPreference.setKey(mController.getPreferenceKey());
when(mScreen.findPreference(mPreference.getKey())).thenReturn(mPreference);
mController.displayPreference(mScreen);
}
@After
public void tearDown() {
ShadowSettings.ShadowSecure.reset();
mController.onStop();
}
@Test
public void testIsChecked_returnsFalse() {
mBackend.setEnabled(false);
assertThat(mController.isChecked()).isFalse();
}
@Test
public void testIsChecked_returnsTrue() {
mBackend.setEnabled(true);
assertThat(mController.isChecked()).isTrue();
}
@Test
public void testSetChecked_setFalse_disablesSetting() {
mBackend.setEnabled(true);
mController.setChecked(false);
assertThat(mBackend.isEnabled()).isFalse();
}
@Test
public void testSetChecked_setTrue_enablesSetting() {
mBackend.setEnabled(false);
mController.setChecked(true);
assertThat(mBackend.isEnabled()).isTrue();
}
@Test
public void testIsSliceable_returnsFalse() {
assertThat(mController.isSliceable()).isFalse();
}
@Test
public void testRegisterAndUnregister() {
mController.onStart();
assertThat(mShadowContentResolver.getContentObservers(
Settings.Secure.getUriFor(Settings.Secure.SCREENSAVER_ENABLED))).hasSize(1);
mController.onStop();
assertThat(mShadowContentResolver.getContentObservers(
Settings.Secure.getUriFor(Settings.Secure.SCREENSAVER_ENABLED))).isEmpty();
}
@Test
public void testUpdateState() {
mController.onStart();
mBackend.setEnabled(true);
triggerOnChangeListener();
assertThat(mPreference.isChecked()).isTrue();
mBackend.setEnabled(false);
triggerOnChangeListener();
assertThat(mPreference.isChecked()).isFalse();
mBackend.setEnabled(true);
triggerOnChangeListener();
assertThat(mPreference.isChecked()).isTrue();
}
private void triggerOnChangeListener() {
mShadowContentResolver.getContentObservers(
Settings.Secure.getUriFor(Settings.Secure.SCREENSAVER_ENABLED))
.forEach(contentObserver -> contentObserver.onChange(false));
}
}

View File

@@ -62,7 +62,8 @@ public class DreamPickerControllerTest {
}
private DreamPickerController buildController() {
final DreamPickerController controller = new DreamPickerController(mContext, mBackend);
final DreamPickerController controller = new DreamPickerController(mContext, "key",
mBackend);
controller.displayPreference(mScreen);
return controller;
}