• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.OverlayInfo;
30 import android.content.pm.ApplicationInfo;
31 import android.content.pm.PackageInfo;
32 import android.content.pm.PackageManager;
33 import android.support.test.InstrumentationRegistry;
34 import android.support.test.filters.SmallTest;
35 import android.support.test.runner.AndroidJUnit4;
36 import android.support.v7.preference.ListPreference;
37 
38 import com.android.settings.display.ThemePreferenceController;
39 import com.android.settings.display.ThemePreferenceController.OverlayManager;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.ArgumentCaptor;
45 
46 import java.util.ArrayList;
47 
48 @SmallTest
49 @RunWith(AndroidJUnit4.class)
50 public class ThemePreferenceControllerTest {
51 
52     private OverlayManager mMockOverlayManager;
53     private ContextWrapper mContext;
54     private ThemePreferenceController mPreferenceController;
55     private PackageManager mMockPackageManager;
56 
57     @Before
setup()58     public void setup() {
59         mMockOverlayManager = mock(OverlayManager.class);
60         mMockPackageManager = mock(PackageManager.class);
61         mContext = new ContextWrapper(InstrumentationRegistry.getTargetContext()) {
62             @Override
63             public PackageManager getPackageManager() {
64                 return mMockPackageManager;
65             }
66         };
67         mPreferenceController = new ThemePreferenceController(mContext, mMockOverlayManager);
68     }
69 
70     @Test
testUpdateState()71     public void testUpdateState() throws Exception {
72         OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android",
73                 "", OverlayInfo.STATE_ENABLED, 0);
74         OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android",
75                 "", 0, 0);
76         when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
77             ApplicationInfo info = mock(ApplicationInfo.class);
78             if ("com.android.Theme1".equals(inv.getArguments()[0])) {
79                 when(info.loadLabel(any())).thenReturn("Theme1");
80             } else {
81                 when(info.loadLabel(any())).thenReturn("Theme2");
82             }
83             return info;
84         });
85         when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
86                 new PackageInfo());
87         when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt())).thenReturn(
88                 list(info1, info2));
89         ListPreference pref = mock(ListPreference.class);
90         mPreferenceController.updateState(pref);
91         ArgumentCaptor<String[]> arg = ArgumentCaptor.forClass(String[].class);
92         verify(pref).setEntries(arg.capture());
93 
94 
95         CharSequence[] entries = arg.getValue();
96         assertThat(entries).asList().containsExactly("Theme1", "Theme2");
97 
98         verify(pref).setEntryValues(arg.capture());
99         CharSequence[] entryValues = arg.getValue();
100         assertThat(entryValues).asList().containsExactly(
101                 "com.android.Theme1", "com.android.Theme2");
102 
103         verify(pref).setValue(eq("com.android.Theme1"));
104     }
105 
106     @Test
testUpdateState_withStaticOverlay()107     public void testUpdateState_withStaticOverlay() throws Exception {
108         OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android",
109                 "", OverlayInfo.STATE_ENABLED, 0);
110         OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android",
111                 "", OverlayInfo.STATE_ENABLED, 0);
112         when(mMockPackageManager.getApplicationInfo(any(), anyInt())).thenAnswer(inv -> {
113             ApplicationInfo info = mock(ApplicationInfo.class);
114             if ("com.android.Theme1".equals(inv.getArguments()[0])) {
115                 when(info.loadLabel(any())).thenReturn("Theme1");
116             } else {
117                 when(info.loadLabel(any())).thenReturn("Theme2");
118             }
119             return info;
120         });
121         PackageInfo pi = new PackageInfo();
122         pi.isStaticOverlay = true;
123         when(mMockPackageManager.getPackageInfo(eq("com.android.Theme1"), anyInt())).thenReturn(pi);
124         when(mMockPackageManager.getPackageInfo(eq("com.android.Theme2"), anyInt())).thenReturn(
125                 new PackageInfo());
126         when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt())).thenReturn(
127                 list(info1, info2));
128         ListPreference pref = mock(ListPreference.class);
129         mPreferenceController.updateState(pref);
130         ArgumentCaptor<String[]> arg = ArgumentCaptor.forClass(String[].class);
131         verify(pref).setEntries(arg.capture());
132 
133 
134         CharSequence[] entries = arg.getValue();
135         assertThat(entries).asList().containsExactly("Theme2");
136 
137         verify(pref).setEntryValues(arg.capture());
138         CharSequence[] entryValues = arg.getValue();
139         assertThat(entryValues).asList().containsExactly("com.android.Theme2");
140 
141         verify(pref).setValue(eq("com.android.Theme2"));
142     }
143 
144     @Test
testAvailable_false()145     public void testAvailable_false() throws Exception {
146         when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
147                 new PackageInfo());
148         when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt()))
149                 .thenReturn(list(new OverlayInfo("", "", "", 0, 0)));
150         assertThat(mPreferenceController.isAvailable()).isFalse();
151     }
152 
153     @Test
testAvailable_true()154     public void testAvailable_true() throws Exception {
155         when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn(
156                  new PackageInfo());
157         when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt()))
158                 .thenReturn(list(new OverlayInfo("", "", "", 0, 0),
159                         new OverlayInfo("", "", "", 0, 0)));
160         assertThat(mPreferenceController.isAvailable()).isTrue();
161     }
162 
list(OverlayInfo... infos)163     private ArrayList<OverlayInfo> list(OverlayInfo... infos) {
164         ArrayList<OverlayInfo> list = new ArrayList<>();
165         for (int i = 0; i < infos.length; i++) {
166             list.add(infos[i]);
167         }
168         return list;
169     }
170 }
171