SimpleCompilationTest.java revision 8b1da958f181639d33dfaa907c0ee66add2decd6
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
19
20import org.apache.commons.lang3.StringUtils;
21import org.junit.Test;
22
23import java.io.File;
24import java.io.IOException;
25import java.net.URISyntaxException;
26
27import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertNotEquals;
29import static org.junit.Assert.assertTrue;
30
31public class SimpleCompilationTest extends BaseCompilationTest {
32
33    @Test
34    public void listTasks() throws IOException, URISyntaxException, InterruptedException {
35        prepareProject();
36        CompilationResult result = runGradle("tasks");
37        assertEquals(0, result.resultCode);
38        assertTrue("there should not be any errors", StringUtils.isEmpty(result.error));
39        assertTrue("Test sanity, empty project tasks",
40                result.resultContainsText("All tasks runnable from root project"));
41    }
42
43    @Test
44    public void testEmptyCompilation() throws IOException, URISyntaxException, InterruptedException {
45        prepareProject();
46        CompilationResult result = runGradle("assembleDebug");
47        assertEquals(0, result.resultCode);
48        assertTrue("there should not be any errors " + result.error, StringUtils.isEmpty(result.error));
49        assertTrue("Test sanity, should compile fine",
50                result.resultContainsText("BUILD SUCCESSFUL"));
51    }
52
53    @Test
54    public void testUndefinedVariable() throws IOException, URISyntaxException,
55            InterruptedException {
56        prepareProject();
57        copyResourceTo("/layout/undefined_variable_binding.xml", "/app/src/main/res/layout/broken.xml");
58        CompilationResult result = runGradle("assembleDebug");
59        assertNotEquals(0, result.resultCode);
60        assertTrue("Undefined variable",
61                result.errorContainsText("Identifiers must have user defined types from the XML file. myVariable is missing it"));
62    }
63
64    @Test
65    public void testSingleModule() throws IOException, URISyntaxException, InterruptedException {
66        prepareApp(toMap(KEY_DEPENDENCIES, "compile project(':module1')",
67                KEY_SETTINGS_INCLUDES, "include ':app'\ninclude ':module1'"));
68        prepareModule("module1", "com.example.module1", toMap());
69        copyResourceTo("/layout/basic_layout.xml", "/module1/src/main/res/layout/module_layout.xml");
70        copyResourceTo("/layout/basic_layout.xml", "/app/src/main/res/layout/app_layout.xml");
71        CompilationResult result = runGradle("assembleDebug");
72        assertEquals(result.error, 0, result.resultCode);
73    }
74
75    @Test
76    public void testTwoLevelDependency() throws IOException, URISyntaxException, InterruptedException {
77        prepareApp(toMap(KEY_DEPENDENCIES, "compile project(':module1')",
78                KEY_SETTINGS_INCLUDES, "include ':app'\ninclude ':module1'\n"
79                        + "include ':module2'"));
80        prepareModule("module1", "com.example.module1", toMap(KEY_DEPENDENCIES,
81                "compile project(':module2')"));
82        prepareModule("module2", "com.example.module2", toMap());
83        copyResourceTo("/layout/basic_layout.xml", "/module2/src/main/res/layout/module2_layout.xml");
84        copyResourceTo("/layout/basic_layout.xml", "/module1/src/main/res/layout/module1_layout.xml");
85        copyResourceTo("/layout/basic_layout.xml", "/app/src/main/res/layout/app_layout.xml");
86        CompilationResult result = runGradle("assembleDebug");
87        assertEquals(result.error, 0, result.resultCode);
88    }
89}
90