-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from dkostyrev/test-plugin-support
Introduce com.android.test plugin support
- Loading branch information
Showing
17 changed files
with
321 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
allprojects { | ||
repositories { | ||
mavenLocal() | ||
google() | ||
jcenter() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../gradle |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../gradle.properties |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../gradlew |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../gradlew.bat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
and-test/test/src/main/java/com/trevjonez/andapp/AppTesting.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
plugin/src/main/kotlin/com/trevjonez/composer/internal/ComposerTestPlugin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.