| 1 |
/* |
| 2 |
* Copyright (C) 2013 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.ide.common.resources; |
| 18 |
|
| 19 |
import com.android.annotations.Nullable; |
| 20 |
import com.android.ide.common.rendering.api.ArrayResourceValue; |
| 21 |
import com.android.ide.common.rendering.api.LayoutLog; |
| 22 |
import com.android.ide.common.rendering.api.ResourceValue; |
| 23 |
import com.android.ide.common.res2.ResourceRepository; |
| 24 |
import com.android.ide.common.resources.configuration.FolderConfiguration; |
| 25 |
import com.android.resources.ResourceType; |
| 26 |
import com.google.common.collect.Lists; |
| 27 |
|
| 28 |
import junit.framework.TestCase; |
| 29 |
|
| 30 |
import java.util.List; |
| 31 |
import java.util.Map; |
| 32 |
|
| 33 |
public class ResourceItemResolverTest extends TestCase { |
| 34 |
@SuppressWarnings("ConstantConditions") |
| 35 |
public void test() throws Exception { |
| 36 |
final TestResourceRepository frameworkResources = TestResourceRepository.create(true, |
| 37 |
new Object[]{ |
| 38 |
"values/strings.xml", "" |
| 39 |
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
| 40 |
+ "<resources>\n" |
| 41 |
+ " <string name=\"ok\">Ok</string>\n" |
| 42 |
+ " <array name=\"my_fw_array\">\"\n" |
| 43 |
+ " <item> fw_value1</item>\n" // also test trimming. |
| 44 |
+ " <item>fw_value2\n</item>\n" |
| 45 |
+ " <item>fw_value3</item>\n" |
| 46 |
+ " </array>\n" |
| 47 |
+ "</resources>\n", |
| 48 |
|
| 49 |
"values/themes.xml", "" |
| 50 |
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
| 51 |
+ "<resources>\n" |
| 52 |
+ " <style name=\"Theme\">\n" |
| 53 |
+ " <item name=\"colorForeground\">@android:color/bright_foreground_dark</item>\n" |
| 54 |
+ " <item name=\"colorBackground\">@android:color/background_dark</item>\n" |
| 55 |
+ " </style>\n" |
| 56 |
+ " <style name=\"Theme.Light\">\n" |
| 57 |
+ " <item name=\"colorBackground\">@android:color/background_light</item>\n" |
| 58 |
+ " <item name=\"colorForeground\">@color/bright_foreground_light</item>\n" |
| 59 |
+ " </style>\n" |
| 60 |
+ "</resources>\n", |
| 61 |
|
| 62 |
"values/colors.xml", "" |
| 63 |
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
| 64 |
+ "<resources>\n" |
| 65 |
+ " <color name=\"background_dark\">#ff000000</color>\n" |
| 66 |
+ " <color name=\"background_light\">#ffffffff</color>\n" |
| 67 |
+ " <color name=\"bright_foreground_dark\">@android:color/background_light</color>\n" |
| 68 |
+ " <color name=\"bright_foreground_light\">@android:color/background_dark</color>\n" |
| 69 |
+ "</resources>\n", |
| 70 |
}); |
| 71 |
|
| 72 |
final ResourceRepository appResources = TestResourceRepository.createRes2(false, |
| 73 |
new Object[]{ |
| 74 |
"layout/layout1.xml", "<!--contents doesn't matter-->", |
| 75 |
|
| 76 |
"layout/layout2.xml", "<!--contents doesn't matter-->", |
| 77 |
|
| 78 |
"layout-land/layout1.xml", "<!--contents doesn't matter-->", |
| 79 |
|
| 80 |
"layout-land/only_land.xml", "<!--contents doesn't matter-->", |
| 81 |
|
| 82 |
"drawable/graphic.9.png", new byte[0], |
| 83 |
|
| 84 |
"values/styles.xml", "" |
| 85 |
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
| 86 |
+ "<resources>\n" |
| 87 |
+ " <style name=\"MyTheme\" parent=\"android:Theme.Light\">\n" |
| 88 |
+ " <item name=\"android:textColor\">#999999</item>\n" |
| 89 |
+ " <item name=\"foo\">?android:colorForeground</item>\n" |
| 90 |
+ " </style>\n" |
| 91 |
+ " <style name=\"MyTheme.Dotted1\" parent=\"\">\n" |
| 92 |
+ " </style>" |
| 93 |
+ " <style name=\"MyTheme.Dotted2\">\n" |
| 94 |
+ " </style>" |
| 95 |
+ " <style name=\"RandomStyle\">\n" |
| 96 |
+ " </style>" |
| 97 |
+ " <style name=\"RandomStyle2\" parent=\"RandomStyle\">\n" |
| 98 |
+ " </style>" |
| 99 |
+ "</resources>\n", |
| 100 |
|
| 101 |
"values/strings.xml", "" |
| 102 |
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
| 103 |
+ "<resources xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\">\n" |
| 104 |
+ " <item type=\"id\" name=\"action_bar_refresh\" />\n" |
| 105 |
+ " <item type=\"dimen\" name=\"dialog_min_width_major\">45%</item>\n" |
| 106 |
+ " <string name=\"home_title\">Home Sample</string>\n" |
| 107 |
+ " <string name=\"show_all_apps\">All</string>\n" |
| 108 |
+ " <string name=\"menu_wallpaper\">Wallpaper</string>\n" |
| 109 |
+ " <string name=\"menu_search\">Search</string>\n" |
| 110 |
+ " <string name=\"menu_settings\">Settings</string>\n" |
| 111 |
+ " <string name=\"dummy\" translatable=\"false\">Ignore Me</string>\n" |
| 112 |
+ " <string name=\"wallpaper_instructions\">Tap picture to set portrait wallpaper</string>\n" |
| 113 |
+ " <string name=\"xliff_string\">First: <xliff:g id=\"firstName\">%1$s</xliff:g> Last: <xliff:g id=\"lastName\">%2$s</xliff:g></string>\n" |
| 114 |
+ " <array name=\"my_array\">\"\n" |
| 115 |
+ " <item>@string/home_title</item>\n" |
| 116 |
+ " <item>value2\n</item>\n" |
| 117 |
+ " <item>value3</item>\n" |
| 118 |
+ " </array>\n" |
| 119 |
+ "</resources>\n", |
| 120 |
|
| 121 |
"values-es/strings.xml", "" |
| 122 |
+ "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" |
| 123 |
+ "<resources>\n" |
| 124 |
+ " <string name=\"show_all_apps\">Todo</string>\n" |
| 125 |
+ "</resources>\n", |
| 126 |
}); |
| 127 |
|
| 128 |
final FolderConfiguration config = FolderConfiguration.getConfigForFolder("values-es-land"); |
| 129 |
assertFalse(appResources.isFramework()); |
| 130 |
assertNotNull(config); |
| 131 |
|
| 132 |
final LayoutLog logger = new LayoutLog() { |
| 133 |
@Override |
| 134 |
public void warning(String tag, String message, Object data) { |
| 135 |
fail(message); |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public void fidelityWarning(String tag, String message, Throwable throwable, |
| 140 |
Object data) { |
| 141 |
fail(message); |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public void error(String tag, String message, Object data) { |
| 146 |
fail(message); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public void error(String tag, String message, Throwable throwable, Object data) { |
| 151 |
fail(message); |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
ResourceItemResolver.ResourceProvider provider = new ResourceItemResolver.ResourceProvider() { |
| 156 |
private ResourceResolver mResolver; |
| 157 |
|
| 158 |
@Nullable |
| 159 |
@Override |
| 160 |
public ResourceResolver getResolver(boolean createIfNecessary) { |
| 161 |
if (mResolver == null && createIfNecessary) { |
| 162 |
Map<ResourceType, Map<String, ResourceValue>> appResourceMap; |
| 163 |
appResourceMap = appResources.getConfiguredResources(config); |
| 164 |
Map<ResourceType, Map<String, ResourceValue>> frameworkResourcesMap = |
| 165 |
frameworkResources.getConfiguredResources(config); |
| 166 |
assertNotNull(appResourceMap); |
| 167 |
mResolver = ResourceResolver.create(appResourceMap, frameworkResourcesMap, |
| 168 |
"MyTheme", true); |
| 169 |
assertNotNull(mResolver); |
| 170 |
mResolver.setLogger(logger); |
| 171 |
} |
| 172 |
|
| 173 |
return mResolver; |
| 174 |
} |
| 175 |
|
| 176 |
@Nullable |
| 177 |
@Override |
| 178 |
public com.android.ide.common.resources.ResourceRepository getFrameworkResources() { |
| 179 |
return frameworkResources; |
| 180 |
} |
| 181 |
|
| 182 |
@Nullable |
| 183 |
@Override |
| 184 |
public ResourceRepository getAppResources() { |
| 185 |
return appResources; |
| 186 |
} |
| 187 |
}; |
| 188 |
|
| 189 |
ResourceItemResolver resolver = new ResourceItemResolver(config, provider, logger); |
| 190 |
|
| 191 |
// findResValue |
| 192 |
assertNotNull(resolver.findResValue("@string/show_all_apps", false)); |
| 193 |
assertNotNull(resolver.findResValue("@android:string/ok", false)); |
| 194 |
assertNotNull(resolver.findResValue("@android:string/ok", true)); |
| 195 |
assertEquals("Todo", resolver.findResValue("@string/show_all_apps", false).getValue()); |
| 196 |
assertEquals("Home Sample", resolver.findResValue("@string/home_title", false).getValue()); |
| 197 |
assertEquals("45%", resolver.findResValue("@dimen/dialog_min_width_major", |
| 198 |
false).getValue()); |
| 199 |
assertNotNull(resolver.findResValue("@android:color/bright_foreground_dark", true)); |
| 200 |
assertEquals("@android:color/background_light", |
| 201 |
resolver.findResValue("@android:color/bright_foreground_dark", true).getValue()); |
| 202 |
assertEquals("#ffffffff", |
| 203 |
resolver.findResValue("@android:color/background_light", true).getValue()); |
| 204 |
|
| 205 |
// resolveResValue |
| 206 |
// android:color/bright_foreground_dark => @android:color/background_light => white |
| 207 |
assertEquals("Todo", resolver.resolveResValue( |
| 208 |
resolver.findResValue("@string/show_all_apps", false)).getValue()); |
| 209 |
assertEquals("#ffffffff", resolver.resolveResValue( |
| 210 |
resolver.findResValue("@android:color/bright_foreground_dark", false)).getValue()); |
| 211 |
|
| 212 |
// Test array values. |
| 213 |
ResourceValue resValue = resolver.findResValue("@array/my_array", false); |
| 214 |
resValue = resolver.resolveResValue(resValue); // test http://b.android.com/187097 |
| 215 |
assertTrue(resValue instanceof ArrayResourceValue); |
| 216 |
assertEquals(3, ((ArrayResourceValue) resValue).getElementCount()); |
| 217 |
assertEquals("@string/home_title", ((ArrayResourceValue) resValue).getElement(0)); |
| 218 |
assertEquals("value2", ((ArrayResourceValue) resValue).getElement(1)); |
| 219 |
assertEquals("value3", ((ArrayResourceValue) resValue).getElement(2)); |
| 220 |
resValue = resolver.findResValue("@android:array/my_fw_array", false); |
| 221 |
assertTrue(resValue instanceof ArrayResourceValue); |
| 222 |
assertEquals(3, ((ArrayResourceValue) resValue).getElementCount()); |
| 223 |
assertEquals("fw_value1", ((ArrayResourceValue) resValue).getElement(0)); |
| 224 |
assertEquals("fw_value2", ((ArrayResourceValue) resValue).getElement(1)); |
| 225 |
assertEquals("fw_value3", ((ArrayResourceValue) resValue).getElement(2)); |
| 226 |
|
| 227 |
|
| 228 |
// Now do everything over again, but this time without a resource resolver. |
| 229 |
// Also set a lookup chain. |
| 230 |
resolver = new ResourceItemResolver(config, frameworkResources, appResources, |
| 231 |
logger); |
| 232 |
List<ResourceValue> chain = Lists.newArrayList(); |
| 233 |
resolver.setLookupChainList(chain); |
| 234 |
|
| 235 |
// findResValue |
| 236 |
assertNotNull(resolver.findResValue("@string/show_all_apps", false)); |
| 237 |
assertNotNull(resolver.findResValue("@android:string/ok", false)); |
| 238 |
assertNotNull(resolver.findResValue("@android:string/ok", true)); |
| 239 |
assertEquals("Todo", resolver.findResValue("@string/show_all_apps", false).getValue()); |
| 240 |
assertEquals("Home Sample", resolver.findResValue("@string/home_title", false).getValue()); |
| 241 |
assertEquals("45%", resolver.findResValue("@dimen/dialog_min_width_major", |
| 242 |
false).getValue()); |
| 243 |
assertNotNull(resolver.findResValue("@android:color/bright_foreground_dark", true)); |
| 244 |
assertEquals("@android:color/background_light", |
| 245 |
resolver.findResValue("@android:color/bright_foreground_dark", true).getValue()); |
| 246 |
assertEquals("#ffffffff", |
| 247 |
resolver.findResValue("@android:color/background_light", true).getValue()); |
| 248 |
assertEquals("Todo", resolver.resolveResValue( |
| 249 |
resolver.findResValue("@string/show_all_apps", false)).getValue()); |
| 250 |
|
| 251 |
chain.clear(); |
| 252 |
ResourceValue v = resolver.findResValue("@android:color/bright_foreground_dark", false); |
| 253 |
assertEquals("@android:color/bright_foreground_dark => @android:color/background_light", |
| 254 |
ResourceItemResolver.getDisplayString(ResourceType.COLOR, "bright_foreground_dark", |
| 255 |
true, chain)); |
| 256 |
assertEquals("First: ${firstName} Last: ${lastName}", |
| 257 |
resolver.findResValue("@string/xliff_string", false).getValue()); |
| 258 |
assertEquals("First: <xliff:g id=\"firstName\">%1$s</xliff:g> Last: <xliff:g id=\"lastName\">%2$s</xliff:g>", |
| 259 |
resolver.findResValue("@string/xliff_string", false).getRawXmlValue()); |
| 260 |
|
| 261 |
chain.clear(); |
| 262 |
assertEquals("#ffffffff", resolver.resolveResValue(v).getValue()); |
| 263 |
assertEquals("@android:color/bright_foreground_dark => @android:color/background_light " |
| 264 |
+ "=> #ffffffff", |
| 265 |
ResourceItemResolver.getDisplayString("@android:color/bright_foreground_dark", |
| 266 |
chain)); |
| 267 |
|
| 268 |
// Try to resolve style attributes |
| 269 |
resolver = new ResourceItemResolver(config, provider, logger); |
| 270 |
resolver.setLookupChainList(chain); |
| 271 |
chain.clear(); |
| 272 |
ResourceValue target = new ResourceValue(ResourceType.STRING, "dummy", false); |
| 273 |
target.setValue("?foo"); |
| 274 |
assertEquals("#ff000000", resolver.resolveResValue(target).getValue()); |
| 275 |
assertEquals("?foo => ?android:colorForeground => @color/bright_foreground_light => " |
| 276 |
+ "@android:color/background_dark => #ff000000", |
| 277 |
ResourceItemResolver.getDisplayString("?foo", chain)); |
| 278 |
|
| 279 |
// Test array values. |
| 280 |
resValue = resolver.findResValue("@array/my_array", false); |
| 281 |
assertTrue(resValue instanceof ArrayResourceValue); |
| 282 |
assertEquals(3, ((ArrayResourceValue) resValue).getElementCount()); |
| 283 |
assertEquals("@string/home_title", ((ArrayResourceValue) resValue).getElement(0)); |
| 284 |
assertEquals("value2", ((ArrayResourceValue) resValue).getElement(1)); |
| 285 |
assertEquals("value3", ((ArrayResourceValue) resValue).getElement(2)); |
| 286 |
resValue = resolver.findResValue("@android:array/my_fw_array", false); |
| 287 |
assertTrue(resValue instanceof ArrayResourceValue); |
| 288 |
assertEquals(3, ((ArrayResourceValue) resValue).getElementCount()); |
| 289 |
assertEquals("fw_value1", ((ArrayResourceValue) resValue).getElement(0)); |
| 290 |
assertEquals("fw_value2", ((ArrayResourceValue) resValue).getElement(1)); |
| 291 |
assertEquals("fw_value3", ((ArrayResourceValue) resValue).getElement(2)); |
| 292 |
|
| 293 |
} |
| 294 |
} |