Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 1 | /* |
| 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. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 16 | |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 17 | package android.content.res; |
| 18 | |
| 19 | import android.annotation.NonNull; |
| 20 | import android.app.ResourcesManager; |
| 21 | import android.os.Binder; |
Yohei Yukawa | 23cbe85 | 2016-05-17 16:42:58 -0700 | [diff] [blame] | 22 | import android.os.LocaleList; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 23 | import android.util.DisplayMetrics; |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 24 | import android.util.TypedValue; |
| 25 | import android.view.Display; |
Adam Lesinski | 8e8d232 | 2016-06-24 12:29:16 -0700 | [diff] [blame] | 26 | import android.view.DisplayAdjustments; |
| 27 | |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 28 | import androidx.test.filters.SmallTest; |
| 29 | |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 30 | import junit.framework.TestCase; |
| 31 | |
| 32 | public 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 Lesinski | 8e8d232 | 2016-06-24 12:29:16 -0700 | [diff] [blame] | 65 | protected DisplayMetrics getDisplayMetrics(int displayId, DisplayAdjustments daj) { |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 66 | 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 Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 75 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 76 | assertNotNull(resources); |
| 77 | |
| 78 | Resources newResources = mResourcesManager.getResources( |
| 79 | null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 80 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 81 | 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 Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 89 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 90 | 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 Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 96 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 97 | 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 Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 105 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 106 | assertNotNull(resources1); |
| 107 | |
| 108 | Resources resources2 = mResourcesManager.getResources( |
| 109 | null, APP_ONE_RES_DIR, new String[] { APP_ONE_RES_SPLIT_DIR }, null, null, |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 110 | Display.DEFAULT_DISPLAY, null, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO,null, |
| 111 | null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 112 | 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 Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 122 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 123 | assertNotNull(resources1); |
| 124 | |
| 125 | Resources resources2 = mResourcesManager.getResources( |
| 126 | null, APP_TWO_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 127 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 128 | 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 Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 135 | overrideConfig, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 136 | 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 Lesinski | 1019000 | 2016-07-22 16:57:10 -0700 | [diff] [blame] | 153 | expectedConfig.setToDefaults(); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 154 | 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 Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 168 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 169 | 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 Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 174 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 175 | 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 Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | @SmallTest |
| 185 | public void testThemesGetUpdatedWithNewImpl() { |
| 186 | Binder activity1 = new Binder(); |
Adam Lesinski | 7f3f499 | 2016-03-30 10:32:15 -0700 | [diff] [blame] | 187 | Resources resources1 = mResourcesManager.createBaseActivityResources( |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 188 | activity1, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
Ryan Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 189 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 190 | 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 Kulian | b047b8b | 2017-02-08 18:38:26 -0800 | [diff] [blame] | 203 | mResourcesManager.updateResourcesForActivity(activity1, overrideConfig, |
| 204 | Display.DEFAULT_DISPLAY, false /* movedToDifferentDisplay */); |
Adam Lesinski | 7f3f499 | 2016-03-30 10:32:15 -0700 | [diff] [blame] | 205 | assertSame(resources1, theme.getResources()); |
Adam Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 206 | |
| 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 Lesinski | 7f3f499 | 2016-03-30 10:32:15 -0700 | [diff] [blame] | 212 | |
| 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 Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 222 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 7f3f499 | 2016-03-30 10:32:15 -0700 | [diff] [blame] | 223 | 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 Mitchell | 6115858 | 2020-01-14 12:35:08 -0800 | [diff] [blame] | 230 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null, null); |
Adam Lesinski | 7f3f499 | 2016-03-30 10:32:15 -0700 | [diff] [blame] | 231 | assertNotNull(resources2); |
| 232 | |
| 233 | assertNotSame(resources1, resources2); |
| 234 | assertNotSame(resources1.getImpl(), resources2.getImpl()); |
| 235 | |
| 236 | final Configuration expectedConfig1 = new Configuration(); |
Adam Lesinski | 1019000 | 2016-07-22 16:57:10 -0700 | [diff] [blame] | 237 | expectedConfig1.setToDefaults(); |
Adam Lesinski | 7f3f499 | 2016-03-30 10:32:15 -0700 | [diff] [blame] | 238 | 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 Lesinski | 1019000 | 2016-07-22 16:57:10 -0700 | [diff] [blame] | 245 | expectedConfig2.setToDefaults(); |
Adam Lesinski | 7f3f499 | 2016-03-30 10:32:15 -0700 | [diff] [blame] | 246 | 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 Kulian | b047b8b | 2017-02-08 18:38:26 -0800 | [diff] [blame] | 253 | mResourcesManager.updateResourcesForActivity(activity1, config1, Display.DEFAULT_DISPLAY, |
| 254 | false /* movedToDifferentDisplay */); |
Adam Lesinski | 7f3f499 | 2016-03-30 10:32:15 -0700 | [diff] [blame] | 255 | |
| 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 Lesinski | 082614c | 2016-03-04 14:33:47 -0800 | [diff] [blame] | 262 | } |