summaryrefslogtreecommitdiff
path: root/build-system/integration-test/application/src/test/java/com/android/build/gradle/integration/application/GenFolderApi2Test.java
blob: e3dd17d753a5d46280f278275e549a34c074375a (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
/*
 * Copyright (C) 2017 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.application;

import static com.android.build.gradle.integration.common.truth.TruthHelper.assertThat;
import static com.android.testutils.truth.PathSubject.assertThat;
import static org.junit.Assert.assertNotNull;

import com.android.build.gradle.integration.common.fixture.GradleTestProject;
import com.android.build.gradle.integration.common.utils.TestFileUtils;
import com.android.builder.model.AndroidArtifact;
import com.android.builder.model.AndroidProject;
import com.android.builder.model.JavaArtifact;
import com.android.builder.model.Variant;
import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.Rule;
import org.junit.Test;

/** Tests for addJavaSourceFoldersToModel Variant API. */
public class GenFolderApi2Test {
    @Rule
    public GradleTestProject project =
            GradleTestProject.builder().fromTestProject("genFolderApi2").create();

    @Test
    public void checkJavaFolderInModel() throws Exception {
        AndroidProject model = project.model().fetchAndroidProjects().getOnlyModel();
        File projectDir = project.getProjectDir();

        File buildDir = new File(projectDir, "build");

        for (Variant variant : model.getVariants()) {

            AndroidArtifact mainInfo = variant.getMainArtifact();
            assertNotNull(
                    "Null-check on mainArtifactInfo for " + variant.getDisplayName(), mainInfo);

            // Get the generated source folders.
            Collection<File> genSourceFolder = mainInfo.getGeneratedSourceFolders();

            // We're looking for a custom folder.
            String sourceFolderStart =
                    new File(buildDir, "customCode").getAbsolutePath() + File.separatorChar;

            assertThat(
                            genSourceFolder
                                    .stream()
                                    .anyMatch(
                                            it ->
                                                    it.getAbsolutePath()
                                                            .startsWith(sourceFolderStart)))
                    .isTrue();

            // Unit testing artifact:
            assertThat(variant.getExtraJavaArtifacts()).hasSize(1);
            JavaArtifact unitTestArtifact = variant.getExtraJavaArtifacts().iterator().next();
            List<File> sortedFolders =
                    unitTestArtifact
                            .getGeneratedSourceFolders()
                            .stream()
                            .sorted()
                            .collect(Collectors.toList());
            assertThat(sortedFolders).hasSize(3);
            assertThat(sortedFolders.get(0).getAbsolutePath()).startsWith(sourceFolderStart);
            assertThat(sortedFolders.get(0).getAbsolutePath()).endsWith("-1");
            assertThat(sortedFolders.get(1).getAbsolutePath()).startsWith(sourceFolderStart);
            assertThat(sortedFolders.get(1).getAbsolutePath()).endsWith("-2");
        }
    }

    @Test
    public void backwardsCompatible() throws Exception {
        // ATTENTION Author and Reviewers - please make sure required changes to the build file
        // are backwards compatible before updating this test.
        assertThat(TestFileUtils.sha1NormalizedLineEndings(project.file("build.gradle")))
                .isEqualTo("48b1df5b5a40abb122b2fb27109c7dd5cc85b2a0");
    }
}