Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 1 | /* |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 2 | * Copyright 2019 The Android Open Source Project |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 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 android.processor.view.inspector; |
| 18 | |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.junit.Assert.assertNotNull; |
| 21 | import static org.junit.Assert.fail; |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 22 | |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 23 | |
| 24 | import android.processor.view.inspector.InspectableClassModel.Accessor; |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 25 | import android.processor.view.inspector.InspectableClassModel.IntEnumEntry; |
| 26 | import android.processor.view.inspector.InspectableClassModel.IntFlagEntry; |
| 27 | import android.processor.view.inspector.InspectableClassModel.Property; |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 28 | |
| 29 | import com.google.common.base.Charsets; |
| 30 | import com.google.common.io.Resources; |
| 31 | import com.squareup.javapoet.ClassName; |
| 32 | |
| 33 | import org.junit.Before; |
| 34 | import org.junit.Test; |
| 35 | |
| 36 | import java.io.IOException; |
| 37 | import java.net.URL; |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 38 | import java.util.Arrays; |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 39 | import java.util.Optional; |
| 40 | |
| 41 | /** |
| 42 | * Tests for {@link InspectionCompanionGenerator} |
| 43 | */ |
| 44 | public class InspectionCompanionGeneratorTest { |
| 45 | private static final String RESOURCE_PATH_TEMPLATE = |
| 46 | "android/processor/view/inspector/InspectionCompanionGeneratorTest/%s.java.txt"; |
| 47 | private static final ClassName TEST_CLASS_NAME = |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 48 | ClassName.get("com.android.node", "TestNode"); |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 49 | private InspectableClassModel mModel; |
| 50 | private InspectionCompanionGenerator mGenerator; |
| 51 | |
| 52 | @Before |
| 53 | public void setup() { |
| 54 | mModel = new InspectableClassModel(TEST_CLASS_NAME); |
| 55 | mGenerator = new InspectionCompanionGenerator(null, getClass()); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void testNodeName() { |
| 60 | mModel.setNodeName(Optional.of("NodeName")); |
| 61 | assertGeneratedFileEquals("NodeName"); |
| 62 | } |
| 63 | |
| 64 | @Test |
| 65 | public void testNestedClass() { |
| 66 | mModel = new InspectableClassModel( |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 67 | ClassName.get("com.android.node", "Outer", "Inner")); |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 68 | assertGeneratedFileEquals("NestedClass"); |
| 69 | } |
| 70 | |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 71 | @Test |
| 72 | public void testSimpleProperties() { |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 73 | addProperty("boolean", "getBoolean", Property.Type.BOOLEAN); |
| 74 | addProperty("byte", "getByte", Property.Type.BYTE); |
| 75 | addProperty("char", "getChar", Property.Type.CHAR); |
| 76 | addProperty("double", "getDouble", Property.Type.DOUBLE); |
| 77 | addProperty("float", "getFloat", Property.Type.FLOAT); |
| 78 | addProperty("int", "getInt", Property.Type.INT); |
| 79 | addProperty("long", "getLong", Property.Type.LONG); |
| 80 | addProperty("short", "getShort", Property.Type.SHORT); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 81 | |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 82 | addProperty("object", "getObject", Property.Type.OBJECT); |
| 83 | addProperty("color", "getColor", Property.Type.COLOR); |
| 84 | addProperty("gravity", "getGravity", Property.Type.GRAVITY); |
Ashley Rose | e891481 | 2019-03-05 17:12:00 -0500 | [diff] [blame] | 85 | addProperty("resourceId", "getResourceId", Property.Type.RESOURCE_ID); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 86 | |
| 87 | assertGeneratedFileEquals("SimpleProperties"); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testNoAttributeId() { |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 92 | final Property property = addProperty( |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 93 | "noAttributeProperty", |
| 94 | "getNoAttributeProperty", |
| 95 | Property.Type.INT); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 96 | property.setAttributeIdInferrableFromR(false); |
| 97 | mModel.putProperty(property); |
| 98 | |
| 99 | assertGeneratedFileEquals("NoAttributeId"); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testSuppliedAttributeId() { |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 104 | final Property property = addProperty( |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 105 | "suppliedAttributeProperty", |
| 106 | "getSuppliedAttributeProperty", |
| 107 | Property.Type.INT); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 108 | property.setAttributeId(0xdecafbad); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 109 | |
| 110 | assertGeneratedFileEquals("SuppliedAttributeId"); |
| 111 | } |
| 112 | |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 113 | @Test |
| 114 | public void testIntEnum() { |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 115 | final Property property = addProperty( |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 116 | "intEnumProperty", |
| 117 | "getIntEnumProperty", |
| 118 | Property.Type.INT_ENUM); |
| 119 | |
| 120 | property.setIntEnumEntries(Arrays.asList( |
| 121 | new IntEnumEntry("THREE", 3), |
| 122 | new IntEnumEntry("TWO", 2), |
| 123 | new IntEnumEntry("ONE", 1))); |
| 124 | |
| 125 | mModel.putProperty(property); |
| 126 | |
| 127 | assertGeneratedFileEquals("IntEnum"); |
| 128 | } |
| 129 | |
| 130 | @Test |
| 131 | public void testIntFlag() { |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 132 | final Property property = addProperty( |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 133 | "intFlag", |
| 134 | "getIntFlag", |
| 135 | Property.Type.INT_FLAG); |
| 136 | |
| 137 | property.setAttributeIdInferrableFromR(false); |
| 138 | property.setIntFlagEntries(Arrays.asList( |
| 139 | new IntFlagEntry("TURBO", 0x1, 0x3), |
| 140 | new IntFlagEntry("OVERDRIVE", 0x2, 0x3), |
| 141 | new IntFlagEntry("WARP", 0x4) |
| 142 | )); |
| 143 | |
Ashley Rose | 0b671da | 2019-01-25 15:41:29 -0500 | [diff] [blame] | 144 | assertGeneratedFileEquals("IntFlag"); |
| 145 | } |
| 146 | |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 147 | @Test |
| 148 | public void testFieldProperty() { |
| 149 | mModel.putProperty(new Property( |
| 150 | "fieldProperty", |
| 151 | Accessor.ofField("fieldProperty"), |
| 152 | Property.Type.INT)); |
| 153 | |
| 154 | assertGeneratedFileEquals("FieldProperty"); |
| 155 | } |
| 156 | |
Ashley Rose | c1a4dec | 2018-12-13 18:06:30 -0500 | [diff] [blame] | 157 | private Property addProperty(String name, String getter, Property.Type type) { |
Ashley Rose | 89d6bce | 2019-03-01 19:24:50 -0500 | [diff] [blame] | 158 | final Property property = new Property(name, Accessor.ofGetter(getter), type); |
Ashley Rose | 171a723 | 2018-12-11 17:32:58 -0500 | [diff] [blame] | 159 | mModel.putProperty(property); |
| 160 | return property; |
| 161 | } |
| 162 | |
Ashley Rose | de080eb | 2018-12-07 17:20:25 -0500 | [diff] [blame] | 163 | private void assertGeneratedFileEquals(String fileName) { |
| 164 | assertEquals( |
| 165 | loadTextResource(String.format(RESOURCE_PATH_TEMPLATE, fileName)), |
| 166 | mGenerator.generateFile(mModel).toString()); |
| 167 | } |
| 168 | |
| 169 | private String loadTextResource(String path) { |
| 170 | try { |
| 171 | final URL url = Resources.getResource(path); |
| 172 | assertNotNull(String.format("Resource file not found: %s", path), url); |
| 173 | return Resources.toString(url, Charsets.UTF_8); |
| 174 | } catch (IOException e) { |
| 175 | fail(e.getMessage()); |
| 176 | return null; |
| 177 | } |
| 178 | } |
| 179 | } |