Skip to content

Commit 35d9502

Browse files
hgracebvictorsanni
andauthored
Disposes test restoration manager when accessed by bindings (#176519)
Fixes #169119 ## Details The error caused by drag is due to the reference to `ServicesBinding.instance.restorationManager` in the `ScrollableState.saveOffset` method. https://github.com/flutter/flutter/blob/018897e3f12c5783fb025bcbd92515a69d4d5c32/packages/flutter/lib/src/widgets/scrollable.dart#L648-L656 The TestWidgetsFlutterBinding now only cleans up the `restorationManager` when `reset` is called. Therefore, we need to find an appropriate place to clean up the `restorationManager`. https://github.com/flutter/flutter/blob/018897e3f12c5783fb025bcbd92515a69d4d5c32/packages/flutter_test/lib/src/binding.dart#L239-L263 https://github.com/flutter/flutter/blob/018897e3f12c5783fb025bcbd92515a69d4d5c32/packages/flutter_test/lib/src/widget_tester.dart#L188-L193 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. --------- Co-authored-by: Victor Sanni <victorsanniay@gmail.com>
1 parent 101ace2 commit 35d9502

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

packages/flutter_test/lib/src/binding.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,8 @@ abstract class TestWidgetsFlutterBinding extends BindingBase
10671067
Container(key: UniqueKey(), child: _postTestMessage),
10681068
); // Unmount any remaining widgets.
10691069
await pump();
1070+
_restorationManager?.dispose();
1071+
_restorationManager = null;
10701072
if (registerTestTextInput) {
10711073
_testTextInput.unregister();
10721074
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter/services.dart';
6+
import 'package:flutter/widgets.dart';
7+
import 'package:flutter_test/flutter_test.dart';
8+
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
9+
10+
void main() {
11+
LeakTesting.enable(); // Enable leak testing and use default collectedLeaksReporter.
12+
13+
// Regression test for https://github.com/flutter/flutter/issues/169119.
14+
testWidgets('Does not leak if restorationManager is accessed', (WidgetTester tester) async {
15+
int counterByWidgets = 0;
16+
final RestorationManager managerByWidgets = WidgetsBinding.instance.restorationManager;
17+
expect(managerByWidgets, isA<TestRestorationManager>());
18+
managerByWidgets.addListener(() => counterByWidgets++);
19+
managerByWidgets.notifyListeners();
20+
expect(counterByWidgets, 1);
21+
22+
int counterByServices = 0;
23+
final RestorationManager managerByServices = ServicesBinding.instance.restorationManager;
24+
expect(managerByServices, isA<TestRestorationManager>());
25+
managerByServices.addListener(() => counterByServices++);
26+
managerByServices.notifyListeners();
27+
expect(counterByServices, 1);
28+
});
29+
}

0 commit comments

Comments
 (0)