ShortcutManager should dispatch creation in constructor. (#133487)

This commit is contained in:
Polina Cherkasova 2023-08-29 10:40:20 -07:00 committed by GitHub
parent d8d7e019c1
commit ae64abc614
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 9 deletions

View File

@ -747,7 +747,11 @@ class ShortcutManager with Diagnosticable, ChangeNotifier {
ShortcutManager({
Map<ShortcutActivator, Intent> shortcuts = const <ShortcutActivator, Intent>{},
this.modal = false,
}) : _shortcuts = shortcuts;
}) : _shortcuts = shortcuts {
if (kFlutterMemoryAllocationsEnabled) {
maybeDispatchObjectCreation();
}
}
/// True if the [ShortcutManager] should not pass on keys that it doesn't
/// handle to any key-handling widgets that are ancestors to this one.
@ -1440,6 +1444,7 @@ class _ShortcutRegistrarState extends State<ShortcutRegistrar> {
void dispose() {
registry.removeListener(_shortcutsChanged);
registry.dispose();
manager.dispose();
super.dispose();
}

View File

@ -177,8 +177,6 @@ void main() {
'ValueNotifier<bool>': 2,
'_InputBorderGap': 1,
},
// TODO(polina-c): investigate notGCed, if it does not disappear after fixing notDisposed.
allowAllNotGCed: true,
),
);

View File

@ -666,6 +666,19 @@ void main() {
expect(pressedKeys, isEmpty);
});
test('$ShortcutManager dispatches object creation in constructor', () {
int eventCount = 0;
void listener(ObjectEvent event) => eventCount++;
MemoryAllocations.instance.addListener(listener);
final ShortcutManager registry = ShortcutManager();
expect(eventCount, 1);
registry.dispose();
MemoryAllocations.instance.removeListener(listener);
});
testWidgets("Shortcuts passes to the next Shortcuts widget if it doesn't map the key", (WidgetTester tester) async {
final GlobalKey containerKey = GlobalKey();
final List<LogicalKeyboardKey> pressedKeys = <LogicalKeyboardKey>[];
@ -1853,20 +1866,17 @@ void main() {
token.dispose();
});
testWidgets('dispatches object creation in constructor', (WidgetTester tester) async {
final MemoryAllocations ma = MemoryAllocations.instance;
assert(!ma.hasListeners);
test('dispatches object creation in constructor', () {
int eventCount = 0;
void listener(ObjectEvent event) => eventCount++;
ma.addListener(listener);
MemoryAllocations.instance.addListener(listener);
final ShortcutRegistry registry = ShortcutRegistry();
expect(eventCount, 1);
registry.dispose();
ma.removeListener(listener);
assert(!ma.hasListeners);
MemoryAllocations.instance.removeListener(listener);
});
});
}