blob: 4eed5040eb5186e9aacedb7acc785eaf6f5a1b65 [file] [log] [blame]
Ashley Rosede080eb2018-12-07 17:20:25 -05001/*
Ashley Rosec1a4dec2018-12-13 18:06:30 -05002 * Copyright 2019 The Android Open Source Project
Ashley Rosede080eb2018-12-07 17:20:25 -05003 *
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
17package android.processor.view.inspector;
18
Ashley Rose0b671da2019-01-25 15:41:29 -050019import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNotNull;
21import static org.junit.Assert.fail;
Ashley Rose171a7232018-12-11 17:32:58 -050022
Ashley Rose89d6bce2019-03-01 19:24:50 -050023
24import android.processor.view.inspector.InspectableClassModel.Accessor;
Ashley Rose0b671da2019-01-25 15:41:29 -050025import android.processor.view.inspector.InspectableClassModel.IntEnumEntry;
26import android.processor.view.inspector.InspectableClassModel.IntFlagEntry;
27import android.processor.view.inspector.InspectableClassModel.Property;
Ashley Rosede080eb2018-12-07 17:20:25 -050028
29import com.google.common.base.Charsets;
30import com.google.common.io.Resources;
31import com.squareup.javapoet.ClassName;
32
33import org.junit.Before;
34import org.junit.Test;
35
36import java.io.IOException;
37import java.net.URL;
Ashley Rose0b671da2019-01-25 15:41:29 -050038import java.util.Arrays;
Ashley Rosede080eb2018-12-07 17:20:25 -050039import java.util.Optional;
40
41/**
42 * Tests for {@link InspectionCompanionGenerator}
43 */
44public 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 Rose0b671da2019-01-25 15:41:29 -050048 ClassName.get("com.android.node", "TestNode");
Ashley Rosede080eb2018-12-07 17:20:25 -050049 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 Rose0b671da2019-01-25 15:41:29 -050067 ClassName.get("com.android.node", "Outer", "Inner"));
Ashley Rosede080eb2018-12-07 17:20:25 -050068 assertGeneratedFileEquals("NestedClass");
69 }
70
Ashley Rose171a7232018-12-11 17:32:58 -050071 @Test
72 public void testSimpleProperties() {
Ashley Rosec1a4dec2018-12-13 18:06:30 -050073 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 Rose171a7232018-12-11 17:32:58 -050081
Ashley Rosec1a4dec2018-12-13 18:06:30 -050082 addProperty("object", "getObject", Property.Type.OBJECT);
83 addProperty("color", "getColor", Property.Type.COLOR);
84 addProperty("gravity", "getGravity", Property.Type.GRAVITY);
Ashley Rosee8914812019-03-05 17:12:00 -050085 addProperty("resourceId", "getResourceId", Property.Type.RESOURCE_ID);
Ashley Rose171a7232018-12-11 17:32:58 -050086
87 assertGeneratedFileEquals("SimpleProperties");
88 }
89
90 @Test
91 public void testNoAttributeId() {
Ashley Rose89d6bce2019-03-01 19:24:50 -050092 final Property property = addProperty(
Ashley Rosec1a4dec2018-12-13 18:06:30 -050093 "noAttributeProperty",
94 "getNoAttributeProperty",
95 Property.Type.INT);
Ashley Rose171a7232018-12-11 17:32:58 -050096 property.setAttributeIdInferrableFromR(false);
97 mModel.putProperty(property);
98
99 assertGeneratedFileEquals("NoAttributeId");
100 }
101
102 @Test
103 public void testSuppliedAttributeId() {
Ashley Rose89d6bce2019-03-01 19:24:50 -0500104 final Property property = addProperty(
Ashley Rosec1a4dec2018-12-13 18:06:30 -0500105 "suppliedAttributeProperty",
106 "getSuppliedAttributeProperty",
107 Property.Type.INT);
Ashley Rose171a7232018-12-11 17:32:58 -0500108 property.setAttributeId(0xdecafbad);
Ashley Rose171a7232018-12-11 17:32:58 -0500109
110 assertGeneratedFileEquals("SuppliedAttributeId");
111 }
112
Ashley Rose0b671da2019-01-25 15:41:29 -0500113 @Test
114 public void testIntEnum() {
Ashley Rose89d6bce2019-03-01 19:24:50 -0500115 final Property property = addProperty(
Ashley Rose0b671da2019-01-25 15:41:29 -0500116 "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 Rose89d6bce2019-03-01 19:24:50 -0500132 final Property property = addProperty(
Ashley Rose0b671da2019-01-25 15:41:29 -0500133 "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 Rose0b671da2019-01-25 15:41:29 -0500144 assertGeneratedFileEquals("IntFlag");
145 }
146
Ashley Rose89d6bce2019-03-01 19:24:50 -0500147 @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 Rosec1a4dec2018-12-13 18:06:30 -0500157 private Property addProperty(String name, String getter, Property.Type type) {
Ashley Rose89d6bce2019-03-01 19:24:50 -0500158 final Property property = new Property(name, Accessor.ofGetter(getter), type);
Ashley Rose171a7232018-12-11 17:32:58 -0500159 mModel.putProperty(property);
160 return property;
161 }
162
Ashley Rosede080eb2018-12-07 17:20:25 -0500163 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}