initialize ALL scope for HostTestTaskManager.

since there is not a full fledge compilation pipeline for HostTest,
just jvm basically, the ALL scope was not initialized. Refactored
the scope initialization in a method and added a test.

Test: Unit Test
Bug: N/A
Change-Id: I3b325a4b3c45434312ac88111fa739f6bed62402
diff --git a/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/HostTestTaskManager.kt b/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/HostTestTaskManager.kt
index c3ecb98..69fb7fd 100644
--- a/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/HostTestTaskManager.kt
+++ b/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/HostTestTaskManager.kt
@@ -257,6 +257,7 @@
 
             val javacTask = createJavacTask(hostTestCreationConfig)
             setJavaCompilerTask(javacTask, hostTestCreationConfig)
+            initializeAllScope(hostTestCreationConfig.artifacts)
         }
         maybeCreateTransformClassesWithAsmTask(hostTestCreationConfig)
 
diff --git a/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/TaskManager.kt b/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/TaskManager.kt
index 27cf91b..5b838a5 100644
--- a/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/TaskManager.kt
+++ b/build-system/gradle-core/src/main/java/com/android/build/gradle/internal/TaskManager.kt
@@ -20,6 +20,7 @@
 import com.android.build.api.artifact.Artifact.Single
 import com.android.build.api.artifact.ScopedArtifact
 import com.android.build.api.artifact.SingleArtifact
+import com.android.build.api.artifact.impl.ArtifactsImpl
 import com.android.build.api.artifact.impl.InternalScopedArtifact
 import com.android.build.api.artifact.impl.InternalScopedArtifacts
 import com.android.build.api.dsl.Device
@@ -1136,42 +1137,7 @@
 
         maybeCreateTransformClassesWithAsmTask(creationConfig)
 
-        // initialize the all classes scope
-        creationConfig.artifacts.forScope(ScopedArtifacts.Scope.ALL)
-            .getScopedArtifactsContainer(ScopedArtifact.CLASSES)
-            .initialScopedContent
-            .run {
-                from(
-                    creationConfig.artifacts.forScope(ScopedArtifacts.Scope.PROJECT)
-                        .getFinalArtifacts(ScopedArtifact.CLASSES)
-                )
-                from(
-                    creationConfig.artifacts.forScope(InternalScopedArtifacts.InternalScope.SUB_PROJECTS)
-                        .getFinalArtifacts(ScopedArtifact.CLASSES)
-                )
-                from(
-                    creationConfig.artifacts.forScope(InternalScopedArtifacts.InternalScope.EXTERNAL_LIBS)
-                        .getFinalArtifacts(ScopedArtifact.CLASSES)
-                )
-            }
-
-        creationConfig.artifacts.forScope(ScopedArtifacts.Scope.ALL)
-            .getScopedArtifactsContainer(ScopedArtifact.JAVA_RES)
-            .initialScopedContent
-            .run {
-                from(
-                    creationConfig.artifacts.forScope(InternalScopedArtifacts.InternalScope.SUB_PROJECTS)
-                        .getFinalArtifacts(ScopedArtifact.JAVA_RES)
-                )
-                from(
-                    creationConfig.artifacts.forScope(ScopedArtifacts.Scope.PROJECT)
-                        .getFinalArtifacts(ScopedArtifact.JAVA_RES)
-                )
-                from(
-                    creationConfig.artifacts.forScope(InternalScopedArtifacts.InternalScope.EXTERNAL_LIBS)
-                        .getFinalArtifacts(ScopedArtifact.JAVA_RES)
-                )
-            }
+        initializeAllScope(creationConfig.artifacts)
 
         // New gradle-transform jacoco instrumentation support.
         if (creationConfig.codeCoverageEnabled &&
@@ -1228,6 +1194,49 @@
     }
 
     /**
+     * initialize the [ScopedArtifacts.Scope.ALL] scope from the project, sub-projects and external
+     * libraries.
+     */
+    internal fun initializeAllScope(artifacts: ArtifactsImpl) {
+        // initialize the all classes scope
+        artifacts.forScope(ScopedArtifacts.Scope.ALL)
+            .getScopedArtifactsContainer(ScopedArtifact.CLASSES)
+            .initialScopedContent
+            .run {
+                from(
+                    artifacts.forScope(ScopedArtifacts.Scope.PROJECT)
+                        .getFinalArtifacts(ScopedArtifact.CLASSES)
+                )
+                from(
+                    artifacts.forScope(InternalScopedArtifacts.InternalScope.SUB_PROJECTS)
+                        .getFinalArtifacts(ScopedArtifact.CLASSES)
+                )
+                from(
+                    artifacts.forScope(InternalScopedArtifacts.InternalScope.EXTERNAL_LIBS)
+                        .getFinalArtifacts(ScopedArtifact.CLASSES)
+                )
+            }
+
+        artifacts.forScope(ScopedArtifacts.Scope.ALL)
+            .getScopedArtifactsContainer(ScopedArtifact.JAVA_RES)
+            .initialScopedContent
+            .run {
+                from(
+                    artifacts.forScope(InternalScopedArtifacts.InternalScope.SUB_PROJECTS)
+                        .getFinalArtifacts(ScopedArtifact.JAVA_RES)
+                )
+                from(
+                    artifacts.forScope(ScopedArtifacts.Scope.PROJECT)
+                        .getFinalArtifacts(ScopedArtifact.JAVA_RES)
+                )
+                from(
+                    artifacts.forScope(InternalScopedArtifacts.InternalScope.EXTERNAL_LIBS)
+                        .getFinalArtifacts(ScopedArtifact.JAVA_RES)
+                )
+            }
+    }
+
+    /**
      * Creates tasks used for DEX generation. This will use an incremental pipeline that uses dex
      * archives in order to enable incremental dexing support.
      */
