2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.display;
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Matchers.anyInt;
22 import static org.mockito.Matchers.anyString;
23 import static org.mockito.Matchers.eq;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import android.content.ContextWrapper;
29 import android.content.om.IOverlayManager;
30 import android.content.om.OverlayInfo;
31 import android.content.pm.ApplicationInfo;
32 import android.content.pm.PackageInfo;
33 import android.content.pm.PackageManager;
34 import android.support.test.InstrumentationRegistry;
35 import android.support.test.filters.SmallTest;
36 import android.support.test.runner.AndroidJUnit4;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.ArgumentCaptor;
43 import java.util.ArrayList;
45 import androidx.preference.ListPreference;
48 @RunWith(AndroidJUnit4.class)
49 public class ThemePreferenceControllerTest {
51 private IOverlayManager mMockOverlayManager;
52 private ContextWrapper mContext;
53 private ThemePreferenceController mPreferenceController;
54 private PackageManager mMockPackageManager;
58 mMockOverlayManager = mock(IOverlayManager.class);
59 mMockPackageManager = mock(PackageManager.class);
60 mContext = new ContextWrapper(InstrumentationRegistry.getTargetContext()) {
62 public PackageManager getPackageManager() {
63 return mMockPackageManager;
66 mPreferenceController = new ThemePreferenceController(mContext, mMockOverlayManager);
70 public void testUpdateState() throws Exception {
71 OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android",
72 OverlayInfo.CATEGORY_THEME, "", OverlayInfo.STATE_ENABLED, 0, 0, true);
73 OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android",
74 OverlayInfo.CATEGORY_THEME, "", 0, 0, 0, true);
75 when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
76 ApplicationInfo info = mock(ApplicationInfo.class);
77 if ("com.android.Theme1".equals(inv.getArguments()[0])) {
78 when(info.loadLabel(any())).thenReturn("Theme1");
80 when(info.loadLabel(any())).thenReturn("Theme2");
84 when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
86 when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt())).thenReturn(
88 ListPreference pref = mock(ListPreference.class);
89 mPreferenceController.updateState(pref);
90 ArgumentCaptor<String[]> arg = ArgumentCaptor.forClass(String[].class);
91 verify(pref).setEntries(arg.capture());
94 CharSequence[] entries = arg.getValue();
95 assertThat(entries).asList().containsExactly("Theme1", "Theme2");
97 verify(pref).setEntryValues(arg.capture());
98 CharSequence[] entryValues = arg.getValue();
99 assertThat(entryValues).asList().containsExactly(
100 "com.android.Theme1", "com.android.Theme2");
102 verify(pref).setValue(eq("com.android.Theme1"));
106 public void testUpdateState_withStaticOverlay() throws Exception {
107 OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android",
108 OverlayInfo.CATEGORY_THEME, "", OverlayInfo.STATE_ENABLED, 0, 0, true);
109 OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android",
110 OverlayInfo.CATEGORY_THEME, "", OverlayInfo.STATE_ENABLED, 0, 0, true);
111 when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
112 ApplicationInfo info = mock(ApplicationInfo.class);
113 if ("com.android.Theme1".equals(inv.getArguments()[0])) {
114 when(info.loadLabel(any())).thenReturn("Theme1");
116 when(info.loadLabel(any())).thenReturn("Theme2");
120 PackageInfo pi = mock(PackageInfo.class);
121 when(pi.isStaticOverlayPackage()).thenReturn(true);
122 when(mMockPackageManager.getPackageInfo(eq("com.android.Theme1"), anyInt())).thenReturn(pi);
123 when(mMockPackageManager.getPackageInfo(eq("com.android.Theme2"), anyInt())).thenReturn(
125 when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt())).thenReturn(
127 ListPreference pref = mock(ListPreference.class);
128 mPreferenceController.updateState(pref);
129 ArgumentCaptor<String[]> arg = ArgumentCaptor.forClass(String[].class);
130 verify(pref).setEntries(arg.capture());
133 CharSequence[] entries = arg.getValue();
134 assertThat(entries).asList().containsExactly("Theme2");
136 verify(pref).setEntryValues(arg.capture());
137 CharSequence[] entryValues = arg.getValue();
138 assertThat(entryValues).asList().containsExactly("com.android.Theme2");
140 verify(pref).setValue(eq("com.android.Theme2"));
143 private ArrayList<OverlayInfo> list(OverlayInfo... infos) {
144 ArrayList<OverlayInfo> list = new ArrayList<>();
145 for (OverlayInfo info : infos) {