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.wrapper.OverlayManagerWrapper; 39 40 import org.junit.Before; 41 import org.junit.Test; 42 import org.junit.runner.RunWith; 43 import org.mockito.ArgumentCaptor; 44 45 import java.util.ArrayList; 46 47 @SmallTest 48 @RunWith(AndroidJUnit4.class) 49 public class ThemePreferenceControllerTest { 50 51 private OverlayManagerWrapper mMockOverlayManager; 52 private ContextWrapper mContext; 53 private ThemePreferenceController mPreferenceController; 54 private PackageManager mMockPackageManager; 55 56 @Before setup()57 public void setup() { 58 mMockOverlayManager = mock(OverlayManagerWrapper.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 testUpdateState()70 public void testUpdateState() throws Exception { 71 OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android", 72 "", "", OverlayInfo.STATE_ENABLED, 0, 0, true); 73 OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android", 74 "", "", 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 testUpdateState_withStaticOverlay()106 public void testUpdateState_withStaticOverlay() throws Exception { 107 OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android", 108 "", "", OverlayInfo.STATE_ENABLED, 0, 0, true); 109 OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android", 110 "", "", 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 @Test testAvailable_false()144 public void testAvailable_false() throws Exception { 145 when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn( 146 new PackageInfo()); 147 when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt())) 148 .thenReturn(list(new OverlayInfo("", "", "", "", 0, 0, 0, false))); 149 assertThat(mPreferenceController.isAvailable()).isFalse(); 150 } 151 152 @Test testAvailable_true()153 public void testAvailable_true() throws Exception { 154 when(mMockPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn( 155 new PackageInfo()); 156 when(mMockOverlayManager.getOverlayInfosForTarget(any(), anyInt())) 157 .thenReturn(list(new OverlayInfo("", "", "", "", 0, 0, 0, true), 158 new OverlayInfo("", "", "", "", 0, 0, 0, true))); 159 assertThat(mPreferenceController.isAvailable()).isTrue(); 160 } 161 list(OverlayInfo... infos)162 private ArrayList<OverlayManagerWrapper.OverlayInfo> list(OverlayInfo... infos) { 163 ArrayList<OverlayManagerWrapper.OverlayInfo> list = new ArrayList<>(); 164 for (OverlayInfo info : infos) { 165 list.add(new OverlayManagerWrapper.OverlayInfo(info)); 166 } 167 return list; 168 } 169 } 170