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 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.anyInt; 23 import static org.mockito.ArgumentMatchers.anyString; 24 import static org.mockito.Mockito.doReturn; 25 import static org.mockito.Mockito.spy; 26 import static org.mockito.Mockito.verify; 27 import static org.mockito.Mockito.when; 28 29 import android.content.Context; 30 import android.content.om.IOverlayManager; 31 import android.content.om.OverlayInfo; 32 import android.content.pm.ApplicationInfo; 33 import android.content.pm.PackageInfo; 34 import android.content.pm.PackageManager; 35 import android.content.pm.PackageManager.NameNotFoundException; 36 37 import androidx.preference.ListPreference; 38 39 import com.android.settings.R; 40 import com.android.settings.testutils.FakeFeatureFactory; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Answers; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 import org.robolectric.RobolectricTestRunner; 49 import org.robolectric.RuntimeEnvironment; 50 51 import java.util.Arrays; 52 53 @RunWith(RobolectricTestRunner.class) 54 public class ThemePreferenceControllerTest { 55 56 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 57 private Context mContext; 58 @Mock 59 private PackageManager mPackageManager; 60 @Mock 61 private ApplicationInfo mApplicationInfo; 62 @Mock 63 private ListPreference mPreference; 64 @Mock 65 private IOverlayManager mOverlayManager; 66 67 private ThemePreferenceController mController; 68 69 @Before setUp()70 public void setUp() throws NameNotFoundException { 71 MockitoAnnotations.initMocks(this); 72 FakeFeatureFactory.setupForTest(); 73 when(mPackageManager.getApplicationInfo(any(), anyInt())).thenReturn(mApplicationInfo); 74 when(mContext.getPackageManager()).thenReturn(mPackageManager); 75 when(mContext.getString(R.string.default_theme)) 76 .thenReturn(RuntimeEnvironment.application.getString(R.string.default_theme)); 77 78 when(mContext.getSystemService(Context.OVERLAY_SERVICE)).thenReturn(mOverlayManager); 79 mController = spy(new ThemePreferenceController(mContext, mOverlayManager)); 80 } 81 82 @Test testAvailable_false()83 public void testAvailable_false() throws Exception { 84 when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn( 85 new PackageInfo()); 86 when(mOverlayManager.getOverlayInfosForTarget(any(), anyInt())) 87 .thenReturn(Arrays.asList(new OverlayInfo("", "", "", "", "", 0, 0, 0, false))); 88 assertThat(mController.isAvailable()).isFalse(); 89 } 90 91 @Test testAvailable_true()92 public void testAvailable_true() throws Exception { 93 when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn( 94 new PackageInfo()); 95 when(mOverlayManager.getOverlayInfosForTarget(any(), anyInt())) 96 .thenReturn(Arrays.asList( 97 new OverlayInfo("", "", "", OverlayInfo.CATEGORY_THEME, "", 0, 0, 0, true), 98 new OverlayInfo("", "", "", OverlayInfo.CATEGORY_THEME, "", 0, 0, 0, 99 true))); 100 assertThat(mController.isAvailable()).isTrue(); 101 } 102 103 @Test updateState_themeSet_shouldSetPreferenceValue()104 public void updateState_themeSet_shouldSetPreferenceValue() throws NameNotFoundException { 105 final String pkg1 = "pkg1.theme1"; 106 final String pkg2 = "pkg2.theme2"; 107 final String themeLabel1 = "Theme1"; 108 final String themeLabel2 = "Theme2"; 109 final String[] themes = {pkg1, pkg2}; 110 doReturn("pkg1.theme1").when(mController).getCurrentTheme(); 111 doReturn(themes).when(mController).getAvailableThemes(false /* currentThemeOnly */); 112 when(mPackageManager.getApplicationInfo(anyString(), anyInt()).loadLabel(mPackageManager)) 113 .thenReturn(themeLabel1) 114 .thenReturn(themeLabel2); 115 116 mController.updateState(mPreference); 117 118 verify(mPreference).setSummary(themeLabel1); 119 verify(mPreference).setValue(pkg1); 120 } 121 122 @Test updateState_themeNull_shouldSetDefaultSummary()123 public void updateState_themeNull_shouldSetDefaultSummary() throws NameNotFoundException { 124 final String pkg1 = "pkg1.theme1"; 125 final String pkg2 = "pkg2.theme2"; 126 final String themeLabel1 = "Theme1"; 127 final String themeLabel2 = "Theme2"; 128 final String[] themes = {pkg1, pkg2}; 129 doReturn(null).when(mController).getCurrentTheme(); 130 doReturn(themes).when(mController).getAvailableThemes(false /* currentThemeOnly */); 131 when(mPackageManager.getApplicationInfo(anyString(), anyInt()).loadLabel(mPackageManager)) 132 .thenReturn(themeLabel1) 133 .thenReturn(themeLabel2); 134 135 mController.updateState(mPreference); 136 137 verify(mPreference) 138 .setSummary(RuntimeEnvironment.application.getString(R.string.default_theme)); 139 verify(mPreference).setValue(null); 140 } 141 142 @Test getCurrentTheme_withEnabledState()143 public void getCurrentTheme_withEnabledState() throws Exception { 144 OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android", "", 145 OverlayInfo.CATEGORY_THEME, "", OverlayInfo.STATE_ENABLED, 0, 0, true); 146 OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android", "", 147 OverlayInfo.CATEGORY_THEME, "", 0, 0, 0, true); 148 when(mOverlayManager.getOverlayInfosForTarget(any(), anyInt())).thenReturn( 149 Arrays.asList(info1, info2)); 150 when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn( 151 new PackageInfo()); 152 153 assertThat(mController.getCurrentTheme()).isEqualTo(info1.packageName); 154 } 155 156 @Test testGetCurrentTheme_withoutEnabledState()157 public void testGetCurrentTheme_withoutEnabledState() throws Exception { 158 OverlayInfo info1 = new OverlayInfo("com.android.Theme1", "android", "", 159 OverlayInfo.CATEGORY_THEME, "", OverlayInfo.STATE_DISABLED, 0, 0, true); 160 OverlayInfo info2 = new OverlayInfo("com.android.Theme2", "android", "", 161 OverlayInfo.CATEGORY_THEME, "", 0, 0, 0, true); 162 when(mOverlayManager.getOverlayInfosForTarget(any(), anyInt())).thenReturn( 163 Arrays.asList(info1, info2)); 164 when(mPackageManager.getPackageInfo(anyString(), anyInt())).thenReturn( 165 new PackageInfo()); 166 167 assertThat(mController.getCurrentTheme()).isNull(); 168 } 169 } 170