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