• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.car.settings.common;
18 
19 import static com.android.settingslib.drawer.CategoryKey.CATEGORY_DEVICE;
20 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_ICON;
21 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_SUMMARY;
22 import static com.android.settingslib.drawer.TileUtils.META_DATA_PREFERENCE_TITLE;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import android.content.Context;
27 import android.content.Intent;
28 import android.content.pm.ActivityInfo;
29 import android.content.pm.ResolveInfo;
30 import android.os.Bundle;
31 
32 import androidx.preference.Preference;
33 
34 import com.android.car.settings.CarSettingsRobolectricTestRunner;
35 import com.android.car.settings.R;
36 import com.android.car.settings.testutils.ShadowApplicationPackageManager;
37 
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.annotation.Config;
45 import org.robolectric.shadow.api.Shadow;
46 
47 import java.util.Map;
48 
49 /** Unit test for {@link ExtraSettingsLoader}. */
50 @RunWith(CarSettingsRobolectricTestRunner.class)
51 @Config(shadows = {ShadowApplicationPackageManager.class})
52 public class ExtraSettingsLoaderTest {
53     private Context mContext;
54     private ExtraSettingsLoader mExtraSettingsLoader;
55     private static final String META_DATA_PREFERENCE_CATEGORY = "com.android.settings.category";
56     private static final String FAKE_CATEGORY = "fake_category";
57     private static final String FAKE_TITLE = "fake_title";
58     private static final String FAKE_SUMMARY = "fake_summary";
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         mContext = RuntimeEnvironment.application;
64         ShadowApplicationPackageManager.setResources(mContext.getResources());
65         mExtraSettingsLoader = new ExtraSettingsLoader(mContext);
66     }
67 
68     @After
tearDown()69     public void tearDown() {
70         ShadowApplicationPackageManager.reset();
71     }
72 
73     @Test
testLoadPreference_stringResources_shouldLoadResources()74     public void testLoadPreference_stringResources_shouldLoadResources() {
75         Intent intent = new Intent();
76         intent.putExtra(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
77         Bundle bundle = new Bundle();
78         bundle.putString(META_DATA_PREFERENCE_TITLE, FAKE_TITLE);
79         bundle.putString(META_DATA_PREFERENCE_SUMMARY, FAKE_SUMMARY);
80         bundle.putString(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
81 
82         ActivityInfo activityInfo = new ActivityInfo();
83         activityInfo.metaData = bundle;
84         activityInfo.packageName = "package_name";
85         activityInfo.name = "class_name";
86 
87         ResolveInfo resolveInfoSystem = new ResolveInfo();
88         resolveInfoSystem.system = true;
89         resolveInfoSystem.activityInfo = activityInfo;
90 
91         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoSystem);
92         Map<Preference, Bundle> preferenceToBundleMap = mExtraSettingsLoader.loadPreferences(
93                 intent);
94 
95         assertThat(preferenceToBundleMap).hasSize(1);
96 
97         for (Preference p : preferenceToBundleMap.keySet()) {
98             assertThat(p.getTitle()).isEqualTo(FAKE_TITLE);
99             assertThat(p.getSummary()).isEqualTo(FAKE_SUMMARY);
100         }
101     }
102 
103     @Test
testLoadPreference_metadataBundleIsValue()104     public void testLoadPreference_metadataBundleIsValue() {
105         Intent intent = new Intent();
106         intent.putExtra(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
107         Bundle bundle = new Bundle();
108         bundle.putString(META_DATA_PREFERENCE_TITLE, FAKE_TITLE);
109         bundle.putString(META_DATA_PREFERENCE_SUMMARY, FAKE_SUMMARY);
110         bundle.putString(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
111 
112         ActivityInfo activityInfo = new ActivityInfo();
113         activityInfo.metaData = bundle;
114         activityInfo.packageName = "package_name";
115         activityInfo.name = "class_name";
116 
117         ResolveInfo resolveInfoSystem = new ResolveInfo();
118         resolveInfoSystem.system = true;
119         resolveInfoSystem.activityInfo = activityInfo;
120         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoSystem);
121 
122         ResolveInfo resolveInfoNonSystem = new ResolveInfo();
123         resolveInfoNonSystem.system = false;
124         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoNonSystem);
125 
126         Map<Preference, Bundle> preferenceToBundleMap = mExtraSettingsLoader.loadPreferences(
127                 intent);
128 
129         assertThat(preferenceToBundleMap).hasSize(1);
130 
131         for (Preference p : preferenceToBundleMap.keySet()) {
132             assertThat(p.getTitle()).isEqualTo(FAKE_TITLE);
133             assertThat(p.getSummary()).isEqualTo(FAKE_SUMMARY);
134 
135             Bundle b = preferenceToBundleMap.get(p);
136             assertThat(b.getString(META_DATA_PREFERENCE_TITLE)).isEqualTo(FAKE_TITLE);
137             assertThat(b.getString(META_DATA_PREFERENCE_SUMMARY)).isEqualTo(FAKE_SUMMARY);
138             assertThat(b.getString(META_DATA_PREFERENCE_CATEGORY)).isEqualTo(FAKE_CATEGORY);
139             assertThat(b.getInt(META_DATA_PREFERENCE_ICON)).isNotNull();
140         }
141     }
142 
143     @Test
testLoadPreference_integerResources_shouldLoadResources()144     public void testLoadPreference_integerResources_shouldLoadResources() {
145         Intent intent = new Intent();
146         intent.putExtra(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
147         Bundle bundle = new Bundle();
148         bundle.putInt(META_DATA_PREFERENCE_TITLE, R.string.fake_title);
149         bundle.putInt(META_DATA_PREFERENCE_SUMMARY, R.string.fake_summary);
150         bundle.putInt(META_DATA_PREFERENCE_CATEGORY, R.string.fake_category);
151 
152         ActivityInfo activityInfo = new ActivityInfo();
153         activityInfo.metaData = bundle;
154         activityInfo.packageName = "package_name";
155         activityInfo.name = "class_name";
156 
157         ResolveInfo resolveInfoSystem = new ResolveInfo();
158         resolveInfoSystem.system = true;
159         resolveInfoSystem.activityInfo = activityInfo;
160         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoSystem);
161 
162         ResolveInfo resolveInfoNonSystem = new ResolveInfo();
163         resolveInfoNonSystem.system = false;
164         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoNonSystem);
165 
166         Map<Preference, Bundle> preferenceToBundleMap = mExtraSettingsLoader.loadPreferences(
167                 intent);
168 
169         assertThat(preferenceToBundleMap).hasSize(1);
170 
171         for (Preference p : preferenceToBundleMap.keySet()) {
172             assertThat(p.getTitle()).isEqualTo(FAKE_TITLE);
173             assertThat(p.getSummary()).isEqualTo(FAKE_SUMMARY);
174             assertThat(p.getIcon()).isNotNull();
175 
176         }
177     }
178 
179     @Test
testLoadPreference_noDefaultSummary()180     public void testLoadPreference_noDefaultSummary() {
181         Intent intent = new Intent();
182         intent.putExtra(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
183         Bundle bundle = new Bundle();
184         bundle.putString(META_DATA_PREFERENCE_TITLE, FAKE_TITLE);
185         bundle.putString(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
186 
187         ActivityInfo activityInfo = new ActivityInfo();
188         activityInfo.metaData = bundle;
189         activityInfo.packageName = "package_name";
190         activityInfo.name = "class_name";
191 
192         ResolveInfo resolveInfoSystem = new ResolveInfo();
193         resolveInfoSystem.system = true;
194         resolveInfoSystem.activityInfo = activityInfo;
195 
196         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoSystem);
197         Map<Preference, Bundle> preferenceToBundleMap = mExtraSettingsLoader.loadPreferences(
198                 intent);
199 
200         for (Preference p : preferenceToBundleMap.keySet()) {
201             assertThat(p.getTitle()).isEqualTo(FAKE_TITLE);
202             assertThat(p.getSummary()).isNull();
203 
204         }
205     }
206 
207     @Test
testLoadPreference_noCategory_shouldSetToDeviceCategory()208     public void testLoadPreference_noCategory_shouldSetToDeviceCategory() {
209         Intent intent = new Intent();
210         intent.putExtra(META_DATA_PREFERENCE_CATEGORY, CATEGORY_DEVICE);
211         Bundle bundle = new Bundle();
212         bundle.putString(META_DATA_PREFERENCE_TITLE, FAKE_TITLE);
213         bundle.putString(META_DATA_PREFERENCE_SUMMARY, FAKE_SUMMARY);
214 
215         ActivityInfo activityInfo = new ActivityInfo();
216         activityInfo.metaData = bundle;
217         activityInfo.packageName = "package_name";
218         activityInfo.name = "class_name";
219 
220         ResolveInfo resolveInfoSystem = new ResolveInfo();
221         resolveInfoSystem.system = true;
222         resolveInfoSystem.activityInfo = activityInfo;
223 
224         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoSystem);
225         Map<Preference, Bundle> preferenceToBundleMap = mExtraSettingsLoader.loadPreferences(
226                 intent);
227 
228         assertThat(preferenceToBundleMap).hasSize(1);
229 
230         for (Preference p : preferenceToBundleMap.keySet()) {
231             assertThat(p.getTitle()).isEqualTo(FAKE_TITLE);
232             assertThat(p.getSummary()).isEqualTo(FAKE_SUMMARY);
233         }
234     }
235 
236     @Test
testLoadPreference_noCategoryMatched_shouldNotReturnPreferences()237     public void testLoadPreference_noCategoryMatched_shouldNotReturnPreferences() {
238         Intent intent = new Intent();
239         intent.putExtra(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
240         Bundle bundle = new Bundle();
241         bundle.putString(META_DATA_PREFERENCE_TITLE, FAKE_TITLE);
242         bundle.putString(META_DATA_PREFERENCE_SUMMARY, FAKE_SUMMARY);
243 
244         ActivityInfo activityInfo = new ActivityInfo();
245         activityInfo.metaData = bundle;
246         activityInfo.packageName = "package_name";
247         activityInfo.name = "class_name";
248 
249         ResolveInfo resolveInfoSystem = new ResolveInfo();
250         resolveInfoSystem.system = true;
251         resolveInfoSystem.activityInfo = activityInfo;
252 
253         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoSystem);
254         Map<Preference, Bundle> preferenceToBundleMap = mExtraSettingsLoader.loadPreferences(
255                 intent);
256 
257         assertThat(preferenceToBundleMap).isEmpty();
258     }
259 
260     @Test
testLoadPreference_shouldLoadDefaultIcon()261     public void testLoadPreference_shouldLoadDefaultIcon() {
262         Intent intent = new Intent();
263         intent.putExtra(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
264         Bundle bundle = new Bundle();
265         bundle.putString(META_DATA_PREFERENCE_TITLE, FAKE_TITLE);
266         bundle.putString(META_DATA_PREFERENCE_SUMMARY, FAKE_SUMMARY);
267         bundle.putString(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
268 
269         ActivityInfo activityInfo = new ActivityInfo();
270         activityInfo.metaData = bundle;
271         activityInfo.packageName = "package_name";
272         activityInfo.name = "class_name";
273 
274         ResolveInfo resolveInfoSystem = new ResolveInfo();
275         resolveInfoSystem.system = true;
276         resolveInfoSystem.activityInfo = activityInfo;
277 
278         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoSystem);
279         Map<Preference, Bundle> preferenceToBundleMap = mExtraSettingsLoader.loadPreferences(
280                 intent);
281 
282         for (Preference p : preferenceToBundleMap.keySet()) {
283             assertThat(p.getTitle()).isEqualTo(FAKE_TITLE);
284             assertThat(p.getSummary()).isEqualTo(FAKE_SUMMARY);
285             assertThat(p.getIcon()).isNotNull();
286         }
287     }
288 
289     @Test
testLoadPreference_noSystemApp_returnsNoPreferences()290     public void testLoadPreference_noSystemApp_returnsNoPreferences() {
291         Intent intent = new Intent();
292         intent.putExtra(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
293         Bundle bundle = new Bundle();
294         bundle.putString(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
295 
296         ActivityInfo activityInfo = new ActivityInfo();
297         activityInfo.metaData = bundle;
298 
299         ResolveInfo resolveInfoNonSystem1 = new ResolveInfo();
300         resolveInfoNonSystem1.system = false;
301         resolveInfoNonSystem1.activityInfo = activityInfo;
302         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoNonSystem1);
303 
304         ResolveInfo resolveInfoNonSystem2 = new ResolveInfo();
305         resolveInfoNonSystem2.system = false;
306         resolveInfoNonSystem2.activityInfo = activityInfo;
307         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoNonSystem2);
308 
309         Map<Preference, Bundle> preferenceToBundleMap = mExtraSettingsLoader.loadPreferences(
310                 intent);
311 
312         assertThat(preferenceToBundleMap).isEmpty();
313     }
314 
315     @Test
testLoadPreference_systemApp_returnsPreferences()316     public void testLoadPreference_systemApp_returnsPreferences() {
317         Intent intent = new Intent();
318         intent.putExtra(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
319         Bundle bundle = new Bundle();
320         bundle.putString(META_DATA_PREFERENCE_TITLE, FAKE_TITLE);
321         bundle.putString(META_DATA_PREFERENCE_SUMMARY, FAKE_SUMMARY);
322         bundle.putString(META_DATA_PREFERENCE_CATEGORY, FAKE_CATEGORY);
323 
324         ActivityInfo activityInfo = new ActivityInfo();
325         activityInfo.metaData = bundle;
326         activityInfo.packageName = "package_name";
327         activityInfo.name = "class_name";
328 
329         ResolveInfo resolveInfoSystem1 = new ResolveInfo();
330         resolveInfoSystem1.system = true;
331         resolveInfoSystem1.activityInfo = activityInfo;
332         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoSystem1);
333 
334         ResolveInfo resolveInfoNonSystem1 = new ResolveInfo();
335         resolveInfoNonSystem1.system = false;
336         resolveInfoNonSystem1.activityInfo = activityInfo;
337         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoNonSystem1);
338 
339         ResolveInfo resolveInfoSystem2 = new ResolveInfo();
340         resolveInfoSystem2.system = true;
341         resolveInfoSystem2.activityInfo = activityInfo;
342         getShadowPackageManager().addResolveInfoForIntent(intent, resolveInfoSystem2);
343 
344         Map<Preference, Bundle> preferenceToBundleMap = mExtraSettingsLoader.loadPreferences(
345                 intent);
346 
347         assertThat(preferenceToBundleMap).hasSize(2);
348 
349         for (Preference p : preferenceToBundleMap.keySet()) {
350             assertThat(p.getTitle()).isEqualTo(FAKE_TITLE);
351             assertThat(p.getSummary()).isEqualTo(FAKE_SUMMARY);
352         }
353     }
354 
getShadowPackageManager()355     private ShadowApplicationPackageManager getShadowPackageManager() {
356         return Shadow.extract(mContext.getPackageManager());
357     }
358 }
359 
360