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