page_storage_test.dart 2.94 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'package:flutter/material.dart';
6
import 'package:flutter_test/flutter_test.dart';
7
import 'package:leak_tracker_flutter_testing/leak_tracker_flutter_testing.dart';
8 9

void main() {
10
  testWidgetsWithLeakTracking('PageStorage read and write', (WidgetTester tester) async {
11
    const Key builderKey = PageStorageKey<String>('builderKey');
12
    late StateSetter setState;
13 14 15
    int storedValue = 0;

    await tester.pumpWidget(
16 17
      MaterialApp(
        home: StatefulBuilder(
18 19
          key: builderKey,
          builder: (BuildContext context, StateSetter setter) {
20
            PageStorage.of(context).writeState(context, storedValue);
21
            setState = setter;
22
            return Center(
23
              child: Text('storedValue: $storedValue'),
24
            );
25 26
          },
        ),
27
      ),
28 29
    );

30
    final Element builderElement = tester.element(find.byKey(builderKey));
31
    expect(PageStorage.of(builderElement), isNotNull);
32
    expect(PageStorage.of(builderElement).readState(builderElement), equals(storedValue));
33 34 35 36 37

    setState(() {
      storedValue = 1;
    });
    await tester.pump();
38
    expect(PageStorage.of(builderElement).readState(builderElement), equals(storedValue));
39 40
  });

41
  testWidgetsWithLeakTracking('PageStorage read and write by identifier', (WidgetTester tester) async {
42
    late StateSetter setState;
43 44 45
    int storedValue = 0;

    Widget buildWidthKey(Key key) {
46 47
      return MaterialApp(
        home: StatefulBuilder(
48 49
          key: key,
          builder: (BuildContext context, StateSetter setter) {
50
            PageStorage.of(context).writeState(context, storedValue, identifier: 123);
51
            setState = setter;
52
            return Center(
53
              child: Text('storedValue: $storedValue'),
54
            );
55 56
          },
        ),
57 58 59
      );
    }

60
    Key key = const Key('Key one');
61 62 63
    await tester.pumpWidget(buildWidthKey(key));
    Element builderElement = tester.element(find.byKey(key));
    expect(PageStorage.of(builderElement), isNotNull);
64 65
    expect(PageStorage.of(builderElement).readState(builderElement), isNull);
    expect(PageStorage.of(builderElement).readState(builderElement, identifier: 123), equals(storedValue));
66 67 68

    // New StatefulBuilder widget - different key - but the same PageStorage identifier.

69
    key = const Key('Key two');
70 71 72
    await tester.pumpWidget(buildWidthKey(key));
    builderElement = tester.element(find.byKey(key));
    expect(PageStorage.of(builderElement), isNotNull);
73 74
    expect(PageStorage.of(builderElement).readState(builderElement), isNull);
    expect(PageStorage.of(builderElement).readState(builderElement, identifier: 123), equals(storedValue));
75 76 77 78 79

    setState(() {
      storedValue = 1;
    });
    await tester.pump();
80
    expect(PageStorage.of(builderElement).readState(builderElement, identifier: 123), equals(storedValue));
81 82 83
  });

}