blob: df5c9d2df90b20fd3b1bd187ab0e519d9dbd987e [file] [log] [blame]
Adam Lesinski082614c2016-03-04 14:33:47 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090016
Adam Lesinski082614c2016-03-04 14:33:47 -080017package android.content.res;
18
19import android.annotation.NonNull;
20import android.app.ResourcesManager;
21import android.os.Binder;
Yohei Yukawa23cbe852016-05-17 16:42:58 -070022import android.os.LocaleList;
Adam Lesinski082614c2016-03-04 14:33:47 -080023import android.util.DisplayMetrics;
Adam Lesinski082614c2016-03-04 14:33:47 -080024import android.util.TypedValue;
25import android.view.Display;
Adam Lesinski8e8d2322016-06-24 12:29:16 -070026import android.view.DisplayAdjustments;
27
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090028import androidx.test.filters.SmallTest;
29
Adam Lesinski082614c2016-03-04 14:33:47 -080030import junit.framework.TestCase;
31
32public class ResourcesManagerTest extends TestCase {
33 private static final String APP_ONE_RES_DIR = "app_one.apk";
34 private static final String APP_ONE_RES_SPLIT_DIR = "app_one_split.apk";
35 private static final String APP_TWO_RES_DIR = "app_two.apk";
36 private static final String LIB_RES_DIR = "lib.apk";
37
38 private ResourcesManager mResourcesManager;
39 private DisplayMetrics mDisplayMetrics;
40
41 @Override
42 protected void setUp() throws Exception {
43 super.setUp();
44
45 mDisplayMetrics = new DisplayMetrics();
46 mDisplayMetrics.setToDefaults();
47
48 // Override defaults (which take device specific properties).
49 mDisplayMetrics.density = 1.0f;
50 mDisplayMetrics.densityDpi = DisplayMetrics.DENSITY_DEFAULT;
51 mDisplayMetrics.xdpi = DisplayMetrics.DENSITY_DEFAULT;
52 mDisplayMetrics.ydpi = DisplayMetrics.DENSITY_DEFAULT;
53 mDisplayMetrics.noncompatDensity = mDisplayMetrics.density;
54 mDisplayMetrics.noncompatDensityDpi = mDisplayMetrics.densityDpi;
55 mDisplayMetrics.noncompatXdpi = DisplayMetrics.DENSITY_DEFAULT;
56 mDisplayMetrics.noncompatYdpi = DisplayMetrics.DENSITY_DEFAULT;
57
58 mResourcesManager = new ResourcesManager() {
59 @Override
60 protected AssetManager createAssetManager(@NonNull ResourcesKey key) {
61 return new AssetManager();
62 }
63
64 @Override
Adam Lesinski8e8d2322016-06-24 12:29:16 -070065 protected DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments daj) {
Adam Lesinski082614c2016-03-04 14:33:47 -080066 return mDisplayMetrics;
67 }
68 };
69 }
70
71 @SmallTest
72 public void testMultipleCallsWithIdenticalParametersCacheReference() {
73 Resources resources = mResourcesManager.getResources(
74 null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null,
Ryan Mitchell61158582020-01-14 12:35:08 -080075 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -080076 assertNotNull(resources);
77
78 Resources newResources = mResourcesManager.getResources(
79 null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null,
Ryan Mitchell61158582020-01-14 12:35:08 -080080 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -080081 assertNotNull(newResources);
82 assertSame(resources, newResources);
83 }
84
85 @SmallTest
86 public void testMultipleCallsWithDifferentParametersReturnDifferentReferences() {
87 Resources resources = mResourcesManager.getResources(
88 null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null,
Ryan Mitchell61158582020-01-14 12:35:08 -080089 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -080090 assertNotNull(resources);
91
92 Configuration overrideConfig = new Configuration();
93 overrideConfig.smallestScreenWidthDp = 200;
94 Resources newResources = mResourcesManager.getResources(
95 null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, overrideConfig,
Ryan Mitchell61158582020-01-14 12:35:08 -080096 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -080097 assertNotNull(newResources);
98 assertNotSame(resources, newResources);
99 }
100
101 @SmallTest
102 public void testAddingASplitCreatesANewImpl() {
103 Resources resources1 = mResourcesManager.getResources(
104 null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null,
Ryan Mitchell61158582020-01-14 12:35:08 -0800105 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -0800106 assertNotNull(resources1);
107
108 Resources resources2 = mResourcesManager.getResources(
109 null, APP_ONE_RES_DIR, new String[] { APP_ONE_RES_SPLIT_DIR }, null, null,
Ryan Mitchell61158582020-01-14 12:35:08 -0800110 Display.DEFAULT_DISPLAY, null, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO,null,
111 null);
Adam Lesinski082614c2016-03-04 14:33:47 -0800112 assertNotNull(resources2);
113
114 assertNotSame(resources1, resources2);
115 assertNotSame(resources1.getImpl(), resources2.getImpl());
116 }
117
118 @SmallTest
119 public void testUpdateConfigurationUpdatesAllAssetManagers() {
120 Resources resources1 = mResourcesManager.getResources(
121 null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null,
Ryan Mitchell61158582020-01-14 12:35:08 -0800122 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -0800123 assertNotNull(resources1);
124
125 Resources resources2 = mResourcesManager.getResources(
126 null, APP_TWO_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null,
Ryan Mitchell61158582020-01-14 12:35:08 -0800127 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -0800128 assertNotNull(resources2);
129
130 Binder activity = new Binder();
131 final Configuration overrideConfig = new Configuration();
132 overrideConfig.orientation = Configuration.ORIENTATION_LANDSCAPE;
133 Resources resources3 = mResourcesManager.getResources(
134 activity, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY,
Ryan Mitchell61158582020-01-14 12:35:08 -0800135 overrideConfig, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -0800136 assertNotNull(resources3);
137
138 // No Resources object should be the same.
139 assertNotSame(resources1, resources2);
140 assertNotSame(resources1, resources3);
141 assertNotSame(resources2, resources3);
142
143 // Each ResourcesImpl should be different.
144 assertNotSame(resources1.getImpl(), resources2.getImpl());
145 assertNotSame(resources1.getImpl(), resources3.getImpl());
146 assertNotSame(resources2.getImpl(), resources3.getImpl());
147
148 Configuration newConfig = new Configuration();
149 newConfig.orientation = Configuration.ORIENTATION_LANDSCAPE;
150 mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null);
151
152 final Configuration expectedConfig = new Configuration();
Adam Lesinski10190002016-07-22 16:57:10 -0700153 expectedConfig.setToDefaults();
Adam Lesinski082614c2016-03-04 14:33:47 -0800154 expectedConfig.setLocales(LocaleList.getAdjustedDefault());
155 expectedConfig.densityDpi = mDisplayMetrics.densityDpi;
156 expectedConfig.orientation = Configuration.ORIENTATION_LANDSCAPE;
157
158 assertEquals(expectedConfig, resources1.getConfiguration());
159 assertEquals(expectedConfig, resources2.getConfiguration());
160 assertEquals(expectedConfig, resources3.getConfiguration());
161 }
162
163 @SmallTest
164 public void testTwoActivitiesWithIdenticalParametersShareImpl() {
165 Binder activity1 = new Binder();
166 Resources resources1 = mResourcesManager.getResources(
167 activity1, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null,
Ryan Mitchell61158582020-01-14 12:35:08 -0800168 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -0800169 assertNotNull(resources1);
170
171 Binder activity2 = new Binder();
172 Resources resources2 = mResourcesManager.getResources(
173 activity2, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null,
Ryan Mitchell61158582020-01-14 12:35:08 -0800174 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -0800175 assertNotNull(resources1);
176
177 // The references themselves should be unique.
178 assertNotSame(resources1, resources2);
179
180 // The implementations should be the same.
181 assertSame(resources1.getImpl(), resources2.getImpl());
Adam Lesinski082614c2016-03-04 14:33:47 -0800182 }
183
184 @SmallTest
185 public void testThemesGetUpdatedWithNewImpl() {
186 Binder activity1 = new Binder();
Adam Lesinski7f3f4992016-03-30 10:32:15 -0700187 Resources resources1 = mResourcesManager.createBaseActivityResources(
Adam Lesinski082614c2016-03-04 14:33:47 -0800188 activity1, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null,
Ryan Mitchell61158582020-01-14 12:35:08 -0800189 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski082614c2016-03-04 14:33:47 -0800190 assertNotNull(resources1);
191
192 Resources.Theme theme = resources1.newTheme();
193 assertSame(resources1, theme.getResources());
194 theme.applyStyle(android.R.style.Theme_NoTitleBar, false);
195
196 TypedValue value = new TypedValue();
197 assertTrue(theme.resolveAttribute(android.R.attr.windowNoTitle, value, true));
198 assertEquals(TypedValue.TYPE_INT_BOOLEAN, value.type);
199 assertTrue(value.data != 0);
200
201 final Configuration overrideConfig = new Configuration();
202 overrideConfig.orientation = Configuration.ORIENTATION_LANDSCAPE;
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800203 mResourcesManager.updateResourcesForActivity(activity1, overrideConfig,
204 Display.DEFAULT_DISPLAY, false /* movedToDifferentDisplay */);
Adam Lesinski7f3f4992016-03-30 10:32:15 -0700205 assertSame(resources1, theme.getResources());
Adam Lesinski082614c2016-03-04 14:33:47 -0800206
207 // Make sure we can still access the data.
208 assertTrue(theme.resolveAttribute(android.R.attr.windowNoTitle, value, true));
209 assertEquals(TypedValue.TYPE_INT_BOOLEAN, value.type);
210 assertTrue(value.data != 0);
211 }
Adam Lesinski7f3f4992016-03-30 10:32:15 -0700212
213 @SmallTest
214 public void testMultipleResourcesForOneActivityGetUpdatedWhenActivityBaseUpdates() {
215 Binder activity1 = new Binder();
216
217 // Create a Resources for the Activity.
218 Configuration config1 = new Configuration();
219 config1.densityDpi = 280;
220 Resources resources1 = mResourcesManager.createBaseActivityResources(
221 activity1, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, config1,
Ryan Mitchell61158582020-01-14 12:35:08 -0800222 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski7f3f4992016-03-30 10:32:15 -0700223 assertNotNull(resources1);
224
225 // Create a Resources based on the Activity.
226 Configuration config2 = new Configuration();
227 config2.screenLayout |= Configuration.SCREENLAYOUT_ROUND_YES;
228 Resources resources2 = mResourcesManager.getResources(
229 activity1, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, config2,
Ryan Mitchell61158582020-01-14 12:35:08 -0800230 CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null);
Adam Lesinski7f3f4992016-03-30 10:32:15 -0700231 assertNotNull(resources2);
232
233 assertNotSame(resources1, resources2);
234 assertNotSame(resources1.getImpl(), resources2.getImpl());
235
236 final Configuration expectedConfig1 = new Configuration();
Adam Lesinski10190002016-07-22 16:57:10 -0700237 expectedConfig1.setToDefaults();
Adam Lesinski7f3f4992016-03-30 10:32:15 -0700238 expectedConfig1.setLocales(LocaleList.getAdjustedDefault());
239 expectedConfig1.densityDpi = 280;
240 assertEquals(expectedConfig1, resources1.getConfiguration());
241
242 // resources2 should be based on the Activity's override config, so the density should
243 // be the same as resources1.
244 final Configuration expectedConfig2 = new Configuration();
Adam Lesinski10190002016-07-22 16:57:10 -0700245 expectedConfig2.setToDefaults();
Adam Lesinski7f3f4992016-03-30 10:32:15 -0700246 expectedConfig2.setLocales(LocaleList.getAdjustedDefault());
247 expectedConfig2.densityDpi = 280;
248 expectedConfig2.screenLayout |= Configuration.SCREENLAYOUT_ROUND_YES;
249 assertEquals(expectedConfig2, resources2.getConfiguration());
250
251 // Now update the Activity base override, and both resources should update.
252 config1.orientation = Configuration.ORIENTATION_LANDSCAPE;
Andrii Kulianb047b8b2017-02-08 18:38:26 -0800253 mResourcesManager.updateResourcesForActivity(activity1, config1, Display.DEFAULT_DISPLAY,
254 false /* movedToDifferentDisplay */);
Adam Lesinski7f3f4992016-03-30 10:32:15 -0700255
256 expectedConfig1.orientation = Configuration.ORIENTATION_LANDSCAPE;
257 assertEquals(expectedConfig1, resources1.getConfiguration());
258
259 expectedConfig2.orientation = Configuration.ORIENTATION_LANDSCAPE;
260 assertEquals(expectedConfig2, resources2.getConfiguration());
261 }
Adam Lesinski082614c2016-03-04 14:33:47 -0800262}