Skip to content

Commit f1d157b

Browse files
Add an integration test to plugin template example (#117062)
* Add an integration test to plugin template example Dart unit tests don't exercise host-side plugin code at all, so the example tests in the plugin template currently have very little meaningful coverage. This adds an integration test to the example app when creating a plugin, so that there's an example of how to actually test that a complete round-trip plugin call works. This is done as a separate template that's currently only used by the plugin template because I don't know what a good example for a non-plugin case would be that isn't largely just a duplicate of the widget tests. However, the integration test pre-includes conditionals around the parts that are plugin-specific so that it can more easily be expanded to other use cases later (e.g., in flutter/flutter#68818). Part of flutter/flutter#82458 * Add integration test to expected dependencies of a plugin app * Test fixes * Make an explicit test case
1 parent b122200 commit f1d157b

File tree

8 files changed

+66
-2
lines changed

8 files changed

+66
-2
lines changed

dev/devicelab/bin/tasks/plugin_dependencies_test.dart

+4
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,10 @@ public class DummyPluginAClass {
215215
final List<dynamic> dependencyGraph = jsonContent['dependencyGraph'] as List<dynamic>;
216216
const String kExpectedPluginsDependenciesContent =
217217
'['
218+
'{'
219+
'"name":"integration_test",'
220+
'"dependencies":[]'
221+
'},'
218222
'{'
219223
'"name":"plugin_a",'
220224
'"dependencies":["plugin_b","plugin_c","plugin_d"]'

packages/flutter_tools/lib/src/commands/create.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ Your $application code is in $relativeAppMain.
595595
templateContext['androidPluginIdentifier'] = androidPluginIdentifier;
596596

597597
generatedCount += await generateApp(
598-
<String>['app', 'app_test_widget'],
598+
<String>['app', 'app_test_widget', 'app_integration_test'],
599599
project.example.directory,
600600
templateContext,
601601
overwrite: overwrite,

packages/flutter_tools/templates/app/pubspec.yaml.tmpl

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ dependencies:
6868
cupertino_icons: ^1.0.2
6969

7070
dev_dependencies:
71+
{{#withPlatformChannelPluginHook}}
72+
integration_test:
73+
sdk: flutter
74+
{{/withPlatformChannelPluginHook}}
7175
flutter_test:
7276
sdk: flutter
7377

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// This is a basic Flutter integration test.
2+
{{#withPlatformChannelPluginHook}}
3+
//
4+
// Since integration tests run in a full Flutter application, they can interact
5+
// with the host side of a plugin implementation, unlike Dart unit tests.
6+
{{/withPlatformChannelPluginHook}}
7+
//
8+
// For more information about Flutter integration tests, please see
9+
// https://docs.flutter.dev/cookbook/testing/integration/introduction
10+
11+
12+
import 'package:flutter_test/flutter_test.dart';
13+
import 'package:integration_test/integration_test.dart';
14+
15+
{{#withPlatformChannelPluginHook}}
16+
import 'package:{{pluginProjectName}}/{{pluginProjectName}}.dart';
17+
{{/withPlatformChannelPluginHook}}
18+
19+
void main() {
20+
IntegrationTestWidgetsFlutterBinding.ensureInitialized();
21+
{{#withPlatformChannelPluginHook}}
22+
23+
testWidgets('getPlatformVersion test', (WidgetTester tester) async {
24+
final {{pluginDartClass}} plugin = {{pluginDartClass}}();
25+
final String? version = await plugin.getPlatformVersion();
26+
// The version string depends on the host platform running the test, so
27+
// just assert that some non-empty string is returned.
28+
expect(version?.isNotEmpty, true);
29+
});
30+
{{/withPlatformChannelPluginHook}}
31+
}

packages/flutter_tools/templates/template_manifest.json

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@
141141

142142
"templates/app_test_widget/test/widget_test.dart.tmpl",
143143

144+
"templates/app_integration_test/integration_test/plugin_integration_test.dart.tmpl",
145+
144146
"templates/cocoapods/Podfile-ios-objc",
145147
"templates/cocoapods/Podfile-ios-swift",
146148
"templates/cocoapods/Podfile-macos",

packages/flutter_tools/test/commands.shard/hermetic/create_usage_test.dart

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ void main() {
7575
}
7676
final List<String> templatePaths = <String>[
7777
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'app'),
78+
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'app_integration_test'),
7879
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'app_shared'),
7980
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'app_test_widget'),
8081
globals.fs.path.join('flutter', 'packages', 'flutter_tools', 'templates', 'cocoapods'),

packages/flutter_tools/test/commands.shard/permeable/create_test.dart

+21
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,27 @@ void main() {
633633
),
634634
});
635635

636+
testUsingContext('plugin example app includes an integration test', () async {
637+
await _createAndAnalyzeProject(
638+
projectDir,
639+
<String>['--template=plugin'],
640+
<String>[
641+
'example/integration_test/plugin_integration_test.dart',
642+
],
643+
);
644+
return _runFlutterTest(projectDir.childDirectory('example'));
645+
}, overrides: <Type, Generator>{
646+
Pub: () => Pub.test(
647+
fileSystem: globals.fs,
648+
logger: globals.logger,
649+
processManager: globals.processManager,
650+
usage: globals.flutterUsage,
651+
botDetector: globals.botDetector,
652+
platform: globals.platform,
653+
stdio: mockStdio,
654+
),
655+
});
656+
636657
testUsingContext('kotlin/swift plugin project', () async {
637658
return _createProject(
638659
projectDir,

packages/flutter_tools/test/commands.shard/permeable/packages_test.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ void main() {
285285
final PackagesCommand command = await runCommandIn(exampleProjectPath, 'get');
286286
final PackagesGetCommand getCommand = command.subcommands['get']! as PackagesGetCommand;
287287

288-
expect((await getCommand.usageValues).commandPackagesNumberPlugins, 1);
288+
// A plugin example depends on the plugin itself, and integration_test.
289+
expect((await getCommand.usageValues).commandPackagesNumberPlugins, 2);
289290
}, overrides: <Type, Generator>{
290291
Stdio: () => mockStdio,
291292
Pub: () => Pub.test(

0 commit comments

Comments
 (0)