| 1 |
/* |
| 2 |
* Copyright (C) 2011 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.configuration; |
| 18 |
|
| 19 |
import com.android.ide.common.res2.ResourceFile; |
| 20 |
import com.android.ide.common.res2.ResourceItem; |
| 21 |
import com.android.resources.Density; |
| 22 |
import com.android.resources.ResourceFolderType; |
| 23 |
import com.android.resources.ResourceType; |
| 24 |
import com.android.resources.ScreenOrientation; |
| 25 |
import com.android.resources.ScreenRound; |
| 26 |
import com.android.resources.UiMode; |
| 27 |
import com.google.common.base.Joiner; |
| 28 |
import com.google.common.collect.ImmutableList; |
| 29 |
import com.google.common.collect.ImmutableSet; |
| 30 |
import com.google.common.collect.Lists; |
| 31 |
|
| 32 |
import junit.framework.TestCase; |
| 33 |
|
| 34 |
import java.io.File; |
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.Arrays; |
| 37 |
import java.util.Collections; |
| 38 |
import java.util.List; |
| 39 |
|
| 40 |
public class FolderConfigurationTest extends TestCase { |
| 41 |
|
| 42 |
/* |
| 43 |
* Test createDefault creates all the qualifiers. |
| 44 |
*/ |
| 45 |
public void testCreateDefault() { |
| 46 |
FolderConfiguration defaultConfig = new FolderConfiguration(); |
| 47 |
defaultConfig.createDefault(); |
| 48 |
|
| 49 |
// this is always valid and up to date. |
| 50 |
final int count = FolderConfiguration.getQualifierCount(); |
| 51 |
|
| 52 |
// make sure all the qualifiers were created. |
| 53 |
for (int i = 0 ; i < count ; i++) { |
| 54 |
assertNotNull(defaultConfig.getQualifier(i)); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public void testSimpleResMatch() { |
| 59 |
runConfigMatchTest( |
| 60 |
"en-rGB-port-hdpi-notouch-12key", |
| 61 |
3, |
| 62 |
"", |
| 63 |
"en", |
| 64 |
"fr-rCA", |
| 65 |
"en-port", |
| 66 |
"en-notouch-12key", |
| 67 |
"port-ldpi", |
| 68 |
"port-notouch-12key"); |
| 69 |
} |
| 70 |
|
| 71 |
public void testIsMatchFor() { |
| 72 |
FolderConfiguration en = FolderConfiguration.getConfigForFolder("values-en"); |
| 73 |
FolderConfiguration enUs = FolderConfiguration.getConfigForFolder("values-en-rUS"); |
| 74 |
assertNotNull(en); |
| 75 |
assertNotNull(enUs); |
| 76 |
assertTrue(enUs.isMatchFor(enUs)); |
| 77 |
assertTrue(en.isMatchFor(en)); |
| 78 |
assertTrue(enUs.isMatchFor(en)); |
| 79 |
assertTrue(en.isMatchFor(enUs)); |
| 80 |
} |
| 81 |
|
| 82 |
public void testVersionResMatch() { |
| 83 |
runConfigMatchTest( |
| 84 |
"en-rUS-w600dp-h1024dp-large-port-mdpi-finger-nokeys-v12", |
| 85 |
2, |
| 86 |
"", |
| 87 |
"large", |
| 88 |
"w540dp"); |
| 89 |
} |
| 90 |
|
| 91 |
public void testVersionResMatchWithBcp47() { |
| 92 |
runConfigMatchTest( |
| 93 |
"b+kok+Knda+419+VARIANT-w600dp", |
| 94 |
2, |
| 95 |
"", |
| 96 |
"large", |
| 97 |
"w540dp"); |
| 98 |
} |
| 99 |
|
| 100 |
@SuppressWarnings("ConstantConditions") |
| 101 |
public void testAddQualifier() { |
| 102 |
FolderConfiguration defaultConfig = new FolderConfiguration(); |
| 103 |
defaultConfig.createDefault(); |
| 104 |
|
| 105 |
final int count = FolderConfiguration.getQualifierCount(); |
| 106 |
for (int i = 0 ; i < count ; i++) { |
| 107 |
FolderConfiguration empty = new FolderConfiguration(); |
| 108 |
|
| 109 |
ResourceQualifier q = defaultConfig.getQualifier(i); |
| 110 |
|
| 111 |
empty.addQualifier(q); |
| 112 |
|
| 113 |
// check it was added |
| 114 |
assertNotNull( |
| 115 |
"addQualifier failed for " + q.getClass().getName(), empty.getQualifier(i)); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@SuppressWarnings("ConstantConditions") |
| 120 |
public void testGetConfig1() { |
| 121 |
FolderConfiguration configForFolder = |
| 122 |
FolderConfiguration.getConfig(new String[] { "values", "en", "rUS" }); |
| 123 |
assertNotNull(configForFolder); |
| 124 |
assertEquals("en", configForFolder.getLocaleQualifier().getLanguage()); |
| 125 |
assertEquals("US", configForFolder.getLocaleQualifier().getRegion()); |
| 126 |
assertNull(configForFolder.getScreenDimensionQualifier()); |
| 127 |
assertNull(configForFolder.getLayoutDirectionQualifier()); |
| 128 |
} |
| 129 |
|
| 130 |
@SuppressWarnings("ConstantConditions") |
| 131 |
public void testInvalidRepeats() { |
| 132 |
assertNull(FolderConfiguration.getConfigForFolder("values-en-rUS-rES")); |
| 133 |
} |
| 134 |
|
| 135 |
@SuppressWarnings("ConstantConditions") |
| 136 |
public void testGetConfig2() { |
| 137 |
FolderConfiguration configForFolder = |
| 138 |
FolderConfiguration.getConfigForFolder("values-en-rUS"); |
| 139 |
assertNotNull(configForFolder); |
| 140 |
assertEquals("en", configForFolder.getLocaleQualifier().getLanguage()); |
| 141 |
assertEquals("US", configForFolder.getLocaleQualifier().getRegion()); |
| 142 |
assertNull(configForFolder.getScreenDimensionQualifier()); |
| 143 |
assertNull(configForFolder.getLayoutDirectionQualifier()); |
| 144 |
} |
| 145 |
|
| 146 |
@SuppressWarnings("ConstantConditions") |
| 147 |
public void testGetConfigCaseInsensitive() { |
| 148 |
FolderConfiguration configForFolder = |
| 149 |
FolderConfiguration.getConfigForFolder("values-EN-rus"); |
| 150 |
assertNotNull(configForFolder); |
| 151 |
assertEquals("en", configForFolder.getLocaleQualifier().getLanguage()); |
| 152 |
assertEquals("US", configForFolder.getLocaleQualifier().getRegion()); |
| 153 |
assertNull(configForFolder.getScreenDimensionQualifier()); |
| 154 |
assertNull(configForFolder.getLayoutDirectionQualifier()); |
| 155 |
assertEquals("layout-en-rUS", configForFolder.getFolderName(ResourceFolderType.LAYOUT)); |
| 156 |
|
| 157 |
runConfigMatchTest( |
| 158 |
"en-rgb-Port-HDPI-notouch-12key", |
| 159 |
3, |
| 160 |
"", |
| 161 |
"en", |
| 162 |
"fr-rCA", |
| 163 |
"en-port", |
| 164 |
"en-notouch-12key", |
| 165 |
"port-ldpi", |
| 166 |
"port-notouch-12key"); |
| 167 |
} |
| 168 |
|
| 169 |
public void testToStrings() { |
| 170 |
FolderConfiguration configForFolder = FolderConfiguration.getConfigForFolder("values-en-rUS"); |
| 171 |
assertNotNull(configForFolder); |
| 172 |
assertEquals("Locale en_US", configForFolder.toDisplayString()); |
| 173 |
assertEquals("en,US", configForFolder.toShortDisplayString()); |
| 174 |
assertEquals("layout-en-rUS", configForFolder.getFolderName(ResourceFolderType.LAYOUT)); |
| 175 |
assertEquals("-en-rUS", configForFolder.getUniqueKey()); |
| 176 |
assertEquals("en-rUS", configForFolder.getQualifierString()); |
| 177 |
assertEquals("", new FolderConfiguration().getQualifierString()); |
| 178 |
} |
| 179 |
|
| 180 |
public void testNormalize() { |
| 181 |
// test normal qualifiers that all have the same min SDK |
| 182 |
doTestNormalize(4, "large"); |
| 183 |
doTestNormalize(8, "notnight"); |
| 184 |
doTestNormalize(13, "sw42dp"); |
| 185 |
doTestNormalize(17, "ldrtl"); |
| 186 |
|
| 187 |
// test we take the highest qualifier |
| 188 |
doTestNormalize(13, "sw42dp", "large"); |
| 189 |
|
| 190 |
// test where different values have different minSdk |
| 191 |
/* Ambiguous now that aapt accepts 3 letter language codes; get clarification. |
| 192 |
doTestNormalize(8, "car"); |
| 193 |
*/ |
| 194 |
doTestNormalize(13, "television"); |
| 195 |
doTestNormalize(16, "appliance"); |
| 196 |
|
| 197 |
// test case where there's already a higher -v# qualifier |
| 198 |
doTestNormalize(18, "sw42dp", "v18"); |
| 199 |
|
| 200 |
// finally test that in some cases it won't add a -v# value. |
| 201 |
FolderConfiguration configForFolder = FolderConfiguration.getConfigFromQualifiers( |
| 202 |
Collections.singletonList("port")); |
| 203 |
|
| 204 |
assertNotNull(configForFolder); |
| 205 |
|
| 206 |
configForFolder.normalize(); |
| 207 |
VersionQualifier versionQualifier = configForFolder.getVersionQualifier(); |
| 208 |
assertNull(versionQualifier); |
| 209 |
} |
| 210 |
|
| 211 |
@SuppressWarnings("ConstantConditions") |
| 212 |
public void testConfigMatch() { |
| 213 |
FolderConfiguration ref = new FolderConfiguration(); |
| 214 |
ref.createDefault(); |
| 215 |
ref.setDensityQualifier(new DensityQualifier(Density.XHIGH)); |
| 216 |
ref.addQualifier(new ScreenOrientationQualifier(ScreenOrientation.PORTRAIT)); |
| 217 |
List<Configurable> configurables = getConfigurable( |
| 218 |
"", // No qualifier |
| 219 |
"xhdpi", // A matching qualifier |
| 220 |
"land", // A non matching qualifier |
| 221 |
"xhdpi-v14", // Matching qualifier with ignored qualifier |
| 222 |
"v14" // Ignored qualifier |
| 223 |
); |
| 224 |
// First check that when all qualifiers are present, we match only one resource. |
| 225 |
List<Configurable> matchingConfigurables = ref.findMatchingConfigurables(configurables); |
| 226 |
assertEquals(ImmutableList.of(configurables.get(3)), matchingConfigurables); |
| 227 |
|
| 228 |
// Now remove the version qualifier and check that we "xhdpi" and "xhdpi-v14" |
| 229 |
ref.setVersionQualifier(null); |
| 230 |
matchingConfigurables = ref.findMatchingConfigurables(configurables); |
| 231 |
assertEquals(ImmutableSet.of(configurables.get(1), configurables.get(3)), |
| 232 |
ImmutableSet.copyOf(matchingConfigurables)); |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
public void testIsRoundMatch() { |
| 237 |
FolderConfiguration configForFolder = FolderConfiguration |
| 238 |
.getConfigForFolder("values-en-round"); |
| 239 |
assertNotNull(configForFolder); |
| 240 |
assertNotNull(configForFolder.getScreenRoundQualifier()); |
| 241 |
assertEquals(ScreenRound.ROUND, configForFolder.getScreenRoundQualifier().getValue()); |
| 242 |
runConfigMatchTest("en-rgb-Round-Port-HDPI-notouch-12key", 4, |
| 243 |
"", |
| 244 |
"en", |
| 245 |
"fr-rCa", |
| 246 |
"en-notround-hdpi", |
| 247 |
"en-notouch"); |
| 248 |
|
| 249 |
runConfigMatchTest("en-rgb-Round-Port-HDPI-notouch-12key", 2, |
| 250 |
"", |
| 251 |
"en", |
| 252 |
"en-round-hdpi", |
| 253 |
"port-12key"); |
| 254 |
} |
| 255 |
|
| 256 |
public void testDensityQualifier() { |
| 257 |
|
| 258 |
// test find correct density |
| 259 |
runConfigMatchTest("hdpi", 2, |
| 260 |
"ldpi", |
| 261 |
"mdpi", |
| 262 |
"hdpi", |
| 263 |
"xhdpi"); |
| 264 |
|
| 265 |
// test mdpi matches no-density |
| 266 |
runConfigMatchTest("mdpi", 0, |
| 267 |
"", |
| 268 |
"ldpi", |
| 269 |
"hdpi"); |
| 270 |
|
| 271 |
// test, if there is no no-density, that we match the higher dpi |
| 272 |
runConfigMatchTest("mdpi", 1, |
| 273 |
"ldpi", |
| 274 |
"hdpi"); |
| 275 |
|
| 276 |
// mdpi is better than no-density |
| 277 |
runConfigMatchTest("mdpi", 1, |
| 278 |
"", |
| 279 |
"mdpi", |
| 280 |
"hdpi"); |
| 281 |
runConfigMatchTest("xhdpi", 2, |
| 282 |
"ldpi", |
| 283 |
"", |
| 284 |
"mdpi"); |
| 285 |
|
| 286 |
// scale down better than scale up |
| 287 |
runConfigMatchTest("xhdpi", 4, |
| 288 |
"", |
| 289 |
"ldpi", |
| 290 |
"mdpi", |
| 291 |
"hdpi", |
| 292 |
"xxxhdpi"); |
| 293 |
runConfigMatchTest("hdpi", 3, |
| 294 |
"", |
| 295 |
"ldpi", |
| 296 |
"mdpi", |
| 297 |
"xhdpi", |
| 298 |
"xxhdpi"); |
| 299 |
runConfigMatchTest("mdpi", 0, |
| 300 |
"ldpi", |
| 301 |
"400dpi", |
| 302 |
"xxhdpi", |
| 303 |
"xxxhdpi"); |
| 304 |
} |
| 305 |
|
| 306 |
public void testNullQualifierValidity() { |
| 307 |
FolderConfiguration folderConfiguration = new FolderConfiguration(); |
| 308 |
for (int i = 0; i < FolderConfiguration.getQualifierCount(); i++) { |
| 309 |
ResourceQualifier qualifier = folderConfiguration.getQualifier(i); |
| 310 |
if (qualifier != null) { |
| 311 |
assertFalse(qualifier.isValid()); |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
// --- helper methods |
| 317 |
|
| 318 |
private static final class MockConfigurable implements Configurable { |
| 319 |
|
| 320 |
private final FolderConfiguration mConfig; |
| 321 |
|
| 322 |
MockConfigurable(String config) { |
| 323 |
mConfig = FolderConfiguration.getConfig(getFolderSegments(config)); |
| 324 |
} |
| 325 |
|
| 326 |
@Override |
| 327 |
public FolderConfiguration getConfiguration() { |
| 328 |
return mConfig; |
| 329 |
} |
| 330 |
|
| 331 |
@Override |
| 332 |
public String toString() { |
| 333 |
return mConfig.toString(); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
private static void runConfigMatchTest(String refConfig, int resultIndex, String... configs) { |
| 338 |
FolderConfiguration reference = FolderConfiguration.getConfig(getFolderSegments(refConfig)); |
| 339 |
assertNotNull(reference); |
| 340 |
|
| 341 |
List<Configurable> list = getConfigurable(configs); |
| 342 |
|
| 343 |
Configurable match = reference.findMatchingConfigurable(list); |
| 344 |
assertEquals(resultIndex, list.indexOf(match)); |
| 345 |
} |
| 346 |
|
| 347 |
private static List<Configurable> getConfigurable(String... configs) { |
| 348 |
ArrayList<Configurable> list = new ArrayList<Configurable>(); |
| 349 |
|
| 350 |
for (String config : configs) { |
| 351 |
list.add(new MockConfigurable(config)); |
| 352 |
} |
| 353 |
|
| 354 |
return list; |
| 355 |
} |
| 356 |
|
| 357 |
private static String[] getFolderSegments(String config) { |
| 358 |
return (!config.isEmpty() ? "foo-" + config : "foo").split("-"); |
| 359 |
} |
| 360 |
|
| 361 |
public void testSort1() { |
| 362 |
List<FolderConfiguration> configs = Lists.newArrayList(); |
| 363 |
FolderConfiguration f1 = FolderConfiguration.getConfigForFolder("values-hdpi"); |
| 364 |
FolderConfiguration f2 = FolderConfiguration.getConfigForFolder("values-v11"); |
| 365 |
FolderConfiguration f3 = FolderConfiguration.getConfigForFolder("values-sp"); |
| 366 |
FolderConfiguration f4 = FolderConfiguration.getConfigForFolder("values-v4"); |
| 367 |
configs.add(f1); |
| 368 |
configs.add(f2); |
| 369 |
configs.add(f3); |
| 370 |
configs.add(f4); |
| 371 |
assertEquals(Arrays.asList(f1, f2, f3, f4), configs); |
| 372 |
Collections.sort(configs); |
| 373 |
assertEquals(Arrays.asList(f2, f4, f1, f3), configs); |
| 374 |
} |
| 375 |
|
| 376 |
public void testSort2() { |
| 377 |
// Test case from |
| 378 |
// http://developer.android.com/guide/topics/resources/providing-resources.html#BestMatch |
| 379 |
List<FolderConfiguration> configs = Lists.newArrayList(); |
| 380 |
for (String name : new String[] { |
| 381 |
"drawable", |
| 382 |
"drawable-en", |
| 383 |
"drawable-fr-rCA", |
| 384 |
"drawable-en-port", |
| 385 |
"drawable-en-notouch-12key", |
| 386 |
"drawable-port-ldpi", |
| 387 |
"drawable-port-notouch-12key" |
| 388 |
}) { |
| 389 |
FolderConfiguration config = FolderConfiguration.getConfigForFolder(name); |
| 390 |
assertNotNull(name, config); |
| 391 |
configs.add(config); |
| 392 |
} |
| 393 |
Collections.sort(configs); |
| 394 |
Collections.reverse(configs); |
| 395 |
//assertEquals("", configs.get(0).toDisplayString()); |
| 396 |
|
| 397 |
List<String> strings = Lists.newArrayList(); |
| 398 |
for (FolderConfiguration config : configs) { |
| 399 |
strings.add(config.getUniqueKey()); |
| 400 |
} |
| 401 |
assertEquals("-fr-rCA,-en-port,-en-notouch-12key,-en,-port-ldpi,-port-notouch-12key,", |
| 402 |
Joiner.on(",").skipNulls().join(strings)); |
| 403 |
|
| 404 |
} |
| 405 |
|
| 406 |
private void doTestNormalize(int expectedVersion, String... segments) { |
| 407 |
FolderConfiguration configForFolder = FolderConfiguration.getConfigFromQualifiers( |
| 408 |
Arrays.asList(segments)); |
| 409 |
|
| 410 |
assertNotNull(configForFolder); |
| 411 |
|
| 412 |
configForFolder.normalize(); |
| 413 |
VersionQualifier versionQualifier = configForFolder.getVersionQualifier(); |
| 414 |
assertNotNull(versionQualifier); |
| 415 |
assertEquals(expectedVersion, versionQualifier.getVersion()); |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
public void testCarModeAndLanguage() { |
| 420 |
FolderConfiguration config = FolderConfiguration.getConfigForFolder("values-car"); |
| 421 |
assertNotNull(config); |
| 422 |
assertNull(config.getLocaleQualifier()); |
| 423 |
assertNotNull(config.getUiModeQualifier()); |
| 424 |
assertEquals(UiMode.CAR, config.getUiModeQualifier().getValue()); |
| 425 |
|
| 426 |
config = FolderConfiguration.getConfigForFolder("values-b+car"); |
| 427 |
assertNotNull(config); |
| 428 |
assertNotNull(config.getLocaleQualifier()); |
| 429 |
assertNull(config.getUiModeQualifier()); |
| 430 |
assertEquals("car", config.getLocaleQualifier().getLanguage()); |
| 431 |
} |
| 432 |
|
| 433 |
public void testIsMatchForBcp47() { |
| 434 |
FolderConfiguration blankFolder = FolderConfiguration.getConfigForFolder("values"); |
| 435 |
FolderConfiguration enFolder = FolderConfiguration.getConfigForFolder("values-en"); |
| 436 |
FolderConfiguration deFolder = FolderConfiguration.getConfigForFolder("values-de"); |
| 437 |
FolderConfiguration deBcp47Folder = FolderConfiguration.getConfigForFolder("values-b+de"); |
| 438 |
assertNotNull(enFolder); |
| 439 |
assertNotNull(deFolder); |
| 440 |
assertNotNull(deBcp47Folder); |
| 441 |
assertFalse(enFolder.isMatchFor(deFolder)); |
| 442 |
assertFalse(deFolder.isMatchFor(enFolder)); |
| 443 |
assertFalse(enFolder.isMatchFor(deBcp47Folder)); |
| 444 |
assertFalse(deBcp47Folder.isMatchFor(enFolder)); |
| 445 |
|
| 446 |
assertTrue(enFolder.isMatchFor(blankFolder)); |
| 447 |
assertTrue(deFolder.isMatchFor(blankFolder)); |
| 448 |
assertTrue(deBcp47Folder.isMatchFor(blankFolder)); |
| 449 |
} |
| 450 |
|
| 451 |
public void testFindMatchingConfigurables() { |
| 452 |
ResourceItem itemBlank = new ResourceItem("foo", ResourceType.STRING, null, null) { |
| 453 |
@Override |
| 454 |
public String toString() { |
| 455 |
return "itemBlank"; |
| 456 |
} |
| 457 |
}; |
| 458 |
ResourceFile sourceBlank = ResourceFile.createSingle(new File("sourceBlank"), itemBlank, ""); |
| 459 |
itemBlank.setSource(sourceBlank); |
| 460 |
FolderConfiguration configBlank = itemBlank.getConfiguration(); |
| 461 |
|
| 462 |
ResourceItem itemEn = new ResourceItem("foo", ResourceType.STRING, null, null) { |
| 463 |
@Override |
| 464 |
public String toString() { |
| 465 |
return "itemEn"; |
| 466 |
} |
| 467 |
}; |
| 468 |
ResourceFile sourceEn = ResourceFile.createSingle(new File("sourceEn"), itemBlank, "en"); |
| 469 |
itemEn.setSource(sourceEn); |
| 470 |
FolderConfiguration configEn = itemEn.getConfiguration(); |
| 471 |
|
| 472 |
ResourceItem itemBcpEn = new ResourceItem("foo", ResourceType.STRING, null, null) { |
| 473 |
@Override |
| 474 |
public String toString() { |
| 475 |
return "itemBcpEn"; |
| 476 |
} |
| 477 |
}; |
| 478 |
ResourceFile sourceBcpEn = ResourceFile.createSingle(new File("sourceBcpEn"), itemBlank, "b+en"); |
| 479 |
itemBcpEn.setSource(sourceBcpEn); |
| 480 |
FolderConfiguration configBcpEn = itemBcpEn.getConfiguration(); |
| 481 |
|
| 482 |
ResourceItem itemDe = new ResourceItem("foo", ResourceType.STRING, null, null) { |
| 483 |
@Override |
| 484 |
public String toString() { |
| 485 |
return "itemDe"; |
| 486 |
} |
| 487 |
}; |
| 488 |
|
| 489 |
ResourceFile sourceDe = ResourceFile.createSingle(new File("sourceDe"), itemBlank, "de"); |
| 490 |
itemDe.setSource(sourceDe); |
| 491 |
FolderConfiguration configDe = itemDe.getConfiguration(); |
| 492 |
|
| 493 |
// "" matches everything |
| 494 |
assertEquals(Arrays.<Configurable>asList(itemBlank, itemBcpEn, itemEn, itemDe), |
| 495 |
configBlank.findMatchingConfigurables( |
| 496 |
Arrays.asList(itemBlank, itemBcpEn, itemEn, itemDe))); |
| 497 |
|
| 498 |
// "de" matches only "" and "de" |
| 499 |
assertEquals(Arrays.<Configurable>asList(itemBlank, itemDe), |
| 500 |
configDe.findMatchingConfigurables( |
| 501 |
Arrays.asList(itemBlank, itemBcpEn, itemEn, itemDe))); |
| 502 |
|
| 503 |
// "en" matches "en" and "b+en" |
| 504 |
assertTrue(configEn.isMatchFor(configBcpEn)); |
| 505 |
assertTrue(configBcpEn.isMatchFor(configEn)); |
| 506 |
assertEquals(Arrays.<Configurable>asList(itemBcpEn, itemEn), |
| 507 |
configEn.findMatchingConfigurables( |
| 508 |
Arrays.asList(itemBlank, itemBcpEn, itemEn, itemDe))); |
| 509 |
|
| 510 |
// "b+en" matches "en and "b+en" |
| 511 |
assertEquals(Arrays.<Configurable>asList(itemBcpEn, itemEn), |
| 512 |
configBcpEn.findMatchingConfigurables( |
| 513 |
Arrays.asList(itemBlank, itemBcpEn, itemEn, itemDe))); |
| 514 |
} |
| 515 |
|
| 516 |
public void testFromQualifierString() throws Exception { |
| 517 |
FolderConfiguration blankFolder = FolderConfiguration.getConfigForQualifierString(""); |
| 518 |
FolderConfiguration enFolder = FolderConfiguration.getConfigForQualifierString("en"); |
| 519 |
FolderConfiguration deFolder = FolderConfiguration.getConfigForQualifierString("de"); |
| 520 |
FolderConfiguration deBcp47Folder = FolderConfiguration.getConfigForQualifierString("b+de"); |
| 521 |
FolderConfiguration twoQualifiersFolder = |
| 522 |
FolderConfiguration.getConfigForQualifierString("de-hdpi"); |
| 523 |
|
| 524 |
assertNotNull(enFolder); |
| 525 |
assertNotNull(deFolder); |
| 526 |
assertNotNull(deBcp47Folder); |
| 527 |
assertFalse(enFolder.isMatchFor(deFolder)); |
| 528 |
assertFalse(deFolder.isMatchFor(enFolder)); |
| 529 |
assertFalse(enFolder.isMatchFor(deBcp47Folder)); |
| 530 |
assertFalse(deBcp47Folder.isMatchFor(enFolder)); |
| 531 |
|
| 532 |
assertTrue(enFolder.isMatchFor(blankFolder)); |
| 533 |
assertTrue(deFolder.isMatchFor(blankFolder)); |
| 534 |
assertTrue(deBcp47Folder.isMatchFor(blankFolder)); |
| 535 |
|
| 536 |
assertEquals("de", twoQualifiersFolder.getLocaleQualifier().getLanguage()); |
| 537 |
assertEquals(Density.HIGH, twoQualifiersFolder.getDensityQualifier().getValue()); |
| 538 |
} |
| 539 |
|
| 540 |
public void testCopyOf() throws Exception { |
| 541 |
FolderConfiguration deBcp47Folder = FolderConfiguration.getConfigForFolder("values-b+de"); |
| 542 |
FolderConfiguration copy = FolderConfiguration.copyOf(deBcp47Folder); |
| 543 |
assertTrue(copy.isMatchFor(deBcp47Folder)); |
| 544 |
|
| 545 |
copy.setLocaleQualifier(new LocaleQualifier("en")); |
| 546 |
assertEquals("en", copy.getLocaleQualifier().getLanguage()); |
| 547 |
assertEquals("de", deBcp47Folder.getLocaleQualifier().getLanguage()); |
| 548 |
|
| 549 |
copy.setDensityQualifier(new DensityQualifier(Density.HIGH)); |
| 550 |
assertEquals(Density.HIGH, copy.getDensityQualifier().getValue()); |
| 551 |
assertEquals(new FolderConfiguration().getDensityQualifier(), |
| 552 |
deBcp47Folder.getDensityQualifier()); |
| 553 |
|
| 554 |
FolderConfiguration blankFolder = FolderConfiguration.getConfigForFolder("values"); |
| 555 |
copy = FolderConfiguration.copyOf(blankFolder); |
| 556 |
assertTrue(copy.isMatchFor(blankFolder)); |
| 557 |
|
| 558 |
copy.setVersionQualifier(new VersionQualifier(21)); |
| 559 |
assertEquals(21, copy.getVersionQualifier().getVersion()); |
| 560 |
assertNull(blankFolder.getVersionQualifier()); |
| 561 |
} |
| 562 |
|
| 563 |
public void testScreenSizeMatching() { |
| 564 |
runConfigMatchTest("normal-v21", 2, "", "v21", "normal", "large"); |
| 565 |
runConfigMatchTest("normal-v21", 1, "", "v21", "small", "large"); |
| 566 |
runConfigMatchTest("normal-v21", 0, "", "v23", "small", "large"); |
| 567 |
runConfigMatchTest("large-v21", 3, "", "v21", "small", "large"); |
| 568 |
runConfigMatchTest("large-v21", 1, "", "v21", "small", "xlarge"); |
| 569 |
runConfigMatchTest("small-v21", 2, "", "v21", "small", "xlarge"); |
| 570 |
runConfigMatchTest("small-v21", 1, "", "v21", "normal", "xlarge"); |
| 571 |
} |
| 572 |
} |