restoration.dart 3.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// 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/foundation.dart';
import 'package:flutter/services.dart';

/// The [RestorationManager] used for tests.
///
/// Unlike the real [RestorationManager], this one just keeps the restoration
/// data in memory and does not make it available to the engine.
class TestRestorationManager extends RestorationManager {
  /// Creates a [TestRestorationManager].
  TestRestorationManager() {
    // Ensures that [rootBucket] always returns a synchronous future to avoid
    // extra pumps in tests.
    restoreFrom(TestRestorationData.empty);
  }

  @override
21
  Future<RestorationBucket?> get rootBucket {
22 23 24 25 26 27 28 29 30 31 32 33 34 35
    _debugRootBucketAccessed = true;
    return super.rootBucket;
  }

  /// The current restoration data from which the current state can be restored.
  ///
  /// To restore the state to the one described by this data, pass the
  /// [TestRestorationData] obtained from this getter back to [restoreFrom].
  ///
  /// See also:
  ///
  ///  * [WidgetTester.getRestorationData], which makes this data available
  ///    in a widget test.
  TestRestorationData get restorationData => _restorationData;
36
  late TestRestorationData _restorationData;
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

  /// Whether the [rootBucket] has been obtained.
  bool get debugRootBucketAccessed => _debugRootBucketAccessed;
  bool _debugRootBucketAccessed = false;

  /// Restores the state from the provided [TestRestorationData].
  ///
  /// The restoration data obtained form [restorationData] can be passed into
  /// this method to restore the state to what it was when the restoration data
  /// was originally retrieved.
  ///
  /// See also:
  ///
  ///  * [WidgetTester.restoreFrom], which exposes this method to a widget test.
  void restoreFrom(TestRestorationData data) {
    _restorationData = data;
    handleRestorationUpdateFromEngine(enabled: true, data: data.binary);
  }

56 57 58 59
  /// Disabled state restoration.
  ///
  /// To turn restoration back on call [restoreFrom].
  void disableRestoration() {
60
    _restorationData = TestRestorationData.empty;
61
    handleRestorationUpdateFromEngine(enabled: false, data: null);
62 63
  }

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
  @override
  Future<void> sendToEngine(Uint8List encodedData) async {
    _restorationData = TestRestorationData._(encodedData);
  }
}

/// Restoration data that can be used to restore the state to the one described
/// by this data.
///
/// See also:
///
///  * [WidgetTester.getRestorationData], which retrieves the restoration data
///    from the widget under test.
///  * [WidgetTester.restoreFrom], which takes a [TestRestorationData] to
///    restore the state of the widget under test from the provided data.
class TestRestorationData {
  const TestRestorationData._(this.binary);

  /// Empty restoration data indicating that no data is available to restore
  /// state from.
  static const TestRestorationData empty = TestRestorationData._(null);

  /// The serialized representation of the restoration data.
  ///
  /// Should only be accessed by the test framework.
  @protected
90
  final Uint8List? binary;
91
}