| 1 |
/* |
| 2 |
* Copyright (C) 2012 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.res2; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertEquals; |
| 20 |
import static org.junit.Assert.assertNotNull; |
| 21 |
|
| 22 |
import com.android.SdkConstants; |
| 23 |
import com.android.ide.common.rendering.api.ResourceValue; |
| 24 |
import com.android.ide.common.resources.configuration.FolderConfiguration; |
| 25 |
import com.android.resources.ResourceType; |
| 26 |
import com.android.testutils.TestUtils; |
| 27 |
import com.google.common.base.Charsets; |
| 28 |
import com.google.common.base.Predicate; |
| 29 |
import com.google.common.collect.Iterables; |
| 30 |
import com.google.common.collect.Maps; |
| 31 |
|
| 32 |
import org.junit.Test; |
| 33 |
import org.w3c.dom.Document; |
| 34 |
|
| 35 |
import java.io.BufferedOutputStream; |
| 36 |
import java.io.File; |
| 37 |
import java.io.FileOutputStream; |
| 38 |
import java.io.IOException; |
| 39 |
import java.io.OutputStreamWriter; |
| 40 |
import java.util.List; |
| 41 |
import java.util.Map; |
| 42 |
|
| 43 |
/** |
| 44 |
*/ |
| 45 |
public class ValueResourceParser2Test extends BaseTestCase { |
| 46 |
|
| 47 |
private static List<ResourceItem> sResources = null; |
| 48 |
|
| 49 |
@Test |
| 50 |
public void testParsedResourcesByCount() throws Exception { |
| 51 |
List<ResourceItem> resources = getParsedResources(); |
| 52 |
|
| 53 |
assertEquals(28, resources.size()); |
| 54 |
} |
| 55 |
|
| 56 |
@Test |
| 57 |
public void testParsedResourcesByName() throws Exception { |
| 58 |
List<ResourceItem> resources = getParsedResources(); |
| 59 |
Map<String, ResourceItem> resourceMap = Maps.newHashMapWithExpectedSize(resources.size()); |
| 60 |
for (ResourceItem item : resources) { |
| 61 |
resourceMap.put(item.getKey(), item); |
| 62 |
} |
| 63 |
|
| 64 |
String[] resourceNames = new String[] { |
| 65 |
"drawable/color_drawable", |
| 66 |
"drawable/drawable_ref", |
| 67 |
"color/color", |
| 68 |
"string/basic_string", |
| 69 |
"string/xliff_string", |
| 70 |
"string/styled_string", |
| 71 |
"string/two", |
| 72 |
"string/many", |
| 73 |
"style/style", |
| 74 |
"array/string_array", |
| 75 |
"array/integer_array", |
| 76 |
"array/my_colors", |
| 77 |
"attr/dimen_attr", |
| 78 |
"attr/string_attr", |
| 79 |
"attr/enum_attr", |
| 80 |
"attr/flag_attr", |
| 81 |
"attr/blah", |
| 82 |
"attr/blah2", |
| 83 |
"attr/flagAttr", |
| 84 |
"declare-styleable/declare_styleable", |
| 85 |
"dimen/dimen", |
| 86 |
"id/item_id", |
| 87 |
"integer/integer", |
| 88 |
"layout/layout_ref", |
| 89 |
"plurals/plurals", |
| 90 |
"plurals/plurals_with_bad_quantity" |
| 91 |
}; |
| 92 |
|
| 93 |
for (String name : resourceNames) { |
| 94 |
assertNotNull(name, resourceMap.get(name)); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
private static class ResourceFinder implements Predicate<ResourceItem> { |
| 99 |
private final String myKeyToFind; |
| 100 |
|
| 101 |
ResourceFinder(String keyToFind) { |
| 102 |
myKeyToFind = keyToFind; |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public boolean apply(ResourceItem input) { |
| 107 |
return input.getKey().equals(myKeyToFind); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
@Test |
| 112 |
public void testParsedResourceByValues() throws Exception { |
| 113 |
List<ResourceItem> resources = getParsedResources(); |
| 114 |
// Test tools:quantity parsing in plurals. |
| 115 |
ResourceItem plurals = Iterables.find(resources, new ResourceFinder("plurals/plurals")); |
| 116 |
ResourceValue pluralsValue = plurals.getResourceValue(false); |
| 117 |
assertNotNull(pluralsValue); |
| 118 |
assertEquals("@string/two", pluralsValue.getValue()); |
| 119 |
|
| 120 |
ResourceItem pluralsWithBadQuantity = Iterables.find(resources, new ResourceFinder("plurals/plurals_with_bad_quantity")); |
| 121 |
ResourceValue pluralsValueBadQuantity = pluralsWithBadQuantity.getResourceValue(false); |
| 122 |
assertNotNull(pluralsValueBadQuantity); |
| 123 |
assertEquals("one", pluralsValueBadQuantity.getValue()); |
| 124 |
|
| 125 |
// Test tools:index parsing in arrays. |
| 126 |
ResourceItem stringArray = Iterables.find(resources, new ResourceFinder("array/string_array")); |
| 127 |
ResourceValue stringArrayValue = stringArray.getResourceValue(false); |
| 128 |
assertNotNull(stringArrayValue); |
| 129 |
assertEquals("GHI", stringArrayValue.getValue()); |
| 130 |
|
| 131 |
ResourceItem integerArray = Iterables.find(resources, new ResourceFinder("array/integer_array")); |
| 132 |
ResourceValue integerArrayValue = integerArray.getResourceValue(false); |
| 133 |
assertNotNull(integerArrayValue); |
| 134 |
assertEquals("3", integerArrayValue.getValue()); |
| 135 |
} |
| 136 |
|
| 137 |
private static List<ResourceItem> getParsedResources() throws MergingException { |
| 138 |
if (sResources == null) { |
| 139 |
File root = TestUtils.getRoot("resources", "baseSet"); |
| 140 |
File values = new File(root, "values"); |
| 141 |
File valuesXml = new File(values, "values.xml"); |
| 142 |
|
| 143 |
ValueResourceParser2 parser = new ValueResourceParser2(valuesXml, null); |
| 144 |
sResources = parser.parseFile(); |
| 145 |
|
| 146 |
// create a fake resource file to allow calling ResourceItem.getKey() |
| 147 |
//noinspection ResultOfObjectAllocationIgnored |
| 148 |
new ResourceFile(valuesXml, sResources, "", new FolderConfiguration()); |
| 149 |
} |
| 150 |
|
| 151 |
return sResources; |
| 152 |
} |
| 153 |
|
| 154 |
@Test |
| 155 |
public void testUtfBom() throws IOException, MergingException { |
| 156 |
File file = File.createTempFile("testUtfBom", SdkConstants.DOT_XML); |
| 157 |
String xml = "" + |
| 158 |
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + |
| 159 |
"<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" + |
| 160 |
" android:layout_width=\"match_parent\"\n" + |
| 161 |
" android:layout_height=\"wrap_content\"\n" + |
| 162 |
" android:orientation=\"vertical\" >\n" + |
| 163 |
"\n" + |
| 164 |
" <Button\n" + |
| 165 |
" android:id=\"@+id/button1\"\n" + |
| 166 |
" android:layout_width=\"wrap_content\"\n" + |
| 167 |
" android:layout_height=\"wrap_content\"\n" + |
| 168 |
" android:text=\"Button\" />\n" + |
| 169 |
" some text\n" + |
| 170 |
"\n" + |
| 171 |
"</LinearLayout>\n"; |
| 172 |
|
| 173 |
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file)); |
| 174 |
OutputStreamWriter writer = new OutputStreamWriter(stream, Charsets.UTF_8); |
| 175 |
stream.write(0xef); |
| 176 |
stream.write(0xbb); |
| 177 |
stream.write(0xbf); |
| 178 |
writer.write(xml); |
| 179 |
writer.close(); |
| 180 |
|
| 181 |
Document document = ValueResourceParser2.parseDocument(file, false); |
| 182 |
assertNotNull(document); |
| 183 |
assertNotNull(document.getDocumentElement()); |
| 184 |
assertEquals("LinearLayout", document.getDocumentElement().getTagName()); |
| 185 |
|
| 186 |
//noinspection ResultOfMethodCallIgnored |
| 187 |
file.delete(); |
| 188 |
} |
| 189 |
|
| 190 |
@Test |
| 191 |
public void testBoolItems() throws IOException, MergingException { |
| 192 |
// BOOL types weren't covered in the baseSet/values.xml, so test that too. |
| 193 |
File file = File.createTempFile("testBoolItems", SdkConstants.DOT_XML); |
| 194 |
String xml = "" + |
| 195 |
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + |
| 196 |
"<resources>\n" + |
| 197 |
"\n" + |
| 198 |
"<bool name=\"truthy\"> true </bool>\n" + |
| 199 |
"<item name=\"not_truthy\" type=\"bool\">false</item>\n" + |
| 200 |
// The parser doesn't really validate if the value is only true or false. |
| 201 |
"<bool name=\"other\">excluded middle</bool>\n" + |
| 202 |
"\n" + |
| 203 |
"</resources>\n"; |
| 204 |
|
| 205 |
BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(file)); |
| 206 |
OutputStreamWriter writer = new OutputStreamWriter(stream, Charsets.UTF_8); |
| 207 |
stream.write(0xef); |
| 208 |
stream.write(0xbb); |
| 209 |
stream.write(0xbf); |
| 210 |
writer.write(xml); |
| 211 |
writer.close(); |
| 212 |
|
| 213 |
ValueResourceParser2 parser = new ValueResourceParser2(file, null); |
| 214 |
List<ResourceItem> items = parser.parseFile(); |
| 215 |
assertEquals(3, items.size()); |
| 216 |
assertEquals(ResourceType.BOOL, items.get(0).getType()); |
| 217 |
assertEquals("truthy", items.get(0).getName()); |
| 218 |
assertEquals(ResourceType.BOOL, items.get(1).getType()); |
| 219 |
assertEquals("not_truthy", items.get(1).getName()); |
| 220 |
assertEquals(ResourceType.BOOL, items.get(2).getType()); |
| 221 |
assertEquals("other", items.get(2).getName()); |
| 222 |
//noinspection ResultOfMethodCallIgnored |
| 223 |
file.delete(); |
| 224 |
} |
| 225 |
} |