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.Mockito.mock; 22 23 import android.content.ComponentName; 24 import android.content.Intent; 25 import android.content.pm.ResolveInfo; 26 27 import androidx.fragment.app.FragmentActivity; 28 import androidx.preference.Preference; 29 30 import com.android.settings.R; 31 import com.android.settings.testutils.shadow.SettingsShadowResources; 32 33 import com.google.common.collect.Lists; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.Robolectric; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.Shadows; 42 import org.robolectric.annotation.Config; 43 import org.robolectric.shadows.ShadowPackageManager; 44 45 @RunWith(RobolectricTestRunner.class) 46 @Config(shadows = {SettingsShadowResources.class}) 47 public class WallpaperPreferenceControllerTest { 48 private static final String TEST_KEY = "test_key"; 49 50 private Intent mWallpaperIntent; 51 private Intent mStylesAndWallpaperIntent; 52 private FragmentActivity mContext; 53 private ShadowPackageManager mShadowPackageManager; 54 55 private WallpaperPreferenceController mController; 56 57 @Before setUp()58 public void setUp() { 59 MockitoAnnotations.initMocks(this); 60 mContext = Robolectric.buildActivity(FragmentActivity.class).get(); 61 SettingsShadowResources.overrideResource( 62 R.string.config_wallpaper_picker_package, "bogus.package.for.testing"); 63 SettingsShadowResources.overrideResource( 64 R.string.config_styles_and_wallpaper_picker_class, "bogus.package.class"); 65 mWallpaperIntent = new Intent().setComponent(new ComponentName( 66 mContext.getString(R.string.config_wallpaper_picker_package), 67 mContext.getString(R.string.config_wallpaper_picker_class))); 68 mStylesAndWallpaperIntent = new Intent().setComponent(new ComponentName( 69 mContext.getString(R.string.config_wallpaper_picker_package), 70 mContext.getString(R.string.config_styles_and_wallpaper_picker_class))); 71 mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager()); 72 mController = new WallpaperPreferenceController(mContext, TEST_KEY); 73 } 74 75 @Test isAvailable_wallpaperPickerEnabledAndStylePickerEnabled_returnsTrue()76 public void isAvailable_wallpaperPickerEnabledAndStylePickerEnabled_returnsTrue() { 77 mShadowPackageManager.setResolveInfosForIntent( 78 mWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class))); 79 mShadowPackageManager.setResolveInfosForIntent( 80 mStylesAndWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class))); 81 82 assertThat(mController.isAvailable()).isTrue(); 83 } 84 85 @Test isAvailable_wallpaperPickerEnabledAndStylePickerDisabled_returnsTrue()86 public void isAvailable_wallpaperPickerEnabledAndStylePickerDisabled_returnsTrue() { 87 mShadowPackageManager.setResolveInfosForIntent( 88 mWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class))); 89 mShadowPackageManager.setResolveInfosForIntent( 90 mStylesAndWallpaperIntent, Lists.newArrayList()); 91 92 assertThat(mController.isAvailable()).isTrue(); 93 } 94 95 @Test isAvailable_wallpaperPickerDisabledAndStylePickerEnabled_returnsTrue()96 public void isAvailable_wallpaperPickerDisabledAndStylePickerEnabled_returnsTrue() { 97 mShadowPackageManager.setResolveInfosForIntent( 98 mWallpaperIntent, Lists.newArrayList()); 99 mShadowPackageManager.setResolveInfosForIntent( 100 mStylesAndWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class))); 101 102 assertThat(mController.isAvailable()).isTrue(); 103 } 104 105 @Test isAvailable_wallpaperPickerDisabledAndStylePickerDisabled_returnsFalse()106 public void isAvailable_wallpaperPickerDisabledAndStylePickerDisabled_returnsFalse() { 107 mShadowPackageManager.setResolveInfosForIntent( 108 mWallpaperIntent, Lists.newArrayList()); 109 mShadowPackageManager.setResolveInfosForIntent( 110 mStylesAndWallpaperIntent, Lists.newArrayList()); 111 112 assertThat(mController.isAvailable()).isFalse(); 113 } 114 115 @Test getComponentClassString_stylesAvailable_returnsStylePickerClassString()116 public void getComponentClassString_stylesAvailable_returnsStylePickerClassString() { 117 mShadowPackageManager.setResolveInfosForIntent( 118 mStylesAndWallpaperIntent, 119 Lists.newArrayList(mock(ResolveInfo.class))); 120 assertThat(mController.getComponentClassString()) 121 .isEqualTo(mContext.getString(R.string.config_styles_and_wallpaper_picker_class)); 122 } 123 124 @Test getComponentClassString_stylesUnavailable_returnsWallpaperPickerClassString()125 public void getComponentClassString_stylesUnavailable_returnsWallpaperPickerClassString() { 126 mShadowPackageManager.setResolveInfosForIntent( 127 mStylesAndWallpaperIntent, Lists.newArrayList()); 128 assertThat(mController.getComponentClassString()) 129 .isEqualTo(mContext.getString(R.string.config_wallpaper_picker_class)); 130 } 131 132 @Test areStylesAvailable_noComponentSpecified()133 public void areStylesAvailable_noComponentSpecified() { 134 SettingsShadowResources.overrideResource( 135 R.string.config_styles_and_wallpaper_picker_class, ""); 136 mShadowPackageManager.setResolveInfosForIntent( 137 mStylesAndWallpaperIntent, Lists.newArrayList()); 138 139 assertThat(mController.areStylesAvailable()).isFalse(); 140 } 141 142 @Test areStylesAvailable_componentUnresolveable()143 public void areStylesAvailable_componentUnresolveable() { 144 mShadowPackageManager.setResolveInfosForIntent( 145 mStylesAndWallpaperIntent, Lists.newArrayList()); 146 147 assertThat(mController.areStylesAvailable()).isFalse(); 148 } 149 150 @Test areStylesAvailable_componentResolved()151 public void areStylesAvailable_componentResolved() { 152 mShadowPackageManager.setResolveInfosForIntent( 153 mStylesAndWallpaperIntent, 154 Lists.newArrayList(mock(ResolveInfo.class))); 155 156 assertThat(mController.areStylesAvailable()).isTrue(); 157 } 158 159 @Test getKeywords_withoutStyles()160 public void getKeywords_withoutStyles() { 161 mShadowPackageManager.setResolveInfosForIntent( 162 mStylesAndWallpaperIntent, Lists.newArrayList()); 163 164 assertThat(mController.getKeywords()) 165 .contains(mContext.getString(R.string.keywords_wallpaper)); 166 assertThat(mController.getKeywords()) 167 .doesNotContain(mContext.getString(R.string.keywords_styles)); 168 } 169 170 @Test getKeywords_withStyles()171 public void getKeywords_withStyles() { 172 mShadowPackageManager.setResolveInfosForIntent( 173 mStylesAndWallpaperIntent, 174 Lists.newArrayList(mock(ResolveInfo.class))); 175 176 assertThat(mController.areStylesAvailable()).isTrue(); 177 assertThat(mController.getKeywords()) 178 .contains(mContext.getString(R.string.keywords_wallpaper)); 179 assertThat(mController.getKeywords()) 180 .contains(mContext.getString(R.string.keywords_styles)); 181 } 182 183 @Test handlePreferenceTreeClick_wallpaperOnly()184 public void handlePreferenceTreeClick_wallpaperOnly() { 185 mShadowPackageManager.setResolveInfosForIntent( 186 mWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class))); 187 mShadowPackageManager.setResolveInfosForIntent( 188 mStylesAndWallpaperIntent, Lists.newArrayList()); 189 Preference preference = new Preference(mContext); 190 preference.setKey(TEST_KEY); 191 192 mController.handlePreferenceTreeClick(preference); 193 194 assertThat(Shadows.shadowOf(mContext) 195 .getNextStartedActivityForResult().intent.getComponent().getClassName()) 196 .isEqualTo(mContext.getString(R.string.config_wallpaper_picker_class)); 197 } 198 199 @Test handlePreferenceTreeClick_stylesAndWallpaper()200 public void handlePreferenceTreeClick_stylesAndWallpaper() { 201 mShadowPackageManager.setResolveInfosForIntent( 202 mWallpaperIntent, Lists.newArrayList()); 203 mShadowPackageManager.setResolveInfosForIntent( 204 mStylesAndWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class))); 205 Preference preference = new Preference(mContext); 206 preference.setKey(TEST_KEY); 207 208 mController.handlePreferenceTreeClick(preference); 209 210 assertThat(Shadows.shadowOf(mContext) 211 .getNextStartedActivityForResult().intent.getComponent().getClassName()) 212 .isEqualTo(mContext.getString(R.string.config_styles_and_wallpaper_picker_class)); 213 } 214 215 @Test handlePreferenceTreeClick_launchSourceExtra()216 public void handlePreferenceTreeClick_launchSourceExtra() { 217 mShadowPackageManager.setResolveInfosForIntent( 218 mWallpaperIntent, Lists.newArrayList()); 219 mShadowPackageManager.setResolveInfosForIntent( 220 mStylesAndWallpaperIntent, Lists.newArrayList()); 221 Preference preference = new Preference(mContext); 222 preference.setKey(TEST_KEY); 223 224 mController.handlePreferenceTreeClick(preference); 225 226 assertThat(Shadows.shadowOf(mContext).getNextStartedActivityForResult() 227 .intent.hasExtra("com.android.wallpaper.LAUNCH_SOURCE")).isTrue(); 228 } 229 } 230