• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 TopLevelWallpaperPreferenceControllerTest {
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 TopLevelWallpaperPreferenceController 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 TopLevelWallpaperPreferenceController(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
handlePreferenceTreeClick_wallpaperOnly()160     public void handlePreferenceTreeClick_wallpaperOnly() {
161         mShadowPackageManager.setResolveInfosForIntent(
162                 mWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class)));
163         mShadowPackageManager.setResolveInfosForIntent(
164                 mStylesAndWallpaperIntent, Lists.newArrayList());
165         Preference preference = new Preference(mContext);
166         preference.setKey(TEST_KEY);
167 
168         mController.handlePreferenceTreeClick(preference);
169 
170         assertThat(Shadows.shadowOf(mContext)
171                 .getNextStartedActivityForResult().intent.getComponent().getClassName())
172                 .isEqualTo(mContext.getString(R.string.config_wallpaper_picker_class));
173     }
174 
175     @Test
handlePreferenceTreeClick_stylesAndWallpaper()176     public void handlePreferenceTreeClick_stylesAndWallpaper() {
177         mShadowPackageManager.setResolveInfosForIntent(
178                 mWallpaperIntent, Lists.newArrayList());
179         mShadowPackageManager.setResolveInfosForIntent(
180                 mStylesAndWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class)));
181         Preference preference = new Preference(mContext);
182         preference.setKey(TEST_KEY);
183 
184         mController.handlePreferenceTreeClick(preference);
185 
186         assertThat(Shadows.shadowOf(mContext)
187                 .getNextStartedActivityForResult().intent.getComponent().getClassName())
188                 .isEqualTo(mContext.getString(R.string.config_styles_and_wallpaper_picker_class));
189     }
190 
191     @Test
handlePreferenceTreeClick_launchSourceExtra()192     public void handlePreferenceTreeClick_launchSourceExtra() {
193         mShadowPackageManager.setResolveInfosForIntent(
194                 mWallpaperIntent, Lists.newArrayList());
195         mShadowPackageManager.setResolveInfosForIntent(
196                 mStylesAndWallpaperIntent, Lists.newArrayList());
197         Preference preference = new Preference(mContext);
198         preference.setKey(TEST_KEY);
199 
200         mController.handlePreferenceTreeClick(preference);
201 
202         assertThat(Shadows.shadowOf(mContext).getNextStartedActivityForResult()
203                 .intent.hasExtra("com.android.wallpaper.LAUNCH_SOURCE")).isTrue();
204     }
205 
206     @Test
handlePreferenceTreeClick_launchClearTask()207     public void handlePreferenceTreeClick_launchClearTask() {
208         mShadowPackageManager.setResolveInfosForIntent(
209                 mWallpaperIntent, Lists.newArrayList());
210         mShadowPackageManager.setResolveInfosForIntent(
211                 mStylesAndWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class)));
212 
213         Preference preference = new Preference(mContext);
214         preference.setKey(TEST_KEY);
215 
216         mController.handlePreferenceTreeClick(preference);
217 
218         assertThat((Shadows.shadowOf(mContext).getNextStartedActivityForResult()
219                 .intent.getFlags() & Intent.FLAG_ACTIVITY_CLEAR_TASK) != 0).isTrue();
220     }
221 }
222