| 1 |
/* |
| 2 |
* Copyright (C) 2016 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 |
package com.android.ide.common.res2; |
| 17 |
|
| 18 |
import static org.junit.Assert.assertEquals; |
| 19 |
import static org.junit.Assert.assertNotNull; |
| 20 |
import static org.junit.Assert.assertTrue; |
| 21 |
|
| 22 |
import com.android.resources.ResourceType; |
| 23 |
import com.android.testutils.TestUtils; |
| 24 |
import com.google.common.base.Function; |
| 25 |
import com.google.common.collect.Collections2; |
| 26 |
|
| 27 |
import org.junit.Test; |
| 28 |
|
| 29 |
import java.io.File; |
| 30 |
import java.util.Arrays; |
| 31 |
import java.util.Collection; |
| 32 |
import java.util.List; |
| 33 |
import java.util.TreeSet; |
| 34 |
|
| 35 |
/** |
| 36 |
* Test the IdGeneratingResourceParser. |
| 37 |
*/ |
| 38 |
public class IdGeneratingResourceParserTest extends BaseTestCase { |
| 39 |
|
| 40 |
@Test |
| 41 |
public void testParseLayoutDocument() throws Exception { |
| 42 |
File root = TestUtils.getRoot("resources", "idGenerating"); |
| 43 |
File layout = new File(root, "layout"); |
| 44 |
File layoutFile = new File(layout, "layout_for_id_scan.xml"); |
| 45 |
|
| 46 |
IdGeneratingResourceParser parser = new IdGeneratingResourceParser(layoutFile, "layout_for_id_scan", ResourceType.LAYOUT); |
| 47 |
ResourceItem fileItem = parser.getFileResourceItem(); |
| 48 |
assertEquals(fileItem.getName(), "layout_for_id_scan"); |
| 49 |
assertEquals(fileItem.getType(), ResourceType.LAYOUT); |
| 50 |
|
| 51 |
List<ResourceItem> idItems = parser.getIdResourceItems(); |
| 52 |
assertResourceItemsNames(idItems, |
| 53 |
"header", "image", "styledView", "imageView", "btn_title_refresh", "title_refresh_progress", |
| 54 |
"imageView2", "imageButton", "noteArea", "text2", "nonExistent"); |
| 55 |
} |
| 56 |
|
| 57 |
@Test |
| 58 |
public void testParseMenuDocument() throws Exception { |
| 59 |
File root = TestUtils.getRoot("resources", "idGenerating"); |
| 60 |
File menu = new File(root, "menu"); |
| 61 |
File menuFile = new File(menu, "menu.xml"); |
| 62 |
|
| 63 |
IdGeneratingResourceParser parser = new IdGeneratingResourceParser(menuFile, "menu", ResourceType.MENU); |
| 64 |
|
| 65 |
ResourceItem fileItem = parser.getFileResourceItem(); |
| 66 |
assertEquals(fileItem.getName(), "menu"); |
| 67 |
assertEquals(fileItem.getType(), ResourceType.MENU); |
| 68 |
|
| 69 |
List<ResourceItem> idItems = parser.getIdResourceItems(); |
| 70 |
assertResourceItemsNames(idItems, "item1", "group", "group_item1", "group_item2", "submenu", "submenu_item2"); |
| 71 |
} |
| 72 |
|
| 73 |
@Test |
| 74 |
public void testParseDataBindingDocument() throws Exception { |
| 75 |
File root = TestUtils.getRoot("resources", "idGenerating"); |
| 76 |
File layout = new File(root, "layout"); |
| 77 |
File layoutFile = new File(layout, "layout_with_databinding.xml"); |
| 78 |
|
| 79 |
try { |
| 80 |
new IdGeneratingResourceParser(layoutFile, "layout_with_databinding", ResourceType.LAYOUT); |
| 81 |
assertTrue("Should have thrown exception", true); |
| 82 |
} |
| 83 |
catch (MergingException e) { |
| 84 |
assertEquals("Error: Does not handle data-binding files", e.getMessage()); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
private static void assertResourceItemsNames(Collection<ResourceItem> idItems, String... expected) { |
| 89 |
Collection<String> idNames = Collections2.transform(idItems, new Function<ResourceItem, String>() { |
| 90 |
@Override |
| 91 |
public String apply(ResourceItem input) { |
| 92 |
assertEquals(input.getType(), ResourceType.ID); |
| 93 |
return input.getName(); |
| 94 |
} |
| 95 |
}); |
| 96 |
assertSameElements(idNames, Arrays.asList(expected)); |
| 97 |
} |
| 98 |
|
| 99 |
private static <T> void assertSameElements(Collection<? extends T> collection, Collection<T> expected) { |
| 100 |
assertNotNull(collection); |
| 101 |
assertNotNull(expected); |
| 102 |
assertEquals(new TreeSet<T>(expected), new TreeSet<T>(collection)); |
| 103 |
} |
| 104 |
} |