MultiLayoutVerificationTest.java revision aafbe5a2394ff9826201cca97d3298a9f355e311
1/*
2 * Copyright (C) 2015 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
17package android.databinding.compilationTest;
18
19import org.junit.Test;
20
21import android.databinding.tool.processing.ErrorMessages;
22import android.databinding.tool.processing.ScopedErrorReport;
23import android.databinding.tool.processing.ScopedException;
24import android.databinding.tool.store.Location;
25
26import java.io.File;
27import java.io.IOException;
28import java.net.URISyntaxException;
29import java.util.List;
30
31import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertNotEquals;
33import static org.junit.Assert.assertNotNull;
34import static org.junit.Assert.assertTrue;
35import static org.junit.Assert.fail;
36
37public class MultiLayoutVerificationTest extends BaseCompilationTest {
38    @Test
39    public void testMultipleLayoutFilesWithNameMismatch()
40            throws IOException, URISyntaxException, InterruptedException {
41        prepareProject();
42        copyResourceTo("/layout/layout_with_class_name.xml",
43                "/app/src/main/res/layout/with_class_name.xml", toMap(KEY_CLASS_NAME,
44                        "AClassName"));
45        copyResourceTo("/layout/layout_with_class_name.xml",
46                "/app/src/main/res/layout-land/with_class_name.xml", toMap(KEY_CLASS_NAME,
47                        "SomeOtherClassName"));
48        CompilationResult result = runGradle("assembleDebug");
49        assertNotEquals(result.output, 0, result.resultCode);
50        List<ScopedException> exceptions = result.getBindingExceptions();
51        assertEquals(result.error, 2, exceptions.size());
52        boolean foundNormal = false;
53        boolean foundLandscape = false;
54        for (ScopedException exception : exceptions) {
55            ScopedErrorReport report = exception.getScopedErrorReport();
56            assertNotNull(report);
57            File file = new File(report.getFilePath());
58            assertTrue(file.exists());
59            assertEquals(1, report.getLocations().size());
60            Location location = report.getLocations().get(0);
61            switch (file.getParentFile().getName()) {
62                case "layout":
63                    assertEquals(new File(testFolder,
64                            "/app/src/main/res/layout/with_class_name.xml")
65                            .getCanonicalFile(), file.getCanonicalFile());
66                    String extract = extract("/app/src/main/res/layout/with_class_name.xml",
67                            location);
68                    assertEquals(extract, "AClassName");
69                    assertEquals(String.format(
70                            ErrorMessages.MULTI_CONFIG_LAYOUT_CLASS_NAME_MISMATCH,
71                            DEFAULT_APP_PACKAGE + ".databinding.AClassName",
72                            "layout/with_class_name"), exception.getBareMessage());
73                    foundNormal = true;
74                    break;
75                case "layout-land":
76                    assertEquals(new File(testFolder,
77                            "/app/src/main/res/layout-land/with_class_name.xml")
78                            .getCanonicalFile(), file.getCanonicalFile());
79                    extract = extract("/app/src/main/res/layout-land/with_class_name.xml",
80                            location);
81                    assertEquals("SomeOtherClassName", extract);
82                    assertEquals(String.format(
83                            ErrorMessages.MULTI_CONFIG_LAYOUT_CLASS_NAME_MISMATCH,
84                            DEFAULT_APP_PACKAGE + ".databinding.SomeOtherClassName",
85                            "layout-land/with_class_name"), exception.getBareMessage());
86                    foundLandscape = true;
87                    break;
88                default:
89                    fail("unexpected error file");
90            }
91        }
92        assertTrue(result.error, foundNormal);
93        assertTrue(result.error, foundLandscape);
94    }
95
96    @Test
97    public void testMultipleLayoutFilesVariableMismatch()
98            throws IOException, URISyntaxException, InterruptedException {
99        prepareProject();
100        copyResourceTo("/layout/layout_with_variable_type.xml",
101                "/app/src/main/res/layout/layout_with_variable_type.xml", toMap(KEY_CLASS_TYPE,
102                        "String"));
103        copyResourceTo("/layout/layout_with_variable_type.xml",
104                "/app/src/main/res/layout-land/layout_with_variable_type.xml", toMap(KEY_CLASS_TYPE,
105                        "CharSequence"));
106        CompilationResult result = runGradle("assembleDebug");
107        assertNotEquals(result.output, 0, result.resultCode);
108        List<ScopedException> exceptions = result.getBindingExceptions();
109        assertEquals(result.error, 2, exceptions.size());
110        boolean foundNormal = false;
111        boolean foundLandscape = false;
112        for (ScopedException exception : exceptions) {
113            ScopedErrorReport report = exception.getScopedErrorReport();
114            assertNotNull(report);
115            File file = new File(report.getFilePath());
116            assertTrue(file.exists());
117            assertEquals(result.error, 1, report.getLocations().size());
118            Location location = report.getLocations().get(0);
119            // validated in switch
120            String config = file.getParentFile().getName();
121            String type = "???";
122            switch (file.getParentFile().getName()) {
123                case "layout":
124                    type = "String";
125                    foundNormal = true;
126                    break;
127                case "layout-land":
128                    type = "CharSequence";
129                    foundLandscape = true;
130                    break;
131                default:
132                    fail("unexpected error file");
133            }
134            assertEquals(new File(testFolder,
135                    "/app/src/main/res/" + config + "/layout_with_variable_type.xml")
136                    .getCanonicalFile(), file.getCanonicalFile());
137            String extract = extract("/app/src/main/res/" + config +
138                            "/layout_with_variable_type.xml", location);
139            assertEquals(extract, "<variable name=\"myVariable\" type=\"" + type + "\"/>");
140            assertEquals(String.format(
141                    ErrorMessages.MULTI_CONFIG_VARIABLE_TYPE_MISMATCH,
142                    "myVariable", type,
143                    config + "/layout_with_variable_type"), exception.getBareMessage());
144        }
145        assertTrue(result.error, foundNormal);
146        assertTrue(result.error, foundLandscape);
147    }
148
149    @Test
150    public void testMultipleLayoutFilesImportMismatch()
151            throws IOException, URISyntaxException, InterruptedException {
152        prepareProject();
153        String typeNormal = "java.util.List";
154        String typeLand = "java.util.Map";
155        copyResourceTo("/layout/layout_with_import_type.xml",
156                "/app/src/main/res/layout/layout_with_import_type.xml", toMap(KEY_IMPORT_TYPE,
157                        typeNormal));
158        copyResourceTo("/layout/layout_with_import_type.xml",
159                "/app/src/main/res/layout-land/layout_with_import_type.xml", toMap(KEY_IMPORT_TYPE,
160                        typeLand));
161        CompilationResult result = runGradle("assembleDebug");
162        assertNotEquals(result.output, 0, result.resultCode);
163        List<ScopedException> exceptions = result.getBindingExceptions();
164        assertEquals(result.error, 2, exceptions.size());
165        boolean foundNormal = false;
166        boolean foundLandscape = false;
167        for (ScopedException exception : exceptions) {
168            ScopedErrorReport report = exception.getScopedErrorReport();
169            assertNotNull(report);
170            File file = new File(report.getFilePath());
171            assertTrue(file.exists());
172            assertEquals(result.error, 1, report.getLocations().size());
173            Location location = report.getLocations().get(0);
174            // validated in switch
175            String config = file.getParentFile().getName();
176            String type = "???";
177            switch (file.getParentFile().getName()) {
178                case "layout":
179                    type = typeNormal;
180                    foundNormal = true;
181                    break;
182                case "layout-land":
183                    type = typeLand;
184                    foundLandscape = true;
185                    break;
186                default:
187                    fail("unexpected error file");
188            }
189            assertEquals(new File(testFolder,
190                    "/app/src/main/res/" + config + "/layout_with_import_type.xml")
191                    .getCanonicalFile(), file.getCanonicalFile());
192            String extract = extract("/app/src/main/res/" + config + "/layout_with_import_type.xml",
193                    location);
194            assertEquals(extract, "<import alias=\"Blah\" type=\"" + type + "\"/>");
195            assertEquals(String.format(
196                    ErrorMessages.MULTI_CONFIG_IMPORT_TYPE_MISMATCH,
197                    "Blah", type,
198                    config + "/layout_with_import_type"), exception.getBareMessage());
199        }
200        assertTrue(result.error, foundNormal);
201        assertTrue(result.error, foundLandscape);
202    }
203
204    @Test
205    public void testSameIdInIncludeAndView()
206            throws IOException, URISyntaxException, InterruptedException {
207        prepareProject();
208        copyResourceTo("/layout/basic_layout.xml",
209                "/app/src/main/res/layout/basic_layout.xml");
210        copyResourceTo("/layout/layout_with_include.xml",
211                "/app/src/main/res/layout/foo.xml", toMap(KEY_INCLUDE_ID, "sharedId"));
212        copyResourceTo("/layout/layout_with_view_id.xml",
213                "/app/src/main/res/layout-land/foo.xml", toMap(KEY_VIEW_ID, "sharedId"));
214        CompilationResult result = runGradle("assembleDebug");
215        assertNotEquals(result.output, 0, result.resultCode);
216        List<ScopedException> exceptions = result.getBindingExceptions();
217        assertEquals(result.error, 2, exceptions.size());
218
219        boolean foundNormal = false;
220        boolean foundLandscape = false;
221        for (ScopedException exception : exceptions) {
222            ScopedErrorReport report = exception.getScopedErrorReport();
223            assertNotNull(report);
224            File file = new File(report.getFilePath());
225            assertTrue(file.exists());
226            assertEquals(result.error, 1, report.getLocations().size());
227            Location location = report.getLocations().get(0);
228            // validated in switch
229            String config = file.getParentFile().getName();
230            switch (file.getParentFile().getName()) {
231                case "layout":
232                    String extract = extract("/app/src/main/res/" + config + "/foo.xml", location);
233                    assertEquals(extract, "<include layout=\"@layout/basic_layout\" "
234                            + "android:id=\"@+id/sharedId\" bind:myVariable=\"@{myVariable}\"/>");
235                    foundNormal = true;
236                    break;
237                case "layout-land":
238                    extract = extract("/app/src/main/res/" + config + "/foo.xml", location);
239                    assertEquals(extract, "<TextView android:layout_width=\"wrap_content\" "
240                            + "android:layout_height=\"wrap_content\" android:id=\"@+id/sharedId\" "
241                            + "android:text=\"@{myVariable}\"/>");
242                    foundLandscape = true;
243                    break;
244                default:
245                    fail("unexpected error file");
246            }
247            assertEquals(new File(testFolder,
248                    "/app/src/main/res/" + config + "/foo.xml").getCanonicalFile(),
249                    file.getCanonicalFile());
250            assertEquals(String.format(
251                    ErrorMessages.MULTI_CONFIG_ID_USED_AS_IMPORT, "@+id/sharedId"),
252                    exception.getBareMessage());
253        }
254        assertTrue(result.error, foundNormal);
255        assertTrue(result.error, foundLandscape);
256    }
257
258
259}
260