Unverified Commit ef49f566 authored by stuartmorgan's avatar stuartmorgan Committed by GitHub

Add Android unit tests to plugin template (#120720)

* Add Java tests

* Add Kotlin

* Add integration testing

* Add cerate tests
parent a664f08a
......@@ -252,36 +252,50 @@ public class $pluginClass: NSObject, FlutterPlugin {
// build files.
await build(buildTarget, validateNativeBuildProject: false);
if (buildTarget == 'ios') {
await testWithNewIOSSimulator('TestNativeUnitTests', (String deviceId) async {
switch(buildTarget) {
case 'apk':
if (await exec(
path.join('.', 'gradlew'),
<String>['testDebugUnitTest'],
workingDirectory: path.join(rootPath, 'android'),
canFail: true,
) != 0) {
throw TaskResult.failure('Platform unit tests failed');
}
break;
case 'ios':
await testWithNewIOSSimulator('TestNativeUnitTests', (String deviceId) async {
if (!await runXcodeTests(
platformDirectory: path.join(rootPath, 'ios'),
destination: 'id=$deviceId',
configuration: 'Debug',
testName: 'native_plugin_unit_tests_ios',
skipCodesign: true,
)) {
throw TaskResult.failure('Platform unit tests failed');
}
});
break;
case 'macos':
if (!await runXcodeTests(
platformDirectory: path.join(rootPath, 'ios'),
destination: 'id=$deviceId',
platformDirectory: path.join(rootPath, 'macos'),
destination: 'platform=macOS',
configuration: 'Debug',
testName: 'native_plugin_unit_tests_ios',
testName: 'native_plugin_unit_tests_macos',
skipCodesign: true,
)) {
throw TaskResult.failure('Platform unit tests failed');
}
});
} else if (buildTarget == 'macos') {
if (!await runXcodeTests(
platformDirectory: path.join(rootPath, 'macos'),
destination: 'platform=macOS',
configuration: 'Debug',
testName: 'native_plugin_unit_tests_macos',
skipCodesign: true,
)) {
throw TaskResult.failure('Platform unit tests failed');
}
} else if (buildTarget == 'windows') {
if (await exec(
path.join(rootPath, 'build', 'windows', 'plugins', 'plugintest', 'Release', 'plugintest_plugin_test'),
<String>[],
canFail: true,
) != 0) {
throw TaskResult.failure('Platform unit tests failed');
}
break;
case 'windows':
if (await exec(
path.join(rootPath, 'build', 'windows', 'plugins', 'plugintest', 'Release', 'plugintest_plugin_test'),
<String>[],
canFail: true,
) != 0) {
throw TaskResult.failure('Platform unit tests failed');
}
break;
}
}
......
......@@ -32,4 +32,19 @@ android {
defaultConfig {
minSdkVersion {{minSdkVersion}}
}
dependencies {
testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:5.0.0'
}
testOptions {
unitTests.all {
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
outputs.upToDateWhen {false}
showStandardStreams = true
}
}
}
}
package {{androidIdentifier}};
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import org.junit.Test;
/**
* This demonstrates a simple unit test of the Java portion of this plugin's implementation.
*
* Once you have built the plugin's example app, you can run these tests from the command
* line by running `./gradlew testDebugUnitTest` in the `example/android/` directory, or
* you can run them directly from IDEs that support JUnit such as Android Studio.
*/
public class {{pluginClass}}Test {
@Test
public void onMethodCall_getPlatformVersion_returnsExpectedValue() {
{{pluginClass}} plugin = new {{pluginClass}}();
final MethodCall call = new MethodCall("getPlatformVersion", null);
MethodChannel.Result mockResult = mock(MethodChannel.Result.class);
plugin.onMethodCall(call, mockResult);
verify(mockResult).success("Android " + android.os.Build.VERSION.RELEASE);
}
}
......@@ -38,9 +38,27 @@ android {
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
}
defaultConfig {
minSdkVersion {{minSdkVersion}}
}
dependencies {
testImplementation 'org.jetbrains.kotlin:kotlin-test'
testImplementation 'org.mockito:mockito-core:5.0.0'
}
testOptions {
unitTests.all {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
outputs.upToDateWhen {false}
showStandardStreams = true
}
}
}
}
package {{androidIdentifier}}
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import kotlin.test.Test
import org.mockito.Mockito
/*
* This demonstrates a simple unit test of the Kotlin portion of this plugin's implementation.
*
* Once you have built the plugin's example app, you can run these tests from the command
* line by running `./gradlew testDebugUnitTest` in the `example/android/` directory, or
* you can run them directly from IDEs that support JUnit such as Android Studio.
*/
internal class {{pluginClass}}Test {
@Test
fun onMethodCall_getPlatformVersion_returnsExpectedValue() {
val plugin = {{pluginClass}}()
val call = MethodCall("getPlatformVersion", null)
val mockResult: MethodChannel.Result = Mockito.mock(MethodChannel.Result::class.java)
plugin.onMethodCall(call, mockResult)
Mockito.verify(mockResult).success("Android " + android.os.Build.VERSION.RELEASE)
}
}
......@@ -251,9 +251,11 @@
"templates/plugin/android-java.tmpl/build.gradle.tmpl",
"templates/plugin/android-java.tmpl/projectName_android.iml.tmpl",
"templates/plugin/android-java.tmpl/src/main/java/androidIdentifier/pluginClass.java.tmpl",
"templates/plugin/android-java.tmpl/src/test/java/androidIdentifier/pluginClassTest.java.tmpl",
"templates/plugin/android-kotlin.tmpl/build.gradle.tmpl",
"templates/plugin/android-kotlin.tmpl/projectName_android.iml.tmpl",
"templates/plugin/android-kotlin.tmpl/src/main/kotlin/androidIdentifier/pluginClass.kt.tmpl",
"templates/plugin/android-kotlin.tmpl/src/test/kotlin/androidIdentifier/pluginClassTest.kt.tmpl",
"templates/plugin/android.tmpl/.gitignore",
"templates/plugin/android.tmpl/gradle/wrapper/gradle-wrapper.properties",
"templates/plugin/android.tmpl/gradle.properties.tmpl",
......
......@@ -2452,6 +2452,63 @@ void main() {
Logger: () => logger,
});
testUsingContext('plugin includes native Kotlin unit tests', () async {
Cache.flutterRoot = '../..';
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>[
'create',
'--no-pub',
'--template=plugin',
'--org=com.example',
'--platforms=android',
projectDir.path]);
expect(projectDir
.childDirectory('android')
.childDirectory('src')
.childDirectory('test')
.childDirectory('kotlin')
.childDirectory('com')
.childDirectory('example')
.childDirectory('flutter_project')
.childFile('FlutterProjectPluginTest.kt'), exists);
}, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(),
Logger: () => logger,
});
testUsingContext('plugin includes native Java unit tests', () async {
Cache.flutterRoot = '../..';
final CreateCommand command = CreateCommand();
final CommandRunner<void> runner = createTestCommandRunner(command);
await runner.run(<String>[
'create',
'--no-pub',
'--template=plugin',
'--org=com.example',
'--platforms=android',
'-a', 'java',
projectDir.path]);
expect(projectDir
.childDirectory('android')
.childDirectory('src')
.childDirectory('test')
.childDirectory('java')
.childDirectory('com')
.childDirectory('example')
.childDirectory('flutter_project')
.childFile('FlutterProjectPluginTest.java'), exists);
}, overrides: <Type, Generator>{
FeatureFlags: () => TestFeatureFlags(),
Logger: () => logger,
});
testUsingContext('plugin includes native Objective-C unit tests', () async {
Cache.flutterRoot = '../..';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment