OSDN Git Service

Remove OverlayManagerWrapper class out from Settings
[android-x86/packages-apps-Settings.git] / tests / unit / src / com / android / settings / display / ThemePreferenceControllerTest.java
1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.android.settings.display;
18
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;
27
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;
37
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.ArgumentCaptor;
42
43 import java.util.ArrayList;
44
45 import androidx.preference.ListPreference;
46
47 @SmallTest
48 @RunWith(AndroidJUnit4.class)
49 public class ThemePreferenceControllerTest {
50
51     private IOverlayManager mMockOverlayManager;
52     private ContextWrapper mContext;
53     private ThemePreferenceController mPreferenceController;
54     private PackageManager mMockPackageManager;
55
56     @Before
57     public void setup() {
58         mMockOverlayManager = mock(IOverlayManager.class);
59         mMockPackageManager = mock(PackageManager.class);
60         mContext = new ContextWrapper(InstrumentationRegistry.getTargetContext()) {
61             @Override
62             public PackageManager getPackageManager() {
63                 return mMockPackageManager;
64             }
65         };
66         mPreferenceController = new ThemePreferenceController(mContext, mMockOverlayManager);
67     }
68
69     @Test
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");
79             } else {
80                 when(info.loadLabel(any())).thenReturn("Theme2");
81             }
82             return info;
83         });
84         when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
85                 new PackageInfo());
86         when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt())).thenReturn(
87                 list(info1, info2));
88         ListPreference pref = mock(ListPreference.class);
89         mPreferenceController.updateState(pref);
90         ArgumentCaptor<String[]> arg = ArgumentCaptor.forClass(String[].class);
91         verify(pref).setEntries(arg.capture());
92
93
94         CharSequence[] entries = arg.getValue();
95         assertThat(entries).asList().containsExactly("Theme1", "Theme2");
96
97         verify(pref).setEntryValues(arg.capture());
98         CharSequence[] entryValues = arg.getValue();
99         assertThat(entryValues).asList().containsExactly(
100                 "com.android.Theme1", "com.android.Theme2");
101
102         verify(pref).setValue(eq("com.android.Theme1"));
103     }
104
105     @Test
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");
115             } else {
116                 when(info.loadLabel(any())).thenReturn("Theme2");
117             }
118             return info;
119         });
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(
124                 new PackageInfo());
125         when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt())).thenReturn(
126                 list(info1, info2));
127         ListPreference pref = mock(ListPreference.class);
128         mPreferenceController.updateState(pref);
129         ArgumentCaptor<String[]> arg = ArgumentCaptor.forClass(String[].class);
130         verify(pref).setEntries(arg.capture());
131
132
133         CharSequence[] entries = arg.getValue();
134         assertThat(entries).asList().containsExactly("Theme2");
135
136         verify(pref).setEntryValues(arg.capture());
137         CharSequence[] entryValues = arg.getValue();
138         assertThat(entryValues).asList().containsExactly("com.android.Theme2");
139
140         verify(pref).setValue(eq("com.android.Theme2"));
141     }
142
143     private ArrayList<OverlayInfo> list(OverlayInfo... infos) {
144         ArrayList<OverlayInfo> list = new ArrayList<>();
145         for (OverlayInfo info : infos) {
146             list.add(info);
147         }
148         return list;
149     }
150 }