Update theme setting to not require reboot
Add calls into the OverlayManagerService to update the overlays directly and remove reboot dialog. Test: make RunSettingsRobotTests Change-Id: I92841e643eab70b4b194f60cce95ce3cedb75972 Fixes: 34701451
This commit is contained in:
@@ -14,95 +14,115 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.core;
|
||||
|
||||
import static junit.framework.TestCase.assertNotNull;
|
||||
package com.android.settings.display;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
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.content.om.OverlayInfo;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
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 com.android.settings.display.ThemePreferenceController.OverlayManager;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@SmallTest
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class ThemePreferenceControllerTest {
|
||||
|
||||
private UiModeManager mMockUiModeManager;
|
||||
private OverlayManager mMockOverlayManager;
|
||||
private ContextWrapper mContext;
|
||||
private ThemePreferenceController mPreferenceController;
|
||||
private PackageManager mMockPackageManager;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
mMockUiModeManager = mock(UiModeManager.class);
|
||||
mContext = new ContextWrapper(InstrumentationRegistry.getTargetContext()) {
|
||||
mMockOverlayManager = mock(OverlayManager.class);
|
||||
mMockPackageManager = mock(PackageManager.class);
|
||||
mContext = new ContextWrapper(InstrumentationRegistry.getContext()) {
|
||||
@Override
|
||||
public Object getSystemService(String name) {
|
||||
if (Context.UI_MODE_SERVICE.equals(name)) {
|
||||
return mMockUiModeManager;
|
||||
}
|
||||
return super.getSystemService(name);
|
||||
public PackageManager getPackageManager() {
|
||||
return mMockPackageManager;
|
||||
}
|
||||
};
|
||||
mPreferenceController = new ThemePreferenceController(mContext);
|
||||
mPreferenceController = new ThemePreferenceController(mContext, mMockOverlayManager);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateState() {
|
||||
when(mMockUiModeManager.getAvailableThemes()).thenReturn(new String[] {
|
||||
null,
|
||||
"Theme1",
|
||||
"Theme2",
|
||||
public void testUpdateState() throws Exception {
|
||||
OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android",
|
||||
"", OverlayInfo.STATE_ENABLED, 0);
|
||||
OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android",
|
||||
"", 0, 0);
|
||||
when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
|
||||
ApplicationInfo info = mock(ApplicationInfo.class);
|
||||
if ("com.android.Theme1".equals(inv.getArguments()[0])) {
|
||||
when(info.loadLabel(any())).thenReturn("Theme1");
|
||||
} else {
|
||||
when(info.loadLabel(any())).thenReturn("Theme2");
|
||||
}
|
||||
return info;
|
||||
});
|
||||
when(mMockUiModeManager.getTheme()).thenReturn("Theme1");
|
||||
when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt())).thenReturn(
|
||||
list(info1, info2));
|
||||
ListPreference pref = mock(ListPreference.class);
|
||||
mPreferenceController.updateState(pref);
|
||||
ArgumentCaptor<String[]> 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]);
|
||||
|
||||
CharSequence[] entries = arg.getValue();
|
||||
assertEquals(2, entries.length);
|
||||
assertEquals("Theme1", entries[0]);
|
||||
assertEquals("Theme2", entries[1]);
|
||||
|
||||
verify(pref).setEntryValues(arg.capture());
|
||||
String[] entryValues = arg.getValue();
|
||||
assertEquals(3, entryValues.length);
|
||||
assertNotNull(entryValues[0]);
|
||||
assertEquals("Theme1", entryValues[1]);
|
||||
assertEquals("Theme2", entryValues[2]);
|
||||
CharSequence[] entryValues = arg.getValue();
|
||||
assertEquals("com.android.Theme1", entryValues[0]);
|
||||
assertEquals("com.android.Theme2", entryValues[1]);
|
||||
|
||||
verify(pref).setValue(eq("Theme1"));
|
||||
verify(pref).setValue(eq("com.android.Theme1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAvailable_false() {
|
||||
when(mMockUiModeManager.getAvailableThemes()).thenReturn(new String[1]);
|
||||
public void testAvailable_false() throws Exception {
|
||||
when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt()))
|
||||
.thenReturn(list(new OverlayInfo("", "", "", 0, 0)));
|
||||
assertFalse(mPreferenceController.isAvailable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAvailable_true() {
|
||||
when(mMockUiModeManager.getAvailableThemes()).thenReturn(new String[2]);
|
||||
public void testAvailable_true() throws Exception {
|
||||
when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt()))
|
||||
.thenReturn(list(new OverlayInfo("", "", "", 0, 0),
|
||||
new OverlayInfo("", "", "", 0, 0)));
|
||||
assertTrue(mPreferenceController.isAvailable());
|
||||
}
|
||||
|
||||
private ArrayList<OverlayInfo> list(OverlayInfo... infos) {
|
||||
ArrayList<OverlayInfo> list = new ArrayList<>();
|
||||
for (int i = 0; i < infos.length; i++) {
|
||||
list.add(infos[i]);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user