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 | */ |
| 16 | package android.content.res; |
| 17 | |
| 18 | import android.annotation.NonNull; |
| 19 | import android.app.ResourcesManager; |
| 20 | import android.os.Binder; |
| 21 | import android.test.suitebuilder.annotation.SmallTest; |
| 22 | import android.util.DisplayMetrics; |
| 23 | import android.util.LocaleList; |
| 24 | import android.util.TypedValue; |
| 25 | import android.view.Display; |
| 26 | import junit.framework.TestCase; |
| 27 | |
| 28 | public class ResourcesManagerTest extends TestCase { |
| 29 | private static final String APP_ONE_RES_DIR = "app_one.apk"; |
| 30 | private static final String APP_ONE_RES_SPLIT_DIR = "app_one_split.apk"; |
| 31 | private static final String APP_TWO_RES_DIR = "app_two.apk"; |
| 32 | private static final String LIB_RES_DIR = "lib.apk"; |
| 33 | |
| 34 | private ResourcesManager mResourcesManager; |
| 35 | private DisplayMetrics mDisplayMetrics; |
| 36 | |
| 37 | @Override |
| 38 | protected void setUp() throws Exception { |
| 39 | super.setUp(); |
| 40 | |
| 41 | mDisplayMetrics = new DisplayMetrics(); |
| 42 | mDisplayMetrics.setToDefaults(); |
| 43 | |
| 44 | // Override defaults (which take device specific properties). |
| 45 | mDisplayMetrics.density = 1.0f; |
| 46 | mDisplayMetrics.densityDpi = DisplayMetrics.DENSITY_DEFAULT; |
| 47 | mDisplayMetrics.xdpi = DisplayMetrics.DENSITY_DEFAULT; |
| 48 | mDisplayMetrics.ydpi = DisplayMetrics.DENSITY_DEFAULT; |
| 49 | mDisplayMetrics.noncompatDensity = mDisplayMetrics.density; |
| 50 | mDisplayMetrics.noncompatDensityDpi = mDisplayMetrics.densityDpi; |
| 51 | mDisplayMetrics.noncompatXdpi = DisplayMetrics.DENSITY_DEFAULT; |
| 52 | mDisplayMetrics.noncompatYdpi = DisplayMetrics.DENSITY_DEFAULT; |
| 53 | |
| 54 | mResourcesManager = new ResourcesManager() { |
| 55 | @Override |
| 56 | protected AssetManager createAssetManager(@NonNull ResourcesKey key) { |
| 57 | return new AssetManager(); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | protected DisplayMetrics getDisplayMetricsLocked(int displayId) { |
| 62 | return mDisplayMetrics; |
| 63 | } |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | @SmallTest |
| 68 | public void testMultipleCallsWithIdenticalParametersCacheReference() { |
| 69 | Resources resources = mResourcesManager.getResources( |
| 70 | null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
| 71 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 72 | assertNotNull(resources); |
| 73 | |
| 74 | Resources newResources = mResourcesManager.getResources( |
| 75 | null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
| 76 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 77 | assertNotNull(newResources); |
| 78 | assertSame(resources, newResources); |
| 79 | } |
| 80 | |
| 81 | @SmallTest |
| 82 | public void testMultipleCallsWithDifferentParametersReturnDifferentReferences() { |
| 83 | Resources resources = mResourcesManager.getResources( |
| 84 | null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
| 85 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 86 | assertNotNull(resources); |
| 87 | |
| 88 | Configuration overrideConfig = new Configuration(); |
| 89 | overrideConfig.smallestScreenWidthDp = 200; |
| 90 | Resources newResources = mResourcesManager.getResources( |
| 91 | null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, overrideConfig, |
| 92 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 93 | assertNotNull(newResources); |
| 94 | assertNotSame(resources, newResources); |
| 95 | } |
| 96 | |
| 97 | @SmallTest |
| 98 | public void testAddingASplitCreatesANewImpl() { |
| 99 | Resources resources1 = mResourcesManager.getResources( |
| 100 | null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
| 101 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 102 | assertNotNull(resources1); |
| 103 | |
| 104 | Resources resources2 = mResourcesManager.getResources( |
| 105 | null, APP_ONE_RES_DIR, new String[] { APP_ONE_RES_SPLIT_DIR }, null, null, |
| 106 | Display.DEFAULT_DISPLAY, null, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 107 | assertNotNull(resources2); |
| 108 | |
| 109 | assertNotSame(resources1, resources2); |
| 110 | assertNotSame(resources1.getImpl(), resources2.getImpl()); |
| 111 | } |
| 112 | |
| 113 | @SmallTest |
| 114 | public void testUpdateConfigurationUpdatesAllAssetManagers() { |
| 115 | Resources resources1 = mResourcesManager.getResources( |
| 116 | null, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
| 117 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 118 | assertNotNull(resources1); |
| 119 | |
| 120 | Resources resources2 = mResourcesManager.getResources( |
| 121 | null, APP_TWO_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
| 122 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 123 | assertNotNull(resources2); |
| 124 | |
| 125 | Binder activity = new Binder(); |
| 126 | final Configuration overrideConfig = new Configuration(); |
| 127 | overrideConfig.orientation = Configuration.ORIENTATION_LANDSCAPE; |
| 128 | Resources resources3 = mResourcesManager.getResources( |
| 129 | activity, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, |
| 130 | overrideConfig, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 131 | assertNotNull(resources3); |
| 132 | |
| 133 | // No Resources object should be the same. |
| 134 | assertNotSame(resources1, resources2); |
| 135 | assertNotSame(resources1, resources3); |
| 136 | assertNotSame(resources2, resources3); |
| 137 | |
| 138 | // Each ResourcesImpl should be different. |
| 139 | assertNotSame(resources1.getImpl(), resources2.getImpl()); |
| 140 | assertNotSame(resources1.getImpl(), resources3.getImpl()); |
| 141 | assertNotSame(resources2.getImpl(), resources3.getImpl()); |
| 142 | |
| 143 | Configuration newConfig = new Configuration(); |
| 144 | newConfig.orientation = Configuration.ORIENTATION_LANDSCAPE; |
| 145 | mResourcesManager.applyConfigurationToResourcesLocked(newConfig, null); |
| 146 | |
| 147 | final Configuration expectedConfig = new Configuration(); |
| 148 | expectedConfig.setLocales(LocaleList.getAdjustedDefault()); |
| 149 | expectedConfig.densityDpi = mDisplayMetrics.densityDpi; |
| 150 | expectedConfig.orientation = Configuration.ORIENTATION_LANDSCAPE; |
| 151 | |
| 152 | assertEquals(expectedConfig, resources1.getConfiguration()); |
| 153 | assertEquals(expectedConfig, resources2.getConfiguration()); |
| 154 | assertEquals(expectedConfig, resources3.getConfiguration()); |
| 155 | } |
| 156 | |
| 157 | @SmallTest |
| 158 | public void testTwoActivitiesWithIdenticalParametersShareImpl() { |
| 159 | Binder activity1 = new Binder(); |
| 160 | Resources resources1 = mResourcesManager.getResources( |
| 161 | activity1, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
| 162 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 163 | assertNotNull(resources1); |
| 164 | |
| 165 | Binder activity2 = new Binder(); |
| 166 | Resources resources2 = mResourcesManager.getResources( |
| 167 | activity2, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
| 168 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 169 | assertNotNull(resources1); |
| 170 | |
| 171 | // The references themselves should be unique. |
| 172 | assertNotSame(resources1, resources2); |
| 173 | |
| 174 | // The implementations should be the same. |
| 175 | assertSame(resources1.getImpl(), resources2.getImpl()); |
| 176 | |
| 177 | final Configuration overrideConfig = new Configuration(); |
| 178 | overrideConfig.orientation = Configuration.ORIENTATION_LANDSCAPE; |
| 179 | Resources resources3 = mResourcesManager.getResources( |
| 180 | activity2, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, |
| 181 | overrideConfig, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 182 | |
| 183 | // Since we requested new resources for activity2, the resource should be the same |
| 184 | // as the one returned before for activity2. |
| 185 | assertSame(resources2, resources3); |
| 186 | |
| 187 | // But the implementation has changed. |
| 188 | assertNotSame(resources1.getImpl(), resources2.getImpl()); |
| 189 | } |
| 190 | |
| 191 | @SmallTest |
| 192 | public void testThemesGetUpdatedWithNewImpl() { |
| 193 | Binder activity1 = new Binder(); |
| 194 | Resources resources1 = mResourcesManager.getResources( |
| 195 | activity1, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, null, |
| 196 | CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 197 | assertNotNull(resources1); |
| 198 | |
| 199 | Resources.Theme theme = resources1.newTheme(); |
| 200 | assertSame(resources1, theme.getResources()); |
| 201 | theme.applyStyle(android.R.style.Theme_NoTitleBar, false); |
| 202 | |
| 203 | TypedValue value = new TypedValue(); |
| 204 | assertTrue(theme.resolveAttribute(android.R.attr.windowNoTitle, value, true)); |
| 205 | assertEquals(TypedValue.TYPE_INT_BOOLEAN, value.type); |
| 206 | assertTrue(value.data != 0); |
| 207 | |
| 208 | final Configuration overrideConfig = new Configuration(); |
| 209 | overrideConfig.orientation = Configuration.ORIENTATION_LANDSCAPE; |
| 210 | Resources resources2 = mResourcesManager.getResources( |
| 211 | activity1, APP_ONE_RES_DIR, null, null, null, Display.DEFAULT_DISPLAY, |
| 212 | overrideConfig, CompatibilityInfo.DEFAULT_COMPATIBILITY_INFO, null); |
| 213 | assertNotNull(resources2); |
| 214 | assertSame(resources1, resources2); |
| 215 | assertSame(resources2, theme.getResources()); |
| 216 | |
| 217 | // Make sure we can still access the data. |
| 218 | assertTrue(theme.resolveAttribute(android.R.attr.windowNoTitle, value, true)); |
| 219 | assertEquals(TypedValue.TYPE_INT_BOOLEAN, value.type); |
| 220 | assertTrue(value.data != 0); |
| 221 | } |
| 222 | } |