diff --git a/build-system/gradle-core/src/test/java/com/android/build/gradle/internal/TaskManagerTest.kt b/build-system/gradle-core/src/test/java/com/android/build/gradle/internal/TaskManagerTest.kt
new file mode 100644
index 0000000..4d4c52a
--- /dev/null
+++ b/build-system/gradle-core/src/test/java/com/android/build/gradle/internal/TaskManagerTest.kt
@@ -0,0 +1,118 @@
+package com.android.build.gradle.internal
+
+import com.android.build.api.artifact.ScopedArtifact
+import com.android.build.api.artifact.impl.ArtifactsImpl
+import com.android.build.api.artifact.impl.InternalScopedArtifacts
+import com.android.build.api.variant.ScopedArtifacts
+import com.android.build.gradle.internal.scope.InternalArtifactType
+import com.android.build.gradle.internal.tasks.factory.GlobalTaskCreationConfig
+import com.google.common.truth.Truth
+import io.grpc.Internal
+import org.gradle.api.Project
+import org.gradle.api.Task
+import org.gradle.api.tasks.TaskProvider
+import org.gradle.testfixtures.ProjectBuilder
+import org.junit.Rule
+import org.junit.Test
+import org.mockito.Mock
+import org.mockito.Mockito
+import org.mockito.junit.MockitoJUnit
+import org.mockito.junit.MockitoRule
+import org.mockito.quality.Strictness
+import java.io.File
+
+class TaskManagerTest {
+
+    @get:Rule
+    var mockitoJUnitRule: MockitoRule =
+        MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS)
+
+    private val project = ProjectBuilder.builder().build()
+
+    @Mock
+    lateinit var globalTaskCreationConfig: GlobalTaskCreationConfig
+
+    class TestTaskManager(project: Project,
+        globalConfig: GlobalTaskCreationConfig
+    ) : TaskManager(project, globalConfig) {
+
+        override val javaResMergingScopes: Set<InternalScopedArtifacts.InternalScope>
+            get() = setOf()
+    }
+
+    @Test
+    fun testEmptyAllScope() {
+        val taskManager = TestTaskManager(project, globalTaskCreationConfig)
+        val artifacts = ArtifactsImpl(project, "test")
+        taskManager.initializeAllScope(artifacts)
+        Truth.assertThat(
+            artifacts.forScope(ScopedArtifacts.Scope.ALL)
+                .getFinalArtifacts(ScopedArtifact.CLASSES)
+                .files
+        ).isEmpty()
+    }
+
+    @Test
+    fun testAllScopeFromProject() {
+        val taskManager = TestTaskManager(project, globalTaskCreationConfig)
+        val artifacts = ArtifactsImpl(project, "test")
+        artifacts.forScope(ScopedArtifacts.Scope.PROJECT).
+            setInitialContent(
+                ScopedArtifact.CLASSES,
+                project.files("/project/classes")
+            )
+        artifacts.forScope(InternalScopedArtifacts.InternalScope.SUB_PROJECTS).
+        setInitialContent(
+            ScopedArtifact.CLASSES,
+            project.files("/sub-project/classes")
+        )
+        artifacts.forScope(InternalScopedArtifacts.InternalScope.EXTERNAL_LIBS).
+        setInitialContent(
+            ScopedArtifact.CLASSES,
+            project.files("/external-libs/classes")
+        )
+        artifacts.forScope(ScopedArtifacts.Scope.PROJECT)
+            .setInitialContent(
+                ScopedArtifact.JAVA_RES,
+                project.files("/project/res")
+            )
+
+        artifacts.forScope(InternalScopedArtifacts.InternalScope.SUB_PROJECTS)
+            .setInitialContent(
+                ScopedArtifact.JAVA_RES,
+                project.files("/sub-project/res")
+            )
+        artifacts.forScope(InternalScopedArtifacts.InternalScope.EXTERNAL_LIBS)
+            .setInitialContent(
+                ScopedArtifact.JAVA_RES,
+                project.files("/external-libs/res")
+            )
+
+        taskManager.initializeAllScope(artifacts)
+
+        Truth.assertThat(
+            artifacts.forScope(ScopedArtifacts.Scope.ALL)
+                .getFinalArtifacts(ScopedArtifact.CLASSES)
+                .files
+                .map(File::getAbsolutePath)
+        ).containsExactly(
+            toProjectFile("/project/classes"),
+            toProjectFile("/sub-project/classes"),
+            toProjectFile("/external-libs/classes"),
+        )
+
+        Truth.assertThat(
+            artifacts.forScope(ScopedArtifacts.Scope.ALL)
+                .getFinalArtifacts(ScopedArtifact.JAVA_RES)
+                .files
+                .map(File::getAbsolutePath)
+        ).containsExactly(
+            toProjectFile("/project/res"),
+            toProjectFile("/sub-project/res"),
+            toProjectFile("/external-libs/res"),
+        )
+    }
+
+    private fun toProjectFile(path: String) =
+        project.layout.projectDirectory.file(path).asFile.absolutePath
+}