From 6324cffc7e13223a0b26dce52215b65c3216492a Mon Sep 17 00:00:00 2001 From: Jason Monk Date: Wed, 25 Jan 2017 10:34:29 -0500 Subject: [PATCH] Move theme setting out of tuner to display Test: runtest --path packages/apps/Settings/tests/unit/src/com/android/settings/display/ Fixes: 34682466 Change-Id: Ifc21e390bf8441308871d487bd60d59826603ec2 --- res/values/strings.xml | 7 ++ res/xml/ia_display_settings.xml | 5 + src/com/android/settings/DisplaySettings.java | 3 + .../display/ThemePreferenceController.java | 112 ++++++++++++++++++ .../ThemePreferenceControllerTest.java | 108 +++++++++++++++++ 5 files changed, 235 insertions(+) create mode 100644 src/com/android/settings/display/ThemePreferenceController.java create mode 100644 tests/unit/src/com/android/settings/display/ThemePreferenceControllerTest.java diff --git a/res/values/strings.xml b/res/values/strings.xml index 0e572a6615b..818ff9f1b93 100644 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -8150,4 +8150,11 @@ Make %1$s your autofill app? %1$s will be able to read your screen and fill fields in other apps. + + Device theme + + Default + + Changing the theme requires a restart. + diff --git a/res/xml/ia_display_settings.xml b/res/xml/ia_display_settings.xml index 946466b502f..ed026f10ff9 100644 --- a/res/xml/ia_display_settings.xml +++ b/res/xml/ia_display_settings.xml @@ -105,6 +105,11 @@ android:title="@string/tap_to_wake" android:summary="@string/tap_to_wake_summary" /> + + { + mUiModeManager.setTheme(defaultToNull((String) newValue)); + ((ListPreference) preference).setValue((String) newValue); + }; + new AlertDialog.Builder(mContext) + .setTitle(R.string.change_theme_reboot) + .setPositiveButton(com.android.internal.R.string.global_action_restart, onConfirm) + .setNegativeButton(android.R.string.cancel, null) + .show(); + return false; + } + + @Override + public boolean isAvailable() { + String[] themes = mUiModeManager.getAvailableThemes(); + return themes != null && themes.length > 1; + } + + private String nullToDefault(String input) { + if (input == null) { + return mContext.getString(R.string.default_theme); + } + return input; + } + + private String defaultToNull(String input) { + if (mContext.getString(R.string.default_theme).equals(input)) { + return null; + } + return input; + } +} diff --git a/tests/unit/src/com/android/settings/display/ThemePreferenceControllerTest.java b/tests/unit/src/com/android/settings/display/ThemePreferenceControllerTest.java new file mode 100644 index 00000000000..231787e5e12 --- /dev/null +++ b/tests/unit/src/com/android/settings/display/ThemePreferenceControllerTest.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2017 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.core; + +import static junit.framework.TestCase.assertNotNull; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.app.UiModeManager; +import android.content.Context; +import android.content.ContextWrapper; +import android.support.test.InstrumentationRegistry; +import android.support.test.filters.SmallTest; +import android.support.test.runner.AndroidJUnit4; +import android.support.v7.preference.ListPreference; + +import com.android.settings.R; +import com.android.settings.display.ThemePreferenceController; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; + +@SmallTest +@RunWith(AndroidJUnit4.class) +public class ThemePreferenceControllerTest { + + private UiModeManager mMockUiModeManager; + private ContextWrapper mContext; + private ThemePreferenceController mPreferenceController; + + @Before + public void setup() { + mMockUiModeManager = mock(UiModeManager.class); + mContext = new ContextWrapper(InstrumentationRegistry.getTargetContext()) { + @Override + public Object getSystemService(String name) { + if (Context.UI_MODE_SERVICE.equals(name)) { + return mMockUiModeManager; + } + return super.getSystemService(name); + } + }; + mPreferenceController = new ThemePreferenceController(mContext); + } + + @Test + public void testUpdateState() { + when(mMockUiModeManager.getAvailableThemes()).thenReturn(new String[] { + null, + "Theme1", + "Theme2", + }); + when(mMockUiModeManager.getTheme()).thenReturn("Theme1"); + ListPreference pref = mock(ListPreference.class); + mPreferenceController.updateState(pref); + ArgumentCaptor arg = ArgumentCaptor.forClass(String[].class); + verify(pref).setEntries(arg.capture()); + + String[] entries = arg.getValue(); + assertEquals(3, entries.length); + assertNotNull(entries[0]); + assertEquals("Theme1", entries[1]); + assertEquals("Theme2", entries[2]); + + verify(pref).setEntryValues(arg.capture()); + String[] entryValues = arg.getValue(); + assertEquals(3, entryValues.length); + assertNotNull(entryValues[0]); + assertEquals("Theme1", entryValues[1]); + assertEquals("Theme2", entryValues[2]); + + verify(pref).setValue(eq("Theme1")); + } + + @Test + public void testAvailable_false() { + when(mMockUiModeManager.getAvailableThemes()).thenReturn(new String[1]); + assertFalse(mPreferenceController.isAvailable()); + } + + @Test + public void testAvailable_true() { + when(mMockUiModeManager.getAvailableThemes()).thenReturn(new String[2]); + assertTrue(mPreferenceController.isAvailable()); + } +}