summaryrefslogtreecommitdiff
path: root/build-system/integration-test/connected/src/test/java/com/android/build/gradle/integration/connected/application/JacocoConnectedTest.java
blob: 969e1dda99601d1f17abc8774fae04f07e8caad0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.build.gradle.integration.connected.application;

import static com.android.testutils.truth.PathSubject.assertThat;

import com.android.build.gradle.integration.common.fixture.GradleTestProject;
import com.android.build.gradle.integration.common.fixture.app.KotlinHelloWorldApp;
import com.android.build.gradle.integration.common.utils.TestFileUtils;
import com.android.build.gradle.integration.connected.utils.EmulatorUtils;
import com.android.utils.FileUtils;
import com.google.common.truth.Truth;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Pattern;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExternalResource;

public class JacocoConnectedTest {

    @ClassRule public static final ExternalResource emulator = EmulatorUtils.getEmulator();

    @Rule
    public final GradleTestProject project =
            GradleTestProject.builder()
                    .fromTestApp(KotlinHelloWorldApp.forPlugin("com.android.application"))
                    .create();

    @Before
    public void setUp() throws IOException {
        TestFileUtils.appendToFile(
                project.getBuildFile(),
                "\nandroid.buildTypes.debug.enableAndroidTestCoverage true");
        // fail fast if no response
        project.addAdbTimeout();
        // run the uninstall tasks in order to (1) make sure nothing is installed at the beginning
        // of each test and (2) check the adb connection before taking the time to build anything.
        project.execute("uninstallAll");
    }

    @Test
    public void connectedCheck() throws Exception {
        project.executor().run("connectedCheck");
        assertThat(project.file("build/reports/coverage/androidTest/debug/connected/index.html")).exists();
        assertThat(
                        project.file(
                                "build/reports/coverage/androidTest/debug/connected/com.example.helloworld/HelloWorld.html"))
                .exists();
    }

    @Test
    public void connectedCheckNamespacedRClasses() throws Exception {
        TestFileUtils.appendToFile(
                project.getBuildFile(), "android.aaptOptions.namespaced = true\n");

        project.executor().run("connectedCheck");

        assertThat(
                        project.file(
                                "build/reports/coverage/androidTest/debug/connected/com.example.helloworld/HelloWorld.html"))
                .exists();
    }

    @Test
    public void connectedCheckWithOrchestrator() throws Exception {
        runConnectedCheckAndAssertCoverageReportExists(/*enableClearPackageDataOption=*/ false);
    }

    /** Regression test for http://b/123987001. */
    @Test
    public void connectedCheckWithOrchestratorAndClearPackageDataEnabled() throws Exception {
        runConnectedCheckAndAssertCoverageReportExists(/*enableClearPackageDataOption=*/ true);
    }

    private void runConnectedCheckAndAssertCoverageReportExists(
            boolean enableClearPackageDataOption) throws Exception {
        TestFileUtils.appendToFile(
                project.getBuildFile(),
                "\n"
                        + "android.defaultConfig.minSdkVersion 16\n"
                        + "android.defaultConfig.testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'\n"
                        + "android.defaultConfig.testInstrumentationRunnerArguments package: 'com.example.helloworld'\n"
                        + (enableClearPackageDataOption
                                ? "android.defaultConfig.testInstrumentationRunnerArguments clearPackageData: 'true'\n"
                                        + "android.defaultConfig.testInstrumentationRunnerArguments useTestStorageService: 'true'\n"
                                : "")
                        + "android.testOptions.execution 'ANDROIDX_TEST_ORCHESTRATOR'\n"
                        // Orchestrator requires some setup time and it usually takes
                        // about an minute. Increase the timeout for running "am instrument" command
                        // to 3 minutes.
                        + "android.adbOptions.timeOutInMs=180000\n"
                        + "dependencies {\n"
                        + "  androidTestImplementation 'androidx.test:core:1.4.0-alpha06'\n"
                        + "  androidTestImplementation 'androidx.test.ext:junit:1.1.3-alpha02'\n"
                        + "  androidTestImplementation 'androidx.test:monitor:1.4.0-alpha06'\n"
                        + "  androidTestImplementation 'androidx.test:rules:1.4.0-alpha06'\n"
                        + "  androidTestImplementation 'androidx.test:runner:1.4.0-alpha06'\n"
                        + "  androidTestUtil 'androidx.test.services:test-services:1.4.0-alpha06'\n"
                        + "  androidTestUtil 'androidx.test:orchestrator:1.4.0-alpha06'\n"
                        + "}");
        TestFileUtils.appendToFile(
                project.getGradlePropertiesFile(),
                "android.useAndroidX=true");

        String testSrc =
                "package com.example.helloworld;\n"
                        + "\n"
                        + "import androidx.test.ext.junit.runners.AndroidJUnit4;\n"
                        + "import org.junit.Rule;\n"
                        + "import org.junit.Test;\n"
                        + "import org.junit.runner.RunWith;\n"
                        + "\n"
                        + "@RunWith(AndroidJUnit4.class)\n"
                        + "public class ExampleTest {\n"
                        + "    @Test\n"
                        + "    public void test1() { }\n"
                        + "\n"
                        + "    @Test\n"
                        + "    public void test2() { }\n"
                        + "}\n";
        Path exampleTest =
                project.getProjectDir()
                        .toPath()
                        .resolve("src/androidTest/java/com/example/helloworld/ExampleTest.java");
        Files.createDirectories(exampleTest.getParent());
        Files.write(exampleTest, testSrc.getBytes());

        // This example project uses deprecated "android.support.test.runner.AndroidJUnit4" runner
        // which cannot be used with androidx.test.ext.junit.runners.AndroidJUnit4 together. So,
        // deleting it here.
        Path deprecatedTest =
                project.getProjectDir()
                        .toPath()
                        .resolve("src/androidTest/java/com/example/helloworld/HelloWorldTest.java");
        Files.deleteIfExists(deprecatedTest);

        project.executor().run("connectedCheck");
        List<File> files =
                FileUtils.find(
                        project.file("build/outputs/code_coverage"), Pattern.compile(".*\\.ec"));

        // ExampleTest has 2 methods, and there should be at least 2 .ec files
        Truth.assertThat(files.size()).isAtLeast(2);
        assertThat(
                        project.file(
                                "build/reports/coverage/androidTest/debug/connected/com.example.helloworld/index.html"))
                .exists();
    }

