Debian Codegraph @

sdk-common/src/test/java/com/android/ide/common/res2/ResourceRepositoryTest2.java

raw
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.res2;
18
19 import static com.android.SdkConstants.FD_RES;
20 import static com.android.SdkConstants.FD_RES_DRAWABLE;
21 import static com.android.SdkConstants.FD_RES_LAYOUT;
22 import static com.android.SdkConstants.FD_RES_VALUES;
23
24 import com.android.ide.common.rendering.api.ResourceValue;
25 import com.android.ide.common.resources.TestResourceRepository;
26 import com.android.ide.common.resources.configuration.FolderConfiguration;
27 import com.android.ide.common.resources.configuration.LocaleQualifier;
28 import com.android.ide.common.resources.configuration.ScreenOrientationQualifier;
29 import com.android.resources.ResourceType;
30 import com.android.resources.ScreenOrientation;
31 import com.android.testutils.TestUtils;
32 import com.android.utils.ILogger;
33 import com.google.common.base.Charsets;
34 import com.google.common.io.Files;
35
36 import junit.framework.TestCase;
37
38 import java.io.File;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.SortedSet;
44
45 @SuppressWarnings("javadoc")
46 public class ResourceRepositoryTest2 extends TestCase {
47 private File mTempDir;
48 private File mRes;
49 private ResourceMerger mResourceMerger;
50 private ResourceRepository mRepository;
51 private ILogger mLogger;
52
53
54 @SuppressWarnings("ResultOfMethodCallIgnored")
55 @Override
56 protected void setUp() throws Exception {
57 super.setUp();
58
59 mTempDir = TestUtils.createTempDirDeletedOnExit();
60 mRes = new File(mTempDir, FD_RES);
61 mRes.mkdirs();
62 File layout = new File(mRes, FD_RES_LAYOUT);
63 File layoutLand = new File(mRes, FD_RES_LAYOUT + "-land");
64 File values = new File(mRes, FD_RES_VALUES);
65 File valuesEs = new File(mRes, FD_RES_VALUES + "-es");
66 File valuesEsUs = new File(mRes, FD_RES_VALUES + "-es-rUS");
67 File valuesKok = new File(mRes, FD_RES_VALUES + "-b+kok");
68 File valuesKokIn = new File(mRes, FD_RES_VALUES + "-b+kok+IN");
69 File drawable = new File(mRes, FD_RES_DRAWABLE);
70 layout.mkdirs();
71 layoutLand.mkdirs();
72 values.mkdirs();
73 valuesEs.mkdirs();
74 valuesEsUs.mkdirs();
75 valuesKok.mkdirs();
76 valuesKokIn.mkdirs();
77 drawable.mkdirs();
78 new File(layout, "layout1.xml").createNewFile();
79 new File(layoutLand, "layout1.xml").createNewFile();
80 new File(layoutLand, "only_land.xml").createNewFile();
81 new File(layout, "layout2.xml").createNewFile();
82 new File(drawable, "graphic.9.png").createNewFile();
83 File strings = new File(values, "strings.xml");
84 Files.write(""
85 + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
86 + "<resources>\n"
87 + " <item type=\"id\" name=\"action_bar_refresh\" />\n"
88 + " <item type=\"dimen\" name=\"dialog_min_width_major\">45%</item>\n"
89 + " <string name=\"home_title\">Home Sample</string>\n"
90 + " <string name=\"show_all_apps\">All</string>\n"
91 + " <string name=\"menu_wallpaper\">Wallpaper</string>\n"
92 + " <string name=\"menu_search\">Search</string>\n"
93 + " <string name=\"menu_settings\">Settings</string>\n"
94 + " <string name=\"dummy\" translatable=\"false\">Ignore Me</string>\n"
95 + " <string name=\"wallpaper_instructions\">Tap picture to set portrait wallpaper</string>\n"
96 + "</resources>\n", strings, Charsets.UTF_8);
97
98 Files.write(""
99 + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
100 + "<resources>\n"
101 + " <string name=\"show_all_apps\">Todo</string>\n"
102 + "</resources>\n", new File(valuesEs, "strings.xml"), Charsets.UTF_8);
103 Files.write(""
104 + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
105 + "<resources>\n"
106 + " <string name=\"show_all_apps\">Todo</string>\n"
107 + "</resources>\n", new File(valuesEsUs, "strings.xml"), Charsets.UTF_8);
108 Files.write(""
109 + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
110 + "<resources>\n"
111 + " <string name=\"show_all_apps\">Todo</string>\n"
112 + "</resources>\n", new File(valuesKok, "strings.xml"), Charsets.UTF_8);
113 Files.write(""
114 + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
115 + "<resources>\n"
116 + " <string name=\"show_all_apps\">Todo</string>\n"
117 + "</resources>\n", new File(valuesKokIn, "strings.xml"), Charsets.UTF_8);
118
119 if ("testGetMatchingFileAliases".equals(getName())) {
120 Files.write(""
121 + "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
122 + "<resources>\n"
123 + " <item name=\"layout2\" type=\"layout\">@layout/indirect3</item>\n"
124 + " <item name=\"indirect3\" type=\"layout\">@layout/indirect2</item>\n"
125 + " <item name=\"indirect2\" type=\"layout\">@layout/indirect1</item>\n"
126 + " <item name=\"indirect1\" type=\"layout\">@layout/layout1</item>\n"
127 + "</resources>", new File(valuesEsUs, "refs.xml"), Charsets.UTF_8);
128 }
129
130 mResourceMerger = new ResourceMerger(0);
131 ResourceSet resourceSet = new ResourceSet("main", null);
132 resourceSet.addSource(mRes);
133 resourceSet.loadFromFiles(mLogger = new RecordingLogger());
134 mResourceMerger.addDataSet(resourceSet);
135
136 mRepository = new ResourceRepository(false);
137 mResourceMerger.mergeData(mRepository.createMergeConsumer(), true /*doCleanUp*/);
138 }
139
140 @Override
141 protected void tearDown() throws Exception {
142 super.tearDown();
143
144 deleteFile(mTempDir);
145 }
146
147 private static void deleteFile(File dir) {
148 if (dir.isDirectory()) {
149 File[] files = dir.listFiles();
150 if (files != null) {
151 for (File f : files) {
152 deleteFile(f);
153 }
154 }
155 } else if (dir.isFile()) {
156 assertTrue(dir.getPath(), dir.delete());
157 }
158 }
159
160 public void testBasic() throws Exception {
161 assertFalse(mRepository.hasResourceItem("@layout/layout0"));
162 assertTrue(mRepository.hasResourceItem("@layout/layout1"));
163 assertFalse(mRepository.hasResourceItem(ResourceType.LAYOUT, "layout0"));
164 assertTrue(mRepository.hasResourceItem(ResourceType.LAYOUT, "layout1"));
165 assertFalse(mRepository.hasResourceItem(ResourceType.STRING, "layout1"));
166 assertTrue(mRepository.hasResourceItem(ResourceType.STRING, "home_title"));
167 assertFalse(mRepository.hasResourceItem(ResourceType.STRING, "home_title2"));
168 assertFalse(mRepository.hasResourceItem(ResourceType.DRAWABLE, "graph"));
169 assertTrue(mRepository.hasResourceItem(ResourceType.DRAWABLE, "graphic"));
170 assertTrue(mRepository.hasResourceItem("@id/action_bar_refresh"));
171 assertTrue(mRepository.hasResourceItem("@drawable/graphic"));
172 assertTrue(mRepository.hasResourcesOfType(ResourceType.DRAWABLE));
173 assertFalse(mRepository.hasResourcesOfType(ResourceType.ANIM));
174
175 List<ResourceType> availableResourceTypes = mRepository.getAvailableResourceTypes();
176 assertEquals(5, availableResourceTypes.size()); // layout, string, drawable, id, dimen
177
178 Collection<String> allStrings = mRepository.getItemsOfType(ResourceType.STRING);
179 assertEquals(7, allStrings.size());
180
181 List<ResourceItem> itemList = mRepository.getResourceItem(ResourceType.STRING, "menu_settings");
182 assertNotNull(itemList);
183 assertEquals(1, itemList.size());
184 for (ResourceItem item : itemList) {
185 assertEquals("menu_settings", item.getName());
186 assertEquals("@string/menu_settings", item.getXmlString(ResourceType.STRING, false));
187 }
188 //assertTrue(item.hasDefault());
189
190 itemList = mRepository.getResourceItem(ResourceType.STRING, "show_all_apps");
191 assertNotNull(itemList);
192 assertTrue(itemList.size() > 1);
193 for (ResourceItem item : itemList) {
194 assertEquals("show_all_apps", item.getName());
195 assertEquals("@string/show_all_apps", item.getXmlString(ResourceType.STRING, false));
196 }
197 //assertTrue(item.hasDefault());
198 FolderConfiguration folderConfig = new FolderConfiguration();
199 folderConfig.setLocaleQualifier(LocaleQualifier.getQualifier("en"));
200 Map<ResourceType, Map<String, ResourceValue>> configuredItems = mRepository
201 .getConfiguredResources(folderConfig);
202 ResourceValue value = configuredItems.get(ResourceType.STRING).get("show_all_apps");
203 assertNotNull(value);
204 assertEquals("All", value.getValue());
205 assertSame(ResourceType.STRING, value.getResourceType());
206
207 folderConfig = new FolderConfiguration();
208 folderConfig.setLocaleQualifier(LocaleQualifier.getQualifier("es"));
209 configuredItems = mRepository.getConfiguredResources(folderConfig);
210 value = configuredItems.get(ResourceType.STRING).get("show_all_apps");
211 assertNotNull(value);
212 assertEquals("Todo", value.getValue());
213 assertSame(ResourceType.STRING, value.getResourceType());
214
215 itemList = mRepository.getResourceItem(ResourceType.LAYOUT, "only_land");
216 assertNotNull(itemList);
217 //assertFalse(item.hasDefault());
218 assertEquals(1, itemList.size());
219 ResourceFile resourceFile = itemList.get(0).getSource();
220 assertEquals("only_land.xml", resourceFile.getFile().getName());
221 assertEquals(ScreenOrientation.LANDSCAPE.getResourceValue(), resourceFile.getQualifiers());
222
223 itemList = mRepository.getResourceItem(ResourceType.LAYOUT, "layout1");
224 assertNotNull(itemList);
225 //assertTrue(item.hasDefault());
226 assertEquals(2, itemList.size());
227
228 SortedSet<String> languages = mRepository.getLanguages();
229 assertEquals(2, languages.size());
230 assertTrue(languages.contains("es"));
231 assertTrue(languages.contains("kok"));
232 assertEquals(Collections.singleton("US"), mRepository.getRegions("es"));
233 assertEquals(Collections.singleton("IN"), mRepository.getRegions("kok"));
234
235 // List<ResourceFile> layouts = mRepository.getSourceFiles(ResourceType.LAYOUT, "layout1",
236 // folderConfig);
237 // assertNotNull(layouts);
238 // assertEquals(1, layouts.size());
239 // com.android.ide.common.resources.ResourceFile file1 = layouts.get(0);
240 // assertEquals("layout1.xml", file1.getFile().getName());
241 // assertSame(mRepository, file1.getRepository());
242 }
243
244 public void testGetConfiguredResources() throws Exception {
245 FolderConfiguration folderConfig = new FolderConfiguration();
246 folderConfig.setLocaleQualifier(LocaleQualifier.getQualifier("es"));
247 folderConfig.setScreenOrientationQualifier(
248 new ScreenOrientationQualifier(ScreenOrientation.LANDSCAPE));
249
250 Map<ResourceType, Map<String, ResourceValue>> configuredResources =
251 mRepository.getConfiguredResources(folderConfig);
252 Map<String, ResourceValue> strings = configuredResources.get(ResourceType.STRING);
253 Map<String, ResourceValue> layouts = configuredResources.get(ResourceType.LAYOUT);
254 Map<String, ResourceValue> ids = configuredResources.get(ResourceType.ID);
255 Map<String, ResourceValue> dimens = configuredResources.get(ResourceType.DIMEN);
256 assertEquals(1, ids.size());
257 assertEquals(1, dimens.size());
258 assertEquals("dialog_min_width_major", dimens.get("dialog_min_width_major").getName());
259 assertEquals("45%", dimens.get("dialog_min_width_major").getValue());
260 assertEquals("Todo", strings.get("show_all_apps").getValue());
261 assertEquals(3, layouts.size());
262 assertNotNull(layouts.get("layout1"));
263
264 ResourceFile file = mRepository.getMatchingFile("dialog_min_width_major",
265 ResourceType.DIMEN, folderConfig);
266 assertNotNull(file);
267 // file = mRepository.getMatchingFile("dialog_min_width_major", ResourceFolderType.VALUES,
268 // folderConfig);
269 // assertNotNull(file);
270 // file = mRepository.getMatchingFile("layout1", ResourceFolderType.LAYOUT, folderConfig);
271 // assertNotNull(file);
272 file = mRepository.getMatchingFile("layout1", ResourceType.LAYOUT, folderConfig);
273 assertNotNull(file);
274 }
275
276 public void testGetMatchingFileAliases() throws Exception {
277 FolderConfiguration folderConfig = new FolderConfiguration();
278 folderConfig.setLocaleQualifier(LocaleQualifier.getQualifier("es"));
279 folderConfig.setScreenOrientationQualifier(
280 new ScreenOrientationQualifier(ScreenOrientation.LANDSCAPE));
281
282 Map<ResourceType, Map<String, ResourceValue>> configuredResources =
283 mRepository.getConfiguredResources(folderConfig);
284 Map<String, ResourceValue> layouts = configuredResources.get(ResourceType.LAYOUT);
285 assertEquals(6, layouts.size());
286 assertNotNull(layouts.get("layout1"));
287
288 folderConfig.setLocaleQualifier(LocaleQualifier.getQualifier("es-rES"));
289 ResourceFile file = mRepository.getMatchingFile("dialog_min_width_major",
290 ResourceType.DIMEN, folderConfig);
291 assertNotNull(file);
292 file = mRepository.getMatchingFile("layout2", ResourceType.LAYOUT, folderConfig);
293 assertNotNull(file);
294 assertEquals("layout2.xml", file.getFile().getName());
295 assertEquals("", file.getQualifiers());
296
297 folderConfig.setLocaleQualifier(LocaleQualifier.getQualifier("es-rUS"));
298 file = mRepository.getMatchingFile("layout2", ResourceType.LAYOUT, folderConfig);
299 assertNotNull(file);
300 assertEquals("layout1.xml", file.getFile().getName());
301 assertEquals("land", file.getQualifiers());
302 }
303
304 public void testUpdates() throws Exception {
305 assertFalse(mRepository.hasResourcesOfType(ResourceType.ANIM));
306 assertFalse(mRepository.hasResourcesOfType(ResourceType.MENU));
307 assertFalse(mRepository.hasResourcesOfType(ResourceType.BOOL));
308
309 assertTrue(mRepository.hasResourcesOfType(ResourceType.DRAWABLE));
310 assertTrue(mRepository.hasResourceItem("@drawable/graphic"));
311
312 // Delete the drawable graphic
313 ResourceSet resourceSet = mResourceMerger.getDataSets().get(0);
314
315 File drawableFolder = new File(mRes, FD_RES_DRAWABLE);
316 File graphicFile = new File(drawableFolder, "graphic.9.png");
317
318 resourceSet.updateWith(mRes, graphicFile, FileStatus.REMOVED, mLogger);
319 mResourceMerger.mergeData(mRepository.createMergeConsumer(), true /*doCleanUp*/);
320
321 assertFalse(mRepository.hasResourceItem("@drawable/graphic"));
322 assertFalse(mRepository.hasResourcesOfType(ResourceType.DRAWABLE));
323
324 // Delete one of the overridden layouts
325 List<ResourceItem> itemList = mRepository.getResourceItem(ResourceType.LAYOUT, "layout1");
326 assertNotNull(itemList);
327 assertTrue(itemList.size() > 1);
328 assertTrue(mRepository.hasResourcesOfType(ResourceType.LAYOUT));
329 assertTrue(mRepository.hasResourceItem("@layout/layout1"));
330
331 File layoutFolder = new File(mRes, FD_RES_LAYOUT + "-land");
332 File layoutFile = new File(layoutFolder, "layout1.xml");
333
334 resourceSet.updateWith(mRes, layoutFile, FileStatus.REMOVED, mLogger);
335 mResourceMerger.mergeData(mRepository.createMergeConsumer(), true /*doCleanUp*/);
336
337 // We still have a layout1: only default now
338 assertTrue(mRepository.hasResourceItem("@layout/layout1"));
339 itemList = mRepository.getResourceItem(ResourceType.LAYOUT, "layout1");
340 assertNotNull(itemList);
341 assertEquals(1, itemList.size());
342
343 // change strings
344 assertTrue(mRepository.hasResourceItem("@string/dummy"));
345 assertFalse(mRepository.hasResourceItem("@string/myDummy"));
346 itemList = mRepository.getResourceItem(ResourceType.STRING, "dummy");
347 assertNotNull(itemList);
348 assertNotNull(itemList.get(0));
349 ResourceFile stringResFile = itemList.get(0).getSource();
350 File stringFile = stringResFile.getFile();
351 assertTrue(stringFile.exists());
352 String strings = Files.toString(stringFile, Charsets.UTF_8);
353 assertNotNull(strings);
354 strings = strings.replace("name=\"dummy\"", "name=\"myDummy\"");
355 Files.write(strings, stringFile, Charsets.UTF_8);
356
357 resourceSet.updateWith(mRes, stringFile, FileStatus.CHANGED, mLogger);
358 mResourceMerger.mergeData(mRepository.createMergeConsumer(), true /*doCleanUp*/);
359
360 assertTrue(mRepository.hasResourceItem("@string/myDummy"));
361 assertFalse(mRepository.hasResourceItem("@string/dummy"));
362
363 // add files
364 assertFalse(mRepository.hasResourceItem("@layout/layout5"));
365 File layout = new File(mRes, FD_RES_LAYOUT);
366 File newFile = new File(layout, "layout5.xml");
367 boolean created = newFile.createNewFile();
368 assertTrue(created);
369 resourceSet.updateWith(mRes, newFile, FileStatus.NEW, mLogger);
370 mResourceMerger.mergeData(mRepository.createMergeConsumer(), true /*doCleanUp*/);
371 assertTrue(mRepository.hasResourceItem("@layout/layout5"));
372 }
373
374 @SuppressWarnings("ConstantConditions")
375 public void testXliff() throws Exception {
376 ResourceRepository resources = TestResourceRepository.createRes2(false, new Object[]{
377 "values/strings.xml", ""
378 + "<resources xmlns:xliff=\"urn:oasis:names:tc:xliff:document:1.2\" >\n"
379 + " <string name=\"share_with_application\">\n"
380 + " Share your score of <xliff:g id=\"score\" example=\"1337\">%1$s</xliff:g>\n"
381 + " with <xliff:g id=\"application_name\" example=\"Bluetooth\">%2$s</xliff:g>!\n"
382 + " </string>\n"
383 + " <string name=\"callDetailsDurationFormat\"><xliff:g id=\"minutes\" example=\"42\">%s</xliff:g> mins <xliff:g id=\"seconds\" example=\"28\">%s</xliff:g> secs</string>\n"
384 + " <string name=\"description_call\">Call <xliff:g id=\"name\">%1$s</xliff:g></string>\n"
385 + " <string name=\"other\"><xliff:g id=\"number_of_sessions\">%1$s</xliff:g> sessions removed from your schedule</string>\n"
386 + " <!-- Format string used to add a suffix like \"KB\" or \"MB\" to a number\n"
387 + " to display a size in kilobytes, megabytes, or other size units.\n"
388 + " Some languages (like French) will want to add a space between\n"
389 + " the placeholders. -->\n"
390 + " <string name=\"fileSizeSuffix\"><xliff:g id=\"number\" example=\"123\">%1$s</xliff:g><xliff:g id=\"unit\" example=\"KB\">%2$s</xliff:g></string>"
391 + "</resources>\n"
392 });
393 assertFalse(resources.isFramework());
394 assertNotNull(resources);
395
396 assertNotNull(resources);
397 assertEquals("Share your score of (1337) with (Bluetooth)!",
398 resources.getResourceItem(ResourceType.STRING, "share_with_application").get(0).getResourceValue(false).getValue());
399 assertEquals("Call ${name}",
400 resources.getResourceItem(ResourceType.STRING, "description_call").get(0).getResourceValue(false).getValue());
401 assertEquals("(42) mins (28) secs",
402 resources.getResourceItem(ResourceType.STRING, "callDetailsDurationFormat").get(0).getResourceValue(false).getValue());
403 assertEquals("${number_of_sessions} sessions removed from your schedule",
404 resources.getResourceItem(ResourceType.STRING, "other").get(0).getResourceValue(false).getValue());
405 assertEquals("(123)(KB)",
406 resources.getResourceItem(ResourceType.STRING, "fileSizeSuffix").get(0).getResourceValue(false).getValue());
407
408 }
409 }