Skip to content

Instantly share code, notes, and snippets.

@abhaysood
Last active September 29, 2021 06:37
Show Gist options
  • Save abhaysood/68d3fc64630131f164632e1845d306f5 to your computer and use it in GitHub Desktop.
Save abhaysood/68d3fc64630131f164632e1845d306f5 to your computer and use it in GitHub Desktop.
Trigger semantics action in widget tests (medium blog)
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