From b3d1ebf117589c90a0fc09e76920dc80d92d4dc8 Mon Sep 17 00:00:00 2001 From: Ian Hickson Date: Fri, 11 Sep 2020 16:30:05 -0700 Subject: [PATCH] Move the registration of the restoration channel to binding initialization. (#65579) --- .../flutter/lib/src/services/restoration.dart | 22 ++++++++++++++++--- .../test/services/channel_buffers_test.dart | 3 +-- .../flutter/test/services/restoration.dart | 10 +++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/packages/flutter/lib/src/services/restoration.dart b/packages/flutter/lib/src/services/restoration.dart index da33b0aadd..42e33eec12 100644 --- a/packages/flutter/lib/src/services/restoration.dart +++ b/packages/flutter/lib/src/services/restoration.dart @@ -97,6 +97,25 @@ typedef _BucketVisitor = void Function(RestorationBucket bucket); /// * [RestorationMixin], which uses [RestorationBucket]s behind the scenes /// to make [State] objects of [StatefulWidget]s restorable. class RestorationManager extends ChangeNotifier { + /// Construct the restoration manager and set up the communications channels + /// with the engine to get restoration messages (by calling [initChannels]). + RestorationManager() { + initChannels(); + } + + /// Sets up the method call handler for [SystemChannels.restoration]. + /// + /// This is called by the constructor to configure the communications channel + /// with the Flutter engine to get restoration messages. + /// + /// Subclasses (especially in tests) can override this to avoid setting up + /// that communications channel, or to set it up differently, as necessary. + @protected + void initChannels() { + assert(!SystemChannels.restoration.checkMethodCallHandler(_methodHandler)); + SystemChannels.restoration.setMethodCallHandler(_methodHandler); + } + /// The root of the [RestorationBucket] hierarchy containing the restoration /// data. /// @@ -128,9 +147,6 @@ class RestorationManager extends ChangeNotifier { /// * [RootRestorationScope], which makes the root bucket available in the /// [Widget] tree. Future get rootBucket { - if (!SystemChannels.restoration.checkMethodCallHandler(_methodHandler)) { - SystemChannels.restoration.setMethodCallHandler(_methodHandler); - } if (_rootBucketIsValid) { return SynchronousFuture(_rootBucket); } diff --git a/packages/flutter/test/services/channel_buffers_test.dart b/packages/flutter/test/services/channel_buffers_test.dart index f6714140a4..ca9c8ab8eb 100644 --- a/packages/flutter/test/services/channel_buffers_test.dart +++ b/packages/flutter/test/services/channel_buffers_test.dart @@ -15,8 +15,7 @@ import 'package:flutter/scheduler.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; -class TestChannelBuffersFlutterBinding extends BindingBase with SchedulerBinding, ServicesBinding { -} +class TestChannelBuffersFlutterBinding extends BindingBase with SchedulerBinding, ServicesBinding { } void main() { ByteData _makeByteData(String str) { diff --git a/packages/flutter/test/services/restoration.dart b/packages/flutter/test/services/restoration.dart index f66625ac28..a12945b4b6 100644 --- a/packages/flutter/test/services/restoration.dart +++ b/packages/flutter/test/services/restoration.dart @@ -10,11 +10,21 @@ import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; class MockRestorationManager extends TestRestorationManager { + MockRestorationManager({ this.enableChannels = false }); + bool get updateScheduled => _updateScheduled; bool _updateScheduled = false; final List _buckets = []; + final bool enableChannels; + + @override + void initChannels() { + if (enableChannels) + super.initChannels(); + } + @override void scheduleSerializationFor(RestorationBucket bucket) { _updateScheduled = true;