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 android.content.Context; 20 import android.content.pm.ApplicationInfo; 21 import android.content.pm.PackageManager; 22 import android.content.pm.PackageManager.NameNotFoundException; 23 import android.support.v7.preference.ListPreference; 24 25 import com.android.settings.R; 26 import com.android.settings.testutils.SettingsRobolectricTestRunner; 27 import com.android.settings.TestConfig; 28 import com.android.settings.display.ThemePreferenceController.OverlayManager; 29 import com.android.settings.testutils.FakeFeatureFactory; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Answers; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.RuntimeEnvironment; 38 import org.robolectric.annotation.Config; 39 40 import static org.mockito.Matchers.any; 41 import static org.mockito.Matchers.anyInt; 42 import static org.mockito.Matchers.anyString; 43 import static org.mockito.Mockito.doReturn; 44 import static org.mockito.Mockito.mock; 45 import static org.mockito.Mockito.spy; 46 import static org.mockito.Mockito.verify; 47 import static org.mockito.Mockito.when; 48 49 @RunWith(SettingsRobolectricTestRunner.class) 50 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 51 public class ThemePreferenceControllerTest { 52 53 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 54 private Context mContext; 55 @Mock 56 private PackageManager mPackageManager; 57 @Mock 58 private ApplicationInfo mApplicationInfo; 59 @Mock 60 private ListPreference mPreference; 61 62 private ThemePreferenceController mController; 63 64 @Before setUp()65 public void setUp() throws NameNotFoundException { 66 MockitoAnnotations.initMocks(this); 67 FakeFeatureFactory.setupForTest(mContext); 68 when(mPackageManager.getApplicationInfo(any(), anyInt())).thenReturn(mApplicationInfo); 69 when(mContext.getPackageManager()).thenReturn(mPackageManager); 70 when(mContext.getString(R.string.default_theme)) 71 .thenReturn(RuntimeEnvironment.application.getString(R.string.default_theme)); 72 73 mController = spy(new ThemePreferenceController(mContext, mock(OverlayManager.class))); 74 } 75 76 @Test updateState_themeSet_shouldSetPreferenceValue()77 public void updateState_themeSet_shouldSetPreferenceValue() throws NameNotFoundException { 78 final String pkg1 = "pkg1.theme1"; 79 final String pkg2 = "pkg2.theme2"; 80 final String themeLabel1 = "Theme1"; 81 final String themeLabel2 = "Theme2"; 82 final String[] themes = {pkg1, pkg2}; 83 doReturn("pkg1.theme1").when(mController).getCurrentTheme(); 84 doReturn(themes).when(mController).getAvailableThemes(); 85 when(mPackageManager.getApplicationInfo(anyString(), anyInt()).loadLabel(mPackageManager)) 86 .thenReturn(themeLabel1) 87 .thenReturn(themeLabel2); 88 89 mController.updateState(mPreference); 90 91 verify(mPreference).setSummary(themeLabel1); 92 verify(mPreference).setValue(pkg1); 93 } 94 95 @Test updateState_themeNull_shouldSetDefaultSummary()96 public void updateState_themeNull_shouldSetDefaultSummary() throws NameNotFoundException { 97 final String pkg1 = "pkg1.theme1"; 98 final String pkg2 = "pkg2.theme2"; 99 final String themeLabel1 = "Theme1"; 100 final String themeLabel2 = "Theme2"; 101 final String[] themes = {pkg1, pkg2}; 102 doReturn(null).when(mController).getCurrentTheme(); 103 doReturn(themes).when(mController).getAvailableThemes(); 104 when(mPackageManager.getApplicationInfo(anyString(), anyInt()).loadLabel(mPackageManager)) 105 .thenReturn(themeLabel1) 106 .thenReturn(themeLabel2); 107 108 mController.updateState(mPreference); 109 110 verify(mPreference) 111 .setSummary(RuntimeEnvironment.application.getString(R.string.default_theme)); 112 verify(mPreference).setValue(null); 113 } 114 } 115