Add test for focus_scope.0.dart (#157772)

Contributes to https://github.com/flutter/flutter/issues/130459

It adds a test for
- `examples/api/lib/widgets/focus_scope/focus_scope.0.dart`
This commit is contained in:
Valentin Vignal 2024-10-29 17:02:01 +08:00 committed by GitHub
parent 55fa5ae07f
commit 2de487308a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 58 additions and 1 deletions

View File

@ -325,5 +325,4 @@ final Set<String> _knownMissingTests = <String>{
'examples/api/test/widgets/notification_listener/notification.0_test.dart',
'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.1_test.dart',
'examples/api/test/widgets/overscroll_indicator/glowing_overscroll_indicator.0_test.dart',
'examples/api/test/widgets/focus_scope/focus_scope.0_test.dart',
};

View File

@ -0,0 +1,58 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_api_samples/widgets/focus_scope/focus_scope.0.dart'
as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
bool hasFocus(WidgetTester tester, IconData icon) =>
tester.widget<IconButton>(
find.ancestor(
of: find.byIcon(icon),
matching: find.byType(IconButton),
),
).focusNode!.hasFocus;
testWidgets('The focus is restricted to the foreground', (WidgetTester tester) async {
await tester.pumpWidget(
const example.FocusScopeExampleApp(),
);
expect(find.text('FOREGROUND'), findsOne);
expect(hasFocus(tester, Icons.menu), true);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pump();
expect(hasFocus(tester, Icons.menu), true);
});
testWidgets('The background can be focused', (WidgetTester tester) async {
await tester.pumpWidget(
const example.FocusScopeExampleApp(),
);
expect(find.text('FOREGROUND'), findsOne);
expect(hasFocus(tester, Icons.menu), true);
await tester.tap(find.byIcon(Icons.menu));
await tester.pumpAndSettle();
expect(hasFocus(tester, Icons.close), true);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pump();
expect(hasFocus(tester, Icons.menu), false);
expect(hasFocus(tester, Icons.close), false);
await tester.sendKeyEvent(LogicalKeyboardKey.tab);
await tester.pump();
expect(hasFocus(tester, Icons.close), true);
});
}