blob: f6d8bb0939db0d0a58ee1f7b6a01dda128de55ef [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 Rose0b671da2019-01-25 15:41:29 -050023import android.processor.view.inspector.InspectableClassModel.IntEnumEntry;
24import android.processor.view.inspector.InspectableClassModel.IntFlagEntry;
25import android.processor.view.inspector.InspectableClassModel.Property;
Ashley Rosede080eb2018-12-07 17:20:25 -050026
27import com.google.common.base.Charsets;
28import com.google.common.io.Resources;
29import com.squareup.javapoet.ClassName;
30
31import org.junit.Before;
32import org.junit.Test;
33
34import java.io.IOException;
35import java.net.URL;
Ashley Rose0b671da2019-01-25 15:41:29 -050036import java.util.Arrays;
Ashley Rosede080eb2018-12-07 17:20:25 -050037import java.util.Optional;
38
39/**
40 * Tests for {@link InspectionCompanionGenerator}
41 */
42public 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 Rose0b671da2019-01-25 15:41:29 -050046 ClassName.get("com.android.node", "TestNode");
Ashley Rosede080eb2018-12-07 17:20:25 -050047 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 Rose0b671da2019-01-25 15:41:29 -050065 ClassName.get("com.android.node", "Outer", "Inner"));
Ashley Rosede080eb2018-12-07 17:20:25 -050066 assertGeneratedFileEquals("NestedClass");
67 }
68
Ashley Rose171a7232018-12-11 17:32:58 -050069 @Test
70 public void testSimpleProperties() {
Ashley Rosec1a4dec2018-12-13 18:06:30 -050071 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 Rose171a7232018-12-11 17:32:58 -050079
Ashley Rosec1a4dec2018-12-13 18:06:30 -050080 addProperty("object", "getObject", Property.Type.OBJECT);
81 addProperty("color", "getColor", Property.Type.COLOR);
82 addProperty("gravity", "getGravity", Property.Type.GRAVITY);
Ashley Rose171a7232018-12-11 17:32:58 -050083
84 assertGeneratedFileEquals("SimpleProperties");
85 }
86
87 @Test
88 public void testNoAttributeId() {
Ashley Rosec1a4dec2018-12-13 18:06:30 -050089 final Property property = new Property(
90 "noAttributeProperty",
91 "getNoAttributeProperty",
92 Property.Type.INT);
Ashley Rose171a7232018-12-11 17:32:58 -050093 property.setAttributeIdInferrableFromR(false);
94 mModel.putProperty(property);
95
96 assertGeneratedFileEquals("NoAttributeId");
97 }
98
99 @Test
100 public void testSuppliedAttributeId() {
Ashley Rosec1a4dec2018-12-13 18:06:30 -0500101 final Property property = new Property(
102 "suppliedAttributeProperty",
103 "getSuppliedAttributeProperty",
104 Property.Type.INT);
Ashley Rose171a7232018-12-11 17:32:58 -0500105 property.setAttributeId(0xdecafbad);
106 mModel.putProperty(property);
107
108 assertGeneratedFileEquals("SuppliedAttributeId");
109 }
110
Ashley Rose0b671da2019-01-25 15:41:29 -0500111 @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 Rosec1a4dec2018-12-13 18:06:30 -0500147 private Property addProperty(String name, String getter, Property.Type type) {
148 final Property property = new Property(name, getter, type);
Ashley Rose171a7232018-12-11 17:32:58 -0500149 mModel.putProperty(property);
150 return property;
151 }
152
Ashley Rosede080eb2018-12-07 17:20:25 -0500153 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}