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_wallpaperPickerEnabled_shouldReturnTrue()76     public void isAvailable_wallpaperPickerEnabled_shouldReturnTrue() {
77         mShadowPackageManager.setResolveInfosForIntent(
78                 mWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class)));
79 
80         assertThat(mController.isAvailable()).isTrue();
81     }
82 
83     @Test
isAvailable_wallpaperPickerDisabled_shouldReturnFalse()84     public void isAvailable_wallpaperPickerDisabled_shouldReturnFalse() {
85         mShadowPackageManager.setResolveInfosForIntent(
86                 mWallpaperIntent, Lists.newArrayList());
87 
88         assertThat(mController.isAvailable()).isFalse();
89     }
90 
91     @Test
areStylesAvailable_noComponentSpecified()92     public void areStylesAvailable_noComponentSpecified() {
93         SettingsShadowResources.overrideResource(
94                 R.string.config_styles_and_wallpaper_picker_class, "");
95         mShadowPackageManager.setResolveInfosForIntent(
96                 mStylesAndWallpaperIntent, Lists.newArrayList());
97 
98         assertThat(mController.areStylesAvailable()).isFalse();
99     }
100 
101     @Test
areStylesAvailable_componentUnresolveable()102     public void areStylesAvailable_componentUnresolveable() {
103         mShadowPackageManager.setResolveInfosForIntent(
104                 mStylesAndWallpaperIntent, Lists.newArrayList());
105 
106         assertThat(mController.areStylesAvailable()).isFalse();
107     }
108 
109     @Test
areStylesAvailable_componentResolved()110     public void areStylesAvailable_componentResolved() {
111         mShadowPackageManager.setResolveInfosForIntent(
112                 mStylesAndWallpaperIntent,
113                 Lists.newArrayList(mock(ResolveInfo.class)));
114 
115         assertThat(mController.areStylesAvailable()).isTrue();
116     }
117 
118     @Test
getKeywords_withoutStyles()119     public void getKeywords_withoutStyles() {
120         mShadowPackageManager.setResolveInfosForIntent(
121                 mStylesAndWallpaperIntent, Lists.newArrayList());
122 
123         assertThat(mController.getKeywords())
124                 .contains(mContext.getString(R.string.keywords_wallpaper));
125         assertThat(mController.getKeywords())
126                 .doesNotContain(mContext.getString(R.string.keywords_styles));
127     }
128 
129     @Test
getKeywords_withStyles()130     public void getKeywords_withStyles() {
131         mShadowPackageManager.setResolveInfosForIntent(
132                 mStylesAndWallpaperIntent,
133                 Lists.newArrayList(mock(ResolveInfo.class)));
134 
135         assertThat(mController.areStylesAvailable()).isTrue();
136         assertThat(mController.getKeywords())
137                 .contains(mContext.getString(R.string.keywords_wallpaper));
138         assertThat(mController.getKeywords())
139                 .contains(mContext.getString(R.string.keywords_styles));
140     }
141 
142     @Test
handlePreferenceTreeClick_wallpaperOnly()143     public void handlePreferenceTreeClick_wallpaperOnly() {
144         mShadowPackageManager.setResolveInfosForIntent(
145                 mWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class)));
146         mShadowPackageManager.setResolveInfosForIntent(
147                 mStylesAndWallpaperIntent, Lists.newArrayList());
148         Preference preference = new Preference(mContext);
149         preference.setKey(TEST_KEY);
150 
151         mController.handlePreferenceTreeClick(preference);
152 
153         assertThat(Shadows.shadowOf(mContext)
154                 .getNextStartedActivityForResult().intent.getComponent().getClassName())
155                 .isEqualTo(mContext.getString(R.string.config_wallpaper_picker_class));
156     }
157 
158     @Test
handlePreferenceTreeClick_stylesAndWallpaper()159     public void handlePreferenceTreeClick_stylesAndWallpaper() {
160         mShadowPackageManager.setResolveInfosForIntent(
161                 mWallpaperIntent, Lists.newArrayList());
162         mShadowPackageManager.setResolveInfosForIntent(
163                 mStylesAndWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class)));
164         Preference preference = new Preference(mContext);
165         preference.setKey(TEST_KEY);
166 
167         mController.handlePreferenceTreeClick(preference);
168 
169         assertThat(Shadows.shadowOf(mContext)
170                 .getNextStartedActivityForResult().intent.getComponent().getClassName())
171                 .isEqualTo(mContext.getString(R.string.config_styles_and_wallpaper_picker_class));
172     }
173 }
174