restoration_test.dart 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
// 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/widgets.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('restartAndRestore', (WidgetTester tester) async {
    await tester.pumpWidget(
      const RootRestorationScope(
        restorationId: 'root-child',
        child: _RestorableWidget(
          restorationId: 'restorable-widget',
        ),
      ),
    );

    final _RestorableWidgetState state = tester.state(find.byType(_RestorableWidget));
    expect(find.text('Hello World 100'), findsOneWidget);
    expect(state.doubleValue, 1.0);

    state.setValues('Guten Morgen', 200, 33.4);
    await tester.pump();

    expect(find.text('Guten Morgen 200'), findsOneWidget);
    expect(state.doubleValue, 33.4);

    await tester.restartAndRestore();

    expect(find.text('Guten Morgen 200'), findsOneWidget);
    expect(find.text('Hello World 100'), findsNothing);
    final _RestorableWidgetState restoredState = tester.state(find.byType(_RestorableWidget));
    expect(restoredState, isNot(same(state)));
    expect(restoredState.doubleValue, 1.0);
  });

  testWidgets('restore from previous restoration data', (WidgetTester tester) async {
    await tester.pumpWidget(
      const RootRestorationScope(
        restorationId: 'root-child',
        child: _RestorableWidget(
          restorationId: 'restorable-widget',
        ),
      ),
    );

    final _RestorableWidgetState state = tester.state(find.byType(_RestorableWidget));
    expect(find.text('Hello World 100'), findsOneWidget);
    expect(state.doubleValue, 1.0);

    state.setValues('Guten Morgen', 200, 33.4);
    await tester.pump();

    expect(find.text('Guten Morgen 200'), findsOneWidget);
    expect(state.doubleValue, 33.4);

    final TestRestorationData data = await tester.getRestorationData();

    state.setValues('See you later!', 400, 123.5);
    await tester.pump();

    expect(find.text('See you later! 400'), findsOneWidget);
    expect(state.doubleValue, 123.5);

    await tester.restoreFrom(data);

    expect(tester.state(find.byType(_RestorableWidget)), same(state));
    expect(find.text('Guten Morgen 200'), findsOneWidget);
    expect(state.doubleValue, 123.5);
  });
}

class _RestorableWidget extends StatefulWidget {
75
  const _RestorableWidget({this.restorationId});
76

77
  final String? restorationId;
78 79 80 81 82 83 84 85 86 87 88 89

  @override
  State<_RestorableWidget> createState() => _RestorableWidgetState();
}

class _RestorableWidgetState extends State<_RestorableWidget> with RestorationMixin {
  final RestorableString stringValue = RestorableString('Hello World');
  final RestorableInt intValue = RestorableInt(100);

  double doubleValue = 1.0; // Not restorable.

  @override
90
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
    registerForRestoration(stringValue, 'string');
    registerForRestoration(intValue, 'int');
  }

  void setValues(String s, int i, double d) {
    setState(() {
      stringValue.value = s;
      intValue.value = i;
      doubleValue = d;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Text('${stringValue.value} ${intValue.value}', textDirection: TextDirection.ltr);
  }

  @override
109
  String? get restorationId => widget.restorationId;
110
}