Skip to content

Commit

Permalink
Merge pull request #71 from dkostyrev/test-plugin-support
Browse files Browse the repository at this point in the history
Introduce com.android.test plugin support
  • Loading branch information
trevjonez authored Apr 13, 2020
2 parents df8d80e + e11f678 commit b29d47f
Show file tree
Hide file tree
Showing 17 changed files with 321 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Items listed here may not be exhaustive, if you are seeing issues, check the git
- Update gradle and AGP to latest stable. `6.2.1` & `3.6.1` respectively.
- Process: Github actions for verification. #36 again, thank you [@plastiv](https://github.com/plastiv).
- Feature: Dynamic-Feature module support
- Feature: Test module support "com.android.test". #71 thanks [@dkostyrev](https://github.com/dkostyrev).

## 1.0.0-rc05
- Catch a specific subset of errors allowing clean disposal of underlying processes. (logcat and instrumentation)
Expand Down
31 changes: 31 additions & 0 deletions and-test/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2020 Trevor Jones
*
* 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.
*/

plugins {
id "com.android.application"
}

android {
compileSdkVersion Integer.parseInt(COMPILE_SDK)
defaultConfig {
applicationId "com.trevjonez.testapp"
minSdkVersion Integer.parseInt(MIN_SDK)
targetSdkVersion Integer.parseInt(COMPILE_SDK)
versionCode 1
versionName "1.0"
resValue "string", "app_name", "TestApp"
}
}
21 changes: 21 additions & 0 deletions and-test/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2020 Trevor Jones
~
~ 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.
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trevjonez.testapp">
<application android:label="@string/app_name" />
</manifest>
7 changes: 7 additions & 0 deletions and-test/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
allprojects {
repositories {
mavenLocal()
google()
jcenter()
}
}
1 change: 1 addition & 0 deletions and-test/gradle
1 change: 1 addition & 0 deletions and-test/gradle.properties
1 change: 1 addition & 0 deletions and-test/gradlew
1 change: 1 addition & 0 deletions and-test/gradlew.bat
35 changes: 35 additions & 0 deletions and-test/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2020 Trevor Jones
*
* 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.
*/


pluginManagement {
repositories {
mavenLocal()
google()
jcenter()
}
resolutionStrategy {
eachPlugin {
if (requested.id.id.startsWith("com.android"))
useModule("com.android.tools.build:gradle:$AGP_VERSION")
}
}
}

rootProject.name = "and-test"

include(":app")
include(":test")
49 changes: 49 additions & 0 deletions and-test/test/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2020 Trevor Jones
*
* 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.
*/

plugins {
id "com.android.test"
id "com.trevjonez.composer"
}