    /** Regression test for http://b/152872138. */
    @Test
    public void testDisablingBuildFeatures() throws IOException, InterruptedException {
        TestFileUtils.appendToFile(
                project.getBuildFile(),
                "\n\n"
                        + "android {\n"
                        + "  buildFeatures {\n"
                        + "    aidl = false\n"
                        + "    renderScript = false\n"
                        + "  }\n"
                        + "}\n");
        project.executor().run("connectedCheck");
        assertThat(
                        project.file(
                                "build/reports/coverage/androidTest/debug/connected/com.example.helloworld/HelloWorld.html"))
                .exists();
        String expectedReportXml =
                "<package name=\"com/example/helloworld\">"
                        + "<class name=\"com/example/helloworld/HelloWorld\" sourcefilename=\"HelloWorld.kt\">"
                        + "<method name=\"&lt;init&gt;\" desc=\"()V\" line=\"6\">"
                        + "<counter type=\"INSTRUCTION\" missed=\"0\" covered=\"3\"/>"
                        + "<counter type=\"LINE\" missed=\"0\" covered=\"1\"/>"
                        + "<counter type=\"COMPLEXITY\" missed=\"0\" covered=\"1\"/>"
                        + "<counter type=\"METHOD\" missed=\"0\" covered=\"1\"/>"
                        + "</method><method name=\"onCreate\" desc=\"(Landroid/os/Bundle;)V\" line=\"9\">"
                        + "<counter type=\"INSTRUCTION\" missed=\"0\" covered=\"7\"/>"
                        + "<counter type=\"LINE\" missed=\"0\" covered=\"3\"/>"
                        + "<counter type=\"COMPLEXITY\" missed=\"0\" covered=\"1\"/>"
                        + "<counter type=\"METHOD\" missed=\"0\" covered=\"1\"/>"
                        + "</method><counter type=\"INSTRUCTION\" missed=\"0\" covered=\"10\"/>"
                        + "<counter type=\"LINE\" missed=\"0\" covered=\"4\"/>"
                        + "<counter type=\"COMPLEXITY\" missed=\"0\" covered=\"2\"/>"
                        + "<counter type=\"METHOD\" missed=\"0\" covered=\"2\"/>"
                        + "<counter type=\"CLASS\" missed=\"0\" covered=\"1\"/>"
                        + "</class>"
                        + "<sourcefile name=\"HelloWorld.kt\">"
                        + "<line nr=\"6\" mi=\"0\" ci=\"3\" mb=\"0\" cb=\"0\"/>"
                        + "<line nr=\"9\" mi=\"0\" ci=\"3\" mb=\"0\" cb=\"0\"/>"
                        + "<line nr=\"10\" mi=\"0\" ci=\"3\" mb=\"0\" cb=\"0\"/>"
                        + "<line nr=\"12\" mi=\"0\" ci=\"1\" mb=\"0\" cb=\"0\"/>"
                        + "<counter type=\"INSTRUCTION\" missed=\"0\" covered=\"10\"/>"
                        + "<counter type=\"LINE\" missed=\"0\" covered=\"4\"/>"
                        + "<counter type=\"COMPLEXITY\" missed=\"0\" covered=\"2\"/>"
                        + "<counter type=\"METHOD\" missed=\"0\" covered=\"2\"/>"
                        + "<counter type=\"CLASS\" missed=\"0\" covered=\"1\"/>"
                        + "</sourcefile><counter type=\"INSTRUCTION\" missed=\"0\" covered=\"10\"/>"
                        + "<counter type=\"LINE\" missed=\"0\" covered=\"4\"/>"
                        + "<counter type=\"COMPLEXITY\" missed=\"0\" covered=\"2\"/>"
                        + "<counter type=\"METHOD\" missed=\"0\" covered=\"2\"/>"
                        + "<counter type=\"CLASS\" missed=\"0\" covered=\"1\"/>"
                        + "</package>"
                        + "<counter type=\"INSTRUCTION\" missed=\"0\" covered=\"10\"/>"
                        + "<counter type=\"LINE\" missed=\"0\" covered=\"4\"/>"
                        + "<counter type=\"COMPLEXITY\" missed=\"0\" covered=\"2\"/>"
                        + "<counter type=\"METHOD\" missed=\"0\" covered=\"2\"/>"
                        + "<counter type=\"CLASS\" missed=\"0\" covered=\"1\"/>"
                        + "</report>";
        assertThat(project.file("build/reports/coverage/androidTest/debug/connected/report.xml"))
                .contains(expectedReportXml);
    }
}