Merge "Custom dark theme scheduling"

This commit is contained in:
Jay Aliomer
2020-01-21 21:27:29 +00:00
committed by Android (Google) Code Review
12 changed files with 448 additions and 30 deletions

View File

@@ -32,7 +32,10 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import java.util.Locale;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
@@ -60,9 +63,12 @@ public class DarkModeActivationPreferenceControllerTest {
private Button mTurnOnButton;
@Mock
private PowerManager mPM;
@Mock
private TimeFormatter mFormat;
private Configuration configNightYes = new Configuration();
private Configuration configNightNo = new Configuration();;
private Configuration mConfigNightYes = new Configuration();
private Configuration mConfigNightNo = new Configuration();
private Locale mLocal = new Locale("ENG");
@Before
public void setUp() {
@@ -77,6 +83,7 @@ public class DarkModeActivationPreferenceControllerTest {
when(mPreference.findViewById(
eq(R.id.dark_ui_turn_off_button))).thenReturn(mTurnOffButton);
when(mService.setNightModeActivated(anyBoolean())).thenReturn(true);
when(mFormat.of(any())).thenReturn("10:00 AM");
when(mContext.getString(
R.string.dark_ui_activation_off_auto)).thenReturn("off_auto");
when(mContext.getString(
@@ -93,16 +100,22 @@ public class DarkModeActivationPreferenceControllerTest {
R.string.dark_ui_summary_off_auto_mode_never)).thenReturn("summary_off_manual");
when(mContext.getString(
R.string.dark_ui_summary_on_auto_mode_never)).thenReturn("summary_on_manual");
mController = new DarkModeActivationPreferenceController(mContext, mPreferenceKey);
when(mContext.getString(
R.string.dark_ui_summary_on_auto_mode_custom)).thenReturn("summary_on_custom");
when(mContext.getString(
R.string.dark_ui_summary_off_auto_mode_custom)).thenReturn("summary_off_custom");
mController = new DarkModeActivationPreferenceController(mContext, mPreferenceKey, mFormat);
mController.displayPreference(mScreen);
configNightNo.uiMode = Configuration.UI_MODE_NIGHT_NO;
configNightYes.uiMode = Configuration.UI_MODE_NIGHT_YES;
mConfigNightNo.uiMode = Configuration.UI_MODE_NIGHT_NO;
mConfigNightYes.uiMode = Configuration.UI_MODE_NIGHT_YES;
mConfigNightNo.locale = mLocal;
mConfigNightYes.locale = mLocal;
}
@Test
public void nightMode_toggleButton_offManual() {
when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
when(res.getConfiguration()).thenReturn(configNightYes);
when(res.getConfiguration()).thenReturn(mConfigNightYes);
mController.updateState(mPreference);
@@ -112,11 +125,37 @@ public class DarkModeActivationPreferenceControllerTest {
R.string.dark_ui_activation_off_manual)));
}
@Test
public void nightMode_toggleButton_offCustom() {
when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_CUSTOM);
when(res.getConfiguration()).thenReturn(mConfigNightYes);
mController.updateState(mPreference);
verify(mTurnOnButton).setVisibility(eq(View.GONE));
verify(mTurnOffButton).setVisibility(eq(View.VISIBLE));
verify(mTurnOffButton).setText(eq(mContext.getString(
R.string.dark_ui_activation_off_custom)));
}
@Test
public void nightMode_toggleButton_onCustom() {
when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_CUSTOM);
when(res.getConfiguration()).thenReturn(mConfigNightYes);
mController.updateState(mPreference);
verify(mTurnOnButton).setVisibility(eq(View.GONE));
verify(mTurnOffButton).setVisibility(eq(View.VISIBLE));
verify(mTurnOffButton).setText(eq(mContext.getString(
R.string.dark_ui_activation_on_custom)));
}
@Test
public void nightMode_toggleButton_onAutoWhenModeIsYes() {
when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
when(res.getConfiguration()).thenReturn(configNightNo);
when(res.getConfiguration()).thenReturn(mConfigNightNo);
mController.updateState(mPreference);
@@ -129,7 +168,7 @@ public class DarkModeActivationPreferenceControllerTest {
@Test
public void nightMode_toggleButton_onAutoWhenModeIsAuto() {
when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_AUTO);
when(res.getConfiguration()).thenReturn(configNightNo);
when(res.getConfiguration()).thenReturn(mConfigNightNo);
mController.updateState(mPreference);
@@ -142,7 +181,7 @@ public class DarkModeActivationPreferenceControllerTest {
@Test
public void nightModeSummary_buttonText_onManual() {
when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_NO);
when(res.getConfiguration()).thenReturn(configNightYes);
when(res.getConfiguration()).thenReturn(mConfigNightYes);
assertEquals(mController.getSummary(), mContext.getString(
R.string.dark_ui_summary_on_auto_mode_never));
@@ -151,7 +190,7 @@ public class DarkModeActivationPreferenceControllerTest {
@Test
public void nightModeSummary_buttonText_offAuto() {
when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_AUTO);
when(res.getConfiguration()).thenReturn(configNightNo);
when(res.getConfiguration()).thenReturn(mConfigNightNo);
assertEquals(mController.getSummary(), mContext.getString(
R.string.dark_ui_summary_off_auto_mode_auto));

View File

@@ -0,0 +1,91 @@
/*
* 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.display.darkmode;
import android.app.UiModeManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import androidx.preference.Preference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(RobolectricTestRunner.class)
public class DarkModeCustomPreferenceControllerTest {
private DarkModeCustomPreferenceController mController;
@Mock
private UiModeManager mService;
@Mock
private Context mContext;
@Mock
private Preference mPreference;
@Mock
private Resources mResources;
@Mock
private ContentResolver mCR;
@Mock
private TimeFormatter mFormat;
@Mock
private DarkModeSettingsFragment mFragment;
private Configuration mConfig = new Configuration();
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mContext.getResources()).thenReturn(mResources);
when(mResources.getConfiguration()).thenReturn(mConfig);
when(mContext.getContentResolver()).thenReturn(mCR);
mService = mock(UiModeManager.class);
when(mContext.getResources()).thenReturn(mResources);
when(mContext.getSystemService(UiModeManager.class)).thenReturn(mService);
mController = new DarkModeCustomPreferenceController(mContext, "key", mFragment, mFormat);
when(mFormat.is24HourFormat()).thenReturn(false);
when(mFormat.of(any())).thenReturn("10:00 AM");
}
@Test
public void nightMode_customOff_hidePreference() {
when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_YES);
mController.refreshSummary(mPreference);
verify(mPreference).setVisible(eq(false));
}
@Test
public void nightMode_customOff_showPreference() {
when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_CUSTOM);
mController.refreshSummary(mPreference);
verify(mPreference).setVisible(eq(true));
}
@Test
public void nightMode_customOff_setSummaryNotNull() {
when(mService.getNightMode()).thenReturn(UiModeManager.MODE_NIGHT_CUSTOM);
mController.refreshSummary(mPreference);
verify(mPreference).setSummary(eq(mFormat.of(null)));
}
}

View File

@@ -65,6 +65,7 @@ public class DarkModeScheduleSelectorControllerTest {
when(mContext.getSystemService(PowerManager.class)).thenReturn(mPM);
when(mContext.getString(R.string.dark_ui_auto_mode_never)).thenReturn("never");
when(mContext.getString(R.string.dark_ui_auto_mode_auto)).thenReturn("auto");
when(mContext.getString(R.string.dark_ui_auto_mode_custom)).thenReturn("custom");
mPreference = spy(new DropDownPreference(mContext));
mPreference.setEntryValues(new CharSequence[]{
mContext.getString(R.string.dark_ui_auto_mode_never),