android {
compileSdkVersion Integer.parseInt(COMPILE_SDK)
defaultConfig {
minSdkVersion Integer.parseInt(MIN_SDK)
targetSdkVersion Integer.parseInt(COMPILE_SDK)
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

targetProjectPath ":app"
}

dependencies {
implementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.test:runner:1.2.0'
implementation 'androidx.test.ext:junit:1.1.1'
}

composer {
verboseOutput true
instrumentationArgument('screenshotsDisabled', 'false')
apkInstallTimeout 10
configs {
debug {
instrumentationArgument('screenshotsEngine', 'uiAutomator')
keepOutput true
}
}
}
26 changes: 26 additions & 0 deletions and-test/test/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright 2020 Trevor Jones
~
~ 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.
-->

<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.trevjonez.testapp.test">

<instrumentation
android:name="androidx.test.runner.AndroidJUnitRunner"
android:targetPackage="com.trevjonez.testapp" />

</manifest>
34 changes: 34 additions & 0 deletions and-test/test/src/main/java/com/trevjonez/andapp/AppTesting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.trevjonez.andapp;

import android.util.Log;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue;

@RunWith(AndroidJUnit4.class)
public class AppTesting {

@Test
public void basicMaths() {
Log.w("AppTesting", "Can do maths and log?");
assertEquals(4, 2 + 2);
}

@Test
public void assumeFailed() {
//noinspection ConstantConditions
assumeTrue("This assumption is clearly bad",false);
}

@Test
public void assertFailed() {
//noinspection ConstantConditions,SimplifiableJUnitAssertion
assertTrue("This assert is clearly bad",false);
}
}
10 changes: 10 additions & 0 deletions plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ tasks.named("test").configure {
systemProperty("andApp", new File(rootProject.projectDir, "and-app").absolutePath)
systemProperty("andLib", new File(rootProject.projectDir, "and-lib").absolutePath)
systemProperty("andDyn", new File(rootProject.projectDir, "and-dyn").absolutePath)
systemProperty("andTest", new File(rootProject.projectDir, "and-test").absolutePath)
systemProperty("org.gradle.testkit.debug", false)

outputs.dir("$buildDir/tests")
Expand Down Expand Up @@ -68,6 +69,15 @@ tasks.named("test").configure {
inputs.dir(new File(rootProject.projectDir, "and-dyn/atInstall/src"))
inputs.files(new File(rootProject.projectDir, "and-dyn/atInstall/build.gradle"))

inputs.dir(new File(rootProject.projectDir, "and-test/app/src"))
inputs.files(new File(rootProject.projectDir, "and-test/app/build.gradle"))

inputs.dir(new File(rootProject.projectDir, "and-test/test/src"))
inputs.files(new File(rootProject.projectDir, "and-test/test/build.gradle"))

inputs.files(new File(rootProject.projectDir, "and-test/build.gradle"))
inputs.files(new File(rootProject.projectDir, "and-test/settings.gradle"))

inputs.dir(new File(rootProject.projectDir, "commander/os/src/main"))
inputs.files(new File(rootProject.projectDir, "commander/os/build.gradle"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.android.build.gradle.api.AndroidBasePlugin
import com.trevjonez.composer.internal.ComposerApplicationPlugin
import com.trevjonez.composer.internal.ComposerDynamicFeaturePlugin
import com.trevjonez.composer.internal.ComposerLibraryPlugin
import com.trevjonez.composer.internal.ComposerTestPlugin
import org.gradle.api.GradleException
import org.gradle.api.Plugin
import org.gradle.api.Project
Expand All @@ -43,6 +44,10 @@ class ComposerPlugin : Plugin<Project> {
project.pluginManager.apply(ComposerDynamicFeaturePlugin::class.java)
}

project.pluginManager.withPlugin("com.android.test") {
project.pluginManager.apply(ComposerTestPlugin::class.java)
}

project.afterEvaluate {
project.extensions.findByType(ConfigExtension::class.java)
?: project.tasks.find { it is ComposerTask } //check if manually created tasks exist before throwing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ abstract class ComposerBasePlugin<T> : Plugin<Project>
return project.layout.buildDirectory.dir("reports/composer/$dirName")
}

open fun T.isTestable(): Boolean {
return testVariant != null
}

lateinit var project: Project
lateinit var globalConfig: ConfigExtension

Expand All @@ -62,8 +66,8 @@ abstract class ComposerBasePlugin<T> : Plugin<Project>
private fun observeVariants() {
testableVariants.all { testableVariant ->
if (globalConfig.variants.isEmpty() || globalConfig.variants.contains(testableVariant.name)) {
if (testableVariant.testVariant == null) {
project.logger.info("variant: ${testableVariant.name}. has no test variant. skipping composer task registration.")
if (!testableVariant.isTestable()) {
project.logger.info("variant: ${testableVariant.name}. is not testable. skipping composer task registration.")
return@all
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.trevjonez.composer.internal

import com.android.build.gradle.AppExtension
import com.android.build.gradle.TestExtension
import com.android.build.gradle.api.ApplicationVariant
import com.trevjonez.composer.ComposerTask
import org.gradle.api.DomainObjectCollection
import org.gradle.api.file.ConfigurableFileCollection
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Provider
import java.io.File

class ComposerTestPlugin : ComposerBasePlugin<ApplicationVariant>() {
private val testExtension by lazy(LazyThreadSafetyMode.NONE) {
requireNotNull(project.findExtension<TestExtension>("android")) {
"Failed to find android test extension"
}
}

private val appExtension by lazy(LazyThreadSafetyMode.NONE) {
val project = requireNotNull(project.findProject(testExtension.targetProjectPath)) {
"Failed to find target project ${testExtension.targetProjectPath}"
}
requireNotNull(project.findExtension<AppExtension>("android")) {
"Failed to find android app extension"
}
}

private val androidTestUtil by lazy(LazyThreadSafetyMode.NONE) {
project.configurations.findByName("androidTestUtil")
}

override val sdkDir: File
get() = testExtension.sdkDirectory

override val testableVariants: DomainObjectCollection<ApplicationVariant>
get() = testExtension.applicationVariants

override fun ApplicationVariant.getApk(task: ComposerTask): Provider<RegularFile> {
val variant = appExtension.applicationVariants.find { it.name == name }
?: error("Failed to find application variant")

task.dependsOn(variant.assembleProvider)
return project.layout.file(project.provider {
variant.outputs.single().outputFile
})
}

override fun ApplicationVariant.getTestApk(task: ComposerTask): Provider<RegularFile> {
task.dependsOn(assembleProvider)
return project.layout.file(project.provider {
outputs.single().outputFile
})
}

override fun ApplicationVariant.getExtraApks(task: ComposerTask): ConfigurableFileCollection {
return project.objects.fileCollection().also {
it.from(project.provider {
androidTestUtil?.resolvedConfiguration?.files?.toList().orEmpty()
})
}
}

override fun ApplicationVariant.getMultiApks(task: ComposerTask): ConfigurableFileCollection {
return project.objects.fileCollection()
}

override fun ApplicationVariant.isTestable(): Boolean {
return true
}
}
Loading

0 comments on commit b29d47f

Please sign in to comment.