Last active
September 29, 2021 06:37
-
-
Save abhaysood/68d3fc64630131f164632e1845d306f5 to your computer and use it in GitHub Desktop.
Trigger semantics action in widget tests (medium blog)
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
import 'package:bug/main.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter/rendering.dart'; | |
import 'package:flutter_test/flutter_test.dart'; | |
void main() { | |
testWidgets('Tap action', (WidgetTester tester) async { | |
await tester.pumpWidget(MyApp()); | |
final helloWorldTextFinder = find.text("Hello World!"); | |
final semantics = tester.getSemantics(helloWorldTextFinder); | |
tester.binding.pipelineOwner.semanticsOwner! | |
.performAction(semantics.id, SemanticsAction.tap); | |
await tester.pump(const Duration(seconds: 1)); | |
expect(find.byType(SnackBar), findsOneWidget); | |
}); | |
testWidgets('Gain focus action', (WidgetTester tester) async { | |
await tester.pumpWidget(MyApp()); | |
final helloWorldTextFinder = find.text("Hello World!"); | |
final semantics = tester.getSemantics(helloWorldTextFinder); | |
tester.binding.pipelineOwner.semanticsOwner! | |
.performAction(semantics.id, SemanticsAction.didGainAccessibilityFocus); | |
await tester.pump(const Duration(seconds: 1)); | |
expect(find.byType(SnackBar), findsOneWidget); | |
}); | |
testWidgets('Lose focus action', (WidgetTester tester) async { | |
await tester.pumpWidget(MyApp()); | |
final helloWorldTextFinder = find.text("Hello World!"); | |
final semantics = tester.getSemantics(helloWorldTextFinder); | |
tester.binding.pipelineOwner.semanticsOwner! | |
.performAction(semantics.id, SemanticsAction.didLoseAccessibilityFocus); | |
await tester.pump(const Duration(seconds: 1)); | |
expect(find.byType(SnackBar), findsOneWidget); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment